19cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner//===--- CGCall.cpp - 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" 20de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall#include "TargetInfo.h" 216b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar#include "clang/Basic/TargetInfo.h" 220dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/Decl.h" 23f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson#include "clang/AST/DeclCXX.h" 240dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/DeclObjC.h" 2506057cef0bcd7804e80f3ce2bbe352178396c715Chandler Carruth#include "clang/Frontend/CodeGenOptions.h" 26d0646bd7c11c12b34971b55e5c1bdd8439401b4cDevang Patel#include "llvm/Attributes.h" 27d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar#include "llvm/Support/CallSite.h" 2854d1ccbfcf19ddf39444f1b4018dd79487cc847bDaniel Dunbar#include "llvm/Target/TargetData.h" 29f85e193739c953358c865005855253af4f68a497John McCall#include "llvm/InlineAsm.h" 3097cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman#include "llvm/Transforms/Utils/Local.h" 310dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace clang; 320dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace CodeGen; 330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 340dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar/***/ 350dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 3604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic unsigned ClangCallConvToLLVMCallConv(CallingConv CC) { 3704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall switch (CC) { 3804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall default: return llvm::CallingConv::C; 3904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall case CC_X86StdCall: return llvm::CallingConv::X86_StdCall; 4004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall case CC_X86FastCall: return llvm::CallingConv::X86_FastCall; 41f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall; 42414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS; 43414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; 4452fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik // TODO: add support for CC_X86Pascal to llvm 4504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall } 4604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall} 4704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall 480b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Derives the 'this' type for codegen purposes, i.e. ignoring method 490b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualification. 500b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// FIXME: address space qualification? 51ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) { 52ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal(); 53ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return Context.getPointerType(CanQualType::CreateUnsafe(RecTy)); 540b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 550b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 560b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the canonical formal type of the given C++ method. 57ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) { 58ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return MD->getType()->getCanonicalTypeUnqualified() 59ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall .getAs<FunctionProtoType>(); 600b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 610b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 620b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the "extra-canonicalized" return type, which discards 630b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualifiers on the return type. Codegen doesn't care about them, 640b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// and it makes ABI code a little easier to be able to assume that 650b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// all parameter and return types are top-level unqualified. 66ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetReturnType(QualType RetTy) { 67ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType(); 680b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 690b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 700f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the argument and result information for a value of the given 710f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// unprototyped freestanding function type. 720b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo & 730f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) { 74de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // When translating an unprototyped function type, always use a 75de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // variadic type. 760f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(FTNP->getResultType().getUnqualifiedType(), 770f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall ArrayRef<CanQualType>(), 780f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FTNP->getExtInfo(), 790f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs(0)); 80de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall} 81de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 820f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the LLVM function layout for a value of the given function 830f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// type, on top of any implicit parameters already stored. Use the 840f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// given ExtInfo instead of the ExtInfo from the function type. 850f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic const CGFunctionInfo &arrangeLLVMFunctionInfo(CodeGenTypes &CGT, 860f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall SmallVectorImpl<CanQualType> &prefix, 870f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CanQual<FunctionProtoType> FTP, 880f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo extInfo) { 890f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size()); 90541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy. 9145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 920f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall prefix.push_back(FTP->getArgType(i)); 93de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CanQualType resultType = FTP->getResultType().getUnqualifiedType(); 940f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return CGT.arrangeLLVMFunctionInfo(resultType, prefix, extInfo, required); 950f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall} 960f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall 970f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the argument and result information for a free function (i.e. 980f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// not a C++ or ObjC instance method) of the given type. 990f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic const CGFunctionInfo &arrangeFreeFunctionType(CodeGenTypes &CGT, 1000f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall SmallVectorImpl<CanQualType> &prefix, 1010f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CanQual<FunctionProtoType> FTP) { 1020f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(CGT, prefix, FTP, FTP->getExtInfo()); 1030f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall} 1040f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall 1050f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Given the formal ext-info of a C++ instance method, adjust it 1060f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// according to the C++ ABI in effect. 1070f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic void adjustCXXMethodInfo(CodeGenTypes &CGT, 1088f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov FunctionType::ExtInfo &extInfo, 1098f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov bool isVariadic) { 1108f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov if (extInfo.getCC() == CC_Default) { 1118f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov CallingConv CC = CGT.getContext().getDefaultCXXMethodCallConv(isVariadic); 1128f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov extInfo = extInfo.withCallingConv(CC); 1138f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov } 1140f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall} 1150f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall 1160f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the argument and result information for a free function (i.e. 1170f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// not a C++ or ObjC instance method) of the given type. 1180f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic const CGFunctionInfo &arrangeCXXMethodType(CodeGenTypes &CGT, 1190f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall SmallVectorImpl<CanQualType> &prefix, 1200f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall CanQual<FunctionProtoType> FTP) { 1210f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo extInfo = FTP->getExtInfo(); 1228f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov adjustCXXMethodInfo(CGT, extInfo, FTP->isVariadic()); 1230f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(CGT, prefix, FTP, extInfo); 1240b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall} 1250b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 126de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a value of the 1270f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// given freestanding function type. 1280b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo & 1290f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) { 130de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall SmallVector<CanQualType, 16> argTypes; 1310f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return ::arrangeFreeFunctionType(*this, argTypes, FTP); 132bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar} 133bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar 13404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic CallingConv getCallingConventionForDecl(const Decl *D) { 135bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar // Set the appropriate calling convention for the Function. 136bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar if (D->hasAttr<StdCallAttr>()) 13704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall return CC_X86StdCall; 138bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar 139bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar if (D->hasAttr<FastCallAttr>()) 14004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall return CC_X86FastCall; 141bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar 142f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor if (D->hasAttr<ThisCallAttr>()) 143f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor return CC_X86ThisCall; 144f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor 14552fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik if (D->hasAttr<PascalAttr>()) 14652fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik return CC_X86Pascal; 14752fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik 148414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov if (PcsAttr *PCS = D->getAttr<PcsAttr>()) 149414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP); 150414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov 15104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall return CC_C; 15245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar} 15345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 154de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a call to an 155de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// unknown C++ non-static member function of the given abstract type. 156de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// The member function must be an ordinary function, i.e. not a 157de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// constructor or destructor. 158de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 159de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD, 160de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const FunctionProtoType *FTP) { 161de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall SmallVector<CanQualType, 16> argTypes; 1620b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 163375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson // Add the 'this' pointer. 164de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTypes.push_back(GetThisType(Context, RD)); 1650b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 1660f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return ::arrangeCXXMethodType(*this, argTypes, 1679c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); 168375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson} 169375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson 170de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration or 171de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of the given C++ non-static member function. The 172de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// member function must be an ordinary function, i.e. not a 173de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// constructor or destructor. 174de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 175de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) { 176fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!"); 177fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!"); 178fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall 179de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CanQual<FunctionProtoType> prototype = GetFormalType(MD); 1801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 181de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (MD->isInstance()) { 182de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // The abstract case is perfectly fine. 183de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return arrangeCXXMethodType(MD->getParent(), prototype.getTypePtr()); 184de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall } 185de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 1860f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeFreeFunctionType(prototype); 187f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson} 188f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson 189de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration 190de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// or definition to the given constructor variant. 191de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 192de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXConstructorDeclaration(const CXXConstructorDecl *D, 193de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CXXCtorType ctorKind) { 194de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall SmallVector<CanQualType, 16> argTypes; 195de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTypes.push_back(GetThisType(Context, D->getParent())); 196de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CanQualType resultType = Context.VoidTy; 197f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 198de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall TheCXXABI.BuildConstructorSignature(D, ctorKind, resultType, argTypes); 1990b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 2004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQual<FunctionProtoType> FTP = GetFormalType(D); 2014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 202de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, argTypes.size()); 203de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 2044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Add the formal parameters. 2054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 206de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTypes.push_back(FTP->getArgType(i)); 2074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 2080f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo extInfo = FTP->getExtInfo(); 2098f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov adjustCXXMethodInfo(*this, extInfo, FTP->isVariadic()); 2100f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo, required); 211f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson} 212f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 213de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration, 214de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition, or call to the given destructor variant. It so 215de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// happens that all three cases produce the same information. 216de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 217de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXDestructor(const CXXDestructorDecl *D, 218de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CXXDtorType dtorKind) { 219de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall SmallVector<CanQualType, 2> argTypes; 220de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTypes.push_back(GetThisType(Context, D->getParent())); 221de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CanQualType resultType = Context.VoidTy; 2224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 223de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall TheCXXABI.BuildDestructorSignature(D, dtorKind, resultType, argTypes); 2240b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall 2254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CanQual<FunctionProtoType> FTP = GetFormalType(D); 2264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall assert(FTP->getNumArgs() == 0 && "dtor with formal parameters"); 2278f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov assert(FTP->isVariadic() == 0 && "dtor with formal parameters"); 2284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 2290f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo extInfo = FTP->getExtInfo(); 2308f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov adjustCXXMethodInfo(*this, extInfo, false); 2310f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo, 2320f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All); 233f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson} 234f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson 235de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the declaration or 236de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of the given function. 237de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 238de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) { 2393eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) 240f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson if (MD->isInstance()) 241de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return arrangeCXXMethodDeclaration(MD); 2421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 243ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified(); 244de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 245ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall assert(isa<FunctionType>(FTy)); 246de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 247de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // When declaring a function without a prototype, always use a 248de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // non-variadic type. 249de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (isa<FunctionNoProtoType>(FTy)) { 250de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>(); 2510f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(noProto->getResultType(), 2520f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall ArrayRef<CanQualType>(), 2530f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall noProto->getExtInfo(), 2540f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs::All); 255de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall } 256de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 257ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall assert(isa<FunctionProtoType>(FTy)); 2580f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>()); 2590dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 2600dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 261de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the declaration or 262de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of an Objective-C method. 263de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 264de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) { 265de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // It happens that this is the same as a call with no optional 266de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // arguments, except also using the formal 'self' type. 267de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType()); 268de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall} 269de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 270de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the function type 271de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// through which to perform a send to the given Objective-C method, 272de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// using the given receiver type. The receiver type is not always 273de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// the 'self' type of the method or even an Objective-C pointer type. 274de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// This is *not* the right method for actually performing such a 275de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// message send, due to the possibility of optional arguments. 276de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 277de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD, 278de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall QualType receiverType) { 279de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall SmallVector<CanQualType, 16> argTys; 280de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTys.push_back(Context.getCanonicalParamType(receiverType)); 281de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType())); 282541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy? 283491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(), 2840b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall e = MD->param_end(); i != e; ++i) { 285de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTys.push_back(Context.getCanonicalParamType((*i)->getType())); 2860b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall } 287f85e193739c953358c865005855253af4f68a497John McCall 288f85e193739c953358c865005855253af4f68a497John McCall FunctionType::ExtInfo einfo; 289f85e193739c953358c865005855253af4f68a497John McCall einfo = einfo.withCallingConv(getCallingConventionForDecl(MD)); 290f85e193739c953358c865005855253af4f68a497John McCall 2914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getContext().getLangOpts().ObjCAutoRefCount && 292f85e193739c953358c865005855253af4f68a497John McCall MD->hasAttr<NSReturnsRetainedAttr>()) 293f85e193739c953358c865005855253af4f68a497John McCall einfo = einfo.withProducesResult(true); 294f85e193739c953358c865005855253af4f68a497John McCall 295de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs required = 296de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All); 297de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 2980f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(GetReturnType(MD->getResultType()), argTys, 2990f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall einfo, required); 3000dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 3010dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 302de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 303de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) { 304b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson // FIXME: Do we need to handle ObjCMethodDecl? 305b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); 3069cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 307b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) 308de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return arrangeCXXConstructorDeclaration(CD, GD.getCtorType()); 309b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson 310b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) 311de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return arrangeCXXDestructor(DD, GD.getDtorType()); 312de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 313de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return arrangeFunctionDeclaration(FD); 314de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall} 3159cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 316de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Figure out the rules for calling a function with the given formal 317de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// type using the given arguments. The arguments are necessary 318de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// because the function might be unprototyped, in which case it's 319de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// target-dependent in crazy ways. 320de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 3210f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args, 3220f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall const FunctionType *fnType) { 323de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs required = RequiredArgs::All; 324de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) { 325de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (proto->isVariadic()) 326de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall required = RequiredArgs(proto->getNumArgs()); 327de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall } else if (CGM.getTargetCodeGenInfo() 328de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall .isNoProtoCallVariadic(args, cast<FunctionNoProtoType>(fnType))) { 329de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall required = RequiredArgs(0); 330de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall } 331de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 3320f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeFreeFunctionCall(fnType->getResultType(), args, 3330f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall fnType->getExtInfo(), required); 334b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson} 335b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson 336de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 3370f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionCall(QualType resultType, 3380f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall const CallArgList &args, 3390f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo info, 3400f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs required) { 341541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy. 342de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall SmallVector<CanQualType, 16> argTypes; 343de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall for (CallArgList::const_iterator i = args.begin(), e = args.end(); 344725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar i != e; ++i) 345de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTypes.push_back(Context.getCanonicalParamType(i->Ty)); 3460f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info, 3470f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall required); 3480f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall} 3490f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall 3500f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange a call to a C++ method, passing the given arguments. 3510f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallconst CGFunctionInfo & 3520f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeCXXMethodCall(const CallArgList &args, 3530f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall const FunctionProtoType *FPT, 3540f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs required) { 3550f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall // FIXME: Kill copy. 3560f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall SmallVector<CanQualType, 16> argTypes; 3570f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall for (CallArgList::const_iterator i = args.begin(), e = args.end(); 3580f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall i != e; ++i) 3590f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall argTypes.push_back(Context.getCanonicalParamType(i->Ty)); 3600f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall 3610f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo info = FPT->getExtInfo(); 3628f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov adjustCXXMethodInfo(*this, info, FPT->isVariadic()); 3630f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(GetReturnType(FPT->getResultType()), 3640f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall argTypes, info, required); 3650dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 3660dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar 367de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 368de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionDeclaration(QualType resultType, 369de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const FunctionArgList &args, 370de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const FunctionType::ExtInfo &info, 371de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall bool isVariadic) { 372541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar // FIXME: Kill copy. 373de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall SmallVector<CanQualType, 16> argTypes; 374de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall for (FunctionArgList::const_iterator i = args.begin(), e = args.end(); 375bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar i != e; ++i) 376de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall argTypes.push_back(Context.getCanonicalParamType((*i)->getType())); 377de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall 378de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs required = 379de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All); 3800f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info, 3810f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall required); 382541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar} 383541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar 384de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() { 3850f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall return arrangeLLVMFunctionInfo(getContext().VoidTy, ArrayRef<CanQualType>(), 3860f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo(), RequiredArgs::All); 387d26bc76c98006609002d9930f8840490e88ac5b5John McCall} 388d26bc76c98006609002d9930f8840490e88ac5b5John McCall 389de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for an abstract value 390de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// of a given function type. This is the method which all of the 391de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// above functions ultimately defer to. 392de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo & 3930f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType, 3940f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall ArrayRef<CanQualType> argTypes, 3950f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall FunctionType::ExtInfo info, 3960f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall RequiredArgs required) { 397ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#ifndef NDEBUG 398de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall for (ArrayRef<CanQualType>::const_iterator 399de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall I = argTypes.begin(), E = argTypes.end(); I != E; ++I) 400ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall assert(I->isCanonicalAsParam()); 401ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#endif 402ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall 403de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall unsigned CC = ClangCallConvToLLVMCallConv(info.getCC()); 40404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall 40540a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar // Lookup or create unique function info. 40640a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar llvm::FoldingSetNodeID ID; 407de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGFunctionInfo::Profile(ID, info, required, resultType, argTypes); 40840a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar 409de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall void *insertPos = 0; 410de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos); 41140a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar if (FI) 41240a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar return *FI; 41340a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar 414de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall // Construct the function info. We co-allocate the ArgInfos. 415de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI = CGFunctionInfo::create(CC, info, resultType, argTypes, required); 416de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FunctionInfos.InsertNode(FI, insertPos); 417541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar 418de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted; 419de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall assert(inserted && "Recursively being processed?"); 42071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 42188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar // Compute ABI information. 422ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner getABIInfo().computeInfo(*FI); 4239cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 424800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Loop over all of the computed argument and return value info. If any of 425800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // them are direct or extend without a specified coerce type, specify the 426800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // default now. 427de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall ABIArgInfo &retInfo = FI->getReturnInfo(); 428de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == 0) 429de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall retInfo.setCoerceToType(ConvertType(FI->getReturnType())); 4309cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 431800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end(); 432800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner I != E; ++I) 433800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0) 4349cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner I->info.setCoerceToType(ConvertType(I->type)); 4359cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 436de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall bool erased = FunctionsBeingProcessed.erase(FI); (void)erased; 437de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall assert(erased && "Not in set?"); 438d26c07142710790b820a66245939668f62eaf2d9Chris Lattner 43988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar return *FI; 4400dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar} 44117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 442de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC, 443de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const FunctionType::ExtInfo &info, 444de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CanQualType resultType, 445de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall ArrayRef<CanQualType> argTypes, 446de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall RequiredArgs required) { 447de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall void *buffer = operator new(sizeof(CGFunctionInfo) + 448de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall sizeof(ArgInfo) * (argTypes.size() + 1)); 449de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGFunctionInfo *FI = new(buffer) CGFunctionInfo(); 450de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->CallingConvention = llvmCC; 451de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->EffectiveCallingConvention = llvmCC; 452de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->ASTCallingConvention = info.getCC(); 453de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->NoReturn = info.getNoReturn(); 454de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->ReturnsRetained = info.getProducesResult(); 455de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->Required = required; 456de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->HasRegParm = info.getHasRegParm(); 457de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->RegParm = info.getRegParm(); 458de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->NumArgs = argTypes.size(); 459de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->getArgsBuffer()[0].type = resultType; 460de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall for (unsigned i = 0, e = argTypes.size(); i != e; ++i) 461de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall FI->getArgsBuffer()[i + 1].type = argTypes[i]; 462de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return FI; 46388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar} 46488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar 46588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar/***/ 46688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar 46742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCallvoid CodeGenTypes::GetExpandedTypes(QualType type, 4685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVectorImpl<llvm::Type*> &expandedTypes) { 469194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) { 470194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson uint64_t NumElts = AT->getSize().getZExtValue(); 471194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson for (uint64_t Elt = 0; Elt < NumElts; ++Elt) 472194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson GetExpandedTypes(AT->getElementType(), expandedTypes); 473eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } else if (const RecordType *RT = type->getAs<RecordType>()) { 474194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson const RecordDecl *RD = RT->getDecl(); 475194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson assert(!RD->hasFlexibleArrayMember() && 476194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson "Cannot expand structure with flexible array."); 477eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (RD->isUnion()) { 478eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov // Unions can be here only in degenerative cases - all the fields are same 479eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov // after flattening. Thus we have to use the "largest" field. 480eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov const FieldDecl *LargestFD = 0; 481eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov CharUnits UnionSize = CharUnits::Zero(); 482eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov 483eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 484eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov i != e; ++i) { 485581deb3da481053c4993c7600f97acf7768caac5David Blaikie const FieldDecl *FD = *i; 486eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov assert(!FD->isBitField() && 487eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov "Cannot expand structure with bit-field members."); 488eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); 489eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (UnionSize < FieldSize) { 490eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov UnionSize = FieldSize; 491eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov LargestFD = FD; 492eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 493eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 494eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (LargestFD) 495eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov GetExpandedTypes(LargestFD->getType(), expandedTypes); 496eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } else { 497eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 498eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov i != e; ++i) { 499581deb3da481053c4993c7600f97acf7768caac5David Blaikie assert(!i->isBitField() && 500eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov "Cannot expand structure with bit-field members."); 501581deb3da481053c4993c7600f97acf7768caac5David Blaikie GetExpandedTypes(i->getType(), expandedTypes); 502eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 503194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } 504194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } else if (const ComplexType *CT = type->getAs<ComplexType>()) { 505194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson llvm::Type *EltTy = ConvertType(CT->getElementType()); 506194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson expandedTypes.push_back(EltTy); 507194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson expandedTypes.push_back(EltTy); 508194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } else 509194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson expandedTypes.push_back(ConvertType(type)); 5105627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar} 5115627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 5121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function::arg_iterator 5135627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV, 5145627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar llvm::Function::arg_iterator AI) { 5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(LV.isSimple() && 5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump "Unexpected non-simple lvalue during struct expansion."); 517194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson 518194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 519194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson unsigned NumElts = AT->getSize().getZExtValue(); 520194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson QualType EltTy = AT->getElementType(); 521194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson for (unsigned Elt = 0; Elt < NumElts; ++Elt) { 522377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, Elt); 523194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson LValue LV = MakeAddrLValue(EltAddr, EltTy); 524194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson AI = ExpandTypeFromArgs(EltTy, LV, AI); 525194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } 526eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 527194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson RecordDecl *RD = RT->getDecl(); 528eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (RD->isUnion()) { 529eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov // Unions can be here only in degenerative cases - all the fields are same 530eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov // after flattening. Thus we have to use the "largest" field. 531eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov const FieldDecl *LargestFD = 0; 532eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov CharUnits UnionSize = CharUnits::Zero(); 533eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov 534eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 535eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov i != e; ++i) { 536581deb3da481053c4993c7600f97acf7768caac5David Blaikie const FieldDecl *FD = *i; 537eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov assert(!FD->isBitField() && 538eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov "Cannot expand structure with bit-field members."); 539eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); 540eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (UnionSize < FieldSize) { 541eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov UnionSize = FieldSize; 542eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov LargestFD = FD; 543eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 544eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 545eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (LargestFD) { 546eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov // FIXME: What are the right qualifiers here? 547377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman LValue SubLV = EmitLValueForField(LV, LargestFD); 548377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman AI = ExpandTypeFromArgs(LargestFD->getType(), SubLV, AI); 549eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 550eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } else { 551eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 552eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov i != e; ++i) { 553581deb3da481053c4993c7600f97acf7768caac5David Blaikie FieldDecl *FD = *i; 554eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov QualType FT = FD->getType(); 555eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov 556eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov // FIXME: What are the right qualifiers here? 557377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman LValue SubLV = EmitLValueForField(LV, FD); 558377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman AI = ExpandTypeFromArgs(FT, SubLV, AI); 559eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 5605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 561194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) { 562194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson QualType EltTy = CT->getElementType(); 563377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real"); 564194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy)); 565377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag"); 566194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy)); 567194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } else { 568194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson EmitStoreThroughLValue(RValue::get(AI), LV); 569194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson ++AI; 5705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 5715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 5725627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar return AI; 5735627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar} 5745627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 575e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are 57608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// accessing some number of bytes out of it, try to gep into the struct to get 57708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// at its inner goodness. Dive as deep as possible without entering an element 57808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// with an in-memory size smaller than DstSize. 57908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattnerstatic llvm::Value * 580e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris LattnerEnterStructPointerForCoercedAccess(llvm::Value *SrcPtr, 5812acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::StructType *SrcSTy, 582e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner uint64_t DstSize, CodeGenFunction &CGF) { 58308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // We can't dive into a zero-element struct. 58408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner if (SrcSTy->getNumElements() == 0) return SrcPtr; 5859cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 5862acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *FirstElt = SrcSTy->getElementType(0); 5879cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 58808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // If the first elt is at least as large as what we're looking for, or if the 58908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // first element is the same size as the whole struct, we can enter it. 5909cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer uint64_t FirstEltSize = 59108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner CGF.CGM.getTargetData().getTypeAllocSize(FirstElt); 5929cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer if (FirstEltSize < DstSize && 59308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy)) 59408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner return SrcPtr; 5959cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 59608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // GEP into the first element. 59708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive"); 5989cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 59908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner // If the first element is a struct, recurse. 6002acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *SrcTy = 60108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 6022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) 603e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 60408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 60508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner return SrcPtr; 60608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner} 60708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 6086d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both 6096d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// are either integers or pointers. This does a truncation of the value if it 6106d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// is too large or a zero extension if it is too small. 6116d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattnerstatic llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val, 6122acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *Ty, 6136d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner CodeGenFunction &CGF) { 6146d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (Val->getType() == Ty) 6156d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return Val; 6169cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6176d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(Val->getType())) { 6186d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // If this is Pointer->Pointer avoid conversion to and from int. 6196d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(Ty)) 6206d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val"); 6219cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6226d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // Convert the pointer to an integer so we can play with its width. 62377b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi"); 6246d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner } 6259cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6262acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *DestIntTy = Ty; 6276d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(DestIntTy)) 62877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner DestIntTy = CGF.IntPtrTy; 6299cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6306d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (Val->getType() != DestIntTy) 6316d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii"); 6329cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6336d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if (isa<llvm::PointerType>(Ty)) 6346d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip"); 6356d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return Val; 6366d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner} 6376d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner 63808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 63908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner 640275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as 641275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// a pointer to an object of type \arg Ty. 642275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// 643275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is smaller than the 644275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; in this situation the values of bits which not 645275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// present in the src are undefined. 646275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr, 6472acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *Ty, 648275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CodeGenFunction &CGF) { 6492acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *SrcTy = 650275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 6519cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6526ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner // If SrcTy and Ty are the same, just do a load. 6536ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner if (SrcTy == Ty) 6546ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner return CGF.Builder.CreateLoad(SrcPtr); 6559cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6569408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty); 6579cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 6582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) { 659e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF); 66008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType(); 66108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner } 6629cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 66308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 664275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 6656d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // If the source and destination are integer or pointer types, just do an 6666d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // extension or truncation to the desired type. 6676d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) && 6686d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) { 6696d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr); 6706d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF); 6716d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner } 6729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 673b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar // If load is legal, just bitcast the src pointer. 6747ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar if (SrcSize >= DstSize) { 675f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // Generally SrcSize is never greater than DstSize, since this means we are 676f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // losing bits. However, this can happen in cases where the structure has 677f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // additional padding, for example due to a user specified alignment. 6787ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar // 679f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: Assert that we aren't truncating non-padding bits when have access 680f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // to that information. 681275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *Casted = 682275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty)); 683386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 684386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar // FIXME: Use better alignment / avoid requiring aligned load. 685386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar Load->setAlignment(1); 686386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar return Load; 687275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar } 6889cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 68935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner // Otherwise do coercion through memory. This is stupid, but 69035b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner // simple. 69135b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner llvm::Value *Tmp = CGF.CreateTempAlloca(Ty); 69235b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner llvm::Value *Casted = 69335b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy)); 69435b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner llvm::StoreInst *Store = 69535b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted); 69635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner // FIXME: Use better alignment / avoid requiring aligned store. 69735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner Store->setAlignment(1); 69835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner return CGF.Builder.CreateLoad(Tmp); 699275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar} 700275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 701badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// Function to store a first-class aggregate into memory. We prefer to 702badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// store the elements rather than the aggregate to be more friendly to 703badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// fast-isel. 704badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// FIXME: Do we need to recurse here? 705badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedmanstatic void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val, 706badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::Value *DestPtr, bool DestIsVolatile, 707badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman bool LowAlignment) { 708badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman // Prefer scalar stores to first-class aggregate stores. 7092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (llvm::StructType *STy = 710badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman dyn_cast<llvm::StructType>(Val->getType())) { 711badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 712badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i); 713badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i); 714badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr, 715badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman DestIsVolatile); 716badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman if (LowAlignment) 717badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman SI->setAlignment(1); 718badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman } 719badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman } else { 72008212631e15a17157368af03180018601b31fb4dBill Wendling llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile); 72108212631e15a17157368af03180018601b31fb4dBill Wendling if (LowAlignment) 72208212631e15a17157368af03180018601b31fb4dBill Wendling SI->setAlignment(1); 723badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman } 724badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman} 725badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman 726275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src, 727275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// where the source and destination may have different types. 728275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// 729275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is larger than the 730275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; the upper bits of the src will be lost. 731275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic void CreateCoercedStore(llvm::Value *Src, 732275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *DstPtr, 733d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson bool DstIsVolatile, 734275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CodeGenFunction &CGF) { 7352acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *SrcTy = Src->getType(); 7362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *DstTy = 737275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 7386ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner if (SrcTy == DstTy) { 7396ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 7406ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner return; 7416ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner } 7429cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 7436ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy); 7449cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 7452acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) { 746e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF); 747e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType(); 748e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner } 7499cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 7506d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // If the source and destination are integer or pointer types, just do an 7516d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner // extension or truncation to the desired type. 7526d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) && 7536d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) { 7546d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF); 7556d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile); 7566d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner return; 7576d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner } 7589cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 7599408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy); 760275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 76188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar // If store is legal, just bitcast the src pointer. 762fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar if (SrcSize <= DstSize) { 763275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *Casted = 764275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy)); 765386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar // FIXME: Use better alignment / avoid requiring aligned store. 766badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman BuildAggStore(CGF, Src, Casted, DstIsVolatile, true); 767275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar } else { 768275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar // Otherwise do coercion through memory. This is stupid, but 769275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar // simple. 770fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar 771fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // Generally SrcSize is never greater than DstSize, since this means we are 772fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // losing bits. However, this can happen in cases where the structure has 773fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // additional padding, for example due to a user specified alignment. 774fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // 775fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // FIXME: Assert that we aren't truncating non-padding bits when have access 776fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar // to that information. 777275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy); 778275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateStore(Src, Tmp); 7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *Casted = 780275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy)); 781386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted); 782386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar // FIXME: Use better alignment / avoid requiring aligned load. 783386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar Load->setAlignment(1); 784d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile); 785275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar } 786275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar} 787275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar 7885627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/ 7895627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 790dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) { 79111e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar return FI.getReturnInfo().isIndirect(); 792bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar} 793bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar 794dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { 795dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) { 796dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar switch (BT->getKind()) { 797dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar default: 798dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar return false; 799dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar case BuiltinType::Float: 800bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Float); 801dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar case BuiltinType::Double: 802bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Double); 803dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar case BuiltinType::LongDouble: 804bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor return getContext().getTargetInfo().useObjCFPRetForRealType( 805dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar TargetInfo::LongDouble); 806dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar } 807dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar } 808dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar 809dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar return false; 810dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar} 811dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar 812eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlssonbool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { 813eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson if (const ComplexType *CT = ResultType->getAs<ComplexType>()) { 814eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) { 815eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson if (BT->getKind() == BuiltinType::LongDouble) 816eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson return getContext().getTargetInfo().useObjCFP2RetForComplexLongDouble(); 817eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson } 818eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson } 819eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson 820eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson return false; 821eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson} 822eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson 8239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { 824de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD); 825de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return GetFunctionType(FI); 826c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall} 827c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 8289cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::FunctionType * 829de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) { 83071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 83171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted; 83271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner assert(Inserted && "Recursively being processed?"); 83371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 8345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Type*, 8> argTypes; 8352acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *resultType = 0; 83645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 83742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const ABIArgInfo &retAI = FI.getReturnInfo(); 83842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall switch (retAI.getKind()) { 8398951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 84042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall llvm_unreachable("Invalid ABI kind for return argument"); 8418951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 842cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 84346327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: 84442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall resultType = retAI.getCoerceToType(); 84546327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar break; 84646327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar 84711e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar case ABIArgInfo::Indirect: { 84842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall assert(!retAI.getIndirectAlign() && "Align unused on indirect return."); 84942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall resultType = llvm::Type::getVoidTy(getLLVMContext()); 85042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall 85142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall QualType ret = FI.getReturnType(); 8522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *ty = ConvertType(ret); 85342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall unsigned addressSpace = Context.getTargetAddressSpace(ret); 85442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(llvm::PointerType::get(ty, addressSpace)); 85545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar break; 85645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar } 85745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 85811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 85942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall resultType = llvm::Type::getVoidTy(getLLVMContext()); 86011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar break; 86145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar } 8621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 86488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar ie = FI.arg_end(); it != ie; ++it) { 86542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall const ABIArgInfo &argAI = it->info; 8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 86742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall switch (argAI.getKind()) { 86811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 86911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar break; 87011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 871800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Indirect: { 872800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // indirect arguments are always on the stack, which is addr space #0. 8732acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *LTy = ConvertTypeForMem(it->type); 87442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(LTy->getPointerTo()); 875800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 876800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 877800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 878800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Extend: 8791ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner case ABIArgInfo::Direct: { 880f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka // Insert a padding type to ensure proper alignment. 881f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka if (llvm::Type *PaddingType = argAI.getPaddingType()) 882f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka argTypes.push_back(PaddingType); 883ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // If the coerce-to type is a first class aggregate, flatten it. Either 884ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // way is semantically identical, but fast-isel and the optimizer 885ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // generally likes scalar values better than FCAs. 8869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner llvm::Type *argType = argAI.getCoerceToType(); 8872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) { 88842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall for (unsigned i = 0, e = st->getNumElements(); i != e; ++i) 88942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(st->getElementType(i)); 890ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } else { 89142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall argTypes.push_back(argType); 892ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 89389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar break; 8941ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner } 8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8968951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 8979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner GetExpandedTypes(it->type, argTypes); 8988951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar break; 8998951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar } 90045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar } 90145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar 90271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased; 90371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner assert(Erased && "Not in set?"); 90471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner 905de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return llvm::FunctionType::get(resultType, argTypes, FI.isVariadic()); 9063913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar} 9073913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar 9082acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerllvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) { 9094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 910ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 9119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 912f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (!isFuncTypeConvertible(FPT)) 913f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner return llvm::StructType::get(getLLVMContext()); 914f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner 915f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner const CGFunctionInfo *Info; 916f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner if (isa<CXXDestructorDecl>(MD)) 917de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall Info = &arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), GD.getDtorType()); 918f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner else 919de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall Info = &arrangeCXXMethodDeclaration(MD); 920de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall return GetFunctionType(*Info); 921ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson} 922ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson 923a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbarvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, 92488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar const Decl *TargetDecl, 9259cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer AttributeListType &PAL, 926ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar unsigned &CallingConv) { 927c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany llvm::Attributes FuncAttrs; 928c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany llvm::Attributes RetAttrs; 9295323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar 930ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar CallingConv = FI.getEffectiveCallingConvention(); 931ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar 93204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall if (FI.isNoReturn()) 93304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall FuncAttrs |= llvm::Attribute::NoReturn; 93404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall 9351102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov // FIXME: handle sseregparm someday... 9365323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar if (TargetDecl) { 9376700415542121e2cb7d867728046ffa21e402019Rafael Espindola if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) 9386700415542121e2cb7d867728046ffa21e402019Rafael Espindola FuncAttrs |= llvm::Attribute::ReturnsTwice; 93940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TargetDecl->hasAttr<NoThrowAttr>()) 940761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel FuncAttrs |= llvm::Attribute::NoUnwind; 9419c0c1f333ab8f5a3da055b99ee94778689face17John McCall else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) { 9429c0c1f333ab8f5a3da055b99ee94778689face17John McCall const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>(); 9438026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl if (FPT && FPT->isNothrow(getContext())) 9449c0c1f333ab8f5a3da055b99ee94778689face17John McCall FuncAttrs |= llvm::Attribute::NoUnwind; 9459c0c1f333ab8f5a3da055b99ee94778689face17John McCall } 9469c0c1f333ab8f5a3da055b99ee94778689face17John McCall 94740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TargetDecl->hasAttr<NoReturnAttr>()) 948761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel FuncAttrs |= llvm::Attribute::NoReturn; 949041087caec03e8855770695d3eabc0feb031f6edEric Christopher 950f87cced71a955dca5731e7b28bc182e4824c0355Rafael Espindola if (TargetDecl->hasAttr<ReturnsTwiceAttr>()) 951f87cced71a955dca5731e7b28bc182e4824c0355Rafael Espindola FuncAttrs |= llvm::Attribute::ReturnsTwice; 952f87cced71a955dca5731e7b28bc182e4824c0355Rafael Espindola 953041087caec03e8855770695d3eabc0feb031f6edEric Christopher // 'const' and 'pure' attribute functions are also nounwind. 954041087caec03e8855770695d3eabc0feb031f6edEric Christopher if (TargetDecl->hasAttr<ConstAttr>()) { 955232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson FuncAttrs |= llvm::Attribute::ReadNone; 956041087caec03e8855770695d3eabc0feb031f6edEric Christopher FuncAttrs |= llvm::Attribute::NoUnwind; 957041087caec03e8855770695d3eabc0feb031f6edEric Christopher } else if (TargetDecl->hasAttr<PureAttr>()) { 95864c2e0762628eba26c100642521b6100c2515cc5Daniel Dunbar FuncAttrs |= llvm::Attribute::ReadOnly; 959041087caec03e8855770695d3eabc0feb031f6edEric Christopher FuncAttrs |= llvm::Attribute::NoUnwind; 960041087caec03e8855770695d3eabc0feb031f6edEric Christopher } 96176168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn if (TargetDecl->hasAttr<MallocAttr>()) 96276168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn RetAttrs |= llvm::Attribute::NoAlias; 9635323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 9645323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar 9652811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth if (CodeGenOpts.OptimizeSize) 9667ab1c3ebd02c31bfa1333cc51de1261c1499d6f7Daniel Dunbar FuncAttrs |= llvm::Attribute::OptimizeForSize; 9672811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth if (CodeGenOpts.DisableRedZone) 96824095dad88dd9d48aa16afa6416417073af251b5Devang Patel FuncAttrs |= llvm::Attribute::NoRedZone; 9692811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth if (CodeGenOpts.NoImplicitFloat) 970acebb397fa5d63835a0de9cee144987057ec1333Devang Patel FuncAttrs |= llvm::Attribute::NoImplicitFloat; 97124095dad88dd9d48aa16afa6416417073af251b5Devang Patel 972a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar QualType RetTy = FI.getReturnType(); 9735323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar unsigned Index = 1; 974b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &RetAI = FI.getReturnInfo(); 97545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar switch (RetAI.getKind()) { 976cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 9772eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner if (RetTy->hasSignedIntegerRepresentation()) 978cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov RetAttrs |= llvm::Attribute::SExt; 9792eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner else if (RetTy->hasUnsignedIntegerRepresentation()) 980cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov RetAttrs |= llvm::Attribute::ZExt; 981800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 98246327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: 983800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Ignore: 9842c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar break; 9852c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 986b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola case ABIArgInfo::Indirect: { 987b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola llvm::Attributes SRETAttrs = llvm::Attribute::StructRet; 988b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola if (RetAI.getInReg()) 989b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola SRETAttrs |= llvm::Attribute::InReg; 990b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola PAL.push_back(llvm::AttributeWithIndex::get(Index, SRETAttrs)); 991b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola 9925323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar ++Index; 9930ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar // sret disables readnone and readonly 9940ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar FuncAttrs &= ~(llvm::Attribute::ReadOnly | 9950ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar llvm::Attribute::ReadNone); 9962c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar break; 997b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola } 9982c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 9998951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 1000b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Invalid ABI kind for return argument"); 10015323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 10022c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 1003a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel if (RetAttrs) 1004a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs)); 10051102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov 10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), 100788c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar ie = FI.arg_end(); it != ie; ++it) { 100888c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar QualType ParamType = it->type; 100988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar const ABIArgInfo &AI = it->info; 1010c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany llvm::Attributes Attrs; 10111102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov 1012d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we 1013d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall // have the corresponding parameter variable. It doesn't make 10147f6890ebb874cc16320259daef50f1b4cfdc47d5Daniel Dunbar // sense to do it here because parameters are so messed up. 10158951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar switch (AI.getKind()) { 1016cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 1017575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor if (ParamType->isSignedIntegerOrEnumerationType()) 1018c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany Attrs |= llvm::Attribute::SExt; 1019575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor else if (ParamType->isUnsignedIntegerOrEnumerationType()) 1020c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany Attrs |= llvm::Attribute::ZExt; 1021800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // FALL THROUGH 102246327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: 1023b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola if (AI.getInReg()) 1024c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany Attrs |= llvm::Attribute::InReg; 1025b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola 10261102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov // FIXME: handle sseregparm someday... 10279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1028f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka // Increment Index if there is padding. 1029f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka Index += (AI.getPaddingType() != 0); 1030f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka 10312acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (llvm::StructType *STy = 1032b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola dyn_cast<llvm::StructType>(AI.getCoerceToType())) { 1033b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola unsigned Extra = STy->getNumElements()-1; // 1 will be added below. 1034b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola if (Attrs != llvm::Attribute::None) 1035b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola for (unsigned I = 0; I < Extra; ++I) 1036b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola PAL.push_back(llvm::AttributeWithIndex::get(Index + I, Attrs)); 1037b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola Index += Extra; 1038b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola } 1039800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 1040800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1041800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Indirect: 1042800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (AI.getIndirectByVal()) 1043c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany Attrs |= llvm::Attribute::ByVal; 1044800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1045c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany Attrs |= 1046800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign()); 1047800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // byval disables readnone and readonly. 1048800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner FuncAttrs &= ~(llvm::Attribute::ReadOnly | 1049800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Attribute::ReadNone); 10508951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar break; 10511102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov 105211434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 105311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar // Skip increment, no matching LLVM parameter. 10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump continue; 105511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 10565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar case ABIArgInfo::Expand: { 10575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Type*, 8> types; 1058f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: This is rather inefficient. Do we ever actually need to do 1059f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // anything here? The result should be just reconstructed on the other 1060f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // side, so extension should be a non-issue. 10619cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner getTypes().GetExpandedTypes(ParamType, types); 106242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall Index += types.size(); 10635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar continue; 10645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar } 10655323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 10661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1067c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany if (Attrs) 1068c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany PAL.push_back(llvm::AttributeWithIndex::get(Index, Attrs)); 10695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar ++Index; 10705323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar } 1071a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel if (FuncAttrs) 1072a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs)); 10735323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar} 10745323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar 1075d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// An argument came in as a promoted argument; demote it back to its 1076d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// declared type. 1077d26bc76c98006609002d9930f8840490e88ac5b5John McCallstatic llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF, 1078d26bc76c98006609002d9930f8840490e88ac5b5John McCall const VarDecl *var, 1079d26bc76c98006609002d9930f8840490e88ac5b5John McCall llvm::Value *value) { 10802acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *varType = CGF.ConvertType(var->getType()); 1081d26bc76c98006609002d9930f8840490e88ac5b5John McCall 1082d26bc76c98006609002d9930f8840490e88ac5b5John McCall // This can happen with promotions that actually don't change the 1083d26bc76c98006609002d9930f8840490e88ac5b5John McCall // underlying type, like the enum promotions. 1084d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (value->getType() == varType) return value; 1085d26bc76c98006609002d9930f8840490e88ac5b5John McCall 1086d26bc76c98006609002d9930f8840490e88ac5b5John McCall assert((varType->isIntegerTy() || varType->isFloatingPointTy()) 1087d26bc76c98006609002d9930f8840490e88ac5b5John McCall && "unexpected promotion type"); 1088d26bc76c98006609002d9930f8840490e88ac5b5John McCall 1089d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isa<llvm::IntegerType>(varType)) 1090d26bc76c98006609002d9930f8840490e88ac5b5John McCall return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote"); 1091d26bc76c98006609002d9930f8840490e88ac5b5John McCall 1092d26bc76c98006609002d9930f8840490e88ac5b5John McCall return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote"); 1093d26bc76c98006609002d9930f8840490e88ac5b5John McCall} 1094d26bc76c98006609002d9930f8840490e88ac5b5John McCall 109588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, 109688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar llvm::Function *Fn, 109717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar const FunctionArgList &Args) { 10980cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // If this is an implicit-return-zero function, go ahead and 10990cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // initialize the return value. TODO: it might be nice to have 11000cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // a more general mechanism for this that didn't require synthesized 11010cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall // return statements. 1102121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) { 11030cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall if (FD->hasImplicitReturnZero()) { 11040cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall QualType RetTy = FD->getResultType().getUnqualifiedType(); 11052acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy); 1106c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy); 11070cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall Builder.CreateStore(Zero, ReturnValue); 11080cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall } 11090cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall } 11100cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall 1111f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We no longer need the types from FunctionArgList; lift up and 1112f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // simplify. 11135251afa21d3583f2740fd4f83659d008625a7260Daniel Dunbar 111417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // Emit allocs for param decls. Give the LLVM Argument nodes names. 111517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar llvm::Function::arg_iterator AI = Fn->arg_begin(); 11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 111717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // Name the struct return argument. 1118dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar if (CGM.ReturnTypeUsesSRet(FI)) { 111917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar AI->setName("agg.result"); 1120410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall AI->addAttr(llvm::Attribute::NoAlias); 112117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar ++AI; 112217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11244b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar assert(FI.arg_size() == Args.size() && 11254b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar "Mismatch between function signature & arguments."); 1126093ac461b37a573dcf226fa55faed96f318169b9Devang Patel unsigned ArgNo = 1; 1127b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin(); 1128093ac461b37a573dcf226fa55faed96f318169b9Devang Patel for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); 1129093ac461b37a573dcf226fa55faed96f318169b9Devang Patel i != e; ++i, ++info_it, ++ArgNo) { 1130d26bc76c98006609002d9930f8840490e88ac5b5John McCall const VarDecl *Arg = *i; 1131b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar QualType Ty = info_it->type; 1132b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &ArgI = info_it->info; 11338951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 1134d26bc76c98006609002d9930f8840490e88ac5b5John McCall bool isPromoted = 1135d26bc76c98006609002d9930f8840490e88ac5b5John McCall isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted(); 1136d26bc76c98006609002d9930f8840490e88ac5b5John McCall 11378951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar switch (ArgI.getKind()) { 11381f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar case ABIArgInfo::Indirect: { 1139ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner llvm::Value *V = AI; 1140cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar 11411f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar if (hasAggregateLLVMType(Ty)) { 1142cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // Aggregates and complex variables are accessed by reference. All we 1143cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // need to do is realign the value, if requested 1144cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar if (ArgI.getIndirectRealign()) { 1145cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce"); 1146cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar 1147cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // Copy from the incoming argument pointer to the temporary with the 1148cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // appropriate alignment. 1149cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // 1150cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // FIXME: We should have a common utility for generating an aggregate 1151cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar // copy. 11522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); 1153fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck CharUnits Size = getContext().getTypeSizeInChars(Ty); 1154c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); 1155c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy); 1156c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi Builder.CreateMemCpy(Dst, 1157c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi Src, 1158fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck llvm::ConstantInt::get(IntPtrTy, 1159fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck Size.getQuantity()), 11609f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer ArgI.getIndirectAlign(), 11619f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer false); 1162cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar V = AlignedTemp; 1163cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar } 11641f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } else { 11651f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar // Load scalar value from indirect argument. 1166fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck CharUnits Alignment = getContext().getTypeAlignInChars(Ty); 1167fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty); 1168d26bc76c98006609002d9930f8840490e88ac5b5John McCall 1169d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isPromoted) 1170d26bc76c98006609002d9930f8840490e88ac5b5John McCall V = emitArgumentDemotion(*this, Arg, V); 11711f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } 1172093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, V, ArgNo); 11731f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar break; 11741f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } 1175cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov 1176cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov case ABIArgInfo::Extend: 117746327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar case ABIArgInfo::Direct: { 11784ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka // Skip the dummy padding argument. 11794ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka if (ArgI.getPaddingType()) 11804ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka ++AI; 11814ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka 1182800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // If we have the trivial case, handle it with no muss and fuss. 1183800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (!isa<llvm::StructType>(ArgI.getCoerceToType()) && 1184117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgI.getCoerceToType() == ConvertType(Ty) && 1185117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgI.getDirectOffset() == 0) { 1186800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner assert(AI != Fn->arg_end() && "Argument mismatch!"); 1187800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *V = AI; 11889cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1189d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall if (Arg->getType().isRestrictQualified()) 1190d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall AI->addAttr(llvm::Attribute::NoAlias); 1191d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall 1192b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner // Ensure the argument is the correct type. 1193b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner if (V->getType() != ArgI.getCoerceToType()) 1194b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner V = Builder.CreateBitCast(V, ArgI.getCoerceToType()); 1195b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner 1196d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isPromoted) 1197d26bc76c98006609002d9930f8840490e88ac5b5John McCall V = emitArgumentDemotion(*this, Arg, V); 1198b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner 1199093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, V, ArgNo); 1200800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 120117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1203a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName()); 12049cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1205deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner // The alignment we need to use is the max of the requested alignment for 1206deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner // the argument plus the alignment required by our access code below. 12079cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer unsigned AlignmentToUse = 1208d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType()); 1209deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner AlignmentToUse = std::max(AlignmentToUse, 1210deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner (unsigned)getContext().getDeclAlign(Arg).getQuantity()); 12119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1212deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner Alloca->setAlignment(AlignmentToUse); 1213121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner llvm::Value *V = Alloca; 1214117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::Value *Ptr = V; // Pointer to store into. 12159cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1216117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 1217117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = ArgI.getDirectOffset()) { 1218117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy()); 1219117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner Ptr = Builder.CreateConstGEP1_32(Ptr, Offs); 12209cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer Ptr = Builder.CreateBitCast(Ptr, 1221117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(ArgI.getCoerceToType())); 1222117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 12239cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1224ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // If the coerce-to type is a first class aggregate, we flatten it and 1225ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // pass the elements. Either way is semantically identical, but fast-isel 1226ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // and the optimizer generally likes scalar values better than FCAs. 1227a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType()); 1228a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov if (STy && STy->getNumElements() > 1) { 1229a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov uint64_t SrcSize = CGM.getTargetData().getTypeAllocSize(STy); 1230a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov llvm::Type *DstTy = 1231a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov cast<llvm::PointerType>(Ptr->getType())->getElementType(); 1232a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov uint64_t DstSize = CGM.getTargetData().getTypeAllocSize(DstTy); 1233a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov 1234a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov if (SrcSize <= DstSize) { 1235a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy)); 1236a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov 1237a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1238a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov assert(AI != Fn->arg_end() && "Argument mismatch!"); 1239a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1240a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i); 1241a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov Builder.CreateStore(AI++, EltPtr); 1242a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov } 1243a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov } else { 1244a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov llvm::AllocaInst *TempAlloca = 1245a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov CreateTempAlloca(ArgI.getCoerceToType(), "coerce"); 1246a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov TempAlloca->setAlignment(AlignmentToUse); 1247a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov llvm::Value *TempV = TempAlloca; 1248a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov 1249a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 1250a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov assert(AI != Fn->arg_end() && "Argument mismatch!"); 1251a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov AI->setName(Arg->getName() + ".coerce" + Twine(i)); 1252a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i); 1253a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov Builder.CreateStore(AI++, EltPtr); 1254a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov } 1255a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov 1256a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse); 1257ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 1258ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } else { 1259309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner // Simple case, just do a coerced store of the argument into the alloca. 1260ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner assert(AI != Fn->arg_end() && "Argument mismatch!"); 1261225e286110bcc8b7b1ff8b35f0d51a10a158b18cChris Lattner AI->setName(Arg->getName() + ".coerce"); 1262117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this); 1263ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 12649cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 12659cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 126689c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar // Match to what EmitParmDecl is expecting for this type. 12678b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar if (!CodeGenFunction::hasAggregateLLVMType(Ty)) { 126891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty); 1269d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (isPromoted) 1270d26bc76c98006609002d9930f8840490e88ac5b5John McCall V = emitArgumentDemotion(*this, Arg, V); 12718b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar } 1272093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, V, ArgNo); 1273ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner continue; // Skip ++AI increment, already done. 127489c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar } 1275800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1276800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Expand: { 1277800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // If this structure was expanded into multiple arguments then 1278800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // we need to create a temporary and reconstruct it from the 1279800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // arguments. 12801bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman llvm::AllocaInst *Alloca = CreateMemTemp(Ty); 12816da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman CharUnits Align = getContext().getDeclAlign(Arg); 12826da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman Alloca->setAlignment(Align.getQuantity()); 12836da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman LValue LV = MakeAddrLValue(Alloca, Ty, Align); 12841bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI); 12851bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman EmitParmDecl(*Arg, Alloca, ArgNo); 1286800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1287800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Name the arguments used in expansion and increment AI. 1288800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner unsigned Index = 0; 1289800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner for (; AI != End; ++AI, ++Index) 12905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner AI->setName(Arg->getName() + "." + Twine(Index)); 1291800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner continue; 1292800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 1293800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1294800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Ignore: 1295800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Initialize the local variable appropriately. 1296800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (hasAggregateLLVMType(Ty)) 1297093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo); 1298800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner else 1299093ac461b37a573dcf226fa55faed96f318169b9Devang Patel EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())), 1300093ac461b37a573dcf226fa55faed96f318169b9Devang Patel ArgNo); 1301800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner 1302800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Skip increment, no matching LLVM parameter. 1303800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner continue; 13048951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar } 13055627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 13065627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar ++AI; 130717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 130817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar assert(AI == Fn->arg_end() && "Argument mismatch!"); 130917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar} 131017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 131177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCallstatic void eraseUnusedBitCasts(llvm::Instruction *insn) { 131277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall while (insn->use_empty()) { 131377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn); 131477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall if (!bitcast) return; 131577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 131677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // This is "safe" because we would have used a ConstantExpr otherwise. 131777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall insn = cast<llvm::Instruction>(bitcast->getOperand(0)); 131877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall bitcast->eraseFromParent(); 131977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall } 132077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall} 132177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 1322f85e193739c953358c865005855253af4f68a497John McCall/// Try to emit a fused autorelease of a return result. 1323f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF, 1324f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result) { 1325f85e193739c953358c865005855253af4f68a497John McCall // We must be immediately followed the cast. 1326f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock(); 1327f85e193739c953358c865005855253af4f68a497John McCall if (BB->empty()) return 0; 1328f85e193739c953358c865005855253af4f68a497John McCall if (&BB->back() != result) return 0; 1329f85e193739c953358c865005855253af4f68a497John McCall 13302acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *resultType = result->getType(); 1331f85e193739c953358c865005855253af4f68a497John McCall 1332f85e193739c953358c865005855253af4f68a497John McCall // result is in a BasicBlock and is therefore an Instruction. 1333f85e193739c953358c865005855253af4f68a497John McCall llvm::Instruction *generator = cast<llvm::Instruction>(result); 1334f85e193739c953358c865005855253af4f68a497John McCall 13355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Instruction*,4> insnsToKill; 1336f85e193739c953358c865005855253af4f68a497John McCall 1337f85e193739c953358c865005855253af4f68a497John McCall // Look for: 1338f85e193739c953358c865005855253af4f68a497John McCall // %generator = bitcast %type1* %generator2 to %type2* 1339f85e193739c953358c865005855253af4f68a497John McCall while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) { 1340f85e193739c953358c865005855253af4f68a497John McCall // We would have emitted this as a constant if the operand weren't 1341f85e193739c953358c865005855253af4f68a497John McCall // an Instruction. 1342f85e193739c953358c865005855253af4f68a497John McCall generator = cast<llvm::Instruction>(bitcast->getOperand(0)); 1343f85e193739c953358c865005855253af4f68a497John McCall 1344f85e193739c953358c865005855253af4f68a497John McCall // Require the generator to be immediately followed by the cast. 1345f85e193739c953358c865005855253af4f68a497John McCall if (generator->getNextNode() != bitcast) 1346f85e193739c953358c865005855253af4f68a497John McCall return 0; 1347f85e193739c953358c865005855253af4f68a497John McCall 1348f85e193739c953358c865005855253af4f68a497John McCall insnsToKill.push_back(bitcast); 1349f85e193739c953358c865005855253af4f68a497John McCall } 1350f85e193739c953358c865005855253af4f68a497John McCall 1351f85e193739c953358c865005855253af4f68a497John McCall // Look for: 1352f85e193739c953358c865005855253af4f68a497John McCall // %generator = call i8* @objc_retain(i8* %originalResult) 1353f85e193739c953358c865005855253af4f68a497John McCall // or 1354f85e193739c953358c865005855253af4f68a497John McCall // %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult) 1355f85e193739c953358c865005855253af4f68a497John McCall llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator); 1356f85e193739c953358c865005855253af4f68a497John McCall if (!call) return 0; 1357f85e193739c953358c865005855253af4f68a497John McCall 1358f85e193739c953358c865005855253af4f68a497John McCall bool doRetainAutorelease; 1359f85e193739c953358c865005855253af4f68a497John McCall 1360f85e193739c953358c865005855253af4f68a497John McCall if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) { 1361f85e193739c953358c865005855253af4f68a497John McCall doRetainAutorelease = true; 1362f85e193739c953358c865005855253af4f68a497John McCall } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints() 1363f85e193739c953358c865005855253af4f68a497John McCall .objc_retainAutoreleasedReturnValue) { 1364f85e193739c953358c865005855253af4f68a497John McCall doRetainAutorelease = false; 1365f85e193739c953358c865005855253af4f68a497John McCall 1366f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall // If we emitted an assembly marker for this call (and the 1367f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall // ARCEntrypoints field should have been set if so), go looking 1368f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall // for that call. If we can't find it, we can't do this 1369f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall // optimization. But it should always be the immediately previous 1370f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall // instruction, unless we needed bitcasts around the call. 1371f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) { 1372f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall llvm::Instruction *prev = call->getPrevNode(); 1373f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall assert(prev); 1374f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall if (isa<llvm::BitCastInst>(prev)) { 1375f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall prev = prev->getPrevNode(); 1376f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall assert(prev); 1377f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall } 1378f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall assert(isa<llvm::CallInst>(prev)); 1379f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall assert(cast<llvm::CallInst>(prev)->getCalledValue() == 1380f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker); 1381f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall insnsToKill.push_back(prev); 1382f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall } 1383f85e193739c953358c865005855253af4f68a497John McCall } else { 1384f85e193739c953358c865005855253af4f68a497John McCall return 0; 1385f85e193739c953358c865005855253af4f68a497John McCall } 1386f85e193739c953358c865005855253af4f68a497John McCall 1387f85e193739c953358c865005855253af4f68a497John McCall result = call->getArgOperand(0); 1388f85e193739c953358c865005855253af4f68a497John McCall insnsToKill.push_back(call); 1389f85e193739c953358c865005855253af4f68a497John McCall 1390f85e193739c953358c865005855253af4f68a497John McCall // Keep killing bitcasts, for sanity. Note that we no longer care 1391f85e193739c953358c865005855253af4f68a497John McCall // about precise ordering as long as there's exactly one use. 1392f85e193739c953358c865005855253af4f68a497John McCall while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) { 1393f85e193739c953358c865005855253af4f68a497John McCall if (!bitcast->hasOneUse()) break; 1394f85e193739c953358c865005855253af4f68a497John McCall insnsToKill.push_back(bitcast); 1395f85e193739c953358c865005855253af4f68a497John McCall result = bitcast->getOperand(0); 1396f85e193739c953358c865005855253af4f68a497John McCall } 1397f85e193739c953358c865005855253af4f68a497John McCall 1398f85e193739c953358c865005855253af4f68a497John McCall // Delete all the unnecessary instructions, from latest to earliest. 13995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner for (SmallVectorImpl<llvm::Instruction*>::iterator 1400f85e193739c953358c865005855253af4f68a497John McCall i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i) 1401f85e193739c953358c865005855253af4f68a497John McCall (*i)->eraseFromParent(); 1402f85e193739c953358c865005855253af4f68a497John McCall 1403f85e193739c953358c865005855253af4f68a497John McCall // Do the fused retain/autorelease if we were asked to. 1404f85e193739c953358c865005855253af4f68a497John McCall if (doRetainAutorelease) 1405f85e193739c953358c865005855253af4f68a497John McCall result = CGF.EmitARCRetainAutoreleaseReturnValue(result); 1406f85e193739c953358c865005855253af4f68a497John McCall 1407f85e193739c953358c865005855253af4f68a497John McCall // Cast back to the result type. 1408f85e193739c953358c865005855253af4f68a497John McCall return CGF.Builder.CreateBitCast(result, resultType); 1409f85e193739c953358c865005855253af4f68a497John McCall} 1410f85e193739c953358c865005855253af4f68a497John McCall 141177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall/// If this is a +1 of the value of an immutable 'self', remove it. 141277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCallstatic llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF, 141377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall llvm::Value *result) { 141477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // This is only applicable to a method with an immutable 'self'. 1415bd9b65ae534cb11aa39737aa43ab82bb29e078f6John McCall const ObjCMethodDecl *method = 1416bd9b65ae534cb11aa39737aa43ab82bb29e078f6John McCall dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl); 141777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall if (!method) return 0; 141877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall const VarDecl *self = method->getSelfDecl(); 141977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall if (!self->getType().isConstQualified()) return 0; 142077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 142177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // Look for a retain call. 142277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall llvm::CallInst *retainCall = 142377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall dyn_cast<llvm::CallInst>(result->stripPointerCasts()); 142477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall if (!retainCall || 142577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain) 142677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall return 0; 142777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 142877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // Look for an ordinary load of 'self'. 142977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall llvm::Value *retainedValue = retainCall->getArgOperand(0); 143077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall llvm::LoadInst *load = 143177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts()); 143277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall if (!load || load->isAtomic() || load->isVolatile() || 143377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall load->getPointerOperand() != CGF.GetAddrOfLocalVar(self)) 143477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall return 0; 143577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 143677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // Okay! Burn it all down. This relies for correctness on the 143777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // assumption that the retain is emitted as part of the return and 143877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // that thereafter everything is used "linearly". 143977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall llvm::Type *resultType = result->getType(); 144077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall eraseUnusedBitCasts(cast<llvm::Instruction>(result)); 144177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall assert(retainCall->use_empty()); 144277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall retainCall->eraseFromParent(); 144377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue)); 144477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 144577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall return CGF.Builder.CreateBitCast(load, resultType); 144677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall} 144777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 1448f85e193739c953358c865005855253af4f68a497John McCall/// Emit an ARC autorelease of the result of a function. 144977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall/// 145077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall/// \return the value to actually return from the function 1451f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF, 1452f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *result) { 145377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // If we're returning 'self', kill the initial retain. This is a 145477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // heuristic attempt to "encourage correctness" in the really unfortunate 145577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // case where we have a return of self during a dealloc and we desperately 145677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall // need to avoid the possible autorelease. 145777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result)) 145877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall return self; 145977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall 1460f85e193739c953358c865005855253af4f68a497John McCall // At -O0, try to emit a fused retain/autorelease. 1461f85e193739c953358c865005855253af4f68a497John McCall if (CGF.shouldUseFusedARCCalls()) 1462f85e193739c953358c865005855253af4f68a497John McCall if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result)) 1463f85e193739c953358c865005855253af4f68a497John McCall return fused; 1464f85e193739c953358c865005855253af4f68a497John McCall 1465f85e193739c953358c865005855253af4f68a497John McCall return CGF.EmitARCAutoreleaseReturnValue(result); 1466f85e193739c953358c865005855253af4f68a497John McCall} 1467f85e193739c953358c865005855253af4f68a497John McCall 1468f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall/// Heuristically search for a dominating store to the return-value slot. 1469f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCallstatic llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) { 1470f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // If there are multiple uses of the return-value slot, just check 1471f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // for something immediately preceding the IP. Sometimes this can 1472f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // happen with how we generate implicit-returns; it can also happen 1473f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // with noreturn cleanups. 1474f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall if (!CGF.ReturnValue->hasOneUse()) { 1475f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 1476f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall if (IP->empty()) return 0; 1477f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back()); 1478f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall if (!store) return 0; 1479f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall if (store->getPointerOperand() != CGF.ReturnValue) return 0; 1480f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall assert(!store->isAtomic() && !store->isVolatile()); // see below 1481f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall return store; 1482f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall } 1483f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall 1484f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall llvm::StoreInst *store = 1485f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall dyn_cast<llvm::StoreInst>(CGF.ReturnValue->use_back()); 1486f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall if (!store) return 0; 1487f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall 1488f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // These aren't actually possible for non-coerced returns, and we 1489f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // only care about non-coerced returns on this code path. 1490f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall assert(!store->isAtomic() && !store->isVolatile()); 1491f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall 1492f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // Now do a first-and-dirty dominance check: just walk up the 1493f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // single-predecessors chain from the current insertion point. 1494f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall llvm::BasicBlock *StoreBB = store->getParent(); 1495f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock(); 1496f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall while (IP != StoreBB) { 1497f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall if (!(IP = IP->getSinglePredecessor())) 1498f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall return 0; 1499f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall } 1500f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall 1501f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // Okay, the store's basic block dominates the insertion point; we 1502f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // can do our thing. 1503f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall return store; 1504f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall} 1505f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall 150635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattnervoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) { 15072c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar // Functions with no result always return void. 1508c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner if (ReturnValue == 0) { 1509c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner Builder.CreateRetVoid(); 1510c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner return; 1511c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } 151221fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar 15134751a53c5e5fed4bf2271e29cae7411c93a77df7Dan Gohman llvm::DebugLoc RetDbgLoc; 1514c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner llvm::Value *RV = 0; 1515c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner QualType RetTy = FI.getReturnType(); 1516c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner const ABIArgInfo &RetAI = FI.getReturnInfo(); 1517cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov 1518c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner switch (RetAI.getKind()) { 151991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar case ABIArgInfo::Indirect: { 152091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 1521c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner if (RetTy->isAnyComplexType()) { 1522c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false); 1523c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner StoreComplexToAddr(RT, CurFn->arg_begin(), false); 1524c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 1525c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner // Do nothing; aggregrates get evaluated directly into the destination. 1526c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } else { 1527c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(), 152891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar false, Alignment, RetTy); 1529c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner } 1530c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner break; 153191a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar } 15328951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 1533c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner case ABIArgInfo::Extend: 1534800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Direct: 1535117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (RetAI.getCoerceToType() == ConvertType(RetTy) && 1536117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner RetAI.getDirectOffset() == 0) { 1537800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // The internal return value temp always will have pointer-to-return-type 1538800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // type, just do a load. 15399cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1540f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // If there is a dominating store to ReturnValue, we can elide 1541f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // the load, zap the store, and usually zap the alloca. 1542f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) { 1543800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // Get the stored value and nuke the now-dead store. 1544800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner RetDbgLoc = SI->getDebugLoc(); 1545800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner RV = SI->getValueOperand(); 1546800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner SI->eraseFromParent(); 15479cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1548800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner // If that was the only use of the return value, nuke it as well now. 1549800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) { 1550800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent(); 1551800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner ReturnValue = 0; 1552800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 1553f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall 1554f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall // Otherwise, we have to do a simple load. 1555f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall } else { 1556f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall RV = Builder.CreateLoad(ReturnValue); 155735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner } 1558800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } else { 1559117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::Value *V = ReturnValue; 1560117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 1561117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = RetAI.getDirectOffset()) { 1562117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner V = Builder.CreateBitCast(V, Builder.getInt8PtrTy()); 1563117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner V = Builder.CreateConstGEP1_32(V, Offs); 15649cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer V = Builder.CreateBitCast(V, 1565117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 1566117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 15679cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 1568117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this); 156935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner } 1570f85e193739c953358c865005855253af4f68a497John McCall 1571f85e193739c953358c865005855253af4f68a497John McCall // In ARC, end functions that return a retainable type with a call 1572f85e193739c953358c865005855253af4f68a497John McCall // to objc_autoreleaseReturnValue. 1573f85e193739c953358c865005855253af4f68a497John McCall if (AutoreleaseResult) { 15744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getLangOpts().ObjCAutoRefCount && 1575f85e193739c953358c865005855253af4f68a497John McCall !FI.isReturnsRetained() && 1576f85e193739c953358c865005855253af4f68a497John McCall RetTy->isObjCRetainableType()); 1577f85e193739c953358c865005855253af4f68a497John McCall RV = emitAutoreleaseOfResult(*this, RV); 1578f85e193739c953358c865005855253af4f68a497John McCall } 1579f85e193739c953358c865005855253af4f68a497John McCall 1580c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner break; 15811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1582800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Ignore: 1583c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner break; 15848951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 1585c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner case ABIArgInfo::Expand: 1586b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Invalid ABI kind for return argument"); 158717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 15881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 158921fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid(); 1590d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel if (!RetDbgLoc.isUnknown()) 1591d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel Ret->setDebugLoc(RetDbgLoc); 159217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar} 159317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 1594413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitDelegateCallArg(CallArgList &args, 1595413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall const VarDecl *param) { 15962736071ea3a46f90e65c93418961611d96c10ab9John McCall // StartFunction converted the ABI-lowered parameter(s) into a 15972736071ea3a46f90e65c93418961611d96c10ab9John McCall // local alloca. We need to turn that into an r-value suitable 15982736071ea3a46f90e65c93418961611d96c10ab9John McCall // for EmitCall. 1599413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall llvm::Value *local = GetAddrOfLocalVar(param); 16002736071ea3a46f90e65c93418961611d96c10ab9John McCall 1601413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall QualType type = param->getType(); 16029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 16032736071ea3a46f90e65c93418961611d96c10ab9John McCall // For the most part, we just need to load the alloca, except: 16042736071ea3a46f90e65c93418961611d96c10ab9John McCall // 1) aggregate r-values are actually pointers to temporaries, and 16052736071ea3a46f90e65c93418961611d96c10ab9John McCall // 2) references to aggregates are pointers directly to the aggregate. 16062736071ea3a46f90e65c93418961611d96c10ab9John McCall // I don't know why references to non-aggregates are different here. 1607413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (const ReferenceType *ref = type->getAs<ReferenceType>()) { 1608413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (hasAggregateLLVMType(ref->getPointeeType())) 1609413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::getAggregate(local), type); 16102736071ea3a46f90e65c93418961611d96c10ab9John McCall 16112736071ea3a46f90e65c93418961611d96c10ab9John McCall // Locals which are references to scalars are represented 16122736071ea3a46f90e65c93418961611d96c10ab9John McCall // with allocas holding the pointer. 1613413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::get(Builder.CreateLoad(local)), type); 16142736071ea3a46f90e65c93418961611d96c10ab9John McCall } 16152736071ea3a46f90e65c93418961611d96c10ab9John McCall 1616413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (type->isAnyComplexType()) { 1617413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false); 1618413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::getComplex(complex), type); 1619413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall } 16202736071ea3a46f90e65c93418961611d96c10ab9John McCall 1621413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall if (hasAggregateLLVMType(type)) 1622413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::getAggregate(local), type); 16232736071ea3a46f90e65c93418961611d96c10ab9John McCall 1624413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall unsigned alignment = getContext().getDeclAlign(param).getQuantity(); 1625413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type); 1626413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(RValue::get(value), type); 16272736071ea3a46f90e65c93418961611d96c10ab9John McCall} 16282736071ea3a46f90e65c93418961611d96c10ab9John McCall 1629f85e193739c953358c865005855253af4f68a497John McCallstatic bool isProvablyNull(llvm::Value *addr) { 1630f85e193739c953358c865005855253af4f68a497John McCall return isa<llvm::ConstantPointerNull>(addr); 1631f85e193739c953358c865005855253af4f68a497John McCall} 1632f85e193739c953358c865005855253af4f68a497John McCall 1633f85e193739c953358c865005855253af4f68a497John McCallstatic bool isProvablyNonNull(llvm::Value *addr) { 1634f85e193739c953358c865005855253af4f68a497John McCall return isa<llvm::AllocaInst>(addr); 1635f85e193739c953358c865005855253af4f68a497John McCall} 1636f85e193739c953358c865005855253af4f68a497John McCall 1637f85e193739c953358c865005855253af4f68a497John McCall/// Emit the actual writing-back of a writeback. 1638f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWriteback(CodeGenFunction &CGF, 1639f85e193739c953358c865005855253af4f68a497John McCall const CallArgList::Writeback &writeback) { 1640f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *srcAddr = writeback.Address; 1641f85e193739c953358c865005855253af4f68a497John McCall assert(!isProvablyNull(srcAddr) && 1642f85e193739c953358c865005855253af4f68a497John McCall "shouldn't have writeback for provably null argument"); 1643f85e193739c953358c865005855253af4f68a497John McCall 1644f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *contBB = 0; 1645f85e193739c953358c865005855253af4f68a497John McCall 1646f85e193739c953358c865005855253af4f68a497John McCall // If the argument wasn't provably non-null, we need to null check 1647f85e193739c953358c865005855253af4f68a497John McCall // before doing the store. 1648f85e193739c953358c865005855253af4f68a497John McCall bool provablyNonNull = isProvablyNonNull(srcAddr); 1649f85e193739c953358c865005855253af4f68a497John McCall if (!provablyNonNull) { 1650f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback"); 1651f85e193739c953358c865005855253af4f68a497John McCall contBB = CGF.createBasicBlock("icr.done"); 1652f85e193739c953358c865005855253af4f68a497John McCall 1653f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 1654f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.CreateCondBr(isNull, contBB, writebackBB); 1655f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitBlock(writebackBB); 1656f85e193739c953358c865005855253af4f68a497John McCall } 1657f85e193739c953358c865005855253af4f68a497John McCall 1658f85e193739c953358c865005855253af4f68a497John McCall // Load the value to writeback. 1659f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary); 1660f85e193739c953358c865005855253af4f68a497John McCall 1661f85e193739c953358c865005855253af4f68a497John McCall // Cast it back, in case we're writing an id to a Foo* or something. 1662f85e193739c953358c865005855253af4f68a497John McCall value = CGF.Builder.CreateBitCast(value, 1663f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(srcAddr->getType())->getElementType(), 1664f85e193739c953358c865005855253af4f68a497John McCall "icr.writeback-cast"); 1665f85e193739c953358c865005855253af4f68a497John McCall 1666f85e193739c953358c865005855253af4f68a497John McCall // Perform the writeback. 1667f85e193739c953358c865005855253af4f68a497John McCall QualType srcAddrType = writeback.AddressType; 1668f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitStoreThroughLValue(RValue::get(value), 1669545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall CGF.MakeAddrLValue(srcAddr, srcAddrType)); 1670f85e193739c953358c865005855253af4f68a497John McCall 1671f85e193739c953358c865005855253af4f68a497John McCall // Jump to the continuation block. 1672f85e193739c953358c865005855253af4f68a497John McCall if (!provablyNonNull) 1673f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitBlock(contBB); 1674f85e193739c953358c865005855253af4f68a497John McCall} 1675f85e193739c953358c865005855253af4f68a497John McCall 1676f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWritebacks(CodeGenFunction &CGF, 1677f85e193739c953358c865005855253af4f68a497John McCall const CallArgList &args) { 1678f85e193739c953358c865005855253af4f68a497John McCall for (CallArgList::writeback_iterator 1679f85e193739c953358c865005855253af4f68a497John McCall i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i) 1680f85e193739c953358c865005855253af4f68a497John McCall emitWriteback(CGF, *i); 1681f85e193739c953358c865005855253af4f68a497John McCall} 1682f85e193739c953358c865005855253af4f68a497John McCall 1683f85e193739c953358c865005855253af4f68a497John McCall/// Emit an argument that's being passed call-by-writeback. That is, 1684f85e193739c953358c865005855253af4f68a497John McCall/// we are passing the address of 1685f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args, 1686f85e193739c953358c865005855253af4f68a497John McCall const ObjCIndirectCopyRestoreExpr *CRE) { 1687f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr()); 1688f85e193739c953358c865005855253af4f68a497John McCall 1689f85e193739c953358c865005855253af4f68a497John McCall // The dest and src types don't necessarily match in LLVM terms 1690f85e193739c953358c865005855253af4f68a497John McCall // because of the crazy ObjC compatibility rules. 1691f85e193739c953358c865005855253af4f68a497John McCall 16922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::PointerType *destType = 1693f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(CGF.ConvertType(CRE->getType())); 1694f85e193739c953358c865005855253af4f68a497John McCall 1695f85e193739c953358c865005855253af4f68a497John McCall // If the address is a constant null, just pass the appropriate null. 1696f85e193739c953358c865005855253af4f68a497John McCall if (isProvablyNull(srcAddr)) { 1697f85e193739c953358c865005855253af4f68a497John McCall args.add(RValue::get(llvm::ConstantPointerNull::get(destType)), 1698f85e193739c953358c865005855253af4f68a497John McCall CRE->getType()); 1699f85e193739c953358c865005855253af4f68a497John McCall return; 1700f85e193739c953358c865005855253af4f68a497John McCall } 1701f85e193739c953358c865005855253af4f68a497John McCall 1702f85e193739c953358c865005855253af4f68a497John McCall QualType srcAddrType = 1703f85e193739c953358c865005855253af4f68a497John McCall CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType(); 1704f85e193739c953358c865005855253af4f68a497John McCall 1705f85e193739c953358c865005855253af4f68a497John McCall // Create the temporary. 1706f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(), 1707f85e193739c953358c865005855253af4f68a497John McCall "icr.temp"); 1708f85e193739c953358c865005855253af4f68a497John McCall 1709f85e193739c953358c865005855253af4f68a497John McCall // Zero-initialize it if we're not doing a copy-initialization. 1710f85e193739c953358c865005855253af4f68a497John McCall bool shouldCopy = CRE->shouldCopy(); 1711f85e193739c953358c865005855253af4f68a497John McCall if (!shouldCopy) { 1712f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *null = 1713f85e193739c953358c865005855253af4f68a497John McCall llvm::ConstantPointerNull::get( 1714f85e193739c953358c865005855253af4f68a497John McCall cast<llvm::PointerType>(destType->getElementType())); 1715f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.CreateStore(null, temp); 1716f85e193739c953358c865005855253af4f68a497John McCall } 1717f85e193739c953358c865005855253af4f68a497John McCall 1718f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *contBB = 0; 1719f85e193739c953358c865005855253af4f68a497John McCall 1720f85e193739c953358c865005855253af4f68a497John McCall // If the address is *not* known to be non-null, we need to switch. 1721f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *finalArgument; 1722f85e193739c953358c865005855253af4f68a497John McCall 1723f85e193739c953358c865005855253af4f68a497John McCall bool provablyNonNull = isProvablyNonNull(srcAddr); 1724f85e193739c953358c865005855253af4f68a497John McCall if (provablyNonNull) { 1725f85e193739c953358c865005855253af4f68a497John McCall finalArgument = temp; 1726f85e193739c953358c865005855253af4f68a497John McCall } else { 1727f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull"); 1728f85e193739c953358c865005855253af4f68a497John McCall 1729f85e193739c953358c865005855253af4f68a497John McCall finalArgument = CGF.Builder.CreateSelect(isNull, 1730f85e193739c953358c865005855253af4f68a497John McCall llvm::ConstantPointerNull::get(destType), 1731f85e193739c953358c865005855253af4f68a497John McCall temp, "icr.argument"); 1732f85e193739c953358c865005855253af4f68a497John McCall 1733f85e193739c953358c865005855253af4f68a497John McCall // If we need to copy, then the load has to be conditional, which 1734f85e193739c953358c865005855253af4f68a497John McCall // means we need control flow. 1735f85e193739c953358c865005855253af4f68a497John McCall if (shouldCopy) { 1736f85e193739c953358c865005855253af4f68a497John McCall contBB = CGF.createBasicBlock("icr.cont"); 1737f85e193739c953358c865005855253af4f68a497John McCall llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy"); 1738f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.CreateCondBr(isNull, contBB, copyBB); 1739f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitBlock(copyBB); 1740f85e193739c953358c865005855253af4f68a497John McCall } 1741f85e193739c953358c865005855253af4f68a497John McCall } 1742f85e193739c953358c865005855253af4f68a497John McCall 1743f85e193739c953358c865005855253af4f68a497John McCall // Perform a copy if necessary. 1744f85e193739c953358c865005855253af4f68a497John McCall if (shouldCopy) { 1745f85e193739c953358c865005855253af4f68a497John McCall LValue srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType); 1746545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall RValue srcRV = CGF.EmitLoadOfLValue(srcLV); 1747f85e193739c953358c865005855253af4f68a497John McCall assert(srcRV.isScalar()); 1748f85e193739c953358c865005855253af4f68a497John McCall 1749f85e193739c953358c865005855253af4f68a497John McCall llvm::Value *src = srcRV.getScalarVal(); 1750f85e193739c953358c865005855253af4f68a497John McCall src = CGF.Builder.CreateBitCast(src, destType->getElementType(), 1751f85e193739c953358c865005855253af4f68a497John McCall "icr.cast"); 1752f85e193739c953358c865005855253af4f68a497John McCall 1753f85e193739c953358c865005855253af4f68a497John McCall // Use an ordinary store, not a store-to-lvalue. 1754f85e193739c953358c865005855253af4f68a497John McCall CGF.Builder.CreateStore(src, temp); 1755f85e193739c953358c865005855253af4f68a497John McCall } 1756f85e193739c953358c865005855253af4f68a497John McCall 1757f85e193739c953358c865005855253af4f68a497John McCall // Finish the control flow if we needed it. 1758f85e193739c953358c865005855253af4f68a497John McCall if (shouldCopy && !provablyNonNull) 1759f85e193739c953358c865005855253af4f68a497John McCall CGF.EmitBlock(contBB); 1760f85e193739c953358c865005855253af4f68a497John McCall 1761f85e193739c953358c865005855253af4f68a497John McCall args.addWriteback(srcAddr, srcAddrType, temp); 1762f85e193739c953358c865005855253af4f68a497John McCall args.add(RValue::get(finalArgument), CRE->getType()); 1763f85e193739c953358c865005855253af4f68a497John McCall} 1764f85e193739c953358c865005855253af4f68a497John McCall 1765413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E, 1766413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall QualType type) { 1767f85e193739c953358c865005855253af4f68a497John McCall if (const ObjCIndirectCopyRestoreExpr *CRE 1768f85e193739c953358c865005855253af4f68a497John McCall = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) { 17694e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie assert(getContext().getLangOpts().ObjCAutoRefCount); 1770f85e193739c953358c865005855253af4f68a497John McCall assert(getContext().hasSameType(E->getType(), type)); 1771f85e193739c953358c865005855253af4f68a497John McCall return emitWritebackArg(*this, args, CRE); 1772f85e193739c953358c865005855253af4f68a497John McCall } 1773f85e193739c953358c865005855253af4f68a497John McCall 17748affed5107ea79bf4d429770d2873e9c7288255eJohn McCall assert(type->isReferenceType() == E->isGLValue() && 17758affed5107ea79bf4d429770d2873e9c7288255eJohn McCall "reference binding to unmaterialized r-value!"); 17768affed5107ea79bf4d429770d2873e9c7288255eJohn McCall 1777cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall if (E->isGLValue()) { 1778cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall assert(E->getObjectKind() == OK_Ordinary); 1779413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0), 1780413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall type); 1781cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall } 17821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 178370cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() && 178470cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman isa<ImplicitCastExpr>(E) && 178555d484802f3e27930317739efc5f5956b78aac25Eli Friedman cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) { 178655d484802f3e27930317739efc5f5956b78aac25Eli Friedman LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr()); 178755d484802f3e27930317739efc5f5956b78aac25Eli Friedman assert(L.isSimple()); 178851f512090530807e2c80f9411cc262025820c859Eli Friedman args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true); 178955d484802f3e27930317739efc5f5956b78aac25Eli Friedman return; 179055d484802f3e27930317739efc5f5956b78aac25Eli Friedman } 179155d484802f3e27930317739efc5f5956b78aac25Eli Friedman 1792413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall args.add(EmitAnyExprToTemp(E), type); 17930139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson} 17940139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson 1795b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 1796b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman// optimizer it can aggressively ignore unwind edges. 1797b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohmanvoid 1798b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan GohmanCodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) { 1799b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman if (CGM.getCodeGenOpts().OptimizationLevel != 0 && 1800b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions) 1801b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman Inst->setMetadata("clang.arc.no_objc_arc_exceptions", 1802b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman CGM.getNoObjCARCExceptionsMetadata()); 1803b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman} 1804b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman 1805f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Emits a call or invoke instruction to the given function, depending 1806f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// on the current state of the EH stack. 1807f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::CallSite 1808f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 18092d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner ArrayRef<llvm::Value *> Args, 18105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner const Twine &Name) { 1811f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::BasicBlock *InvokeDest = getInvokeDest(); 1812b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman 1813b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman llvm::Instruction *Inst; 1814f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!InvokeDest) 1815b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman Inst = Builder.CreateCall(Callee, Args, Name); 1816b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman else { 1817b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont"); 1818b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name); 1819b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman EmitBlock(ContBB); 1820b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman } 1821b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman 1822b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 1823b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman // optimizer it can aggressively ignore unwind edges. 18244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount) 1825b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman AddObjCARCExceptionMetadata(Inst); 1826f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 1827b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman return Inst; 1828f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall} 1829f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 18304c7d9f1507d0f102bd4133bba63348636facd469Jay Foadllvm::CallSite 18314c7d9f1507d0f102bd4133bba63348636facd469Jay FoadCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee, 18325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner const Twine &Name) { 18332d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name); 18344c7d9f1507d0f102bd4133bba63348636facd469Jay Foad} 18354c7d9f1507d0f102bd4133bba63348636facd469Jay Foad 1836708554498595e047cc53e366c91cc063fcc1c5bcChris Lattnerstatic void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo, 1837708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner llvm::FunctionType *FTy) { 1838708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner if (ArgNo < FTy->getNumParams()) 1839708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner assert(Elt->getType() == FTy->getParamType(ArgNo)); 1840708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner else 1841708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner assert(FTy->isVarArg()); 1842708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner ++ArgNo; 1843708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner} 1844708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 1845811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattnervoid CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV, 18465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value*,16> &Args, 1847811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner llvm::FunctionType *IRFuncTy) { 1848194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { 1849194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson unsigned NumElts = AT->getSize().getZExtValue(); 1850194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson QualType EltTy = AT->getElementType(); 1851194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson llvm::Value *Addr = RV.getAggregateAddr(); 1852194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson for (unsigned Elt = 0; Elt < NumElts; ++Elt) { 1853194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt); 1854194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson LValue LV = MakeAddrLValue(EltAddr, EltTy); 1855194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson RValue EltRV; 1856ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman if (EltTy->isAnyComplexType()) 1857ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman // FIXME: Volatile? 1858ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman EltRV = RValue::getComplex(LoadComplexFromAddr(LV.getAddress(), false)); 1859ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman else if (CodeGenFunction::hasAggregateLLVMType(EltTy)) 186051f512090530807e2c80f9411cc262025820c859Eli Friedman EltRV = LV.asAggregateRValue(); 1861194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson else 1862194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson EltRV = EmitLoadOfLValue(LV); 1863194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy); 1864811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner } 1865eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } else if (const RecordType *RT = Ty->getAs<RecordType>()) { 1866194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson RecordDecl *RD = RT->getDecl(); 1867194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson assert(RV.isAggregate() && "Unexpected rvalue during struct expansion"); 1868377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty); 1869eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov 1870eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (RD->isUnion()) { 1871eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov const FieldDecl *LargestFD = 0; 1872eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov CharUnits UnionSize = CharUnits::Zero(); 1873eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov 1874eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 1875eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov i != e; ++i) { 1876581deb3da481053c4993c7600f97acf7768caac5David Blaikie const FieldDecl *FD = *i; 1877eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov assert(!FD->isBitField() && 1878eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov "Cannot expand structure with bit-field members."); 1879eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType()); 1880eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (UnionSize < FieldSize) { 1881eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov UnionSize = FieldSize; 1882eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov LargestFD = FD; 1883eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 1884eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 1885eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov if (LargestFD) { 1886377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman RValue FldRV = EmitRValueForField(LV, LargestFD); 1887eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov ExpandTypeToArgs(LargestFD->getType(), FldRV, Args, IRFuncTy); 1888eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 1889eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } else { 1890eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); 1891eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov i != e; ++i) { 1892581deb3da481053c4993c7600f97acf7768caac5David Blaikie FieldDecl *FD = *i; 1893eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov 1894377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman RValue FldRV = EmitRValueForField(LV, FD); 1895eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov ExpandTypeToArgs(FD->getType(), FldRV, Args, IRFuncTy); 1896eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov } 1897194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } 1898ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman } else if (Ty->isAnyComplexType()) { 1899194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson ComplexPairTy CV = RV.getComplexVal(); 1900194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson Args.push_back(CV.first); 1901194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson Args.push_back(CV.second); 1902194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson } else { 1903811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner assert(RV.isScalar() && 1904811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner "Unexpected non-scalar rvalue during struct expansion."); 1905811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner 1906811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner // Insert a bitcast as needed. 1907811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner llvm::Value *V = RV.getScalarVal(); 1908811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner if (Args.size() < IRFuncTy->getNumParams() && 1909811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner V->getType() != IRFuncTy->getParamType(Args.size())) 1910811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size())); 1911811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner 1912811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner Args.push_back(V); 1913811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner } 1914811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner} 1915811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner 1916811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner 191788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel DunbarRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, 19181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump llvm::Value *Callee, 1919f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson ReturnValueSlot ReturnValue, 1920c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar const CallArgList &CallArgs, 1921dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall const Decl *TargetDecl, 19224b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall llvm::Instruction **callOrInvoke) { 1923f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: We no longer need the types from CallArgs; lift up and simplify. 19245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner SmallVector<llvm::Value*, 16> Args; 192517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar 192617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // Handle struct-return functions by passing a pointer to the 192717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar // location that we would like to return into. 1928bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar QualType RetTy = CallInfo.getReturnType(); 1929b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &RetAI = CallInfo.getReturnInfo(); 19301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1931708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner // IRArgNo - Keep track of the argument number in the callee we're looking at. 1932708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner unsigned IRArgNo = 0; 1933708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner llvm::FunctionType *IRFuncTy = 1934708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner cast<llvm::FunctionType>( 1935708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner cast<llvm::PointerType>(Callee->getType())->getElementType()); 19361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19375db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // If the call returns a temporary with struct return, create a temporary 1938d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson // alloca to hold the result, unless one is given to us. 1939dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar if (CGM.ReturnTypeUsesSRet(CallInfo)) { 1940d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson llvm::Value *Value = ReturnValue.getValue(); 1941d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson if (!Value) 1942195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar Value = CreateMemTemp(RetTy); 1943d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson Args.push_back(Value); 1944708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner checkArgMatches(Value, IRArgNo, IRFuncTy); 1945d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson } 19461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19474b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar assert(CallInfo.arg_size() == CallArgs.size() && 19484b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar "Mismatch between function signature & arguments."); 1949b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin(); 19501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end(); 1951b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar I != E; ++I, ++info_it) { 1952b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar const ABIArgInfo &ArgInfo = info_it->info; 1953c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman RValue RV = I->RV; 19545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar 195597cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman unsigned TypeAlign = 1956c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman getContext().getTypeAlignInChars(I->Ty).getQuantity(); 19575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar switch (ArgInfo.getKind()) { 195891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar case ABIArgInfo::Indirect: { 19591f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar if (RV.isScalar() || RV.isComplex()) { 19601f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar // Make a temporary alloca to pass the argument. 196170cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 196270cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman if (ArgInfo.getIndirectAlign() > AI->getAlignment()) 196370cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman AI->setAlignment(ArgInfo.getIndirectAlign()); 196470cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman Args.push_back(AI); 1965708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 19661f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar if (RV.isScalar()) 196791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false, 196897cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman TypeAlign, I->Ty); 19691f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar else 19701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump StoreComplexToAddr(RV.getComplexVal(), Args.back(), false); 1971708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 1972708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner // Validate argument match. 1973708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner checkArgMatches(AI, IRArgNo, IRFuncTy); 19741f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } else { 1975ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // We want to avoid creating an unnecessary temporary+copy here; 1976ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // however, we need one in two cases: 1977ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // 1. If the argument is not byval, and we are required to copy the 1978ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // source. (This case doesn't occur on any common architecture.) 1979ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // 2. If the argument is byval, RV is not sufficiently aligned, and 1980ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // we cannot force it to be sufficiently aligned. 198197cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman llvm::Value *Addr = RV.getAggregateAddr(); 198297cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman unsigned Align = ArgInfo.getIndirectAlign(); 198397cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman const llvm::TargetData *TD = &CGM.getTargetData(); 198497cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) || 198597cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman (ArgInfo.getIndirectByVal() && TypeAlign < Align && 198697cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) { 1987ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // Create an aligned temporary, and copy to it. 198897cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman llvm::AllocaInst *AI = CreateMemTemp(I->Ty); 198997cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman if (Align > AI->getAlignment()) 199097cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman AI->setAlignment(Align); 1991ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman Args.push_back(AI); 1992649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified()); 1993708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 1994708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner // Validate argument match. 1995708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner checkArgMatches(AI, IRArgNo, IRFuncTy); 1996ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman } else { 1997ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman // Skip the extra memcpy call. 199897cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman Args.push_back(Addr); 1999708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 2000708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner // Validate argument match. 2001708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner checkArgMatches(Addr, IRArgNo, IRFuncTy); 2002ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman } 20031f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar } 20041f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar break; 200591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar } 20061f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar 200711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 200811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar break; 20099cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 2010800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Extend: 2011800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Direct: { 2012f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka // Insert a padding argument to ensure proper alignment. 2013f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) { 2014f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka Args.push_back(llvm::UndefValue::get(PaddingType)); 2015f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka ++IRArgNo; 2016f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka } 2017f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka 2018800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) && 2019117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgInfo.getCoerceToType() == ConvertType(info_it->type) && 2020117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner ArgInfo.getDirectOffset() == 0) { 2021708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner llvm::Value *V; 2022800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (RV.isScalar()) 2023708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner V = RV.getScalarVal(); 2024800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner else 2025708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner V = Builder.CreateLoad(RV.getAggregateAddr()); 2026708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 202721ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner // If the argument doesn't match, perform a bitcast to coerce it. This 202821ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner // can happen due to trivial type mismatches. 202921ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner if (IRArgNo < IRFuncTy->getNumParams() && 203021ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner V->getType() != IRFuncTy->getParamType(IRArgNo)) 203121ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo)); 2032708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner Args.push_back(V); 2033708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 2034708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner checkArgMatches(V, IRArgNo, IRFuncTy); 2035800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner break; 2036800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 203711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 203889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar // FIXME: Avoid the conversion through memory if possible. 203989c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar llvm::Value *SrcPtr; 204089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar if (RV.isScalar()) { 2041c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman SrcPtr = CreateMemTemp(I->Ty, "coerce"); 204297cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty); 204389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar } else if (RV.isComplex()) { 2044c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman SrcPtr = CreateMemTemp(I->Ty, "coerce"); 204589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false); 20461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } else 204789c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar SrcPtr = RV.getAggregateAddr(); 20489cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 2049117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 2050117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = ArgInfo.getDirectOffset()) { 2051117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy()); 2052117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs); 20539cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer SrcPtr = Builder.CreateBitCast(SrcPtr, 2054117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(ArgInfo.getCoerceToType())); 2055117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner 2056117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 20579cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 2058ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // If the coerce-to type is a first class aggregate, we flatten it and 2059ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // pass the elements. Either way is semantically identical, but fast-isel 2060ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner // and the optimizer generally likes scalar values better than FCAs. 20612acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (llvm::StructType *STy = 2062309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) { 20639282688a296b306c4ae2d115f55101647056d1daChris Lattner SrcPtr = Builder.CreateBitCast(SrcPtr, 20649282688a296b306c4ae2d115f55101647056d1daChris Lattner llvm::PointerType::getUnqual(STy)); 20659282688a296b306c4ae2d115f55101647056d1daChris Lattner for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { 20669282688a296b306c4ae2d115f55101647056d1daChris Lattner llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i); 2067deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner llvm::LoadInst *LI = Builder.CreateLoad(EltPtr); 2068deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner // We don't know what we're loading from. 2069deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner LI->setAlignment(1); 2070deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner Args.push_back(LI); 2071708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 2072708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner // Validate argument match. 2073708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner checkArgMatches(LI, IRArgNo, IRFuncTy); 2074309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner } 2075ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } else { 2076309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner // In the simple case, just pass the coerced loaded value. 2077309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(), 2078309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner *this)); 2079708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner 2080708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner // Validate argument match. 2081708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner checkArgMatches(Args.back(), IRArgNo, IRFuncTy); 2082ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner } 20839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 208489c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar break; 208589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar } 208689c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar 20875627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar case ABIArgInfo::Expand: 2088811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy); 2089708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner IRArgNo = Args.size(); 20905627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar break; 209117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 209217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 20931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20945db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // If the callee is a bitcast of a function to a varargs pointer to function 20955db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // type, check to see if we can remove the bitcast. This handles some cases 20965db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // with unprototyped functions. 20975db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee)) 20985db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) { 20992acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType()); 21002acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *CurFT = 21015db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner cast<llvm::FunctionType>(CurPT->getElementType()); 21022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::FunctionType *ActualFT = CalleeF->getFunctionType(); 21031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21045db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (CE->getOpcode() == llvm::Instruction::BitCast && 21055db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner ActualFT->getReturnType() == CurFT->getReturnType() && 2106d6bebbfd510f3b495795b88aafd10ead3cb211e9Chris Lattner ActualFT->getNumParams() == CurFT->getNumParams() && 2107c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian ActualFT->getNumParams() == Args.size() && 2108c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian (CurFT->isVarArg() || !ActualFT->isVarArg())) { 21095db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner bool ArgsMatch = true; 21105db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i) 21115db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (ActualFT->getParamType(i) != CurFT->getParamType(i)) { 21125db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner ArgsMatch = false; 21135db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner break; 21145db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner } 21151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21165db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // Strip the cast if we can get away with it. This is a nice cleanup, 21175db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // but also allows us to inline the function at -O0 if it is marked 21185db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner // always_inline. 21195db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner if (ArgsMatch) 21205db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner Callee = CalleeF; 21215db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner } 21225db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner } 21231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2124ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar unsigned CallingConv; 2125761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel CodeGen::AttributeListType AttributeList; 2126ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv); 2127a61ab0521576e9e9e90cd2d482f8684e6070a93cChris Lattner llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList); 21281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2129f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall llvm::BasicBlock *InvokeDest = 0; 2130f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) 2131f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall InvokeDest = getInvokeDest(); 2132f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall 2133d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar llvm::CallSite CS; 2134f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall if (!InvokeDest) { 21354c7d9f1507d0f102bd4133bba63348636facd469Jay Foad CS = Builder.CreateCall(Callee, Args); 21369834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar } else { 21379834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar llvm::BasicBlock *Cont = createBasicBlock("invoke.cont"); 21384c7d9f1507d0f102bd4133bba63348636facd469Jay Foad CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args); 21399834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar EmitBlock(Cont); 2140f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar } 2141ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner if (callOrInvoke) 21424b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall *callOrInvoke = CS.getInstruction(); 2143f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar 2144d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar CS.setAttributes(Attrs); 2145ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); 2146d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar 2147b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC 2148b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman // optimizer it can aggressively ignore unwind edges. 21494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (CGM.getLangOpts().ObjCAutoRefCount) 2150b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman AddObjCARCExceptionMetadata(CS.getInstruction()); 2151b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman 2152d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // If the call doesn't return, finish the basic block and clear the 2153d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // insertion point; this allows the rest of IRgen to discard 2154d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // unreachable code. 2155d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar if (CS.doesNotReturn()) { 2156d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar Builder.CreateUnreachable(); 2157d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar Builder.ClearInsertionPoint(); 21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2159f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // FIXME: For now, emit a dummy basic block because expr emitters in 2160f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // generally are not ready to handle emitting expressions at unreachable 2161f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump // points. 2162d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar EnsureInsertPoint(); 21631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2164d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar // Return a reasonable RValue. 2165d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar return GetUndefRValue(RetTy); 21661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump } 2167d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar 2168d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar llvm::Instruction *CI = CS.getInstruction(); 2169ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer if (Builder.isNamePreserving() && !CI->getType()->isVoidTy()) 217017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar CI->setName("call"); 21712c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 2172f85e193739c953358c865005855253af4f68a497John McCall // Emit any writebacks immediately. Arguably this should happen 2173f85e193739c953358c865005855253af4f68a497John McCall // after any return-value munging. 2174f85e193739c953358c865005855253af4f68a497John McCall if (CallArgs.hasWritebacks()) 2175f85e193739c953358c865005855253af4f68a497John McCall emitWritebacks(*this, CallArgs); 2176f85e193739c953358c865005855253af4f68a497John McCall 21772c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar switch (RetAI.getKind()) { 217891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar case ABIArgInfo::Indirect: { 217991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 21802c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar if (RetTy->isAnyComplexType()) 21815627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar return RValue::getComplex(LoadComplexFromAddr(Args[0], false)); 21823403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 21835627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar return RValue::getAggregate(Args[0]); 218491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy)); 218591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar } 21868951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 218711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar case ABIArgInfo::Ignore: 21880bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar // If we are ignoring an argument that had a result, make sure to 21890bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar // construct the appropriate return value for our caller. 219013e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar return GetUndefRValue(RetTy); 21919cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 2192800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Extend: 2193800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner case ABIArgInfo::Direct: { 21946af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner llvm::Type *RetIRTy = ConvertType(RetTy); 21956af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) { 2196800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (RetTy->isAnyComplexType()) { 2197800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *Real = Builder.CreateExtractValue(CI, 0); 2198800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *Imag = Builder.CreateExtractValue(CI, 1); 2199800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner return RValue::getComplex(std::make_pair(Real, Imag)); 2200800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 2201800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (CodeGenFunction::hasAggregateLLVMType(RetTy)) { 2202800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner llvm::Value *DestPtr = ReturnValue.getValue(); 2203800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner bool DestIsVolatile = ReturnValue.isVolatile(); 220411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar 2205800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner if (!DestPtr) { 2206800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner DestPtr = CreateMemTemp(RetTy, "agg.tmp"); 2207800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner DestIsVolatile = false; 2208800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 2209badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false); 2210800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner return RValue::getAggregate(DestPtr); 2211800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 22126af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner 22136af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner // If the argument doesn't match, perform a bitcast to coerce it. This 22146af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner // can happen due to trivial type mismatches. 22156af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner llvm::Value *V = CI; 22166af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner if (V->getType() != RetIRTy) 22176af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner V = Builder.CreateBitCast(V, RetIRTy); 22186af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner return RValue::get(V); 2219800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner } 22209cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 2221d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson llvm::Value *DestPtr = ReturnValue.getValue(); 2222d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson bool DestIsVolatile = ReturnValue.isVolatile(); 22239cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 2224d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson if (!DestPtr) { 2225195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar DestPtr = CreateMemTemp(RetTy, "coerce"); 2226d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson DestIsVolatile = false; 2227d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson } 22289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 2229117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner // If the value is offset in memory, apply the offset now. 2230117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::Value *StorePtr = DestPtr; 2231117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner if (unsigned Offs = RetAI.getDirectOffset()) { 2232117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy()); 2233117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs); 22349cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer StorePtr = Builder.CreateBitCast(StorePtr, 2235117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner llvm::PointerType::getUnqual(RetAI.getCoerceToType())); 2236117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner } 2237117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this); 22389cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer 223991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity(); 2240ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson if (RetTy->isAnyComplexType()) 2241d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson return RValue::getComplex(LoadComplexFromAddr(DestPtr, false)); 22423403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner if (CodeGenFunction::hasAggregateLLVMType(RetTy)) 2243d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson return RValue::getAggregate(DestPtr); 224491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy)); 2245639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar } 22468951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar 22478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar case ABIArgInfo::Expand: 2248b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Invalid ABI kind for return argument"); 224917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar } 22502c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar 2251b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie llvm_unreachable("Unhandled ABIArgInfo::Kind"); 225217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar} 2253b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar 2254b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar/* VarArg handling */ 2255b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar 2256b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbarllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) { 2257b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this); 2258b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar} 2259