19cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner//===--- CGCall.cpp - Encapsulate calling convention details ----*- C++ -*-===//
20dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
30dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//                     The LLVM Compiler Infrastructure
40dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
50dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// This file is distributed under the University of Illinois Open Source
60dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// License. See LICENSE.TXT for details.
70dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
80dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
90dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
100dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// These classes wrap the information about a call or function
110dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar// definition used to handle ABI compliancy.
120dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//
130dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar//===----------------------------------------------------------------------===//
140dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
150dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CGCall.h"
16ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner#include "ABIInfo.h"
1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CGCXXABI.h"
180dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "CodeGenFunction.h"
19b768807c49a1c7085def099b848631856af766faDaniel Dunbar#include "CodeGenModule.h"
20de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall#include "TargetInfo.h"
210dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/Decl.h"
22f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson#include "clang/AST/DeclCXX.h"
230dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar#include "clang/AST/DeclObjC.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/TargetInfo.h"
2506057cef0bcd7804e80f3ce2bbe352178396c715Chandler Carruth#include "clang/Frontend/CodeGenOptions.h"
26be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling#include "llvm/ADT/StringExtras.h"
273b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/Attributes.h"
283b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/DataLayout.h"
293b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth#include "llvm/IR/InlineAsm.h"
30c0dcc2d7beaeb6a80c0c7d69086b4c8894b662a0Bill Wendling#include "llvm/MC/SubtargetFeature.h"
3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/CallSite.h"
3297cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman#include "llvm/Transforms/Utils/Local.h"
330dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace clang;
340dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbarusing namespace CodeGen;
350dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
360dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar/***/
370dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
3804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
3904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  switch (CC) {
4004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  default: return llvm::CallingConv::C;
4104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
4204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
43f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
44414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
45414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
4638980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  case CC_IntelOclBicc: return llvm::CallingConv::Intel_OCL_BI;
4752fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  // TODO: add support for CC_X86Pascal to llvm
4804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  }
4904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall}
5004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
510b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Derives the 'this' type for codegen purposes, i.e. ignoring method
520b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualification.
530b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// FIXME: address space qualification?
54ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
55ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
56ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
570b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
580b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
590b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the canonical formal type of the given C++ method.
60ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
61ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return MD->getType()->getCanonicalTypeUnqualified()
62ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall           .getAs<FunctionProtoType>();
630b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
640b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
650b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// Returns the "extra-canonicalized" return type, which discards
660b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// qualifiers on the return type.  Codegen doesn't care about them,
670b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// and it makes ABI code a little easier to be able to assume that
680b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall/// all parameter and return types are top-level unqualified.
69ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCallstatic CanQualType GetReturnType(QualType RetTy) {
70ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
710b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
720b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
730f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the argument and result information for a value of the given
740f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// unprototyped freestanding function type.
750b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
760f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> FTNP) {
77de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // When translating an unprototyped function type, always use a
78de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // variadic type.
790f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
800f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 ArrayRef<CanQualType>(),
810f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 FTNP->getExtInfo(),
820f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 RequiredArgs(0));
83de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall}
84de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
850f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the LLVM function layout for a value of the given function
860f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// type, on top of any implicit parameters already stored.  Use the
870f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// given ExtInfo instead of the ExtInfo from the function type.
880f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic const CGFunctionInfo &arrangeLLVMFunctionInfo(CodeGenTypes &CGT,
890f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                       SmallVectorImpl<CanQualType> &prefix,
900f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                             CanQual<FunctionProtoType> FTP,
910f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                              FunctionType::ExtInfo extInfo) {
920f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, prefix.size());
93541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
9445c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
950f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall    prefix.push_back(FTP->getArgType(i));
96de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQualType resultType = FTP->getResultType().getUnqualifiedType();
970f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return CGT.arrangeLLVMFunctionInfo(resultType, prefix, extInfo, required);
980f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall}
990f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall
1000f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the argument and result information for a free function (i.e.
1010f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// not a C++ or ObjC instance method) of the given type.
1020f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic const CGFunctionInfo &arrangeFreeFunctionType(CodeGenTypes &CGT,
1030f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      SmallVectorImpl<CanQualType> &prefix,
1040f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                            CanQual<FunctionProtoType> FTP) {
1050f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(CGT, prefix, FTP, FTP->getExtInfo());
1060f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall}
1070f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall
1080f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Given the formal ext-info of a C++ instance method, adjust it
1090f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// according to the C++ ABI in effect.
1100f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic void adjustCXXMethodInfo(CodeGenTypes &CGT,
1118f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov                                FunctionType::ExtInfo &extInfo,
1128f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov                                bool isVariadic) {
1138f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  if (extInfo.getCC() == CC_Default) {
1148f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov    CallingConv CC = CGT.getContext().getDefaultCXXMethodCallConv(isVariadic);
1158f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov    extInfo = extInfo.withCallingConv(CC);
1168f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  }
1170f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall}
1180f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall
1190f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange the argument and result information for a free function (i.e.
1200f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// not a C++ or ObjC instance method) of the given type.
1210f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallstatic const CGFunctionInfo &arrangeCXXMethodType(CodeGenTypes &CGT,
1220f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      SmallVectorImpl<CanQualType> &prefix,
1230f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                            CanQual<FunctionProtoType> FTP) {
1240f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  FunctionType::ExtInfo extInfo = FTP->getExtInfo();
1258f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  adjustCXXMethodInfo(CGT, extInfo, FTP->isVariadic());
1260f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(CGT, prefix, FTP, extInfo);
1270b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall}
1280b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
129de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a value of the
1300f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// given freestanding function type.
1310b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCallconst CGFunctionInfo &
1320f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> FTP) {
133de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
1340f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return ::arrangeFreeFunctionType(*this, argTypes, FTP);
135bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar}
136bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
13704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCallstatic CallingConv getCallingConventionForDecl(const Decl *D) {
138bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  // Set the appropriate calling convention for the Function.
139bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<StdCallAttr>())
14004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86StdCall;
141bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
142bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar  if (D->hasAttr<FastCallAttr>())
14304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall    return CC_X86FastCall;
144bac7c250c9b098ee3d637c8ed77da62e860d9244Daniel Dunbar
145f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor  if (D->hasAttr<ThisCallAttr>())
146f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor    return CC_X86ThisCall;
147f813a2c03fcb05381b3252010435f557eb6b3cdeDouglas Gregor
14852fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik  if (D->hasAttr<PascalAttr>())
14952fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik    return CC_X86Pascal;
15052fc314e1b5e1baee6305067cf831763d02bd243Dawn Perchik
151414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  if (PcsAttr *PCS = D->getAttr<PcsAttr>())
152414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
153414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov
154263366f9241366f29ba65b703120f302490c39ffDerek Schuff  if (D->hasAttr<PnaclCallAttr>())
155263366f9241366f29ba65b703120f302490c39ffDerek Schuff    return CC_PnaclCall;
156263366f9241366f29ba65b703120f302490c39ffDerek Schuff
15738980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei  if (D->hasAttr<IntelOclBiccAttr>())
15838980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei    return CC_IntelOclBicc;
15938980086c0f791e8c23cc882574f18e5b4a87db6Guy Benyei
16004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  return CC_C;
16145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar}
16245c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
163de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a call to an
164de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// unknown C++ non-static member function of the given abstract type.
165de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// The member function must be an ordinary function, i.e. not a
166de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// constructor or destructor.
167de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
168de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXMethodType(const CXXRecordDecl *RD,
169de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                   const FunctionProtoType *FTP) {
170de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
1710b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
172375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson  // Add the 'this' pointer.
173de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTypes.push_back(GetThisType(Context, RD));
1740b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
1750f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return ::arrangeCXXMethodType(*this, argTypes,
1769c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller              FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
177375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson}
178375c31c4673f83f925de221752cf801c2fbbb246Anders Carlsson
179de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration or
180de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of the given C++ non-static member function.  The
181de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// member function must be an ordinary function, i.e. not a
182de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// constructor or destructor.
183de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
184de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
185fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
186fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall  assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
187fc4002872864e3c29c896000519ae989b6fdb7ddJohn McCall
188de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQual<FunctionProtoType> prototype = GetFormalType(MD);
1891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  if (MD->isInstance()) {
191de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    // The abstract case is perfectly fine.
192de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return arrangeCXXMethodType(MD->getParent(), prototype.getTypePtr());
193de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
194de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
1950f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeFreeFunctionType(prototype);
196f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson}
197f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson
198de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration
199de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// or definition to the given constructor variant.
200de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
201de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXConstructorDeclaration(const CXXConstructorDecl *D,
202de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                               CXXCtorType ctorKind) {
203de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
204de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTypes.push_back(GetThisType(Context, D->getParent()));
205de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQualType resultType = Context.VoidTy;
206f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
207de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  TheCXXABI.BuildConstructorSignature(D, ctorKind, resultType, argTypes);
2080b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
2094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
2104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
211de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required = RequiredArgs::forPrototypePlus(FTP, argTypes.size());
212de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
2134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  // Add the formal parameters.
2144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
215de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTypes.push_back(FTP->getArgType(i));
2164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
2170f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  FunctionType::ExtInfo extInfo = FTP->getExtInfo();
2188f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  adjustCXXMethodInfo(*this, extInfo, FTP->isVariadic());
2190f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo, required);
220f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
221f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
222de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for a declaration,
223de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition, or call to the given destructor variant.  It so
224de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// happens that all three cases produce the same information.
225de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
226de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeCXXDestructor(const CXXDestructorDecl *D,
227de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                   CXXDtorType dtorKind) {
228de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 2> argTypes;
229de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTypes.push_back(GetThisType(Context, D->getParent()));
230de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CanQualType resultType = Context.VoidTy;
2314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
232de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  TheCXXABI.BuildDestructorSignature(D, dtorKind, resultType, argTypes);
2330b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall
2344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  CanQual<FunctionProtoType> FTP = GetFormalType(D);
2354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
2368f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  assert(FTP->isVariadic() == 0 && "dtor with formal parameters");
2374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
2380f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  FunctionType::ExtInfo extInfo = FTP->getExtInfo();
2398f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  adjustCXXMethodInfo(*this, extInfo, false);
2400f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(resultType, argTypes, extInfo,
2410f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 RequiredArgs::All);
242f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson}
243f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson
244de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the declaration or
245de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of the given function.
246de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
247de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionDeclaration(const FunctionDecl *FD) {
2483eb67ca786ef75bad43d30349c7334b921ba0dbcChris Lattner  if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
249f6f8ae561ef78af169cbd2c067cae7ee4b2044e9Anders Carlsson    if (MD->isInstance())
250de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      return arrangeCXXMethodDeclaration(MD);
2511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
252ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
253de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
254ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionType>(FTy));
255de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
256de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // When declaring a function without a prototype, always use a
257de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // non-variadic type.
258de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  if (isa<FunctionNoProtoType>(FTy)) {
259de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    CanQual<FunctionNoProtoType> noProto = FTy.getAs<FunctionNoProtoType>();
2600f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall    return arrangeLLVMFunctionInfo(noProto->getResultType(),
2610f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                   ArrayRef<CanQualType>(),
2620f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                   noProto->getExtInfo(),
2630f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                   RequiredArgs::All);
264de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
265de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
266ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall  assert(isa<FunctionProtoType>(FTy));
2670f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeFreeFunctionType(FTy.getAs<FunctionProtoType>());
2680dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
2690dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
270de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the declaration or
271de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// definition of an Objective-C method.
272de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
273de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeObjCMethodDeclaration(const ObjCMethodDecl *MD) {
274de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // It happens that this is the same as a call with no optional
275de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // arguments, except also using the formal 'self' type.
276de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeObjCMessageSendSignature(MD, MD->getSelfDecl()->getType());
277de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall}
278de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
279de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for the function type
280de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// through which to perform a send to the given Objective-C method,
281de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// using the given receiver type.  The receiver type is not always
282de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// the 'self' type of the method or even an Objective-C pointer type.
283de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// This is *not* the right method for actually performing such a
284de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// message send, due to the possibility of optional arguments.
285de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
286de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeObjCMessageSendSignature(const ObjCMethodDecl *MD,
287de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                              QualType receiverType) {
288de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTys;
289de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTys.push_back(Context.getCanonicalParamType(receiverType));
290de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  argTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
291541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy?
292491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator i = MD->param_begin(),
2930b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall         e = MD->param_end(); i != e; ++i) {
294de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTys.push_back(Context.getCanonicalParamType((*i)->getType()));
2950b0ef0a70b8010c66fad2603e4423ef1c1dc7015John McCall  }
296f85e193739c953358c865005855253af4f68a497John McCall
297f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo;
298f85e193739c953358c865005855253af4f68a497John McCall  einfo = einfo.withCallingConv(getCallingConventionForDecl(MD));
299f85e193739c953358c865005855253af4f68a497John McCall
3004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getContext().getLangOpts().ObjCAutoRefCount &&
301f85e193739c953358c865005855253af4f68a497John McCall      MD->hasAttr<NSReturnsRetainedAttr>())
302f85e193739c953358c865005855253af4f68a497John McCall    einfo = einfo.withProducesResult(true);
303f85e193739c953358c865005855253af4f68a497John McCall
304de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required =
305de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    (MD->isVariadic() ? RequiredArgs(argTys.size()) : RequiredArgs::All);
306de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
3070f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(GetReturnType(MD->getResultType()), argTys,
3080f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 einfo, required);
3090dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
3100dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
311de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
312de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeGlobalDeclaration(GlobalDecl GD) {
313b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  // FIXME: Do we need to handle ObjCMethodDecl?
314b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
3159cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
316b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
317de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return arrangeCXXConstructorDeclaration(CD, GD.getCtorType());
318b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
319b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson  if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
320de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return arrangeCXXDestructor(DD, GD.getDtorType());
321de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
322de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return arrangeFunctionDeclaration(FD);
323de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall}
3249cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
325e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall/// Arrange a call as unto a free function, except possibly with an
326e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall/// additional number of formal parameters considered required.
327e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCallstatic const CGFunctionInfo &
328e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCallarrangeFreeFunctionLikeCall(CodeGenTypes &CGT,
329e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall                            const CallArgList &args,
330e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall                            const FunctionType *fnType,
331e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall                            unsigned numExtraRequiredArgs) {
332e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  assert(args.size() >= numExtraRequiredArgs);
333e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall
334e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // In most cases, there are no optional arguments.
335e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  RequiredArgs required = RequiredArgs::All;
336e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall
337e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // If we have a variadic prototype, the required arguments are the
338e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // extra prefix plus the arguments in the prototype.
339e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) {
340e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall    if (proto->isVariadic())
341e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall      required = RequiredArgs(proto->getNumArgs() + numExtraRequiredArgs);
342e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall
343e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // If we don't have a prototype at all, but we're supposed to
344e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // explicitly use the variadic convention for unprototyped calls,
345e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // treat all of the arguments as required but preserve the nominal
346e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // possibility of variadics.
347e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  } else if (CGT.CGM.getTargetCodeGenInfo()
348e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall               .isNoProtoCallVariadic(args, cast<FunctionNoProtoType>(fnType))) {
349e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall    required = RequiredArgs(args.size());
350e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  }
351e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall
352e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  return CGT.arrangeFreeFunctionCall(fnType->getResultType(), args,
353e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall                                     fnType->getExtInfo(), required);
354e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall}
355e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall
356de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Figure out the rules for calling a function with the given formal
357de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// type using the given arguments.  The arguments are necessary
358de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// because the function might be unprototyped, in which case it's
359de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// target-dependent in crazy ways.
360de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
3610f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionCall(const CallArgList &args,
3620f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      const FunctionType *fnType) {
363e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  return arrangeFreeFunctionLikeCall(*this, args, fnType, 0);
364e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall}
365de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
366e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall/// A block function call is essentially a free-function call with an
367e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall/// extra implicit argument.
368e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCallconst CGFunctionInfo &
369e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCallCodeGenTypes::arrangeBlockFunctionCall(const CallArgList &args,
370e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall                                       const FunctionType *fnType) {
371e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  return arrangeFreeFunctionLikeCall(*this, args, fnType, 1);
372b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson}
373b2bcf1c176b200b36f371e189ce22f93c86cdf45Anders Carlsson
374de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
3750f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeFreeFunctionCall(QualType resultType,
3760f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      const CallArgList &args,
3770f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      FunctionType::ExtInfo info,
3780f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      RequiredArgs required) {
379541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
380de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
381de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (CallArgList::const_iterator i = args.begin(), e = args.end();
382725ad31086e3d6c41afa10c43db44f2e7060a961Daniel Dunbar       i != e; ++i)
383de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTypes.push_back(Context.getCanonicalParamType(i->Ty));
3840f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info,
3850f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 required);
3860f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall}
3870f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall
3880f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall/// Arrange a call to a C++ method, passing the given arguments.
3890f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallconst CGFunctionInfo &
3900f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeCXXMethodCall(const CallArgList &args,
3910f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                   const FunctionProtoType *FPT,
3920f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                   RequiredArgs required) {
3930f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  // FIXME: Kill copy.
3940f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  SmallVector<CanQualType, 16> argTypes;
3950f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  for (CallArgList::const_iterator i = args.begin(), e = args.end();
3960f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall       i != e; ++i)
3970f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall    argTypes.push_back(Context.getCanonicalParamType(i->Ty));
3980f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall
3990f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  FunctionType::ExtInfo info = FPT->getExtInfo();
4008f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  adjustCXXMethodInfo(*this, info, FPT->isVariadic());
4010f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(GetReturnType(FPT->getResultType()),
4020f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 argTypes, info, required);
4030dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
4040dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar
405de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
406de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::arrangeFunctionDeclaration(QualType resultType,
407de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                         const FunctionArgList &args,
408de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                         const FunctionType::ExtInfo &info,
409de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                         bool isVariadic) {
410541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar  // FIXME: Kill copy.
411de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  SmallVector<CanQualType, 16> argTypes;
412de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (FunctionArgList::const_iterator i = args.begin(), e = args.end();
413bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar       i != e; ++i)
414de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    argTypes.push_back(Context.getCanonicalParamType((*i)->getType()));
415de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
416de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  RequiredArgs required =
417de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    (isVariadic ? RequiredArgs(args.size()) : RequiredArgs::All);
4180f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(GetReturnType(resultType), argTypes, info,
4190f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 required);
420541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar}
421541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
422de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &CodeGenTypes::arrangeNullaryFunction() {
4230f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall  return arrangeLLVMFunctionInfo(getContext().VoidTy, ArrayRef<CanQualType>(),
4240f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                 FunctionType::ExtInfo(), RequiredArgs::All);
425d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
426d26bc76c98006609002d9930f8840490e88ac5b5John McCall
427de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// Arrange the argument and result information for an abstract value
428de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// of a given function type.  This is the method which all of the
429de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall/// above functions ultimately defer to.
430de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallconst CGFunctionInfo &
4310f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCallCodeGenTypes::arrangeLLVMFunctionInfo(CanQualType resultType,
4320f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      ArrayRef<CanQualType> argTypes,
4330f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      FunctionType::ExtInfo info,
4340f3d0970dcdf6cf17550b86838dff12813968dbcJohn McCall                                      RequiredArgs required) {
435ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#ifndef NDEBUG
436de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (ArrayRef<CanQualType>::const_iterator
437de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall         I = argTypes.begin(), E = argTypes.end(); I != E; ++I)
438ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall    assert(I->isCanonicalAsParam());
439ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall#endif
440ead608af31b6c9abeae1ca6d0b75094dac4641c0John McCall
441de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  unsigned CC = ClangCallConvToLLVMCallConv(info.getCC());
44204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
44340a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  // Lookup or create unique function info.
44440a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  llvm::FoldingSetNodeID ID;
445de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CGFunctionInfo::Profile(ID, info, required, resultType, argTypes);
44640a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
447de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  void *insertPos = 0;
448de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, insertPos);
44940a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar  if (FI)
45040a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar    return *FI;
45140a6be686e5d5bb4198f1affda574e8a4b3a7710Daniel Dunbar
452de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  // Construct the function info.  We co-allocate the ArgInfos.
453de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI = CGFunctionInfo::create(CC, info, resultType, argTypes, required);
454de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FunctionInfos.InsertNode(FI, insertPos);
455541b63b1a9db77e4a8670e9823711c2c12e58afbDaniel Dunbar
456de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool inserted = FunctionsBeingProcessed.insert(FI); (void)inserted;
457de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  assert(inserted && "Recursively being processed?");
45871305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
45988c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // Compute ABI information.
460ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  getABIInfo().computeInfo(*FI);
4619cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
462800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // Loop over all of the computed argument and return value info.  If any of
463800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // them are direct or extend without a specified coerce type, specify the
464800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  // default now.
465de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  ABIArgInfo &retInfo = FI->getReturnInfo();
466de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  if (retInfo.canHaveCoerceToType() && retInfo.getCoerceToType() == 0)
467de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    retInfo.setCoerceToType(ConvertType(FI->getReturnType()));
4689cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
469800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
470800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner       I != E; ++I)
471800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
4729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      I->info.setCoerceToType(ConvertType(I->type));
4739cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
474de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool erased = FunctionsBeingProcessed.erase(FI); (void)erased;
475de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  assert(erased && "Not in set?");
476d26c07142710790b820a66245939668f62eaf2d9Chris Lattner
47788c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  return *FI;
4780dbe227feccf6a8dbadfff8ca3f80416b7bf2f28Daniel Dunbar}
47917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
480de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCGFunctionInfo *CGFunctionInfo::create(unsigned llvmCC,
481de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       const FunctionType::ExtInfo &info,
482de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       CanQualType resultType,
483de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       ArrayRef<CanQualType> argTypes,
484de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                       RequiredArgs required) {
485de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  void *buffer = operator new(sizeof(CGFunctionInfo) +
486de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                              sizeof(ArgInfo) * (argTypes.size() + 1));
487de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  CGFunctionInfo *FI = new(buffer) CGFunctionInfo();
488de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->CallingConvention = llvmCC;
489de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->EffectiveCallingConvention = llvmCC;
490de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->ASTCallingConvention = info.getCC();
491de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->NoReturn = info.getNoReturn();
492de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->ReturnsRetained = info.getProducesResult();
493de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->Required = required;
494de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->HasRegParm = info.getHasRegParm();
495de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->RegParm = info.getRegParm();
496de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->NumArgs = argTypes.size();
497de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  FI->getArgsBuffer()[0].type = resultType;
498de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  for (unsigned i = 0, e = argTypes.size(); i != e; ++i)
499de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    FI->getArgsBuffer()[i + 1].type = argTypes[i];
500de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return FI;
50188c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar}
50288c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
50388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar/***/
50488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar
50542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCallvoid CodeGenTypes::GetExpandedTypes(QualType type,
5065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                     SmallVectorImpl<llvm::Type*> &expandedTypes) {
507194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(type)) {
508194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    uint64_t NumElts = AT->getSize().getZExtValue();
509194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (uint64_t Elt = 0; Elt < NumElts; ++Elt)
510194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      GetExpandedTypes(AT->getElementType(), expandedTypes);
511eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  } else if (const RecordType *RT = type->getAs<RecordType>()) {
512194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const RecordDecl *RD = RT->getDecl();
513194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    assert(!RD->hasFlexibleArrayMember() &&
514194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson           "Cannot expand structure with flexible array.");
515eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->isUnion()) {
516eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // Unions can be here only in degenerative cases - all the fields are same
517eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // after flattening. Thus we have to use the "largest" field.
518eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      const FieldDecl *LargestFD = 0;
519eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      CharUnits UnionSize = CharUnits::Zero();
520eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
521eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
522eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
523581deb3da481053c4993c7600f97acf7768caac5David Blaikie        const FieldDecl *FD = *i;
524eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        assert(!FD->isBitField() &&
525eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
526eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
527eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        if (UnionSize < FieldSize) {
528eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          UnionSize = FieldSize;
529eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          LargestFD = FD;
530eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        }
531eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
532eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      if (LargestFD)
533eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        GetExpandedTypes(LargestFD->getType(), expandedTypes);
534eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    } else {
535eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
536eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
537581deb3da481053c4993c7600f97acf7768caac5David Blaikie        assert(!i->isBitField() &&
538eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
539581deb3da481053c4993c7600f97acf7768caac5David Blaikie        GetExpandedTypes(i->getType(), expandedTypes);
540eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
541194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
542194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const ComplexType *CT = type->getAs<ComplexType>()) {
543194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    llvm::Type *EltTy = ConvertType(CT->getElementType());
544194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    expandedTypes.push_back(EltTy);
545194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    expandedTypes.push_back(EltTy);
546194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else
547194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    expandedTypes.push_back(ConvertType(type));
5485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
5495627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpllvm::Function::arg_iterator
5515627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel DunbarCodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
5525627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                                    llvm::Function::arg_iterator AI) {
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(LV.isSimple() &&
5541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump         "Unexpected non-simple lvalue during struct expansion.");
555194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
556194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
557194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    unsigned NumElts = AT->getSize().getZExtValue();
558194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    QualType EltTy = AT->getElementType();
559194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
560377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman      llvm::Value *EltAddr = Builder.CreateConstGEP2_32(LV.getAddress(), 0, Elt);
561194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      LValue LV = MakeAddrLValue(EltAddr, EltTy);
562194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      AI = ExpandTypeFromArgs(EltTy, LV, AI);
563194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
564eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
565194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    RecordDecl *RD = RT->getDecl();
566eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->isUnion()) {
567eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // Unions can be here only in degenerative cases - all the fields are same
568eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      // after flattening. Thus we have to use the "largest" field.
569eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      const FieldDecl *LargestFD = 0;
570eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      CharUnits UnionSize = CharUnits::Zero();
571eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
572eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
573eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
574581deb3da481053c4993c7600f97acf7768caac5David Blaikie        const FieldDecl *FD = *i;
575eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        assert(!FD->isBitField() &&
576eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
577eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
578eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        if (UnionSize < FieldSize) {
579eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          UnionSize = FieldSize;
580eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          LargestFD = FD;
581eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        }
582eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
583eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      if (LargestFD) {
584eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        // FIXME: What are the right qualifiers here?
585377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue SubLV = EmitLValueForField(LV, LargestFD);
586377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        AI = ExpandTypeFromArgs(LargestFD->getType(), SubLV, AI);
587eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
588eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    } else {
589eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
590eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
591581deb3da481053c4993c7600f97acf7768caac5David Blaikie        FieldDecl *FD = *i;
592eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        QualType FT = FD->getType();
593eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
594eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        // FIXME: What are the right qualifiers here?
595377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        LValue SubLV = EmitLValueForField(LV, FD);
596377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        AI = ExpandTypeFromArgs(FT, SubLV, AI);
597eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
5985627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
599194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
600194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    QualType EltTy = CT->getElementType();
601377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    llvm::Value *RealAddr = Builder.CreateStructGEP(LV.getAddress(), 0, "real");
602194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(RealAddr, EltTy));
603377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    llvm::Value *ImagAddr = Builder.CreateStructGEP(LV.getAddress(), 1, "imag");
604194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    EmitStoreThroughLValue(RValue::get(AI++), MakeAddrLValue(ImagAddr, EltTy));
605194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
606194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    EmitStoreThroughLValue(RValue::get(AI), LV);
607194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    ++AI;
6085627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  }
6095627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
6105627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  return AI;
6115627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar}
6125627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
613e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner/// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
61408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// accessing some number of bytes out of it, try to gep into the struct to get
61508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// at its inner goodness.  Dive as deep as possible without entering an element
61608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner/// with an in-memory size smaller than DstSize.
61708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattnerstatic llvm::Value *
618e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris LattnerEnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
6192acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                   llvm::StructType *SrcSTy,
620e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner                                   uint64_t DstSize, CodeGenFunction &CGF) {
62108dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // We can't dive into a zero-element struct.
62208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  if (SrcSTy->getNumElements() == 0) return SrcPtr;
6239cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *FirstElt = SrcSTy->getElementType(0);
6259cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
62608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first elt is at least as large as what we're looking for, or if the
62708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // first element is the same size as the whole struct, we can enter it.
6289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  uint64_t FirstEltSize =
62925a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow    CGF.CGM.getDataLayout().getTypeAllocSize(FirstElt);
6309cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  if (FirstEltSize < DstSize &&
63125a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow      FirstEltSize < CGF.CGM.getDataLayout().getTypeAllocSize(SrcSTy))
63208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    return SrcPtr;
6339cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
63408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // GEP into the first element.
63508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
6369cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
63708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  // If the first element is a struct, recurse.
6382acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SrcTy =
63908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
6402acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
641e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
64208dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
64308dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  return SrcPtr;
64408dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner}
64508dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
6466d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
6476d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// are either integers or pointers.  This does a truncation of the value if it
6486d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner/// is too large or a zero extension if it is too small.
6496d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattnerstatic llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
6502acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                             llvm::Type *Ty,
6516d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner                                             CodeGenFunction &CGF) {
6526d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() == Ty)
6536d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return Val;
6549cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6556d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Val->getType())) {
6566d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // If this is Pointer->Pointer avoid conversion to and from int.
6576d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    if (isa<llvm::PointerType>(Ty))
6586d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
6599cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6606d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    // Convert the pointer to an integer so we can play with its width.
66177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
6626d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
6639cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6642acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *DestIntTy = Ty;
6656d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(DestIntTy))
66677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    DestIntTy = CGF.IntPtrTy;
6679cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6686d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (Val->getType() != DestIntTy)
6696d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
6709cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6716d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if (isa<llvm::PointerType>(Ty))
6726d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
6736d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  return Val;
6746d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner}
6756d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner
67608dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
67708dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner
678275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
679275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// a pointer to an object of type \arg Ty.
680275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
681275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is smaller than the
682275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; in this situation the values of bits which not
683275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// present in the src are undefined.
684275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
6852acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner                                      llvm::Type *Ty,
686275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                                      CodeGenFunction &CGF) {
6872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SrcTy =
688275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
6899cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6906ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  // If SrcTy and Ty are the same, just do a load.
6916ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == Ty)
6926ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return CGF.Builder.CreateLoad(SrcPtr);
6939cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
69425a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(Ty);
6959cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
6962acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
697e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
69808dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner    SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
69908dd2a0e4c6b3c7cb601e5053eb02cb7d084f87eChris Lattner  }
7009cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
70125a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
702275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
7036d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
7046d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
7056d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
7066d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
7076d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
7086d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
7096d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
7109cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
711b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  // If load is legal, just bitcast the src pointer.
7127ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar  if (SrcSize >= DstSize) {
713f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // Generally SrcSize is never greater than DstSize, since this means we are
714f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // losing bits. However, this can happen in cases where the structure has
715f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // additional padding, for example due to a user specified alignment.
7167ef455be9beb7a755d815bfbdc38d55d1ce59b86Daniel Dunbar    //
717f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: Assert that we aren't truncating non-padding bits when have access
718f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // to that information.
719275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
720275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
721386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
722386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned load.
723386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    Load->setAlignment(1);
724386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    return Load;
725275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
7269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
72735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // Otherwise do coercion through memory. This is stupid, but
72835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  // simple.
72935b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
730f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren  llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
731f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren  llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
732f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren  llvm::Value *SrcCasted = CGF.Builder.CreateBitCast(SrcPtr, I8PtrTy);
733060f34d6d12a851faa9463da522f7dff1104d0e1Manman Ren  // FIXME: Use better alignment.
734f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren  CGF.Builder.CreateMemCpy(Casted, SrcCasted,
735f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren      llvm::ConstantInt::get(CGF.IntPtrTy, SrcSize),
736f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren      1, false);
73735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  return CGF.Builder.CreateLoad(Tmp);
738275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
739275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
740badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// Function to store a first-class aggregate into memory.  We prefer to
741badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// store the elements rather than the aggregate to be more friendly to
742badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// fast-isel.
743badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman// FIXME: Do we need to recurse here?
744badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedmanstatic void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
745badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                          llvm::Value *DestPtr, bool DestIsVolatile,
746badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                          bool LowAlignment) {
747badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  // Prefer scalar stores to first-class aggregate stores.
7482acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy =
749badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        dyn_cast<llvm::StructType>(Val->getType())) {
750badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
751badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
752badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
753badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
754badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman                                                    DestIsVolatile);
755badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman      if (LowAlignment)
756badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        SI->setAlignment(1);
757badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    }
758badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  } else {
75908212631e15a17157368af03180018601b31fb4dBill Wendling    llvm::StoreInst *SI = CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
76008212631e15a17157368af03180018601b31fb4dBill Wendling    if (LowAlignment)
76108212631e15a17157368af03180018601b31fb4dBill Wendling      SI->setAlignment(1);
762badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman  }
763badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman}
764badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman
765275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
766275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// where the source and destination may have different types.
767275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar///
768275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// This safely handles the case when the src type is larger than the
769275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar/// destination type; the upper bits of the src will be lost.
770275e10d62af4a129a8559253958296d8659684c9Daniel Dunbarstatic void CreateCoercedStore(llvm::Value *Src,
771275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               llvm::Value *DstPtr,
772d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                               bool DstIsVolatile,
773275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar                               CodeGenFunction &CGF) {
7742acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *SrcTy = Src->getType();
7752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *DstTy =
776275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
7776ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  if (SrcTy == DstTy) {
7786ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
7796ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner    return;
7806ae0069f5db96b8ed5adc598d576e695e5ac4134Chris Lattner  }
7819cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
78225a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  uint64_t SrcSize = CGF.CGM.getDataLayout().getTypeAllocSize(SrcTy);
7839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
7842acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
785e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
786e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner    DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
787e7bb777caf478ac8b096bd6a0c14d78ea8b2f5beChris Lattner  }
7889cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
7896d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // If the source and destination are integer or pointer types, just do an
7906d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  // extension or truncation to the desired type.
7916d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
7926d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner      (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
7936d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
7946d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
7956d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner    return;
7966d11cdbde41aa847369db85369b2ce5f82515b06Chris Lattner  }
7979cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
79825a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow  uint64_t DstSize = CGF.CGM.getDataLayout().getTypeAllocSize(DstTy);
799275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
80088c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar  // If store is legal, just bitcast the src pointer.
801fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar  if (SrcSize <= DstSize) {
802275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Casted =
803275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
804386621f5e12f5db95af3b82250b08fdae3e3d321Daniel Dunbar    // FIXME: Use better alignment / avoid requiring aligned store.
805badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman    BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
806275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  } else {
807275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // Otherwise do coercion through memory. This is stupid, but
808275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    // simple.
809fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar
810fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // Generally SrcSize is never greater than DstSize, since this means we are
811fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // losing bits. However, this can happen in cases where the structure has
812fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // additional padding, for example due to a user specified alignment.
813fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    //
814fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // FIXME: Assert that we aren't truncating non-padding bits when have access
815fdf4986c4c75514df428ed71d5942252f18e129bDaniel Dunbar    // to that information.
816275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
817275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar    CGF.Builder.CreateStore(Src, Tmp);
818f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren    llvm::Type *I8PtrTy = CGF.Builder.getInt8PtrTy();
819f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren    llvm::Value *Casted = CGF.Builder.CreateBitCast(Tmp, I8PtrTy);
820f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren    llvm::Value *DstCasted = CGF.Builder.CreateBitCast(DstPtr, I8PtrTy);
821060f34d6d12a851faa9463da522f7dff1104d0e1Manman Ren    // FIXME: Use better alignment.
822f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren    CGF.Builder.CreateMemCpy(DstCasted, Casted,
823f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren        llvm::ConstantInt::get(CGF.IntPtrTy, DstSize),
824f51c61c78e8487061b6a434286488fa3e5b919e5Manman Ren        1, false);
825275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar  }
826275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar}
827275e10d62af4a129a8559253958296d8659684c9Daniel Dunbar
8285627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar/***/
8295627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
830dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
83111e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  return FI.getReturnInfo().isIndirect();
832bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar}
833bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar
834dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbarbool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
835dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
836dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    switch (BT->getKind()) {
837dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    default:
838dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar      return false;
839dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Float:
840bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Float);
841dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::Double:
842bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      return getContext().getTargetInfo().useObjCFPRetForRealType(TargetInfo::Double);
843dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    case BuiltinType::LongDouble:
844bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      return getContext().getTargetInfo().useObjCFPRetForRealType(
845dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar        TargetInfo::LongDouble);
846dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar    }
847dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  }
848dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
849dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  return false;
850dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar}
851dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar
852eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlssonbool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) {
853eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson  if (const ComplexType *CT = ResultType->getAs<ComplexType>()) {
854eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson    if (const BuiltinType *BT = CT->getElementType()->getAs<BuiltinType>()) {
855eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson      if (BT->getKind() == BuiltinType::LongDouble)
856eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson        return getContext().getTargetInfo().useObjCFP2RetForComplexLongDouble();
857eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson    }
858eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson  }
859eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson
860eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson  return false;
861eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson}
862eea64802558cc398571938b1f28cda1d4fa79ec3Anders Carlsson
8639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
864de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  const CGFunctionInfo &FI = arrangeGlobalDeclaration(GD);
865de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return GetFunctionType(FI);
866c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall}
867c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall
8689cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::FunctionType *
869de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallCodeGenTypes::GetFunctionType(const CGFunctionInfo &FI) {
87071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
87171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool Inserted = FunctionsBeingProcessed.insert(&FI); (void)Inserted;
87271305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(Inserted && "Recursively being processed?");
87371305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
8745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Type*, 8> argTypes;
8752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *resultType = 0;
87645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
87742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  const ABIArgInfo &retAI = FI.getReturnInfo();
87842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall  switch (retAI.getKind()) {
8798951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
88042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    llvm_unreachable("Invalid ABI kind for return argument");
8818951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
882cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
88346327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
88442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = retAI.getCoerceToType();
88546327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    break;
88646327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar
88711e383aa491a23ebd4a49688a09984c5e16a2e34Daniel Dunbar  case ABIArgInfo::Indirect: {
88842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
88942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = llvm::Type::getVoidTy(getLLVMContext());
89042e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall
89142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    QualType ret = FI.getReturnType();
8922acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *ty = ConvertType(ret);
89342e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    unsigned addressSpace = Context.getTargetAddressSpace(ret);
89442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
89545c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar    break;
89645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
89745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
89811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
89942e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    resultType = llvm::Type::getVoidTy(getLLVMContext());
90011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    break;
90145c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
9021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
903e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  // Add in all of the required arguments.
904e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  CGFunctionInfo::const_arg_iterator it = FI.arg_begin(), ie;
905e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  if (FI.isVariadic()) {
906e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall    ie = it + FI.getRequiredArgs().getNumRequiredArgs();
907e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  } else {
908e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall    ie = FI.arg_end();
909e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  }
910e56bb36e8eea89bae7dfe6eb6ea0455af126bf4aJohn McCall  for (; it != ie; ++it) {
91142e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    const ABIArgInfo &argAI = it->info;
9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
913e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    // Insert a padding type to ensure proper alignment.
914e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    if (llvm::Type *PaddingType = argAI.getPaddingType())
915e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      argTypes.push_back(PaddingType);
916e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola
91742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall    switch (argAI.getKind()) {
91811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
91911434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
92011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
921800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect: {
922800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // indirect arguments are always on the stack, which is addr space #0.
9232acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type *LTy = ConvertTypeForMem(it->type);
92442e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      argTypes.push_back(LTy->getPointerTo());
925800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
926800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
927800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
928800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
9291ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    case ABIArgInfo::Direct: {
930ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, flatten it.  Either
931ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // way is semantically identical, but fast-isel and the optimizer
932ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // generally likes scalar values better than FCAs.
9339cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      llvm::Type *argType = argAI.getCoerceToType();
9342acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      if (llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
93542e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall        for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
93642e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall          argTypes.push_back(st->getElementType(i));
937ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
93842e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall        argTypes.push_back(argType);
939ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
94089c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
9411ed72678b41c6038b08f48cb7e2a7b61e2dd9179Chris Lattner    }
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9438951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    case ABIArgInfo::Expand:
9449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      GetExpandedTypes(it->type, argTypes);
9458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
9468951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
94745c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  }
94845c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar
94971305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  bool Erased = FunctionsBeingProcessed.erase(&FI); (void)Erased;
95071305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner  assert(Erased && "Not in set?");
95171305cc81bd379ddb8aa0d49e268267383202ca9Chris Lattner
952de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return llvm::FunctionType::get(resultType, argTypes, FI.isVariadic());
9533913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar}
9543913f184c84135fb4612743f1faa6c1edd2dd055Daniel Dunbar
9552acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerllvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
9564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
957ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
9589cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
959f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (!isFuncTypeConvertible(FPT))
960f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner    return llvm::StructType::get(getLLVMContext());
961f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner
962f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  const CGFunctionInfo *Info;
963f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  if (isa<CXXDestructorDecl>(MD))
964de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    Info = &arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), GD.getDtorType());
965f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner  else
966de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    Info = &arrangeCXXMethodDeclaration(MD);
967de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  return GetFunctionType(*Info);
968ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson}
969ecf282b0486873309fd58ec4d3ec0dbf983b33d4Anders Carlsson
970a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbarvoid CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
97188b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                           const Decl *TargetDecl,
9729cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer                                           AttributeListType &PAL,
97394236e7393d5cd4acbc5f0b503d23bbe78d9aeecBill Wendling                                           unsigned &CallingConv,
97494236e7393d5cd4acbc5f0b503d23bbe78d9aeecBill Wendling                                           bool AttrOnCallSite) {
9750d5833921cc728bc1d2e45fbaf7b3e11cddbf99dBill Wendling  llvm::AttrBuilder FuncAttrs;
9760d5833921cc728bc1d2e45fbaf7b3e11cddbf99dBill Wendling  llvm::AttrBuilder RetAttrs;
9775323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
978ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CallingConv = FI.getEffectiveCallingConvention();
979ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar
98004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (FI.isNoReturn())
98172390b39c545426023ec104afe8706395d732badBill Wendling    FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
98204a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall
9831102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov  // FIXME: handle sseregparm someday...
9845323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  if (TargetDecl) {
9856700415542121e2cb7d867728046ffa21e402019Rafael Espindola    if (TargetDecl->hasAttr<ReturnsTwiceAttr>())
98672390b39c545426023ec104afe8706395d732badBill Wendling      FuncAttrs.addAttribute(llvm::Attribute::ReturnsTwice);
98740b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis    if (TargetDecl->hasAttr<NoThrowAttr>())
98872390b39c545426023ec104afe8706395d732badBill Wendling      FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
9897586a6e6b7d79d4be031d2d0d6a35d5996cd0db9Richard Smith    if (TargetDecl->hasAttr<NoReturnAttr>())
9907586a6e6b7d79d4be031d2d0d6a35d5996cd0db9Richard Smith      FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
9917586a6e6b7d79d4be031d2d0d6a35d5996cd0db9Richard Smith
9927586a6e6b7d79d4be031d2d0d6a35d5996cd0db9Richard Smith    if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
9939c0c1f333ab8f5a3da055b99ee94778689face17John McCall      const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
9948026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl      if (FPT && FPT->isNothrow(getContext()))
99572390b39c545426023ec104afe8706395d732badBill Wendling        FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
9963c5cd15da59d084ee441ac1512907a73f0e1f2bdRichard Smith      // Don't use [[noreturn]] or _Noreturn for a call to a virtual function.
9973c5cd15da59d084ee441ac1512907a73f0e1f2bdRichard Smith      // These attributes are not inherited by overloads.
9983c5cd15da59d084ee441ac1512907a73f0e1f2bdRichard Smith      const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Fn);
9993c5cd15da59d084ee441ac1512907a73f0e1f2bdRichard Smith      if (Fn->isNoReturn() && !(AttrOnCallSite && MD && MD->isVirtual()))
10007586a6e6b7d79d4be031d2d0d6a35d5996cd0db9Richard Smith        FuncAttrs.addAttribute(llvm::Attribute::NoReturn);
10019c0c1f333ab8f5a3da055b99ee94778689face17John McCall    }
10029c0c1f333ab8f5a3da055b99ee94778689face17John McCall
1003041087caec03e8855770695d3eabc0feb031f6edEric Christopher    // 'const' and 'pure' attribute functions are also nounwind.
1004041087caec03e8855770695d3eabc0feb031f6edEric Christopher    if (TargetDecl->hasAttr<ConstAttr>()) {
100572390b39c545426023ec104afe8706395d732badBill Wendling      FuncAttrs.addAttribute(llvm::Attribute::ReadNone);
100672390b39c545426023ec104afe8706395d732badBill Wendling      FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1007041087caec03e8855770695d3eabc0feb031f6edEric Christopher    } else if (TargetDecl->hasAttr<PureAttr>()) {
100872390b39c545426023ec104afe8706395d732badBill Wendling      FuncAttrs.addAttribute(llvm::Attribute::ReadOnly);
100972390b39c545426023ec104afe8706395d732badBill Wendling      FuncAttrs.addAttribute(llvm::Attribute::NoUnwind);
1010041087caec03e8855770695d3eabc0feb031f6edEric Christopher    }
101176168e289ca4b307259e3bc9b3353f03b05bb6b9Ryan Flynn    if (TargetDecl->hasAttr<MallocAttr>())
101272390b39c545426023ec104afe8706395d732badBill Wendling      RetAttrs.addAttribute(llvm::Attribute::NoAlias);
10135323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
10145323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
10152811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.OptimizeSize)
101672390b39c545426023ec104afe8706395d732badBill Wendling    FuncAttrs.addAttribute(llvm::Attribute::OptimizeForSize);
10179046768d2c7e21a7933c8158664550a866fcdd74Quentin Colombet  if (CodeGenOpts.OptimizeSize == 2)
101872390b39c545426023ec104afe8706395d732badBill Wendling    FuncAttrs.addAttribute(llvm::Attribute::MinSize);
10192811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.DisableRedZone)
102072390b39c545426023ec104afe8706395d732badBill Wendling    FuncAttrs.addAttribute(llvm::Attribute::NoRedZone);
10212811ccf48d6d898c42cc4cfad37abedb36236d20Chandler Carruth  if (CodeGenOpts.NoImplicitFloat)
102272390b39c545426023ec104afe8706395d732badBill Wendling    FuncAttrs.addAttribute(llvm::Attribute::NoImplicitFloat);
102324095dad88dd9d48aa16afa6416417073af251b5Devang Patel
102493e4bff1a30d2ac37461943599efd59f04a5c00fBill Wendling  if (AttrOnCallSite) {
102593e4bff1a30d2ac37461943599efd59f04a5c00fBill Wendling    // Attributes that should go on the call site only.
102693e4bff1a30d2ac37461943599efd59f04a5c00fBill Wendling    if (!CodeGenOpts.SimplifyLibCalls)
102793e4bff1a30d2ac37461943599efd59f04a5c00fBill Wendling      FuncAttrs.addAttribute(llvm::Attribute::NoBuiltin);
1028be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling  } else {
1029be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling    // Attributes that should go on the function, but not the call site.
1030be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling    if (!CodeGenOpts.DisableFPElim) {
10314159f05999914fc1d342131f64c1c37f00059288Bill Wendling      FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
10324159f05999914fc1d342131f64c1c37f00059288Bill Wendling      FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf", "false");
1033be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling    } else if (CodeGenOpts.OmitLeafFramePointer) {
10344159f05999914fc1d342131f64c1c37f00059288Bill Wendling      FuncAttrs.addAttribute("no-frame-pointer-elim", "false");
10354159f05999914fc1d342131f64c1c37f00059288Bill Wendling      FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf", "true");
1036be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling    } else {
10374159f05999914fc1d342131f64c1c37f00059288Bill Wendling      FuncAttrs.addAttribute("no-frame-pointer-elim", "true");
10384159f05999914fc1d342131f64c1c37f00059288Bill Wendling      FuncAttrs.addAttribute("no-frame-pointer-elim-non-leaf", "true");
1039be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling    }
1040be9e8bfef6ee7f81ea30e9d358101b4f8d560c6bBill Wendling
10414159f05999914fc1d342131f64c1c37f00059288Bill Wendling    FuncAttrs.addAttribute("less-precise-fpmad",
10424159f05999914fc1d342131f64c1c37f00059288Bill Wendling                           CodeGenOpts.LessPreciseFPMAD ? "true" : "false");
10434159f05999914fc1d342131f64c1c37f00059288Bill Wendling    FuncAttrs.addAttribute("no-infs-fp-math",
10444159f05999914fc1d342131f64c1c37f00059288Bill Wendling                           CodeGenOpts.NoInfsFPMath ? "true" : "false");
10454159f05999914fc1d342131f64c1c37f00059288Bill Wendling    FuncAttrs.addAttribute("no-nans-fp-math",
10464159f05999914fc1d342131f64c1c37f00059288Bill Wendling                           CodeGenOpts.NoNaNsFPMath ? "true" : "false");
10474159f05999914fc1d342131f64c1c37f00059288Bill Wendling    FuncAttrs.addAttribute("unsafe-fp-math",
10484159f05999914fc1d342131f64c1c37f00059288Bill Wendling                           CodeGenOpts.UnsafeFPMath ? "true" : "false");
10494159f05999914fc1d342131f64c1c37f00059288Bill Wendling    FuncAttrs.addAttribute("use-soft-float",
10504159f05999914fc1d342131f64c1c37f00059288Bill Wendling                           CodeGenOpts.SoftFloat ? "true" : "false");
1051c0dcc2d7beaeb6a80c0c7d69086b4c8894b662a0Bill Wendling  }
1052c0dcc2d7beaeb6a80c0c7d69086b4c8894b662a0Bill Wendling
1053a0a99e02f5b2de3817706071077298ef040634feDaniel Dunbar  QualType RetTy = FI.getReturnType();
10545323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  unsigned Index = 1;
1055b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = FI.getReturnInfo();
105645c25ba11cbf8c9a461def5b03f6ee9481e06769Daniel Dunbar  switch (RetAI.getKind()) {
1057cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case ABIArgInfo::Extend:
10582eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   if (RetTy->hasSignedIntegerRepresentation())
105972390b39c545426023ec104afe8706395d732badBill Wendling     RetAttrs.addAttribute(llvm::Attribute::SExt);
10602eb9cdd5922c6fe48af185bee1988dabb5bd644eChris Lattner   else if (RetTy->hasUnsignedIntegerRepresentation())
106172390b39c545426023ec104afe8706395d732badBill Wendling     RetAttrs.addAttribute(llvm::Attribute::ZExt);
1062800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    break;
106346327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar  case ABIArgInfo::Direct:
1064800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
10652c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
10662c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1067b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  case ABIArgInfo::Indirect: {
10680d5833921cc728bc1d2e45fbaf7b3e11cddbf99dBill Wendling    llvm::AttrBuilder SRETAttrs;
106972390b39c545426023ec104afe8706395d732badBill Wendling    SRETAttrs.addAttribute(llvm::Attribute::StructRet);
1070b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    if (RetAI.getInReg())
107172390b39c545426023ec104afe8706395d732badBill Wendling      SRETAttrs.addAttribute(llvm::Attribute::InReg);
1072603571a2d6daca43162bf1d9469baf919697c00aBill Wendling    PAL.push_back(llvm::
1073b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                  AttributeSet::get(getLLVMContext(), Index, SRETAttrs));
1074b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
10755323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    ++Index;
10760ac86f0821a2ba7ae09793fee4451bef99e9b2f3Daniel Dunbar    // sret disables readnone and readonly
107772390b39c545426023ec104afe8706395d732badBill Wendling    FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
107872390b39c545426023ec104afe8706395d732badBill Wendling      .removeAttribute(llvm::Attribute::ReadNone);
10792c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar    break;
1080b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
10812c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
10828951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
1083b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid ABI kind for return argument");
10845323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
10852c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
1086603571a2d6daca43162bf1d9469baf919697c00aBill Wendling  if (RetAttrs.hasAttributes())
1087603571a2d6daca43162bf1d9469baf919697c00aBill Wendling    PAL.push_back(llvm::
1088b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                  AttributeSet::get(getLLVMContext(),
1089b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                                    llvm::AttributeSet::ReturnIndex,
1090b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                                    RetAttrs));
10911102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
109388c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar         ie = FI.arg_end(); it != ie; ++it) {
109488c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    QualType ParamType = it->type;
109588c2fa96be989571b4afb6229f0ef5a3ef4450cbDaniel Dunbar    const ABIArgInfo &AI = it->info;
10960d5833921cc728bc1d2e45fbaf7b3e11cddbf99dBill Wendling    llvm::AttrBuilder Attrs;
10971102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
1098e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    if (AI.getPaddingType()) {
1099b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling      if (AI.getPaddingInReg())
1100b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling        PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index,
1101b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                                              llvm::Attribute::InReg));
1102e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      // Increment Index if there is padding.
1103e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      ++Index;
1104e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    }
1105e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola
1106d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
1107d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall    // have the corresponding parameter variable.  It doesn't make
11087f6890ebb874cc16320259daef50f1b4cfdc47d5Daniel Dunbar    // sense to do it here because parameters are so messed up.
11098951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (AI.getKind()) {
1110cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
1111575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      if (ParamType->isSignedIntegerOrEnumerationType())
111272390b39c545426023ec104afe8706395d732badBill Wendling        Attrs.addAttribute(llvm::Attribute::SExt);
1113575a1c9dc8dc5b4977194993e289f9eda7295c39Douglas Gregor      else if (ParamType->isUnsignedIntegerOrEnumerationType())
111472390b39c545426023ec104afe8706395d732badBill Wendling        Attrs.addAttribute(llvm::Attribute::ZExt);
1115800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // FALL THROUGH
111646327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct:
1117b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      if (AI.getInReg())
111872390b39c545426023ec104afe8706395d732badBill Wendling        Attrs.addAttribute(llvm::Attribute::InReg);
1119b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
11201102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov      // FIXME: handle sseregparm someday...
11219cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
11222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      if (llvm::StructType *STy =
1123b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola          dyn_cast<llvm::StructType>(AI.getCoerceToType())) {
1124b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola        unsigned Extra = STy->getNumElements()-1;  // 1 will be added below.
1125603571a2d6daca43162bf1d9469baf919697c00aBill Wendling        if (Attrs.hasAttributes())
1126b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola          for (unsigned I = 0; I < Extra; ++I)
1127b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling            PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index + I,
1128b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                                                  Attrs));
1129b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola        Index += Extra;
1130b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      }
1131800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      break;
1132800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1133800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Indirect:
11340b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola      if (AI.getInReg())
113572390b39c545426023ec104afe8706395d732badBill Wendling        Attrs.addAttribute(llvm::Attribute::InReg);
11360b4cc950c54c8dd2de51587ef48446de670fa012Rafael Espindola
1137800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (AI.getIndirectByVal())
113872390b39c545426023ec104afe8706395d732badBill Wendling        Attrs.addAttribute(llvm::Attribute::ByVal);
1139603571a2d6daca43162bf1d9469baf919697c00aBill Wendling
1140603571a2d6daca43162bf1d9469baf919697c00aBill Wendling      Attrs.addAlignmentAttr(AI.getIndirectAlign());
1141800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1142800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // byval disables readnone and readonly.
114372390b39c545426023ec104afe8706395d732badBill Wendling      FuncAttrs.removeAttribute(llvm::Attribute::ReadOnly)
114472390b39c545426023ec104afe8706395d732badBill Wendling        .removeAttribute(llvm::Attribute::ReadNone);
11458951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar      break;
11461102f4282ceb430dbc9fcedb9dd2ad25898a09e8Anton Korobeynikov
114711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
114811434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      // Skip increment, no matching LLVM parameter.
11491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      continue;
115011434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
11515627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand: {
11525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<llvm::Type*, 8> types;
1153f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // FIXME: This is rather inefficient. Do we ever actually need to do
1154f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // anything here? The result should be just reconstructed on the other
1155f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump      // side, so extension should be a non-issue.
11569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      getTypes().GetExpandedTypes(ParamType, types);
115742e06119496e84e74cfc60230b18fcb53b35eb1cJohn McCall      Index += types.size();
11585627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      continue;
11595627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    }
11605323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar    }
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1162603571a2d6daca43162bf1d9469baf919697c00aBill Wendling    if (Attrs.hasAttributes())
1163b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling      PAL.push_back(llvm::AttributeSet::get(getLLVMContext(), Index, Attrs));
11645627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++Index;
11655323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar  }
1166603571a2d6daca43162bf1d9469baf919697c00aBill Wendling  if (FuncAttrs.hasAttributes())
116775d37b493e2282f993a19a166bd573b03a7535afBill Wendling    PAL.push_back(llvm::
1168b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                  AttributeSet::get(getLLVMContext(),
1169b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                                    llvm::AttributeSet::FunctionIndex,
1170b263bdf2954953cc7cca5d667eb01319ea0f72ecBill Wendling                                    FuncAttrs));
11715323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar}
11725323a4b0a1c248fa2ffdf886bb41a5d8fba71d2dDaniel Dunbar
1173d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// An argument came in as a promoted argument; demote it back to its
1174d26bc76c98006609002d9930f8840490e88ac5b5John McCall/// declared type.
1175d26bc76c98006609002d9930f8840490e88ac5b5John McCallstatic llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
1176d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         const VarDecl *var,
1177d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                         llvm::Value *value) {
11782acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *varType = CGF.ConvertType(var->getType());
1179d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1180d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // This can happen with promotions that actually don't change the
1181d26bc76c98006609002d9930f8840490e88ac5b5John McCall  // underlying type, like the enum promotions.
1182d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (value->getType() == varType) return value;
1183d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1184d26bc76c98006609002d9930f8840490e88ac5b5John McCall  assert((varType->isIntegerTy() || varType->isFloatingPointTy())
1185d26bc76c98006609002d9930f8840490e88ac5b5John McCall         && "unexpected promotion type");
1186d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1187d26bc76c98006609002d9930f8840490e88ac5b5John McCall  if (isa<llvm::IntegerType>(varType))
1188d26bc76c98006609002d9930f8840490e88ac5b5John McCall    return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
1189d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1190d26bc76c98006609002d9930f8840490e88ac5b5John McCall  return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
1191d26bc76c98006609002d9930f8840490e88ac5b5John McCall}
1192d26bc76c98006609002d9930f8840490e88ac5b5John McCall
119388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbarvoid CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
119488b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                                         llvm::Function *Fn,
119517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                                         const FunctionArgList &Args) {
11960cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // If this is an implicit-return-zero function, go ahead and
11970cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // initialize the return value.  TODO: it might be nice to have
11980cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // a more general mechanism for this that didn't require synthesized
11990cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  // return statements.
1200121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
12010cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    if (FD->hasImplicitReturnZero()) {
12020cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      QualType RetTy = FD->getResultType().getUnqualifiedType();
12032acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
1204c9c88b4159791c48e486ca94e3743b5979e2b7a6Owen Anderson      llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
12050cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall      Builder.CreateStore(Zero, ReturnValue);
12060cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall    }
12070cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall  }
12080cfeb63f532973777f6fe75d3468c3acad4adfe3John McCall
1209f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from FunctionArgList; lift up and
1210f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // simplify.
12115251afa21d3583f2740fd4f83659d008625a7260Daniel Dunbar
121217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
121317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  llvm::Function::arg_iterator AI = Fn->arg_begin();
12141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121517b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Name the struct return argument.
1216dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(FI)) {
121717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    AI->setName("agg.result");
121889530e45729d300b39c7332d2ad20c3ea476ab94Bill Wendling    AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
121989530e45729d300b39c7332d2ad20c3ea476ab94Bill Wendling                                        AI->getArgNo() + 1,
122089530e45729d300b39c7332d2ad20c3ea476ab94Bill Wendling                                        llvm::Attribute::NoAlias));
122117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    ++AI;
122217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12244b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(FI.arg_size() == Args.size() &&
12254b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
1226093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  unsigned ArgNo = 1;
1227b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
1228093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1229093ac461b37a573dcf226fa55faed96f318169b9Devang Patel       i != e; ++i, ++info_it, ++ArgNo) {
1230d26bc76c98006609002d9930f8840490e88ac5b5John McCall    const VarDecl *Arg = *i;
1231b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    QualType Ty = info_it->type;
1232b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgI = info_it->info;
12338951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1234d26bc76c98006609002d9930f8840490e88ac5b5John McCall    bool isPromoted =
1235d26bc76c98006609002d9930f8840490e88ac5b5John McCall      isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
1236d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1237e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    // Skip the dummy padding argument.
1238e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    if (ArgI.getPaddingType())
1239e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      ++AI;
1240e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola
12418951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    switch (ArgI.getKind()) {
12421f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    case ABIArgInfo::Indirect: {
1243ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      llvm::Value *V = AI;
1244cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
12459d232c884ea9872d6555df0fd7359699819bc1f1John McCall      if (!hasScalarEvaluationKind(Ty)) {
1246cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // Aggregates and complex variables are accessed by reference.  All we
1247cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        // need to do is realign the value, if requested
1248cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        if (ArgI.getIndirectRealign()) {
1249cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
1250cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar
1251cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // Copy from the incoming argument pointer to the temporary with the
1252cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // appropriate alignment.
1253cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          //
1254cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // FIXME: We should have a common utility for generating an aggregate
1255cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          // copy.
12562acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner          llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
1257fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck          CharUnits Size = getContext().getTypeSizeInChars(Ty);
1258c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
1259c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
1260c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi          Builder.CreateMemCpy(Dst,
1261c95a8fcb0a2f4148f1852c52c34ad3a1771d7e5dNAKAMURA Takumi                               Src,
1262fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                               llvm::ConstantInt::get(IntPtrTy,
1263fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck                                                      Size.getQuantity()),
12649f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               ArgI.getIndirectAlign(),
12659f0c7cc36d29cf591c33962931f5862847145f3eBenjamin Kramer                               false);
1266cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar          V = AlignedTemp;
1267cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar        }
12681f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
12691f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Load scalar value from indirect argument.
1270fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
1271fe71008c2764768f25478b16c1802755189ed7c9Ken Dyck        V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
1272d26bc76c98006609002d9930f8840490e88ac5b5John McCall
1273d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1274d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
12751f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
1276093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
12771f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
12781f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar    }
1279cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
1280cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    case ABIArgInfo::Extend:
128146327aaf031529be2cf8bb21bc76d7a5ae0d43cdDaniel Dunbar    case ABIArgInfo::Direct: {
12824ba3fd45bb134e4a5119f297537adce4e7ef4a39Akira Hatanaka
1283800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If we have the trivial case, handle it with no muss and fuss.
1284800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
1285117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getCoerceToType() == ConvertType(Ty) &&
1286117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgI.getDirectOffset() == 0) {
1287800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
1288800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *V = AI;
12899cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1290a6375560645177168099f1a1d96be8fa4718aa8eBill Wendling        if (Arg->getType().isRestrictQualified())
129189530e45729d300b39c7332d2ad20c3ea476ab94Bill Wendling          AI->addAttr(llvm::AttributeSet::get(getLLVMContext(),
129289530e45729d300b39c7332d2ad20c3ea476ab94Bill Wendling                                              AI->getArgNo() + 1,
129389530e45729d300b39c7332d2ad20c3ea476ab94Bill Wendling                                              llvm::Attribute::NoAlias));
1294d8e10d26b5a24257fe13c289b653fd450326eeffJohn McCall
1295b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner        // Ensure the argument is the correct type.
1296b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner        if (V->getType() != ArgI.getCoerceToType())
1297b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner          V = Builder.CreateBitCast(V, ArgI.getCoerceToType());
1298b13eab95e1a5bb1e78706179f15f8416e9fbcfbfChris Lattner
1299d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1300d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
13018b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola
13028b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola        // Because of merging of function types from multiple decls it is
13038b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola        // possible for the type of an argument to not match the corresponding
13048b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola        // type in the function type. Since we are codegening the callee
13058b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola        // in here, add a cast to the argument type.
13068b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola        llvm::Type *LTy = ConvertType(Arg->getType());
13078b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola        if (V->getType() != LTy)
13088b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola          V = Builder.CreateBitCast(V, LTy);
13098b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola
1310093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, V, ArgNo);
1311800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
131217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar      }
13131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1314a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov      llvm::AllocaInst *Alloca = CreateMemTemp(Ty, Arg->getName());
13159cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1316deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // The alignment we need to use is the max of the requested alignment for
1317deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      // the argument plus the alignment required by our access code below.
13189cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      unsigned AlignmentToUse =
131925a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        CGM.getDataLayout().getABITypeAlignment(ArgI.getCoerceToType());
1320deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      AlignmentToUse = std::max(AlignmentToUse,
1321deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner                        (unsigned)getContext().getDeclAlign(Arg).getQuantity());
13229cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1323deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner      Alloca->setAlignment(AlignmentToUse);
1324121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner      llvm::Value *V = Alloca;
1325117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *Ptr = V;    // Pointer to store into.
13269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1327117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1328117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgI.getDirectOffset()) {
1329117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
1330117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
13319cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        Ptr = Builder.CreateBitCast(Ptr,
1332117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                          llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
1333117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
13349cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1335ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
1336ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
1337ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
1338a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov      llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgI.getCoerceToType());
1339a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov      if (STy && STy->getNumElements() > 1) {
134025a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(STy);
1341a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        llvm::Type *DstTy =
1342a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          cast<llvm::PointerType>(Ptr->getType())->getElementType();
134325a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(DstTy);
1344a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1345a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        if (SrcSize <= DstSize) {
1346a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
1347a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1348a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1349a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            assert(AI != Fn->arg_end() && "Argument mismatch!");
1350a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            AI->setName(Arg->getName() + ".coerce" + Twine(i));
1351a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
1352a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            Builder.CreateStore(AI++, EltPtr);
1353a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          }
1354a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov        } else {
1355a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          llvm::AllocaInst *TempAlloca =
1356a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            CreateTempAlloca(ArgI.getCoerceToType(), "coerce");
1357a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          TempAlloca->setAlignment(AlignmentToUse);
1358a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          llvm::Value *TempV = TempAlloca;
1359a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1360a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1361a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            assert(AI != Fn->arg_end() && "Argument mismatch!");
1362a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            AI->setName(Arg->getName() + ".coerce" + Twine(i));
1363a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            llvm::Value *EltPtr = Builder.CreateConstGEP2_32(TempV, 0, i);
1364a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov            Builder.CreateStore(AI++, EltPtr);
1365a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          }
1366a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov
1367a6ce20ea10b1788ed1f266d5809a7ac2bca7bf1bEvgeniy Stepanov          Builder.CreateMemCpy(Ptr, TempV, DstSize, AlignmentToUse);
1368ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        }
1369ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
1370309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // Simple case, just do a coerced store of the argument into the alloca.
1371ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner        assert(AI != Fn->arg_end() && "Argument mismatch!");
1372225e286110bcc8b7b1ff8b35f0d51a10a158b18cChris Lattner        AI->setName(Arg->getName() + ".coerce");
1373117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
1374ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
13759cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
13769cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
137789c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // Match to what EmitParmDecl is expecting for this type.
13789d232c884ea9872d6555df0fd7359699819bc1f1John McCall      if (CodeGenFunction::hasScalarEvaluationKind(Ty)) {
137991a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar        V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
1380d26bc76c98006609002d9930f8840490e88ac5b5John McCall        if (isPromoted)
1381d26bc76c98006609002d9930f8840490e88ac5b5John McCall          V = emitArgumentDemotion(*this, Arg, V);
13828b29a387788bbb7a7c3b64c37473bc46299d2132Daniel Dunbar      }
1383093ac461b37a573dcf226fa55faed96f318169b9Devang Patel      EmitParmDecl(*Arg, V, ArgNo);
1384ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      continue;  // Skip ++AI increment, already done.
138589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
1386800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1387800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Expand: {
1388800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // If this structure was expanded into multiple arguments then
1389800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // we need to create a temporary and reconstruct it from the
1390800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // arguments.
13911bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman      llvm::AllocaInst *Alloca = CreateMemTemp(Ty);
13926da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      CharUnits Align = getContext().getDeclAlign(Arg);
13936da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      Alloca->setAlignment(Align.getQuantity());
13946da2c716017d5c8530ec99779524491ebc5dadb8Eli Friedman      LValue LV = MakeAddrLValue(Alloca, Ty, Align);
13951bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman      llvm::Function::arg_iterator End = ExpandTypeFromArgs(Ty, LV, AI);
13961bb94a417a53a524784ec98f232acc70e62370b2Eli Friedman      EmitParmDecl(*Arg, Alloca, ArgNo);
1397800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1398800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Name the arguments used in expansion and increment AI.
1399800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      unsigned Index = 0;
1400800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      for (; AI != End; ++AI, ++Index)
14015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        AI->setName(Arg->getName() + "." + Twine(Index));
1402800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
1403800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
1404800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1405800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Ignore:
1406800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Initialize the local variable appropriately.
14079d232c884ea9872d6555df0fd7359699819bc1f1John McCall      if (!hasScalarEvaluationKind(Ty))
1408093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1409800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      else
1410093ac461b37a573dcf226fa55faed96f318169b9Devang Patel        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1411093ac461b37a573dcf226fa55faed96f318169b9Devang Patel                     ArgNo);
1412800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner
1413800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // Skip increment, no matching LLVM parameter.
1414800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      continue;
14158951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar    }
14165627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
14175627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    ++AI;
141817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
141917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  assert(AI == Fn->arg_end() && "Argument mismatch!");
142017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
142117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
142277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCallstatic void eraseUnusedBitCasts(llvm::Instruction *insn) {
142377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  while (insn->use_empty()) {
142477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(insn);
142577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    if (!bitcast) return;
142677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
142777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    // This is "safe" because we would have used a ConstantExpr otherwise.
142877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    insn = cast<llvm::Instruction>(bitcast->getOperand(0));
142977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    bitcast->eraseFromParent();
143077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  }
143177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall}
143277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
1433f85e193739c953358c865005855253af4f68a497John McCall/// Try to emit a fused autorelease of a return result.
1434f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
1435f85e193739c953358c865005855253af4f68a497John McCall                                                    llvm::Value *result) {
1436f85e193739c953358c865005855253af4f68a497John McCall  // We must be immediately followed the cast.
1437f85e193739c953358c865005855253af4f68a497John McCall  llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
1438f85e193739c953358c865005855253af4f68a497John McCall  if (BB->empty()) return 0;
1439f85e193739c953358c865005855253af4f68a497John McCall  if (&BB->back() != result) return 0;
1440f85e193739c953358c865005855253af4f68a497John McCall
14412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *resultType = result->getType();
1442f85e193739c953358c865005855253af4f68a497John McCall
1443f85e193739c953358c865005855253af4f68a497John McCall  // result is in a BasicBlock and is therefore an Instruction.
1444f85e193739c953358c865005855253af4f68a497John McCall  llvm::Instruction *generator = cast<llvm::Instruction>(result);
1445f85e193739c953358c865005855253af4f68a497John McCall
14465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Instruction*,4> insnsToKill;
1447f85e193739c953358c865005855253af4f68a497John McCall
1448f85e193739c953358c865005855253af4f68a497John McCall  // Look for:
1449f85e193739c953358c865005855253af4f68a497John McCall  //  %generator = bitcast %type1* %generator2 to %type2*
1450f85e193739c953358c865005855253af4f68a497John McCall  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
1451f85e193739c953358c865005855253af4f68a497John McCall    // We would have emitted this as a constant if the operand weren't
1452f85e193739c953358c865005855253af4f68a497John McCall    // an Instruction.
1453f85e193739c953358c865005855253af4f68a497John McCall    generator = cast<llvm::Instruction>(bitcast->getOperand(0));
1454f85e193739c953358c865005855253af4f68a497John McCall
1455f85e193739c953358c865005855253af4f68a497John McCall    // Require the generator to be immediately followed by the cast.
1456f85e193739c953358c865005855253af4f68a497John McCall    if (generator->getNextNode() != bitcast)
1457f85e193739c953358c865005855253af4f68a497John McCall      return 0;
1458f85e193739c953358c865005855253af4f68a497John McCall
1459f85e193739c953358c865005855253af4f68a497John McCall    insnsToKill.push_back(bitcast);
1460f85e193739c953358c865005855253af4f68a497John McCall  }
1461f85e193739c953358c865005855253af4f68a497John McCall
1462f85e193739c953358c865005855253af4f68a497John McCall  // Look for:
1463f85e193739c953358c865005855253af4f68a497John McCall  //   %generator = call i8* @objc_retain(i8* %originalResult)
1464f85e193739c953358c865005855253af4f68a497John McCall  // or
1465f85e193739c953358c865005855253af4f68a497John McCall  //   %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
1466f85e193739c953358c865005855253af4f68a497John McCall  llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
1467f85e193739c953358c865005855253af4f68a497John McCall  if (!call) return 0;
1468f85e193739c953358c865005855253af4f68a497John McCall
1469f85e193739c953358c865005855253af4f68a497John McCall  bool doRetainAutorelease;
1470f85e193739c953358c865005855253af4f68a497John McCall
1471f85e193739c953358c865005855253af4f68a497John McCall  if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) {
1472f85e193739c953358c865005855253af4f68a497John McCall    doRetainAutorelease = true;
1473f85e193739c953358c865005855253af4f68a497John McCall  } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints()
1474f85e193739c953358c865005855253af4f68a497John McCall                                          .objc_retainAutoreleasedReturnValue) {
1475f85e193739c953358c865005855253af4f68a497John McCall    doRetainAutorelease = false;
1476f85e193739c953358c865005855253af4f68a497John McCall
1477f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall    // If we emitted an assembly marker for this call (and the
1478f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall    // ARCEntrypoints field should have been set if so), go looking
1479f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall    // for that call.  If we can't find it, we can't do this
1480f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall    // optimization.  But it should always be the immediately previous
1481f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall    // instruction, unless we needed bitcasts around the call.
1482f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall    if (CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker) {
1483f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall      llvm::Instruction *prev = call->getPrevNode();
1484f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall      assert(prev);
1485f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall      if (isa<llvm::BitCastInst>(prev)) {
1486f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall        prev = prev->getPrevNode();
1487f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall        assert(prev);
1488f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall      }
1489f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall      assert(isa<llvm::CallInst>(prev));
1490f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall      assert(cast<llvm::CallInst>(prev)->getCalledValue() ==
1491f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall               CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker);
1492f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall      insnsToKill.push_back(prev);
1493f9fdcc0531ca53651c1d7d0877290e232cb5468dJohn McCall    }
1494f85e193739c953358c865005855253af4f68a497John McCall  } else {
1495f85e193739c953358c865005855253af4f68a497John McCall    return 0;
1496f85e193739c953358c865005855253af4f68a497John McCall  }
1497f85e193739c953358c865005855253af4f68a497John McCall
1498f85e193739c953358c865005855253af4f68a497John McCall  result = call->getArgOperand(0);
1499f85e193739c953358c865005855253af4f68a497John McCall  insnsToKill.push_back(call);
1500f85e193739c953358c865005855253af4f68a497John McCall
1501f85e193739c953358c865005855253af4f68a497John McCall  // Keep killing bitcasts, for sanity.  Note that we no longer care
1502f85e193739c953358c865005855253af4f68a497John McCall  // about precise ordering as long as there's exactly one use.
1503f85e193739c953358c865005855253af4f68a497John McCall  while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
1504f85e193739c953358c865005855253af4f68a497John McCall    if (!bitcast->hasOneUse()) break;
1505f85e193739c953358c865005855253af4f68a497John McCall    insnsToKill.push_back(bitcast);
1506f85e193739c953358c865005855253af4f68a497John McCall    result = bitcast->getOperand(0);
1507f85e193739c953358c865005855253af4f68a497John McCall  }
1508f85e193739c953358c865005855253af4f68a497John McCall
1509f85e193739c953358c865005855253af4f68a497John McCall  // Delete all the unnecessary instructions, from latest to earliest.
15105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<llvm::Instruction*>::iterator
1511f85e193739c953358c865005855253af4f68a497John McCall         i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i)
1512f85e193739c953358c865005855253af4f68a497John McCall    (*i)->eraseFromParent();
1513f85e193739c953358c865005855253af4f68a497John McCall
1514f85e193739c953358c865005855253af4f68a497John McCall  // Do the fused retain/autorelease if we were asked to.
1515f85e193739c953358c865005855253af4f68a497John McCall  if (doRetainAutorelease)
1516f85e193739c953358c865005855253af4f68a497John McCall    result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
1517f85e193739c953358c865005855253af4f68a497John McCall
1518f85e193739c953358c865005855253af4f68a497John McCall  // Cast back to the result type.
1519f85e193739c953358c865005855253af4f68a497John McCall  return CGF.Builder.CreateBitCast(result, resultType);
1520f85e193739c953358c865005855253af4f68a497John McCall}
1521f85e193739c953358c865005855253af4f68a497John McCall
152277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall/// If this is a +1 of the value of an immutable 'self', remove it.
152377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCallstatic llvm::Value *tryRemoveRetainOfSelf(CodeGenFunction &CGF,
152477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall                                          llvm::Value *result) {
152577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // This is only applicable to a method with an immutable 'self'.
1526bd9b65ae534cb11aa39737aa43ab82bb29e078f6John McCall  const ObjCMethodDecl *method =
1527bd9b65ae534cb11aa39737aa43ab82bb29e078f6John McCall    dyn_cast_or_null<ObjCMethodDecl>(CGF.CurCodeDecl);
152877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!method) return 0;
152977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  const VarDecl *self = method->getSelfDecl();
153077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!self->getType().isConstQualified()) return 0;
153177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
153277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // Look for a retain call.
153377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::CallInst *retainCall =
153477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    dyn_cast<llvm::CallInst>(result->stripPointerCasts());
153577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!retainCall ||
153677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall      retainCall->getCalledValue() != CGF.CGM.getARCEntrypoints().objc_retain)
153777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    return 0;
153877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
153977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // Look for an ordinary load of 'self'.
154077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::Value *retainedValue = retainCall->getArgOperand(0);
154177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::LoadInst *load =
154277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    dyn_cast<llvm::LoadInst>(retainedValue->stripPointerCasts());
154377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (!load || load->isAtomic() || load->isVolatile() ||
154477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall      load->getPointerOperand() != CGF.GetAddrOfLocalVar(self))
154577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    return 0;
154677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
154777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // Okay!  Burn it all down.  This relies for correctness on the
154877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // assumption that the retain is emitted as part of the return and
154977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // that thereafter everything is used "linearly".
155077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  llvm::Type *resultType = result->getType();
155177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  eraseUnusedBitCasts(cast<llvm::Instruction>(result));
155277fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  assert(retainCall->use_empty());
155377fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  retainCall->eraseFromParent();
155477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  eraseUnusedBitCasts(cast<llvm::Instruction>(retainedValue));
155577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
155677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  return CGF.Builder.CreateBitCast(load, resultType);
155777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall}
155877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
1559f85e193739c953358c865005855253af4f68a497John McCall/// Emit an ARC autorelease of the result of a function.
156077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall///
156177fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall/// \return the value to actually return from the function
1562f85e193739c953358c865005855253af4f68a497John McCallstatic llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
1563f85e193739c953358c865005855253af4f68a497John McCall                                            llvm::Value *result) {
156477fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // If we're returning 'self', kill the initial retain.  This is a
156577fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // heuristic attempt to "encourage correctness" in the really unfortunate
156677fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // case where we have a return of self during a dealloc and we desperately
156777fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  // need to avoid the possible autorelease.
156877fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall  if (llvm::Value *self = tryRemoveRetainOfSelf(CGF, result))
156977fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall    return self;
157077fe6cd58f284bcc0130b8fd90018d7e5a960b56John McCall
1571f85e193739c953358c865005855253af4f68a497John McCall  // At -O0, try to emit a fused retain/autorelease.
1572f85e193739c953358c865005855253af4f68a497John McCall  if (CGF.shouldUseFusedARCCalls())
1573f85e193739c953358c865005855253af4f68a497John McCall    if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
1574f85e193739c953358c865005855253af4f68a497John McCall      return fused;
1575f85e193739c953358c865005855253af4f68a497John McCall
1576f85e193739c953358c865005855253af4f68a497John McCall  return CGF.EmitARCAutoreleaseReturnValue(result);
1577f85e193739c953358c865005855253af4f68a497John McCall}
1578f85e193739c953358c865005855253af4f68a497John McCall
1579f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall/// Heuristically search for a dominating store to the return-value slot.
1580f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCallstatic llvm::StoreInst *findDominatingStoreToReturnValue(CodeGenFunction &CGF) {
1581f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // If there are multiple uses of the return-value slot, just check
1582f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // for something immediately preceding the IP.  Sometimes this can
1583f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // happen with how we generate implicit-returns; it can also happen
1584f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // with noreturn cleanups.
1585f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  if (!CGF.ReturnValue->hasOneUse()) {
1586f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1587f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (IP->empty()) return 0;
1588f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    llvm::StoreInst *store = dyn_cast<llvm::StoreInst>(&IP->back());
1589f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (!store) return 0;
1590f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (store->getPointerOperand() != CGF.ReturnValue) return 0;
1591f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    assert(!store->isAtomic() && !store->isVolatile()); // see below
1592f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    return store;
1593f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  }
1594f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1595f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  llvm::StoreInst *store =
1596f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    dyn_cast<llvm::StoreInst>(CGF.ReturnValue->use_back());
1597f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  if (!store) return 0;
1598f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1599f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // These aren't actually possible for non-coerced returns, and we
1600f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // only care about non-coerced returns on this code path.
1601f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  assert(!store->isAtomic() && !store->isVolatile());
1602f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1603f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // Now do a first-and-dirty dominance check: just walk up the
1604f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // single-predecessors chain from the current insertion point.
1605f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  llvm::BasicBlock *StoreBB = store->getParent();
1606f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  llvm::BasicBlock *IP = CGF.Builder.GetInsertBlock();
1607f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  while (IP != StoreBB) {
1608f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall    if (!(IP = IP->getSinglePredecessor()))
1609f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      return 0;
1610f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  }
1611f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1612f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // Okay, the store's basic block dominates the insertion point; we
1613f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  // can do our thing.
1614f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall  return store;
1615f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall}
1616f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
161735b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattnervoid CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
16182c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  // Functions with no result always return void.
1619c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  if (ReturnValue == 0) {
1620c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    Builder.CreateRetVoid();
1621c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    return;
1622c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  }
162321fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar
16244751a53c5e5fed4bf2271e29cae7411c93a77df7Dan Gohman  llvm::DebugLoc RetDbgLoc;
1625c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  llvm::Value *RV = 0;
1626c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  QualType RetTy = FI.getReturnType();
1627c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  const ABIArgInfo &RetAI = FI.getReturnInfo();
1628cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov
1629c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  switch (RetAI.getKind()) {
163091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  case ABIArgInfo::Indirect: {
16319d232c884ea9872d6555df0fd7359699819bc1f1John McCall    switch (getEvaluationKind(RetTy)) {
16329d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Complex: {
16339d232c884ea9872d6555df0fd7359699819bc1f1John McCall      ComplexPairTy RT =
16349d232c884ea9872d6555df0fd7359699819bc1f1John McCall        EmitLoadOfComplex(MakeNaturalAlignAddrLValue(ReturnValue, RetTy));
16359d232c884ea9872d6555df0fd7359699819bc1f1John McCall      EmitStoreOfComplex(RT,
16369d232c884ea9872d6555df0fd7359699819bc1f1John McCall                       MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy),
16379d232c884ea9872d6555df0fd7359699819bc1f1John McCall                         /*isInit*/ true);
16389d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
16399d232c884ea9872d6555df0fd7359699819bc1f1John McCall    }
16409d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Aggregate:
1641c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner      // Do nothing; aggregrates get evaluated directly into the destination.
16429d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
16439d232c884ea9872d6555df0fd7359699819bc1f1John McCall    case TEK_Scalar:
16449d232c884ea9872d6555df0fd7359699819bc1f1John McCall      EmitStoreOfScalar(Builder.CreateLoad(ReturnValue),
16459d232c884ea9872d6555df0fd7359699819bc1f1John McCall                        MakeNaturalAlignAddrLValue(CurFn->arg_begin(), RetTy),
16469d232c884ea9872d6555df0fd7359699819bc1f1John McCall                        /*isInit*/ true);
16479d232c884ea9872d6555df0fd7359699819bc1f1John McCall      break;
1648c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    }
1649c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
165091a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar  }
16518951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1652c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Extend:
1653800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct:
1654117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1655117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        RetAI.getDirectOffset() == 0) {
1656800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // The internal return value temp always will have pointer-to-return-type
1657800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      // type, just do a load.
16589cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1659f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      // If there is a dominating store to ReturnValue, we can elide
1660f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      // the load, zap the store, and usually zap the alloca.
1661f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      if (llvm::StoreInst *SI = findDominatingStoreToReturnValue(*this)) {
1662800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // Get the stored value and nuke the now-dead store.
1663800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RetDbgLoc = SI->getDebugLoc();
1664800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        RV = SI->getValueOperand();
1665800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        SI->eraseFromParent();
16669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1667800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        // If that was the only use of the return value, nuke it as well now.
1668800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1669800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1670800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          ReturnValue = 0;
1671800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
1672f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall
1673f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      // Otherwise, we have to do a simple load.
1674f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall      } else {
1675f48f79636d5506d15784c2c2fa8a02086adda40aJohn McCall        RV = Builder.CreateLoad(ReturnValue);
167635b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner      }
1677800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    } else {
1678117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      llvm::Value *V = ReturnValue;
1679117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
1680117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = RetAI.getDirectOffset()) {
1681117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1682117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        V = Builder.CreateConstGEP1_32(V, Offs);
16839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        V = Builder.CreateBitCast(V,
1684117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1685117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
16869cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1687117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
168835b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner    }
1689f85e193739c953358c865005855253af4f68a497John McCall
1690f85e193739c953358c865005855253af4f68a497John McCall    // In ARC, end functions that return a retainable type with a call
1691f85e193739c953358c865005855253af4f68a497John McCall    // to objc_autoreleaseReturnValue.
1692f85e193739c953358c865005855253af4f68a497John McCall    if (AutoreleaseResult) {
16934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      assert(getLangOpts().ObjCAutoRefCount &&
1694f85e193739c953358c865005855253af4f68a497John McCall             !FI.isReturnsRetained() &&
1695f85e193739c953358c865005855253af4f68a497John McCall             RetTy->isObjCRetainableType());
1696f85e193739c953358c865005855253af4f68a497John McCall      RV = emitAutoreleaseOfResult(*this, RV);
1697f85e193739c953358c865005855253af4f68a497John McCall    }
1698f85e193739c953358c865005855253af4f68a497John McCall
1699c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1701800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Ignore:
1702c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner    break;
17038951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
1704c6e6dd2611bccd0d4df1f83a92bebb9b5d139b7dChris Lattner  case ABIArgInfo::Expand:
1705b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid ABI kind for return argument");
170617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
17071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
170821fcc8f01246b91dbef65e7af85f2f0947758a00Daniel Dunbar  llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1709d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel  if (!RetDbgLoc.isUnknown())
1710d3f265d68b2609a29acddb6de0b7e3b4dbe16204Devang Patel    Ret->setDebugLoc(RetDbgLoc);
171117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
171217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1713413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1714413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                          const VarDecl *param) {
17152736071ea3a46f90e65c93418961611d96c10ab9John McCall  // StartFunction converted the ABI-lowered parameter(s) into a
17162736071ea3a46f90e65c93418961611d96c10ab9John McCall  // local alloca.  We need to turn that into an r-value suitable
17172736071ea3a46f90e65c93418961611d96c10ab9John McCall  // for EmitCall.
1718413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  llvm::Value *local = GetAddrOfLocalVar(param);
17192736071ea3a46f90e65c93418961611d96c10ab9John McCall
1720413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  QualType type = param->getType();
17219cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
17222736071ea3a46f90e65c93418961611d96c10ab9John McCall  // For the most part, we just need to load the alloca, except:
17232736071ea3a46f90e65c93418961611d96c10ab9John McCall  // 1) aggregate r-values are actually pointers to temporaries, and
17249d232c884ea9872d6555df0fd7359699819bc1f1John McCall  // 2) references to non-scalars are pointers directly to the aggregate.
17259d232c884ea9872d6555df0fd7359699819bc1f1John McCall  // I don't know why references to scalars are different here.
1726413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
17279d232c884ea9872d6555df0fd7359699819bc1f1John McCall    if (!hasScalarEvaluationKind(ref->getPointeeType()))
1728413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall      return args.add(RValue::getAggregate(local), type);
17292736071ea3a46f90e65c93418961611d96c10ab9John McCall
17302736071ea3a46f90e65c93418961611d96c10ab9John McCall    // Locals which are references to scalars are represented
17312736071ea3a46f90e65c93418961611d96c10ab9John McCall    // with allocas holding the pointer.
1732413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(RValue::get(Builder.CreateLoad(local)), type);
17332736071ea3a46f90e65c93418961611d96c10ab9John McCall  }
17342736071ea3a46f90e65c93418961611d96c10ab9John McCall
17359d232c884ea9872d6555df0fd7359699819bc1f1John McCall  args.add(convertTempToRValue(local, type), type);
17362736071ea3a46f90e65c93418961611d96c10ab9John McCall}
17372736071ea3a46f90e65c93418961611d96c10ab9John McCall
1738f85e193739c953358c865005855253af4f68a497John McCallstatic bool isProvablyNull(llvm::Value *addr) {
1739f85e193739c953358c865005855253af4f68a497John McCall  return isa<llvm::ConstantPointerNull>(addr);
1740f85e193739c953358c865005855253af4f68a497John McCall}
1741f85e193739c953358c865005855253af4f68a497John McCall
1742f85e193739c953358c865005855253af4f68a497John McCallstatic bool isProvablyNonNull(llvm::Value *addr) {
1743f85e193739c953358c865005855253af4f68a497John McCall  return isa<llvm::AllocaInst>(addr);
1744f85e193739c953358c865005855253af4f68a497John McCall}
1745f85e193739c953358c865005855253af4f68a497John McCall
1746f85e193739c953358c865005855253af4f68a497John McCall/// Emit the actual writing-back of a writeback.
1747f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWriteback(CodeGenFunction &CGF,
1748f85e193739c953358c865005855253af4f68a497John McCall                          const CallArgList::Writeback &writeback) {
1749f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *srcAddr = writeback.Address;
1750f85e193739c953358c865005855253af4f68a497John McCall  assert(!isProvablyNull(srcAddr) &&
1751f85e193739c953358c865005855253af4f68a497John McCall         "shouldn't have writeback for provably null argument");
1752f85e193739c953358c865005855253af4f68a497John McCall
1753f85e193739c953358c865005855253af4f68a497John McCall  llvm::BasicBlock *contBB = 0;
1754f85e193739c953358c865005855253af4f68a497John McCall
1755f85e193739c953358c865005855253af4f68a497John McCall  // If the argument wasn't provably non-null, we need to null check
1756f85e193739c953358c865005855253af4f68a497John McCall  // before doing the store.
1757f85e193739c953358c865005855253af4f68a497John McCall  bool provablyNonNull = isProvablyNonNull(srcAddr);
1758f85e193739c953358c865005855253af4f68a497John McCall  if (!provablyNonNull) {
1759f85e193739c953358c865005855253af4f68a497John McCall    llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
1760f85e193739c953358c865005855253af4f68a497John McCall    contBB = CGF.createBasicBlock("icr.done");
1761f85e193739c953358c865005855253af4f68a497John McCall
1762f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1763f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
1764f85e193739c953358c865005855253af4f68a497John McCall    CGF.EmitBlock(writebackBB);
1765f85e193739c953358c865005855253af4f68a497John McCall  }
1766f85e193739c953358c865005855253af4f68a497John McCall
1767f85e193739c953358c865005855253af4f68a497John McCall  // Load the value to writeback.
1768f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
1769f85e193739c953358c865005855253af4f68a497John McCall
1770f85e193739c953358c865005855253af4f68a497John McCall  // Cast it back, in case we're writing an id to a Foo* or something.
1771f85e193739c953358c865005855253af4f68a497John McCall  value = CGF.Builder.CreateBitCast(value,
1772f85e193739c953358c865005855253af4f68a497John McCall               cast<llvm::PointerType>(srcAddr->getType())->getElementType(),
1773f85e193739c953358c865005855253af4f68a497John McCall                            "icr.writeback-cast");
1774f85e193739c953358c865005855253af4f68a497John McCall
1775f85e193739c953358c865005855253af4f68a497John McCall  // Perform the writeback.
1776f85e193739c953358c865005855253af4f68a497John McCall  QualType srcAddrType = writeback.AddressType;
1777f85e193739c953358c865005855253af4f68a497John McCall  CGF.EmitStoreThroughLValue(RValue::get(value),
1778545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall                             CGF.MakeAddrLValue(srcAddr, srcAddrType));
1779f85e193739c953358c865005855253af4f68a497John McCall
1780f85e193739c953358c865005855253af4f68a497John McCall  // Jump to the continuation block.
1781f85e193739c953358c865005855253af4f68a497John McCall  if (!provablyNonNull)
1782f85e193739c953358c865005855253af4f68a497John McCall    CGF.EmitBlock(contBB);
1783f85e193739c953358c865005855253af4f68a497John McCall}
1784f85e193739c953358c865005855253af4f68a497John McCall
1785f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWritebacks(CodeGenFunction &CGF,
1786f85e193739c953358c865005855253af4f68a497John McCall                           const CallArgList &args) {
1787f85e193739c953358c865005855253af4f68a497John McCall  for (CallArgList::writeback_iterator
1788f85e193739c953358c865005855253af4f68a497John McCall         i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i)
1789f85e193739c953358c865005855253af4f68a497John McCall    emitWriteback(CGF, *i);
1790f85e193739c953358c865005855253af4f68a497John McCall}
1791f85e193739c953358c865005855253af4f68a497John McCall
1792f85e193739c953358c865005855253af4f68a497John McCall/// Emit an argument that's being passed call-by-writeback.  That is,
1793f85e193739c953358c865005855253af4f68a497John McCall/// we are passing the address of
1794f85e193739c953358c865005855253af4f68a497John McCallstatic void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
1795f85e193739c953358c865005855253af4f68a497John McCall                             const ObjCIndirectCopyRestoreExpr *CRE) {
1796f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr());
1797f85e193739c953358c865005855253af4f68a497John McCall
1798f85e193739c953358c865005855253af4f68a497John McCall  // The dest and src types don't necessarily match in LLVM terms
1799f85e193739c953358c865005855253af4f68a497John McCall  // because of the crazy ObjC compatibility rules.
1800f85e193739c953358c865005855253af4f68a497John McCall
18012acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::PointerType *destType =
1802f85e193739c953358c865005855253af4f68a497John McCall    cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
1803f85e193739c953358c865005855253af4f68a497John McCall
1804f85e193739c953358c865005855253af4f68a497John McCall  // If the address is a constant null, just pass the appropriate null.
1805f85e193739c953358c865005855253af4f68a497John McCall  if (isProvablyNull(srcAddr)) {
1806f85e193739c953358c865005855253af4f68a497John McCall    args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
1807f85e193739c953358c865005855253af4f68a497John McCall             CRE->getType());
1808f85e193739c953358c865005855253af4f68a497John McCall    return;
1809f85e193739c953358c865005855253af4f68a497John McCall  }
1810f85e193739c953358c865005855253af4f68a497John McCall
1811f85e193739c953358c865005855253af4f68a497John McCall  QualType srcAddrType =
1812f85e193739c953358c865005855253af4f68a497John McCall    CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
1813f85e193739c953358c865005855253af4f68a497John McCall
1814f85e193739c953358c865005855253af4f68a497John McCall  // Create the temporary.
1815f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(),
1816f85e193739c953358c865005855253af4f68a497John McCall                                           "icr.temp");
181782c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian  // Loading an l-value can introduce a cleanup if the l-value is __weak,
181882c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian  // and that cleanup will be conditional if we can't prove that the l-value
181982c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian  // isn't null, so we need to register a dominating point so that the cleanups
182082c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian  // system will make valid IR.
182182c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian  CodeGenFunction::ConditionalEvaluation condEval(CGF);
182282c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian
1823f85e193739c953358c865005855253af4f68a497John McCall  // Zero-initialize it if we're not doing a copy-initialization.
1824f85e193739c953358c865005855253af4f68a497John McCall  bool shouldCopy = CRE->shouldCopy();
1825f85e193739c953358c865005855253af4f68a497John McCall  if (!shouldCopy) {
1826f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *null =
1827f85e193739c953358c865005855253af4f68a497John McCall      llvm::ConstantPointerNull::get(
1828f85e193739c953358c865005855253af4f68a497John McCall        cast<llvm::PointerType>(destType->getElementType()));
1829f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.CreateStore(null, temp);
1830f85e193739c953358c865005855253af4f68a497John McCall  }
183182c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian
1832f85e193739c953358c865005855253af4f68a497John McCall  llvm::BasicBlock *contBB = 0;
1833f85e193739c953358c865005855253af4f68a497John McCall
1834f85e193739c953358c865005855253af4f68a497John McCall  // If the address is *not* known to be non-null, we need to switch.
1835f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *finalArgument;
1836f85e193739c953358c865005855253af4f68a497John McCall
1837f85e193739c953358c865005855253af4f68a497John McCall  bool provablyNonNull = isProvablyNonNull(srcAddr);
1838f85e193739c953358c865005855253af4f68a497John McCall  if (provablyNonNull) {
1839f85e193739c953358c865005855253af4f68a497John McCall    finalArgument = temp;
1840f85e193739c953358c865005855253af4f68a497John McCall  } else {
1841f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1842f85e193739c953358c865005855253af4f68a497John McCall
1843f85e193739c953358c865005855253af4f68a497John McCall    finalArgument = CGF.Builder.CreateSelect(isNull,
1844f85e193739c953358c865005855253af4f68a497John McCall                                   llvm::ConstantPointerNull::get(destType),
1845f85e193739c953358c865005855253af4f68a497John McCall                                             temp, "icr.argument");
1846f85e193739c953358c865005855253af4f68a497John McCall
1847f85e193739c953358c865005855253af4f68a497John McCall    // If we need to copy, then the load has to be conditional, which
1848f85e193739c953358c865005855253af4f68a497John McCall    // means we need control flow.
1849f85e193739c953358c865005855253af4f68a497John McCall    if (shouldCopy) {
1850f85e193739c953358c865005855253af4f68a497John McCall      contBB = CGF.createBasicBlock("icr.cont");
1851f85e193739c953358c865005855253af4f68a497John McCall      llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
1852f85e193739c953358c865005855253af4f68a497John McCall      CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
1853f85e193739c953358c865005855253af4f68a497John McCall      CGF.EmitBlock(copyBB);
185482c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian      condEval.begin(CGF);
1855f85e193739c953358c865005855253af4f68a497John McCall    }
1856f85e193739c953358c865005855253af4f68a497John McCall  }
1857f85e193739c953358c865005855253af4f68a497John McCall
1858f85e193739c953358c865005855253af4f68a497John McCall  // Perform a copy if necessary.
1859f85e193739c953358c865005855253af4f68a497John McCall  if (shouldCopy) {
1860f85e193739c953358c865005855253af4f68a497John McCall    LValue srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
1861545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall    RValue srcRV = CGF.EmitLoadOfLValue(srcLV);
1862f85e193739c953358c865005855253af4f68a497John McCall    assert(srcRV.isScalar());
1863f85e193739c953358c865005855253af4f68a497John McCall
1864f85e193739c953358c865005855253af4f68a497John McCall    llvm::Value *src = srcRV.getScalarVal();
1865f85e193739c953358c865005855253af4f68a497John McCall    src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
1866f85e193739c953358c865005855253af4f68a497John McCall                                    "icr.cast");
1867f85e193739c953358c865005855253af4f68a497John McCall
1868f85e193739c953358c865005855253af4f68a497John McCall    // Use an ordinary store, not a store-to-lvalue.
1869f85e193739c953358c865005855253af4f68a497John McCall    CGF.Builder.CreateStore(src, temp);
1870f85e193739c953358c865005855253af4f68a497John McCall  }
187182c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian
1872f85e193739c953358c865005855253af4f68a497John McCall  // Finish the control flow if we needed it.
187382c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian  if (shouldCopy && !provablyNonNull) {
1874f85e193739c953358c865005855253af4f68a497John McCall    CGF.EmitBlock(contBB);
187582c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian    condEval.end(CGF);
187682c458ea76bf8f0981e3d1b5070c0b0e5878d784Fariborz Jahanian  }
1877f85e193739c953358c865005855253af4f68a497John McCall
1878f85e193739c953358c865005855253af4f68a497John McCall  args.addWriteback(srcAddr, srcAddrType, temp);
1879f85e193739c953358c865005855253af4f68a497John McCall  args.add(RValue::get(finalArgument), CRE->getType());
1880f85e193739c953358c865005855253af4f68a497John McCall}
1881f85e193739c953358c865005855253af4f68a497John McCall
1882413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCallvoid CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1883413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                                  QualType type) {
1884f85e193739c953358c865005855253af4f68a497John McCall  if (const ObjCIndirectCopyRestoreExpr *CRE
1885f85e193739c953358c865005855253af4f68a497John McCall        = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
18867edf9e38b91917b661277601c0e448eef0eb2b56Richard Smith    assert(getLangOpts().ObjCAutoRefCount);
1887f85e193739c953358c865005855253af4f68a497John McCall    assert(getContext().hasSameType(E->getType(), type));
1888f85e193739c953358c865005855253af4f68a497John McCall    return emitWritebackArg(*this, args, CRE);
1889f85e193739c953358c865005855253af4f68a497John McCall  }
1890f85e193739c953358c865005855253af4f68a497John McCall
18918affed5107ea79bf4d429770d2873e9c7288255eJohn McCall  assert(type->isReferenceType() == E->isGLValue() &&
18928affed5107ea79bf4d429770d2873e9c7288255eJohn McCall         "reference binding to unmaterialized r-value!");
18938affed5107ea79bf4d429770d2873e9c7288255eJohn McCall
1894cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall  if (E->isGLValue()) {
1895cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall    assert(E->getObjectKind() == OK_Ordinary);
1896413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1897413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall                    type);
1898cec52f0623d57f090e3477941acebe4932fa7abdJohn McCall  }
18991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19009d232c884ea9872d6555df0fd7359699819bc1f1John McCall  if (hasAggregateEvaluationKind(type) &&
190170cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman      isa<ImplicitCastExpr>(E) &&
190255d484802f3e27930317739efc5f5956b78aac25Eli Friedman      cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
190355d484802f3e27930317739efc5f5956b78aac25Eli Friedman    LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
190455d484802f3e27930317739efc5f5956b78aac25Eli Friedman    assert(L.isSimple());
190551f512090530807e2c80f9411cc262025820c859Eli Friedman    args.add(L.asAggregateRValue(), type, /*NeedsCopy*/true);
190655d484802f3e27930317739efc5f5956b78aac25Eli Friedman    return;
190755d484802f3e27930317739efc5f5956b78aac25Eli Friedman  }
190855d484802f3e27930317739efc5f5956b78aac25Eli Friedman
1909413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  args.add(EmitAnyExprToTemp(E), type);
19100139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson}
19110139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson
1912b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman// In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
1913b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman// optimizer it can aggressively ignore unwind edges.
1914b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohmanvoid
1915b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan GohmanCodeGenFunction::AddObjCARCExceptionMetadata(llvm::Instruction *Inst) {
1916b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  if (CGM.getCodeGenOpts().OptimizationLevel != 0 &&
1917b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman      !CGM.getCodeGenOpts().ObjCAutoRefCountExceptions)
1918b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    Inst->setMetadata("clang.arc.no_objc_arc_exceptions",
1919b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman                      CGM.getNoObjCARCExceptionsMetadata());
1920b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman}
1921b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
1922bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Emits a call to the given no-arguments nounwind runtime function.
1923bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallInst *
1924bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallCodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
1925bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         const llvm::Twine &name) {
1926bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return EmitNounwindRuntimeCall(callee, ArrayRef<llvm::Value*>(), name);
1927bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
1928bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
1929bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Emits a call to the given nounwind runtime function.
1930bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallInst *
1931bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallCodeGenFunction::EmitNounwindRuntimeCall(llvm::Value *callee,
1932bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         ArrayRef<llvm::Value*> args,
1933bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         const llvm::Twine &name) {
1934bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *call = EmitRuntimeCall(callee, args, name);
1935bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  call->setDoesNotThrow();
1936bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return call;
1937bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
1938bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
1939bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Emits a simple call (never an invoke) to the given no-arguments
1940bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// runtime function.
1941bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallInst *
1942bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallCodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
1943bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                 const llvm::Twine &name) {
1944bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return EmitRuntimeCall(callee, ArrayRef<llvm::Value*>(), name);
1945bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
1946bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
1947bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Emits a simple call (never an invoke) to the given runtime
1948bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// function.
1949bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallInst *
1950bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallCodeGenFunction::EmitRuntimeCall(llvm::Value *callee,
1951bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                 ArrayRef<llvm::Value*> args,
1952bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                 const llvm::Twine &name) {
1953bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallInst *call = Builder.CreateCall(callee, args, name);
1954bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  call->setCallingConv(getRuntimeCC());
1955bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return call;
1956bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
1957bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
1958bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Emits a call or invoke to the given noreturn runtime function.
1959bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallvoid CodeGenFunction::EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
1960bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                               ArrayRef<llvm::Value*> args) {
1961bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  if (getInvokeDest()) {
1962bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    llvm::InvokeInst *invoke =
1963bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall      Builder.CreateInvoke(callee,
1964bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                           getUnreachableBlock(),
1965bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                           getInvokeDest(),
1966bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                           args);
1967bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    invoke->setDoesNotReturn();
1968bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    invoke->setCallingConv(getRuntimeCC());
1969bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  } else {
1970bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    llvm::CallInst *call = Builder.CreateCall(callee, args);
1971bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    call->setDoesNotReturn();
1972bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    call->setCallingConv(getRuntimeCC());
1973bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall    Builder.CreateUnreachable();
1974bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  }
1975bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
1976bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
1977bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Emits a call or invoke instruction to the given nullary runtime
1978bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// function.
1979bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallSite
1980bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallCodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
1981bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         const Twine &name) {
1982bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return EmitRuntimeCallOrInvoke(callee, ArrayRef<llvm::Value*>(), name);
1983bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
1984bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
1985bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall/// Emits a call or invoke instruction to the given runtime function.
1986bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallSite
1987bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallCodeGenFunction::EmitRuntimeCallOrInvoke(llvm::Value *callee,
1988bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         ArrayRef<llvm::Value*> args,
1989bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                         const Twine &name) {
1990bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  llvm::CallSite callSite = EmitCallOrInvoke(callee, args, name);
1991bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  callSite.setCallingConv(getRuntimeCC());
1992bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return callSite;
1993bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
1994bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
1995bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallllvm::CallSite
1996bd7370a78604e9a20d698bfe328c1e43f12a0613John McCallCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1997bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall                                  const Twine &Name) {
1998bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall  return EmitCallOrInvoke(Callee, ArrayRef<llvm::Value *>(), Name);
1999bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall}
2000bd7370a78604e9a20d698bfe328c1e43f12a0613John McCall
2001f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// Emits a call or invoke instruction to the given function, depending
2002f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// on the current state of the EH stack.
2003f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallllvm::CallSite
2004f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallCodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
20052d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris Lattner                                  ArrayRef<llvm::Value *> Args,
20065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  const Twine &Name) {
2007f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = getInvokeDest();
2008b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
2009b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  llvm::Instruction *Inst;
2010f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest)
2011b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    Inst = Builder.CreateCall(Callee, Args, Name);
2012b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  else {
2013b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
2014b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    Inst = Builder.CreateInvoke(Callee, ContBB, InvokeDest, Args, Name);
2015b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    EmitBlock(ContBB);
2016b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  }
2017b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
2018b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2019b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // optimizer it can aggressively ignore unwind edges.
20204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().ObjCAutoRefCount)
2021b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    AddObjCARCExceptionMetadata(Inst);
2022f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
2023b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  return Inst;
2024f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall}
2025f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
2026708554498595e047cc53e366c91cc063fcc1c5bcChris Lattnerstatic void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo,
2027708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner                            llvm::FunctionType *FTy) {
2028708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  if (ArgNo < FTy->getNumParams())
2029708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    assert(Elt->getType() == FTy->getParamType(ArgNo));
2030708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  else
2031708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    assert(FTy->isVarArg());
2032708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  ++ArgNo;
2033708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner}
2034708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2035811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattnervoid CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
20365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                       SmallVector<llvm::Value*,16> &Args,
2037811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner                                       llvm::FunctionType *IRFuncTy) {
2038194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2039194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    unsigned NumElts = AT->getSize().getZExtValue();
2040194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    QualType EltTy = AT->getElementType();
2041194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    llvm::Value *Addr = RV.getAggregateAddr();
2042194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
2043194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      llvm::Value *EltAddr = Builder.CreateConstGEP2_32(Addr, 0, Elt);
20449d232c884ea9872d6555df0fd7359699819bc1f1John McCall      RValue EltRV = convertTempToRValue(EltAddr, EltTy);
2045194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      ExpandTypeToArgs(EltTy, EltRV, Args, IRFuncTy);
2046811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    }
2047eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2048194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    RecordDecl *RD = RT->getDecl();
2049194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
2050377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman    LValue LV = MakeAddrLValue(RV.getAggregateAddr(), Ty);
2051eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2052eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->isUnion()) {
2053eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      const FieldDecl *LargestFD = 0;
2054eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      CharUnits UnionSize = CharUnits::Zero();
2055eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2056eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2057eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
2058581deb3da481053c4993c7600f97acf7768caac5David Blaikie        const FieldDecl *FD = *i;
2059eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        assert(!FD->isBitField() &&
2060eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov               "Cannot expand structure with bit-field members.");
2061eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        CharUnits FieldSize = getContext().getTypeSizeInChars(FD->getType());
2062eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        if (UnionSize < FieldSize) {
2063eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          UnionSize = FieldSize;
2064eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov          LargestFD = FD;
2065eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        }
2066eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
2067eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      if (LargestFD) {
2068377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        RValue FldRV = EmitRValueForField(LV, LargestFD);
2069eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        ExpandTypeToArgs(LargestFD->getType(), FldRV, Args, IRFuncTy);
2070eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
2071eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    } else {
2072eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2073eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov           i != e; ++i) {
2074581deb3da481053c4993c7600f97acf7768caac5David Blaikie        FieldDecl *FD = *i;
2075eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2076377ecc7996dce6803f7b7b6208cab5e197c9c5b8Eli Friedman        RValue FldRV = EmitRValueForField(LV, FD);
2077eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov        ExpandTypeToArgs(FD->getType(), FldRV, Args, IRFuncTy);
2078eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      }
2079194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2080ca3d3fcabaa0d7255e9a778ef468daa6e052b211Eli Friedman  } else if (Ty->isAnyComplexType()) {
2081194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    ComplexPairTy CV = RV.getComplexVal();
2082194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Args.push_back(CV.first);
2083194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Args.push_back(CV.second);
2084194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
2085811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    assert(RV.isScalar() &&
2086811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner           "Unexpected non-scalar rvalue during struct expansion.");
2087811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
2088811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    // Insert a bitcast as needed.
2089811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    llvm::Value *V = RV.getScalarVal();
2090811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    if (Args.size() < IRFuncTy->getNumParams() &&
2091811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner        V->getType() != IRFuncTy->getParamType(Args.size()))
2092811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner      V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size()));
2093811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
2094811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner    Args.push_back(V);
2095811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner  }
2096811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner}
2097811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
2098811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner
209988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel DunbarRValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
21001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 llvm::Value *Callee,
2101f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                                 ReturnValueSlot ReturnValue,
2102c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar                                 const CallArgList &CallArgs,
2103dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall                                 const Decl *TargetDecl,
21044b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall                                 llvm::Instruction **callOrInvoke) {
2105f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump  // FIXME: We no longer need the types from CallArgs; lift up and simplify.
21065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<llvm::Value*, 16> Args;
210717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
210817b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // Handle struct-return functions by passing a pointer to the
210917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  // location that we would like to return into.
2110bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  QualType RetTy = CallInfo.getReturnType();
2111b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
21121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2113708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  // IRArgNo - Keep track of the argument number in the callee we're looking at.
2114708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  unsigned IRArgNo = 0;
2115708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner  llvm::FunctionType *IRFuncTy =
2116708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    cast<llvm::FunctionType>(
2117708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner                  cast<llvm::PointerType>(Callee->getType())->getElementType());
21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21195db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the call returns a temporary with struct return, create a temporary
2120d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  // alloca to hold the result, unless one is given to us.
2121dacf9dda17346c628fdd8c5df53c681738db0dc5Daniel Dunbar  if (CGM.ReturnTypeUsesSRet(CallInfo)) {
2122d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *Value = ReturnValue.getValue();
2123d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!Value)
2124195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      Value = CreateMemTemp(RetTy);
2125d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    Args.push_back(Value);
2126708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner    checkArgMatches(Value, IRArgNo, IRFuncTy);
2127d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson  }
21281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21294b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar  assert(CallInfo.arg_size() == CallArgs.size() &&
21304b5f0a4bd6645e87e5feae4be4675ce87d97b4a5Daniel Dunbar         "Mismatch between function signature & arguments.");
2131b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
21321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
2133b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar       I != E; ++I, ++info_it) {
2134b225be44aaab85a603e80dbe0eb3d81638b20d23Daniel Dunbar    const ABIArgInfo &ArgInfo = info_it->info;
2135c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman    RValue RV = I->RV;
21365627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
21379d232c884ea9872d6555df0fd7359699819bc1f1John McCall    CharUnits TypeAlign = getContext().getTypeAlignInChars(I->Ty);
2138e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola
2139e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    // Insert a padding argument to ensure proper alignment.
2140e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    if (llvm::Type *PaddingType = ArgInfo.getPaddingType()) {
2141e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      Args.push_back(llvm::UndefValue::get(PaddingType));
2142e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola      ++IRArgNo;
2143e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola    }
2144e4aeeaae8ee93ad5e07c646046c650d594f2775eRafael Espindola
21455627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    switch (ArgInfo.getKind()) {
214691a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    case ABIArgInfo::Indirect: {
21471f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      if (RV.isScalar() || RV.isComplex()) {
21481f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        // Make a temporary alloca to pass the argument.
214970cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman        llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
215070cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman        if (ArgInfo.getIndirectAlign() > AI->getAlignment())
215170cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman          AI->setAlignment(ArgInfo.getIndirectAlign());
215270cbd2a2a07ff3109adb2d584f7ad4b5cce88af2Eli Friedman        Args.push_back(AI);
21539d232c884ea9872d6555df0fd7359699819bc1f1John McCall
21549d232c884ea9872d6555df0fd7359699819bc1f1John McCall        LValue argLV =
21559d232c884ea9872d6555df0fd7359699819bc1f1John McCall          MakeAddrLValue(Args.back(), I->Ty, TypeAlign);
2156708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
21571f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        if (RV.isScalar())
21589d232c884ea9872d6555df0fd7359699819bc1f1John McCall          EmitStoreOfScalar(RV.getScalarVal(), argLV, /*init*/ true);
21591f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar        else
21609d232c884ea9872d6555df0fd7359699819bc1f1John McCall          EmitStoreOfComplex(RV.getComplexVal(), argLV, /*init*/ true);
2161708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2162708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        // Validate argument match.
2163708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        checkArgMatches(AI, IRArgNo, IRFuncTy);
21641f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      } else {
2165ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        // We want to avoid creating an unnecessary temporary+copy here;
2166d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei        // however, we need one in three cases:
2167ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        // 1. If the argument is not byval, and we are required to copy the
2168ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        //    source.  (This case doesn't occur on any common architecture.)
2169ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        // 2. If the argument is byval, RV is not sufficiently aligned, and
2170ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        //    we cannot force it to be sufficiently aligned.
2171d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei        // 3. If the argument is byval, but RV is located in an address space
2172d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei        //    different than that of the argument (0).
217397cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        llvm::Value *Addr = RV.getAggregateAddr();
217497cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        unsigned Align = ArgInfo.getIndirectAlign();
217525a6a84cf5067b32c271e3ba078676dee838798dMicah Villmow        const llvm::DataLayout *TD = &CGM.getDataLayout();
2176d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei        const unsigned RVAddrSpace = Addr->getType()->getPointerAddressSpace();
2177d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei        const unsigned ArgAddrSpace = (IRArgNo < IRFuncTy->getNumParams() ?
2178d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei          IRFuncTy->getParamType(IRArgNo)->getPointerAddressSpace() : 0);
217997cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman        if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
21809d232c884ea9872d6555df0fd7359699819bc1f1John McCall            (ArgInfo.getIndirectByVal() && TypeAlign.getQuantity() < Align &&
2181d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei             llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align) ||
2182d436c99c47f7fca4e60077aa7e38cab7c1323f08Guy Benyei             (ArgInfo.getIndirectByVal() && (RVAddrSpace != ArgAddrSpace))) {
2183ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman          // Create an aligned temporary, and copy to it.
218497cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman          llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
218597cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman          if (Align > AI->getAlignment())
218697cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman            AI->setAlignment(Align);
2187ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman          Args.push_back(AI);
2188649b4a1a9b5e6f768ca0cb84bd97b00f51083e15Chad Rosier          EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
2189708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2190708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          // Validate argument match.
2191708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          checkArgMatches(AI, IRArgNo, IRFuncTy);
2192ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        } else {
2193ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman          // Skip the extra memcpy call.
219497cb5a4a21866610227963fc3dcce9d89b2f7990Eli Friedman          Args.push_back(Addr);
2195708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2196708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          // Validate argument match.
2197708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          checkArgMatches(Addr, IRArgNo, IRFuncTy);
2198ea5e4da116aae0eb932d32b73e7cec2f04932248Eli Friedman        }
21991f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      }
22001f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar      break;
220191a16fa3265686b90054715eea504d9b4a13438bDaniel Dunbar    }
22021f7459814ed78067a8f0db3e1082b45ec89c16abDaniel Dunbar
220311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar    case ABIArgInfo::Ignore:
220411434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar      break;
22059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2206800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Extend:
2207800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    case ABIArgInfo::Direct: {
2208800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
2209117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
2210117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner          ArgInfo.getDirectOffset() == 0) {
2211708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        llvm::Value *V;
2212800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (RV.isScalar())
2213708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          V = RV.getScalarVal();
2214800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        else
2215708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          V = Builder.CreateLoad(RV.getAggregateAddr());
2216708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
221721ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner        // If the argument doesn't match, perform a bitcast to coerce it.  This
221821ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner        // can happen due to trivial type mismatches.
221921ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner        if (IRArgNo < IRFuncTy->getNumParams() &&
222021ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner            V->getType() != IRFuncTy->getParamType(IRArgNo))
222121ca1fdb25c2bb98721e569aacd10e8b684dd51aChris Lattner          V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo));
2222708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        Args.push_back(V);
2223708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2224708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        checkArgMatches(V, IRArgNo, IRFuncTy);
2225800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        break;
2226800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
222711434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
222889c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      // FIXME: Avoid the conversion through memory if possible.
222989c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      llvm::Value *SrcPtr;
22309d232c884ea9872d6555df0fd7359699819bc1f1John McCall      if (RV.isScalar() || RV.isComplex()) {
2231c6d07821c529bb95e4cf072e49b736c5142f1786Eli Friedman        SrcPtr = CreateMemTemp(I->Ty, "coerce");
22329d232c884ea9872d6555df0fd7359699819bc1f1John McCall        LValue SrcLV = MakeAddrLValue(SrcPtr, I->Ty, TypeAlign);
22339d232c884ea9872d6555df0fd7359699819bc1f1John McCall        if (RV.isScalar()) {
22349d232c884ea9872d6555df0fd7359699819bc1f1John McCall          EmitStoreOfScalar(RV.getScalarVal(), SrcLV, /*init*/ true);
22359d232c884ea9872d6555df0fd7359699819bc1f1John McCall        } else {
22369d232c884ea9872d6555df0fd7359699819bc1f1John McCall          EmitStoreOfComplex(RV.getComplexVal(), SrcLV, /*init*/ true);
22379d232c884ea9872d6555df0fd7359699819bc1f1John McCall        }
22381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      } else
223989c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar        SrcPtr = RV.getAggregateAddr();
22409cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2241117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      // If the value is offset in memory, apply the offset now.
2242117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      if (unsigned Offs = ArgInfo.getDirectOffset()) {
2243117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
2244117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner        SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
22459cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer        SrcPtr = Builder.CreateBitCast(SrcPtr,
2246117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                       llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
2247117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2248117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      }
22499cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2250ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // If the coerce-to type is a first class aggregate, we flatten it and
2251ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // pass the elements. Either way is semantically identical, but fast-isel
2252ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      // and the optimizer generally likes scalar values better than FCAs.
22532acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      if (llvm::StructType *STy =
2254309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner            dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
2255f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        llvm::Type *SrcTy =
2256f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth          cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
2257f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy);
2258f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy);
2259f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth
2260f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        // If the source type is smaller than the destination type of the
2261f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        // coerce-to logic, copy the source value into a temp alloca the size
2262f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        // of the destination type to allow loading all of it. The bits past
2263f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        // the source value are left undef.
2264f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        if (SrcSize < DstSize) {
2265f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth          llvm::AllocaInst *TempAlloca
2266f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth            = CreateTempAlloca(STy, SrcPtr->getName() + ".coerce");
2267f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth          Builder.CreateMemCpy(TempAlloca, SrcPtr, SrcSize, 0);
2268f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth          SrcPtr = TempAlloca;
2269f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        } else {
2270f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth          SrcPtr = Builder.CreateBitCast(SrcPtr,
2271f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth                                         llvm::PointerType::getUnqual(STy));
2272f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth        }
2273f82232c8b73851337b83b954ba1292cf6475c7c5Chandler Carruth
22749282688a296b306c4ae2d115f55101647056d1daChris Lattner        for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
22759282688a296b306c4ae2d115f55101647056d1daChris Lattner          llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
2276deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
2277deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          // We don't know what we're loading from.
2278deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          LI->setAlignment(1);
2279deabde24e1712253e483a46d24c0e10f25ebba99Chris Lattner          Args.push_back(LI);
2280708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2281708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          // Validate argument match.
2282708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner          checkArgMatches(LI, IRArgNo, IRFuncTy);
2283309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        }
2284ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      } else {
2285309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        // In the simple case, just pass the coerced loaded value.
2286309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner        Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
2287309c59f6d3a4fd883fdf87334271df2c55338aaeChris Lattner                                         *this));
2288708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner
2289708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        // Validate argument match.
2290708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner        checkArgMatches(Args.back(), IRArgNo, IRFuncTy);
2291ce70016434ff82a29a60ef82894d934b8a23f23dChris Lattner      }
22929cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
229389c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar      break;
229489c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar    }
229589c9d8e7f0700d27b1d93dc3832eb1af9b92c221Daniel Dunbar
22965627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar    case ABIArgInfo::Expand:
2297811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner      ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy);
2298708554498595e047cc53e366c91cc063fcc1c5bcChris Lattner      IRArgNo = Args.size();
22995627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar      break;
230017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    }
230117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
23021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23035db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // If the callee is a bitcast of a function to a varargs pointer to function
23045db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // type, check to see if we can remove the bitcast.  This handles some cases
23055db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  // with unprototyped functions.
23065db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
23075db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
23082acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
23092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::FunctionType *CurFT =
23105db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        cast<llvm::FunctionType>(CurPT->getElementType());
23112acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner      llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
23121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23135db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      if (CE->getOpcode() == llvm::Instruction::BitCast &&
23145db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          ActualFT->getReturnType() == CurFT->getReturnType() &&
2315d6bebbfd510f3b495795b88aafd10ead3cb211e9Chris Lattner          ActualFT->getNumParams() == CurFT->getNumParams() &&
2316c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          ActualFT->getNumParams() == Args.size() &&
2317c0ddef23136368ce1bd882f7edd43591c8f30aa6Fariborz Jahanian          (CurFT->isVarArg() || !ActualFT->isVarArg())) {
23185db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        bool ArgsMatch = true;
23195db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
23205db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
23215db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            ArgsMatch = false;
23225db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner            break;
23235db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          }
23241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23255db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // Strip the cast if we can get away with it.  This is a nice cleanup,
23265db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // but also allows us to inline the function at -O0 if it is marked
23275db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        // always_inline.
23285db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner        if (ArgsMatch)
23295db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner          Callee = CalleeF;
23305db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner      }
23315db7ae59bb0115ccc420ff1d688abc8706559b57Chris Lattner    }
23321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2333ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  unsigned CallingConv;
2334761d7f78e2dac7ea5f35828c2271e60d91e106ceDevang Patel  CodeGen::AttributeListType AttributeList;
233594236e7393d5cd4acbc5f0b503d23bbe78d9aeecBill Wendling  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList,
233694236e7393d5cd4acbc5f0b503d23bbe78d9aeecBill Wendling                             CallingConv, true);
2337785b778203a474c6e4b9e17ae91cd2a358868877Bill Wendling  llvm::AttributeSet Attrs = llvm::AttributeSet::get(getLLVMContext(),
233894236e7393d5cd4acbc5f0b503d23bbe78d9aeecBill Wendling                                                     AttributeList);
23391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2340f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *InvokeDest = 0;
234101ad954459c9bdd21e55290859643e334e9c70f4Bill Wendling  if (!Attrs.hasAttribute(llvm::AttributeSet::FunctionIndex,
234201ad954459c9bdd21e55290859643e334e9c70f4Bill Wendling                          llvm::Attribute::NoUnwind))
2343f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    InvokeDest = getInvokeDest();
2344f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
2345d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::CallSite CS;
2346f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  if (!InvokeDest) {
23474c7d9f1507d0f102bd4133bba63348636facd469Jay Foad    CS = Builder.CreateCall(Callee, Args);
23489834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  } else {
23499834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
23504c7d9f1507d0f102bd4133bba63348636facd469Jay Foad    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest, Args);
23519834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar    EmitBlock(Cont);
2352f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar  }
2353ce93399f26f23735b8e291321f18ad54f64cb58aChris Lattner  if (callOrInvoke)
23544b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall    *callOrInvoke = CS.getInstruction();
2355f4fe0f082f21048c0777ad5aeac04a5a94db1f46Daniel Dunbar
2356d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  CS.setAttributes(Attrs);
2357ca6408c3176783f0b29da4679a08512aa05f0c73Daniel Dunbar  CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
2358d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
2359b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // In ObjC ARC mode with no ObjC ARC exception safety, tell the ARC
2360b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman  // optimizer it can aggressively ignore unwind edges.
23614e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (CGM.getLangOpts().ObjCAutoRefCount)
2362b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman    AddObjCARCExceptionMetadata(CS.getInstruction());
2363b49bd27b334a6c4e3bf9d810a7d5b022578f1194Dan Gohman
2364d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // If the call doesn't return, finish the basic block and clear the
2365d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // insertion point; this allows the rest of IRgen to discard
2366d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  // unreachable code.
2367d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  if (CS.doesNotReturn()) {
2368d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.CreateUnreachable();
2369d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    Builder.ClearInsertionPoint();
23701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2371f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // FIXME: For now, emit a dummy basic block because expr emitters in
2372f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // generally are not ready to handle emitting expressions at unreachable
2373f5408fe484495ee4efbdd709c8a2c2fdbbbdb328Mike Stump    // points.
2374d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    EnsureInsertPoint();
23751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2376d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    // Return a reasonable RValue.
2377d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar    return GetUndefRValue(RetTy);
23781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
2379d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar
2380d14151d5f52fa9048d41d7539299243e05755ec4Daniel Dunbar  llvm::Instruction *CI = CS.getInstruction();
2381ffbb15e54a6dc120087003d1e42448b8705bd58aBenjamin Kramer  if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
238217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar    CI->setName("call");
23832c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
2384f85e193739c953358c865005855253af4f68a497John McCall  // Emit any writebacks immediately.  Arguably this should happen
2385f85e193739c953358c865005855253af4f68a497John McCall  // after any return-value munging.
2386f85e193739c953358c865005855253af4f68a497John McCall  if (CallArgs.hasWritebacks())
2387f85e193739c953358c865005855253af4f68a497John McCall    emitWritebacks(*this, CallArgs);
2388f85e193739c953358c865005855253af4f68a497John McCall
23892c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar  switch (RetAI.getKind()) {
23909d232c884ea9872d6555df0fd7359699819bc1f1John McCall  case ABIArgInfo::Indirect:
23919d232c884ea9872d6555df0fd7359699819bc1f1John McCall    return convertTempToRValue(Args[0], RetTy);
23928951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
239311434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar  case ABIArgInfo::Ignore:
23940bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // If we are ignoring an argument that had a result, make sure to
23950bcc52114e37a8d152d9a05095ee7f7687c9aa94Daniel Dunbar    // construct the appropriate return value for our caller.
239613e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar    return GetUndefRValue(RetTy);
23979cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2398800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Extend:
2399800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner  case ABIArgInfo::Direct: {
24006af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner    llvm::Type *RetIRTy = ConvertType(RetTy);
24016af13f3a3538d6c075a6282a7f393c26ee1563c7Chris Lattner    if (RetAI.getCoerceToType() == RetIRTy && RetAI.getDirectOffset() == 0) {
24029d232c884ea9872d6555df0fd7359699819bc1f1John McCall      switch (getEvaluationKind(RetTy)) {
24039d232c884ea9872d6555df0fd7359699819bc1f1John McCall      case TEK_Complex: {
2404800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
2405800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
2406800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getComplex(std::make_pair(Real, Imag));
2407800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
24089d232c884ea9872d6555df0fd7359699819bc1f1John McCall      case TEK_Aggregate: {
2409800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        llvm::Value *DestPtr = ReturnValue.getValue();
2410800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        bool DestIsVolatile = ReturnValue.isVolatile();
241111434925bf81c932c6c8fe3533bc0ba900d50dc2Daniel Dunbar
2412800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        if (!DestPtr) {
2413800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestPtr = CreateMemTemp(RetTy, "agg.tmp");
2414800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner          DestIsVolatile = false;
2415800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        }
2416badea57d1db45caa95e71a256f4f4cf94fe20451Eli Friedman        BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
2417800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return RValue::getAggregate(DestPtr);
2418800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      }
24199d232c884ea9872d6555df0fd7359699819bc1f1John McCall      case TEK_Scalar: {
24209d232c884ea9872d6555df0fd7359699819bc1f1John McCall        // If the argument doesn't match, perform a bitcast to coerce it.  This
24219d232c884ea9872d6555df0fd7359699819bc1f1John McCall        // can happen due to trivial type mismatches.
24229d232c884ea9872d6555df0fd7359699819bc1f1John McCall        llvm::Value *V = CI;
24239d232c884ea9872d6555df0fd7359699819bc1f1John McCall        if (V->getType() != RetIRTy)
24249d232c884ea9872d6555df0fd7359699819bc1f1John McCall          V = Builder.CreateBitCast(V, RetIRTy);
24259d232c884ea9872d6555df0fd7359699819bc1f1John McCall        return RValue::get(V);
24269d232c884ea9872d6555df0fd7359699819bc1f1John McCall      }
24279d232c884ea9872d6555df0fd7359699819bc1f1John McCall      }
24289d232c884ea9872d6555df0fd7359699819bc1f1John McCall      llvm_unreachable("bad evaluation kind");
2429800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    }
24309cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2431d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    llvm::Value *DestPtr = ReturnValue.getValue();
2432d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    bool DestIsVolatile = ReturnValue.isVolatile();
24339cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2434d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    if (!DestPtr) {
2435195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar      DestPtr = CreateMemTemp(RetTy, "coerce");
2436d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson      DestIsVolatile = false;
2437d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson    }
24389cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2439117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the value is offset in memory, apply the offset now.
2440117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    llvm::Value *StorePtr = DestPtr;
2441117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (unsigned Offs = RetAI.getDirectOffset()) {
2442117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
2443117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
24449cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      StorePtr = Builder.CreateBitCast(StorePtr,
2445117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner                         llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
2446117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    }
2447117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
24489cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
24499d232c884ea9872d6555df0fd7359699819bc1f1John McCall    return convertTempToRValue(DestPtr, RetTy);
2450639ffe47097d01249b98b7acd102aaad6697b43aDaniel Dunbar  }
24518951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar
24528951dbd225580173193ec9db503d9d9844ff97d6Daniel Dunbar  case ABIArgInfo::Expand:
2453b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid ABI kind for return argument");
245417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar  }
24552c8e0f32b9c33686be23c70add0b97490903de9fDaniel Dunbar
2456b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled ABIArgInfo::Kind");
245717b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar}
2458b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
2459b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar/* VarArg handling */
2460b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar
2461b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbarllvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
2462b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
2463b4094ea09eee7d3a847cadf181a81efc99003dafDaniel Dunbar}
2464