CGCall.cpp revision 55d484802f3e27930317739efc5f5956b78aac25
10dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
20dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
30dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//                     The LLVM Compiler Infrastructure
40dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
50dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// This file is distributed under the University of Illinois Open Source
60dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// License. See LICENSE.TXT for details.
70dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
80dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
90dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
100dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// These classes wrap the information about a call or function
110dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// definition used to handle ABI compliancy.
120dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
130dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
140dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
150dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h"
164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall#include "CGCXXABI.h"
17ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner#include "ABIInfo.h"
180dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CodeGenFunction.h"
19b768807c49a1c7085def099b848631856af766faDaniel Dunbar#include "CodeGenModule.h"
206b1da0ea19c12346192f5ea4d70872c13bfcc82aDaniel Dunbar#include "clang/Basic/TargetInfo.h"
210dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/Decl.h"
22f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson#include "clang/AST/DeclCXX.h"
230dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/DeclObjC.h"
2406057cef0bcd7804e80f3ce2bbe352178396c715Chandler Carruth#include "clang/Frontend/CodeGenOptions.h"
25d0646bd7c11c12b34971b55e5c1bdd8439401b4cDevang Patel#include "llvm/Attributes.h"
26d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar#include "llvm/Support/CallSite.h"
2754d1ccbfcf19ddf39444f1b4018dd79487cc847bDaniel Dunbar#include "llvm/Target/TargetData.h"
280dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace clang;
290dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace CodeGen;
300dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
310dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar/***/
320dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
3304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
3404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  switch (CC) {
3504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  default: return llvm::CallingConv::C;
3604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
3704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
38f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
39414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
40414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
4152fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  // TODO: add support for CC_X86Pascal to llvm
4204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
4404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
450b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Derives the 'this' type for codegen purposes, i.e. ignoring method
460b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualification.
470b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// FIXME: address space qualification?
48ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
49ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
50ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
510b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
520b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
530b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the canonical formal type of the given C++ method.
54ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
55ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return MD->getType()->getCanonicalTypeUnqualified()
56ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall           .getAs<FunctionProtoType>();
570b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
580b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
590b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the "extra-canonicalized" return type, which discards
600b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualifiers on the return type.  Codegen doesn't care about them,
610b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// and it makes ABI code a little easier to be able to assume that
620b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// all parameter and return types are top-level unqualified.
63ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetReturnType(QualType RetTy) {
64ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
650b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
660b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
670b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
68bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris LattnerCodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
69bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                              bool IsRecursive) {
70ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
71ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                         llvm::SmallVector<CanQualType, 16>(),
72bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                         FTNP->getExtInfo(), IsRecursive);
7345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
7445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
750b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// \param Args - contains any initial parameters besides those
760b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall///   in the formal type
770b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallstatic const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
78ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                                  llvm::SmallVectorImpl<CanQualType> &ArgTys,
79bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                             CanQual<FunctionProtoType> FTP,
80bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                             bool IsRecursive = false) {
81541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
8245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
83541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    ArgTys.push_back(FTP->getArgType(i));
84ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
859c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
860b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
870b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
880b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
89bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris LattnerCodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
90bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                              bool IsRecursive) {
91ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
929c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
93bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar}
94bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
9504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic CallingConv getCallingConventionForDecl(const Decl *D) {
96bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  // Set the appropriate calling convention for the Function.
97bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<StdCallAttr>())
9804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86StdCall;
99bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
100bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<FastCallAttr>())
10104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86FastCall;
102bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
103f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  if (D->hasAttr<ThisCallAttr>())
104f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor    return CC_X86ThisCall;
105f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor
10652fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  if (D->hasAttr<PascalAttr>())
10752fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik    return CC_X86Pascal;
10852fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik
109414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  if (PcsAttr *PCS = D->getAttr<PcsAttr>())
110414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
111414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov
11204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  return CC_C;
11345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
11445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
115375c31c4673f83f925de221752cf801c2fbbb246Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
116375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson                                                 const FunctionProtoType *FTP) {
117ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
1180b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
119375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson  // Add the 'this' pointer.
1200b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  ArgTys.push_back(GetThisType(Context, RD));
1210b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1220b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  return ::getFunctionInfo(*this, ArgTys,
1239c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller              FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
124375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson}
125375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson
126f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
127ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
1280b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
129fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
130fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
131fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall
1323eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  // Add the 'this' pointer unless this is a static method.
1333eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  if (MD->isInstance())
1340b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall    ArgTys.push_back(GetThisType(Context, MD->getParent()));
1351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1369c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
137f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson}
138f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson
1399cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencerconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
140f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                                    CXXCtorType Type) {
141ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
1420b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  ArgTys.push_back(GetThisType(Context, D->getParent()));
1434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQualType ResTy = Context.VoidTy;
144f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
1454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
1460b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
1484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Add the formal parameters.
1504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
1514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(FTP->getArgType(i));
1524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1539c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
154f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
155f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
156f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
157f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                                    CXXDtorType Type) {
1584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  llvm::SmallVector<CanQualType, 2> ArgTys;
159ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  ArgTys.push_back(GetThisType(Context, D->getParent()));
1604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQualType ResTy = Context.VoidTy;
1614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
1630b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
1654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
1664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1679c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
168f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
169f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
170541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
1713eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
172f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson    if (MD->isInstance())
173f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson      return getFunctionInfo(MD);
1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
175ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
176ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionType>(FTy));
1770b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  if (isa<FunctionNoProtoType>(FTy))
1789cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer    return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
179ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionProtoType>(FTy));
180ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return getFunctionInfo(FTy.getAs<FunctionProtoType>());
1810dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
1820dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
183541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
184ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
185ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
186ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
187541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy?
1882073216a1075767b5d25c23d1462b7034686d94dChris Lattner  for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
1890b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall         e = MD->param_end(); i != e; ++i) {
1900b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall    ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
1910b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  }
1920b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  return getFunctionInfo(GetReturnType(MD->getResultType()),
1930b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall                         ArgTys,
194264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                         FunctionType::ExtInfo(
195264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                             /*NoReturn*/ false,
196a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman                             /*HasRegParm*/ false,
197425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                             /*RegParm*/ 0,
198264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                             getCallingConventionForDecl(MD)));
1990dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
2000dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
201b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
202b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  // FIXME: Do we need to handle ObjCMethodDecl?
203b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2049cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
205b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
206b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson    return getFunctionInfo(CD, GD.getCtorType());
207b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
208b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
209b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson    return getFunctionInfo(DD, GD.getDtorType());
2109cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
211b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  return getFunctionInfo(FD);
212b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson}
213b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
2141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
215bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                                                    const CallArgList &Args,
216264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                            const FunctionType::ExtInfo &Info) {
217541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
218ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
220725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar       i != e; ++i)
221c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman    ArgTys.push_back(Context.getCanonicalParamType(i->Ty));
222264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
2230dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
2240dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
226bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                                                    const FunctionArgList &Args,
227264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                            const FunctionType::ExtInfo &Info) {
228541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
229ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
231bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar       i != e; ++i)
232d26bc76c98006609002d9930f8840490e88ac5b5John McCall    ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
233264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
234541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar}
235541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
236d26bc76c98006609002d9930f8840490e88ac5b5John McCallconst CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
237d26bc76c98006609002d9930f8840490e88ac5b5John McCall  llvm::SmallVector<CanQualType, 1> args;
238d26bc76c98006609002d9930f8840490e88ac5b5John McCall  return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
239d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
240d26bc76c98006609002d9930f8840490e88ac5b5John McCall
241ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
242ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                           const llvm::SmallVectorImpl<CanQualType> &ArgTys,
243bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                            const FunctionType::ExtInfo &Info,
244bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                                    bool IsRecursive) {
245ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#ifndef NDEBUG
246ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  for (llvm::SmallVectorImpl<CanQualType>::const_iterator
247ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall         I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
248ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall    assert(I->isCanonicalAsParam());
249ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#endif
250ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall
251425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
25204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
25340a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  // Lookup or create unique function info.
25440a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  llvm::FoldingSetNodeID ID;
255264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CGFunctionInfo::Profile(ID, Info, ResTy,
256bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                          ArgTys.begin(), ArgTys.end());
25740a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
25840a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  void *InsertPos = 0;
25940a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
26040a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  if (FI)
26140a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    return *FI;
26240a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
26388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // Construct the function info.
264a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getHasRegParm(), Info.getRegParm(), ResTy,
2659c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                          ArgTys.data(), ArgTys.size());
26635e67d4387bbe3e7e17ee6b17eaa42eebb0eb9f1Daniel Dunbar  FunctionInfos.InsertNode(FI, InsertPos);
267541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
26888c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // Compute ABI information.
269ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  getABIInfo().computeInfo(*FI);
2709cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
271800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // Loop over all of the computed argument and return value info.  If any of
272800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // them are direct or extend without a specified coerce type, specify the
273800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // default now.
274800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  ABIArgInfo &RetInfo = FI->getReturnInfo();
275800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
276800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
2779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
278800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
279800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner       I != E; ++I)
280800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
281800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      I->info.setCoerceToType(ConvertTypeRecursive(I->type));
282541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
283a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner  // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
284a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner  // types, resolve them now.  These pointers may point to this function, which
285a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner  // we *just* filled in the FunctionInfo for.
286ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  if (!IsRecursive && !PointersToResolve.empty())
287a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner    HandleLateResolvedPointers();
2889cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
28988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  return *FI;
2900dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
29117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
292bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel DunbarCGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
293a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman                               bool _NoReturn, bool _HasRegParm, unsigned _RegParm,
294ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                               CanQualType ResTy,
295bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner                               const CanQualType *ArgTys,
296bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner                               unsigned NumArgTys)
297ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  : CallingConvention(_CallingConvention),
29804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    EffectiveCallingConvention(_CallingConvention),
299a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    NoReturn(_NoReturn), HasRegParm(_HasRegParm), RegParm(_RegParm)
300bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar{
301bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner  NumArgs = NumArgTys;
3029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
303ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner  // FIXME: Coallocate with the CGFunctionInfo object.
304bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner  Args = new ArgInfo[1 + NumArgTys];
30588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  Args[0].type = ResTy;
306bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner  for (unsigned i = 0; i != NumArgTys; ++i)
30788c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    Args[1 + i].type = ArgTys[i];
30888c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar}
30988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
31088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar/***/
31188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
31242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCallvoid CodeGenTypes::GetExpandedTypes(QualType type,
31342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall                     llvm::SmallVectorImpl<const llvm::Type*> &expandedTypes,
31442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall                                    bool isRecursive) {
31542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  const RecordType *RT = type->getAsStructureType();
3165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
3175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!RD->hasFlexibleArrayMember() &&
3195627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         "Cannot expand structure with flexible array.");
3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
32217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
3235627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    const FieldDecl *FD = *i;
3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(!FD->isBitField() &&
3255627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar           "Cannot expand structure with bit-field members.");
3261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    QualType fieldType = FD->getType();
32842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    if (fieldType->isRecordType())
32942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      GetExpandedTypes(fieldType, expandedTypes, isRecursive);
330deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner    else
33142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      expandedTypes.push_back(ConvertType(fieldType, isRecursive));
3325627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
3335627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
3345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function::arg_iterator
3365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
3375627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    llvm::Function::arg_iterator AI) {
3385627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
3395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
3405627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3415627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(LV.isSimple() &&
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         "Unexpected non-simple lvalue during struct expansion.");
3445627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = LV.getAddress();
34517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
34617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldDecl *FD = *i;
3485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
3495627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3505627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
351e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson    LValue LV = EmitLValueForField(Addr, FD, 0);
3525627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
3535627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      AI = ExpandTypeFromArgs(FT, LV, AI);
3545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
3555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
3565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ++AI;
3575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
3585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
3595627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  return AI;
3615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
3625627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
3655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
3665627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
3675627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
3685627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3695627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
3705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
3715627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = RV.getAggregateAddr();
37217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
37317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
3741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldDecl *FD = *i;
3755627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
378e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson    LValue LV = EmitLValueForField(Addr, FD, 0);
3795627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
3805627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
3815627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
3825627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      RValue RV = EmitLoadOfLValue(LV, FT);
3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(RV.isScalar() &&
3845627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar             "Unexpected non-scalar rvalue during struct expansion.");
3855627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      Args.push_back(RV.getScalarVal());
3865627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
3875627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
3885627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
3895627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
390e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
39108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// accessing some number of bytes out of it, try to gep into the struct to get
39208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// at its inner goodness.  Dive as deep as possible without entering an element
39308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// with an in-memory size smaller than DstSize.
39408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattnerstatic llvm::Value *
395e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris LattnerEnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
396e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner                                   const llvm::StructType *SrcSTy,
397e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner                                   uint64_t DstSize, CodeGenFunction &CGF) {
39808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // We can't dive into a zero-element struct.
39908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (SrcSTy->getNumElements() == 0) return SrcPtr;
4009cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
40108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  const llvm::Type *FirstElt = SrcSTy->getElementType(0);
4029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
40308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first elt is at least as large as what we're looking for, or if the
40408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // first element is the same size as the whole struct, we can enter it.
4059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  uint64_t FirstEltSize =
40608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
4079cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  if (FirstEltSize < DstSize &&
40808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner      FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
40908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    return SrcPtr;
4109cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
41108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // GEP into the first element.
41208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
4139cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
41408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first element is a struct, recurse.
41508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  const llvm::Type *SrcTy =
41608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
41708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
418e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
41908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
42008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  return SrcPtr;
42108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner}
42208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
4236d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
4246d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// are either integers or pointers.  This does a truncation of the value if it
4256d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// is too large or a zero extension if it is too small.
4266d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattnerstatic llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
4276d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner                                             const llvm::Type *Ty,
4286d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner                                             CodeGenFunction &CGF) {
4296d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() == Ty)
4306d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return Val;
4319cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4326d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Val->getType())) {
4336d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // If this is Pointer->Pointer avoid conversion to and from int.
4346d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    if (isa<llvm::PointerType>(Ty))
4356d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
4369cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4376d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // Convert the pointer to an integer so we can play with its width.
43877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
4396d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
4409cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4416d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  const llvm::Type *DestIntTy = Ty;
4426d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(DestIntTy))
44377b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    DestIntTy = CGF.IntPtrTy;
4449cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4456d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() != DestIntTy)
4466d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
4479cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4486d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Ty))
4496d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
4506d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  return Val;
4516d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner}
4526d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner
45308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
45408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
455275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
456275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// a pointer to an object of type \arg Ty.
457275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
458275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is smaller than the
459275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; in this situation the values of bits which not
460275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// present in the src are undefined.
461275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
462275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      const llvm::Type *Ty,
463275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      CodeGenFunction &CGF) {
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *SrcTy =
465275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
4669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4676ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  // If SrcTy and Ty are the same, just do a load.
4686ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == Ty)
4696ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return CGF.Builder.CreateLoad(SrcPtr);
4709cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4719408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
4729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
47308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
474e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
47508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
47608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  }
4779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
47808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
479275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
4806d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
4816d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
4826d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
4836d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
4846d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
4856d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
4866d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
4879cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
488b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  // If load is legal, just bitcast the src pointer.
4897ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar  if (SrcSize >= DstSize) {
490f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // Generally SrcSize is never greater than DstSize, since this means we are
491f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // losing bits. However, this can happen in cases where the structure has
492f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // additional padding, for example due to a user specified alignment.
4937ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar    //
494f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: Assert that we aren't truncating non-padding bits when have access
495f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // to that information.
496275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
497275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
498386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
499386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned load.
500386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    Load->setAlignment(1);
501386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    return Load;
502275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
5039cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
50435b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // Otherwise do coercion through memory. This is stupid, but
50535b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // simple.
50635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
50735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::Value *Casted =
50835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
50935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::StoreInst *Store =
51035b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
51135b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // FIXME: Use better alignment / avoid requiring aligned store.
51235b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  Store->setAlignment(1);
51335b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  return CGF.Builder.CreateLoad(Tmp);
514275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
515275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
516badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// Function to store a first-class aggregate into memory.  We prefer to
517badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// store the elements rather than the aggregate to be more friendly to
518badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// fast-isel.
519badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// FIXME: Do we need to recurse here?
520badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedmanstatic void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
521badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                          llvm::Value *DestPtr, bool DestIsVolatile,
522badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                          bool LowAlignment) {
523badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  // Prefer scalar stores to first-class aggregate stores.
524badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  if (const llvm::StructType *STy =
525badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        dyn_cast<llvm::StructType>(Val->getType())) {
526badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
527badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
528badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
529badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
530badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                                                    DestIsVolatile);
531badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      if (LowAlignment)
532badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        SI->setAlignment(1);
533badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    }
534badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  } else {
535badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
536badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  }
537badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman}
538badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman
539275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
540275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// where the source and destination may have different types.
541275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
542275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is larger than the
543275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; the upper bits of the src will be lost.
544275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic void CreateCoercedStore(llvm::Value *Src,
545275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               llvm::Value *DstPtr,
546d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                               bool DstIsVolatile,
547275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               CodeGenFunction &CGF) {
548275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  const llvm::Type *SrcTy = Src->getType();
5491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *DstTy =
550275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
5516ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == DstTy) {
5526ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
5536ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return;
5546ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  }
5559cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5566ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
5579cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
558e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner  if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
559e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
560e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
561e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner  }
5629cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5636d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
5646d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
5656d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
5666d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
5676d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
5686d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
5696d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return;
5706d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
5719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5729408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
573275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
57488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // If store is legal, just bitcast the src pointer.
575fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar  if (SrcSize <= DstSize) {
576275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
577275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
578386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned store.
579badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
580275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  } else {
581275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // Otherwise do coercion through memory. This is stupid, but
582275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // simple.
583fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar
584fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // Generally SrcSize is never greater than DstSize, since this means we are
585fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // losing bits. However, this can happen in cases where the structure has
586fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // additional padding, for example due to a user specified alignment.
587fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    //
588fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // FIXME: Assert that we aren't truncating non-padding bits when have access
589fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // to that information.
590275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
591275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(Src, Tmp);
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *Casted =
593275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
594386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
595386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned load.
596386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    Load->setAlignment(1);
597d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
598275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
599275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
600275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
6015627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/
6025627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
603dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
60411e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  return FI.getReturnInfo().isIndirect();
605bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar}
606bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar
607dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
608dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
609dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    switch (BT->getKind()) {
610dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    default:
611dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return false;
612dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Float:
613dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
614dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Double:
615dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
616dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::LongDouble:
617dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return getContext().Target.useObjCFPRetForRealType(
618dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar        TargetInfo::LongDouble);
619dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    }
620dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  }
621dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
622dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  return false;
623dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar}
624dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
625c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCallconst llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
626c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  const CGFunctionInfo &FI = getFunctionInfo(GD);
6279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
628c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  // For definition purposes, don't consider a K&R function variadic.
629c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  bool Variadic = false;
630c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  if (const FunctionProtoType *FPT =
631c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall        cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
632c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall    Variadic = FPT->isVariadic();
633c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall
634bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner  return GetFunctionType(FI, Variadic, false);
635c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall}
636c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall
63745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbarconst llvm::FunctionType *
63842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCallCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic,
63942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall                              bool isRecursive) {
64042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  llvm::SmallVector<const llvm::Type*, 8> argTypes;
64142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  const llvm::Type *resultType = 0;
64245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
64342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  const ABIArgInfo &retAI = FI.getReturnInfo();
64442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  switch (retAI.getKind()) {
6458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
64642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    llvm_unreachable("Invalid ABI kind for return argument");
6478951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
648cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
64946327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
65042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = retAI.getCoerceToType();
65146327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    break;
65246327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar
65311e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  case ABIArgInfo::Indirect: {
65442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
65542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = llvm::Type::getVoidTy(getLLVMContext());
65642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall
65742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    QualType ret = FI.getReturnType();
65842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    const llvm::Type *ty = ConvertType(ret, isRecursive);
65942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    unsigned addressSpace = Context.getTargetAddressSpace(ret);
66042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
66145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
66245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
66345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
66411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
66542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = llvm::Type::getVoidTy(getLLVMContext());
66611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    break;
66745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
6681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
67088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar         ie = FI.arg_end(); it != ie; ++it) {
67142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    const ABIArgInfo &argAI = it->info;
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    switch (argAI.getKind()) {
67411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
67511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
67611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
677800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect: {
678800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // indirect arguments are always on the stack, which is addr space #0.
67942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      const llvm::Type *LTy = ConvertTypeForMem(it->type, isRecursive);
68042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      argTypes.push_back(LTy->getPointerTo());
681800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
682800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
683800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
684800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
6851ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    case ABIArgInfo::Direct: {
686ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, flatten it.  Either
687ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // way is semantically identical, but fast-isel and the optimizer
688ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // generally likes scalar values better than FCAs.
68942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      const llvm::Type *argType = argAI.getCoerceToType();
69042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      if (const llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
69142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall        for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
69242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall          argTypes.push_back(st->getElementType(i));
693ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
69442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall        argTypes.push_back(argType);
695ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
69689c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
6971ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    }
6981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6998951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
70042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      GetExpandedTypes(it->type, argTypes, isRecursive);
7018951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
7028951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
70345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
70445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
70542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  return llvm::FunctionType::get(resultType, argTypes, isVariadic);
7063913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar}
7073913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar
7084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallconst llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
7094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
710ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
7119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
7124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (!VerifyFuncTypeComplete(FPT)) {
7134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    const CGFunctionInfo *Info;
7144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    if (isa<CXXDestructorDecl>(MD))
7154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
7164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    else
7174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      Info = &getFunctionInfo(MD);
7184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return GetFunctionType(*Info, FPT->isVariadic(), false);
7194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
720ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson
721ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson  return llvm::OpaqueType::get(getLLVMContext());
722ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson}
723ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson
724a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbarvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
72588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                           const Decl *TargetDecl,
7269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer                                           AttributeListType &PAL,
727ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar                                           unsigned &CallingConv) {
7285323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned FuncAttrs = 0;
729a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  unsigned RetAttrs = 0;
7305323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
731ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CallingConv = FI.getEffectiveCallingConvention();
732ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar
73304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (FI.isNoReturn())
73404a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    FuncAttrs |= llvm::Attribute::NoReturn;
73504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
7361102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov  // FIXME: handle sseregparm someday...
7375323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  if (TargetDecl) {
73840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<NoThrowAttr>())
739761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoUnwind;
7409c0c1f333ab8f5a3da055b99ee94778689face17John McCall    else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
7419c0c1f333ab8f5a3da055b99ee94778689face17John McCall      const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
7428026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl      if (FPT && FPT->isNothrow(getContext()))
7439c0c1f333ab8f5a3da055b99ee94778689face17John McCall        FuncAttrs |= llvm::Attribute::NoUnwind;
7449c0c1f333ab8f5a3da055b99ee94778689face17John McCall    }
7459c0c1f333ab8f5a3da055b99ee94778689face17John McCall
74640b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<NoReturnAttr>())
747761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoReturn;
74840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<ConstAttr>())
749232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      FuncAttrs |= llvm::Attribute::ReadNone;
75040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    else if (TargetDecl->hasAttr<PureAttr>())
75164c2e0762628eba26c100642521b6100c2515cc5Daniel Dunbar      FuncAttrs |= llvm::Attribute::ReadOnly;
75276168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn    if (TargetDecl->hasAttr<MallocAttr>())
75376168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn      RetAttrs |= llvm::Attribute::NoAlias;
7545323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
7555323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
7562811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.OptimizeSize)
7577ab1c3ebd02c31bfa1333cc51de1261c1499d6f7Daniel Dunbar    FuncAttrs |= llvm::Attribute::OptimizeForSize;
7582811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.DisableRedZone)
75924095dad88dd9d48aa16afa6416417073af251b5Devang Patel    FuncAttrs |= llvm::Attribute::NoRedZone;
7602811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.NoImplicitFloat)
761acebb397fa5d63835a0de9cee144987057ec1333Devang Patel    FuncAttrs |= llvm::Attribute::NoImplicitFloat;
76224095dad88dd9d48aa16afa6416417073af251b5Devang Patel
763a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  QualType RetTy = FI.getReturnType();
7645323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned Index = 1;
765b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = FI.getReturnInfo();
76645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  switch (RetAI.getKind()) {
767cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
7682eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   if (RetTy->hasSignedIntegerRepresentation())
769cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov     RetAttrs |= llvm::Attribute::SExt;
7702eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   else if (RetTy->hasUnsignedIntegerRepresentation())
771cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov     RetAttrs |= llvm::Attribute::ZExt;
772800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    break;
77346327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
774800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
7752c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
7762c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
77711e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  case ABIArgInfo::Indirect:
7781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PAL.push_back(llvm::AttributeWithIndex::get(Index,
779fb97cf24158aa7f1fd74374052f99733ef331bb9Chris Lattner                                                llvm::Attribute::StructRet));
7805323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    ++Index;
7810ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar    // sret disables readnone and readonly
7820ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar    FuncAttrs &= ~(llvm::Attribute::ReadOnly |
7830ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar                   llvm::Attribute::ReadNone);
7842c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
7852c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
7868951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(0 && "Invalid ABI kind for return argument");
7885323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
7892c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
790a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (RetAttrs)
791a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
7921102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
79317d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar  // FIXME: RegParm should be reduced in case of global register variable.
794a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  signed RegParm;
795a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (FI.getHasRegParm())
796a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    RegParm = FI.getRegParm();
797a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  else
79817d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar    RegParm = CodeGenOpts.NumRegisterParameters;
7991102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
8001102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov  unsigned PointerWidth = getContext().Target.getPointerWidth(0);
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
80288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar         ie = FI.arg_end(); it != ie; ++it) {
80388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    QualType ParamType = it->type;
80488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const ABIArgInfo &AI = it->info;
805761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    unsigned Attributes = 0;
8061102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
807d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
808d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // have the corresponding parameter variable.  It doesn't make
8097f6890ebb874cc16320259daef50f1b4cfdc47d5Daniel Dunbar    // sense to do it here because parameters are so messed up.
8108951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
811cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
812575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      if (ParamType->isSignedIntegerOrEnumerationType())
8132eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner        Attributes |= llvm::Attribute::SExt;
814575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      else if (ParamType->isUnsignedIntegerOrEnumerationType())
8152eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner        Attributes |= llvm::Attribute::ZExt;
816800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // FALL THROUGH
81746327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct:
8181102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      if (RegParm > 0 &&
8191102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov          (ParamType->isIntegerType() || ParamType->isPointerType())) {
8201102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov        RegParm -=
821800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
8221102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov        if (RegParm >= 0)
8231102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov          Attributes |= llvm::Attribute::InReg;
8241102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      }
8251102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      // FIXME: handle sseregparm someday...
8269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
827800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (const llvm::StructType *STy =
828800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner            dyn_cast<llvm::StructType>(AI.getCoerceToType()))
829800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        Index += STy->getNumElements()-1;  // 1 will be added below.
830800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
831800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
832800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect:
833800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (AI.getIndirectByVal())
834800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        Attributes |= llvm::Attribute::ByVal;
835800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
836800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      Attributes |=
837800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
838800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // byval disables readnone and readonly.
839800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      FuncAttrs &= ~(llvm::Attribute::ReadOnly |
840800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner                     llvm::Attribute::ReadNone);
8418951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
8421102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
84311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
84411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      // Skip increment, no matching LLVM parameter.
8451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      continue;
84611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
8475627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
84842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      llvm::SmallVector<const llvm::Type*, 8> types;
849f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // FIXME: This is rather inefficient. Do we ever actually need to do
850f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // anything here? The result should be just reconstructed on the other
851f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // side, so extension should be a non-issue.
85242e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      getTypes().GetExpandedTypes(ParamType, types, false);
85342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      Index += types.size();
8545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
8555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
8565323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    }
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
858761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    if (Attributes)
859761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
8605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++Index;
8615323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
862a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (FuncAttrs)
863a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
8645323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
8655323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
866d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// An argument came in as a promoted argument; demote it back to its
867d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// declared type.
868d26bc76c98006609002d9930f8840490e88ac5b5John McCallstatic llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
869d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         const VarDecl *var,
870d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         llvm::Value *value) {
871d26bc76c98006609002d9930f8840490e88ac5b5John McCall  const llvm::Type *varType = CGF.ConvertType(var->getType());
872d26bc76c98006609002d9930f8840490e88ac5b5John McCall
873d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // This can happen with promotions that actually don't change the
874d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // underlying type, like the enum promotions.
875d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (value->getType() == varType) return value;
876d26bc76c98006609002d9930f8840490e88ac5b5John McCall
877d26bc76c98006609002d9930f8840490e88ac5b5John McCall  assert((varType->isIntegerTy() || varType->isFloatingPointTy())
878d26bc76c98006609002d9930f8840490e88ac5b5John McCall         && "unexpected promotion type");
879d26bc76c98006609002d9930f8840490e88ac5b5John McCall
880d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (isa<llvm::IntegerType>(varType))
881d26bc76c98006609002d9930f8840490e88ac5b5John McCall    return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
882d26bc76c98006609002d9930f8840490e88ac5b5John McCall
883d26bc76c98006609002d9930f8840490e88ac5b5John McCall  return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
884d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
885d26bc76c98006609002d9930f8840490e88ac5b5John McCall
88688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
88788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                         llvm::Function *Fn,
88817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         const FunctionArgList &Args) {
8890cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // If this is an implicit-return-zero function, go ahead and
8900cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // initialize the return value.  TODO: it might be nice to have
8910cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // a more general mechanism for this that didn't require synthesized
8920cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // return statements.
893121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
8940cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    if (FD->hasImplicitReturnZero()) {
8950cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      QualType RetTy = FD->getResultType().getUnqualifiedType();
8960cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
897c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson      llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
8980cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      Builder.CreateStore(Zero, ReturnValue);
8990cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    }
9000cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  }
9010cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall
902f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from FunctionArgList; lift up and
903f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // simplify.
9045251afa21d3583f2740fd4f83659d008625a7260Daniel Dunbar
90517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
90617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::Function::arg_iterator AI = Fn->arg_begin();
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Name the struct return argument.
909dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(FI)) {
91017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    AI->setName("agg.result");
91117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    ++AI;
91217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
9131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9144b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(FI.arg_size() == Args.size() &&
9154b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
916093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  unsigned ArgNo = 1;
917b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
918093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
919093ac461b37a573dcf226fa55faed96f318169b9Devang Patel       i != e; ++i, ++info_it, ++ArgNo) {
920d26bc76c98006609002d9930f8840490e88ac5b5John McCall    const VarDecl *Arg = *i;
921b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    QualType Ty = info_it->type;
922b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgI = info_it->info;
9238951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
924d26bc76c98006609002d9930f8840490e88ac5b5John McCall    bool isPromoted =
925d26bc76c98006609002d9930f8840490e88ac5b5John McCall      isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
926d26bc76c98006609002d9930f8840490e88ac5b5John McCall
9278951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (ArgI.getKind()) {
9281f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    case ABIArgInfo::Indirect: {
929ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      llvm::Value *V = AI;
930cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
9311f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      if (hasAggregateLLVMType(Ty)) {
932cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // Aggregates and complex variables are accessed by reference.  All we
933cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // need to do is realign the value, if requested
934cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        if (ArgI.getIndirectRealign()) {
935cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
936cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
937cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // Copy from the incoming argument pointer to the temporary with the
938cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // appropriate alignment.
939cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          //
940cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // FIXME: We should have a common utility for generating an aggregate
941cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // copy.
9429f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer          const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
943fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck          CharUnits Size = getContext().getTypeSizeInChars(Ty);
944c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
945c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
946c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          Builder.CreateMemCpy(Dst,
947c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi                               Src,
948fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                               llvm::ConstantInt::get(IntPtrTy,
949fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                                                      Size.getQuantity()),
9509f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               ArgI.getIndirectAlign(),
9519f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               false);
952cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          V = AlignedTemp;
953cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        }
9541f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
9551f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Load scalar value from indirect argument.
956fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
957fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
958d26bc76c98006609002d9930f8840490e88ac5b5John McCall
959d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
960d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
9611f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
962093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
9631f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
9641f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    }
965cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
966cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
96746327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct: {
968800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If we have the trivial case, handle it with no muss and fuss.
969800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
970117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getCoerceToType() == ConvertType(Ty) &&
971117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getDirectOffset() == 0) {
972800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
973800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *V = AI;
9749cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
975d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall        if (Arg->getType().isRestrictQualified())
976d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall          AI->addAttr(llvm::Attribute::NoAlias);
977d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall
978d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
979d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
980d26bc76c98006609002d9930f8840490e88ac5b5John McCall
981093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, V, ArgNo);
982800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
98317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar      }
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
985121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner      llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
9869cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
987deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // The alignment we need to use is the max of the requested alignment for
988deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // the argument plus the alignment required by our access code below.
9899cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      unsigned AlignmentToUse =
990d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
991deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      AlignmentToUse = std::max(AlignmentToUse,
992deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner                        (unsigned)getContext().getDeclAlign(Arg).getQuantity());
9939cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
994deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      Alloca->setAlignment(AlignmentToUse);
995121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner      llvm::Value *V = Alloca;
996117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *Ptr = V;    // Pointer to store into.
9979cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
998117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
999117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgI.getDirectOffset()) {
1000117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1001117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
10029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        Ptr = Builder.CreateBitCast(Ptr,
1003117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                          llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1004117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
10059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1006ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
1007ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
1008ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
1009ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      if (const llvm::StructType *STy =
1010ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner            dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
10119282688a296b306c4ae2d115f55101647056d1daChris Lattner        Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
10129cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
10139282688a296b306c4ae2d115f55101647056d1daChris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
10149282688a296b306c4ae2d115f55101647056d1daChris Lattner          assert(AI != Fn->arg_end() && "Argument mismatch!");
10159282688a296b306c4ae2d115f55101647056d1daChris Lattner          AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
10169282688a296b306c4ae2d115f55101647056d1daChris Lattner          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
10179282688a296b306c4ae2d115f55101647056d1daChris Lattner          Builder.CreateStore(AI++, EltPtr);
1018ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        }
1019ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
1020309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // Simple case, just do a coerced store of the argument into the alloca.
1021ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
1022225e286110bcc8b7b1ff8b35f0d51a10a158b18cChris Lattner        AI->setName(Arg->getName() + ".coerce");
1023117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
1024ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
10259cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
10269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
102789c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // Match to what EmitParmDecl is expecting for this type.
10288b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar      if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
102991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar        V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
1030d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1031d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
10328b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar      }
1033093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
1034ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      continue;  // Skip ++AI increment, already done.
103589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
1036800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1037800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Expand: {
1038800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If this structure was expanded into multiple arguments then
1039800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // we need to create a temporary and reconstruct it from the
1040800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // arguments.
1041800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
1042800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::Function::arg_iterator End =
104379c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar        ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
1044093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, Temp, ArgNo);
1045800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1046800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Name the arguments used in expansion and increment AI.
1047800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      unsigned Index = 0;
1048800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      for (; AI != End; ++AI, ++Index)
1049800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1050800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
1051800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
1052800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1053800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Ignore:
1054800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Initialize the local variable appropriately.
1055800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (hasAggregateLLVMType(Ty))
1056093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1057800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      else
1058093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1059093ac461b37a573dcf226fa55faed96f318169b9Devang Patel                     ArgNo);
1060800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1061800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Skip increment, no matching LLVM parameter.
1062800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
10638951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
10645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
10655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++AI;
106617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
106717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  assert(AI == Fn->arg_end() && "Argument mismatch!");
106817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
106917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
107035b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattnervoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
10712c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  // Functions with no result always return void.
1072c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  if (ReturnValue == 0) {
1073c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    Builder.CreateRetVoid();
1074c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    return;
1075c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  }
107621fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar
10774751a53c5e5fed4bf2271e29cae7411c93a77df7Dan Gohman  llvm::DebugLoc RetDbgLoc;
1078c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  llvm::Value *RV = 0;
1079c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  QualType RetTy = FI.getReturnType();
1080c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  const ABIArgInfo &RetAI = FI.getReturnInfo();
1081cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
1082c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  switch (RetAI.getKind()) {
108391a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  case ABIArgInfo::Indirect: {
108491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1085c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    if (RetTy->isAnyComplexType()) {
1086c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1087c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1088c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1089c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      // Do nothing; aggregrates get evaluated directly into the destination.
1090c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    } else {
1091c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
109291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar                        false, Alignment, RetTy);
1093c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    }
1094c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
109591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  }
10968951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1097c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Extend:
1098800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct:
1099117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1100117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        RetAI.getDirectOffset() == 0) {
1101800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // The internal return value temp always will have pointer-to-return-type
1102800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // type, just do a load.
11039cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1104800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If the instruction right before the insertion point is a store to the
1105800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // return value, we can elide the load, zap the store, and usually zap the
1106800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // alloca.
1107800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1108800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::StoreInst *SI = 0;
11099cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      if (InsertBB->empty() ||
1110800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1111800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1112800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RV = Builder.CreateLoad(ReturnValue);
1113800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      } else {
1114800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // Get the stored value and nuke the now-dead store.
1115800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RetDbgLoc = SI->getDebugLoc();
1116800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RV = SI->getValueOperand();
1117800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        SI->eraseFromParent();
11189cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1119800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // If that was the only use of the return value, nuke it as well now.
1120800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1121800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1122800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          ReturnValue = 0;
1123800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
112435b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner      }
1125800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    } else {
1126117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *V = ReturnValue;
1127117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1128117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = RetAI.getDirectOffset()) {
1129117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1130117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateConstGEP1_32(V, Offs);
11319cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        V = Builder.CreateBitCast(V,
1132117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1133117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
11349cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1135117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
113635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    }
1137c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
11381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1139800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
1140c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
11418951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1142c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Expand:
1143c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    assert(0 && "Invalid ABI kind for return argument");
114417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114621fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar  llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1147d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel  if (!RetDbgLoc.isUnknown())
1148d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel    Ret->setDebugLoc(RetDbgLoc);
114917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
115017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1151413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1152413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                          const VarDecl *param) {
11532736071ea3a46f90e65c93418961611d96c10ab9John McCall  // StartFunction converted the ABI-lowered parameter(s) into a
11542736071ea3a46f90e65c93418961611d96c10ab9John McCall  // local alloca.  We need to turn that into an r-value suitable
11552736071ea3a46f90e65c93418961611d96c10ab9John McCall  // for EmitCall.
1156413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  llvm::Value *local = GetAddrOfLocalVar(param);
11572736071ea3a46f90e65c93418961611d96c10ab9John McCall
1158413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  QualType type = param->getType();
11599cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
11602736071ea3a46f90e65c93418961611d96c10ab9John McCall  // For the most part, we just need to load the alloca, except:
11612736071ea3a46f90e65c93418961611d96c10ab9John McCall  // 1) aggregate r-values are actually pointers to temporaries, and
11622736071ea3a46f90e65c93418961611d96c10ab9John McCall  // 2) references to aggregates are pointers directly to the aggregate.
11632736071ea3a46f90e65c93418961611d96c10ab9John McCall  // I don't know why references to non-aggregates are different here.
1164413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1165413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    if (hasAggregateLLVMType(ref->getPointeeType()))
1166413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall      return args.add(RValue::getAggregate(local), type);
11672736071ea3a46f90e65c93418961611d96c10ab9John McCall
11682736071ea3a46f90e65c93418961611d96c10ab9John McCall    // Locals which are references to scalars are represented
11692736071ea3a46f90e65c93418961611d96c10ab9John McCall    // with allocas holding the pointer.
1170413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::get(Builder.CreateLoad(local)), type);
11712736071ea3a46f90e65c93418961611d96c10ab9John McCall  }
11722736071ea3a46f90e65c93418961611d96c10ab9John McCall
1173413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (type->isAnyComplexType()) {
1174413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1175413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::getComplex(complex), type);
1176413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  }
11772736071ea3a46f90e65c93418961611d96c10ab9John McCall
1178413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (hasAggregateLLVMType(type))
1179413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::getAggregate(local), type);
11802736071ea3a46f90e65c93418961611d96c10ab9John McCall
1181413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1182413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1183413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  return args.add(RValue::get(value), type);
11842736071ea3a46f90e65c93418961611d96c10ab9John McCall}
11852736071ea3a46f90e65c93418961611d96c10ab9John McCall
1186413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1187413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                  QualType type) {
1188413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (type->isReferenceType())
1189413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1190413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                    type);
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
119255d484802f3e27930317739efc5f5956b78aac25Eli Friedman  if (hasAggregateLLVMType(type) && isa<ImplicitCastExpr>(E) &&
119355d484802f3e27930317739efc5f5956b78aac25Eli Friedman      cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
119455d484802f3e27930317739efc5f5956b78aac25Eli Friedman    LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
119555d484802f3e27930317739efc5f5956b78aac25Eli Friedman    assert(L.isSimple());
119655d484802f3e27930317739efc5f5956b78aac25Eli Friedman    args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()),
119755d484802f3e27930317739efc5f5956b78aac25Eli Friedman             type, /*NeedsCopy*/true);
119855d484802f3e27930317739efc5f5956b78aac25Eli Friedman    return;
119955d484802f3e27930317739efc5f5956b78aac25Eli Friedman  }
120055d484802f3e27930317739efc5f5956b78aac25Eli Friedman
1201413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  args.add(EmitAnyExprToTemp(E), type);
12020139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson}
12030139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson
1204f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Emits a call or invoke instruction to the given function, depending
1205f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// on the current state of the EH stack.
1206f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::CallSite
1207f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1208f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgBegin,
1209f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgEnd,
1210f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  const llvm::Twine &Name) {
1211f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = getInvokeDest();
1212f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest)
1213f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1214f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1215f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1216f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1217f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                                  ArgBegin, ArgEnd, Name);
1218f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBlock(ContBB);
1219f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return Invoke;
1220f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
1221f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
122288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel DunbarRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 llvm::Value *Callee,
1224f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                                 ReturnValueSlot ReturnValue,
1225c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar                                 const CallArgList &CallArgs,
1226dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall                                 const Decl *TargetDecl,
12274b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall                                 llvm::Instruction **callOrInvoke) {
1228f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
122917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::SmallVector<llvm::Value*, 16> Args;
123017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
123117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Handle struct-return functions by passing a pointer to the
123217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // location that we would like to return into.
1233bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  QualType RetTy = CallInfo.getReturnType();
1234b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12375db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the call returns a temporary with struct return, create a temporary
1238d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  // alloca to hold the result, unless one is given to us.
1239dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(CallInfo)) {
1240d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *Value = ReturnValue.getValue();
1241d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!Value)
1242195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      Value = CreateMemTemp(RetTy);
1243d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    Args.push_back(Value);
1244d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  }
12451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12464b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(CallInfo.arg_size() == CallArgs.size() &&
12474b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
1248b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
12491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1250b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar       I != E; ++I, ++info_it) {
1251b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgInfo = info_it->info;
1252c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman    RValue RV = I->RV;
12535627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
125491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment =
1255c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman      getContext().getTypeAlignInChars(I->Ty).getQuantity();
12565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    switch (ArgInfo.getKind()) {
125791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    case ABIArgInfo::Indirect: {
12581f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      if (RV.isScalar() || RV.isComplex()) {
12591f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Make a temporary alloca to pass the argument.
1260c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman        Args.push_back(CreateMemTemp(I->Ty));
12611f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        if (RV.isScalar())
126291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar          EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1263c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman                            Alignment, I->Ty);
12641f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        else
12651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
126655d484802f3e27930317739efc5f5956b78aac25Eli Friedman      } else if (I->NeedsCopy && !ArgInfo.getIndirectByVal()) {
126755d484802f3e27930317739efc5f5956b78aac25Eli Friedman        Args.push_back(CreateMemTemp(I->Ty));
126855d484802f3e27930317739efc5f5956b78aac25Eli Friedman        EmitAggregateCopy(Args.back(), RV.getAggregateAddr(), I->Ty,
126955d484802f3e27930317739efc5f5956b78aac25Eli Friedman                          RV.isVolatileQualified());
12701f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
12711f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        Args.push_back(RV.getAggregateAddr());
12721f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
12731f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
127491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    }
12751f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar
127611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
127711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
12789cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1279800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
1280800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Direct: {
1281800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
1282117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1283117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getDirectOffset() == 0) {
1284800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (RV.isScalar())
1285800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          Args.push_back(RV.getScalarVal());
1286800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        else
1287800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1288800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
1289800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
129011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
129189c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // FIXME: Avoid the conversion through memory if possible.
129289c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      llvm::Value *SrcPtr;
129389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      if (RV.isScalar()) {
1294c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman        SrcPtr = CreateMemTemp(I->Ty, "coerce");
1295c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman        EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment, I->Ty);
129689c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      } else if (RV.isComplex()) {
1297c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman        SrcPtr = CreateMemTemp(I->Ty, "coerce");
129889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar        StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
130089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar        SrcPtr = RV.getAggregateAddr();
13019cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1302117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1303117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgInfo.getDirectOffset()) {
1304117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1305117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
13069cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        SrcPtr = Builder.CreateBitCast(SrcPtr,
1307117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                       llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1308117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
1309117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
13109cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1311ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
1312ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
1313ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
1314ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      if (const llvm::StructType *STy =
1315309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner            dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
13169282688a296b306c4ae2d115f55101647056d1daChris Lattner        SrcPtr = Builder.CreateBitCast(SrcPtr,
13179282688a296b306c4ae2d115f55101647056d1daChris Lattner                                       llvm::PointerType::getUnqual(STy));
13189282688a296b306c4ae2d115f55101647056d1daChris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
13199282688a296b306c4ae2d115f55101647056d1daChris Lattner          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
1320deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1321deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          // We don't know what we're loading from.
1322deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          LI->setAlignment(1);
1323deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          Args.push_back(LI);
1324309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        }
1325ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
1326309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // In the simple case, just pass the coerced loaded value.
1327309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1328309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner                                         *this));
1329ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
13309cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
133189c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
133289c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
133389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar
13345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand:
1335c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman      ExpandTypeToArgs(I->Ty, RV, Args);
13365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
133717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
133817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
13391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13405db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the callee is a bitcast of a function to a varargs pointer to function
13415db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // type, check to see if we can remove the bitcast.  This handles some cases
13425db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // with unprototyped functions.
13435db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
13445db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
13455db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
13465db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      const llvm::FunctionType *CurFT =
13475db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        cast<llvm::FunctionType>(CurPT->getElementType());
13485db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
13491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13505db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      if (CE->getOpcode() == llvm::Instruction::BitCast &&
13515db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          ActualFT->getReturnType() == CurFT->getReturnType() &&
1352d6bebbfd510f3b495795b88aafd10ead3cb211e9Chris Lattner          ActualFT->getNumParams() == CurFT->getNumParams() &&
1353c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          ActualFT->getNumParams() == Args.size() &&
1354c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          (CurFT->isVarArg() || !ActualFT->isVarArg())) {
13555db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        bool ArgsMatch = true;
13565db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
13575db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
13585db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            ArgsMatch = false;
13595db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            break;
13605db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          }
13611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13625db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // Strip the cast if we can get away with it.  This is a nice cleanup,
13635db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // but also allows us to inline the function at -O0 if it is marked
13645db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // always_inline.
13655db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        if (ArgsMatch)
13665db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          Callee = CalleeF;
13675db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      }
13685db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    }
13691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
137017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1371ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  unsigned CallingConv;
1372761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CodeGen::AttributeListType AttributeList;
1373ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
13749834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
13759834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar                                                   AttributeList.end());
13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1377f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = 0;
1378f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1379f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    InvokeDest = getInvokeDest();
1380f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1381d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::CallSite CS;
1382f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest) {
1383beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
13849834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  } else {
13859834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1387beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                              Args.data(), Args.data()+Args.size());
13889834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    EmitBlock(Cont);
1389f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar  }
1390ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner  if (callOrInvoke)
13914b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall    *callOrInvoke = CS.getInstruction();
1392f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar
1393d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  CS.setAttributes(Attrs);
1394ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1395d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
1396d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // If the call doesn't return, finish the basic block and clear the
1397d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // insertion point; this allows the rest of IRgen to discard
1398d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // unreachable code.
1399d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  if (CS.doesNotReturn()) {
1400d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.CreateUnreachable();
1401d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.ClearInsertionPoint();
14021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1403f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: For now, emit a dummy basic block because expr emitters in
1404f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // generally are not ready to handle emitting expressions at unreachable
1405f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // points.
1406d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    EnsureInsertPoint();
14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1408d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    // Return a reasonable RValue.
1409d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    return GetUndefRValue(RetTy);
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1411d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
1412d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::Instruction *CI = CS.getInstruction();
1413ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer  if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
141417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setName("call");
14152c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
14162c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
141791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  case ABIArgInfo::Indirect: {
141891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
14192c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    if (RetTy->isAnyComplexType())
14205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
14213403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
14225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getAggregate(Args[0]);
142391a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
142491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  }
14258951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
142611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
14270bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // If we are ignoring an argument that had a result, make sure to
14280bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // construct the appropriate return value for our caller.
142913e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar    return GetUndefRValue(RetTy);
14309cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1431800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Extend:
1432800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct: {
1433117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1434117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        RetAI.getDirectOffset() == 0) {
1435800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (RetTy->isAnyComplexType()) {
1436800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1437800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1438800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getComplex(std::make_pair(Real, Imag));
1439800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
1440800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1441800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *DestPtr = ReturnValue.getValue();
1442800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        bool DestIsVolatile = ReturnValue.isVolatile();
144311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
1444800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (!DestPtr) {
1445800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1446800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestIsVolatile = false;
1447800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
1448badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
1449800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getAggregate(DestPtr);
1450800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
1451800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return RValue::get(CI);
1452800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
14539cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1454d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *DestPtr = ReturnValue.getValue();
1455d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    bool DestIsVolatile = ReturnValue.isVolatile();
14569cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1457d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!DestPtr) {
1458195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      DestPtr = CreateMemTemp(RetTy, "coerce");
1459d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      DestIsVolatile = false;
1460d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    }
14619cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1462117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the value is offset in memory, apply the offset now.
1463117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    llvm::Value *StorePtr = DestPtr;
1464117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (unsigned Offs = RetAI.getDirectOffset()) {
1465117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1466117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
14679cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      StorePtr = Builder.CreateBitCast(StorePtr,
1468117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1469117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    }
1470117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
14719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
147291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1473ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson    if (RetTy->isAnyComplexType())
1474d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
14753403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1476d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      return RValue::getAggregate(DestPtr);
147791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
1478639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  }
14798951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
14808951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
14811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(0 && "Invalid ABI kind for return argument");
148217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
14832c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
14842c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  assert(0 && "Unhandled ABIArgInfo::Kind");
14852c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  return RValue::get(0);
148617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
1487b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
1488b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar/* VarArg handling */
1489b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
1490b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbarllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1491b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1492b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar}
1493