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
70de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a value of the
71de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// given unprototyped function type.
720b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
73de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
74de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // When translating an unprototyped function type, always use a
75de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // variadic type.
76de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(FTNP->getResultType().getUnqualifiedType(),
77de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             ArrayRef<CanQualType>(),
78de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             FTNP->getExtInfo(),
79de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             RequiredArgs(0));
80de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall}
81de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
82de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a value of the
83de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// given function type, on top of any implicit parameters already
84de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// stored.
85de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallstatic const CGFunctionInfo &arrangeFunctionType(CodeGenTypes &CGT,
86de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  SmallVectorImpl<CanQualType> &argTypes,
879cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                             CanQual<FunctionProtoType> FTP) {
88de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, argTypes.size());
89541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
9045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
91de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTypes.push_back(FTP->getArgType(i));
92de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQualType resultType = FTP->getResultType().getUnqualifiedType();
93de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return CGT.arrangeFunctionType(resultType, argTypes,
94de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                 FTP->getExtInfo(), required);
950b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
960b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
97de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a value of the
98de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// given function type.
990b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
100de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionType(CanQual<FunctionProtoType> FTP) {
101de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
102de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return ::arrangeFunctionType(*this, argTypes, FTP);
103bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar}
104bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
10504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic CallingConv getCallingConventionForDecl(const Decl *D) {
106bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  // Set the appropriate calling convention for the Function.
107bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<StdCallAttr>())
10804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86StdCall;
109bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
110bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<FastCallAttr>())
11104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86FastCall;
112bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
113f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  if (D->hasAttr<ThisCallAttr>())
114f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor    return CC_X86ThisCall;
115f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor
11652fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  if (D->hasAttr<PascalAttr>())
11752fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik    return CC_X86Pascal;
11852fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik
119414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  if (PcsAttr *PCS = D->getAttr<PcsAttr>())
120414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
121414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov
12204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  return CC_C;
12345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
12445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
125de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a call to an
126de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// unknown C++ non-static member function of the given abstract type.
127de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// The member function must be an ordinary function, i.e. not a
128de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// constructor or destructor.
129de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
130de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
131de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                   const FunctionProtoType *FTP) {
132de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
1330b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
134375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson  // Add the 'this' pointer.
135de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTypes.push_back(GetThisType(Context, RD));
1360b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
137de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return ::arrangeFunctionType(*this, argTypes,
1389c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller              FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
139375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson}
140375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson
141de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration or
142de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of the given C++ non-static member function.  The
143de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// member function must be an ordinary function, i.e. not a
144de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// constructor or destructor.
145de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
146de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
147fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
148fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
149fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall
150de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQual<FunctionProtoType> prototype = GetFormalType(MD);
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  if (MD->isInstance()) {
153de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    // The abstract case is perfectly fine.
154de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return arrangeCXXMethodType(MD->getParent(), prototype.getTypePtr());
155de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
156de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
157de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(prototype);
158f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson}
159f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson
160de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration
161de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// or definition to the given constructor variant.
162de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
163de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXConstructorDeclaration(const CXXConstructorDecl *D,
164de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                               CXXCtorType ctorKind) {
165de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
166de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTypes.push_back(GetThisType(Context, D->getParent()));
167de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQualType resultType = Context.VoidTy;
168f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
169de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  TheCXXABI.BuildConstructorSignature(D, ctorKind, resultType, argTypes);
1700b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
1724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
173de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, argTypes.size());
174de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
1754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Add the formal parameters.
1764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
177de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTypes.push_back(FTP->getArgType(i));
1784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
179de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(resultType, argTypes, FTP->getExtInfo(), required);
180f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
181f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
182de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration,
183de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition, or call to the given destructor variant.  It so
184de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// happens that all three cases produce the same information.
185de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
186de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXDestructor(const CXXDestructorDecl *D,
187de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                   CXXDtorType dtorKind) {
188de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 2> argTypes;
189de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTypes.push_back(GetThisType(Context, D->getParent()));
190de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQualType resultType = Context.VoidTy;
1914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
192de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  TheCXXABI.BuildDestructorSignature(D, dtorKind, resultType, argTypes);
1930b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
1954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
1964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
197de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(resultType, argTypes, FTP->getExtInfo(),
198de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             RequiredArgs::All);
199f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
200f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
201de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the declaration or
202de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of the given function.
203de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
204de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
2053eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
206f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson    if (MD->isInstance())
207de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      return arrangeCXXMethodDeclaration(MD);
2081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
209ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
210de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
211ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionType>(FTy));
212de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
213de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // When declaring a function without a prototype, always use a
214de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // non-variadic type.
215de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  if (isa<FunctionNoProtoType>(FTy)) {
216de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>();
217de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return arrangeFunctionType(noProto->getResultType(),
218de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                               ArrayRef<CanQualType>(),
219de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                               noProto->getExtInfo(),
220de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                               RequiredArgs::All);
221de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
222de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
223ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionProtoType>(FTy));
224de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(FTy.getAs<FunctionProtoType>());
2250dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
2260dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
227de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the declaration or
228de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of an Objective-C method.
229de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
230de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
231de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // It happens that this is the same as a call with no optional
232de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // arguments, except also using the formal 'self' type.
233de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
234de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall}
235de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
236de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the function type
237de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// through which to perform a send to the given Objective-C method,
238de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// using the given receiver type.  The receiver type is not always
239de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// the 'self' type of the method or even an Objective-C pointer type.
240de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// This is *not* the right method for actually performing such a
241de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// message send, due to the possibility of optional arguments.
242de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
243de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
244de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                              QualType receiverType) {
245de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTys;
246de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTys.push_back(Context.getCanonicalParamType(receiverType));
247de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
248541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy?
249491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
2500b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall         e = MD->param_end(); i != e; ++i) {
251de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTys.push_back(Context.getCanonicalParamType((*i)->getType()));
2520b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  }
253f85e193739c953358c865005855253af4f68a497John McCall
254f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo;
255f85e193739c953358c865005855253af4f68a497John McCall  einfo = einfo.withCallingConv(getCallingConventionForDecl(MD));
256f85e193739c953358c865005855253af4f68a497John McCall
2574e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getContext().getLangOpts().ObjCAutoRefCount &&
258f85e193739c953358c865005855253af4f68a497John McCall      MD->hasAttr<NSReturnsRetainedAttr>())
259f85e193739c953358c865005855253af4f68a497John McCall    einfo = einfo.withProducesResult(true);
260f85e193739c953358c865005855253af4f68a497John McCall
261de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required =
262de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
263de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
264de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(GetReturnType(MD->getResultType()), argTys,
265de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             einfo, required);
2660dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
2670dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
268de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
269de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
270b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  // FIXME: Do we need to handle ObjCMethodDecl?
271b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
273b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
274de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return arrangeCXXConstructorDeclaration(CD, GD.getCtorType());
275b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
276b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
277de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return arrangeCXXDestructor(DD, GD.getDtorType());
278de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
279de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionDeclaration(FD);
280de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall}
2819cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
282de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Figure out the rules for calling a function with the given formal
283de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// type using the given arguments.  The arguments are necessary
284de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// because the function might be unprototyped, in which case it's
285de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// target-dependent in crazy ways.
286de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
287de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionCall(const CallArgList &args,
288de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  const FunctionType *fnType) {
289de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required = RequiredArgs::All;
290de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
291de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (proto->isVariadic())
292de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      required = RequiredArgs(proto->getNumArgs());
293de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  } else if (CGM.getTargetCodeGenInfo()
294de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall               .isNoProtoCallVariadic(args, cast<FunctionNoProtoType>(fnType))) {
295de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    required = RequiredArgs(0);
296de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
297de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
298de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionCall(fnType->getResultType(), args,
299de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             fnType->getExtInfo(), required);
300b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson}
301b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
302de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
303de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionCall(QualType resultType,
304de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  const CallArgList &args,
305de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  const FunctionType::ExtInfo &info,
306de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  RequiredArgs required) {
307541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
308de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
309de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (CallArgList::const_iterator i = args.begin(), e = args.end();
310725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar       i != e; ++i)
311de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTypes.push_back(Context.getCanonicalParamType(i->Ty));
312de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(GetReturnType(resultType), argTypes, info,
313de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             required);
3140dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
3150dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
316de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
317de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionDeclaration(QualType resultType,
318de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                         const FunctionArgList &args,
319de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                         const FunctionType::ExtInfo &info,
320de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                         bool isVariadic) {
321541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
322de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
323de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (FunctionArgList::const_iterator i = args.begin(), e = args.end();
324bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar       i != e; ++i)
325de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTypes.push_back(Context.getCanonicalParamType((*i)->getType()));
326de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
327de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required =
328de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All);
329de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(GetReturnType(resultType), argTypes, info,
330de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             required);
331541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar}
332541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
333de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
334de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionType(getContext().VoidTy, ArrayRef<CanQualType>(),
335de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             FunctionType::ExtInfo(), RequiredArgs::All);
336d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
337d26bc76c98006609002d9930f8840490e88ac5b5John McCall
338de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for an abstract value
339de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// of a given function type.  This is the method which all of the
340de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// above functions ultimately defer to.
341de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
342de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionType(CanQualType resultType,
343de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  ArrayRef<CanQualType> argTypes,
344de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  const FunctionType::ExtInfo &info,
345de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                  RequiredArgs required) {
346ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#ifndef NDEBUG
347de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (ArrayRef<CanQualType>::const_iterator
348de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall         I = argTypes.begin(), E = argTypes.end(); I != E; ++I)
349ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall    assert(I->isCanonicalAsParam());
350ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#endif
351ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall
352de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
35304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
35440a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  // Lookup or create unique function info.
35540a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  llvm::FoldingSetNodeID ID;
356de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CGFunctionInfo::Profile(ID, info, required, resultType, argTypes);
35740a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
358de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  void *insertPos = 0;
359de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
36040a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  if (FI)
36140a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    return *FI;
36240a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
363de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // Construct the function info.  We co-allocate the ArgInfos.
364de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI = CGFunctionInfo::create(CC, info, resultType, argTypes, required);
365de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FunctionInfos.InsertNode(FI, insertPos);
366541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
367de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted;
368de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  assert(inserted && "Recursively being processed?");
36971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
37088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // Compute ABI information.
371ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  getABIInfo().computeInfo(*FI);
3729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
373800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // Loop over all of the computed argument and return value info.  If any of
374800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // them are direct or extend without a specified coerce type, specify the
375800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // default now.
376de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  ABIArgInfo &retInfo = FI->getReturnInfo();
377de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == 0)
378de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
3799cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
380800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
381800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner       I != E; ++I)
382800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
3839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      I->info.setCoerceToType(ConvertType(I->type));
3849cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
385de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
386de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  assert(erased && "Not in set?");
387d26c07142710790b820a66245939668f62eaf2d9Chris Lattner
38888c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  return *FI;
3890dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
39017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
391de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
392de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       const FunctionType::ExtInfo &info,
393de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       CanQualType resultType,
394de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       ArrayRef<CanQualType> argTypes,
395de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       RequiredArgs required) {
396de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  void *buffer = operator new(sizeof(CGFunctionInfo) +
397de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                              sizeof(ArgInfo) * (argTypes.size() + 1));
398de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
399de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->CallingConvention = llvmCC;
400de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->EffectiveCallingConvention = llvmCC;
401de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->ASTCallingConvention = info.getCC();
402de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->NoReturn = info.getNoReturn();
403de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->ReturnsRetained = info.getProducesResult();
404de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->Required = required;
405de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->HasRegParm = info.getHasRegParm();
406de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->RegParm = info.getRegParm();
407de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->NumArgs = argTypes.size();
408de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->getArgsBuffer()[0].type = resultType;
409de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
410de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    FI->getArgsBuffer()[i + 1].type = argTypes[i];
411de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return FI;
41288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar}
41388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
41488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar/***/
41588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
41642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCallvoid CodeGenTypes::GetExpandedTypes(QualType type,
4175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                     SmallVectorImpl<llvm::Type*> &expandedTypes) {
418194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) {
419194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    uint64_t NumElts = AT->getSize().getZExtValue();
420194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (uint64_t Elt = 0; Elt < NumElts; ++Elt)
421194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      GetExpandedTypes(AT->getElementType(), expandedTypes);
422eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  } else if (const RecordType *RT = type->getAs<RecordType>()) {
423194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const RecordDecl *RD = RT->getDecl();
424194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    assert(!RD->hasFlexibleArrayMember() &&
425194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson           "Cannot expand structure with flexible array.");
426eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->isUnion()) {
427eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // Unions can be here only in degenerative cases - all the fields are same
428eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // after flattening. Thus we have to use the "largest" field.
429eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      const FieldDecl *LargestFD = 0;
430eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      CharUnits UnionSize = CharUnits::Zero();
431eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
432eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
433eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
434eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        const FieldDecl *FD = *i;
435eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        assert(!FD->isBitField() &&
436eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
437eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
438eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        if (UnionSize < FieldSize) {
439eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          UnionSize = FieldSize;
440eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          LargestFD = FD;
441eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        }
442eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
443eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      if (LargestFD)
444eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        GetExpandedTypes(LargestFD->getType(), expandedTypes);
445eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    } else {
446eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
447eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
448eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        const FieldDecl *FD = *i;
449eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        assert(!FD->isBitField() &&
450eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
451eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        GetExpandedTypes(FD->getType(), expandedTypes);
452eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
453194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
454194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const ComplexType *CT = type->getAs<ComplexType>()) {
455194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    llvm::Type *EltTy = ConvertType(CT->getElementType());
456194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    expandedTypes.push_back(EltTy);
457194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    expandedTypes.push_back(EltTy);
458194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else
459194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    expandedTypes.push_back(ConvertType(type));
4605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
4615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function::arg_iterator
4635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
4645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    llvm::Function::arg_iterator AI) {
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(LV.isSimple() &&
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         "Unexpected non-simple lvalue during struct expansion.");
467194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
468194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
469194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    unsigned NumElts = AT->getSize().getZExtValue();
470194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    QualType EltTy = AT->getElementType();
471194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
472377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman      llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, Elt);
473194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      LValue LV = MakeAddrLValue(EltAddr, EltTy);
474194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      AI = ExpandTypeFromArgs(EltTy, LV, AI);
475194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
476eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
477194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    RecordDecl *RD = RT->getDecl();
478eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->isUnion()) {
479eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // Unions can be here only in degenerative cases - all the fields are same
480eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // after flattening. Thus we have to use the "largest" field.
481eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      const FieldDecl *LargestFD = 0;
482eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      CharUnits UnionSize = CharUnits::Zero();
483eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
484eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
485eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
486eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        const FieldDecl *FD = *i;
487eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        assert(!FD->isBitField() &&
488eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
489eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
490eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        if (UnionSize < FieldSize) {
491eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          UnionSize = FieldSize;
492eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          LargestFD = FD;
493eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        }
494eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
495eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      if (LargestFD) {
496eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        // FIXME: What are the right qualifiers here?
497377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue SubLV = EmitLValueForField(LV, LargestFD);
498377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        AI = ExpandTypeFromArgs(LargestFD->getType(), SubLV, AI);
499eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
500eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    } else {
501eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
502eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
503eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        FieldDecl *FD = *i;
504eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        QualType FT = FD->getType();
505eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
506eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        // FIXME: What are the right qualifiers here?
507377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue SubLV = EmitLValueForField(LV, FD);
508377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        AI = ExpandTypeFromArgs(FT, SubLV, AI);
509eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
5105627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
511194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
512194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    QualType EltTy = CT->getElementType();
513377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real");
514194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy));
515377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag");
516194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy));
517194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
518194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    EmitStoreThroughLValue(RValue::get(AI), LV);
519194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    ++AI;
5205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
5215627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
5225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  return AI;
5235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
5245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
525e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
52608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// accessing some number of bytes out of it, try to gep into the struct to get
52708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// at its inner goodness.  Dive as deep as possible without entering an element
52808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// with an in-memory size smaller than DstSize.
52908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattnerstatic llvm::Value *
530e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris LattnerEnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
5312acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                   llvm::StructType *SrcSTy,
532e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner                                   uint64_t DstSize, CodeGenFunction &CGF) {
53308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // We can't dive into a zero-element struct.
53408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (SrcSTy->getNumElements() == 0) return SrcPtr;
5359cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *FirstElt = SrcSTy->getElementType(0);
5379cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
53808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first elt is at least as large as what we're looking for, or if the
53908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // first element is the same size as the whole struct, we can enter it.
5409cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  uint64_t FirstEltSize =
54108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
5429cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  if (FirstEltSize < DstSize &&
54308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner      FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
54408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    return SrcPtr;
5459cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
54608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // GEP into the first element.
54708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
5489cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
54908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first element is a struct, recurse.
5502acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SrcTy =
55108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
5522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
553e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
55408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
55508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  return SrcPtr;
55608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner}
55708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
5586d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
5596d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// are either integers or pointers.  This does a truncation of the value if it
5606d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// is too large or a zero extension if it is too small.
5616d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattnerstatic llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
5622acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                             llvm::Type *Ty,
5636d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner                                             CodeGenFunction &CGF) {
5646d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() == Ty)
5656d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return Val;
5669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5676d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Val->getType())) {
5686d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // If this is Pointer->Pointer avoid conversion to and from int.
5696d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    if (isa<llvm::PointerType>(Ty))
5706d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
5719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5726d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // Convert the pointer to an integer so we can play with its width.
57377b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
5746d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
5759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5762acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *DestIntTy = Ty;
5776d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(DestIntTy))
57877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    DestIntTy = CGF.IntPtrTy;
5799cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5806d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() != DestIntTy)
5816d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
5829cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5836d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Ty))
5846d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
5856d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  return Val;
5866d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner}
5876d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner
58808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
58908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
590275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
591275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// a pointer to an object of type \arg Ty.
592275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
593275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is smaller than the
594275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; in this situation the values of bits which not
595275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// present in the src are undefined.
596275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
5972acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                      llvm::Type *Ty,
598275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      CodeGenFunction &CGF) {
5992acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SrcTy =
600275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
6019cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6026ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  // If SrcTy and Ty are the same, just do a load.
6036ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == Ty)
6046ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return CGF.Builder.CreateLoad(SrcPtr);
6059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6069408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
6079cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6082acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
609e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
61008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
61108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  }
6129cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
61308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
614275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
6156d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
6166d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
6176d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
6186d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
6196d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
6206d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
6216d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
6229cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
623b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  // If load is legal, just bitcast the src pointer.
6247ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar  if (SrcSize >= DstSize) {
625f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // Generally SrcSize is never greater than DstSize, since this means we are
626f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // losing bits. However, this can happen in cases where the structure has
627f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // additional padding, for example due to a user specified alignment.
6287ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar    //
629f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: Assert that we aren't truncating non-padding bits when have access
630f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // to that information.
631275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
632275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
633386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
634386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned load.
635386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    Load->setAlignment(1);
636386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    return Load;
637275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
6389cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
63935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // Otherwise do coercion through memory. This is stupid, but
64035b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // simple.
64135b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
64235b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::Value *Casted =
64335b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
64435b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::StoreInst *Store =
64535b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
64635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // FIXME: Use better alignment / avoid requiring aligned store.
64735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  Store->setAlignment(1);
64835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  return CGF.Builder.CreateLoad(Tmp);
649275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
650275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
651badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// Function to store a first-class aggregate into memory.  We prefer to
652badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// store the elements rather than the aggregate to be more friendly to
653badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// fast-isel.
654badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// FIXME: Do we need to recurse here?
655badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedmanstatic void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
656badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                          llvm::Value *DestPtr, bool DestIsVolatile,
657badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                          bool LowAlignment) {
658badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  // Prefer scalar stores to first-class aggregate stores.
6592acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy =
660badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        dyn_cast<llvm::StructType>(Val->getType())) {
661badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
662badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
663badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
664badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
665badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                                                    DestIsVolatile);
666badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      if (LowAlignment)
667badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        SI->setAlignment(1);
668badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    }
669badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  } else {
67008212631e15a17157368af03180018601b31fb4dBill Wendling    llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
67108212631e15a17157368af03180018601b31fb4dBill Wendling    if (LowAlignment)
67208212631e15a17157368af03180018601b31fb4dBill Wendling      SI->setAlignment(1);
673badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  }
674badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman}
675badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman
676275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
677275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// where the source and destination may have different types.
678275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
679275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is larger than the
680275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; the upper bits of the src will be lost.
681275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic void CreateCoercedStore(llvm::Value *Src,
682275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               llvm::Value *DstPtr,
683d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                               bool DstIsVolatile,
684275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               CodeGenFunction &CGF) {
6852acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SrcTy = Src->getType();
6862acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *DstTy =
687275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
6886ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == DstTy) {
6896ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
6906ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return;
6916ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  }
6929cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6936ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
6949cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6952acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
696e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
697e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
698e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner  }
6999cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
7006d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
7016d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
7026d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
7036d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
7046d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
7056d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
7066d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return;
7076d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
7089cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
7099408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
710275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
71188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // If store is legal, just bitcast the src pointer.
712fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar  if (SrcSize <= DstSize) {
713275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
714275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
715386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned store.
716badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
717275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  } else {
718275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // Otherwise do coercion through memory. This is stupid, but
719275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // simple.
720fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar
721fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // Generally SrcSize is never greater than DstSize, since this means we are
722fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // losing bits. However, this can happen in cases where the structure has
723fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // additional padding, for example due to a user specified alignment.
724fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    //
725fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // FIXME: Assert that we aren't truncating non-padding bits when have access
726fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // to that information.
727275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
728275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(Src, Tmp);
7291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *Casted =
730275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
731386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
732386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned load.
733386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    Load->setAlignment(1);
734d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
735275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
736275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
737275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
7385627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/
7395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
740dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
74111e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  return FI.getReturnInfo().isIndirect();
742bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar}
743bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar
744dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
745dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
746dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    switch (BT->getKind()) {
747dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    default:
748dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return false;
749dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Float:
750bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Float);
751dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Double:
752bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Double);
753dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::LongDouble:
754bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      return getContext().getTargetInfo().useObjCFPRetForRealType(
755dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar        TargetInfo::LongDouble);
756dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    }
757dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  }
758dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
759dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  return false;
760dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar}
761dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
762eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlssonbool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
763eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson  if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
764eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson    if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
765eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson      if (BT->getKind() == BuiltinType::LongDouble)
766eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson        return getContext().getTargetInfo().useObjCFP2RetForComplexLongDouble();
767eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson    }
768eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson  }
769eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson
770eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson  return false;
771eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson}
772eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson
7739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
774de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
775de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return GetFunctionType(FI);
776c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall}
777c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall
7789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::FunctionType *
779de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
78071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
78171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted;
78271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(Inserted && "Recursively being processed?");
78371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
7845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Type*, 8> argTypes;
7852acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *resultType = 0;
78645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
78742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  const ABIArgInfo &retAI = FI.getReturnInfo();
78842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  switch (retAI.getKind()) {
7898951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
79042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    llvm_unreachable("Invalid ABI kind for return argument");
7918951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
792cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
79346327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
79442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = retAI.getCoerceToType();
79546327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    break;
79646327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar
79711e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  case ABIArgInfo::Indirect: {
79842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
79942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = llvm::Type::getVoidTy(getLLVMContext());
80042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall
80142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    QualType ret = FI.getReturnType();
8022acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *ty = ConvertType(ret);
80342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    unsigned addressSpace = Context.getTargetAddressSpace(ret);
80442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
80545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
80645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
80745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
80811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
80942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = llvm::Type::getVoidTy(getLLVMContext());
81011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    break;
81145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
81488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar         ie = FI.arg_end(); it != ie; ++it) {
81542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    const ABIArgInfo &argAI = it->info;
8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    switch (argAI.getKind()) {
81811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
81911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
82011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
821800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect: {
822800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // indirect arguments are always on the stack, which is addr space #0.
8232acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *LTy = ConvertTypeForMem(it->type);
82442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      argTypes.push_back(LTy->getPointerTo());
825800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
826800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
827800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
828800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
8291ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    case ABIArgInfo::Direct: {
830f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka      // Insert a padding type to ensure proper alignment.
831f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka      if (llvm::Type *PaddingType = argAI.getPaddingType())
832f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka        argTypes.push_back(PaddingType);
833ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, flatten it.  Either
834ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // way is semantically identical, but fast-isel and the optimizer
835ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // generally likes scalar values better than FCAs.
8369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      llvm::Type *argType = argAI.getCoerceToType();
8372acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      if (llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
83842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall        for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
83942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall          argTypes.push_back(st->getElementType(i));
840ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
84142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall        argTypes.push_back(argType);
842ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
84389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
8441ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    }
8451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8468951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
8479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      GetExpandedTypes(it->type, argTypes);
8488951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
8498951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
85045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
85145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
85271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
85371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(Erased && "Not in set?");
85471305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
855de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return llvm::FunctionType::get(resultType, argTypes, FI.isVariadic());
8563913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar}
8573913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar
8582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerllvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
8594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
860ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
8619cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
862f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (!isFuncTypeConvertible(FPT))
863f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return llvm::StructType::get(getLLVMContext());
864f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
865f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const CGFunctionInfo *Info;
866f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (isa<CXXDestructorDecl>(MD))
867de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    Info = &arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), GD.getDtorType());
868f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  else
869de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    Info = &arrangeCXXMethodDeclaration(MD);
870de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return GetFunctionType(*Info);
871ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson}
872ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson
873a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbarvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
87488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                           const Decl *TargetDecl,
8759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer                                           AttributeListType &PAL,
876ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar                                           unsigned &CallingConv) {
877c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany  llvm::Attributes FuncAttrs;
878c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany  llvm::Attributes RetAttrs;
8795323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
880ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CallingConv = FI.getEffectiveCallingConvention();
881ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar
88204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (FI.isNoReturn())
88304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    FuncAttrs |= llvm::Attribute::NoReturn;
88404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
8851102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov  // FIXME: handle sseregparm someday...
8865323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  if (TargetDecl) {
8876700415542121e2cb7d867728046ffa21e402019Rafael Espindola    if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
8886700415542121e2cb7d867728046ffa21e402019Rafael Espindola      FuncAttrs |= llvm::Attribute::ReturnsTwice;
88940b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<NoThrowAttr>())
890761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoUnwind;
8919c0c1f333ab8f5a3da055b99ee94778689face17John McCall    else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
8929c0c1f333ab8f5a3da055b99ee94778689face17John McCall      const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
8938026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl      if (FPT && FPT->isNothrow(getContext()))
8949c0c1f333ab8f5a3da055b99ee94778689face17John McCall        FuncAttrs |= llvm::Attribute::NoUnwind;
8959c0c1f333ab8f5a3da055b99ee94778689face17John McCall    }
8969c0c1f333ab8f5a3da055b99ee94778689face17John McCall
89740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<NoReturnAttr>())
898761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoReturn;
899041087caec03e8855770695d3eabc0feb031f6edEric Christopher
900f87cced71a955dca5731e7b28bc182e4824c0355Rafael Espindola    if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
901f87cced71a955dca5731e7b28bc182e4824c0355Rafael Espindola      FuncAttrs |= llvm::Attribute::ReturnsTwice;
902f87cced71a955dca5731e7b28bc182e4824c0355Rafael Espindola
903041087caec03e8855770695d3eabc0feb031f6edEric Christopher    // 'const' and 'pure' attribute functions are also nounwind.
904041087caec03e8855770695d3eabc0feb031f6edEric Christopher    if (TargetDecl->hasAttr<ConstAttr>()) {
905232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      FuncAttrs |= llvm::Attribute::ReadNone;
906041087caec03e8855770695d3eabc0feb031f6edEric Christopher      FuncAttrs |= llvm::Attribute::NoUnwind;
907041087caec03e8855770695d3eabc0feb031f6edEric Christopher    } else if (TargetDecl->hasAttr<PureAttr>()) {
90864c2e0762628eba26c100642521b6100c2515cc5Daniel Dunbar      FuncAttrs |= llvm::Attribute::ReadOnly;
909041087caec03e8855770695d3eabc0feb031f6edEric Christopher      FuncAttrs |= llvm::Attribute::NoUnwind;
910041087caec03e8855770695d3eabc0feb031f6edEric Christopher    }
91176168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn    if (TargetDecl->hasAttr<MallocAttr>())
91276168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn      RetAttrs |= llvm::Attribute::NoAlias;
9135323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
9145323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
9152811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.OptimizeSize)
9167ab1c3ebd02c31bfa1333cc51de1261c1499d6f7Daniel Dunbar    FuncAttrs |= llvm::Attribute::OptimizeForSize;
9172811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.DisableRedZone)
91824095dad88dd9d48aa16afa6416417073af251b5Devang Patel    FuncAttrs |= llvm::Attribute::NoRedZone;
9192811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.NoImplicitFloat)
920acebb397fa5d63835a0de9cee144987057ec1333Devang Patel    FuncAttrs |= llvm::Attribute::NoImplicitFloat;
92124095dad88dd9d48aa16afa6416417073af251b5Devang Patel
922a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  QualType RetTy = FI.getReturnType();
9235323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned Index = 1;
924b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = FI.getReturnInfo();
92545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  switch (RetAI.getKind()) {
926cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
9272eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   if (RetTy->hasSignedIntegerRepresentation())
928cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov     RetAttrs |= llvm::Attribute::SExt;
9292eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   else if (RetTy->hasUnsignedIntegerRepresentation())
930cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov     RetAttrs |= llvm::Attribute::ZExt;
931800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    break;
93246327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
933800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
9342c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
9352c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
93611e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  case ABIArgInfo::Indirect:
9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PAL.push_back(llvm::AttributeWithIndex::get(Index,
938fb97cf24158aa7f1fd74374052f99733ef331bb9Chris Lattner                                                llvm::Attribute::StructRet));
9395323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    ++Index;
9400ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar    // sret disables readnone and readonly
9410ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar    FuncAttrs &= ~(llvm::Attribute::ReadOnly |
9420ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar                   llvm::Attribute::ReadNone);
9432c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
9442c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
9458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
946b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid ABI kind for return argument");
9475323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
9482c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
949a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (RetAttrs)
950a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
9511102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
95217d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar  // FIXME: RegParm should be reduced in case of global register variable.
953a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  signed RegParm;
954a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (FI.getHasRegParm())
955a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    RegParm = FI.getRegParm();
956a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  else
95717d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar    RegParm = CodeGenOpts.NumRegisterParameters;
9581102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
959bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  unsigned PointerWidth = getContext().getTargetInfo().getPointerWidth(0);
9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
96188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar         ie = FI.arg_end(); it != ie; ++it) {
96288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    QualType ParamType = it->type;
96388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const ABIArgInfo &AI = it->info;
964c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany    llvm::Attributes Attrs;
9651102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
966d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
967d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // have the corresponding parameter variable.  It doesn't make
9687f6890ebb874cc16320259daef50f1b4cfdc47d5Daniel Dunbar    // sense to do it here because parameters are so messed up.
9698951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
970cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
971575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      if (ParamType->isSignedIntegerOrEnumerationType())
972c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany        Attrs |= llvm::Attribute::SExt;
973575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      else if (ParamType->isUnsignedIntegerOrEnumerationType())
974c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany        Attrs |= llvm::Attribute::ZExt;
975800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // FALL THROUGH
97646327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct:
9771102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      if (RegParm > 0 &&
9782871020b2d30b8a642c02280be849a7924426222Rafael Espindola          (ParamType->isIntegerType() || ParamType->isPointerType() ||
9792871020b2d30b8a642c02280be849a7924426222Rafael Espindola           ParamType->isReferenceType())) {
9801102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov        RegParm -=
981800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
9821102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov        if (RegParm >= 0)
983c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany          Attrs |= llvm::Attribute::InReg;
9841102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      }
9851102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      // FIXME: handle sseregparm someday...
9869cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
987f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka      // Increment Index if there is padding.
988f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka      Index += (AI.getPaddingType() != 0);
989f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka
9902acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      if (llvm::StructType *STy =
991800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner            dyn_cast<llvm::StructType>(AI.getCoerceToType()))
992800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        Index += STy->getNumElements()-1;  // 1 will be added below.
993800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
994800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
995800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect:
996800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (AI.getIndirectByVal())
997c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany        Attrs |= llvm::Attribute::ByVal;
998800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
999c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany      Attrs |=
1000800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
1001800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // byval disables readnone and readonly.
1002800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      FuncAttrs &= ~(llvm::Attribute::ReadOnly |
1003800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner                     llvm::Attribute::ReadNone);
10048951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
10051102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
100611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
100711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      // Skip increment, no matching LLVM parameter.
10081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      continue;
100911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
10105627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
10115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<llvm::Type*, 8> types;
1012f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // FIXME: This is rather inefficient. Do we ever actually need to do
1013f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // anything here? The result should be just reconstructed on the other
1014f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // side, so extension should be a non-issue.
10159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      getTypes().GetExpandedTypes(ParamType, types);
101642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      Index += types.size();
10175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
10185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
10195323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    }
10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1021c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany    if (Attrs)
1022c891666f477bfee387c10a429038eb6d98e9d175Kostya Serebryany      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attrs));
10235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++Index;
10245323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
1025a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (FuncAttrs)
1026a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
10275323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
10285323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
1029d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// An argument came in as a promoted argument; demote it back to its
1030d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// declared type.
1031d26bc76c98006609002d9930f8840490e88ac5b5John McCallstatic llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
1032d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         const VarDecl *var,
1033d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         llvm::Value *value) {
10342acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *varType = CGF.ConvertType(var->getType());
1035d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1036d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // This can happen with promotions that actually don't change the
1037d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // underlying type, like the enum promotions.
1038d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (value->getType() == varType) return value;
1039d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1040d26bc76c98006609002d9930f8840490e88ac5b5John McCall  assert((varType->isIntegerTy() || varType->isFloatingPointTy())
1041d26bc76c98006609002d9930f8840490e88ac5b5John McCall         && "unexpected promotion type");
1042d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1043d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (isa<llvm::IntegerType>(varType))
1044d26bc76c98006609002d9930f8840490e88ac5b5John McCall    return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
1045d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1046d26bc76c98006609002d9930f8840490e88ac5b5John McCall  return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
1047d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
1048d26bc76c98006609002d9930f8840490e88ac5b5John McCall
104988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
105088b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                         llvm::Function *Fn,
105117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         const FunctionArgList &Args) {
10520cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // If this is an implicit-return-zero function, go ahead and
10530cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // initialize the return value.  TODO: it might be nice to have
10540cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // a more general mechanism for this that didn't require synthesized
10550cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // return statements.
1056121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
10570cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    if (FD->hasImplicitReturnZero()) {
10580cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      QualType RetTy = FD->getResultType().getUnqualifiedType();
10592acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
1060c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson      llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
10610cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      Builder.CreateStore(Zero, ReturnValue);
10620cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    }
10630cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  }
10640cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall
1065f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from FunctionArgList; lift up and
1066f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // simplify.
10675251afa21d3583f2740fd4f83659d008625a7260Daniel Dunbar
106817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
106917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::Function::arg_iterator AI = Fn->arg_begin();
10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Name the struct return argument.
1072dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(FI)) {
107317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    AI->setName("agg.result");
1074410ffb2bc5f072d58a73c14560345bcf77dec1ccJohn McCall    AI->addAttr(llvm::Attribute::NoAlias);
107517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    ++AI;
107617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
10771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10784b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(FI.arg_size() == Args.size() &&
10794b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
1080093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  unsigned ArgNo = 1;
1081b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
1082093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1083093ac461b37a573dcf226fa55faed96f318169b9Devang Patel       i != e; ++i, ++info_it, ++ArgNo) {
1084d26bc76c98006609002d9930f8840490e88ac5b5John McCall    const VarDecl *Arg = *i;
1085b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    QualType Ty = info_it->type;
1086b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgI = info_it->info;
10878951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1088d26bc76c98006609002d9930f8840490e88ac5b5John McCall    bool isPromoted =
1089d26bc76c98006609002d9930f8840490e88ac5b5John McCall      isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
1090d26bc76c98006609002d9930f8840490e88ac5b5John McCall
10918951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (ArgI.getKind()) {
10921f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    case ABIArgInfo::Indirect: {
1093ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      llvm::Value *V = AI;
1094cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
10951f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      if (hasAggregateLLVMType(Ty)) {
1096cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // Aggregates and complex variables are accessed by reference.  All we
1097cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // need to do is realign the value, if requested
1098cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        if (ArgI.getIndirectRealign()) {
1099cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
1100cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
1101cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // Copy from the incoming argument pointer to the temporary with the
1102cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // appropriate alignment.
1103cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          //
1104cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // FIXME: We should have a common utility for generating an aggregate
1105cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // copy.
11062acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner          llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
1107fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck          CharUnits Size = getContext().getTypeSizeInChars(Ty);
1108c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
1109c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
1110c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          Builder.CreateMemCpy(Dst,
1111c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi                               Src,
1112fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                               llvm::ConstantInt::get(IntPtrTy,
1113fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                                                      Size.getQuantity()),
11149f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               ArgI.getIndirectAlign(),
11159f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               false);
1116cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          V = AlignedTemp;
1117cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        }
11181f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
11191f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Load scalar value from indirect argument.
1120fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
1121fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
1122d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1123d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1124d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
11251f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
1126093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
11271f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
11281f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    }
1129cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
1130cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
113146327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct: {
11324ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka      // Skip the dummy padding argument.
11334ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka      if (ArgI.getPaddingType())
11344ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka        ++AI;
11354ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka
1136800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If we have the trivial case, handle it with no muss and fuss.
1137800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
1138117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getCoerceToType() == ConvertType(Ty) &&
1139117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getDirectOffset() == 0) {
1140800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
1141800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *V = AI;
11429cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1143d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall        if (Arg->getType().isRestrictQualified())
1144d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall          AI->addAttr(llvm::Attribute::NoAlias);
1145d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall
1146b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner        // Ensure the argument is the correct type.
1147b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner        if (V->getType() != ArgI.getCoerceToType())
1148b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner          V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
1149b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner
1150d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1151d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
1152b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner
1153093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, V, ArgNo);
1154800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
115517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar      }
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1157a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov      llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName());
11589cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1159deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // The alignment we need to use is the max of the requested alignment for
1160deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // the argument plus the alignment required by our access code below.
11619cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      unsigned AlignmentToUse =
1162d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
1163deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      AlignmentToUse = std::max(AlignmentToUse,
1164deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner                        (unsigned)getContext().getDeclAlign(Arg).getQuantity());
11659cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1166deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      Alloca->setAlignment(AlignmentToUse);
1167121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner      llvm::Value *V = Alloca;
1168117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *Ptr = V;    // Pointer to store into.
11699cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1170117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1171117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgI.getDirectOffset()) {
1172117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1173117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
11749cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        Ptr = Builder.CreateBitCast(Ptr,
1175117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                          llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1176117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
11779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1178ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
1179ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
1180ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
1181a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov      llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
1182a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov      if (STy && STy->getNumElements() > 1) {
1183a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        uint64_t SrcSize = CGM.getTargetData().getTypeAllocSize(STy);
1184a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        llvm::Type *DstTy =
1185a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          cast<llvm::PointerType>(Ptr->getType())->getElementType();
1186a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        uint64_t DstSize = CGM.getTargetData().getTypeAllocSize(DstTy);
1187a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1188a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        if (SrcSize <= DstSize) {
1189a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
1190a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1191a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1192a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            assert(AI != Fn->arg_end() && "Argument mismatch!");
1193a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            AI->setName(Arg->getName() + ".coerce" + Twine(i));
1194a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1195a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            Builder.CreateStore(AI++, EltPtr);
1196a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          }
1197a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        } else {
1198a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          llvm::AllocaInst *TempAlloca =
1199a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            CreateTempAlloca(ArgI.getCoerceToType(), "coerce");
1200a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          TempAlloca->setAlignment(AlignmentToUse);
1201a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          llvm::Value *TempV = TempAlloca;
1202a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1203a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1204a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            assert(AI != Fn->arg_end() && "Argument mismatch!");
1205a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            AI->setName(Arg->getName() + ".coerce" + Twine(i));
1206a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i);
1207a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            Builder.CreateStore(AI++, EltPtr);
1208a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          }
1209a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1210a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse);
1211ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        }
1212ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
1213309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // Simple case, just do a coerced store of the argument into the alloca.
1214ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
1215225e286110bcc8b7b1ff8b35f0d51a10a158b18cChris Lattner        AI->setName(Arg->getName() + ".coerce");
1216117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
1217ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
12189cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
12199cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
122089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // Match to what EmitParmDecl is expecting for this type.
12218b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar      if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
122291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar        V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
1223d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1224d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
12258b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar      }
1226093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
1227ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      continue;  // Skip ++AI increment, already done.
122889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
1229800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1230800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Expand: {
1231800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If this structure was expanded into multiple arguments then
1232800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // we need to create a temporary and reconstruct it from the
1233800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // arguments.
12341bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman      llvm::AllocaInst *Alloca = CreateMemTemp(Ty);
12356da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      CharUnits Align = getContext().getDeclAlign(Arg);
12366da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      Alloca->setAlignment(Align.getQuantity());
12376da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      LValue LV = MakeAddrLValue(Alloca, Ty, Align);
12381bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman      llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI);
12391bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman      EmitParmDecl(*Arg, Alloca, ArgNo);
1240800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1241800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Name the arguments used in expansion and increment AI.
1242800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      unsigned Index = 0;
1243800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      for (; AI != End; ++AI, ++Index)
12445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        AI->setName(Arg->getName() + "." + Twine(Index));
1245800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
1246800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
1247800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1248800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Ignore:
1249800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Initialize the local variable appropriately.
1250800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (hasAggregateLLVMType(Ty))
1251093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1252800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      else
1253093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1254093ac461b37a573dcf226fa55faed96f318169b9Devang Patel                     ArgNo);
1255800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1256800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Skip increment, no matching LLVM parameter.
1257800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
12588951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
12595627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
12605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++AI;
126117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
126217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  assert(AI == Fn->arg_end() && "Argument mismatch!");
126317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
126417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
126577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCallstatic void eraseUnusedBitCasts(llvm::Instruction *insn) {
126677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  while (insn->use_empty()) {
126777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
126877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    if (!bitcast) return;
126977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
127077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    // This is "safe" because we would have used a ConstantExpr otherwise.
127177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    insn = cast<llvm::Instruction>(bitcast->getOperand(0));
127277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    bitcast->eraseFromParent();
127377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  }
127477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall}
127577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
1276f85e193739c953358c865005855253af4f68a497John McCall/// Try to emit a fused autorelease of a return result.
1277f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
1278f85e193739c953358c865005855253af4f68a497John McCall                                                    llvm::Value *result) {
1279f85e193739c953358c865005855253af4f68a497John McCall  // We must be immediately followed the cast.
1280f85e193739c953358c865005855253af4f68a497John McCall  llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
1281f85e193739c953358c865005855253af4f68a497John McCall  if (BB->empty()) return 0;
1282f85e193739c953358c865005855253af4f68a497John McCall  if (&BB->back() != result) return 0;
1283f85e193739c953358c865005855253af4f68a497John McCall
12842acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *resultType = result->getType();
1285f85e193739c953358c865005855253af4f68a497John McCall
1286f85e193739c953358c865005855253af4f68a497John McCall  // result is in a BasicBlock and is therefore an Instruction.
1287f85e193739c953358c865005855253af4f68a497John McCall  llvm::Instruction *generator = cast<llvm::Instruction>(result);
1288f85e193739c953358c865005855253af4f68a497John McCall
12895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Instruction*,4> insnsToKill;
1290f85e193739c953358c865005855253af4f68a497John McCall
1291f85e193739c953358c865005855253af4f68a497John McCall  // Look for:
1292f85e193739c953358c865005855253af4f68a497John McCall  //  %generator = bitcast %type1* %generator2 to %type2*
1293f85e193739c953358c865005855253af4f68a497John McCall  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
1294f85e193739c953358c865005855253af4f68a497John McCall    // We would have emitted this as a constant if the operand weren't
1295f85e193739c953358c865005855253af4f68a497John McCall    // an Instruction.
1296f85e193739c953358c865005855253af4f68a497John McCall    generator = cast<llvm::Instruction>(bitcast->getOperand(0));
1297f85e193739c953358c865005855253af4f68a497John McCall
1298f85e193739c953358c865005855253af4f68a497John McCall    // Require the generator to be immediately followed by the cast.
1299f85e193739c953358c865005855253af4f68a497John McCall    if (generator->getNextNode() != bitcast)
1300f85e193739c953358c865005855253af4f68a497John McCall      return 0;
1301f85e193739c953358c865005855253af4f68a497John McCall
1302f85e193739c953358c865005855253af4f68a497John McCall    insnsToKill.push_back(bitcast);
1303f85e193739c953358c865005855253af4f68a497John McCall  }
1304f85e193739c953358c865005855253af4f68a497John McCall
1305f85e193739c953358c865005855253af4f68a497John McCall  // Look for:
1306f85e193739c953358c865005855253af4f68a497John McCall  //   %generator = call i8* @objc_retain(i8* %originalResult)
1307f85e193739c953358c865005855253af4f68a497John McCall  // or
1308f85e193739c953358c865005855253af4f68a497John McCall  //   %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
1309f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
1310f85e193739c953358c865005855253af4f68a497John McCall  if (!call) return 0;
1311f85e193739c953358c865005855253af4f68a497John McCall
1312f85e193739c953358c865005855253af4f68a497John McCall  bool doRetainAutorelease;
1313f85e193739c953358c865005855253af4f68a497John McCall
1314f85e193739c953358c865005855253af4f68a497John McCall  if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) {
1315f85e193739c953358c865005855253af4f68a497John McCall    doRetainAutorelease = true;
1316f85e193739c953358c865005855253af4f68a497John McCall  } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints()
1317f85e193739c953358c865005855253af4f68a497John McCall                                          .objc_retainAutoreleasedReturnValue) {
1318f85e193739c953358c865005855253af4f68a497John McCall    doRetainAutorelease = false;
1319f85e193739c953358c865005855253af4f68a497John McCall
1320f85e193739c953358c865005855253af4f68a497John McCall    // Look for an inline asm immediately preceding the call and kill it, too.
1321f85e193739c953358c865005855253af4f68a497John McCall    llvm::Instruction *prev = call->getPrevNode();
1322f85e193739c953358c865005855253af4f68a497John McCall    if (llvm::CallInst *asmCall = dyn_cast_or_null<llvm::CallInst>(prev))
1323f85e193739c953358c865005855253af4f68a497John McCall      if (asmCall->getCalledValue()
1324f85e193739c953358c865005855253af4f68a497John McCall            == CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker)
1325f85e193739c953358c865005855253af4f68a497John McCall        insnsToKill.push_back(prev);
1326f85e193739c953358c865005855253af4f68a497John McCall  } else {
1327f85e193739c953358c865005855253af4f68a497John McCall    return 0;
1328f85e193739c953358c865005855253af4f68a497John McCall  }
1329f85e193739c953358c865005855253af4f68a497John McCall
1330f85e193739c953358c865005855253af4f68a497John McCall  result = call->getArgOperand(0);
1331f85e193739c953358c865005855253af4f68a497John McCall  insnsToKill.push_back(call);
1332f85e193739c953358c865005855253af4f68a497John McCall
1333f85e193739c953358c865005855253af4f68a497John McCall  // Keep killing bitcasts, for sanity.  Note that we no longer care
1334f85e193739c953358c865005855253af4f68a497John McCall  // about precise ordering as long as there's exactly one use.
1335f85e193739c953358c865005855253af4f68a497John McCall  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
1336f85e193739c953358c865005855253af4f68a497John McCall    if (!bitcast->hasOneUse()) break;
1337f85e193739c953358c865005855253af4f68a497John McCall    insnsToKill.push_back(bitcast);
1338f85e193739c953358c865005855253af4f68a497John McCall    result = bitcast->getOperand(0);
1339f85e193739c953358c865005855253af4f68a497John McCall  }
1340f85e193739c953358c865005855253af4f68a497John McCall
1341f85e193739c953358c865005855253af4f68a497John McCall  // Delete all the unnecessary instructions, from latest to earliest.
13425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<llvm::Instruction*>::iterator
1343f85e193739c953358c865005855253af4f68a497John McCall         i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i)
1344f85e193739c953358c865005855253af4f68a497John McCall    (*i)->eraseFromParent();
1345f85e193739c953358c865005855253af4f68a497John McCall
1346f85e193739c953358c865005855253af4f68a497John McCall  // Do the fused retain/autorelease if we were asked to.
1347f85e193739c953358c865005855253af4f68a497John McCall  if (doRetainAutorelease)
1348f85e193739c953358c865005855253af4f68a497John McCall    result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
1349f85e193739c953358c865005855253af4f68a497John McCall
1350f85e193739c953358c865005855253af4f68a497John McCall  // Cast back to the result type.
1351f85e193739c953358c865005855253af4f68a497John McCall  return CGF.Builder.CreateBitCast(result, resultType);
1352f85e193739c953358c865005855253af4f68a497John McCall}
1353f85e193739c953358c865005855253af4f68a497John McCall
135477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall/// If this is a +1 of the value of an immutable 'self', remove it.
135577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCallstatic llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
135677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall                                          llvm::Value *result) {
135777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // This is only applicable to a method with an immutable 'self'.
135877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  const ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CGF.CurCodeDecl);
135977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!method) return 0;
136077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  const VarDecl *self = method->getSelfDecl();
136177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!self->getType().isConstQualified()) return 0;
136277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
136377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // Look for a retain call.
136477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::CallInst *retainCall =
136577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    dyn_cast<llvm::CallInst>(result->stripPointerCasts());
136677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!retainCall ||
136777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall      retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain)
136877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    return 0;
136977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
137077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // Look for an ordinary load of 'self'.
137177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::Value *retainedValue = retainCall->getArgOperand(0);
137277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::LoadInst *load =
137377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
137477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!load || load->isAtomic() || load->isVolatile() ||
137577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall      load->getPointerOperand() != CGF.GetAddrOfLocalVar(self))
137677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    return 0;
137777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
137877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // Okay!  Burn it all down.  This relies for correctness on the
137977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // assumption that the retain is emitted as part of the return and
138077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // that thereafter everything is used "linearly".
138177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::Type *resultType = result->getType();
138277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  eraseUnusedBitCasts(cast<llvm::Instruction>(result));
138377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  assert(retainCall->use_empty());
138477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  retainCall->eraseFromParent();
138577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
138677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
138777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  return CGF.Builder.CreateBitCast(load, resultType);
138877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall}
138977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
1390f85e193739c953358c865005855253af4f68a497John McCall/// Emit an ARC autorelease of the result of a function.
139177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall///
139277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall/// \return the value to actually return from the function
1393f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
1394f85e193739c953358c865005855253af4f68a497John McCall                                            llvm::Value *result) {
139577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // If we're returning 'self', kill the initial retain.  This is a
139677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // heuristic attempt to "encourage correctness" in the really unfortunate
139777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // case where we have a return of self during a dealloc and we desperately
139877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // need to avoid the possible autorelease.
139977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
140077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    return self;
140177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
1402f85e193739c953358c865005855253af4f68a497John McCall  // At -O0, try to emit a fused retain/autorelease.
1403f85e193739c953358c865005855253af4f68a497John McCall  if (CGF.shouldUseFusedARCCalls())
1404f85e193739c953358c865005855253af4f68a497John McCall    if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
1405f85e193739c953358c865005855253af4f68a497John McCall      return fused;
1406f85e193739c953358c865005855253af4f68a497John McCall
1407f85e193739c953358c865005855253af4f68a497John McCall  return CGF.EmitARCAutoreleaseReturnValue(result);
1408f85e193739c953358c865005855253af4f68a497John McCall}
1409f85e193739c953358c865005855253af4f68a497John McCall
1410f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall/// Heuristically search for a dominating store to the return-value slot.
1411f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCallstatic llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
1412f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // If there are multiple uses of the return-value slot, just check
1413f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // for something immediately preceding the IP.  Sometimes this can
1414f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // happen with how we generate implicit-returns; it can also happen
1415f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // with noreturn cleanups.
1416f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  if (!CGF.ReturnValue->hasOneUse()) {
1417f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1418f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (IP->empty()) return 0;
1419f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back());
1420f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (!store) return 0;
1421f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (store->getPointerOperand() != CGF.ReturnValue) return 0;
1422f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    assert(!store->isAtomic() && !store->isVolatile()); // see below
1423f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    return store;
1424f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  }
1425f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1426f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  llvm::StoreInst *store =
1427f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    dyn_cast<llvm::StoreInst>(CGF.ReturnValue->use_back());
1428f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  if (!store) return 0;
1429f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1430f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // These aren't actually possible for non-coerced returns, and we
1431f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // only care about non-coerced returns on this code path.
1432f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  assert(!store->isAtomic() && !store->isVolatile());
1433f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1434f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // Now do a first-and-dirty dominance check: just walk up the
1435f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // single-predecessors chain from the current insertion point.
1436f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  llvm::BasicBlock *StoreBB = store->getParent();
1437f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1438f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  while (IP != StoreBB) {
1439f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (!(IP = IP->getSinglePredecessor()))
1440f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      return 0;
1441f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  }
1442f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1443f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // Okay, the store's basic block dominates the insertion point; we
1444f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // can do our thing.
1445f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  return store;
1446f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall}
1447f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
144835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattnervoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
14492c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  // Functions with no result always return void.
1450c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  if (ReturnValue == 0) {
1451c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    Builder.CreateRetVoid();
1452c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    return;
1453c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  }
145421fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar
14554751a53c5e5fed4bf2271e29cae7411c93a77df7Dan Gohman  llvm::DebugLoc RetDbgLoc;
1456c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  llvm::Value *RV = 0;
1457c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  QualType RetTy = FI.getReturnType();
1458c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  const ABIArgInfo &RetAI = FI.getReturnInfo();
1459cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
1460c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  switch (RetAI.getKind()) {
146191a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  case ABIArgInfo::Indirect: {
146291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1463c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    if (RetTy->isAnyComplexType()) {
1464c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1465c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1466c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1467c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      // Do nothing; aggregrates get evaluated directly into the destination.
1468c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    } else {
1469c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
147091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar                        false, Alignment, RetTy);
1471c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    }
1472c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
147391a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  }
14748951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1475c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Extend:
1476800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct:
1477117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1478117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        RetAI.getDirectOffset() == 0) {
1479800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // The internal return value temp always will have pointer-to-return-type
1480800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // type, just do a load.
14819cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1482f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      // If there is a dominating store to ReturnValue, we can elide
1483f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      // the load, zap the store, and usually zap the alloca.
1484f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) {
1485800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // Get the stored value and nuke the now-dead store.
1486800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RetDbgLoc = SI->getDebugLoc();
1487800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RV = SI->getValueOperand();
1488800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        SI->eraseFromParent();
14899cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1490800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // If that was the only use of the return value, nuke it as well now.
1491800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1492800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1493800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          ReturnValue = 0;
1494800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
1495f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1496f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      // Otherwise, we have to do a simple load.
1497f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      } else {
1498f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall        RV = Builder.CreateLoad(ReturnValue);
149935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner      }
1500800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    } else {
1501117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *V = ReturnValue;
1502117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1503117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = RetAI.getDirectOffset()) {
1504117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1505117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateConstGEP1_32(V, Offs);
15069cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        V = Builder.CreateBitCast(V,
1507117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1508117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
15099cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1510117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
151135b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    }
1512f85e193739c953358c865005855253af4f68a497John McCall
1513f85e193739c953358c865005855253af4f68a497John McCall    // In ARC, end functions that return a retainable type with a call
1514f85e193739c953358c865005855253af4f68a497John McCall    // to objc_autoreleaseReturnValue.
1515f85e193739c953358c865005855253af4f68a497John McCall    if (AutoreleaseResult) {
15164e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      assert(getLangOpts().ObjCAutoRefCount &&
1517f85e193739c953358c865005855253af4f68a497John McCall             !FI.isReturnsRetained() &&
1518f85e193739c953358c865005855253af4f68a497John McCall             RetTy->isObjCRetainableType());
1519f85e193739c953358c865005855253af4f68a497John McCall      RV = emitAutoreleaseOfResult(*this, RV);
1520f85e193739c953358c865005855253af4f68a497John McCall    }
1521f85e193739c953358c865005855253af4f68a497John McCall
1522c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
15231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1524800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
1525c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
15268951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1527c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Expand:
1528b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid ABI kind for return argument");
152917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
153121fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar  llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1532d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel  if (!RetDbgLoc.isUnknown())
1533d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel    Ret->setDebugLoc(RetDbgLoc);
153417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
153517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1536413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1537413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                          const VarDecl *param) {
15382736071ea3a46f90e65c93418961611d96c10ab9John McCall  // StartFunction converted the ABI-lowered parameter(s) into a
15392736071ea3a46f90e65c93418961611d96c10ab9John McCall  // local alloca.  We need to turn that into an r-value suitable
15402736071ea3a46f90e65c93418961611d96c10ab9John McCall  // for EmitCall.
1541413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  llvm::Value *local = GetAddrOfLocalVar(param);
15422736071ea3a46f90e65c93418961611d96c10ab9John McCall
1543413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  QualType type = param->getType();
15449cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
15452736071ea3a46f90e65c93418961611d96c10ab9John McCall  // For the most part, we just need to load the alloca, except:
15462736071ea3a46f90e65c93418961611d96c10ab9John McCall  // 1) aggregate r-values are actually pointers to temporaries, and
15472736071ea3a46f90e65c93418961611d96c10ab9John McCall  // 2) references to aggregates are pointers directly to the aggregate.
15482736071ea3a46f90e65c93418961611d96c10ab9John McCall  // I don't know why references to non-aggregates are different here.
1549413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1550413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    if (hasAggregateLLVMType(ref->getPointeeType()))
1551413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall      return args.add(RValue::getAggregate(local), type);
15522736071ea3a46f90e65c93418961611d96c10ab9John McCall
15532736071ea3a46f90e65c93418961611d96c10ab9John McCall    // Locals which are references to scalars are represented
15542736071ea3a46f90e65c93418961611d96c10ab9John McCall    // with allocas holding the pointer.
1555413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::get(Builder.CreateLoad(local)), type);
15562736071ea3a46f90e65c93418961611d96c10ab9John McCall  }
15572736071ea3a46f90e65c93418961611d96c10ab9John McCall
1558413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (type->isAnyComplexType()) {
1559413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1560413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::getComplex(complex), type);
1561413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  }
15622736071ea3a46f90e65c93418961611d96c10ab9John McCall
1563413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (hasAggregateLLVMType(type))
1564413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::getAggregate(local), type);
15652736071ea3a46f90e65c93418961611d96c10ab9John McCall
1566413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1567413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1568413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  return args.add(RValue::get(value), type);
15692736071ea3a46f90e65c93418961611d96c10ab9John McCall}
15702736071ea3a46f90e65c93418961611d96c10ab9John McCall
1571f85e193739c953358c865005855253af4f68a497John McCallstatic bool isProvablyNull(llvm::Value *addr) {
1572f85e193739c953358c865005855253af4f68a497John McCall  return isa<llvm::ConstantPointerNull>(addr);
1573f85e193739c953358c865005855253af4f68a497John McCall}
1574f85e193739c953358c865005855253af4f68a497John McCall
1575f85e193739c953358c865005855253af4f68a497John McCallstatic bool isProvablyNonNull(llvm::Value *addr) {
1576f85e193739c953358c865005855253af4f68a497John McCall  return isa<llvm::AllocaInst>(addr);
1577f85e193739c953358c865005855253af4f68a497John McCall}
1578f85e193739c953358c865005855253af4f68a497John McCall
1579f85e193739c953358c865005855253af4f68a497John McCall/// Emit the actual writing-back of a writeback.
1580f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWriteback(CodeGenFunction &CGF,
1581f85e193739c953358c865005855253af4f68a497John McCall                          const CallArgList::Writeback &writeback) {
1582f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *srcAddr = writeback.Address;
1583f85e193739c953358c865005855253af4f68a497John McCall  assert(!isProvablyNull(srcAddr) &&
1584f85e193739c953358c865005855253af4f68a497John McCall         "shouldn't have writeback for provably null argument");
1585f85e193739c953358c865005855253af4f68a497John McCall
1586f85e193739c953358c865005855253af4f68a497John McCall  llvm::BasicBlock *contBB = 0;
1587f85e193739c953358c865005855253af4f68a497John McCall
1588f85e193739c953358c865005855253af4f68a497John McCall  // If the argument wasn't provably non-null, we need to null check
1589f85e193739c953358c865005855253af4f68a497John McCall  // before doing the store.
1590f85e193739c953358c865005855253af4f68a497John McCall  bool provablyNonNull = isProvablyNonNull(srcAddr);
1591f85e193739c953358c865005855253af4f68a497John McCall  if (!provablyNonNull) {
1592f85e193739c953358c865005855253af4f68a497John McCall    llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
1593f85e193739c953358c865005855253af4f68a497John McCall    contBB = CGF.createBasicBlock("icr.done");
1594f85e193739c953358c865005855253af4f68a497John McCall
1595f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1596f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
1597f85e193739c953358c865005855253af4f68a497John McCall    CGF.EmitBlock(writebackBB);
1598f85e193739c953358c865005855253af4f68a497John McCall  }
1599f85e193739c953358c865005855253af4f68a497John McCall
1600f85e193739c953358c865005855253af4f68a497John McCall  // Load the value to writeback.
1601f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
1602f85e193739c953358c865005855253af4f68a497John McCall
1603f85e193739c953358c865005855253af4f68a497John McCall  // Cast it back, in case we're writing an id to a Foo* or something.
1604f85e193739c953358c865005855253af4f68a497John McCall  value = CGF.Builder.CreateBitCast(value,
1605f85e193739c953358c865005855253af4f68a497John McCall               cast<llvm::PointerType>(srcAddr->getType())->getElementType(),
1606f85e193739c953358c865005855253af4f68a497John McCall                            "icr.writeback-cast");
1607f85e193739c953358c865005855253af4f68a497John McCall
1608f85e193739c953358c865005855253af4f68a497John McCall  // Perform the writeback.
1609f85e193739c953358c865005855253af4f68a497John McCall  QualType srcAddrType = writeback.AddressType;
1610f85e193739c953358c865005855253af4f68a497John McCall  CGF.EmitStoreThroughLValue(RValue::get(value),
1611545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall                             CGF.MakeAddrLValue(srcAddr, srcAddrType));
1612f85e193739c953358c865005855253af4f68a497John McCall
1613f85e193739c953358c865005855253af4f68a497John McCall  // Jump to the continuation block.
1614f85e193739c953358c865005855253af4f68a497John McCall  if (!provablyNonNull)
1615f85e193739c953358c865005855253af4f68a497John McCall    CGF.EmitBlock(contBB);
1616f85e193739c953358c865005855253af4f68a497John McCall}
1617f85e193739c953358c865005855253af4f68a497John McCall
1618f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWritebacks(CodeGenFunction &CGF,
1619f85e193739c953358c865005855253af4f68a497John McCall                           const CallArgList &args) {
1620f85e193739c953358c865005855253af4f68a497John McCall  for (CallArgList::writeback_iterator
1621f85e193739c953358c865005855253af4f68a497John McCall         i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i)
1622f85e193739c953358c865005855253af4f68a497John McCall    emitWriteback(CGF, *i);
1623f85e193739c953358c865005855253af4f68a497John McCall}
1624f85e193739c953358c865005855253af4f68a497John McCall
1625f85e193739c953358c865005855253af4f68a497John McCall/// Emit an argument that's being passed call-by-writeback.  That is,
1626f85e193739c953358c865005855253af4f68a497John McCall/// we are passing the address of
1627f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
1628f85e193739c953358c865005855253af4f68a497John McCall                             const ObjCIndirectCopyRestoreExpr *CRE) {
1629f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr());
1630f85e193739c953358c865005855253af4f68a497John McCall
1631f85e193739c953358c865005855253af4f68a497John McCall  // The dest and src types don't necessarily match in LLVM terms
1632f85e193739c953358c865005855253af4f68a497John McCall  // because of the crazy ObjC compatibility rules.
1633f85e193739c953358c865005855253af4f68a497John McCall
16342acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *destType =
1635f85e193739c953358c865005855253af4f68a497John McCall    cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
1636f85e193739c953358c865005855253af4f68a497John McCall
1637f85e193739c953358c865005855253af4f68a497John McCall  // If the address is a constant null, just pass the appropriate null.
1638f85e193739c953358c865005855253af4f68a497John McCall  if (isProvablyNull(srcAddr)) {
1639f85e193739c953358c865005855253af4f68a497John McCall    args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
1640f85e193739c953358c865005855253af4f68a497John McCall             CRE->getType());
1641f85e193739c953358c865005855253af4f68a497John McCall    return;
1642f85e193739c953358c865005855253af4f68a497John McCall  }
1643f85e193739c953358c865005855253af4f68a497John McCall
1644f85e193739c953358c865005855253af4f68a497John McCall  QualType srcAddrType =
1645f85e193739c953358c865005855253af4f68a497John McCall    CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
1646f85e193739c953358c865005855253af4f68a497John McCall
1647f85e193739c953358c865005855253af4f68a497John McCall  // Create the temporary.
1648f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(),
1649f85e193739c953358c865005855253af4f68a497John McCall                                           "icr.temp");
1650f85e193739c953358c865005855253af4f68a497John McCall
1651f85e193739c953358c865005855253af4f68a497John McCall  // Zero-initialize it if we're not doing a copy-initialization.
1652f85e193739c953358c865005855253af4f68a497John McCall  bool shouldCopy = CRE->shouldCopy();
1653f85e193739c953358c865005855253af4f68a497John McCall  if (!shouldCopy) {
1654f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *null =
1655f85e193739c953358c865005855253af4f68a497John McCall      llvm::ConstantPointerNull::get(
1656f85e193739c953358c865005855253af4f68a497John McCall        cast<llvm::PointerType>(destType->getElementType()));
1657f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.CreateStore(null, temp);
1658f85e193739c953358c865005855253af4f68a497John McCall  }
1659f85e193739c953358c865005855253af4f68a497John McCall
1660f85e193739c953358c865005855253af4f68a497John McCall  llvm::BasicBlock *contBB = 0;
1661f85e193739c953358c865005855253af4f68a497John McCall
1662f85e193739c953358c865005855253af4f68a497John McCall  // If the address is *not* known to be non-null, we need to switch.
1663f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *finalArgument;
1664f85e193739c953358c865005855253af4f68a497John McCall
1665f85e193739c953358c865005855253af4f68a497John McCall  bool provablyNonNull = isProvablyNonNull(srcAddr);
1666f85e193739c953358c865005855253af4f68a497John McCall  if (provablyNonNull) {
1667f85e193739c953358c865005855253af4f68a497John McCall    finalArgument = temp;
1668f85e193739c953358c865005855253af4f68a497John McCall  } else {
1669f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1670f85e193739c953358c865005855253af4f68a497John McCall
1671f85e193739c953358c865005855253af4f68a497John McCall    finalArgument = CGF.Builder.CreateSelect(isNull,
1672f85e193739c953358c865005855253af4f68a497John McCall                                   llvm::ConstantPointerNull::get(destType),
1673f85e193739c953358c865005855253af4f68a497John McCall                                             temp, "icr.argument");
1674f85e193739c953358c865005855253af4f68a497John McCall
1675f85e193739c953358c865005855253af4f68a497John McCall    // If we need to copy, then the load has to be conditional, which
1676f85e193739c953358c865005855253af4f68a497John McCall    // means we need control flow.
1677f85e193739c953358c865005855253af4f68a497John McCall    if (shouldCopy) {
1678f85e193739c953358c865005855253af4f68a497John McCall      contBB = CGF.createBasicBlock("icr.cont");
1679f85e193739c953358c865005855253af4f68a497John McCall      llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
1680f85e193739c953358c865005855253af4f68a497John McCall      CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
1681f85e193739c953358c865005855253af4f68a497John McCall      CGF.EmitBlock(copyBB);
1682f85e193739c953358c865005855253af4f68a497John McCall    }
1683f85e193739c953358c865005855253af4f68a497John McCall  }
1684f85e193739c953358c865005855253af4f68a497John McCall
1685f85e193739c953358c865005855253af4f68a497John McCall  // Perform a copy if necessary.
1686f85e193739c953358c865005855253af4f68a497John McCall  if (shouldCopy) {
1687f85e193739c953358c865005855253af4f68a497John McCall    LValue srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
1688545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    RValue srcRV = CGF.EmitLoadOfLValue(srcLV);
1689f85e193739c953358c865005855253af4f68a497John McCall    assert(srcRV.isScalar());
1690f85e193739c953358c865005855253af4f68a497John McCall
1691f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *src = srcRV.getScalarVal();
1692f85e193739c953358c865005855253af4f68a497John McCall    src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
1693f85e193739c953358c865005855253af4f68a497John McCall                                    "icr.cast");
1694f85e193739c953358c865005855253af4f68a497John McCall
1695f85e193739c953358c865005855253af4f68a497John McCall    // Use an ordinary store, not a store-to-lvalue.
1696f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.CreateStore(src, temp);
1697f85e193739c953358c865005855253af4f68a497John McCall  }
1698f85e193739c953358c865005855253af4f68a497John McCall
1699f85e193739c953358c865005855253af4f68a497John McCall  // Finish the control flow if we needed it.
1700f85e193739c953358c865005855253af4f68a497John McCall  if (shouldCopy && !provablyNonNull)
1701f85e193739c953358c865005855253af4f68a497John McCall    CGF.EmitBlock(contBB);
1702f85e193739c953358c865005855253af4f68a497John McCall
1703f85e193739c953358c865005855253af4f68a497John McCall  args.addWriteback(srcAddr, srcAddrType, temp);
1704f85e193739c953358c865005855253af4f68a497John McCall  args.add(RValue::get(finalArgument), CRE->getType());
1705f85e193739c953358c865005855253af4f68a497John McCall}
1706f85e193739c953358c865005855253af4f68a497John McCall
1707413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1708413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                  QualType type) {
1709f85e193739c953358c865005855253af4f68a497John McCall  if (const ObjCIndirectCopyRestoreExpr *CRE
1710f85e193739c953358c865005855253af4f68a497John McCall        = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
17114e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    assert(getContext().getLangOpts().ObjCAutoRefCount);
1712f85e193739c953358c865005855253af4f68a497John McCall    assert(getContext().hasSameType(E->getType(), type));
1713f85e193739c953358c865005855253af4f68a497John McCall    return emitWritebackArg(*this, args, CRE);
1714f85e193739c953358c865005855253af4f68a497John McCall  }
1715f85e193739c953358c865005855253af4f68a497John McCall
17168affed5107ea79bf4d429770d2873e9c7288255eJohn McCall  assert(type->isReferenceType() == E->isGLValue() &&
17178affed5107ea79bf4d429770d2873e9c7288255eJohn McCall         "reference binding to unmaterialized r-value!");
17188affed5107ea79bf4d429770d2873e9c7288255eJohn McCall
1719cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall  if (E->isGLValue()) {
1720cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall    assert(E->getObjectKind() == OK_Ordinary);
1721413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1722413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                    type);
1723cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall  }
17241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
172570cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman  if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() &&
172670cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman      isa<ImplicitCastExpr>(E) &&
172755d484802f3e27930317739efc5f5956b78aac25Eli Friedman      cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
172855d484802f3e27930317739efc5f5956b78aac25Eli Friedman    LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
172955d484802f3e27930317739efc5f5956b78aac25Eli Friedman    assert(L.isSimple());
173051f512090530807e2c80f9411cc262025820c859Eli Friedman    args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
173155d484802f3e27930317739efc5f5956b78aac25Eli Friedman    return;
173255d484802f3e27930317739efc5f5956b78aac25Eli Friedman  }
173355d484802f3e27930317739efc5f5956b78aac25Eli Friedman
1734413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  args.add(EmitAnyExprToTemp(E), type);
17350139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson}
17360139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson
1737b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
1738b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman// optimizer it can aggressively ignore unwind edges.
1739b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohmanvoid
1740b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan GohmanCodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
1741b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
1742b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman      !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
1743b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
1744b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman                      CGM.getNoObjCARCExceptionsMetadata());
1745b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman}
1746b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
1747f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Emits a call or invoke instruction to the given function, depending
1748f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// on the current state of the EH stack.
1749f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::CallSite
1750f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
17512d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                                  ArrayRef<llvm::Value *> Args,
17525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  const Twine &Name) {
1753f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = getInvokeDest();
1754b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
1755b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  llvm::Instruction *Inst;
1756f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest)
1757b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    Inst = Builder.CreateCall(Callee, Args, Name);
1758b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  else {
1759b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1760b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name);
1761b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    EmitBlock(ContBB);
1762b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  }
1763b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
1764b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
1765b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // optimizer it can aggressively ignore unwind edges.
17664e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().ObjCAutoRefCount)
1767b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    AddObjCARCExceptionMetadata(Inst);
1768f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1769b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  return Inst;
1770f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
1771f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
17724c7d9f1507d0f102bd4133bba63348636facd469Jay Foadllvm::CallSite
17734c7d9f1507d0f102bd4133bba63348636facd469Jay FoadCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
17745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  const Twine &Name) {
17752d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner  return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name);
17764c7d9f1507d0f102bd4133bba63348636facd469Jay Foad}
17774c7d9f1507d0f102bd4133bba63348636facd469Jay Foad
1778708554498595e047cc53e366c91cc063fcc1c5bcChris Lattnerstatic void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo,
1779708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner                            llvm::FunctionType *FTy) {
1780708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  if (ArgNo < FTy->getNumParams())
1781708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    assert(Elt->getType() == FTy->getParamType(ArgNo));
1782708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  else
1783708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    assert(FTy->isVarArg());
1784708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  ++ArgNo;
1785708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner}
1786708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
1787811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattnervoid CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
17885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                       SmallVector<llvm::Value*,16> &Args,
1789811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner                                       llvm::FunctionType *IRFuncTy) {
1790194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
1791194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    unsigned NumElts = AT->getSize().getZExtValue();
1792194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    QualType EltTy = AT->getElementType();
1793194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    llvm::Value *Addr = RV.getAggregateAddr();
1794194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
1795194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt);
1796194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      LValue LV = MakeAddrLValue(EltAddr, EltTy);
1797194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      RValue EltRV;
1798ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman      if (EltTy->isAnyComplexType())
1799ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman        // FIXME: Volatile?
1800ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman        EltRV = RValue::getComplex(LoadComplexFromAddr(LV.getAddress(), false));
1801ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman      else if (CodeGenFunction::hasAggregateLLVMType(EltTy))
180251f512090530807e2c80f9411cc262025820c859Eli Friedman        EltRV = LV.asAggregateRValue();
1803194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      else
1804194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        EltRV = EmitLoadOfLValue(LV);
1805194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy);
1806811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    }
1807eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
1808194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    RecordDecl *RD = RT->getDecl();
1809194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1810377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty);
1811eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
1812eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->isUnion()) {
1813eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      const FieldDecl *LargestFD = 0;
1814eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      CharUnits UnionSize = CharUnits::Zero();
1815eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
1816eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1817eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
1818eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        const FieldDecl *FD = *i;
1819eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        assert(!FD->isBitField() &&
1820eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
1821eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
1822eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        if (UnionSize < FieldSize) {
1823eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          UnionSize = FieldSize;
1824eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          LargestFD = FD;
1825eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        }
1826eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
1827eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      if (LargestFD) {
1828377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        RValue FldRV = EmitRValueForField(LV, LargestFD);
1829eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        ExpandTypeToArgs(LargestFD->getType(), FldRV, Args, IRFuncTy);
1830eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
1831eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    } else {
1832eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1833eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
1834eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        FieldDecl *FD = *i;
1835eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
1836377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        RValue FldRV = EmitRValueForField(LV, FD);
1837eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        ExpandTypeToArgs(FD->getType(), FldRV, Args, IRFuncTy);
1838eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
1839194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
1840ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman  } else if (Ty->isAnyComplexType()) {
1841194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    ComplexPairTy CV = RV.getComplexVal();
1842194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Args.push_back(CV.first);
1843194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Args.push_back(CV.second);
1844194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
1845811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    assert(RV.isScalar() &&
1846811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner           "Unexpected non-scalar rvalue during struct expansion.");
1847811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
1848811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    // Insert a bitcast as needed.
1849811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    llvm::Value *V = RV.getScalarVal();
1850811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    if (Args.size() < IRFuncTy->getNumParams() &&
1851811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner        V->getType() != IRFuncTy->getParamType(Args.size()))
1852811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner      V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size()));
1853811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
1854811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    Args.push_back(V);
1855811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner  }
1856811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner}
1857811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
1858811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
185988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel DunbarRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
18601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 llvm::Value *Callee,
1861f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                                 ReturnValueSlot ReturnValue,
1862c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar                                 const CallArgList &CallArgs,
1863dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall                                 const Decl *TargetDecl,
18644b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall                                 llvm::Instruction **callOrInvoke) {
1865f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
18665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value*, 16> Args;
186717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
186817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Handle struct-return functions by passing a pointer to the
186917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // location that we would like to return into.
1870bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  QualType RetTy = CallInfo.getReturnType();
1871b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
18721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1873708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  // IRArgNo - Keep track of the argument number in the callee we're looking at.
1874708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  unsigned IRArgNo = 0;
1875708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  llvm::FunctionType *IRFuncTy =
1876708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    cast<llvm::FunctionType>(
1877708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner                  cast<llvm::PointerType>(Callee->getType())->getElementType());
18781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18795db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the call returns a temporary with struct return, create a temporary
1880d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  // alloca to hold the result, unless one is given to us.
1881dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(CallInfo)) {
1882d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *Value = ReturnValue.getValue();
1883d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!Value)
1884195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      Value = CreateMemTemp(RetTy);
1885d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    Args.push_back(Value);
1886708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    checkArgMatches(Value, IRArgNo, IRFuncTy);
1887d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  }
18881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18894b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(CallInfo.arg_size() == CallArgs.size() &&
18904b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
1891b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
18921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1893b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar       I != E; ++I, ++info_it) {
1894b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgInfo = info_it->info;
1895c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman    RValue RV = I->RV;
18965627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
189797cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman    unsigned TypeAlign =
1898c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman      getContext().getTypeAlignInChars(I->Ty).getQuantity();
18995627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    switch (ArgInfo.getKind()) {
190091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    case ABIArgInfo::Indirect: {
19011f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      if (RV.isScalar() || RV.isComplex()) {
19021f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Make a temporary alloca to pass the argument.
190370cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman        llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
190470cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman        if (ArgInfo.getIndirectAlign() > AI->getAlignment())
190570cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman          AI->setAlignment(ArgInfo.getIndirectAlign());
190670cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman        Args.push_back(AI);
1907708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
19081f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        if (RV.isScalar())
190991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar          EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
191097cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman                            TypeAlign, I->Ty);
19111f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        else
19121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1913708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
1914708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        // Validate argument match.
1915708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        checkArgMatches(AI, IRArgNo, IRFuncTy);
19161f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
1917ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        // We want to avoid creating an unnecessary temporary+copy here;
1918ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        // however, we need one in two cases:
1919ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        // 1. If the argument is not byval, and we are required to copy the
1920ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        //    source.  (This case doesn't occur on any common architecture.)
1921ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        // 2. If the argument is byval, RV is not sufficiently aligned, and
1922ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        //    we cannot force it to be sufficiently aligned.
192397cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        llvm::Value *Addr = RV.getAggregateAddr();
192497cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        unsigned Align = ArgInfo.getIndirectAlign();
192597cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        const llvm::TargetData *TD = &CGM.getTargetData();
192697cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
192797cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman            (ArgInfo.getIndirectByVal() && TypeAlign < Align &&
192897cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman             llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) {
1929ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman          // Create an aligned temporary, and copy to it.
193097cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman          llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
193197cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman          if (Align > AI->getAlignment())
193297cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman            AI->setAlignment(Align);
1933ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman          Args.push_back(AI);
1934649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier          EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
1935708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
1936708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          // Validate argument match.
1937708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          checkArgMatches(AI, IRArgNo, IRFuncTy);
1938ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        } else {
1939ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman          // Skip the extra memcpy call.
194097cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman          Args.push_back(Addr);
1941708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
1942708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          // Validate argument match.
1943708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          checkArgMatches(Addr, IRArgNo, IRFuncTy);
1944ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        }
19451f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
19461f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
194791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    }
19481f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar
194911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
195011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
19519cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1952800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
1953800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Direct: {
1954f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka      // Insert a padding argument to ensure proper alignment.
1955f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka      if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) {
1956f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka        Args.push_back(llvm::UndefValue::get(PaddingType));
1957f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka        ++IRArgNo;
1958f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka      }
1959f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka
1960800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
1961117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1962117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getDirectOffset() == 0) {
1963708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        llvm::Value *V;
1964800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (RV.isScalar())
1965708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          V = RV.getScalarVal();
1966800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        else
1967708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          V = Builder.CreateLoad(RV.getAggregateAddr());
1968708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
196921ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner        // If the argument doesn't match, perform a bitcast to coerce it.  This
197021ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner        // can happen due to trivial type mismatches.
197121ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner        if (IRArgNo < IRFuncTy->getNumParams() &&
197221ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner            V->getType() != IRFuncTy->getParamType(IRArgNo))
197321ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner          V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo));
1974708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        Args.push_back(V);
1975708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
1976708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        checkArgMatches(V, IRArgNo, IRFuncTy);
1977800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
1978800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
197911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
198089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // FIXME: Avoid the conversion through memory if possible.
198189c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      llvm::Value *SrcPtr;
198289c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      if (RV.isScalar()) {
1983c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman        SrcPtr = CreateMemTemp(I->Ty, "coerce");
198497cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty);
198589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      } else if (RV.isComplex()) {
1986c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman        SrcPtr = CreateMemTemp(I->Ty, "coerce");
198789c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar        StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
19881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
198989c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar        SrcPtr = RV.getAggregateAddr();
19909cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1991117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1992117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgInfo.getDirectOffset()) {
1993117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1994117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
19959cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        SrcPtr = Builder.CreateBitCast(SrcPtr,
1996117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                       llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1997117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
1998117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
19999cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2000ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
2001ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
2002ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
20032acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      if (llvm::StructType *STy =
2004309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner            dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
20059282688a296b306c4ae2d115f55101647056d1daChris Lattner        SrcPtr = Builder.CreateBitCast(SrcPtr,
20069282688a296b306c4ae2d115f55101647056d1daChris Lattner                                       llvm::PointerType::getUnqual(STy));
20079282688a296b306c4ae2d115f55101647056d1daChris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
20089282688a296b306c4ae2d115f55101647056d1daChris Lattner          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
2009deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
2010deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          // We don't know what we're loading from.
2011deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          LI->setAlignment(1);
2012deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          Args.push_back(LI);
2013708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2014708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          // Validate argument match.
2015708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          checkArgMatches(LI, IRArgNo, IRFuncTy);
2016309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        }
2017ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
2018309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // In the simple case, just pass the coerced loaded value.
2019309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2020309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner                                         *this));
2021708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2022708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        // Validate argument match.
2023708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        checkArgMatches(Args.back(), IRArgNo, IRFuncTy);
2024ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
20259cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
202689c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
202789c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
202889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar
20295627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand:
2030811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner      ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy);
2031708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner      IRArgNo = Args.size();
20325627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
203317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
203417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
20351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20365db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the callee is a bitcast of a function to a varargs pointer to function
20375db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // type, check to see if we can remove the bitcast.  This handles some cases
20385db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // with unprototyped functions.
20395db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
20405db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
20412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
20422acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::FunctionType *CurFT =
20435db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        cast<llvm::FunctionType>(CurPT->getElementType());
20442acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
20451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20465db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      if (CE->getOpcode() == llvm::Instruction::BitCast &&
20475db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          ActualFT->getReturnType() == CurFT->getReturnType() &&
2048d6bebbfd510f3b495795b88aafd10ead3cb211e9Chris Lattner          ActualFT->getNumParams() == CurFT->getNumParams() &&
2049c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          ActualFT->getNumParams() == Args.size() &&
2050c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          (CurFT->isVarArg() || !ActualFT->isVarArg())) {
20515db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        bool ArgsMatch = true;
20525db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
20535db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
20545db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            ArgsMatch = false;
20555db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            break;
20565db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          }
20571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20585db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // Strip the cast if we can get away with it.  This is a nice cleanup,
20595db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // but also allows us to inline the function at -O0 if it is marked
20605db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // always_inline.
20615db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        if (ArgsMatch)
20625db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          Callee = CalleeF;
20635db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      }
20645db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    }
20651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2066ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  unsigned CallingConv;
2067761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CodeGen::AttributeListType AttributeList;
2068ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
20699834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
20709834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar                                                   AttributeList.end());
20711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2072f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = 0;
2073f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
2074f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    InvokeDest = getInvokeDest();
2075f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
2076d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::CallSite CS;
2077f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest) {
20784c7d9f1507d0f102bd4133bba63348636facd469Jay Foad    CS = Builder.CreateCall(Callee, Args);
20799834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  } else {
20809834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
20814c7d9f1507d0f102bd4133bba63348636facd469Jay Foad    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args);
20829834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    EmitBlock(Cont);
2083f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar  }
2084ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner  if (callOrInvoke)
20854b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall    *callOrInvoke = CS.getInstruction();
2086f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar
2087d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  CS.setAttributes(Attrs);
2088ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
2089d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
2090b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2091b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // optimizer it can aggressively ignore unwind edges.
20924e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().ObjCAutoRefCount)
2093b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    AddObjCARCExceptionMetadata(CS.getInstruction());
2094b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
2095d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // If the call doesn't return, finish the basic block and clear the
2096d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // insertion point; this allows the rest of IRgen to discard
2097d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // unreachable code.
2098d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  if (CS.doesNotReturn()) {
2099d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.CreateUnreachable();
2100d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.ClearInsertionPoint();
21011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2102f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: For now, emit a dummy basic block because expr emitters in
2103f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // generally are not ready to handle emitting expressions at unreachable
2104f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // points.
2105d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    EnsureInsertPoint();
21061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2107d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    // Return a reasonable RValue.
2108d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    return GetUndefRValue(RetTy);
21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2110d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
2111d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::Instruction *CI = CS.getInstruction();
2112ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer  if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
211317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setName("call");
21142c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
2115f85e193739c953358c865005855253af4f68a497John McCall  // Emit any writebacks immediately.  Arguably this should happen
2116f85e193739c953358c865005855253af4f68a497John McCall  // after any return-value munging.
2117f85e193739c953358c865005855253af4f68a497John McCall  if (CallArgs.hasWritebacks())
2118f85e193739c953358c865005855253af4f68a497John McCall    emitWritebacks(*this, CallArgs);
2119f85e193739c953358c865005855253af4f68a497John McCall
21202c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
212191a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  case ABIArgInfo::Indirect: {
212291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
21232c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    if (RetTy->isAnyComplexType())
21245627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
21253403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
21265627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getAggregate(Args[0]);
212791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
212891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  }
21298951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
213011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
21310bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // If we are ignoring an argument that had a result, make sure to
21320bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // construct the appropriate return value for our caller.
213313e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar    return GetUndefRValue(RetTy);
21349cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2135800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Extend:
2136800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct: {
21376af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner    llvm::Type *RetIRTy = ConvertType(RetTy);
21386af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner    if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
2139800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (RetTy->isAnyComplexType()) {
2140800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2141800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2142800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getComplex(std::make_pair(Real, Imag));
2143800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
2144800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
2145800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *DestPtr = ReturnValue.getValue();
2146800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        bool DestIsVolatile = ReturnValue.isVolatile();
214711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
2148800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (!DestPtr) {
2149800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestPtr = CreateMemTemp(RetTy, "agg.tmp");
2150800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestIsVolatile = false;
2151800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
2152badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
2153800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getAggregate(DestPtr);
2154800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
21556af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner
21566af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner      // If the argument doesn't match, perform a bitcast to coerce it.  This
21576af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner      // can happen due to trivial type mismatches.
21586af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner      llvm::Value *V = CI;
21596af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner      if (V->getType() != RetIRTy)
21606af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner        V = Builder.CreateBitCast(V, RetIRTy);
21616af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner      return RValue::get(V);
2162800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
21639cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2164d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *DestPtr = ReturnValue.getValue();
2165d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    bool DestIsVolatile = ReturnValue.isVolatile();
21669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2167d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!DestPtr) {
2168195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      DestPtr = CreateMemTemp(RetTy, "coerce");
2169d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      DestIsVolatile = false;
2170d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    }
21719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2172117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the value is offset in memory, apply the offset now.
2173117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    llvm::Value *StorePtr = DestPtr;
2174117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (unsigned Offs = RetAI.getDirectOffset()) {
2175117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
2176117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
21779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      StorePtr = Builder.CreateBitCast(StorePtr,
2178117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
2179117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    }
2180117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
21819cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
218291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
2183ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson    if (RetTy->isAnyComplexType())
2184d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
21853403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
2186d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      return RValue::getAggregate(DestPtr);
218791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
2188639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  }
21898951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
21908951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
2191b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid ABI kind for return argument");
219217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
21932c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
2194b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled ABIArgInfo::Kind");
219517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
2196b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
2197b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar/* VarArg handling */
2198b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
2199b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbarllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2200b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2201b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar}
2202