1 //===--- CGCall.cpp - Encapsulate calling convention details ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These classes wrap the information about a call or function
11 // definition used to handle ABI compliancy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "CGCall.h"
16 #include "CGCXXABI.h"
17 #include "ABIInfo.h"
18 #include "CodeGenFunction.h"
19 #include "CodeGenModule.h"
20 #include "clang/Basic/TargetInfo.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/Frontend/CodeGenOptions.h"
25 #include "llvm/Attributes.h"
26 #include "llvm/Support/CallSite.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/InlineAsm.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 using namespace clang;
31 using namespace CodeGen;
32 
33 /***/
34 
35 static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
36   switch (CC) {
37   default: return llvm::CallingConv::C;
38   case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
39   case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
40   case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
41   case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
42   case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
43   // TODO: add support for CC_X86Pascal to llvm
44   }
45 }
46 
47 /// Derives the 'this' type for codegen purposes, i.e. ignoring method
48 /// qualification.
49 /// FIXME: address space qualification?
50 static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
51   QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
52   return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
53 }
54 
55 /// Returns the canonical formal type of the given C++ method.
56 static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
57   return MD->getType()->getCanonicalTypeUnqualified()
58            .getAs<FunctionProtoType>();
59 }
60 
61 /// Returns the "extra-canonicalized" return type, which discards
62 /// qualifiers on the return type.  Codegen doesn't care about them,
63 /// and it makes ABI code a little easier to be able to assume that
64 /// all parameter and return types are top-level unqualified.
65 static CanQualType GetReturnType(QualType RetTy) {
66   return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
67 }
68 
69 const CGFunctionInfo &
70 CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP) {
71   return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
72                          llvm::SmallVector<CanQualType, 16>(),
73                          FTNP->getExtInfo());
74 }
75 
76 /// \param Args - contains any initial parameters besides those
77 ///   in the formal type
78 static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
79                                   llvm::SmallVectorImpl<CanQualType> &ArgTys,
80                                              CanQual<FunctionProtoType> FTP) {
81   // FIXME: Kill copy.
82   for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
83     ArgTys.push_back(FTP->getArgType(i));
84   CanQualType ResTy = FTP->getResultType().getUnqualifiedType();
85   return CGT.getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
86 }
87 
88 const CGFunctionInfo &
89 CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP) {
90   llvm::SmallVector<CanQualType, 16> ArgTys;
91   return ::getFunctionInfo(*this, ArgTys, FTP);
92 }
93 
94 static CallingConv getCallingConventionForDecl(const Decl *D) {
95   // Set the appropriate calling convention for the Function.
96   if (D->hasAttr<StdCallAttr>())
97     return CC_X86StdCall;
98 
99   if (D->hasAttr<FastCallAttr>())
100     return CC_X86FastCall;
101 
102   if (D->hasAttr<ThisCallAttr>())
103     return CC_X86ThisCall;
104 
105   if (D->hasAttr<PascalAttr>())
106     return CC_X86Pascal;
107 
108   if (PcsAttr *PCS = D->getAttr<PcsAttr>())
109     return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
110 
111   return CC_C;
112 }
113 
114 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
115                                                  const FunctionProtoType *FTP) {
116   llvm::SmallVector<CanQualType, 16> ArgTys;
117 
118   // Add the 'this' pointer.
119   ArgTys.push_back(GetThisType(Context, RD));
120 
121   return ::getFunctionInfo(*this, ArgTys,
122               FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
123 }
124 
125 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
126   llvm::SmallVector<CanQualType, 16> ArgTys;
127 
128   assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
129   assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
130 
131   // Add the 'this' pointer unless this is a static method.
132   if (MD->isInstance())
133     ArgTys.push_back(GetThisType(Context, MD->getParent()));
134 
135   return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
136 }
137 
138 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
139                                                     CXXCtorType Type) {
140   llvm::SmallVector<CanQualType, 16> ArgTys;
141   ArgTys.push_back(GetThisType(Context, D->getParent()));
142   CanQualType ResTy = Context.VoidTy;
143 
144   TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
145 
146   CanQual<FunctionProtoType> FTP = GetFormalType(D);
147 
148   // Add the formal parameters.
149   for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
150     ArgTys.push_back(FTP->getArgType(i));
151 
152   return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
153 }
154 
155 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
156                                                     CXXDtorType Type) {
157   llvm::SmallVector<CanQualType, 2> ArgTys;
158   ArgTys.push_back(GetThisType(Context, D->getParent()));
159   CanQualType ResTy = Context.VoidTy;
160 
161   TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
162 
163   CanQual<FunctionProtoType> FTP = GetFormalType(D);
164   assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
165 
166   return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
167 }
168 
169 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
170   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
171     if (MD->isInstance())
172       return getFunctionInfo(MD);
173 
174   CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
175   assert(isa<FunctionType>(FTy));
176   if (isa<FunctionNoProtoType>(FTy))
177     return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
178   assert(isa<FunctionProtoType>(FTy));
179   return getFunctionInfo(FTy.getAs<FunctionProtoType>());
180 }
181 
182 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
183   llvm::SmallVector<CanQualType, 16> ArgTys;
184   ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
185   ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
186   // FIXME: Kill copy?
187   for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
188          e = MD->param_end(); i != e; ++i) {
189     ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
190   }
191 
192   FunctionType::ExtInfo einfo;
193   einfo = einfo.withCallingConv(getCallingConventionForDecl(MD));
194 
195   if (getContext().getLangOptions().ObjCAutoRefCount &&
196       MD->hasAttr<NSReturnsRetainedAttr>())
197     einfo = einfo.withProducesResult(true);
198 
199   return getFunctionInfo(GetReturnType(MD->getResultType()), ArgTys, einfo);
200 }
201 
202 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
203   // FIXME: Do we need to handle ObjCMethodDecl?
204   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
205 
206   if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
207     return getFunctionInfo(CD, GD.getCtorType());
208 
209   if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
210     return getFunctionInfo(DD, GD.getDtorType());
211 
212   return getFunctionInfo(FD);
213 }
214 
215 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
216                                                     const CallArgList &Args,
217                                             const FunctionType::ExtInfo &Info) {
218   // FIXME: Kill copy.
219   llvm::SmallVector<CanQualType, 16> ArgTys;
220   for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
221        i != e; ++i)
222     ArgTys.push_back(Context.getCanonicalParamType(i->Ty));
223   return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
224 }
225 
226 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
227                                                     const FunctionArgList &Args,
228                                             const FunctionType::ExtInfo &Info) {
229   // FIXME: Kill copy.
230   llvm::SmallVector<CanQualType, 16> ArgTys;
231   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
232        i != e; ++i)
233     ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
234   return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
235 }
236 
237 const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
238   llvm::SmallVector<CanQualType, 1> args;
239   return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
240 }
241 
242 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
243                            const llvm::SmallVectorImpl<CanQualType> &ArgTys,
244                                             const FunctionType::ExtInfo &Info) {
245 #ifndef NDEBUG
246   for (llvm::SmallVectorImpl<CanQualType>::const_iterator
247          I = ArgTys.begin(), E = ArgTys.end(); I != E; ++I)
248     assert(I->isCanonicalAsParam());
249 #endif
250 
251   unsigned CC = ClangCallConvToLLVMCallConv(Info.getCC());
252 
253   // Lookup or create unique function info.
254   llvm::FoldingSetNodeID ID;
255   CGFunctionInfo::Profile(ID, Info, ResTy, ArgTys.begin(), ArgTys.end());
256 
257   void *InsertPos = 0;
258   CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
259   if (FI)
260     return *FI;
261 
262   // Construct the function info.
263   FI = new CGFunctionInfo(CC, Info.getNoReturn(), Info.getProducesResult(),
264                           Info.getHasRegParm(), Info.getRegParm(), ResTy,
265                           ArgTys.data(), ArgTys.size());
266   FunctionInfos.InsertNode(FI, InsertPos);
267 
268   // Compute ABI information.
269   getABIInfo().computeInfo(*FI);
270 
271   // Loop over all of the computed argument and return value info.  If any of
272   // them are direct or extend without a specified coerce type, specify the
273   // default now.
274   ABIArgInfo &RetInfo = FI->getReturnInfo();
275   if (RetInfo.canHaveCoerceToType() && RetInfo.getCoerceToType() == 0)
276     RetInfo.setCoerceToType(ConvertType(FI->getReturnType()));
277 
278   for (CGFunctionInfo::arg_iterator I = FI->arg_begin(), E = FI->arg_end();
279        I != E; ++I)
280     if (I->info.canHaveCoerceToType() && I->info.getCoerceToType() == 0)
281       I->info.setCoerceToType(ConvertType(I->type));
282 
283   return *FI;
284 }
285 
286 CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
287                                bool _NoReturn, bool returnsRetained,
288                                bool _HasRegParm, unsigned _RegParm,
289                                CanQualType ResTy,
290                                const CanQualType *ArgTys,
291                                unsigned NumArgTys)
292   : CallingConvention(_CallingConvention),
293     EffectiveCallingConvention(_CallingConvention),
294     NoReturn(_NoReturn), ReturnsRetained(returnsRetained),
295     HasRegParm(_HasRegParm), RegParm(_RegParm)
296 {
297   NumArgs = NumArgTys;
298 
299   // FIXME: Coallocate with the CGFunctionInfo object.
300   Args = new ArgInfo[1 + NumArgTys];
301   Args[0].type = ResTy;
302   for (unsigned i = 0; i != NumArgTys; ++i)
303     Args[1 + i].type = ArgTys[i];
304 }
305 
306 /***/
307 
308 void CodeGenTypes::GetExpandedTypes(QualType type,
309                      llvm::SmallVectorImpl<llvm::Type*> &expandedTypes) {
310   const RecordType *RT = type->getAsStructureType();
311   assert(RT && "Can only expand structure types.");
312   const RecordDecl *RD = RT->getDecl();
313   assert(!RD->hasFlexibleArrayMember() &&
314          "Cannot expand structure with flexible array.");
315 
316   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
317          i != e; ++i) {
318     const FieldDecl *FD = *i;
319     assert(!FD->isBitField() &&
320            "Cannot expand structure with bit-field members.");
321 
322     QualType fieldType = FD->getType();
323     if (fieldType->isRecordType())
324       GetExpandedTypes(fieldType, expandedTypes);
325     else
326       expandedTypes.push_back(ConvertType(fieldType));
327   }
328 }
329 
330 llvm::Function::arg_iterator
331 CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
332                                     llvm::Function::arg_iterator AI) {
333   const RecordType *RT = Ty->getAsStructureType();
334   assert(RT && "Can only expand structure types.");
335 
336   RecordDecl *RD = RT->getDecl();
337   assert(LV.isSimple() &&
338          "Unexpected non-simple lvalue during struct expansion.");
339   llvm::Value *Addr = LV.getAddress();
340   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
341          i != e; ++i) {
342     FieldDecl *FD = *i;
343     QualType FT = FD->getType();
344 
345     // FIXME: What are the right qualifiers here?
346     LValue LV = EmitLValueForField(Addr, FD, 0);
347     if (CodeGenFunction::hasAggregateLLVMType(FT)) {
348       AI = ExpandTypeFromArgs(FT, LV, AI);
349     } else {
350       EmitStoreThroughLValue(RValue::get(AI), LV);
351       ++AI;
352     }
353   }
354 
355   return AI;
356 }
357 
358 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
359 /// accessing some number of bytes out of it, try to gep into the struct to get
360 /// at its inner goodness.  Dive as deep as possible without entering an element
361 /// with an in-memory size smaller than DstSize.
362 static llvm::Value *
363 EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
364                                    const llvm::StructType *SrcSTy,
365                                    uint64_t DstSize, CodeGenFunction &CGF) {
366   // We can't dive into a zero-element struct.
367   if (SrcSTy->getNumElements() == 0) return SrcPtr;
368 
369   const llvm::Type *FirstElt = SrcSTy->getElementType(0);
370 
371   // If the first elt is at least as large as what we're looking for, or if the
372   // first element is the same size as the whole struct, we can enter it.
373   uint64_t FirstEltSize =
374     CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
375   if (FirstEltSize < DstSize &&
376       FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
377     return SrcPtr;
378 
379   // GEP into the first element.
380   SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
381 
382   // If the first element is a struct, recurse.
383   const llvm::Type *SrcTy =
384     cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
385   if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
386     return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
387 
388   return SrcPtr;
389 }
390 
391 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
392 /// are either integers or pointers.  This does a truncation of the value if it
393 /// is too large or a zero extension if it is too small.
394 static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
395                                              const llvm::Type *Ty,
396                                              CodeGenFunction &CGF) {
397   if (Val->getType() == Ty)
398     return Val;
399 
400   if (isa<llvm::PointerType>(Val->getType())) {
401     // If this is Pointer->Pointer avoid conversion to and from int.
402     if (isa<llvm::PointerType>(Ty))
403       return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
404 
405     // Convert the pointer to an integer so we can play with its width.
406     Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
407   }
408 
409   const llvm::Type *DestIntTy = Ty;
410   if (isa<llvm::PointerType>(DestIntTy))
411     DestIntTy = CGF.IntPtrTy;
412 
413   if (Val->getType() != DestIntTy)
414     Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
415 
416   if (isa<llvm::PointerType>(Ty))
417     Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
418   return Val;
419 }
420 
421 
422 
423 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
424 /// a pointer to an object of type \arg Ty.
425 ///
426 /// This safely handles the case when the src type is smaller than the
427 /// destination type; in this situation the values of bits which not
428 /// present in the src are undefined.
429 static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
430                                       const llvm::Type *Ty,
431                                       CodeGenFunction &CGF) {
432   const llvm::Type *SrcTy =
433     cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
434 
435   // If SrcTy and Ty are the same, just do a load.
436   if (SrcTy == Ty)
437     return CGF.Builder.CreateLoad(SrcPtr);
438 
439   uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
440 
441   if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
442     SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
443     SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
444   }
445 
446   uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
447 
448   // If the source and destination are integer or pointer types, just do an
449   // extension or truncation to the desired type.
450   if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
451       (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
452     llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
453     return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
454   }
455 
456   // If load is legal, just bitcast the src pointer.
457   if (SrcSize >= DstSize) {
458     // Generally SrcSize is never greater than DstSize, since this means we are
459     // losing bits. However, this can happen in cases where the structure has
460     // additional padding, for example due to a user specified alignment.
461     //
462     // FIXME: Assert that we aren't truncating non-padding bits when have access
463     // to that information.
464     llvm::Value *Casted =
465       CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
466     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
467     // FIXME: Use better alignment / avoid requiring aligned load.
468     Load->setAlignment(1);
469     return Load;
470   }
471 
472   // Otherwise do coercion through memory. This is stupid, but
473   // simple.
474   llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
475   llvm::Value *Casted =
476     CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
477   llvm::StoreInst *Store =
478     CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
479   // FIXME: Use better alignment / avoid requiring aligned store.
480   Store->setAlignment(1);
481   return CGF.Builder.CreateLoad(Tmp);
482 }
483 
484 // Function to store a first-class aggregate into memory.  We prefer to
485 // store the elements rather than the aggregate to be more friendly to
486 // fast-isel.
487 // FIXME: Do we need to recurse here?
488 static void BuildAggStore(CodeGenFunction &CGF, llvm::Value *Val,
489                           llvm::Value *DestPtr, bool DestIsVolatile,
490                           bool LowAlignment) {
491   // Prefer scalar stores to first-class aggregate stores.
492   if (const llvm::StructType *STy =
493         dyn_cast<llvm::StructType>(Val->getType())) {
494     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
495       llvm::Value *EltPtr = CGF.Builder.CreateConstGEP2_32(DestPtr, 0, i);
496       llvm::Value *Elt = CGF.Builder.CreateExtractValue(Val, i);
497       llvm::StoreInst *SI = CGF.Builder.CreateStore(Elt, EltPtr,
498                                                     DestIsVolatile);
499       if (LowAlignment)
500         SI->setAlignment(1);
501     }
502   } else {
503     CGF.Builder.CreateStore(Val, DestPtr, DestIsVolatile);
504   }
505 }
506 
507 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
508 /// where the source and destination may have different types.
509 ///
510 /// This safely handles the case when the src type is larger than the
511 /// destination type; the upper bits of the src will be lost.
512 static void CreateCoercedStore(llvm::Value *Src,
513                                llvm::Value *DstPtr,
514                                bool DstIsVolatile,
515                                CodeGenFunction &CGF) {
516   const llvm::Type *SrcTy = Src->getType();
517   const llvm::Type *DstTy =
518     cast<llvm::PointerType>(DstPtr->getType())->getElementType();
519   if (SrcTy == DstTy) {
520     CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
521     return;
522   }
523 
524   uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
525 
526   if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
527     DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
528     DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
529   }
530 
531   // If the source and destination are integer or pointer types, just do an
532   // extension or truncation to the desired type.
533   if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
534       (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
535     Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
536     CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
537     return;
538   }
539 
540   uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
541 
542   // If store is legal, just bitcast the src pointer.
543   if (SrcSize <= DstSize) {
544     llvm::Value *Casted =
545       CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
546     // FIXME: Use better alignment / avoid requiring aligned store.
547     BuildAggStore(CGF, Src, Casted, DstIsVolatile, true);
548   } else {
549     // Otherwise do coercion through memory. This is stupid, but
550     // simple.
551 
552     // Generally SrcSize is never greater than DstSize, since this means we are
553     // losing bits. However, this can happen in cases where the structure has
554     // additional padding, for example due to a user specified alignment.
555     //
556     // FIXME: Assert that we aren't truncating non-padding bits when have access
557     // to that information.
558     llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
559     CGF.Builder.CreateStore(Src, Tmp);
560     llvm::Value *Casted =
561       CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
562     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
563     // FIXME: Use better alignment / avoid requiring aligned load.
564     Load->setAlignment(1);
565     CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
566   }
567 }
568 
569 /***/
570 
571 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
572   return FI.getReturnInfo().isIndirect();
573 }
574 
575 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
576   if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
577     switch (BT->getKind()) {
578     default:
579       return false;
580     case BuiltinType::Float:
581       return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
582     case BuiltinType::Double:
583       return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
584     case BuiltinType::LongDouble:
585       return getContext().Target.useObjCFPRetForRealType(
586         TargetInfo::LongDouble);
587     }
588   }
589 
590   return false;
591 }
592 
593 llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
594   const CGFunctionInfo &FI = getFunctionInfo(GD);
595 
596   // For definition purposes, don't consider a K&R function variadic.
597   bool Variadic = false;
598   if (const FunctionProtoType *FPT =
599         cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
600     Variadic = FPT->isVariadic();
601 
602   return GetFunctionType(FI, Variadic);
603 }
604 
605 llvm::FunctionType *
606 CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool isVariadic) {
607   llvm::SmallVector<llvm::Type*, 8> argTypes;
608   const llvm::Type *resultType = 0;
609 
610   const ABIArgInfo &retAI = FI.getReturnInfo();
611   switch (retAI.getKind()) {
612   case ABIArgInfo::Expand:
613     llvm_unreachable("Invalid ABI kind for return argument");
614 
615   case ABIArgInfo::Extend:
616   case ABIArgInfo::Direct:
617     resultType = retAI.getCoerceToType();
618     break;
619 
620   case ABIArgInfo::Indirect: {
621     assert(!retAI.getIndirectAlign() && "Align unused on indirect return.");
622     resultType = llvm::Type::getVoidTy(getLLVMContext());
623 
624     QualType ret = FI.getReturnType();
625     const llvm::Type *ty = ConvertType(ret);
626     unsigned addressSpace = Context.getTargetAddressSpace(ret);
627     argTypes.push_back(llvm::PointerType::get(ty, addressSpace));
628     break;
629   }
630 
631   case ABIArgInfo::Ignore:
632     resultType = llvm::Type::getVoidTy(getLLVMContext());
633     break;
634   }
635 
636   for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
637          ie = FI.arg_end(); it != ie; ++it) {
638     const ABIArgInfo &argAI = it->info;
639 
640     switch (argAI.getKind()) {
641     case ABIArgInfo::Ignore:
642       break;
643 
644     case ABIArgInfo::Indirect: {
645       // indirect arguments are always on the stack, which is addr space #0.
646       const llvm::Type *LTy = ConvertTypeForMem(it->type);
647       argTypes.push_back(LTy->getPointerTo());
648       break;
649     }
650 
651     case ABIArgInfo::Extend:
652     case ABIArgInfo::Direct: {
653       // If the coerce-to type is a first class aggregate, flatten it.  Either
654       // way is semantically identical, but fast-isel and the optimizer
655       // generally likes scalar values better than FCAs.
656       llvm::Type *argType = argAI.getCoerceToType();
657       if (const llvm::StructType *st = dyn_cast<llvm::StructType>(argType)) {
658         for (unsigned i = 0, e = st->getNumElements(); i != e; ++i)
659           argTypes.push_back(st->getElementType(i));
660       } else {
661         argTypes.push_back(argType);
662       }
663       break;
664     }
665 
666     case ABIArgInfo::Expand:
667       GetExpandedTypes(it->type, argTypes);
668       break;
669     }
670   }
671 
672   return llvm::FunctionType::get(resultType, argTypes, isVariadic);
673 }
674 
675 const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
676   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
677   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
678 
679   if (!isFuncTypeConvertible(FPT))
680     return llvm::StructType::get(getLLVMContext());
681 
682   const CGFunctionInfo *Info;
683   if (isa<CXXDestructorDecl>(MD))
684     Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
685   else
686     Info = &getFunctionInfo(MD);
687   return GetFunctionType(*Info, FPT->isVariadic());
688 }
689 
690 void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
691                                            const Decl *TargetDecl,
692                                            AttributeListType &PAL,
693                                            unsigned &CallingConv) {
694   unsigned FuncAttrs = 0;
695   unsigned RetAttrs = 0;
696 
697   CallingConv = FI.getEffectiveCallingConvention();
698 
699   if (FI.isNoReturn())
700     FuncAttrs |= llvm::Attribute::NoReturn;
701 
702   // FIXME: handle sseregparm someday...
703   if (TargetDecl) {
704     if (TargetDecl->hasAttr<NoThrowAttr>())
705       FuncAttrs |= llvm::Attribute::NoUnwind;
706     else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
707       const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
708       if (FPT && FPT->isNothrow(getContext()))
709         FuncAttrs |= llvm::Attribute::NoUnwind;
710     }
711 
712     if (TargetDecl->hasAttr<NoReturnAttr>())
713       FuncAttrs |= llvm::Attribute::NoReturn;
714     if (TargetDecl->hasAttr<ConstAttr>())
715       FuncAttrs |= llvm::Attribute::ReadNone;
716     else if (TargetDecl->hasAttr<PureAttr>())
717       FuncAttrs |= llvm::Attribute::ReadOnly;
718     if (TargetDecl->hasAttr<MallocAttr>())
719       RetAttrs |= llvm::Attribute::NoAlias;
720   }
721 
722   if (CodeGenOpts.OptimizeSize)
723     FuncAttrs |= llvm::Attribute::OptimizeForSize;
724   if (CodeGenOpts.DisableRedZone)
725     FuncAttrs |= llvm::Attribute::NoRedZone;
726   if (CodeGenOpts.NoImplicitFloat)
727     FuncAttrs |= llvm::Attribute::NoImplicitFloat;
728 
729   QualType RetTy = FI.getReturnType();
730   unsigned Index = 1;
731   const ABIArgInfo &RetAI = FI.getReturnInfo();
732   switch (RetAI.getKind()) {
733   case ABIArgInfo::Extend:
734    if (RetTy->hasSignedIntegerRepresentation())
735      RetAttrs |= llvm::Attribute::SExt;
736    else if (RetTy->hasUnsignedIntegerRepresentation())
737      RetAttrs |= llvm::Attribute::ZExt;
738     break;
739   case ABIArgInfo::Direct:
740   case ABIArgInfo::Ignore:
741     break;
742 
743   case ABIArgInfo::Indirect:
744     PAL.push_back(llvm::AttributeWithIndex::get(Index,
745                                                 llvm::Attribute::StructRet));
746     ++Index;
747     // sret disables readnone and readonly
748     FuncAttrs &= ~(llvm::Attribute::ReadOnly |
749                    llvm::Attribute::ReadNone);
750     break;
751 
752   case ABIArgInfo::Expand:
753     assert(0 && "Invalid ABI kind for return argument");
754   }
755 
756   if (RetAttrs)
757     PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
758 
759   // FIXME: RegParm should be reduced in case of global register variable.
760   signed RegParm;
761   if (FI.getHasRegParm())
762     RegParm = FI.getRegParm();
763   else
764     RegParm = CodeGenOpts.NumRegisterParameters;
765 
766   unsigned PointerWidth = getContext().Target.getPointerWidth(0);
767   for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
768          ie = FI.arg_end(); it != ie; ++it) {
769     QualType ParamType = it->type;
770     const ABIArgInfo &AI = it->info;
771     unsigned Attributes = 0;
772 
773     // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
774     // have the corresponding parameter variable.  It doesn't make
775     // sense to do it here because parameters are so messed up.
776     switch (AI.getKind()) {
777     case ABIArgInfo::Extend:
778       if (ParamType->isSignedIntegerOrEnumerationType())
779         Attributes |= llvm::Attribute::SExt;
780       else if (ParamType->isUnsignedIntegerOrEnumerationType())
781         Attributes |= llvm::Attribute::ZExt;
782       // FALL THROUGH
783     case ABIArgInfo::Direct:
784       if (RegParm > 0 &&
785           (ParamType->isIntegerType() || ParamType->isPointerType())) {
786         RegParm -=
787         (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
788         if (RegParm >= 0)
789           Attributes |= llvm::Attribute::InReg;
790       }
791       // FIXME: handle sseregparm someday...
792 
793       if (const llvm::StructType *STy =
794             dyn_cast<llvm::StructType>(AI.getCoerceToType()))
795         Index += STy->getNumElements()-1;  // 1 will be added below.
796       break;
797 
798     case ABIArgInfo::Indirect:
799       if (AI.getIndirectByVal())
800         Attributes |= llvm::Attribute::ByVal;
801 
802       Attributes |=
803         llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
804       // byval disables readnone and readonly.
805       FuncAttrs &= ~(llvm::Attribute::ReadOnly |
806                      llvm::Attribute::ReadNone);
807       break;
808 
809     case ABIArgInfo::Ignore:
810       // Skip increment, no matching LLVM parameter.
811       continue;
812 
813     case ABIArgInfo::Expand: {
814       llvm::SmallVector<llvm::Type*, 8> types;
815       // FIXME: This is rather inefficient. Do we ever actually need to do
816       // anything here? The result should be just reconstructed on the other
817       // side, so extension should be a non-issue.
818       getTypes().GetExpandedTypes(ParamType, types);
819       Index += types.size();
820       continue;
821     }
822     }
823 
824     if (Attributes)
825       PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
826     ++Index;
827   }
828   if (FuncAttrs)
829     PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
830 }
831 
832 /// An argument came in as a promoted argument; demote it back to its
833 /// declared type.
834 static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
835                                          const VarDecl *var,
836                                          llvm::Value *value) {
837   const llvm::Type *varType = CGF.ConvertType(var->getType());
838 
839   // This can happen with promotions that actually don't change the
840   // underlying type, like the enum promotions.
841   if (value->getType() == varType) return value;
842 
843   assert((varType->isIntegerTy() || varType->isFloatingPointTy())
844          && "unexpected promotion type");
845 
846   if (isa<llvm::IntegerType>(varType))
847     return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
848 
849   return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
850 }
851 
852 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
853                                          llvm::Function *Fn,
854                                          const FunctionArgList &Args) {
855   // If this is an implicit-return-zero function, go ahead and
856   // initialize the return value.  TODO: it might be nice to have
857   // a more general mechanism for this that didn't require synthesized
858   // return statements.
859   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
860     if (FD->hasImplicitReturnZero()) {
861       QualType RetTy = FD->getResultType().getUnqualifiedType();
862       const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
863       llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
864       Builder.CreateStore(Zero, ReturnValue);
865     }
866   }
867 
868   // FIXME: We no longer need the types from FunctionArgList; lift up and
869   // simplify.
870 
871   // Emit allocs for param decls.  Give the LLVM Argument nodes names.
872   llvm::Function::arg_iterator AI = Fn->arg_begin();
873 
874   // Name the struct return argument.
875   if (CGM.ReturnTypeUsesSRet(FI)) {
876     AI->setName("agg.result");
877     ++AI;
878   }
879 
880   assert(FI.arg_size() == Args.size() &&
881          "Mismatch between function signature & arguments.");
882   unsigned ArgNo = 1;
883   CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
884   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
885        i != e; ++i, ++info_it, ++ArgNo) {
886     const VarDecl *Arg = *i;
887     QualType Ty = info_it->type;
888     const ABIArgInfo &ArgI = info_it->info;
889 
890     bool isPromoted =
891       isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
892 
893     switch (ArgI.getKind()) {
894     case ABIArgInfo::Indirect: {
895       llvm::Value *V = AI;
896 
897       if (hasAggregateLLVMType(Ty)) {
898         // Aggregates and complex variables are accessed by reference.  All we
899         // need to do is realign the value, if requested
900         if (ArgI.getIndirectRealign()) {
901           llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
902 
903           // Copy from the incoming argument pointer to the temporary with the
904           // appropriate alignment.
905           //
906           // FIXME: We should have a common utility for generating an aggregate
907           // copy.
908           const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
909           CharUnits Size = getContext().getTypeSizeInChars(Ty);
910           llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
911           llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
912           Builder.CreateMemCpy(Dst,
913                                Src,
914                                llvm::ConstantInt::get(IntPtrTy,
915                                                       Size.getQuantity()),
916                                ArgI.getIndirectAlign(),
917                                false);
918           V = AlignedTemp;
919         }
920       } else {
921         // Load scalar value from indirect argument.
922         CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
923         V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
924 
925         if (isPromoted)
926           V = emitArgumentDemotion(*this, Arg, V);
927       }
928       EmitParmDecl(*Arg, V, ArgNo);
929       break;
930     }
931 
932     case ABIArgInfo::Extend:
933     case ABIArgInfo::Direct: {
934       // If we have the trivial case, handle it with no muss and fuss.
935       if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
936           ArgI.getCoerceToType() == ConvertType(Ty) &&
937           ArgI.getDirectOffset() == 0) {
938         assert(AI != Fn->arg_end() && "Argument mismatch!");
939         llvm::Value *V = AI;
940 
941         if (Arg->getType().isRestrictQualified())
942           AI->addAttr(llvm::Attribute::NoAlias);
943 
944         if (isPromoted)
945           V = emitArgumentDemotion(*this, Arg, V);
946 
947         EmitParmDecl(*Arg, V, ArgNo);
948         break;
949       }
950 
951       llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
952 
953       // The alignment we need to use is the max of the requested alignment for
954       // the argument plus the alignment required by our access code below.
955       unsigned AlignmentToUse =
956         CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
957       AlignmentToUse = std::max(AlignmentToUse,
958                         (unsigned)getContext().getDeclAlign(Arg).getQuantity());
959 
960       Alloca->setAlignment(AlignmentToUse);
961       llvm::Value *V = Alloca;
962       llvm::Value *Ptr = V;    // Pointer to store into.
963 
964       // If the value is offset in memory, apply the offset now.
965       if (unsigned Offs = ArgI.getDirectOffset()) {
966         Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
967         Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
968         Ptr = Builder.CreateBitCast(Ptr,
969                           llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
970       }
971 
972       // If the coerce-to type is a first class aggregate, we flatten it and
973       // pass the elements. Either way is semantically identical, but fast-isel
974       // and the optimizer generally likes scalar values better than FCAs.
975       if (const llvm::StructType *STy =
976             dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
977         Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
978 
979         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
980           assert(AI != Fn->arg_end() && "Argument mismatch!");
981           AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
982           llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
983           Builder.CreateStore(AI++, EltPtr);
984         }
985       } else {
986         // Simple case, just do a coerced store of the argument into the alloca.
987         assert(AI != Fn->arg_end() && "Argument mismatch!");
988         AI->setName(Arg->getName() + ".coerce");
989         CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
990       }
991 
992 
993       // Match to what EmitParmDecl is expecting for this type.
994       if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
995         V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
996         if (isPromoted)
997           V = emitArgumentDemotion(*this, Arg, V);
998       }
999       EmitParmDecl(*Arg, V, ArgNo);
1000       continue;  // Skip ++AI increment, already done.
1001     }
1002 
1003     case ABIArgInfo::Expand: {
1004       // If this structure was expanded into multiple arguments then
1005       // we need to create a temporary and reconstruct it from the
1006       // arguments.
1007       llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
1008       llvm::Function::arg_iterator End =
1009         ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
1010       EmitParmDecl(*Arg, Temp, ArgNo);
1011 
1012       // Name the arguments used in expansion and increment AI.
1013       unsigned Index = 0;
1014       for (; AI != End; ++AI, ++Index)
1015         AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1016       continue;
1017     }
1018 
1019     case ABIArgInfo::Ignore:
1020       // Initialize the local variable appropriately.
1021       if (hasAggregateLLVMType(Ty))
1022         EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1023       else
1024         EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1025                      ArgNo);
1026 
1027       // Skip increment, no matching LLVM parameter.
1028       continue;
1029     }
1030 
1031     ++AI;
1032   }
1033   assert(AI == Fn->arg_end() && "Argument mismatch!");
1034 }
1035 
1036 /// Try to emit a fused autorelease of a return result.
1037 static llvm::Value *tryEmitFusedAutoreleaseOfResult(CodeGenFunction &CGF,
1038                                                     llvm::Value *result) {
1039   // We must be immediately followed the cast.
1040   llvm::BasicBlock *BB = CGF.Builder.GetInsertBlock();
1041   if (BB->empty()) return 0;
1042   if (&BB->back() != result) return 0;
1043 
1044   const llvm::Type *resultType = result->getType();
1045 
1046   // result is in a BasicBlock and is therefore an Instruction.
1047   llvm::Instruction *generator = cast<llvm::Instruction>(result);
1048 
1049   llvm::SmallVector<llvm::Instruction*,4> insnsToKill;
1050 
1051   // Look for:
1052   //  %generator = bitcast %type1* %generator2 to %type2*
1053   while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(generator)) {
1054     // We would have emitted this as a constant if the operand weren't
1055     // an Instruction.
1056     generator = cast<llvm::Instruction>(bitcast->getOperand(0));
1057 
1058     // Require the generator to be immediately followed by the cast.
1059     if (generator->getNextNode() != bitcast)
1060       return 0;
1061 
1062     insnsToKill.push_back(bitcast);
1063   }
1064 
1065   // Look for:
1066   //   %generator = call i8* @objc_retain(i8* %originalResult)
1067   // or
1068   //   %generator = call i8* @objc_retainAutoreleasedReturnValue(i8* %originalResult)
1069   llvm::CallInst *call = dyn_cast<llvm::CallInst>(generator);
1070   if (!call) return 0;
1071 
1072   bool doRetainAutorelease;
1073 
1074   if (call->getCalledValue() == CGF.CGM.getARCEntrypoints().objc_retain) {
1075     doRetainAutorelease = true;
1076   } else if (call->getCalledValue() == CGF.CGM.getARCEntrypoints()
1077                                           .objc_retainAutoreleasedReturnValue) {
1078     doRetainAutorelease = false;
1079 
1080     // Look for an inline asm immediately preceding the call and kill it, too.
1081     llvm::Instruction *prev = call->getPrevNode();
1082     if (llvm::CallInst *asmCall = dyn_cast_or_null<llvm::CallInst>(prev))
1083       if (asmCall->getCalledValue()
1084             == CGF.CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker)
1085         insnsToKill.push_back(prev);
1086   } else {
1087     return 0;
1088   }
1089 
1090   result = call->getArgOperand(0);
1091   insnsToKill.push_back(call);
1092 
1093   // Keep killing bitcasts, for sanity.  Note that we no longer care
1094   // about precise ordering as long as there's exactly one use.
1095   while (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(result)) {
1096     if (!bitcast->hasOneUse()) break;
1097     insnsToKill.push_back(bitcast);
1098     result = bitcast->getOperand(0);
1099   }
1100 
1101   // Delete all the unnecessary instructions, from latest to earliest.
1102   for (llvm::SmallVectorImpl<llvm::Instruction*>::iterator
1103          i = insnsToKill.begin(), e = insnsToKill.end(); i != e; ++i)
1104     (*i)->eraseFromParent();
1105 
1106   // Do the fused retain/autorelease if we were asked to.
1107   if (doRetainAutorelease)
1108     result = CGF.EmitARCRetainAutoreleaseReturnValue(result);
1109 
1110   // Cast back to the result type.
1111   return CGF.Builder.CreateBitCast(result, resultType);
1112 }
1113 
1114 /// Emit an ARC autorelease of the result of a function.
1115 static llvm::Value *emitAutoreleaseOfResult(CodeGenFunction &CGF,
1116                                             llvm::Value *result) {
1117   // At -O0, try to emit a fused retain/autorelease.
1118   if (CGF.shouldUseFusedARCCalls())
1119     if (llvm::Value *fused = tryEmitFusedAutoreleaseOfResult(CGF, result))
1120       return fused;
1121 
1122   return CGF.EmitARCAutoreleaseReturnValue(result);
1123 }
1124 
1125 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
1126   // Functions with no result always return void.
1127   if (ReturnValue == 0) {
1128     Builder.CreateRetVoid();
1129     return;
1130   }
1131 
1132   llvm::DebugLoc RetDbgLoc;
1133   llvm::Value *RV = 0;
1134   QualType RetTy = FI.getReturnType();
1135   const ABIArgInfo &RetAI = FI.getReturnInfo();
1136 
1137   switch (RetAI.getKind()) {
1138   case ABIArgInfo::Indirect: {
1139     unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1140     if (RetTy->isAnyComplexType()) {
1141       ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1142       StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1143     } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1144       // Do nothing; aggregrates get evaluated directly into the destination.
1145     } else {
1146       EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1147                         false, Alignment, RetTy);
1148     }
1149     break;
1150   }
1151 
1152   case ABIArgInfo::Extend:
1153   case ABIArgInfo::Direct:
1154     if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1155         RetAI.getDirectOffset() == 0) {
1156       // The internal return value temp always will have pointer-to-return-type
1157       // type, just do a load.
1158 
1159       // If the instruction right before the insertion point is a store to the
1160       // return value, we can elide the load, zap the store, and usually zap the
1161       // alloca.
1162       llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1163       llvm::StoreInst *SI = 0;
1164       if (InsertBB->empty() ||
1165           !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1166           SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1167         RV = Builder.CreateLoad(ReturnValue);
1168       } else {
1169         // Get the stored value and nuke the now-dead store.
1170         RetDbgLoc = SI->getDebugLoc();
1171         RV = SI->getValueOperand();
1172         SI->eraseFromParent();
1173 
1174         // If that was the only use of the return value, nuke it as well now.
1175         if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1176           cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1177           ReturnValue = 0;
1178         }
1179       }
1180     } else {
1181       llvm::Value *V = ReturnValue;
1182       // If the value is offset in memory, apply the offset now.
1183       if (unsigned Offs = RetAI.getDirectOffset()) {
1184         V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1185         V = Builder.CreateConstGEP1_32(V, Offs);
1186         V = Builder.CreateBitCast(V,
1187                          llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1188       }
1189 
1190       RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
1191     }
1192 
1193     // In ARC, end functions that return a retainable type with a call
1194     // to objc_autoreleaseReturnValue.
1195     if (AutoreleaseResult) {
1196       assert(getLangOptions().ObjCAutoRefCount &&
1197              !FI.isReturnsRetained() &&
1198              RetTy->isObjCRetainableType());
1199       RV = emitAutoreleaseOfResult(*this, RV);
1200     }
1201 
1202     break;
1203 
1204   case ABIArgInfo::Ignore:
1205     break;
1206 
1207   case ABIArgInfo::Expand:
1208     assert(0 && "Invalid ABI kind for return argument");
1209   }
1210 
1211   llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1212   if (!RetDbgLoc.isUnknown())
1213     Ret->setDebugLoc(RetDbgLoc);
1214 }
1215 
1216 void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1217                                           const VarDecl *param) {
1218   // StartFunction converted the ABI-lowered parameter(s) into a
1219   // local alloca.  We need to turn that into an r-value suitable
1220   // for EmitCall.
1221   llvm::Value *local = GetAddrOfLocalVar(param);
1222 
1223   QualType type = param->getType();
1224 
1225   // For the most part, we just need to load the alloca, except:
1226   // 1) aggregate r-values are actually pointers to temporaries, and
1227   // 2) references to aggregates are pointers directly to the aggregate.
1228   // I don't know why references to non-aggregates are different here.
1229   if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1230     if (hasAggregateLLVMType(ref->getPointeeType()))
1231       return args.add(RValue::getAggregate(local), type);
1232 
1233     // Locals which are references to scalars are represented
1234     // with allocas holding the pointer.
1235     return args.add(RValue::get(Builder.CreateLoad(local)), type);
1236   }
1237 
1238   if (type->isAnyComplexType()) {
1239     ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1240     return args.add(RValue::getComplex(complex), type);
1241   }
1242 
1243   if (hasAggregateLLVMType(type))
1244     return args.add(RValue::getAggregate(local), type);
1245 
1246   unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1247   llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1248   return args.add(RValue::get(value), type);
1249 }
1250 
1251 static bool isProvablyNull(llvm::Value *addr) {
1252   return isa<llvm::ConstantPointerNull>(addr);
1253 }
1254 
1255 static bool isProvablyNonNull(llvm::Value *addr) {
1256   return isa<llvm::AllocaInst>(addr);
1257 }
1258 
1259 /// Emit the actual writing-back of a writeback.
1260 static void emitWriteback(CodeGenFunction &CGF,
1261                           const CallArgList::Writeback &writeback) {
1262   llvm::Value *srcAddr = writeback.Address;
1263   assert(!isProvablyNull(srcAddr) &&
1264          "shouldn't have writeback for provably null argument");
1265 
1266   llvm::BasicBlock *contBB = 0;
1267 
1268   // If the argument wasn't provably non-null, we need to null check
1269   // before doing the store.
1270   bool provablyNonNull = isProvablyNonNull(srcAddr);
1271   if (!provablyNonNull) {
1272     llvm::BasicBlock *writebackBB = CGF.createBasicBlock("icr.writeback");
1273     contBB = CGF.createBasicBlock("icr.done");
1274 
1275     llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1276     CGF.Builder.CreateCondBr(isNull, contBB, writebackBB);
1277     CGF.EmitBlock(writebackBB);
1278   }
1279 
1280   // Load the value to writeback.
1281   llvm::Value *value = CGF.Builder.CreateLoad(writeback.Temporary);
1282 
1283   // Cast it back, in case we're writing an id to a Foo* or something.
1284   value = CGF.Builder.CreateBitCast(value,
1285                cast<llvm::PointerType>(srcAddr->getType())->getElementType(),
1286                             "icr.writeback-cast");
1287 
1288   // Perform the writeback.
1289   QualType srcAddrType = writeback.AddressType;
1290   CGF.EmitStoreThroughLValue(RValue::get(value),
1291                              CGF.MakeAddrLValue(srcAddr, srcAddrType));
1292 
1293   // Jump to the continuation block.
1294   if (!provablyNonNull)
1295     CGF.EmitBlock(contBB);
1296 }
1297 
1298 static void emitWritebacks(CodeGenFunction &CGF,
1299                            const CallArgList &args) {
1300   for (CallArgList::writeback_iterator
1301          i = args.writeback_begin(), e = args.writeback_end(); i != e; ++i)
1302     emitWriteback(CGF, *i);
1303 }
1304 
1305 /// Emit an argument that's being passed call-by-writeback.  That is,
1306 /// we are passing the address of
1307 static void emitWritebackArg(CodeGenFunction &CGF, CallArgList &args,
1308                              const ObjCIndirectCopyRestoreExpr *CRE) {
1309   llvm::Value *srcAddr = CGF.EmitScalarExpr(CRE->getSubExpr());
1310 
1311   // The dest and src types don't necessarily match in LLVM terms
1312   // because of the crazy ObjC compatibility rules.
1313 
1314   const llvm::PointerType *destType =
1315     cast<llvm::PointerType>(CGF.ConvertType(CRE->getType()));
1316 
1317   // If the address is a constant null, just pass the appropriate null.
1318   if (isProvablyNull(srcAddr)) {
1319     args.add(RValue::get(llvm::ConstantPointerNull::get(destType)),
1320              CRE->getType());
1321     return;
1322   }
1323 
1324   QualType srcAddrType =
1325     CRE->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
1326 
1327   // Create the temporary.
1328   llvm::Value *temp = CGF.CreateTempAlloca(destType->getElementType(),
1329                                            "icr.temp");
1330 
1331   // Zero-initialize it if we're not doing a copy-initialization.
1332   bool shouldCopy = CRE->shouldCopy();
1333   if (!shouldCopy) {
1334     llvm::Value *null =
1335       llvm::ConstantPointerNull::get(
1336         cast<llvm::PointerType>(destType->getElementType()));
1337     CGF.Builder.CreateStore(null, temp);
1338   }
1339 
1340   llvm::BasicBlock *contBB = 0;
1341 
1342   // If the address is *not* known to be non-null, we need to switch.
1343   llvm::Value *finalArgument;
1344 
1345   bool provablyNonNull = isProvablyNonNull(srcAddr);
1346   if (provablyNonNull) {
1347     finalArgument = temp;
1348   } else {
1349     llvm::Value *isNull = CGF.Builder.CreateIsNull(srcAddr, "icr.isnull");
1350 
1351     finalArgument = CGF.Builder.CreateSelect(isNull,
1352                                    llvm::ConstantPointerNull::get(destType),
1353                                              temp, "icr.argument");
1354 
1355     // If we need to copy, then the load has to be conditional, which
1356     // means we need control flow.
1357     if (shouldCopy) {
1358       contBB = CGF.createBasicBlock("icr.cont");
1359       llvm::BasicBlock *copyBB = CGF.createBasicBlock("icr.copy");
1360       CGF.Builder.CreateCondBr(isNull, contBB, copyBB);
1361       CGF.EmitBlock(copyBB);
1362     }
1363   }
1364 
1365   // Perform a copy if necessary.
1366   if (shouldCopy) {
1367     LValue srcLV = CGF.MakeAddrLValue(srcAddr, srcAddrType);
1368     RValue srcRV = CGF.EmitLoadOfLValue(srcLV);
1369     assert(srcRV.isScalar());
1370 
1371     llvm::Value *src = srcRV.getScalarVal();
1372     src = CGF.Builder.CreateBitCast(src, destType->getElementType(),
1373                                     "icr.cast");
1374 
1375     // Use an ordinary store, not a store-to-lvalue.
1376     CGF.Builder.CreateStore(src, temp);
1377   }
1378 
1379   // Finish the control flow if we needed it.
1380   if (shouldCopy && !provablyNonNull)
1381     CGF.EmitBlock(contBB);
1382 
1383   args.addWriteback(srcAddr, srcAddrType, temp);
1384   args.add(RValue::get(finalArgument), CRE->getType());
1385 }
1386 
1387 void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1388                                   QualType type) {
1389   if (const ObjCIndirectCopyRestoreExpr *CRE
1390         = dyn_cast<ObjCIndirectCopyRestoreExpr>(E)) {
1391     assert(getContext().getLangOptions().ObjCAutoRefCount);
1392     assert(getContext().hasSameType(E->getType(), type));
1393     return emitWritebackArg(*this, args, CRE);
1394   }
1395 
1396   if (type->isReferenceType())
1397     return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1398                     type);
1399 
1400   if (hasAggregateLLVMType(type) && !E->getType()->isAnyComplexType() &&
1401       isa<ImplicitCastExpr>(E) &&
1402       cast<CastExpr>(E)->getCastKind() == CK_LValueToRValue) {
1403     LValue L = EmitLValue(cast<CastExpr>(E)->getSubExpr());
1404     assert(L.isSimple());
1405     args.add(RValue::getAggregate(L.getAddress(), L.isVolatileQualified()),
1406              type, /*NeedsCopy*/true);
1407     return;
1408   }
1409 
1410   args.add(EmitAnyExprToTemp(E), type);
1411 }
1412 
1413 /// Emits a call or invoke instruction to the given function, depending
1414 /// on the current state of the EH stack.
1415 llvm::CallSite
1416 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1417                                   llvm::Value * const *ArgBegin,
1418                                   llvm::Value * const *ArgEnd,
1419                                   const llvm::Twine &Name) {
1420   llvm::BasicBlock *InvokeDest = getInvokeDest();
1421   if (!InvokeDest)
1422     return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1423 
1424   llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1425   llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1426                                                   ArgBegin, ArgEnd, Name);
1427   EmitBlock(ContBB);
1428   return Invoke;
1429 }
1430 
1431 static void checkArgMatches(llvm::Value *Elt, unsigned &ArgNo,
1432                             llvm::FunctionType *FTy) {
1433   if (ArgNo < FTy->getNumParams())
1434     assert(Elt->getType() == FTy->getParamType(ArgNo));
1435   else
1436     assert(FTy->isVarArg());
1437   ++ArgNo;
1438 }
1439 
1440 void CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1441                                        llvm::SmallVector<llvm::Value*,16> &Args,
1442                                        llvm::FunctionType *IRFuncTy) {
1443   const RecordType *RT = Ty->getAsStructureType();
1444   assert(RT && "Can only expand structure types.");
1445 
1446   RecordDecl *RD = RT->getDecl();
1447   assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1448   llvm::Value *Addr = RV.getAggregateAddr();
1449   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1450        i != e; ++i) {
1451     FieldDecl *FD = *i;
1452     QualType FT = FD->getType();
1453 
1454     // FIXME: What are the right qualifiers here?
1455     LValue LV = EmitLValueForField(Addr, FD, 0);
1456     if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1457       ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()),
1458                        Args, IRFuncTy);
1459       continue;
1460     }
1461 
1462     RValue RV = EmitLoadOfLValue(LV);
1463     assert(RV.isScalar() &&
1464            "Unexpected non-scalar rvalue during struct expansion.");
1465 
1466     // Insert a bitcast as needed.
1467     llvm::Value *V = RV.getScalarVal();
1468     if (Args.size() < IRFuncTy->getNumParams() &&
1469         V->getType() != IRFuncTy->getParamType(Args.size()))
1470       V = Builder.CreateBitCast(V, IRFuncTy->getParamType(Args.size()));
1471 
1472     Args.push_back(V);
1473   }
1474 }
1475 
1476 
1477 RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1478                                  llvm::Value *Callee,
1479                                  ReturnValueSlot ReturnValue,
1480                                  const CallArgList &CallArgs,
1481                                  const Decl *TargetDecl,
1482                                  llvm::Instruction **callOrInvoke) {
1483   // FIXME: We no longer need the types from CallArgs; lift up and simplify.
1484   llvm::SmallVector<llvm::Value*, 16> Args;
1485 
1486   // Handle struct-return functions by passing a pointer to the
1487   // location that we would like to return into.
1488   QualType RetTy = CallInfo.getReturnType();
1489   const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
1490 
1491   // IRArgNo - Keep track of the argument number in the callee we're looking at.
1492   unsigned IRArgNo = 0;
1493   llvm::FunctionType *IRFuncTy =
1494     cast<llvm::FunctionType>(
1495                   cast<llvm::PointerType>(Callee->getType())->getElementType());
1496 
1497   // If the call returns a temporary with struct return, create a temporary
1498   // alloca to hold the result, unless one is given to us.
1499   if (CGM.ReturnTypeUsesSRet(CallInfo)) {
1500     llvm::Value *Value = ReturnValue.getValue();
1501     if (!Value)
1502       Value = CreateMemTemp(RetTy);
1503     Args.push_back(Value);
1504     checkArgMatches(Value, IRArgNo, IRFuncTy);
1505   }
1506 
1507   assert(CallInfo.arg_size() == CallArgs.size() &&
1508          "Mismatch between function signature & arguments.");
1509   CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
1510   for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1511        I != E; ++I, ++info_it) {
1512     const ABIArgInfo &ArgInfo = info_it->info;
1513     RValue RV = I->RV;
1514 
1515     unsigned TypeAlign =
1516       getContext().getTypeAlignInChars(I->Ty).getQuantity();
1517     switch (ArgInfo.getKind()) {
1518     case ABIArgInfo::Indirect: {
1519       if (RV.isScalar() || RV.isComplex()) {
1520         // Make a temporary alloca to pass the argument.
1521         llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
1522         if (ArgInfo.getIndirectAlign() > AI->getAlignment())
1523           AI->setAlignment(ArgInfo.getIndirectAlign());
1524         Args.push_back(AI);
1525 
1526         if (RV.isScalar())
1527           EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1528                             TypeAlign, I->Ty);
1529         else
1530           StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1531 
1532         // Validate argument match.
1533         checkArgMatches(AI, IRArgNo, IRFuncTy);
1534       } else {
1535         // We want to avoid creating an unnecessary temporary+copy here;
1536         // however, we need one in two cases:
1537         // 1. If the argument is not byval, and we are required to copy the
1538         //    source.  (This case doesn't occur on any common architecture.)
1539         // 2. If the argument is byval, RV is not sufficiently aligned, and
1540         //    we cannot force it to be sufficiently aligned.
1541         llvm::Value *Addr = RV.getAggregateAddr();
1542         unsigned Align = ArgInfo.getIndirectAlign();
1543         const llvm::TargetData *TD = &CGM.getTargetData();
1544         if ((!ArgInfo.getIndirectByVal() && I->NeedsCopy) ||
1545             (ArgInfo.getIndirectByVal() && TypeAlign < Align &&
1546              llvm::getOrEnforceKnownAlignment(Addr, Align, TD) < Align)) {
1547           // Create an aligned temporary, and copy to it.
1548           llvm::AllocaInst *AI = CreateMemTemp(I->Ty);
1549           if (Align > AI->getAlignment())
1550             AI->setAlignment(Align);
1551           Args.push_back(AI);
1552           EmitAggregateCopy(AI, Addr, I->Ty, RV.isVolatileQualified());
1553 
1554           // Validate argument match.
1555           checkArgMatches(AI, IRArgNo, IRFuncTy);
1556         } else {
1557           // Skip the extra memcpy call.
1558           Args.push_back(Addr);
1559 
1560           // Validate argument match.
1561           checkArgMatches(Addr, IRArgNo, IRFuncTy);
1562         }
1563       }
1564       break;
1565     }
1566 
1567     case ABIArgInfo::Ignore:
1568       break;
1569 
1570     case ABIArgInfo::Extend:
1571     case ABIArgInfo::Direct: {
1572       if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
1573           ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1574           ArgInfo.getDirectOffset() == 0) {
1575         llvm::Value *V;
1576         if (RV.isScalar())
1577           V = RV.getScalarVal();
1578         else
1579           V = Builder.CreateLoad(RV.getAggregateAddr());
1580 
1581         // If the argument doesn't match, perform a bitcast to coerce it.  This
1582         // can happen due to trivial type mismatches.
1583         if (IRArgNo < IRFuncTy->getNumParams() &&
1584             V->getType() != IRFuncTy->getParamType(IRArgNo))
1585           V = Builder.CreateBitCast(V, IRFuncTy->getParamType(IRArgNo));
1586         Args.push_back(V);
1587 
1588         checkArgMatches(V, IRArgNo, IRFuncTy);
1589         break;
1590       }
1591 
1592       // FIXME: Avoid the conversion through memory if possible.
1593       llvm::Value *SrcPtr;
1594       if (RV.isScalar()) {
1595         SrcPtr = CreateMemTemp(I->Ty, "coerce");
1596         EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, TypeAlign, I->Ty);
1597       } else if (RV.isComplex()) {
1598         SrcPtr = CreateMemTemp(I->Ty, "coerce");
1599         StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1600       } else
1601         SrcPtr = RV.getAggregateAddr();
1602 
1603       // If the value is offset in memory, apply the offset now.
1604       if (unsigned Offs = ArgInfo.getDirectOffset()) {
1605         SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1606         SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
1607         SrcPtr = Builder.CreateBitCast(SrcPtr,
1608                        llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1609 
1610       }
1611 
1612       // If the coerce-to type is a first class aggregate, we flatten it and
1613       // pass the elements. Either way is semantically identical, but fast-isel
1614       // and the optimizer generally likes scalar values better than FCAs.
1615       if (const llvm::StructType *STy =
1616             dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
1617         SrcPtr = Builder.CreateBitCast(SrcPtr,
1618                                        llvm::PointerType::getUnqual(STy));
1619         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1620           llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
1621           llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1622           // We don't know what we're loading from.
1623           LI->setAlignment(1);
1624           Args.push_back(LI);
1625 
1626           // Validate argument match.
1627           checkArgMatches(LI, IRArgNo, IRFuncTy);
1628         }
1629       } else {
1630         // In the simple case, just pass the coerced loaded value.
1631         Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1632                                          *this));
1633 
1634         // Validate argument match.
1635         checkArgMatches(Args.back(), IRArgNo, IRFuncTy);
1636       }
1637 
1638       break;
1639     }
1640 
1641     case ABIArgInfo::Expand:
1642       ExpandTypeToArgs(I->Ty, RV, Args, IRFuncTy);
1643       IRArgNo = Args.size();
1644       break;
1645     }
1646   }
1647 
1648   // If the callee is a bitcast of a function to a varargs pointer to function
1649   // type, check to see if we can remove the bitcast.  This handles some cases
1650   // with unprototyped functions.
1651   if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1652     if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1653       const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1654       const llvm::FunctionType *CurFT =
1655         cast<llvm::FunctionType>(CurPT->getElementType());
1656       const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
1657 
1658       if (CE->getOpcode() == llvm::Instruction::BitCast &&
1659           ActualFT->getReturnType() == CurFT->getReturnType() &&
1660           ActualFT->getNumParams() == CurFT->getNumParams() &&
1661           ActualFT->getNumParams() == Args.size() &&
1662           (CurFT->isVarArg() || !ActualFT->isVarArg())) {
1663         bool ArgsMatch = true;
1664         for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1665           if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1666             ArgsMatch = false;
1667             break;
1668           }
1669 
1670         // Strip the cast if we can get away with it.  This is a nice cleanup,
1671         // but also allows us to inline the function at -O0 if it is marked
1672         // always_inline.
1673         if (ArgsMatch)
1674           Callee = CalleeF;
1675       }
1676     }
1677 
1678   unsigned CallingConv;
1679   CodeGen::AttributeListType AttributeList;
1680   CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
1681   llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1682                                                    AttributeList.end());
1683 
1684   llvm::BasicBlock *InvokeDest = 0;
1685   if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1686     InvokeDest = getInvokeDest();
1687 
1688   llvm::CallSite CS;
1689   if (!InvokeDest) {
1690     CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
1691   } else {
1692     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1693     CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1694                               Args.data(), Args.data()+Args.size());
1695     EmitBlock(Cont);
1696   }
1697   if (callOrInvoke)
1698     *callOrInvoke = CS.getInstruction();
1699 
1700   CS.setAttributes(Attrs);
1701   CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1702 
1703   // If the call doesn't return, finish the basic block and clear the
1704   // insertion point; this allows the rest of IRgen to discard
1705   // unreachable code.
1706   if (CS.doesNotReturn()) {
1707     Builder.CreateUnreachable();
1708     Builder.ClearInsertionPoint();
1709 
1710     // FIXME: For now, emit a dummy basic block because expr emitters in
1711     // generally are not ready to handle emitting expressions at unreachable
1712     // points.
1713     EnsureInsertPoint();
1714 
1715     // Return a reasonable RValue.
1716     return GetUndefRValue(RetTy);
1717   }
1718 
1719   llvm::Instruction *CI = CS.getInstruction();
1720   if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
1721     CI->setName("call");
1722 
1723   // Emit any writebacks immediately.  Arguably this should happen
1724   // after any return-value munging.
1725   if (CallArgs.hasWritebacks())
1726     emitWritebacks(*this, CallArgs);
1727 
1728   switch (RetAI.getKind()) {
1729   case ABIArgInfo::Indirect: {
1730     unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1731     if (RetTy->isAnyComplexType())
1732       return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
1733     if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1734       return RValue::getAggregate(Args[0]);
1735     return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1736   }
1737 
1738   case ABIArgInfo::Ignore:
1739     // If we are ignoring an argument that had a result, make sure to
1740     // construct the appropriate return value for our caller.
1741     return GetUndefRValue(RetTy);
1742 
1743   case ABIArgInfo::Extend:
1744   case ABIArgInfo::Direct: {
1745     if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1746         RetAI.getDirectOffset() == 0) {
1747       if (RetTy->isAnyComplexType()) {
1748         llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1749         llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1750         return RValue::getComplex(std::make_pair(Real, Imag));
1751       }
1752       if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1753         llvm::Value *DestPtr = ReturnValue.getValue();
1754         bool DestIsVolatile = ReturnValue.isVolatile();
1755 
1756         if (!DestPtr) {
1757           DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1758           DestIsVolatile = false;
1759         }
1760         BuildAggStore(*this, CI, DestPtr, DestIsVolatile, false);
1761         return RValue::getAggregate(DestPtr);
1762       }
1763       return RValue::get(CI);
1764     }
1765 
1766     llvm::Value *DestPtr = ReturnValue.getValue();
1767     bool DestIsVolatile = ReturnValue.isVolatile();
1768 
1769     if (!DestPtr) {
1770       DestPtr = CreateMemTemp(RetTy, "coerce");
1771       DestIsVolatile = false;
1772     }
1773 
1774     // If the value is offset in memory, apply the offset now.
1775     llvm::Value *StorePtr = DestPtr;
1776     if (unsigned Offs = RetAI.getDirectOffset()) {
1777       StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1778       StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
1779       StorePtr = Builder.CreateBitCast(StorePtr,
1780                          llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1781     }
1782     CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
1783 
1784     unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1785     if (RetTy->isAnyComplexType())
1786       return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
1787     if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1788       return RValue::getAggregate(DestPtr);
1789     return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
1790   }
1791 
1792   case ABIArgInfo::Expand:
1793     assert(0 && "Invalid ABI kind for return argument");
1794   }
1795 
1796   assert(0 && "Unhandled ABIArgInfo::Kind");
1797   return RValue::get(0);
1798 }
1799 
1800 /* VarArg handling */
1801 
1802 llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1803   return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1804 }
1805