CGCall.cpp revision a49218e17bcbb1acde0245773173e2c0c42f4f19
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;
3952fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  // TODO: add support for CC_X86Pascal to llvm
4004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
4204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
430b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Derives the 'this' type for codegen purposes, i.e. ignoring method
440b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualification.
450b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// FIXME: address space qualification?
46ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
47ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
48ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
490b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
500b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
510b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the canonical formal type of the given C++ method.
52ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
53ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return MD->getType()->getCanonicalTypeUnqualified()
54ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall           .getAs<FunctionProtoType>();
550b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
560b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
570b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the "extra-canonicalized" return type, which discards
580b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualifiers on the return type.  Codegen doesn't care about them,
590b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// and it makes ABI code a little easier to be able to assume that
600b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// all parameter and return types are top-level unqualified.
61ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetReturnType(QualType RetTy) {
62ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
630b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
640b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
650b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
66bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris LattnerCodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
67bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                              bool IsRecursive) {
68ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
69ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                         llvm::SmallVector<CanQualType, 16>(),
70bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                         FTNP->getExtInfo(), IsRecursive);
7145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
7245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
730b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// \param Args - contains any initial parameters besides those
740b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall///   in the formal type
750b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallstatic const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
76ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                                  llvm::SmallVectorImpl<CanQualType> &ArgTys,
77bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                             CanQual<FunctionProtoType> FTP,
78bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                             bool IsRecursive = false) {
79541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
8045c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
81541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar    ArgTys.push_back(FTP->getArgType(i));
82ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
839c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo(), IsRecursive);
840b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
850b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
860b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
87bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris LattnerCodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
88bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                              bool IsRecursive) {
89ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
909c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
91bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar}
92bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
9304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic CallingConv getCallingConventionForDecl(const Decl *D) {
94bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  // Set the appropriate calling convention for the Function.
95bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<StdCallAttr>())
9604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86StdCall;
97bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
98bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<FastCallAttr>())
9904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86FastCall;
100bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
101f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  if (D->hasAttr<ThisCallAttr>())
102f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor    return CC_X86ThisCall;
103f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor
10452fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  if (D->hasAttr<PascalAttr>())
10552fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik    return CC_X86Pascal;
10652fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik
10704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  return CC_C;
10845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
10945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
110375c31c4673f83f925de221752cf801c2fbbb246Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
111375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson                                                 const FunctionProtoType *FTP) {
112ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
1130b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
114375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson  // Add the 'this' pointer.
1150b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  ArgTys.push_back(GetThisType(Context, RD));
1160b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1170b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  return ::getFunctionInfo(*this, ArgTys,
1189c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller              FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
119375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson}
120375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson
121f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
122ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
1230b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
124fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
125fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
126fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall
1273eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  // Add the 'this' pointer unless this is a static method.
1283eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  if (MD->isInstance())
1290b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall    ArgTys.push_back(GetThisType(Context, MD->getParent()));
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1319c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
132f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson}
133f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson
1349cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencerconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
135f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                                    CXXCtorType Type) {
136ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
1370b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  ArgTys.push_back(GetThisType(Context, D->getParent()));
1384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQualType ResTy = Context.VoidTy;
139f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
1404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
1410b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
1434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Add the formal parameters.
1454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
1464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    ArgTys.push_back(FTP->getArgType(i));
1474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1489c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
149f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
150f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
151f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
152f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson                                                    CXXDtorType Type) {
1534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  llvm::SmallVector<CanQualType, 2> ArgTys;
154ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  ArgTys.push_back(GetThisType(Context, D->getParent()));
1554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQualType ResTy = Context.VoidTy;
1564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
1580b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
1604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
1614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
1629c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller  return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
163f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
164f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
165541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
1663eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
167f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson    if (MD->isInstance())
168f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson      return getFunctionInfo(MD);
1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
170ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
171ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionType>(FTy));
1720b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  if (isa<FunctionNoProtoType>(FTy))
1739cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer    return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
174ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionProtoType>(FTy));
175ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return getFunctionInfo(FTy.getAs<FunctionProtoType>());
1760dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
1770dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
178541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbarconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
179ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
180ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
181ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
182541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy?
1832073216a1075767b5d25c23d1462b7034686d94dChris Lattner  for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
1840b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall         e = MD->param_end(); i != e; ++i) {
1850b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall    ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
1860b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  }
1870b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  return getFunctionInfo(GetReturnType(MD->getResultType()),
1880b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall                         ArgTys,
189264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                         FunctionType::ExtInfo(
190264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                             /*NoReturn*/ false,
191a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman                             /*HasRegParm*/ false,
192425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola                             /*RegParm*/ 0,
193264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                             getCallingConventionForDecl(MD)));
1940dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
1950dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
196b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlssonconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
197b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  // FIXME: Do we need to handle ObjCMethodDecl?
198b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
1999cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
200b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
201b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson    return getFunctionInfo(CD, GD.getCtorType());
202b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
203b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
204b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson    return getFunctionInfo(DD, GD.getDtorType());
2059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
206b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  return getFunctionInfo(FD);
207b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson}
208b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
210bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                                                    const CallArgList &Args,
211264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                            const FunctionType::ExtInfo &Info) {
212541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
213ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
2141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
215725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar       i != e; ++i)
2160b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall    ArgTys.push_back(Context.getCanonicalParamType(i->second));
217264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
2180dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
2190dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
2201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
221bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                                                    const FunctionArgList &Args,
222264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                                            const FunctionType::ExtInfo &Info) {
223541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
224ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  llvm::SmallVector<CanQualType, 16> ArgTys;
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
226bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar       i != e; ++i)
227d26bc76c98006609002d9930f8840490e88ac5b5John McCall    ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
228264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
229541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar}
230541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
231d26bc76c98006609002d9930f8840490e88ac5b5John McCallconst CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
232d26bc76c98006609002d9930f8840490e88ac5b5John McCall  llvm::SmallVector<CanQualType, 1> args;
233d26bc76c98006609002d9930f8840490e88ac5b5John McCall  return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
234d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
235d26bc76c98006609002d9930f8840490e88ac5b5John McCall
236ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallconst CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
237ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                           const llvm::SmallVectorImpl<CanQualType> &ArgTys,
238bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                            const FunctionType::ExtInfo &Info,
239bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                                    bool IsRecursive) {
240ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#ifndef NDEBUG
241ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  for (llvm::SmallVectorImpl<CanQualType>::const_iterator
242ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall         I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
243ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall    assert(I->isCanonicalAsParam());
244ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#endif
245ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall
246425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola  unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
24704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
24840a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  // Lookup or create unique function info.
24940a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  llvm::FoldingSetNodeID ID;
250264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  CGFunctionInfo::Profile(ID, Info, ResTy,
251bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar                          ArgTys.begin(), ArgTys.end());
25240a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
25340a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  void *InsertPos = 0;
25440a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
25540a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  if (FI)
25640a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    return *FI;
25740a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
25888c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // Construct the function info.
259a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getHasRegParm(), Info.getRegParm(), ResTy,
2609c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                          ArgTys.data(), ArgTys.size());
26135e67d4387bbe3e7e17ee6b17eaa42eebb0eb9f1Daniel Dunbar  FunctionInfos.InsertNode(FI, InsertPos);
262541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
26388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // Compute ABI information.
264ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  getABIInfo().computeInfo(*FI);
2659cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
266800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // Loop over all of the computed argument and return value info.  If any of
267800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // them are direct or extend without a specified coerce type, specify the
268800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // default now.
269800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  ABIArgInfo &RetInfo = FI->getReturnInfo();
270800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
271800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    RetInfo.setCoerceToType(ConvertTypeRecursive(FI->getReturnType()));
2729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
273800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
274800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner       I != E; ++I)
275800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
276800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      I->info.setCoerceToType(ConvertTypeRecursive(I->type));
277541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
278a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner  // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
279a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner  // types, resolve them now.  These pointers may point to this function, which
280a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner  // we *just* filled in the FunctionInfo for.
281ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  if (!IsRecursive && !PointersToResolve.empty())
282a9fa85841102e81daaa23650d89b120fe9dacedcChris Lattner    HandleLateResolvedPointers();
2839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
28488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  return *FI;
2850dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
28617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
287bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel DunbarCGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
288a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman                               bool _NoReturn, bool _HasRegParm, unsigned _RegParm,
289ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall                               CanQualType ResTy,
290bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner                               const CanQualType *ArgTys,
291bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner                               unsigned NumArgTys)
292ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  : CallingConvention(_CallingConvention),
29304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    EffectiveCallingConvention(_CallingConvention),
294a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    NoReturn(_NoReturn), HasRegParm(_HasRegParm), RegParm(_RegParm)
295bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar{
296bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner  NumArgs = NumArgTys;
2979cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
298ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner  // FIXME: Coallocate with the CGFunctionInfo object.
299bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner  Args = new ArgInfo[1 + NumArgTys];
30088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  Args[0].type = ResTy;
301bb52114f81e8829fe29a9a0faa49e8b2157206ccChris Lattner  for (unsigned i = 0; i != NumArgTys; ++i)
30288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    Args[1 + i].type = ArgTys[i];
30388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar}
30488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
30588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar/***/
30688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid CodeGenTypes::GetExpandedTypes(QualType Ty,
308bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                    std::vector<const llvm::Type*> &ArgTys,
309bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                                    bool IsRecursive) {
3105627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
3115627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
3125627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
3131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(!RD->hasFlexibleArrayMember() &&
3145627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar         "Cannot expand structure with flexible array.");
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
31717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
3185627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    const FieldDecl *FD = *i;
3191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(!FD->isBitField() &&
3205627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar           "Cannot expand structure with bit-field members.");
3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3225627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
323deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner    if (CodeGenFunction::hasAggregateLLVMType(FT))
324bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      GetExpandedTypes(FT, ArgTys, IsRecursive);
325deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner    else
326bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      ArgTys.push_back(ConvertType(FT, IsRecursive));
3275627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
3285627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
3295627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function::arg_iterator
3315627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
3325627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    llvm::Function::arg_iterator AI) {
3335627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
3345627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
3355627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(LV.isSimple() &&
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         "Unexpected non-simple lvalue during struct expansion.");
3395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = LV.getAddress();
34017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
34117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
3421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldDecl *FD = *i;
3435627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
3445627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3455627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
346e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson    LValue LV = EmitLValueForField(Addr, FD, 0);
3475627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
3485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      AI = ExpandTypeFromArgs(FT, LV, AI);
3495627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
3505627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
3515627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ++AI;
3525627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
3535627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
3545627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3555627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  return AI;
3565627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
3575627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
3591eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpCodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
3605627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
3615627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  const RecordType *RT = Ty->getAsStructureType();
3625627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RT && "Can only expand structure types.");
3635627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
3645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  RecordDecl *RD = RT->getDecl();
3655627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
3665627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  llvm::Value *Addr = RV.getAggregateAddr();
36717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
36817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldDecl *FD = *i;
3705627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    QualType FT = FD->getType();
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3725627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    // FIXME: What are the right qualifiers here?
373e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson    LValue LV = EmitLValueForField(Addr, FD, 0);
3745627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
3755627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
3765627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    } else {
3775627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      RValue RV = EmitLoadOfLValue(LV, FT);
3781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(RV.isScalar() &&
3795627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar             "Unexpected non-scalar rvalue during struct expansion.");
3805627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      Args.push_back(RV.getScalarVal());
3815627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
3825627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
3835627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
3845627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
385e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
38608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// accessing some number of bytes out of it, try to gep into the struct to get
38708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// at its inner goodness.  Dive as deep as possible without entering an element
38808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// with an in-memory size smaller than DstSize.
38908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattnerstatic llvm::Value *
390e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris LattnerEnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
391e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner                                   const llvm::StructType *SrcSTy,
392e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner                                   uint64_t DstSize, CodeGenFunction &CGF) {
39308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // We can't dive into a zero-element struct.
39408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (SrcSTy->getNumElements() == 0) return SrcPtr;
3959cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
39608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  const llvm::Type *FirstElt = SrcSTy->getElementType(0);
3979cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
39808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first elt is at least as large as what we're looking for, or if the
39908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // first element is the same size as the whole struct, we can enter it.
4009cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  uint64_t FirstEltSize =
40108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
4029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  if (FirstEltSize < DstSize &&
40308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner      FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
40408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    return SrcPtr;
4059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
40608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // GEP into the first element.
40708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
4089cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
40908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first element is a struct, recurse.
41008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  const llvm::Type *SrcTy =
41108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
41208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
413e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
41408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
41508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  return SrcPtr;
41608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner}
41708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
4186d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
4196d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// are either integers or pointers.  This does a truncation of the value if it
4206d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// is too large or a zero extension if it is too small.
4216d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattnerstatic llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
4226d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner                                             const llvm::Type *Ty,
4236d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner                                             CodeGenFunction &CGF) {
4246d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() == Ty)
4256d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return Val;
4269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4276d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Val->getType())) {
4286d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // If this is Pointer->Pointer avoid conversion to and from int.
4296d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    if (isa<llvm::PointerType>(Ty))
4306d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
4319cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4326d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // Convert the pointer to an integer so we can play with its width.
43377b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
4346d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
4359cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4366d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  const llvm::Type *DestIntTy = Ty;
4376d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(DestIntTy))
43877b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    DestIntTy = CGF.IntPtrTy;
4399cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4406d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() != DestIntTy)
4416d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
4429cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4436d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Ty))
4446d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
4456d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  return Val;
4466d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner}
4476d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner
44808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
44908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
450275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
451275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// a pointer to an object of type \arg Ty.
452275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
453275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is smaller than the
454275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; in this situation the values of bits which not
455275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// present in the src are undefined.
456275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
457275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      const llvm::Type *Ty,
458275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      CodeGenFunction &CGF) {
4591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *SrcTy =
460275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
4619cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4626ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  // If SrcTy and Ty are the same, just do a load.
4636ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == Ty)
4646ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return CGF.Builder.CreateLoad(SrcPtr);
4659cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
4669408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
4679cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
46808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
469e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
47008dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
47108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  }
4729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
47308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
474275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
4756d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
4766d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
4776d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
4786d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
4796d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
4806d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
4816d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
4829cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
483b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  // If load is legal, just bitcast the src pointer.
4847ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar  if (SrcSize >= DstSize) {
485f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // Generally SrcSize is never greater than DstSize, since this means we are
486f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // losing bits. However, this can happen in cases where the structure has
487f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // additional padding, for example due to a user specified alignment.
4887ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar    //
489f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: Assert that we aren't truncating non-padding bits when have access
490f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // to that information.
491275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
492275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
493386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
494386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned load.
495386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    Load->setAlignment(1);
496386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    return Load;
497275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
4989cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
49935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // Otherwise do coercion through memory. This is stupid, but
50035b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // simple.
50135b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
50235b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::Value *Casted =
50335b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
50435b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::StoreInst *Store =
50535b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
50635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // FIXME: Use better alignment / avoid requiring aligned store.
50735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  Store->setAlignment(1);
50835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  return CGF.Builder.CreateLoad(Tmp);
509275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
510275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
511275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
512275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// where the source and destination may have different types.
513275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
514275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is larger than the
515275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; the upper bits of the src will be lost.
516275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic void CreateCoercedStore(llvm::Value *Src,
517275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               llvm::Value *DstPtr,
518d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                               bool DstIsVolatile,
519275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               CodeGenFunction &CGF) {
520275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  const llvm::Type *SrcTy = Src->getType();
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const llvm::Type *DstTy =
522275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
5236ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == DstTy) {
5246ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
5256ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return;
5266ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  }
5279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5286ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
5299cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
530e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner  if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
531e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
532e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
533e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner  }
5349cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5356d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
5366d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
5376d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
5386d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
5396d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
5406d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
5416d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return;
5426d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
5439cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
5449408c45009b417e758749b3d95cdfb87dcb68ea9Duncan Sands  uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
545275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
54688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // If store is legal, just bitcast the src pointer.
547fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar  if (SrcSize <= DstSize) {
548275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
549275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
550386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned store.
551d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
552275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  } else {
553275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // Otherwise do coercion through memory. This is stupid, but
554275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // simple.
555fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar
556fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // Generally SrcSize is never greater than DstSize, since this means we are
557fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // losing bits. However, this can happen in cases where the structure has
558fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // additional padding, for example due to a user specified alignment.
559fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    //
560fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // FIXME: Assert that we aren't truncating non-padding bits when have access
561fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // to that information.
562275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
563275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(Src, Tmp);
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    llvm::Value *Casted =
565275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
566386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
567386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned load.
568386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    Load->setAlignment(1);
569d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
570275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
571275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
572275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
5735627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/
5745627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
575dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
57611e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  return FI.getReturnInfo().isIndirect();
577bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar}
578bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar
579dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
580dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
581dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    switch (BT->getKind()) {
582dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    default:
583dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return false;
584dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Float:
585dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
586dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Double:
587dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
588dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::LongDouble:
589dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return getContext().Target.useObjCFPRetForRealType(
590dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar        TargetInfo::LongDouble);
591dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    }
592dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  }
593dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
594dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  return false;
595dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar}
596dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
597c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCallconst llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
598c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  const CGFunctionInfo &FI = getFunctionInfo(GD);
5999cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
600c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  // For definition purposes, don't consider a K&R function variadic.
601c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  bool Variadic = false;
602c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  if (const FunctionProtoType *FPT =
603c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall        cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
604c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall    Variadic = FPT->isVariadic();
605c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall
606bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner  return GetFunctionType(FI, Variadic, false);
607c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall}
608c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall
60945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbarconst llvm::FunctionType *
610bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris LattnerCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
611bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner                              bool IsRecursive) {
61245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  std::vector<const llvm::Type*> ArgTys;
61345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
61445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  const llvm::Type *ResultType = 0;
61545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
616a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  QualType RetTy = FI.getReturnType();
617b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = FI.getReturnInfo();
6188951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  switch (RetAI.getKind()) {
6198951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
6208951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    assert(0 && "Invalid ABI kind for return argument");
6218951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
622cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
62346327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
624800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    ResultType = RetAI.getCoerceToType();
62546327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    break;
62646327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar
62711e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  case ABIArgInfo::Indirect: {
62811e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar    assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
6290032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    ResultType = llvm::Type::getVoidTy(getLLVMContext());
630bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner    const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
631207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = Context.getTargetAddressSpace(RetTy);
632207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    ArgTys.push_back(llvm::PointerType::get(STy, AS));
63345c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
63445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
63545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
63611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
6370032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    ResultType = llvm::Type::getVoidTy(getLLVMContext());
63811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    break;
63945c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
64288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar         ie = FI.arg_end(); it != ie; ++it) {
64388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const ABIArgInfo &AI = it->info;
6441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
64611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
64711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
64811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
649800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect: {
650800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // indirect arguments are always on the stack, which is addr space #0.
651800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
652800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
653800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
654800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
655800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
656800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
6571ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    case ABIArgInfo::Direct: {
658ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, flatten it.  Either
659ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // way is semantically identical, but fast-isel and the optimizer
660ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // generally likes scalar values better than FCAs.
661ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      const llvm::Type *ArgTy = AI.getCoerceToType();
662ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
663ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
664ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner          ArgTys.push_back(STy->getElementType(i));
665ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
666ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        ArgTys.push_back(ArgTy);
667ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
66889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
6691ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    }
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6718951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
672bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      GetExpandedTypes(it->type, ArgTys, IsRecursive);
6738951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
6748951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
67545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
67645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
677bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
6783913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar}
6793913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar
6804c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallconst llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
6814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
682ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
6839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  if (!VerifyFuncTypeComplete(FPT)) {
6854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    const CGFunctionInfo *Info;
6864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    if (isa<CXXDestructorDecl>(MD))
6874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
6884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    else
6894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall      Info = &getFunctionInfo(MD);
6904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return GetFunctionType(*Info, FPT->isVariadic(), false);
6914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
692ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson
693ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson  return llvm::OpaqueType::get(getLLVMContext());
694ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson}
695ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson
696a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbarvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
69788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                           const Decl *TargetDecl,
6989cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer                                           AttributeListType &PAL,
699ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar                                           unsigned &CallingConv) {
7005323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned FuncAttrs = 0;
701a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  unsigned RetAttrs = 0;
7025323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
703ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CallingConv = FI.getEffectiveCallingConvention();
704ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar
70504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (FI.isNoReturn())
70604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    FuncAttrs |= llvm::Attribute::NoReturn;
70704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
7081102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov  // FIXME: handle sseregparm someday...
7095323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  if (TargetDecl) {
71040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<NoThrowAttr>())
711761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoUnwind;
7129c0c1f333ab8f5a3da055b99ee94778689face17John McCall    else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
7139c0c1f333ab8f5a3da055b99ee94778689face17John McCall      const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
7148026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl      if (FPT && FPT->isNothrow(getContext()))
7159c0c1f333ab8f5a3da055b99ee94778689face17John McCall        FuncAttrs |= llvm::Attribute::NoUnwind;
7169c0c1f333ab8f5a3da055b99ee94778689face17John McCall    }
7179c0c1f333ab8f5a3da055b99ee94778689face17John McCall
71840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<NoReturnAttr>())
719761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      FuncAttrs |= llvm::Attribute::NoReturn;
72040b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<ConstAttr>())
721232eb7d33b96ad8f99de3b5ae840421b3a7c6cb7Anders Carlsson      FuncAttrs |= llvm::Attribute::ReadNone;
72240b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    else if (TargetDecl->hasAttr<PureAttr>())
72364c2e0762628eba26c100642521b6100c2515cc5Daniel Dunbar      FuncAttrs |= llvm::Attribute::ReadOnly;
72476168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn    if (TargetDecl->hasAttr<MallocAttr>())
72576168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn      RetAttrs |= llvm::Attribute::NoAlias;
7265323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
7275323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
7282811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.OptimizeSize)
7297ab1c3ebd02c31bfa1333cc51de1261c1499d6f7Daniel Dunbar    FuncAttrs |= llvm::Attribute::OptimizeForSize;
7302811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.DisableRedZone)
73124095dad88dd9d48aa16afa6416417073af251b5Devang Patel    FuncAttrs |= llvm::Attribute::NoRedZone;
7322811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.NoImplicitFloat)
733acebb397fa5d63835a0de9cee144987057ec1333Devang Patel    FuncAttrs |= llvm::Attribute::NoImplicitFloat;
73424095dad88dd9d48aa16afa6416417073af251b5Devang Patel
735a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  QualType RetTy = FI.getReturnType();
7365323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned Index = 1;
737b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = FI.getReturnInfo();
73845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  switch (RetAI.getKind()) {
739cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
7402eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   if (RetTy->hasSignedIntegerRepresentation())
741cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov     RetAttrs |= llvm::Attribute::SExt;
7422eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   else if (RetTy->hasUnsignedIntegerRepresentation())
743cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov     RetAttrs |= llvm::Attribute::ZExt;
744800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    break;
74546327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
746800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
7472c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
7482c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
74911e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  case ABIArgInfo::Indirect:
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    PAL.push_back(llvm::AttributeWithIndex::get(Index,
751fb97cf24158aa7f1fd74374052f99733ef331bb9Chris Lattner                                                llvm::Attribute::StructRet));
7525323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    ++Index;
7530ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar    // sret disables readnone and readonly
7540ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar    FuncAttrs &= ~(llvm::Attribute::ReadOnly |
7550ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar                   llvm::Attribute::ReadNone);
7562c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
7572c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
7588951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(0 && "Invalid ABI kind for return argument");
7605323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
7612c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
762a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (RetAttrs)
763a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
7641102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
76517d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar  // FIXME: RegParm should be reduced in case of global register variable.
766a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  signed RegParm;
767a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (FI.getHasRegParm())
768a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    RegParm = FI.getRegParm();
769a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  else
77017d3fea677753e6e3e82ffe2cbdeccbf5f2e7497Daniel Dunbar    RegParm = CodeGenOpts.NumRegisterParameters;
7711102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
7721102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov  unsigned PointerWidth = getContext().Target.getPointerWidth(0);
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
77488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar         ie = FI.arg_end(); it != ie; ++it) {
77588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    QualType ParamType = it->type;
77688c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const ABIArgInfo &AI = it->info;
777761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    unsigned Attributes = 0;
7781102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
779d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
780d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // have the corresponding parameter variable.  It doesn't make
7817f6890ebb874cc16320259daef50f1b4cfdc47d5Daniel Dunbar    // sense to do it here because parameters are so messed up.
7828951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
783cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
7842eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner      if (ParamType->isSignedIntegerType())
7852eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner        Attributes |= llvm::Attribute::SExt;
7862eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner      else if (ParamType->isUnsignedIntegerType())
7872eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner        Attributes |= llvm::Attribute::ZExt;
788800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // FALL THROUGH
78946327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct:
7901102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      if (RegParm > 0 &&
7911102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov          (ParamType->isIntegerType() || ParamType->isPointerType())) {
7921102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov        RegParm -=
793800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
7941102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov        if (RegParm >= 0)
7951102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov          Attributes |= llvm::Attribute::InReg;
7961102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      }
7971102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      // FIXME: handle sseregparm someday...
7989cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
799800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (const llvm::StructType *STy =
800800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner            dyn_cast<llvm::StructType>(AI.getCoerceToType()))
801800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        Index += STy->getNumElements()-1;  // 1 will be added below.
802800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
803800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
804800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect:
805800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (AI.getIndirectByVal())
806800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        Attributes |= llvm::Attribute::ByVal;
807800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
808800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      Attributes |=
809800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
810800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // byval disables readnone and readonly.
811800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      FuncAttrs &= ~(llvm::Attribute::ReadOnly |
812800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner                     llvm::Attribute::ReadNone);
8138951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
8141102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
81511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
81611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      // Skip increment, no matching LLVM parameter.
8171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      continue;
81811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
8195627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      std::vector<const llvm::Type*> Tys;
821f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // FIXME: This is rather inefficient. Do we ever actually need to do
822f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // anything here? The result should be just reconstructed on the other
823f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // side, so extension should be a non-issue.
824bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattner      getTypes().GetExpandedTypes(ParamType, Tys, false);
8255627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      Index += Tys.size();
8265627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
8275627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
8285323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    }
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
830761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel    if (Attributes)
831761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
8325627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++Index;
8335323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
834a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel  if (FuncAttrs)
835a2c6912c416c2d9f79d18f3a88ab0ae2609286c3Devang Patel    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
8365323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
8375323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
838d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// An argument came in as a promoted argument; demote it back to its
839d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// declared type.
840d26bc76c98006609002d9930f8840490e88ac5b5John McCallstatic llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
841d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         const VarDecl *var,
842d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         llvm::Value *value) {
843d26bc76c98006609002d9930f8840490e88ac5b5John McCall  const llvm::Type *varType = CGF.ConvertType(var->getType());
844d26bc76c98006609002d9930f8840490e88ac5b5John McCall
845d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // This can happen with promotions that actually don't change the
846d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // underlying type, like the enum promotions.
847d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (value->getType() == varType) return value;
848d26bc76c98006609002d9930f8840490e88ac5b5John McCall
849d26bc76c98006609002d9930f8840490e88ac5b5John McCall  assert((varType->isIntegerTy() || varType->isFloatingPointTy())
850d26bc76c98006609002d9930f8840490e88ac5b5John McCall         && "unexpected promotion type");
851d26bc76c98006609002d9930f8840490e88ac5b5John McCall
852d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (isa<llvm::IntegerType>(varType))
853d26bc76c98006609002d9930f8840490e88ac5b5John McCall    return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
854d26bc76c98006609002d9930f8840490e88ac5b5John McCall
855d26bc76c98006609002d9930f8840490e88ac5b5John McCall  return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
856d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
857d26bc76c98006609002d9930f8840490e88ac5b5John McCall
85888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
85988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                         llvm::Function *Fn,
86017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         const FunctionArgList &Args) {
8610cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // If this is an implicit-return-zero function, go ahead and
8620cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // initialize the return value.  TODO: it might be nice to have
8630cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // a more general mechanism for this that didn't require synthesized
8640cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // return statements.
865121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
8660cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    if (FD->hasImplicitReturnZero()) {
8670cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      QualType RetTy = FD->getResultType().getUnqualifiedType();
8680cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
869c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson      llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
8700cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      Builder.CreateStore(Zero, ReturnValue);
8710cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    }
8720cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  }
8730cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall
874f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from FunctionArgList; lift up and
875f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // simplify.
8765251afa21d3583f2740fd4f83659d008625a7260Daniel Dunbar
87717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
87817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::Function::arg_iterator AI = Fn->arg_begin();
8791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Name the struct return argument.
881dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(FI)) {
88217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    AI->setName("agg.result");
88317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    ++AI;
88417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
8851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8864b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(FI.arg_size() == Args.size() &&
8874b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
888093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  unsigned ArgNo = 1;
889b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
890093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
891093ac461b37a573dcf226fa55faed96f318169b9Devang Patel       i != e; ++i, ++info_it, ++ArgNo) {
892d26bc76c98006609002d9930f8840490e88ac5b5John McCall    const VarDecl *Arg = *i;
893b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    QualType Ty = info_it->type;
894b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgI = info_it->info;
8958951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
896d26bc76c98006609002d9930f8840490e88ac5b5John McCall    bool isPromoted =
897d26bc76c98006609002d9930f8840490e88ac5b5John McCall      isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
898d26bc76c98006609002d9930f8840490e88ac5b5John McCall
8998951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (ArgI.getKind()) {
9001f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    case ABIArgInfo::Indirect: {
901ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      llvm::Value *V = AI;
902cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
9031f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      if (hasAggregateLLVMType(Ty)) {
904cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // Aggregates and complex variables are accessed by reference.  All we
905cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // need to do is realign the value, if requested
906cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        if (ArgI.getIndirectRealign()) {
907cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
908cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
909cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // Copy from the incoming argument pointer to the temporary with the
910cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // appropriate alignment.
911cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          //
912cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // FIXME: We should have a common utility for generating an aggregate
913cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // copy.
9149f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer          const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
915fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck          CharUnits Size = getContext().getTypeSizeInChars(Ty);
916c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
917c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
918c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          Builder.CreateMemCpy(Dst,
919c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi                               Src,
920fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                               llvm::ConstantInt::get(IntPtrTy,
921fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                                                      Size.getQuantity()),
9229f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               ArgI.getIndirectAlign(),
9239f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               false);
924cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          V = AlignedTemp;
925cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        }
9261f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
9271f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Load scalar value from indirect argument.
928fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
929fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
930d26bc76c98006609002d9930f8840490e88ac5b5John McCall
931d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
932d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
9331f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
934093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
9351f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
9361f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    }
937cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
938cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
93946327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct: {
940800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If we have the trivial case, handle it with no muss and fuss.
941800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
942117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getCoerceToType() == ConvertType(Ty) &&
943117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getDirectOffset() == 0) {
944800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
945800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *V = AI;
9469cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
947d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall        if (Arg->getType().isRestrictQualified())
948d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall          AI->addAttr(llvm::Attribute::NoAlias);
949d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall
950d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
951d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
952d26bc76c98006609002d9930f8840490e88ac5b5John McCall
953093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, V, ArgNo);
954800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
95517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar      }
9561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
957121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner      llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
9589cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
959deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // The alignment we need to use is the max of the requested alignment for
960deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // the argument plus the alignment required by our access code below.
9619cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      unsigned AlignmentToUse =
962d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall        CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
963deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      AlignmentToUse = std::max(AlignmentToUse,
964deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner                        (unsigned)getContext().getDeclAlign(Arg).getQuantity());
9659cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
966deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      Alloca->setAlignment(AlignmentToUse);
967121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner      llvm::Value *V = Alloca;
968117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *Ptr = V;    // Pointer to store into.
9699cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
970117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
971117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgI.getDirectOffset()) {
972117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
973117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
9749cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        Ptr = Builder.CreateBitCast(Ptr,
975117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                          llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
976117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
9779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
978ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
979ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
980ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
981ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      if (const llvm::StructType *STy =
982ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner            dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
9839282688a296b306c4ae2d115f55101647056d1daChris Lattner        Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
9849cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
9859282688a296b306c4ae2d115f55101647056d1daChris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
9869282688a296b306c4ae2d115f55101647056d1daChris Lattner          assert(AI != Fn->arg_end() && "Argument mismatch!");
9879282688a296b306c4ae2d115f55101647056d1daChris Lattner          AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
9889282688a296b306c4ae2d115f55101647056d1daChris Lattner          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
9899282688a296b306c4ae2d115f55101647056d1daChris Lattner          Builder.CreateStore(AI++, EltPtr);
990ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        }
991ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
992309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // Simple case, just do a coerced store of the argument into the alloca.
993ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
994225e286110bcc8b7b1ff8b35f0d51a10a158b18cChris Lattner        AI->setName(Arg->getName() + ".coerce");
995117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
996ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
9979cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
9989cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
99989c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // Match to what EmitParmDecl is expecting for this type.
10008b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar      if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
100191a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar        V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
1002d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1003d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
10048b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar      }
1005093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
1006ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      continue;  // Skip ++AI increment, already done.
100789c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
1008800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1009800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Expand: {
1010800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If this structure was expanded into multiple arguments then
1011800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // we need to create a temporary and reconstruct it from the
1012800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // arguments.
1013800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
1014800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::Function::arg_iterator End =
101579c3928d816f317dd27109fb92e7d190c1c68329Daniel Dunbar        ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
1016093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, Temp, ArgNo);
1017800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1018800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Name the arguments used in expansion and increment AI.
1019800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      unsigned Index = 0;
1020800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      for (; AI != End; ++AI, ++Index)
1021800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1022800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
1023800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
1024800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1025800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Ignore:
1026800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Initialize the local variable appropriately.
1027800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (hasAggregateLLVMType(Ty))
1028093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1029800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      else
1030093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1031093ac461b37a573dcf226fa55faed96f318169b9Devang Patel                     ArgNo);
1032800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1033800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Skip increment, no matching LLVM parameter.
1034800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
10358951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
10365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
10375627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++AI;
103817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
103917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  assert(AI == Fn->arg_end() && "Argument mismatch!");
104017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
104117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
104235b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattnervoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
10432c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  // Functions with no result always return void.
1044c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  if (ReturnValue == 0) {
1045c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    Builder.CreateRetVoid();
1046c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    return;
1047c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  }
104821fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar
10494751a53c5e5fed4bf2271e29cae7411c93a77df7Dan Gohman  llvm::DebugLoc RetDbgLoc;
1050c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  llvm::Value *RV = 0;
1051c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  QualType RetTy = FI.getReturnType();
1052c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  const ABIArgInfo &RetAI = FI.getReturnInfo();
1053cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
1054c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  switch (RetAI.getKind()) {
105591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  case ABIArgInfo::Indirect: {
105691a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1057c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    if (RetTy->isAnyComplexType()) {
1058c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1059c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1060c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1061c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      // Do nothing; aggregrates get evaluated directly into the destination.
1062c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    } else {
1063c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
106491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar                        false, Alignment, RetTy);
1065c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    }
1066c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
106791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  }
10688951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1069c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Extend:
1070800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct:
1071117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1072117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        RetAI.getDirectOffset() == 0) {
1073800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // The internal return value temp always will have pointer-to-return-type
1074800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // type, just do a load.
10759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1076800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If the instruction right before the insertion point is a store to the
1077800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // return value, we can elide the load, zap the store, and usually zap the
1078800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // alloca.
1079800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1080800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      llvm::StoreInst *SI = 0;
10819cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      if (InsertBB->empty() ||
1082800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1083800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1084800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RV = Builder.CreateLoad(ReturnValue);
1085800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      } else {
1086800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // Get the stored value and nuke the now-dead store.
1087800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RetDbgLoc = SI->getDebugLoc();
1088800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RV = SI->getValueOperand();
1089800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        SI->eraseFromParent();
10909cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1091800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // If that was the only use of the return value, nuke it as well now.
1092800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1093800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1094800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          ReturnValue = 0;
1095800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
109635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner      }
1097800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    } else {
1098117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *V = ReturnValue;
1099117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1100117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = RetAI.getDirectOffset()) {
1101117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1102117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateConstGEP1_32(V, Offs);
11039cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        V = Builder.CreateBitCast(V,
1104117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1105117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
11069cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1107117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
110835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    }
1109c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
11101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1111800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
1112c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
11138951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1114c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Expand:
1115c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    assert(0 && "Invalid ABI kind for return argument");
111617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111821fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar  llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1119d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel  if (!RetDbgLoc.isUnknown())
1120d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel    Ret->setDebugLoc(RetDbgLoc);
112117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
112217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1123413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1124413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                          const VarDecl *param) {
11252736071ea3a46f90e65c93418961611d96c10ab9John McCall  // StartFunction converted the ABI-lowered parameter(s) into a
11262736071ea3a46f90e65c93418961611d96c10ab9John McCall  // local alloca.  We need to turn that into an r-value suitable
11272736071ea3a46f90e65c93418961611d96c10ab9John McCall  // for EmitCall.
1128413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  llvm::Value *local = GetAddrOfLocalVar(param);
11292736071ea3a46f90e65c93418961611d96c10ab9John McCall
1130413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  QualType type = param->getType();
11319cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
11322736071ea3a46f90e65c93418961611d96c10ab9John McCall  // For the most part, we just need to load the alloca, except:
11332736071ea3a46f90e65c93418961611d96c10ab9John McCall  // 1) aggregate r-values are actually pointers to temporaries, and
11342736071ea3a46f90e65c93418961611d96c10ab9John McCall  // 2) references to aggregates are pointers directly to the aggregate.
11352736071ea3a46f90e65c93418961611d96c10ab9John McCall  // I don't know why references to non-aggregates are different here.
1136413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1137413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    if (hasAggregateLLVMType(ref->getPointeeType()))
1138413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall      return args.add(RValue::getAggregate(local), type);
11392736071ea3a46f90e65c93418961611d96c10ab9John McCall
11402736071ea3a46f90e65c93418961611d96c10ab9John McCall    // Locals which are references to scalars are represented
11412736071ea3a46f90e65c93418961611d96c10ab9John McCall    // with allocas holding the pointer.
1142413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::get(Builder.CreateLoad(local)), type);
11432736071ea3a46f90e65c93418961611d96c10ab9John McCall  }
11442736071ea3a46f90e65c93418961611d96c10ab9John McCall
1145413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (type->isAnyComplexType()) {
1146413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1147413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::getComplex(complex), type);
1148413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  }
11492736071ea3a46f90e65c93418961611d96c10ab9John McCall
1150413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (hasAggregateLLVMType(type))
1151413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::getAggregate(local), type);
11522736071ea3a46f90e65c93418961611d96c10ab9John McCall
1153413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1154413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1155413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  return args.add(RValue::get(value), type);
11562736071ea3a46f90e65c93418961611d96c10ab9John McCall}
11572736071ea3a46f90e65c93418961611d96c10ab9John McCall
1158413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1159413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                  QualType type) {
1160413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (type->isReferenceType())
1161413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1162413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                    type);
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1164413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  args.add(EmitAnyExprToTemp(E), type);
11650139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson}
11660139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson
1167f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Emits a call or invoke instruction to the given function, depending
1168f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// on the current state of the EH stack.
1169f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::CallSite
1170f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1171f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgBegin,
1172f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgEnd,
1173f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  const llvm::Twine &Name) {
1174f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = getInvokeDest();
1175f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest)
1176f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1177f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1178f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1179f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1180f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                                  ArgBegin, ArgEnd, Name);
1181f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EmitBlock(ContBB);
1182f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  return Invoke;
1183f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
1184f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
118588b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel DunbarRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 llvm::Value *Callee,
1187f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                                 ReturnValueSlot ReturnValue,
1188c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar                                 const CallArgList &CallArgs,
1189dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall                                 const Decl *TargetDecl,
11904b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall                                 llvm::Instruction **callOrInvoke) {
1191f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
119217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::SmallVector<llvm::Value*, 16> Args;
119317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
119417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Handle struct-return functions by passing a pointer to the
119517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // location that we would like to return into.
1196bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  QualType RetTy = CallInfo.getReturnType();
1197b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12005db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the call returns a temporary with struct return, create a temporary
1201d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  // alloca to hold the result, unless one is given to us.
1202dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(CallInfo)) {
1203d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *Value = ReturnValue.getValue();
1204d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!Value)
1205195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      Value = CreateMemTemp(RetTy);
1206d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    Args.push_back(Value);
1207d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  }
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12094b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(CallInfo.arg_size() == CallArgs.size() &&
12104b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
1211b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1213b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar       I != E; ++I, ++info_it) {
1214b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgInfo = info_it->info;
121517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    RValue RV = I->first;
12165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
121791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment =
121891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar      getContext().getTypeAlignInChars(I->second).getQuantity();
12195627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    switch (ArgInfo.getKind()) {
122091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    case ABIArgInfo::Indirect: {
12211f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      if (RV.isScalar() || RV.isComplex()) {
12221f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Make a temporary alloca to pass the argument.
1223195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar        Args.push_back(CreateMemTemp(I->second));
12241f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        if (RV.isScalar())
122591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar          EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
122691a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar                            Alignment, I->second);
12271f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        else
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
12291f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
12301f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        Args.push_back(RV.getAggregateAddr());
12311f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
12321f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
123391a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    }
12341f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar
123511434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
123611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
12379cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1238800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
1239800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Direct: {
1240800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
1241117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1242117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getDirectOffset() == 0) {
1243800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (RV.isScalar())
1244800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          Args.push_back(RV.getScalarVal());
1245800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        else
1246800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1247800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
1248800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
124911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
125089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // FIXME: Avoid the conversion through memory if possible.
125189c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      llvm::Value *SrcPtr;
125289c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      if (RV.isScalar()) {
1253195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar        SrcPtr = CreateMemTemp(I->second, "coerce");
125491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar        EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
125591a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar                          I->second);
125689c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      } else if (RV.isComplex()) {
1257195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar        SrcPtr = CreateMemTemp(I->second, "coerce");
125889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar        StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
12591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
126089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar        SrcPtr = RV.getAggregateAddr();
12619cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1262117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1263117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgInfo.getDirectOffset()) {
1264117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1265117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
12669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        SrcPtr = Builder.CreateBitCast(SrcPtr,
1267117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                       llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1268117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
1269117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
12709cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1271ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
1272ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
1273ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
1274ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      if (const llvm::StructType *STy =
1275309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner            dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
12769282688a296b306c4ae2d115f55101647056d1daChris Lattner        SrcPtr = Builder.CreateBitCast(SrcPtr,
12779282688a296b306c4ae2d115f55101647056d1daChris Lattner                                       llvm::PointerType::getUnqual(STy));
12789282688a296b306c4ae2d115f55101647056d1daChris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
12799282688a296b306c4ae2d115f55101647056d1daChris Lattner          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
1280deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1281deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          // We don't know what we're loading from.
1282deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          LI->setAlignment(1);
1283deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          Args.push_back(LI);
1284309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        }
1285ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
1286309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // In the simple case, just pass the coerced loaded value.
1287309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1288309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner                                         *this));
1289ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
12909cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
129189c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
129289c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
129389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar
12945627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand:
12955627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      ExpandTypeToArgs(I->second, RV, Args);
12965627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
129717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
129817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13005db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the callee is a bitcast of a function to a varargs pointer to function
13015db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // type, check to see if we can remove the bitcast.  This handles some cases
13025db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // with unprototyped functions.
13035db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
13045db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
13055db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
13065db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      const llvm::FunctionType *CurFT =
13075db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        cast<llvm::FunctionType>(CurPT->getElementType());
13085db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
13091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13105db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      if (CE->getOpcode() == llvm::Instruction::BitCast &&
13115db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          ActualFT->getReturnType() == CurFT->getReturnType() &&
1312d6bebbfd510f3b495795b88aafd10ead3cb211e9Chris Lattner          ActualFT->getNumParams() == CurFT->getNumParams() &&
1313c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          ActualFT->getNumParams() == Args.size() &&
1314c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          (CurFT->isVarArg() || !ActualFT->isVarArg())) {
13155db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        bool ArgsMatch = true;
13165db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
13175db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
13185db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            ArgsMatch = false;
13195db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            break;
13205db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          }
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13225db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // Strip the cast if we can get away with it.  This is a nice cleanup,
13235db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // but also allows us to inline the function at -O0 if it is marked
13245db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // always_inline.
13255db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        if (ArgsMatch)
13265db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          Callee = CalleeF;
13275db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      }
13285db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    }
13291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
133017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1331ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  unsigned CallingConv;
1332761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CodeGen::AttributeListType AttributeList;
1333ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
13349834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
13359834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar                                                   AttributeList.end());
13361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1337f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = 0;
1338f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1339f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    InvokeDest = getInvokeDest();
1340f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1341d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::CallSite CS;
1342f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest) {
1343beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
13449834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  } else {
13459834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1347beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                              Args.data(), Args.data()+Args.size());
13489834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    EmitBlock(Cont);
1349f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar  }
1350ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner  if (callOrInvoke)
13514b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall    *callOrInvoke = CS.getInstruction();
1352f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar
1353d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  CS.setAttributes(Attrs);
1354ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1355d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
1356d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // If the call doesn't return, finish the basic block and clear the
1357d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // insertion point; this allows the rest of IRgen to discard
1358d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // unreachable code.
1359d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  if (CS.doesNotReturn()) {
1360d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.CreateUnreachable();
1361d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.ClearInsertionPoint();
13621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1363f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: For now, emit a dummy basic block because expr emitters in
1364f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // generally are not ready to handle emitting expressions at unreachable
1365f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // points.
1366d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    EnsureInsertPoint();
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1368d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    // Return a reasonable RValue.
1369d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    return GetUndefRValue(RetTy);
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
1371d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
1372d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::Instruction *CI = CS.getInstruction();
1373ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer  if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
137417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setName("call");
13752c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
13762c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
137791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  case ABIArgInfo::Indirect: {
137891a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
13792c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    if (RetTy->isAnyComplexType())
13805627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
13813403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
13825627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      return RValue::getAggregate(Args[0]);
138391a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
138491a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  }
13858951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
138611434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
13870bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // If we are ignoring an argument that had a result, make sure to
13880bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // construct the appropriate return value for our caller.
138913e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar    return GetUndefRValue(RetTy);
13909cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1391800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Extend:
1392800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct: {
1393117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1394117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        RetAI.getDirectOffset() == 0) {
1395800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (RetTy->isAnyComplexType()) {
1396800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1397800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1398800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getComplex(std::make_pair(Real, Imag));
1399800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
1400800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1401800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *DestPtr = ReturnValue.getValue();
1402800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        bool DestIsVolatile = ReturnValue.isVolatile();
140311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
1404800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (!DestPtr) {
1405800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1406800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestIsVolatile = false;
1407800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
1408800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1409800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getAggregate(DestPtr);
1410800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
1411800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return RValue::get(CI);
1412800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
14139cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1414d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *DestPtr = ReturnValue.getValue();
1415d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    bool DestIsVolatile = ReturnValue.isVolatile();
14169cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1417d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!DestPtr) {
1418195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      DestPtr = CreateMemTemp(RetTy, "coerce");
1419d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      DestIsVolatile = false;
1420d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    }
14219cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1422117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the value is offset in memory, apply the offset now.
1423117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    llvm::Value *StorePtr = DestPtr;
1424117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (unsigned Offs = RetAI.getDirectOffset()) {
1425117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1426117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
14279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      StorePtr = Builder.CreateBitCast(StorePtr,
1428117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1429117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    }
1430117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
14319cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
143291a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1433ad3d6917dabbdab3399ff8307240aad58247d2e3Anders Carlsson    if (RetTy->isAnyComplexType())
1434d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
14353403084886b3d0fc23eee2b5708f0ac0329423e0Chris Lattner    if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1436d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      return RValue::getAggregate(DestPtr);
143791a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
1438639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  }
14398951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
14408951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
14411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(0 && "Invalid ABI kind for return argument");
144217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
14432c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
14442c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  assert(0 && "Unhandled ABIArgInfo::Kind");
14452c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  return RValue::get(0);
144617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
1447b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
1448b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar/* VarArg handling */
1449b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
1450b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbarllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1451b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1452b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar}
1453