CGCall.cpp revision 97cb5a4a21866610227963fc3dcce9d89b2f7990
10dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===// 20dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// 30dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// The LLVM Compiler Infrastructure 40dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// 50dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// This file is distributed under the University of Illinois Open Source 60dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// License. See LICENSE.TXT for details. 70dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// 80dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===// 90dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// 100dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// These classes wrap the information about a call or function 110dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// definition used to handle ABI compliancy. 120dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// 130dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===// 140dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 150dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h" 164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall#include "CGCXXABI.h" 17ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner#include "ABIInfo.h" 180dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CodeGenFunction.h" 19b768807c49a1c7085def099b848631856af766faDaniel Dunbar#include "CodeGenModule.h" 206b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar#include "clang/Basic/TargetInfo.h" 210dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/Decl.h" 22f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson#include "clang/AST/DeclCXX.h" 230dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/DeclObjC.h" 2406057cef0bcd7804e80f3ce2bbe352178396c715Chandler Carruth#include "clang/Frontend/CodeGenOptions.h" 25d0646bd7c11c12b34971b55e5c1bdd8439401b4cDevang Patel#include "llvm/Attributes.h" 26d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar#include "llvm/Support/CallSite.h" 2754d1ccbfcf19ddf39444f1b4018dd79487cc847bDaniel Dunbar#include "llvm/Target/TargetData.h" 2897cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman#include "llvm/Transforms/Utils/Local.h" 290dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace clang; 300dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace CodeGen; 310dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 320dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar/***/ 330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 3404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { 3504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall switch (CC) { 3604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall default: return llvm::CallingConv::C; 3704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; 3804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; 39f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; 40414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; 41414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 4252fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik // TODO: add support for CC_X86Pascal to llvm 4304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall } 4404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall} 4504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall 460b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Derives the 'this' type for codegen purposes, i.e. ignoring method 470b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualification. 480b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// FIXME: address space qualification? 49ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { 50ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); 51ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); 520b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 530b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 540b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the canonical formal type of the given C++ method. 55ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { 56ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return MD->getType()->getCanonicalTypeUnqualified() 57ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall .getAs<FunctionProtoType>(); 580b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 590b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 600b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the "extra-canonicalized" return type, which discards 610b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualifiers on the return type. Codegen doesn't care about them, 620b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// and it makes ABI code a little easier to be able to assume that 630b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// all parameter and return types are top-level unqualified. 64ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetReturnType(QualType RetTy) { 65ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); 660b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 670b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 680b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo & 69bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris LattnerCodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP, 70bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner bool IsRecursive) { 71ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(), 72ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16>(), 73bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner FTNP->getExtInfo(), IsRecursive); 7445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar} 7545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 760b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// \param Args - contains any initial parameters besides those 770b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// in the formal type 780b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallstatic const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT, 79ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVectorImpl<CanQualType> &ArgTys, 80bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner CanQual<FunctionProtoType> FTP, 81bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner bool IsRecursive = false) { 82541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy. 8345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 84541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar ArgTys.push_back(FTP->getArgType(i)); 85ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall CanQualType ResTy = FTP->getResultType().getUnqualifiedType(); 869c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive); 870b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 880b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 890b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo & 90bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris LattnerCodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP, 91bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner bool IsRecursive) { 92ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16> ArgTys; 939c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive); 94bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar} 95bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar 9604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic CallingConv getCallingConventionForDecl(const Decl *D) { 97bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar // Set the appropriate calling convention for the Function. 98bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar if (D->hasAttr<StdCallAttr>()) 9904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall return CC_X86StdCall; 100bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar 101bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar if (D->hasAttr<FastCallAttr>()) 10204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall return CC_X86FastCall; 103bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar 104f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor if (D->hasAttr<ThisCallAttr>()) 105f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor return CC_X86ThisCall; 106f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor 10752fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik if (D->hasAttr<PascalAttr>()) 10852fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik return CC_X86Pascal; 10952fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik 110414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov if (PcsAttr *PCS = D->getAttr<PcsAttr>()) 111414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); 112414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov 11304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall return CC_C; 11445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar} 11545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 116375c31c4673f83f925de221752cf801c2fbbb246Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD, 117375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson const FunctionProtoType *FTP) { 118ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16> ArgTys; 1190b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 120375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson // Add the 'this' pointer. 1210b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall ArgTys.push_back(GetThisType(Context, RD)); 1220b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 1230b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall return ::getFunctionInfo(*this, ArgTys, 1249c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); 125375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson} 126375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson 127f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) { 128ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16> ArgTys; 1290b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 130fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!"); 131fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); 132fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall 1333eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner // Add the 'this' pointer unless this is a static method. 1343eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner if (MD->isInstance()) 1350b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall ArgTys.push_back(GetThisType(Context, MD->getParent())); 1361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1379c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD)); 138f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson} 139f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson 1409cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencerconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D, 141f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXCtorType Type) { 142ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16> ArgTys; 1430b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall ArgTys.push_back(GetThisType(Context, D->getParent())); 1444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType ResTy = Context.VoidTy; 145f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 1464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys); 1470b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 1484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQual<FunctionProtoType> FTP = GetFormalType(D); 1494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Add the formal parameters. 1514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 1524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall ArgTys.push_back(FTP->getArgType(i)); 1534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1549c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); 155f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson} 156f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 157f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D, 158f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson CXXDtorType Type) { 1594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall llvm::SmallVector<CanQualType, 2> ArgTys; 160ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall ArgTys.push_back(GetThisType(Context, D->getParent())); 1614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQualType ResTy = Context.VoidTy; 1624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys); 1640b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 1654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQual<FunctionProtoType> FTP = GetFormalType(D); 1664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall assert(FTP->getNumArgs() == 0 && "dtor with formal parameters"); 1674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1689c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo()); 169f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson} 170f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 171541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) { 1723eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 173f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson if (MD->isInstance()) 174f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson return getFunctionInfo(MD); 1751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 176ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); 177ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall assert(isa<FunctionType>(FTy)); 1780b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall if (isa<FunctionNoProtoType>(FTy)) 1799cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer return getFunctionInfo(FTy.getAs<FunctionNoProtoType>()); 180ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall assert(isa<FunctionProtoType>(FTy)); 181ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return getFunctionInfo(FTy.getAs<FunctionProtoType>()); 1820dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 1830dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 184541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) { 185ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16> ArgTys; 186ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType())); 187ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); 188541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy? 1892073216a1075767b5d25c23d1462b7034686d94dChris Lattner for (ObjCMethodDecl::param_iterator i = MD->param_begin(), 1900b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall e = MD->param_end(); i != e; ++i) { 1910b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall ArgTys.push_back(Context.getCanonicalParamType((*i)->getType())); 1920b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall } 1930b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall return getFunctionInfo(GetReturnType(MD->getResultType()), 1940b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall ArgTys, 195264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola FunctionType::ExtInfo( 196264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola /*NoReturn*/ false, 197a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman /*HasRegParm*/ false, 198425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola /*RegParm*/ 0, 199264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola getCallingConventionForDecl(MD))); 2000dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 2010dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 202b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) { 203b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson // FIXME: Do we need to handle ObjCMethodDecl? 204b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 2059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 206b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) 207b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson return getFunctionInfo(CD, GD.getCtorType()); 208b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson 209b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) 210b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson return getFunctionInfo(DD, GD.getDtorType()); 2119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 212b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson return getFunctionInfo(FD); 213b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson} 214b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson 2151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, 216bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar const CallArgList &Args, 217264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola const FunctionType::ExtInfo &Info) { 218541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy. 219ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16> ArgTys; 2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (CallArgList::const_iterator i = Args.begin(), e = Args.end(); 221725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar i != e; ++i) 222c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman ArgTys.push_back(Context.getCanonicalParamType(i->Ty)); 223264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); 2240dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 2250dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy, 227bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar const FunctionArgList &Args, 228264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola const FunctionType::ExtInfo &Info) { 229541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy. 230ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall llvm::SmallVector<CanQualType, 16> ArgTys; 2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 232bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar i != e; ++i) 233d26bc76c98006609002d9930f8840490e88ac5b5John McCall ArgTys.push_back(Context.getCanonicalParamType((*i)->getType())); 234264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info); 235541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar} 236541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar 237d26bc76c98006609002d9930f8840490e88ac5b5John McCallconst CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() { 238d26bc76c98006609002d9930f8840490e88ac5b5John McCall llvm::SmallVector<CanQualType, 1> args; 239d26bc76c98006609002d9930f8840490e88ac5b5John McCall return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo()); 240d26bc76c98006609002d9930f8840490e88ac5b5John McCall} 241d26bc76c98006609002d9930f8840490e88ac5b5John McCall 242ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy, 243ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall const llvm::SmallVectorImpl<CanQualType> &ArgTys, 244bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner const FunctionType::ExtInfo &Info, 245bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner bool IsRecursive) { 246ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#ifndef NDEBUG 247ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall for (llvm::SmallVectorImpl<CanQualType>::const_iterator 248ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I) 249ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall assert(I->isCanonicalAsParam()); 250ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#endif 251ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall 252425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC()); 25304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall 25440a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar // Lookup or create unique function info. 25540a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar llvm::FoldingSetNodeID ID; 256264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola CGFunctionInfo::Profile(ID, Info, ResTy, 257bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar ArgTys.begin(), ArgTys.end()); 25840a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar 25940a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar void *InsertPos = 0; 26040a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos); 26140a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar if (FI) 26240a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar return *FI; 26340a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar 26488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar // Construct the function info. 265a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getHasRegParm(), Info.getRegParm(), ResTy, 2669c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller ArgTys.data(), ArgTys.size()); 26735e67d4387bbe3e7e17ee6b17eaa42eebb0eb9f1Daniel Dunbar FunctionInfos.InsertNode(FI, InsertPos); 268541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar 26988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar // Compute ABI information. 270ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner getABIInfo().computeInfo(*FI); 2719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 272800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Loop over all of the computed argument and return value info. If any of 273800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // them are direct or extend without a specified coerce type, specify the 274800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // default now. 275800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner ABIArgInfo &RetInfo = FI->getReturnInfo(); 276800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0) 277800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType())); 2789cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 279800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end(); 280800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner I != E; ++I) 281800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0) 282800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner I->info.setCoerceToType(ConvertTypeRecursive(I->type)); 283541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar 284a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer 285a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner // types, resolve them now. These pointers may point to this function, which 286a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner // we *just* filled in the FunctionInfo for. 287ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner if (!IsRecursive && !PointersToResolve.empty()) 288a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner HandleLateResolvedPointers(); 2899cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 29088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar return *FI; 2910dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 29217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 293bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel DunbarCGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention, 294a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman bool _NoReturn, bool _HasRegParm, unsigned _RegParm, 295ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall CanQualType ResTy, 296bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner const CanQualType *ArgTys, 297bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner unsigned NumArgTys) 298ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar : CallingConvention(_CallingConvention), 29904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall EffectiveCallingConvention(_CallingConvention), 300a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman NoReturn(_NoReturn), HasRegParm(_HasRegParm), RegParm(_RegParm) 301bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar{ 302bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner NumArgs = NumArgTys; 3039cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 304ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // FIXME: Coallocate with the CGFunctionInfo object. 305bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner Args = new ArgInfo[1 + NumArgTys]; 30688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar Args[0].type = ResTy; 307bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner for (unsigned i = 0; i != NumArgTys; ++i) 30888c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar Args[1 + i].type = ArgTys[i]; 30988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar} 31088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar 31188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar/***/ 31288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar 31342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCallvoid CodeGenTypes::GetExpandedTypes(QualType type, 31442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall llvm::SmallVectorImpl<const llvm::Type*> &expandedTypes, 31542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall bool isRecursive) { 31642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const RecordType *RT = type->getAsStructureType(); 3175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar assert(RT && "Can only expand structure types."); 3185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar const RecordDecl *RD = RT->getDecl(); 3191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!RD->hasFlexibleArrayMember() && 3205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar "Cannot expand structure with flexible array."); 3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 32317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i != e; ++i) { 3245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar const FieldDecl *FD = *i; 3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(!FD->isBitField() && 3265627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar "Cannot expand structure with bit-field members."); 3271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 32842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall QualType fieldType = FD->getType(); 32942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall if (fieldType->isRecordType()) 33042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall GetExpandedTypes(fieldType, expandedTypes, isRecursive); 331deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner else 33242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall expandedTypes.push_back(ConvertType(fieldType, isRecursive)); 3335627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 3345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar} 3355627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 3361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function::arg_iterator 3375627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, 3385627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar llvm::Function::arg_iterator AI) { 3395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar const RecordType *RT = Ty->getAsStructureType(); 3405627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar assert(RT && "Can only expand structure types."); 3415627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 3425627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar RecordDecl *RD = RT->getDecl(); 3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(LV.isSimple() && 3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump "Unexpected non-simple lvalue during struct expansion."); 3455627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar llvm::Value *Addr = LV.getAddress(); 34617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 34717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i != e; ++i) { 3481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FieldDecl *FD = *i; 3495627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar QualType FT = FD->getType(); 3505627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 3515627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar // FIXME: What are the right qualifiers here? 352e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson LValue LV = EmitLValueForField(Addr, FD, 0); 3535627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar if (CodeGenFunction::hasAggregateLLVMType(FT)) { 3545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar AI = ExpandTypeFromArgs(FT, LV, AI); 3555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } else { 3565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar EmitStoreThroughLValue(RValue::get(AI), LV, FT); 3575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar ++AI; 3585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 3595627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 3605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 3615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar return AI; 3625627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar} 3635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid 3651eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, 3665627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar llvm::SmallVector<llvm::Value*, 16> &Args) { 3675627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar const RecordType *RT = Ty->getAsStructureType(); 3685627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar assert(RT && "Can only expand structure types."); 3695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 3705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar RecordDecl *RD = RT->getDecl(); 3715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); 3725627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar llvm::Value *Addr = RV.getAggregateAddr(); 37317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 37417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis i != e; ++i) { 3751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump FieldDecl *FD = *i; 3765627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar QualType FT = FD->getType(); 3771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3785627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar // FIXME: What are the right qualifiers here? 379e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson LValue LV = EmitLValueForField(Addr, FD, 0); 3805627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar if (CodeGenFunction::hasAggregateLLVMType(FT)) { 3815627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args); 3825627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } else { 3835627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar RValue RV = EmitLoadOfLValue(LV, FT); 3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(RV.isScalar() && 3855627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar "Unexpected non-scalar rvalue during struct expansion."); 3865627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar Args.push_back(RV.getScalarVal()); 3875627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 3885627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 3895627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar} 3905627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 391e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are 39208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// accessing some number of bytes out of it, try to gep into the struct to get 39308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// at its inner goodness. Dive as deep as possible without entering an element 39408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// with an in-memory size smaller than DstSize. 39508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattnerstatic llvm::Value * 396e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris LattnerEnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, 397e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner const llvm::StructType *SrcSTy, 398e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner uint64_t DstSize, CodeGenFunction &CGF) { 39908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // We can't dive into a zero-element struct. 40008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner if (SrcSTy->getNumElements() == 0) return SrcPtr; 4019cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 40208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner const llvm::Type *FirstElt = SrcSTy->getElementType(0); 4039cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 40408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // If the first elt is at least as large as what we're looking for, or if the 40508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // first element is the same size as the whole struct, we can enter it. 4069cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer uint64_t FirstEltSize = 40708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner CGF.CGM.getTargetData().getTypeAllocSize(FirstElt); 4089cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer if (FirstEltSize < DstSize && 40908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy)) 41008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner return SrcPtr; 4119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 41208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // GEP into the first element. 41308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); 4149cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 41508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // If the first element is a struct, recurse. 41608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner const llvm::Type *SrcTy = 41708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 41808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) 419e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 42008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 42108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner return SrcPtr; 42208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner} 42308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 4246d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both 4256d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// are either integers or pointers. This does a truncation of the value if it 4266d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// is too large or a zero extension if it is too small. 4276d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattnerstatic llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, 4286d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner const llvm::Type *Ty, 4296d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner CodeGenFunction &CGF) { 4306d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (Val->getType() == Ty) 4316d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return Val; 4329cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 4336d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(Val->getType())) { 4346d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // If this is Pointer->Pointer avoid conversion to and from int. 4356d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(Ty)) 4366d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); 4379cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 4386d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // Convert the pointer to an integer so we can play with its width. 43977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); 4406d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner } 4419cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 4426d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner const llvm::Type *DestIntTy = Ty; 4436d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(DestIntTy)) 44477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner DestIntTy = CGF.IntPtrTy; 4459cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 4466d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (Val->getType() != DestIntTy) 4476d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); 4489cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 4496d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(Ty)) 4506d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); 4516d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return Val; 4526d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner} 4536d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner 45408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 45508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 456275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as 457275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// a pointer to an object of type \arg Ty. 458275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// 459275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is smaller than the 460275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; in this situation the values of bits which not 461275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// present in the src are undefined. 462275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, 463275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar const llvm::Type *Ty, 464275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CodeGenFunction &CGF) { 4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::Type *SrcTy = 466275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 4679cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 4686ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner // If SrcTy and Ty are the same, just do a load. 4696ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner if (SrcTy == Ty) 4706ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner return CGF.Builder.CreateLoad(SrcPtr); 4719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 4729408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); 4739cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 47408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { 475e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 47608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 47708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner } 4789cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 47908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 480275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 4816d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // If the source and destination are integer or pointer types, just do an 4826d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // extension or truncation to the desired type. 4836d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && 4846d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { 4856d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); 4866d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); 4876d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner } 4889cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 489b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar // If load is legal, just bitcast the src pointer. 4907ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar if (SrcSize >= DstSize) { 491f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // Generally SrcSize is never greater than DstSize, since this means we are 492f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // losing bits. However, this can happen in cases where the structure has 493f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // additional padding, for example due to a user specified alignment. 4947ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar // 495f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: Assert that we aren't truncating non-padding bits when have access 496f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // to that information. 497275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *Casted = 498275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); 499386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 500386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar // FIXME: Use better alignment / avoid requiring aligned load. 501386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar Load->setAlignment(1); 502386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar return Load; 503275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar } 5049cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 50535b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner // Otherwise do coercion through memory. This is stupid, but 50635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner // simple. 50735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); 50835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner llvm::Value *Casted = 50935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); 51035b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner llvm::StoreInst *Store = 51135b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); 51235b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner // FIXME: Use better alignment / avoid requiring aligned store. 51335b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner Store->setAlignment(1); 51435b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner return CGF.Builder.CreateLoad(Tmp); 515275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar} 516275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 517badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// Function to store a first-class aggregate into memory. We prefer to 518badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// store the elements rather than the aggregate to be more friendly to 519badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// fast-isel. 520badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// FIXME: Do we need to recurse here? 521badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedmanstatic void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, 522badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::Value *DestPtr, bool DestIsVolatile, 523badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman bool LowAlignment) { 524badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman // Prefer scalar stores to first-class aggregate stores. 525badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman if (const llvm::StructType *STy = 526badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman dyn_cast<llvm::StructType>(Val->getType())) { 527badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 528badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); 529badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); 530badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, 531badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman DestIsVolatile); 532badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman if (LowAlignment) 533badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman SI->setAlignment(1); 534badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman } 535badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman } else { 536badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); 537badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman } 538badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman} 539badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman 540275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, 541275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// where the source and destination may have different types. 542275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// 543275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is larger than the 544275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; the upper bits of the src will be lost. 545275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic void CreateCoercedStore(llvm::Value *Src, 546275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *DstPtr, 547d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson bool DstIsVolatile, 548275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CodeGenFunction &CGF) { 549275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar const llvm::Type *SrcTy = Src->getType(); 5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const llvm::Type *DstTy = 551275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 5526ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner if (SrcTy == DstTy) { 5536ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 5546ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner return; 5556ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner } 5569cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 5576ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 5589cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 559e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { 560e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); 561e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 562e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner } 5639cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 5646d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // If the source and destination are integer or pointer types, just do an 5656d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // extension or truncation to the desired type. 5666d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && 5676d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { 5686d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); 5696d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 5706d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return; 5716d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner } 5729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 5739408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); 574275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 57588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar // If store is legal, just bitcast the src pointer. 576fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar if (SrcSize <= DstSize) { 577275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *Casted = 578275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); 579386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar // FIXME: Use better alignment / avoid requiring aligned store. 580badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); 581275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar } else { 582275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar // Otherwise do coercion through memory. This is stupid, but 583275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar // simple. 584fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar 585fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // Generally SrcSize is never greater than DstSize, since this means we are 586fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // losing bits. However, this can happen in cases where the structure has 587fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // additional padding, for example due to a user specified alignment. 588fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // 589fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // FIXME: Assert that we aren't truncating non-padding bits when have access 590fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // to that information. 591275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); 592275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateStore(Src, Tmp); 5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *Casted = 594275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); 595386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 596386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar // FIXME: Use better alignment / avoid requiring aligned load. 597386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar Load->setAlignment(1); 598d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile); 599275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar } 600275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar} 601275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 6025627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/ 6035627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 604dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { 60511e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar return FI.getReturnInfo().isIndirect(); 606bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar} 607bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar 608dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { 609dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { 610dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar switch (BT->getKind()) { 611dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar default: 612dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar return false; 613dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar case BuiltinType::Float: 614dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float); 615dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar case BuiltinType::Double: 616dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double); 617dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar case BuiltinType::LongDouble: 618dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar return getContext().Target.useObjCFPRetForRealType( 619dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar TargetInfo::LongDouble); 620dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar } 621dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar } 622dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar 623dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar return false; 624dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar} 625dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar 626c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCallconst llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { 627c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall const CGFunctionInfo &FI = getFunctionInfo(GD); 6289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 629c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // For definition purposes, don't consider a K&R function variadic. 630c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall bool Variadic = false; 631c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (const FunctionProtoType *FPT = 632c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>()) 633c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall Variadic = FPT->isVariadic(); 634c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 635bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner return GetFunctionType(FI, Variadic, false); 636c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall} 637c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 63845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbarconst llvm::FunctionType * 63942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCallCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic, 64042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall bool isRecursive) { 64142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall llvm::SmallVector<const llvm::Type*, 8> argTypes; 64242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const llvm::Type *resultType = 0; 64345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 64442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const ABIArgInfo &retAI = FI.getReturnInfo(); 64542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall switch (retAI.getKind()) { 6468951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 64742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall llvm_unreachable("Invalid ABI kind for return argument"); 6488951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 649cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 65046327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: 65142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall resultType = retAI.getCoerceToType(); 65246327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar break; 65346327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar 65411e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar case ABIArgInfo::Indirect: { 65542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); 65642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall resultType = llvm::Type::getVoidTy(getLLVMContext()); 65742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall 65842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall QualType ret = FI.getReturnType(); 65942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const llvm::Type *ty = ConvertType(ret, isRecursive); 66042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall unsigned addressSpace = Context.getTargetAddressSpace(ret); 66142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(llvm::PointerType::get(ty, addressSpace)); 66245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar break; 66345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar } 66445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 66511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 66642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall resultType = llvm::Type::getVoidTy(getLLVMContext()); 66711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar break; 66845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar } 6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 67188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar ie = FI.arg_end(); it != ie; ++it) { 67242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const ABIArgInfo &argAI = it->info; 6731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 67442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall switch (argAI.getKind()) { 67511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 67611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar break; 67711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 678800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Indirect: { 679800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // indirect arguments are always on the stack, which is addr space #0. 68042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const llvm::Type *LTy = ConvertTypeForMem(it->type, isRecursive); 68142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(LTy->getPointerTo()); 682800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 683800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 684800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 685800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Extend: 6861ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner case ABIArgInfo::Direct: { 687ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // If the coerce-to type is a first class aggregate, flatten it. Either 688ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // way is semantically identical, but fast-isel and the optimizer 689ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // generally likes scalar values better than FCAs. 69042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const llvm::Type *argType = argAI.getCoerceToType(); 69142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall if (const llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) { 69242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) 69342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(st->getElementType(i)); 694ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } else { 69542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(argType); 696ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 69789c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar break; 6981ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner } 6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7008951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 70142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall GetExpandedTypes(it->type, argTypes, isRecursive); 7028951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar break; 7038951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar } 70445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar } 70545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 70642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall return llvm::FunctionType::get(resultType, argTypes, isVariadic); 7073913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar} 7083913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar 7094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallconst llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { 7104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 711ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 7129cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 7134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!VerifyFuncTypeComplete(FPT)) { 7144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const CGFunctionInfo *Info; 7154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (isa<CXXDestructorDecl>(MD)) 7164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType()); 7174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall else 7184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall Info = &getFunctionInfo(MD); 7194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return GetFunctionType(*Info, FPT->isVariadic(), false); 7204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 721ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson 722ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson return llvm::OpaqueType::get(getLLVMContext()); 723ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson} 724ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson 725a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbarvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, 72688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar const Decl *TargetDecl, 7279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer AttributeListType &PAL, 728ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar unsigned &CallingConv) { 7295323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar unsigned FuncAttrs = 0; 730a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel unsigned RetAttrs = 0; 7315323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar 732ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar CallingConv = FI.getEffectiveCallingConvention(); 733ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar 73404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall if (FI.isNoReturn()) 73504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall FuncAttrs |= llvm::Attribute::NoReturn; 73604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall 7371102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov // FIXME: handle sseregparm someday... 7385323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar if (TargetDecl) { 73940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TargetDecl->hasAttr<NoThrowAttr>()) 740761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel FuncAttrs |= llvm::Attribute::NoUnwind; 7419c0c1f333ab8f5a3da055b99ee94778689face17John McCall else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { 7429c0c1f333ab8f5a3da055b99ee94778689face17John McCall const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); 7438026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl if (FPT && FPT->isNothrow(getContext())) 7449c0c1f333ab8f5a3da055b99ee94778689face17John McCall FuncAttrs |= llvm::Attribute::NoUnwind; 7459c0c1f333ab8f5a3da055b99ee94778689face17John McCall } 7469c0c1f333ab8f5a3da055b99ee94778689face17John McCall 74740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TargetDecl->hasAttr<NoReturnAttr>()) 748761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel FuncAttrs |= llvm::Attribute::NoReturn; 74940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TargetDecl->hasAttr<ConstAttr>()) 750232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson FuncAttrs |= llvm::Attribute::ReadNone; 75140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis else if (TargetDecl->hasAttr<PureAttr>()) 75264c2e0762628eba26c100642521b6100c2515cc5Daniel Dunbar FuncAttrs |= llvm::Attribute::ReadOnly; 75376168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn if (TargetDecl->hasAttr<MallocAttr>()) 75476168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn RetAttrs |= llvm::Attribute::NoAlias; 7555323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 7565323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar 7572811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth if (CodeGenOpts.OptimizeSize) 7587ab1c3ebd02c31bfa1333cc51de1261c1499d6f7Daniel Dunbar FuncAttrs |= llvm::Attribute::OptimizeForSize; 7592811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth if (CodeGenOpts.DisableRedZone) 76024095dad88dd9d48aa16afa6416417073af251b5Devang Patel FuncAttrs |= llvm::Attribute::NoRedZone; 7612811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth if (CodeGenOpts.NoImplicitFloat) 762acebb397fa5d63835a0de9cee144987057ec1333Devang Patel FuncAttrs |= llvm::Attribute::NoImplicitFloat; 76324095dad88dd9d48aa16afa6416417073af251b5Devang Patel 764a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar QualType RetTy = FI.getReturnType(); 7655323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar unsigned Index = 1; 766b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &RetAI = FI.getReturnInfo(); 76745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar switch (RetAI.getKind()) { 768cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 7692eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner if (RetTy->hasSignedIntegerRepresentation()) 770cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov RetAttrs |= llvm::Attribute::SExt; 7712eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner else if (RetTy->hasUnsignedIntegerRepresentation()) 772cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov RetAttrs |= llvm::Attribute::ZExt; 773800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 77446327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: 775800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Ignore: 7762c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar break; 7772c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 77811e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar case ABIArgInfo::Indirect: 7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump PAL.push_back(llvm::AttributeWithIndex::get(Index, 780fb97cf24158aa7f1fd74374052f99733ef331bb9Chris Lattner llvm::Attribute::StructRet)); 7815323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar ++Index; 7820ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar // sret disables readnone and readonly 7830ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar FuncAttrs &= ~(llvm::Attribute::ReadOnly | 7840ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar llvm::Attribute::ReadNone); 7852c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar break; 7862c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 7878951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 7881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(0 && "Invalid ABI kind for return argument"); 7895323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 7902c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 791a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel if (RetAttrs) 792a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); 7931102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov 79417d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar // FIXME: RegParm should be reduced in case of global register variable. 795a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman signed RegParm; 796a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman if (FI.getHasRegParm()) 797a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman RegParm = FI.getRegParm(); 798a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman else 79917d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar RegParm = CodeGenOpts.NumRegisterParameters; 8001102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov 8011102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov unsigned PointerWidth = getContext().Target.getPointerWidth(0); 8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 80388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar ie = FI.arg_end(); it != ie; ++it) { 80488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar QualType ParamType = it->type; 80588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar const ABIArgInfo &AI = it->info; 806761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel unsigned Attributes = 0; 8071102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov 808d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we 809d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall // have the corresponding parameter variable. It doesn't make 8107f6890ebb874cc16320259daef50f1b4cfdc47d5Daniel Dunbar // sense to do it here because parameters are so messed up. 8118951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar switch (AI.getKind()) { 812cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 813575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor if (ParamType->isSignedIntegerOrEnumerationType()) 8142eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner Attributes |= llvm::Attribute::SExt; 815575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor else if (ParamType->isUnsignedIntegerOrEnumerationType()) 8162eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner Attributes |= llvm::Attribute::ZExt; 817800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // FALL THROUGH 81846327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: 8191102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov if (RegParm > 0 && 8201102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov (ParamType->isIntegerType() || ParamType->isPointerType())) { 8211102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov RegParm -= 822800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth; 8231102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov if (RegParm >= 0) 8241102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov Attributes |= llvm::Attribute::InReg; 8251102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov } 8261102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov // FIXME: handle sseregparm someday... 8279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 828800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (const llvm::StructType *STy = 829800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner dyn_cast<llvm::StructType>(AI.getCoerceToType())) 830800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner Index += STy->getNumElements()-1; // 1 will be added below. 831800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 832800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 833800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Indirect: 834800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (AI.getIndirectByVal()) 835800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner Attributes |= llvm::Attribute::ByVal; 836800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 837800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner Attributes |= 838800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); 839800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // byval disables readnone and readonly. 840800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner FuncAttrs &= ~(llvm::Attribute::ReadOnly | 841800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Attribute::ReadNone); 8428951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar break; 8431102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov 84411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 84511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar // Skip increment, no matching LLVM parameter. 8461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump continue; 84711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 8485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar case ABIArgInfo::Expand: { 84942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall llvm::SmallVector<const llvm::Type*, 8> types; 850f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: This is rather inefficient. Do we ever actually need to do 851f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // anything here? The result should be just reconstructed on the other 852f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // side, so extension should be a non-issue. 85342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall getTypes().GetExpandedTypes(ParamType, types, false); 85442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall Index += types.size(); 8555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar continue; 8565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 8575323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 8581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 859761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel if (Attributes) 860761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes)); 8615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar ++Index; 8625323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 863a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel if (FuncAttrs) 864a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); 8655323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar} 8665323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar 867d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// An argument came in as a promoted argument; demote it back to its 868d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// declared type. 869d26bc76c98006609002d9930f8840490e88ac5b5John McCallstatic llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, 870d26bc76c98006609002d9930f8840490e88ac5b5John McCall const VarDecl *var, 871d26bc76c98006609002d9930f8840490e88ac5b5John McCall llvm::Value *value) { 872d26bc76c98006609002d9930f8840490e88ac5b5John McCall const llvm::Type *varType = CGF.ConvertType(var->getType()); 873d26bc76c98006609002d9930f8840490e88ac5b5John McCall 874d26bc76c98006609002d9930f8840490e88ac5b5John McCall // This can happen with promotions that actually don't change the 875d26bc76c98006609002d9930f8840490e88ac5b5John McCall // underlying type, like the enum promotions. 876d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (value->getType() == varType) return value; 877d26bc76c98006609002d9930f8840490e88ac5b5John McCall 878d26bc76c98006609002d9930f8840490e88ac5b5John McCall assert((varType->isIntegerTy() || varType->isFloatingPointTy()) 879d26bc76c98006609002d9930f8840490e88ac5b5John McCall && "unexpected promotion type"); 880d26bc76c98006609002d9930f8840490e88ac5b5John McCall 881d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isa<llvm::IntegerType>(varType)) 882d26bc76c98006609002d9930f8840490e88ac5b5John McCall return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); 883d26bc76c98006609002d9930f8840490e88ac5b5John McCall 884d26bc76c98006609002d9930f8840490e88ac5b5John McCall return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); 885d26bc76c98006609002d9930f8840490e88ac5b5John McCall} 886d26bc76c98006609002d9930f8840490e88ac5b5John McCall 88788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, 88888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar llvm::Function *Fn, 88917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar const FunctionArgList &Args) { 8900cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // If this is an implicit-return-zero function, go ahead and 8910cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // initialize the return value. TODO: it might be nice to have 8920cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // a more general mechanism for this that didn't require synthesized 8930cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // return statements. 894121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) { 8950cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall if (FD->hasImplicitReturnZero()) { 8960cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall QualType RetTy = FD->getResultType().getUnqualifiedType(); 8970cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); 898c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); 8990cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall Builder.CreateStore(Zero, ReturnValue); 9000cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall } 9010cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall } 9020cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall 903f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We no longer need the types from FunctionArgList; lift up and 904f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // simplify. 9055251afa21d3583f2740fd4f83659d008625a7260Daniel Dunbar 90617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // Emit allocs for param decls. Give the LLVM Argument nodes names. 90717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar llvm::Function::arg_iterator AI = Fn->arg_begin(); 9081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 90917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // Name the struct return argument. 910dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar if (CGM.ReturnTypeUsesSRet(FI)) { 91117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar AI->setName("agg.result"); 91217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar ++AI; 91317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9154b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar assert(FI.arg_size() == Args.size() && 9164b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar "Mismatch between function signature & arguments."); 917093ac461b37a573dcf226fa55faed96f318169b9Devang Patel unsigned ArgNo = 1; 918b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); 919093ac461b37a573dcf226fa55faed96f318169b9Devang Patel for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 920093ac461b37a573dcf226fa55faed96f318169b9Devang Patel i != e; ++i, ++info_it, ++ArgNo) { 921d26bc76c98006609002d9930f8840490e88ac5b5John McCall const VarDecl *Arg = *i; 922b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar QualType Ty = info_it->type; 923b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &ArgI = info_it->info; 9248951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 925d26bc76c98006609002d9930f8840490e88ac5b5John McCall bool isPromoted = 926d26bc76c98006609002d9930f8840490e88ac5b5John McCall isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); 927d26bc76c98006609002d9930f8840490e88ac5b5John McCall 9288951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar switch (ArgI.getKind()) { 9291f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar case ABIArgInfo::Indirect: { 930ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner llvm::Value *V = AI; 931cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar 9321f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar if (hasAggregateLLVMType(Ty)) { 933cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // Aggregates and complex variables are accessed by reference. All we 934cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // need to do is realign the value, if requested 935cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar if (ArgI.getIndirectRealign()) { 936cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); 937cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar 938cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // Copy from the incoming argument pointer to the temporary with the 939cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // appropriate alignment. 940cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // 941cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // FIXME: We should have a common utility for generating an aggregate 942cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // copy. 9439f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer const llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); 944fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck CharUnits Size = getContext().getTypeSizeInChars(Ty); 945c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); 946c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); 947c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi Builder.CreateMemCpy(Dst, 948c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi Src, 949fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck llvm::ConstantInt::get(IntPtrTy, 950fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck Size.getQuantity()), 9519f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer ArgI.getIndirectAlign(), 9529f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer false); 953cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar V = AlignedTemp; 954cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar } 9551f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } else { 9561f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar // Load scalar value from indirect argument. 957fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck CharUnits Alignment = getContext().getTypeAlignInChars(Ty); 958fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty); 959d26bc76c98006609002d9930f8840490e88ac5b5John McCall 960d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isPromoted) 961d26bc76c98006609002d9930f8840490e88ac5b5John McCall V = emitArgumentDemotion(*this, Arg, V); 9621f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } 963093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, V, ArgNo); 9641f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar break; 9651f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } 966cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov 967cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 96846327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: { 969800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // If we have the trivial case, handle it with no muss and fuss. 970800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && 971117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgI.getCoerceToType() == ConvertType(Ty) && 972117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgI.getDirectOffset() == 0) { 973800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner assert(AI != Fn->arg_end() && "Argument mismatch!"); 974800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *V = AI; 9759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 976d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall if (Arg->getType().isRestrictQualified()) 977d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall AI->addAttr(llvm::Attribute::NoAlias); 978d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall 979d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isPromoted) 980d26bc76c98006609002d9930f8840490e88ac5b5John McCall V = emitArgumentDemotion(*this, Arg, V); 981d26bc76c98006609002d9930f8840490e88ac5b5John McCall 982093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, V, ArgNo); 983800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 98417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 986121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce"); 9879cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 988deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner // The alignment we need to use is the max of the requested alignment for 989deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner // the argument plus the alignment required by our access code below. 9909cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer unsigned AlignmentToUse = 991d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType()); 992deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner AlignmentToUse = std::max(AlignmentToUse, 993deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner (unsigned)getContext().getDeclAlign(Arg).getQuantity()); 9949cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 995deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner Alloca->setAlignment(AlignmentToUse); 996121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner llvm::Value *V = Alloca; 997117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::Value *Ptr = V; // Pointer to store into. 9989cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 999117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 1000117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = ArgI.getDirectOffset()) { 1001117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); 1002117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); 10039cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer Ptr = Builder.CreateBitCast(Ptr, 1004117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(ArgI.getCoerceToType())); 1005117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 10069cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1007ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // If the coerce-to type is a first class aggregate, we flatten it and 1008ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // pass the elements. Either way is semantically identical, but fast-isel 1009ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // and the optimizer generally likes scalar values better than FCAs. 1010ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner if (const llvm::StructType *STy = 1011ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) { 10129282688a296b306c4ae2d115f55101647056d1daChris Lattner Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); 10139cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 10149282688a296b306c4ae2d115f55101647056d1daChris Lattner for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 10159282688a296b306c4ae2d115f55101647056d1daChris Lattner assert(AI != Fn->arg_end() && "Argument mismatch!"); 10169282688a296b306c4ae2d115f55101647056d1daChris Lattner AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i)); 10179282688a296b306c4ae2d115f55101647056d1daChris Lattner llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); 10189282688a296b306c4ae2d115f55101647056d1daChris Lattner Builder.CreateStore(AI++, EltPtr); 1019ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 1020ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } else { 1021309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner // Simple case, just do a coerced store of the argument into the alloca. 1022ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner assert(AI != Fn->arg_end() && "Argument mismatch!"); 1023225e286110bcc8b7b1ff8b35f0d51a10a158b18cChris Lattner AI->setName(Arg->getName() + ".coerce"); 1024117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this); 1025ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 10269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 10279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 102889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar // Match to what EmitParmDecl is expecting for this type. 10298b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { 103091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty); 1031d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isPromoted) 1032d26bc76c98006609002d9930f8840490e88ac5b5John McCall V = emitArgumentDemotion(*this, Arg, V); 10338b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar } 1034093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, V, ArgNo); 1035ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner continue; // Skip ++AI increment, already done. 103689c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar } 1037800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1038800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Expand: { 1039800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // If this structure was expanded into multiple arguments then 1040800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // we need to create a temporary and reconstruct it from the 1041800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // arguments. 1042800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr"); 1043800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Function::arg_iterator End = 104479c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI); 1045093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, Temp, ArgNo); 1046800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1047800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Name the arguments used in expansion and increment AI. 1048800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner unsigned Index = 0; 1049800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner for (; AI != End; ++AI, ++Index) 1050800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner AI->setName(Arg->getName() + "." + llvm::Twine(Index)); 1051800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner continue; 1052800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 1053800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1054800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Ignore: 1055800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Initialize the local variable appropriately. 1056800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (hasAggregateLLVMType(Ty)) 1057093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo); 1058800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner else 1059093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())), 1060093ac461b37a573dcf226fa55faed96f318169b9Devang Patel ArgNo); 1061800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1062800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Skip increment, no matching LLVM parameter. 1063800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner continue; 10648951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar } 10655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 10665627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar ++AI; 106717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 106817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar assert(AI == Fn->arg_end() && "Argument mismatch!"); 106917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar} 107017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 107135b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattnervoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) { 10722c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar // Functions with no result always return void. 1073c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner if (ReturnValue == 0) { 1074c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner Builder.CreateRetVoid(); 1075c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner return; 1076c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } 107721fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar 10784751a53c5e5fed4bf2271e29cae7411c93a77df7Dan Gohman llvm::DebugLoc RetDbgLoc; 1079c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner llvm::Value *RV = 0; 1080c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner QualType RetTy = FI.getReturnType(); 1081c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner const ABIArgInfo &RetAI = FI.getReturnInfo(); 1082cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov 1083c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner switch (RetAI.getKind()) { 108491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar case ABIArgInfo::Indirect: { 108591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 1086c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner if (RetTy->isAnyComplexType()) { 1087c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); 1088c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner StoreComplexToAddr(RT, CurFn->arg_begin(), false); 1089c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 1090c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner // Do nothing; aggregrates get evaluated directly into the destination. 1091c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } else { 1092c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), 109391a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar false, Alignment, RetTy); 1094c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } 1095c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner break; 109691a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar } 10978951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 1098c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner case ABIArgInfo::Extend: 1099800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Direct: 1100117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (RetAI.getCoerceToType() == ConvertType(RetTy) && 1101117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner RetAI.getDirectOffset() == 0) { 1102800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // The internal return value temp always will have pointer-to-return-type 1103800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // type, just do a load. 11049cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1105800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // If the instruction right before the insertion point is a store to the 1106800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // return value, we can elide the load, zap the store, and usually zap the 1107800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // alloca. 1108800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::BasicBlock *InsertBB = Builder.GetInsertBlock(); 1109800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::StoreInst *SI = 0; 11109cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer if (InsertBB->empty() || 1111800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) || 1112800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner SI->getPointerOperand() != ReturnValue || SI->isVolatile()) { 1113800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner RV = Builder.CreateLoad(ReturnValue); 1114800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } else { 1115800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Get the stored value and nuke the now-dead store. 1116800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner RetDbgLoc = SI->getDebugLoc(); 1117800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner RV = SI->getValueOperand(); 1118800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner SI->eraseFromParent(); 11199cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1120800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // If that was the only use of the return value, nuke it as well now. 1121800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { 1122800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); 1123800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner ReturnValue = 0; 1124800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 112535b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner } 1126800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } else { 1127117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::Value *V = ReturnValue; 1128117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 1129117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = RetAI.getDirectOffset()) { 1130117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); 1131117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner V = Builder.CreateConstGEP1_32(V, Offs); 11329cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer V = Builder.CreateBitCast(V, 1133117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 1134117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 11359cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1136117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); 113735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner } 1138c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner break; 11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1140800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Ignore: 1141c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner break; 11428951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 1143c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner case ABIArgInfo::Expand: 1144c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner assert(0 && "Invalid ABI kind for return argument"); 114517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 11461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 114721fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid(); 1148d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel if (!RetDbgLoc.isUnknown()) 1149d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel Ret->setDebugLoc(RetDbgLoc); 115017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar} 115117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 1152413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitDelegateCallArg(CallArgList &args, 1153413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall const VarDecl *param) { 11542736071ea3a46f90e65c93418961611d96c10ab9John McCall // StartFunction converted the ABI-lowered parameter(s) into a 11552736071ea3a46f90e65c93418961611d96c10ab9John McCall // local alloca. We need to turn that into an r-value suitable 11562736071ea3a46f90e65c93418961611d96c10ab9John McCall // for EmitCall. 1157413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall llvm::Value *local = GetAddrOfLocalVar(param); 11582736071ea3a46f90e65c93418961611d96c10ab9John McCall 1159413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall QualType type = param->getType(); 11609cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 11612736071ea3a46f90e65c93418961611d96c10ab9John McCall // For the most part, we just need to load the alloca, except: 11622736071ea3a46f90e65c93418961611d96c10ab9John McCall // 1) aggregate r-values are actually pointers to temporaries, and 11632736071ea3a46f90e65c93418961611d96c10ab9John McCall // 2) references to aggregates are pointers directly to the aggregate. 11642736071ea3a46f90e65c93418961611d96c10ab9John McCall // I don't know why references to non-aggregates are different here. 1165413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (const ReferenceType *ref = type->getAs<ReferenceType>()) { 1166413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (hasAggregateLLVMType(ref->getPointeeType())) 1167413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::getAggregate(local), type); 11682736071ea3a46f90e65c93418961611d96c10ab9John McCall 11692736071ea3a46f90e65c93418961611d96c10ab9John McCall // Locals which are references to scalars are represented 11702736071ea3a46f90e65c93418961611d96c10ab9John McCall // with allocas holding the pointer. 1171413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::get(Builder.CreateLoad(local)), type); 11722736071ea3a46f90e65c93418961611d96c10ab9John McCall } 11732736071ea3a46f90e65c93418961611d96c10ab9John McCall 1174413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (type->isAnyComplexType()) { 1175413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false); 1176413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::getComplex(complex), type); 1177413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall } 11782736071ea3a46f90e65c93418961611d96c10ab9John McCall 1179413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (hasAggregateLLVMType(type)) 1180413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::getAggregate(local), type); 11812736071ea3a46f90e65c93418961611d96c10ab9John McCall 1182413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall unsigned alignment = getContext().getDeclAlign(param).getQuantity(); 1183413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type); 1184413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::get(value), type); 11852736071ea3a46f90e65c93418961611d96c10ab9John McCall} 11862736071ea3a46f90e65c93418961611d96c10ab9John McCall 1187413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, 1188413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall QualType type) { 1189413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (type->isReferenceType()) 1190413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0), 1191413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall type); 11921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 119370cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() && 119470cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman isa<ImplicitCastExpr>(E) && 119555d484802f3e27930317739efc5f5956b78aac25Eli Friedman cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { 119655d484802f3e27930317739efc5f5956b78aac25Eli Friedman LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); 119755d484802f3e27930317739efc5f5956b78aac25Eli Friedman assert(L.isSimple()); 119855d484802f3e27930317739efc5f5956b78aac25Eli Friedman args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()), 119955d484802f3e27930317739efc5f5956b78aac25Eli Friedman type, /*NeedsCopy*/true); 120055d484802f3e27930317739efc5f5956b78aac25Eli Friedman return; 120155d484802f3e27930317739efc5f5956b78aac25Eli Friedman } 120255d484802f3e27930317739efc5f5956b78aac25Eli Friedman 1203413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall args.add(EmitAnyExprToTemp(E), type); 12040139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson} 12050139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson 1206f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Emits a call or invoke instruction to the given function, depending 1207f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// on the current state of the EH stack. 1208f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::CallSite 1209f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 1210f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::Value * const *ArgBegin, 1211f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::Value * const *ArgEnd, 1212f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall const llvm::Twine &Name) { 1213f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::BasicBlock *InvokeDest = getInvokeDest(); 1214f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!InvokeDest) 1215f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name); 1216f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 1217f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); 1218f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest, 1219f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall ArgBegin, ArgEnd, Name); 1220f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall EmitBlock(ContBB); 1221f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall return Invoke; 1222f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall} 1223f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 122488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel DunbarRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, 12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *Callee, 1226f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson ReturnValueSlot ReturnValue, 1227c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar const CallArgList &CallArgs, 1228dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall const Decl *TargetDecl, 12294b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall llvm::Instruction **callOrInvoke) { 1230f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We no longer need the types from CallArgs; lift up and simplify. 123117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar llvm::SmallVector<llvm::Value*, 16> Args; 123217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 123317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // Handle struct-return functions by passing a pointer to the 123417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // location that we would like to return into. 1235bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar QualType RetTy = CallInfo.getReturnType(); 1236b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); 12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12395db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // If the call returns a temporary with struct return, create a temporary 1240d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson // alloca to hold the result, unless one is given to us. 1241dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar if (CGM.ReturnTypeUsesSRet(CallInfo)) { 1242d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson llvm::Value *Value = ReturnValue.getValue(); 1243d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson if (!Value) 1244195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar Value = CreateMemTemp(RetTy); 1245d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson Args.push_back(Value); 1246d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson } 12471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12484b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar assert(CallInfo.arg_size() == CallArgs.size() && 12494b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar "Mismatch between function signature & arguments."); 1250b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); 12511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 1252b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar I != E; ++I, ++info_it) { 1253b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &ArgInfo = info_it->info; 1254c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman RValue RV = I->RV; 12555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 125697cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman unsigned TypeAlign = 1257c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman getContext().getTypeAlignInChars(I->Ty).getQuantity(); 12585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar switch (ArgInfo.getKind()) { 125991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar case ABIArgInfo::Indirect: { 12601f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar if (RV.isScalar() || RV.isComplex()) { 12611f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar // Make a temporary alloca to pass the argument. 126270cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 126370cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman if (ArgInfo.getIndirectAlign() > AI->getAlignment()) 126470cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman AI->setAlignment(ArgInfo.getIndirectAlign()); 126570cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman Args.push_back(AI); 12661f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar if (RV.isScalar()) 126791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, 126897cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman TypeAlign, I->Ty); 12691f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar else 12701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); 12711f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } else { 1272ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // We want to avoid creating an unnecessary temporary+copy here; 1273ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // however, we need one in two cases: 1274ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // 1. If the argument is not byval, and we are required to copy the 1275ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // source. (This case doesn't occur on any common architecture.) 1276ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // 2. If the argument is byval, RV is not sufficiently aligned, and 1277ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // we cannot force it to be sufficiently aligned. 127897cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman llvm::Value *Addr = RV.getAggregateAddr(); 127997cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman unsigned Align = ArgInfo.getIndirectAlign(); 128097cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman const llvm::TargetData *TD = &CGM.getTargetData(); 128197cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || 128297cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman (ArgInfo.getIndirectByVal() && TypeAlign < Align && 128397cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) { 1284ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // Create an aligned temporary, and copy to it. 128597cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 128697cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman if (Align > AI->getAlignment()) 128797cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman AI->setAlignment(Align); 1288ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman Args.push_back(AI); 128997cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); 1290ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman } else { 1291ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // Skip the extra memcpy call. 129297cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman Args.push_back(Addr); 1293ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman } 12941f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } 12951f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar break; 129691a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar } 12971f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar 129811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 129911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar break; 13009cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1301800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Extend: 1302800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Direct: { 1303800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && 1304117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgInfo.getCoerceToType() == ConvertType(info_it->type) && 1305117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgInfo.getDirectOffset() == 0) { 1306800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (RV.isScalar()) 1307800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner Args.push_back(RV.getScalarVal()); 1308800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner else 1309800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner Args.push_back(Builder.CreateLoad(RV.getAggregateAddr())); 1310800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 1311800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 131211434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 131389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar // FIXME: Avoid the conversion through memory if possible. 131489c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar llvm::Value *SrcPtr; 131589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar if (RV.isScalar()) { 1316c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman SrcPtr = CreateMemTemp(I->Ty, "coerce"); 131797cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty); 131889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar } else if (RV.isComplex()) { 1319c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman SrcPtr = CreateMemTemp(I->Ty, "coerce"); 132089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); 13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 132289c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar SrcPtr = RV.getAggregateAddr(); 13239cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1324117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 1325117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = ArgInfo.getDirectOffset()) { 1326117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); 1327117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); 13289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer SrcPtr = Builder.CreateBitCast(SrcPtr, 1329117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); 1330117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner 1331117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 13329cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1333ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // If the coerce-to type is a first class aggregate, we flatten it and 1334ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // pass the elements. Either way is semantically identical, but fast-isel 1335ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // and the optimizer generally likes scalar values better than FCAs. 1336ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner if (const llvm::StructType *STy = 1337309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) { 13389282688a296b306c4ae2d115f55101647056d1daChris Lattner SrcPtr = Builder.CreateBitCast(SrcPtr, 13399282688a296b306c4ae2d115f55101647056d1daChris Lattner llvm::PointerType::getUnqual(STy)); 13409282688a296b306c4ae2d115f55101647056d1daChris Lattner for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 13419282688a296b306c4ae2d115f55101647056d1daChris Lattner llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); 1342deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); 1343deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner // We don't know what we're loading from. 1344deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner LI->setAlignment(1); 1345deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner Args.push_back(LI); 1346309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner } 1347ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } else { 1348309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner // In the simple case, just pass the coerced loaded value. 1349309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), 1350309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner *this)); 1351ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 13529cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 135389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar break; 135489c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar } 135589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar 13565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar case ABIArgInfo::Expand: 1357c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman ExpandTypeToArgs(I->Ty, RV, Args); 13585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar break; 135917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 136017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 13611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13625db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // If the callee is a bitcast of a function to a varargs pointer to function 13635db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // type, check to see if we can remove the bitcast. This handles some cases 13645db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // with unprototyped functions. 13655db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) 13665db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { 13675db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); 13685db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner const llvm::FunctionType *CurFT = 13695db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner cast<llvm::FunctionType>(CurPT->getElementType()); 13705db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner const llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); 13711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13725db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (CE->getOpcode() == llvm::Instruction::BitCast && 13735db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner ActualFT->getReturnType() == CurFT->getReturnType() && 1374d6bebbfd510f3b495795b88aafd10ead3cb211e9Chris Lattner ActualFT->getNumParams() == CurFT->getNumParams() && 1375c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian ActualFT->getNumParams() == Args.size() && 1376c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian (CurFT->isVarArg() || !ActualFT->isVarArg())) { 13775db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner bool ArgsMatch = true; 13785db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) 13795db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { 13805db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner ArgsMatch = false; 13815db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner break; 13825db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner } 13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13845db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // Strip the cast if we can get away with it. This is a nice cleanup, 13855db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // but also allows us to inline the function at -O0 if it is marked 13865db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // always_inline. 13875db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (ArgsMatch) 13885db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner Callee = CalleeF; 13895db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner } 13905db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner } 13911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 139217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 1393ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar unsigned CallingConv; 1394761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel CodeGen::AttributeListType AttributeList; 1395ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv); 13969834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(), 13979834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar AttributeList.end()); 13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1399f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::BasicBlock *InvokeDest = 0; 1400f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) 1401f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall InvokeDest = getInvokeDest(); 1402f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 1403d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar llvm::CallSite CS; 1404f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!InvokeDest) { 1405beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size()); 14069834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar } else { 14079834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); 14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, 1409beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad Args.data(), Args.data()+Args.size()); 14109834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar EmitBlock(Cont); 1411f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar } 1412ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner if (callOrInvoke) 14134b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall *callOrInvoke = CS.getInstruction(); 1414f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar 1415d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar CS.setAttributes(Attrs); 1416ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 1417d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar 1418d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // If the call doesn't return, finish the basic block and clear the 1419d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // insertion point; this allows the rest of IRgen to discard 1420d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // unreachable code. 1421d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar if (CS.doesNotReturn()) { 1422d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar Builder.CreateUnreachable(); 1423d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar Builder.ClearInsertionPoint(); 14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1425f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: For now, emit a dummy basic block because expr emitters in 1426f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // generally are not ready to handle emitting expressions at unreachable 1427f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // points. 1428d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar EnsureInsertPoint(); 14291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1430d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // Return a reasonable RValue. 1431d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar return GetUndefRValue(RetTy); 14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 1433d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar 1434d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar llvm::Instruction *CI = CS.getInstruction(); 1435ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) 143617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar CI->setName("call"); 14372c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 14382c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar switch (RetAI.getKind()) { 143991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar case ABIArgInfo::Indirect: { 144091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 14412c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar if (RetTy->isAnyComplexType()) 14425627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); 14433403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 14445627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar return RValue::getAggregate(Args[0]); 144591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy)); 144691a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar } 14478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 144811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 14490bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar // If we are ignoring an argument that had a result, make sure to 14500bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar // construct the appropriate return value for our caller. 145113e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar return GetUndefRValue(RetTy); 14529cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1453800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Extend: 1454800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Direct: { 1455117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (RetAI.getCoerceToType() == ConvertType(RetTy) && 1456117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner RetAI.getDirectOffset() == 0) { 1457800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (RetTy->isAnyComplexType()) { 1458800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *Real = Builder.CreateExtractValue(CI, 0); 1459800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); 1460800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner return RValue::getComplex(std::make_pair(Real, Imag)); 1461800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 1462800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 1463800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *DestPtr = ReturnValue.getValue(); 1464800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner bool DestIsVolatile = ReturnValue.isVolatile(); 146511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 1466800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (!DestPtr) { 1467800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner DestPtr = CreateMemTemp(RetTy, "agg.tmp"); 1468800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner DestIsVolatile = false; 1469800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 1470badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); 1471800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner return RValue::getAggregate(DestPtr); 1472800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 1473800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner return RValue::get(CI); 1474800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 14759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1476d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson llvm::Value *DestPtr = ReturnValue.getValue(); 1477d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson bool DestIsVolatile = ReturnValue.isVolatile(); 14789cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1479d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson if (!DestPtr) { 1480195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar DestPtr = CreateMemTemp(RetTy, "coerce"); 1481d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson DestIsVolatile = false; 1482d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson } 14839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1484117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 1485117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::Value *StorePtr = DestPtr; 1486117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = RetAI.getDirectOffset()) { 1487117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); 1488117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); 14899cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer StorePtr = Builder.CreateBitCast(StorePtr, 1490117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 1491117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 1492117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); 14939cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 149491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 1495ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson if (RetTy->isAnyComplexType()) 1496d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson return RValue::getComplex(LoadComplexFromAddr(DestPtr, false)); 14973403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 1498d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson return RValue::getAggregate(DestPtr); 149991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy)); 1500639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar } 15018951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 15028951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 15031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(0 && "Invalid ABI kind for return argument"); 150417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 15052c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 15062c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar assert(0 && "Unhandled ABIArgInfo::Kind"); 15072c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar return RValue::get(0); 150817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar} 1509b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar 1510b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar/* VarArg handling */ 1511b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar 1512b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbarllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { 1513b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); 1514b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar} 1515