1 //===----- CGCall.h - 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 using namespace clang;
29 using namespace CodeGen;
30 
31 /***/
32 
33 static unsigned ClangCallConvToLLVMCallConv(CallingConv CC) {
34   switch (CC) {
35   default: return llvm::CallingConv::C;
36   case CC_X86StdCall: return llvm::CallingConv::X86_StdCall;
37   case CC_X86FastCall: return llvm::CallingConv::X86_FastCall;
38   case CC_X86ThisCall: return llvm::CallingConv::X86_ThisCall;
39   case CC_AAPCS: return llvm::CallingConv::ARM_AAPCS;
40   case CC_AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
41   // TODO: add support for CC_X86Pascal to llvm
42   }
43 }
44 
45 /// Derives the 'this' type for codegen purposes, i.e. ignoring method
46 /// qualification.
47 /// FIXME: address space qualification?
48 static CanQualType GetThisType(ASTContext &Context, const CXXRecordDecl *RD) {
49   QualType RecTy = Context.getTagDeclType(RD)->getCanonicalTypeInternal();
50   return Context.getPointerType(CanQualType::CreateUnsafe(RecTy));
51 }
52 
53 /// Returns the canonical formal type of the given C++ method.
54 static CanQual<FunctionProtoType> GetFormalType(const CXXMethodDecl *MD) {
55   return MD->getType()->getCanonicalTypeUnqualified()
56            .getAs<FunctionProtoType>();
57 }
58 
59 /// Returns the "extra-canonicalized" return type, which discards
60 /// qualifiers on the return type.  Codegen doesn't care about them,
61 /// and it makes ABI code a little easier to be able to assume that
62 /// all parameter and return types are top-level unqualified.
63 static CanQualType GetReturnType(QualType RetTy) {
64   return RetTy->getCanonicalTypeUnqualified().getUnqualifiedType();
65 }
66 
67 const CGFunctionInfo &
68 CodeGenTypes::getFunctionInfo(CanQual<FunctionNoProtoType> FTNP,
69                               bool IsRecursive) {
70   return getFunctionInfo(FTNP->getResultType().getUnqualifiedType(),
71                          llvm::SmallVector<CanQualType, 16>(),
72                          FTNP->getExtInfo(), IsRecursive);
73 }
74 
75 /// \param Args - contains any initial parameters besides those
76 ///   in the formal type
77 static const CGFunctionInfo &getFunctionInfo(CodeGenTypes &CGT,
78                                   llvm::SmallVectorImpl<CanQualType> &ArgTys,
79                                              CanQual<FunctionProtoType> FTP,
80                                              bool IsRecursive = false) {
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(), IsRecursive);
86 }
87 
88 const CGFunctionInfo &
89 CodeGenTypes::getFunctionInfo(CanQual<FunctionProtoType> FTP,
90                               bool IsRecursive) {
91   llvm::SmallVector<CanQualType, 16> ArgTys;
92   return ::getFunctionInfo(*this, ArgTys, FTP, IsRecursive);
93 }
94 
95 static CallingConv getCallingConventionForDecl(const Decl *D) {
96   // Set the appropriate calling convention for the Function.
97   if (D->hasAttr<StdCallAttr>())
98     return CC_X86StdCall;
99 
100   if (D->hasAttr<FastCallAttr>())
101     return CC_X86FastCall;
102 
103   if (D->hasAttr<ThisCallAttr>())
104     return CC_X86ThisCall;
105 
106   if (D->hasAttr<PascalAttr>())
107     return CC_X86Pascal;
108 
109   if (PcsAttr *PCS = D->getAttr<PcsAttr>())
110     return (PCS->getPCS() == PcsAttr::AAPCS ? CC_AAPCS : CC_AAPCS_VFP);
111 
112   return CC_C;
113 }
114 
115 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXRecordDecl *RD,
116                                                  const FunctionProtoType *FTP) {
117   llvm::SmallVector<CanQualType, 16> ArgTys;
118 
119   // Add the 'this' pointer.
120   ArgTys.push_back(GetThisType(Context, RD));
121 
122   return ::getFunctionInfo(*this, ArgTys,
123               FTP->getCanonicalTypeUnqualified().getAs<FunctionProtoType>());
124 }
125 
126 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXMethodDecl *MD) {
127   llvm::SmallVector<CanQualType, 16> ArgTys;
128 
129   assert(!isa<CXXConstructorDecl>(MD) && "wrong method for contructors!");
130   assert(!isa<CXXDestructorDecl>(MD) && "wrong method for destructors!");
131 
132   // Add the 'this' pointer unless this is a static method.
133   if (MD->isInstance())
134     ArgTys.push_back(GetThisType(Context, MD->getParent()));
135 
136   return ::getFunctionInfo(*this, ArgTys, GetFormalType(MD));
137 }
138 
139 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXConstructorDecl *D,
140                                                     CXXCtorType Type) {
141   llvm::SmallVector<CanQualType, 16> ArgTys;
142   ArgTys.push_back(GetThisType(Context, D->getParent()));
143   CanQualType ResTy = Context.VoidTy;
144 
145   TheCXXABI.BuildConstructorSignature(D, Type, ResTy, ArgTys);
146 
147   CanQual<FunctionProtoType> FTP = GetFormalType(D);
148 
149   // Add the formal parameters.
150   for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
151     ArgTys.push_back(FTP->getArgType(i));
152 
153   return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
154 }
155 
156 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const CXXDestructorDecl *D,
157                                                     CXXDtorType Type) {
158   llvm::SmallVector<CanQualType, 2> ArgTys;
159   ArgTys.push_back(GetThisType(Context, D->getParent()));
160   CanQualType ResTy = Context.VoidTy;
161 
162   TheCXXABI.BuildDestructorSignature(D, Type, ResTy, ArgTys);
163 
164   CanQual<FunctionProtoType> FTP = GetFormalType(D);
165   assert(FTP->getNumArgs() == 0 && "dtor with formal parameters");
166 
167   return getFunctionInfo(ResTy, ArgTys, FTP->getExtInfo());
168 }
169 
170 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
171   if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
172     if (MD->isInstance())
173       return getFunctionInfo(MD);
174 
175   CanQualType FTy = FD->getType()->getCanonicalTypeUnqualified();
176   assert(isa<FunctionType>(FTy));
177   if (isa<FunctionNoProtoType>(FTy))
178     return getFunctionInfo(FTy.getAs<FunctionNoProtoType>());
179   assert(isa<FunctionProtoType>(FTy));
180   return getFunctionInfo(FTy.getAs<FunctionProtoType>());
181 }
182 
183 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
184   llvm::SmallVector<CanQualType, 16> ArgTys;
185   ArgTys.push_back(Context.getCanonicalParamType(MD->getSelfDecl()->getType()));
186   ArgTys.push_back(Context.getCanonicalParamType(Context.getObjCSelType()));
187   // FIXME: Kill copy?
188   for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
189          e = MD->param_end(); i != e; ++i) {
190     ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
191   }
192   return getFunctionInfo(GetReturnType(MD->getResultType()),
193                          ArgTys,
194                          FunctionType::ExtInfo(
195                              /*NoReturn*/ false,
196                              /*HasRegParm*/ false,
197                              /*RegParm*/ 0,
198                              getCallingConventionForDecl(MD)));
199 }
200 
201 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(GlobalDecl GD) {
202   // FIXME: Do we need to handle ObjCMethodDecl?
203   const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
204 
205   if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
206     return getFunctionInfo(CD, GD.getCtorType());
207 
208   if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD))
209     return getFunctionInfo(DD, GD.getDtorType());
210 
211   return getFunctionInfo(FD);
212 }
213 
214 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
215                                                     const CallArgList &Args,
216                                             const FunctionType::ExtInfo &Info) {
217   // FIXME: Kill copy.
218   llvm::SmallVector<CanQualType, 16> ArgTys;
219   for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
220        i != e; ++i)
221     ArgTys.push_back(Context.getCanonicalParamType(i->second));
222   return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
223 }
224 
225 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
226                                                     const FunctionArgList &Args,
227                                             const FunctionType::ExtInfo &Info) {
228   // FIXME: Kill copy.
229   llvm::SmallVector<CanQualType, 16> ArgTys;
230   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
231        i != e; ++i)
232     ArgTys.push_back(Context.getCanonicalParamType((*i)->getType()));
233   return getFunctionInfo(GetReturnType(ResTy), ArgTys, Info);
234 }
235 
236 const CGFunctionInfo &CodeGenTypes::getNullaryFunctionInfo() {
237   llvm::SmallVector<CanQualType, 1> args;
238   return getFunctionInfo(getContext().VoidTy, args, FunctionType::ExtInfo());
239 }
240 
241 const CGFunctionInfo &CodeGenTypes::getFunctionInfo(CanQualType ResTy,
242                            const llvm::SmallVectorImpl<CanQualType> &ArgTys,
243                                             const FunctionType::ExtInfo &Info,
244                                                     bool IsRecursive) {
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,
256                           ArgTys.begin(), ArgTys.end());
257 
258   void *InsertPos = 0;
259   CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
260   if (FI)
261     return *FI;
262 
263   // Construct the function info.
264   FI = new CGFunctionInfo(CC, Info.getNoReturn(), 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(ConvertTypeRecursive(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(ConvertTypeRecursive(I->type));
282 
283   // If this is a top-level call and ConvertTypeRecursive hit unresolved pointer
284   // types, resolve them now.  These pointers may point to this function, which
285   // we *just* filled in the FunctionInfo for.
286   if (!IsRecursive && !PointersToResolve.empty())
287     HandleLateResolvedPointers();
288 
289   return *FI;
290 }
291 
292 CGFunctionInfo::CGFunctionInfo(unsigned _CallingConvention,
293                                bool _NoReturn, bool _HasRegParm, unsigned _RegParm,
294                                CanQualType ResTy,
295                                const CanQualType *ArgTys,
296                                unsigned NumArgTys)
297   : CallingConvention(_CallingConvention),
298     EffectiveCallingConvention(_CallingConvention),
299     NoReturn(_NoReturn), HasRegParm(_HasRegParm), RegParm(_RegParm)
300 {
301   NumArgs = NumArgTys;
302 
303   // FIXME: Coallocate with the CGFunctionInfo object.
304   Args = new ArgInfo[1 + NumArgTys];
305   Args[0].type = ResTy;
306   for (unsigned i = 0; i != NumArgTys; ++i)
307     Args[1 + i].type = ArgTys[i];
308 }
309 
310 /***/
311 
312 void CodeGenTypes::GetExpandedTypes(QualType Ty,
313                                     std::vector<const llvm::Type*> &ArgTys,
314                                     bool IsRecursive) {
315   const RecordType *RT = Ty->getAsStructureType();
316   assert(RT && "Can only expand structure types.");
317   const RecordDecl *RD = RT->getDecl();
318   assert(!RD->hasFlexibleArrayMember() &&
319          "Cannot expand structure with flexible array.");
320 
321   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
322          i != e; ++i) {
323     const FieldDecl *FD = *i;
324     assert(!FD->isBitField() &&
325            "Cannot expand structure with bit-field members.");
326 
327     QualType FT = FD->getType();
328     if (CodeGenFunction::hasAggregateLLVMType(FT))
329       GetExpandedTypes(FT, ArgTys, IsRecursive);
330     else
331       ArgTys.push_back(ConvertType(FT, IsRecursive));
332   }
333 }
334 
335 llvm::Function::arg_iterator
336 CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
337                                     llvm::Function::arg_iterator AI) {
338   const RecordType *RT = Ty->getAsStructureType();
339   assert(RT && "Can only expand structure types.");
340 
341   RecordDecl *RD = RT->getDecl();
342   assert(LV.isSimple() &&
343          "Unexpected non-simple lvalue during struct expansion.");
344   llvm::Value *Addr = LV.getAddress();
345   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
346          i != e; ++i) {
347     FieldDecl *FD = *i;
348     QualType FT = FD->getType();
349 
350     // FIXME: What are the right qualifiers here?
351     LValue LV = EmitLValueForField(Addr, FD, 0);
352     if (CodeGenFunction::hasAggregateLLVMType(FT)) {
353       AI = ExpandTypeFromArgs(FT, LV, AI);
354     } else {
355       EmitStoreThroughLValue(RValue::get(AI), LV, FT);
356       ++AI;
357     }
358   }
359 
360   return AI;
361 }
362 
363 void
364 CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
365                                   llvm::SmallVector<llvm::Value*, 16> &Args) {
366   const RecordType *RT = Ty->getAsStructureType();
367   assert(RT && "Can only expand structure types.");
368 
369   RecordDecl *RD = RT->getDecl();
370   assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
371   llvm::Value *Addr = RV.getAggregateAddr();
372   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
373          i != e; ++i) {
374     FieldDecl *FD = *i;
375     QualType FT = FD->getType();
376 
377     // FIXME: What are the right qualifiers here?
378     LValue LV = EmitLValueForField(Addr, FD, 0);
379     if (CodeGenFunction::hasAggregateLLVMType(FT)) {
380       ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
381     } else {
382       RValue RV = EmitLoadOfLValue(LV, FT);
383       assert(RV.isScalar() &&
384              "Unexpected non-scalar rvalue during struct expansion.");
385       Args.push_back(RV.getScalarVal());
386     }
387   }
388 }
389 
390 /// EnterStructPointerForCoercedAccess - Given a struct pointer that we are
391 /// accessing some number of bytes out of it, try to gep into the struct to get
392 /// at its inner goodness.  Dive as deep as possible without entering an element
393 /// with an in-memory size smaller than DstSize.
394 static llvm::Value *
395 EnterStructPointerForCoercedAccess(llvm::Value *SrcPtr,
396                                    const llvm::StructType *SrcSTy,
397                                    uint64_t DstSize, CodeGenFunction &CGF) {
398   // We can't dive into a zero-element struct.
399   if (SrcSTy->getNumElements() == 0) return SrcPtr;
400 
401   const llvm::Type *FirstElt = SrcSTy->getElementType(0);
402 
403   // If the first elt is at least as large as what we're looking for, or if the
404   // first element is the same size as the whole struct, we can enter it.
405   uint64_t FirstEltSize =
406     CGF.CGM.getTargetData().getTypeAllocSize(FirstElt);
407   if (FirstEltSize < DstSize &&
408       FirstEltSize < CGF.CGM.getTargetData().getTypeAllocSize(SrcSTy))
409     return SrcPtr;
410 
411   // GEP into the first element.
412   SrcPtr = CGF.Builder.CreateConstGEP2_32(SrcPtr, 0, 0, "coerce.dive");
413 
414   // If the first element is a struct, recurse.
415   const llvm::Type *SrcTy =
416     cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
417   if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy))
418     return EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
419 
420   return SrcPtr;
421 }
422 
423 /// CoerceIntOrPtrToIntOrPtr - Convert a value Val to the specific Ty where both
424 /// are either integers or pointers.  This does a truncation of the value if it
425 /// is too large or a zero extension if it is too small.
426 static llvm::Value *CoerceIntOrPtrToIntOrPtr(llvm::Value *Val,
427                                              const llvm::Type *Ty,
428                                              CodeGenFunction &CGF) {
429   if (Val->getType() == Ty)
430     return Val;
431 
432   if (isa<llvm::PointerType>(Val->getType())) {
433     // If this is Pointer->Pointer avoid conversion to and from int.
434     if (isa<llvm::PointerType>(Ty))
435       return CGF.Builder.CreateBitCast(Val, Ty, "coerce.val");
436 
437     // Convert the pointer to an integer so we can play with its width.
438     Val = CGF.Builder.CreatePtrToInt(Val, CGF.IntPtrTy, "coerce.val.pi");
439   }
440 
441   const llvm::Type *DestIntTy = Ty;
442   if (isa<llvm::PointerType>(DestIntTy))
443     DestIntTy = CGF.IntPtrTy;
444 
445   if (Val->getType() != DestIntTy)
446     Val = CGF.Builder.CreateIntCast(Val, DestIntTy, false, "coerce.val.ii");
447 
448   if (isa<llvm::PointerType>(Ty))
449     Val = CGF.Builder.CreateIntToPtr(Val, Ty, "coerce.val.ip");
450   return Val;
451 }
452 
453 
454 
455 /// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
456 /// a pointer to an object of type \arg Ty.
457 ///
458 /// This safely handles the case when the src type is smaller than the
459 /// destination type; in this situation the values of bits which not
460 /// present in the src are undefined.
461 static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
462                                       const llvm::Type *Ty,
463                                       CodeGenFunction &CGF) {
464   const llvm::Type *SrcTy =
465     cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
466 
467   // If SrcTy and Ty are the same, just do a load.
468   if (SrcTy == Ty)
469     return CGF.Builder.CreateLoad(SrcPtr);
470 
471   uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(Ty);
472 
473   if (const llvm::StructType *SrcSTy = dyn_cast<llvm::StructType>(SrcTy)) {
474     SrcPtr = EnterStructPointerForCoercedAccess(SrcPtr, SrcSTy, DstSize, CGF);
475     SrcTy = cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
476   }
477 
478   uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
479 
480   // If the source and destination are integer or pointer types, just do an
481   // extension or truncation to the desired type.
482   if ((isa<llvm::IntegerType>(Ty) || isa<llvm::PointerType>(Ty)) &&
483       (isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy))) {
484     llvm::LoadInst *Load = CGF.Builder.CreateLoad(SrcPtr);
485     return CoerceIntOrPtrToIntOrPtr(Load, Ty, CGF);
486   }
487 
488   // If load is legal, just bitcast the src pointer.
489   if (SrcSize >= DstSize) {
490     // Generally SrcSize is never greater than DstSize, since this means we are
491     // losing bits. However, this can happen in cases where the structure has
492     // additional padding, for example due to a user specified alignment.
493     //
494     // FIXME: Assert that we aren't truncating non-padding bits when have access
495     // to that information.
496     llvm::Value *Casted =
497       CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
498     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
499     // FIXME: Use better alignment / avoid requiring aligned load.
500     Load->setAlignment(1);
501     return Load;
502   }
503 
504   // Otherwise do coercion through memory. This is stupid, but
505   // simple.
506   llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
507   llvm::Value *Casted =
508     CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
509   llvm::StoreInst *Store =
510     CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
511   // FIXME: Use better alignment / avoid requiring aligned store.
512   Store->setAlignment(1);
513   return CGF.Builder.CreateLoad(Tmp);
514 }
515 
516 /// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
517 /// where the source and destination may have different types.
518 ///
519 /// This safely handles the case when the src type is larger than the
520 /// destination type; the upper bits of the src will be lost.
521 static void CreateCoercedStore(llvm::Value *Src,
522                                llvm::Value *DstPtr,
523                                bool DstIsVolatile,
524                                CodeGenFunction &CGF) {
525   const llvm::Type *SrcTy = Src->getType();
526   const llvm::Type *DstTy =
527     cast<llvm::PointerType>(DstPtr->getType())->getElementType();
528   if (SrcTy == DstTy) {
529     CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
530     return;
531   }
532 
533   uint64_t SrcSize = CGF.CGM.getTargetData().getTypeAllocSize(SrcTy);
534 
535   if (const llvm::StructType *DstSTy = dyn_cast<llvm::StructType>(DstTy)) {
536     DstPtr = EnterStructPointerForCoercedAccess(DstPtr, DstSTy, SrcSize, CGF);
537     DstTy = cast<llvm::PointerType>(DstPtr->getType())->getElementType();
538   }
539 
540   // If the source and destination are integer or pointer types, just do an
541   // extension or truncation to the desired type.
542   if ((isa<llvm::IntegerType>(SrcTy) || isa<llvm::PointerType>(SrcTy)) &&
543       (isa<llvm::IntegerType>(DstTy) || isa<llvm::PointerType>(DstTy))) {
544     Src = CoerceIntOrPtrToIntOrPtr(Src, DstTy, CGF);
545     CGF.Builder.CreateStore(Src, DstPtr, DstIsVolatile);
546     return;
547   }
548 
549   uint64_t DstSize = CGF.CGM.getTargetData().getTypeAllocSize(DstTy);
550 
551   // If store is legal, just bitcast the src pointer.
552   if (SrcSize <= DstSize) {
553     llvm::Value *Casted =
554       CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
555     // FIXME: Use better alignment / avoid requiring aligned store.
556     CGF.Builder.CreateStore(Src, Casted, DstIsVolatile)->setAlignment(1);
557   } else {
558     // Otherwise do coercion through memory. This is stupid, but
559     // simple.
560 
561     // Generally SrcSize is never greater than DstSize, since this means we are
562     // losing bits. However, this can happen in cases where the structure has
563     // additional padding, for example due to a user specified alignment.
564     //
565     // FIXME: Assert that we aren't truncating non-padding bits when have access
566     // to that information.
567     llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
568     CGF.Builder.CreateStore(Src, Tmp);
569     llvm::Value *Casted =
570       CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
571     llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
572     // FIXME: Use better alignment / avoid requiring aligned load.
573     Load->setAlignment(1);
574     CGF.Builder.CreateStore(Load, DstPtr, DstIsVolatile);
575   }
576 }
577 
578 /***/
579 
580 bool CodeGenModule::ReturnTypeUsesSRet(const CGFunctionInfo &FI) {
581   return FI.getReturnInfo().isIndirect();
582 }
583 
584 bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) {
585   if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
586     switch (BT->getKind()) {
587     default:
588       return false;
589     case BuiltinType::Float:
590       return getContext().Target.useObjCFPRetForRealType(TargetInfo::Float);
591     case BuiltinType::Double:
592       return getContext().Target.useObjCFPRetForRealType(TargetInfo::Double);
593     case BuiltinType::LongDouble:
594       return getContext().Target.useObjCFPRetForRealType(
595         TargetInfo::LongDouble);
596     }
597   }
598 
599   return false;
600 }
601 
602 const llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) {
603   const CGFunctionInfo &FI = getFunctionInfo(GD);
604 
605   // For definition purposes, don't consider a K&R function variadic.
606   bool Variadic = false;
607   if (const FunctionProtoType *FPT =
608         cast<FunctionDecl>(GD.getDecl())->getType()->getAs<FunctionProtoType>())
609     Variadic = FPT->isVariadic();
610 
611   return GetFunctionType(FI, Variadic, false);
612 }
613 
614 const llvm::FunctionType *
615 CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic,
616                               bool IsRecursive) {
617   std::vector<const llvm::Type*> ArgTys;
618 
619   const llvm::Type *ResultType = 0;
620 
621   QualType RetTy = FI.getReturnType();
622   const ABIArgInfo &RetAI = FI.getReturnInfo();
623   switch (RetAI.getKind()) {
624   case ABIArgInfo::Expand:
625     assert(0 && "Invalid ABI kind for return argument");
626 
627   case ABIArgInfo::Extend:
628   case ABIArgInfo::Direct:
629     ResultType = RetAI.getCoerceToType();
630     break;
631 
632   case ABIArgInfo::Indirect: {
633     assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
634     ResultType = llvm::Type::getVoidTy(getLLVMContext());
635     const llvm::Type *STy = ConvertType(RetTy, IsRecursive);
636     unsigned AS = Context.getTargetAddressSpace(RetTy);
637     ArgTys.push_back(llvm::PointerType::get(STy, AS));
638     break;
639   }
640 
641   case ABIArgInfo::Ignore:
642     ResultType = llvm::Type::getVoidTy(getLLVMContext());
643     break;
644   }
645 
646   for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
647          ie = FI.arg_end(); it != ie; ++it) {
648     const ABIArgInfo &AI = it->info;
649 
650     switch (AI.getKind()) {
651     case ABIArgInfo::Ignore:
652       break;
653 
654     case ABIArgInfo::Indirect: {
655       // indirect arguments are always on the stack, which is addr space #0.
656       const llvm::Type *LTy = ConvertTypeForMem(it->type, IsRecursive);
657       ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
658       break;
659     }
660 
661     case ABIArgInfo::Extend:
662     case ABIArgInfo::Direct: {
663       // If the coerce-to type is a first class aggregate, flatten it.  Either
664       // way is semantically identical, but fast-isel and the optimizer
665       // generally likes scalar values better than FCAs.
666       const llvm::Type *ArgTy = AI.getCoerceToType();
667       if (const llvm::StructType *STy = dyn_cast<llvm::StructType>(ArgTy)) {
668         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
669           ArgTys.push_back(STy->getElementType(i));
670       } else {
671         ArgTys.push_back(ArgTy);
672       }
673       break;
674     }
675 
676     case ABIArgInfo::Expand:
677       GetExpandedTypes(it->type, ArgTys, IsRecursive);
678       break;
679     }
680   }
681 
682   return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
683 }
684 
685 const llvm::Type *CodeGenTypes::GetFunctionTypeForVTable(GlobalDecl GD) {
686   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
687   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
688 
689   if (!VerifyFuncTypeComplete(FPT)) {
690     const CGFunctionInfo *Info;
691     if (isa<CXXDestructorDecl>(MD))
692       Info = &getFunctionInfo(cast<CXXDestructorDecl>(MD), GD.getDtorType());
693     else
694       Info = &getFunctionInfo(MD);
695     return GetFunctionType(*Info, FPT->isVariadic(), false);
696   }
697 
698   return llvm::OpaqueType::get(getLLVMContext());
699 }
700 
701 void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
702                                            const Decl *TargetDecl,
703                                            AttributeListType &PAL,
704                                            unsigned &CallingConv) {
705   unsigned FuncAttrs = 0;
706   unsigned RetAttrs = 0;
707 
708   CallingConv = FI.getEffectiveCallingConvention();
709 
710   if (FI.isNoReturn())
711     FuncAttrs |= llvm::Attribute::NoReturn;
712 
713   // FIXME: handle sseregparm someday...
714   if (TargetDecl) {
715     if (TargetDecl->hasAttr<NoThrowAttr>())
716       FuncAttrs |= llvm::Attribute::NoUnwind;
717     else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
718       const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
719       if (FPT && FPT->isNothrow(getContext()))
720         FuncAttrs |= llvm::Attribute::NoUnwind;
721     }
722 
723     if (TargetDecl->hasAttr<NoReturnAttr>())
724       FuncAttrs |= llvm::Attribute::NoReturn;
725     if (TargetDecl->hasAttr<ConstAttr>())
726       FuncAttrs |= llvm::Attribute::ReadNone;
727     else if (TargetDecl->hasAttr<PureAttr>())
728       FuncAttrs |= llvm::Attribute::ReadOnly;
729     if (TargetDecl->hasAttr<MallocAttr>())
730       RetAttrs |= llvm::Attribute::NoAlias;
731   }
732 
733   if (CodeGenOpts.OptimizeSize)
734     FuncAttrs |= llvm::Attribute::OptimizeForSize;
735   if (CodeGenOpts.DisableRedZone)
736     FuncAttrs |= llvm::Attribute::NoRedZone;
737   if (CodeGenOpts.NoImplicitFloat)
738     FuncAttrs |= llvm::Attribute::NoImplicitFloat;
739 
740   QualType RetTy = FI.getReturnType();
741   unsigned Index = 1;
742   const ABIArgInfo &RetAI = FI.getReturnInfo();
743   switch (RetAI.getKind()) {
744   case ABIArgInfo::Extend:
745    if (RetTy->hasSignedIntegerRepresentation())
746      RetAttrs |= llvm::Attribute::SExt;
747    else if (RetTy->hasUnsignedIntegerRepresentation())
748      RetAttrs |= llvm::Attribute::ZExt;
749     break;
750   case ABIArgInfo::Direct:
751   case ABIArgInfo::Ignore:
752     break;
753 
754   case ABIArgInfo::Indirect:
755     PAL.push_back(llvm::AttributeWithIndex::get(Index,
756                                                 llvm::Attribute::StructRet));
757     ++Index;
758     // sret disables readnone and readonly
759     FuncAttrs &= ~(llvm::Attribute::ReadOnly |
760                    llvm::Attribute::ReadNone);
761     break;
762 
763   case ABIArgInfo::Expand:
764     assert(0 && "Invalid ABI kind for return argument");
765   }
766 
767   if (RetAttrs)
768     PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
769 
770   // FIXME: RegParm should be reduced in case of global register variable.
771   signed RegParm;
772   if (FI.getHasRegParm())
773     RegParm = FI.getRegParm();
774   else
775     RegParm = CodeGenOpts.NumRegisterParameters;
776 
777   unsigned PointerWidth = getContext().Target.getPointerWidth(0);
778   for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
779          ie = FI.arg_end(); it != ie; ++it) {
780     QualType ParamType = it->type;
781     const ABIArgInfo &AI = it->info;
782     unsigned Attributes = 0;
783 
784     // 'restrict' -> 'noalias' is done in EmitFunctionProlog when we
785     // have the corresponding parameter variable.  It doesn't make
786     // sense to do it here because parameters are so messed up.
787     switch (AI.getKind()) {
788     case ABIArgInfo::Extend:
789       if (ParamType->isSignedIntegerType())
790         Attributes |= llvm::Attribute::SExt;
791       else if (ParamType->isUnsignedIntegerType())
792         Attributes |= llvm::Attribute::ZExt;
793       // FALL THROUGH
794     case ABIArgInfo::Direct:
795       if (RegParm > 0 &&
796           (ParamType->isIntegerType() || ParamType->isPointerType())) {
797         RegParm -=
798         (Context.getTypeSize(ParamType) + PointerWidth - 1) / PointerWidth;
799         if (RegParm >= 0)
800           Attributes |= llvm::Attribute::InReg;
801       }
802       // FIXME: handle sseregparm someday...
803 
804       if (const llvm::StructType *STy =
805             dyn_cast<llvm::StructType>(AI.getCoerceToType()))
806         Index += STy->getNumElements()-1;  // 1 will be added below.
807       break;
808 
809     case ABIArgInfo::Indirect:
810       if (AI.getIndirectByVal())
811         Attributes |= llvm::Attribute::ByVal;
812 
813       Attributes |=
814         llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
815       // byval disables readnone and readonly.
816       FuncAttrs &= ~(llvm::Attribute::ReadOnly |
817                      llvm::Attribute::ReadNone);
818       break;
819 
820     case ABIArgInfo::Ignore:
821       // Skip increment, no matching LLVM parameter.
822       continue;
823 
824     case ABIArgInfo::Expand: {
825       std::vector<const llvm::Type*> Tys;
826       // FIXME: This is rather inefficient. Do we ever actually need to do
827       // anything here? The result should be just reconstructed on the other
828       // side, so extension should be a non-issue.
829       getTypes().GetExpandedTypes(ParamType, Tys, false);
830       Index += Tys.size();
831       continue;
832     }
833     }
834 
835     if (Attributes)
836       PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
837     ++Index;
838   }
839   if (FuncAttrs)
840     PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
841 }
842 
843 /// An argument came in as a promoted argument; demote it back to its
844 /// declared type.
845 static llvm::Value *emitArgumentDemotion(CodeGenFunction &CGF,
846                                          const VarDecl *var,
847                                          llvm::Value *value) {
848   const llvm::Type *varType = CGF.ConvertType(var->getType());
849 
850   // This can happen with promotions that actually don't change the
851   // underlying type, like the enum promotions.
852   if (value->getType() == varType) return value;
853 
854   assert((varType->isIntegerTy() || varType->isFloatingPointTy())
855          && "unexpected promotion type");
856 
857   if (isa<llvm::IntegerType>(varType))
858     return CGF.Builder.CreateTrunc(value, varType, "arg.unpromote");
859 
860   return CGF.Builder.CreateFPCast(value, varType, "arg.unpromote");
861 }
862 
863 void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
864                                          llvm::Function *Fn,
865                                          const FunctionArgList &Args) {
866   // If this is an implicit-return-zero function, go ahead and
867   // initialize the return value.  TODO: it might be nice to have
868   // a more general mechanism for this that didn't require synthesized
869   // return statements.
870   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(CurFuncDecl)) {
871     if (FD->hasImplicitReturnZero()) {
872       QualType RetTy = FD->getResultType().getUnqualifiedType();
873       const llvm::Type* LLVMTy = CGM.getTypes().ConvertType(RetTy);
874       llvm::Constant* Zero = llvm::Constant::getNullValue(LLVMTy);
875       Builder.CreateStore(Zero, ReturnValue);
876     }
877   }
878 
879   // FIXME: We no longer need the types from FunctionArgList; lift up and
880   // simplify.
881 
882   // Emit allocs for param decls.  Give the LLVM Argument nodes names.
883   llvm::Function::arg_iterator AI = Fn->arg_begin();
884 
885   // Name the struct return argument.
886   if (CGM.ReturnTypeUsesSRet(FI)) {
887     AI->setName("agg.result");
888     ++AI;
889   }
890 
891   assert(FI.arg_size() == Args.size() &&
892          "Mismatch between function signature & arguments.");
893   unsigned ArgNo = 1;
894   CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
895   for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
896        i != e; ++i, ++info_it, ++ArgNo) {
897     const VarDecl *Arg = *i;
898     QualType Ty = info_it->type;
899     const ABIArgInfo &ArgI = info_it->info;
900 
901     bool isPromoted =
902       isa<ParmVarDecl>(Arg) && cast<ParmVarDecl>(Arg)->isKNRPromoted();
903 
904     switch (ArgI.getKind()) {
905     case ABIArgInfo::Indirect: {
906       llvm::Value *V = AI;
907 
908       if (hasAggregateLLVMType(Ty)) {
909         // Aggregates and complex variables are accessed by reference.  All we
910         // need to do is realign the value, if requested
911         if (ArgI.getIndirectRealign()) {
912           llvm::Value *AlignedTemp = CreateMemTemp(Ty, "coerce");
913 
914           // Copy from the incoming argument pointer to the temporary with the
915           // appropriate alignment.
916           //
917           // FIXME: We should have a common utility for generating an aggregate
918           // copy.
919           const llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
920           CharUnits Size = getContext().getTypeSizeInChars(Ty);
921           llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
922           llvm::Value *Src = Builder.CreateBitCast(V, I8PtrTy);
923           Builder.CreateMemCpy(Dst,
924                                Src,
925                                llvm::ConstantInt::get(IntPtrTy,
926                                                       Size.getQuantity()),
927                                ArgI.getIndirectAlign(),
928                                false);
929           V = AlignedTemp;
930         }
931       } else {
932         // Load scalar value from indirect argument.
933         CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
934         V = EmitLoadOfScalar(V, false, Alignment.getQuantity(), Ty);
935 
936         if (isPromoted)
937           V = emitArgumentDemotion(*this, Arg, V);
938       }
939       EmitParmDecl(*Arg, V, ArgNo);
940       break;
941     }
942 
943     case ABIArgInfo::Extend:
944     case ABIArgInfo::Direct: {
945       // If we have the trivial case, handle it with no muss and fuss.
946       if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
947           ArgI.getCoerceToType() == ConvertType(Ty) &&
948           ArgI.getDirectOffset() == 0) {
949         assert(AI != Fn->arg_end() && "Argument mismatch!");
950         llvm::Value *V = AI;
951 
952         if (Arg->getType().isRestrictQualified())
953           AI->addAttr(llvm::Attribute::NoAlias);
954 
955         if (isPromoted)
956           V = emitArgumentDemotion(*this, Arg, V);
957 
958         EmitParmDecl(*Arg, V, ArgNo);
959         break;
960       }
961 
962       llvm::AllocaInst *Alloca = CreateMemTemp(Ty, "coerce");
963 
964       // The alignment we need to use is the max of the requested alignment for
965       // the argument plus the alignment required by our access code below.
966       unsigned AlignmentToUse =
967         CGM.getTargetData().getABITypeAlignment(ArgI.getCoerceToType());
968       AlignmentToUse = std::max(AlignmentToUse,
969                         (unsigned)getContext().getDeclAlign(Arg).getQuantity());
970 
971       Alloca->setAlignment(AlignmentToUse);
972       llvm::Value *V = Alloca;
973       llvm::Value *Ptr = V;    // Pointer to store into.
974 
975       // If the value is offset in memory, apply the offset now.
976       if (unsigned Offs = ArgI.getDirectOffset()) {
977         Ptr = Builder.CreateBitCast(Ptr, Builder.getInt8PtrTy());
978         Ptr = Builder.CreateConstGEP1_32(Ptr, Offs);
979         Ptr = Builder.CreateBitCast(Ptr,
980                           llvm::PointerType::getUnqual(ArgI.getCoerceToType()));
981       }
982 
983       // If the coerce-to type is a first class aggregate, we flatten it and
984       // pass the elements. Either way is semantically identical, but fast-isel
985       // and the optimizer generally likes scalar values better than FCAs.
986       if (const llvm::StructType *STy =
987             dyn_cast<llvm::StructType>(ArgI.getCoerceToType())) {
988         Ptr = Builder.CreateBitCast(Ptr, llvm::PointerType::getUnqual(STy));
989 
990         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
991           assert(AI != Fn->arg_end() && "Argument mismatch!");
992           AI->setName(Arg->getName() + ".coerce" + llvm::Twine(i));
993           llvm::Value *EltPtr = Builder.CreateConstGEP2_32(Ptr, 0, i);
994           Builder.CreateStore(AI++, EltPtr);
995         }
996       } else {
997         // Simple case, just do a coerced store of the argument into the alloca.
998         assert(AI != Fn->arg_end() && "Argument mismatch!");
999         AI->setName(Arg->getName() + ".coerce");
1000         CreateCoercedStore(AI++, Ptr, /*DestIsVolatile=*/false, *this);
1001       }
1002 
1003 
1004       // Match to what EmitParmDecl is expecting for this type.
1005       if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1006         V = EmitLoadOfScalar(V, false, AlignmentToUse, Ty);
1007         if (isPromoted)
1008           V = emitArgumentDemotion(*this, Arg, V);
1009       }
1010       EmitParmDecl(*Arg, V, ArgNo);
1011       continue;  // Skip ++AI increment, already done.
1012     }
1013 
1014     case ABIArgInfo::Expand: {
1015       // If this structure was expanded into multiple arguments then
1016       // we need to create a temporary and reconstruct it from the
1017       // arguments.
1018       llvm::Value *Temp = CreateMemTemp(Ty, Arg->getName() + ".addr");
1019       llvm::Function::arg_iterator End =
1020         ExpandTypeFromArgs(Ty, MakeAddrLValue(Temp, Ty), AI);
1021       EmitParmDecl(*Arg, Temp, ArgNo);
1022 
1023       // Name the arguments used in expansion and increment AI.
1024       unsigned Index = 0;
1025       for (; AI != End; ++AI, ++Index)
1026         AI->setName(Arg->getName() + "." + llvm::Twine(Index));
1027       continue;
1028     }
1029 
1030     case ABIArgInfo::Ignore:
1031       // Initialize the local variable appropriately.
1032       if (hasAggregateLLVMType(Ty))
1033         EmitParmDecl(*Arg, CreateMemTemp(Ty), ArgNo);
1034       else
1035         EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())),
1036                      ArgNo);
1037 
1038       // Skip increment, no matching LLVM parameter.
1039       continue;
1040     }
1041 
1042     ++AI;
1043   }
1044   assert(AI == Fn->arg_end() && "Argument mismatch!");
1045 }
1046 
1047 void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI) {
1048   // Functions with no result always return void.
1049   if (ReturnValue == 0) {
1050     Builder.CreateRetVoid();
1051     return;
1052   }
1053 
1054   llvm::DebugLoc RetDbgLoc;
1055   llvm::Value *RV = 0;
1056   QualType RetTy = FI.getReturnType();
1057   const ABIArgInfo &RetAI = FI.getReturnInfo();
1058 
1059   switch (RetAI.getKind()) {
1060   case ABIArgInfo::Indirect: {
1061     unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1062     if (RetTy->isAnyComplexType()) {
1063       ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1064       StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1065     } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1066       // Do nothing; aggregrates get evaluated directly into the destination.
1067     } else {
1068       EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1069                         false, Alignment, RetTy);
1070     }
1071     break;
1072   }
1073 
1074   case ABIArgInfo::Extend:
1075   case ABIArgInfo::Direct:
1076     if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1077         RetAI.getDirectOffset() == 0) {
1078       // The internal return value temp always will have pointer-to-return-type
1079       // type, just do a load.
1080 
1081       // If the instruction right before the insertion point is a store to the
1082       // return value, we can elide the load, zap the store, and usually zap the
1083       // alloca.
1084       llvm::BasicBlock *InsertBB = Builder.GetInsertBlock();
1085       llvm::StoreInst *SI = 0;
1086       if (InsertBB->empty() ||
1087           !(SI = dyn_cast<llvm::StoreInst>(&InsertBB->back())) ||
1088           SI->getPointerOperand() != ReturnValue || SI->isVolatile()) {
1089         RV = Builder.CreateLoad(ReturnValue);
1090       } else {
1091         // Get the stored value and nuke the now-dead store.
1092         RetDbgLoc = SI->getDebugLoc();
1093         RV = SI->getValueOperand();
1094         SI->eraseFromParent();
1095 
1096         // If that was the only use of the return value, nuke it as well now.
1097         if (ReturnValue->use_empty() && isa<llvm::AllocaInst>(ReturnValue)) {
1098           cast<llvm::AllocaInst>(ReturnValue)->eraseFromParent();
1099           ReturnValue = 0;
1100         }
1101       }
1102     } else {
1103       llvm::Value *V = ReturnValue;
1104       // If the value is offset in memory, apply the offset now.
1105       if (unsigned Offs = RetAI.getDirectOffset()) {
1106         V = Builder.CreateBitCast(V, Builder.getInt8PtrTy());
1107         V = Builder.CreateConstGEP1_32(V, Offs);
1108         V = Builder.CreateBitCast(V,
1109                          llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1110       }
1111 
1112       RV = CreateCoercedLoad(V, RetAI.getCoerceToType(), *this);
1113     }
1114     break;
1115 
1116   case ABIArgInfo::Ignore:
1117     break;
1118 
1119   case ABIArgInfo::Expand:
1120     assert(0 && "Invalid ABI kind for return argument");
1121   }
1122 
1123   llvm::Instruction *Ret = RV ? Builder.CreateRet(RV) : Builder.CreateRetVoid();
1124   if (!RetDbgLoc.isUnknown())
1125     Ret->setDebugLoc(RetDbgLoc);
1126 }
1127 
1128 void CodeGenFunction::EmitDelegateCallArg(CallArgList &args,
1129                                           const VarDecl *param) {
1130   // StartFunction converted the ABI-lowered parameter(s) into a
1131   // local alloca.  We need to turn that into an r-value suitable
1132   // for EmitCall.
1133   llvm::Value *local = GetAddrOfLocalVar(param);
1134 
1135   QualType type = param->getType();
1136 
1137   // For the most part, we just need to load the alloca, except:
1138   // 1) aggregate r-values are actually pointers to temporaries, and
1139   // 2) references to aggregates are pointers directly to the aggregate.
1140   // I don't know why references to non-aggregates are different here.
1141   if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
1142     if (hasAggregateLLVMType(ref->getPointeeType()))
1143       return args.add(RValue::getAggregate(local), type);
1144 
1145     // Locals which are references to scalars are represented
1146     // with allocas holding the pointer.
1147     return args.add(RValue::get(Builder.CreateLoad(local)), type);
1148   }
1149 
1150   if (type->isAnyComplexType()) {
1151     ComplexPairTy complex = LoadComplexFromAddr(local, /*volatile*/ false);
1152     return args.add(RValue::getComplex(complex), type);
1153   }
1154 
1155   if (hasAggregateLLVMType(type))
1156     return args.add(RValue::getAggregate(local), type);
1157 
1158   unsigned alignment = getContext().getDeclAlign(param).getQuantity();
1159   llvm::Value *value = EmitLoadOfScalar(local, false, alignment, type);
1160   return args.add(RValue::get(value), type);
1161 }
1162 
1163 void CodeGenFunction::EmitCallArg(CallArgList &args, const Expr *E,
1164                                   QualType type) {
1165   if (type->isReferenceType())
1166     return args.add(EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0),
1167                     type);
1168 
1169   args.add(EmitAnyExprToTemp(E), type);
1170 }
1171 
1172 /// Emits a call or invoke instruction to the given function, depending
1173 /// on the current state of the EH stack.
1174 llvm::CallSite
1175 CodeGenFunction::EmitCallOrInvoke(llvm::Value *Callee,
1176                                   llvm::Value * const *ArgBegin,
1177                                   llvm::Value * const *ArgEnd,
1178                                   const llvm::Twine &Name) {
1179   llvm::BasicBlock *InvokeDest = getInvokeDest();
1180   if (!InvokeDest)
1181     return Builder.CreateCall(Callee, ArgBegin, ArgEnd, Name);
1182 
1183   llvm::BasicBlock *ContBB = createBasicBlock("invoke.cont");
1184   llvm::InvokeInst *Invoke = Builder.CreateInvoke(Callee, ContBB, InvokeDest,
1185                                                   ArgBegin, ArgEnd, Name);
1186   EmitBlock(ContBB);
1187   return Invoke;
1188 }
1189 
1190 RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1191                                  llvm::Value *Callee,
1192                                  ReturnValueSlot ReturnValue,
1193                                  const CallArgList &CallArgs,
1194                                  const Decl *TargetDecl,
1195                                  llvm::Instruction **callOrInvoke) {
1196   // FIXME: We no longer need the types from CallArgs; lift up and simplify.
1197   llvm::SmallVector<llvm::Value*, 16> Args;
1198 
1199   // Handle struct-return functions by passing a pointer to the
1200   // location that we would like to return into.
1201   QualType RetTy = CallInfo.getReturnType();
1202   const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
1203 
1204 
1205   // If the call returns a temporary with struct return, create a temporary
1206   // alloca to hold the result, unless one is given to us.
1207   if (CGM.ReturnTypeUsesSRet(CallInfo)) {
1208     llvm::Value *Value = ReturnValue.getValue();
1209     if (!Value)
1210       Value = CreateMemTemp(RetTy);
1211     Args.push_back(Value);
1212   }
1213 
1214   assert(CallInfo.arg_size() == CallArgs.size() &&
1215          "Mismatch between function signature & arguments.");
1216   CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
1217   for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1218        I != E; ++I, ++info_it) {
1219     const ABIArgInfo &ArgInfo = info_it->info;
1220     RValue RV = I->first;
1221 
1222     unsigned Alignment =
1223       getContext().getTypeAlignInChars(I->second).getQuantity();
1224     switch (ArgInfo.getKind()) {
1225     case ABIArgInfo::Indirect: {
1226       if (RV.isScalar() || RV.isComplex()) {
1227         // Make a temporary alloca to pass the argument.
1228         Args.push_back(CreateMemTemp(I->second));
1229         if (RV.isScalar())
1230           EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false,
1231                             Alignment, I->second);
1232         else
1233           StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1234       } else {
1235         Args.push_back(RV.getAggregateAddr());
1236       }
1237       break;
1238     }
1239 
1240     case ABIArgInfo::Ignore:
1241       break;
1242 
1243     case ABIArgInfo::Extend:
1244     case ABIArgInfo::Direct: {
1245       if (!isa<llvm::StructType>(ArgInfo.getCoerceToType()) &&
1246           ArgInfo.getCoerceToType() == ConvertType(info_it->type) &&
1247           ArgInfo.getDirectOffset() == 0) {
1248         if (RV.isScalar())
1249           Args.push_back(RV.getScalarVal());
1250         else
1251           Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1252         break;
1253       }
1254 
1255       // FIXME: Avoid the conversion through memory if possible.
1256       llvm::Value *SrcPtr;
1257       if (RV.isScalar()) {
1258         SrcPtr = CreateMemTemp(I->second, "coerce");
1259         EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false, Alignment,
1260                           I->second);
1261       } else if (RV.isComplex()) {
1262         SrcPtr = CreateMemTemp(I->second, "coerce");
1263         StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1264       } else
1265         SrcPtr = RV.getAggregateAddr();
1266 
1267       // If the value is offset in memory, apply the offset now.
1268       if (unsigned Offs = ArgInfo.getDirectOffset()) {
1269         SrcPtr = Builder.CreateBitCast(SrcPtr, Builder.getInt8PtrTy());
1270         SrcPtr = Builder.CreateConstGEP1_32(SrcPtr, Offs);
1271         SrcPtr = Builder.CreateBitCast(SrcPtr,
1272                        llvm::PointerType::getUnqual(ArgInfo.getCoerceToType()));
1273 
1274       }
1275 
1276       // If the coerce-to type is a first class aggregate, we flatten it and
1277       // pass the elements. Either way is semantically identical, but fast-isel
1278       // and the optimizer generally likes scalar values better than FCAs.
1279       if (const llvm::StructType *STy =
1280             dyn_cast<llvm::StructType>(ArgInfo.getCoerceToType())) {
1281         SrcPtr = Builder.CreateBitCast(SrcPtr,
1282                                        llvm::PointerType::getUnqual(STy));
1283         for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1284           llvm::Value *EltPtr = Builder.CreateConstGEP2_32(SrcPtr, 0, i);
1285           llvm::LoadInst *LI = Builder.CreateLoad(EltPtr);
1286           // We don't know what we're loading from.
1287           LI->setAlignment(1);
1288           Args.push_back(LI);
1289         }
1290       } else {
1291         // In the simple case, just pass the coerced loaded value.
1292         Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1293                                          *this));
1294       }
1295 
1296       break;
1297     }
1298 
1299     case ABIArgInfo::Expand:
1300       ExpandTypeToArgs(I->second, RV, Args);
1301       break;
1302     }
1303   }
1304 
1305   // If the callee is a bitcast of a function to a varargs pointer to function
1306   // type, check to see if we can remove the bitcast.  This handles some cases
1307   // with unprototyped functions.
1308   if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Callee))
1309     if (llvm::Function *CalleeF = dyn_cast<llvm::Function>(CE->getOperand(0))) {
1310       const llvm::PointerType *CurPT=cast<llvm::PointerType>(Callee->getType());
1311       const llvm::FunctionType *CurFT =
1312         cast<llvm::FunctionType>(CurPT->getElementType());
1313       const llvm::FunctionType *ActualFT = CalleeF->getFunctionType();
1314 
1315       if (CE->getOpcode() == llvm::Instruction::BitCast &&
1316           ActualFT->getReturnType() == CurFT->getReturnType() &&
1317           ActualFT->getNumParams() == CurFT->getNumParams() &&
1318           ActualFT->getNumParams() == Args.size() &&
1319           (CurFT->isVarArg() || !ActualFT->isVarArg())) {
1320         bool ArgsMatch = true;
1321         for (unsigned i = 0, e = ActualFT->getNumParams(); i != e; ++i)
1322           if (ActualFT->getParamType(i) != CurFT->getParamType(i)) {
1323             ArgsMatch = false;
1324             break;
1325           }
1326 
1327         // Strip the cast if we can get away with it.  This is a nice cleanup,
1328         // but also allows us to inline the function at -O0 if it is marked
1329         // always_inline.
1330         if (ArgsMatch)
1331           Callee = CalleeF;
1332       }
1333     }
1334 
1335 
1336   unsigned CallingConv;
1337   CodeGen::AttributeListType AttributeList;
1338   CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList, CallingConv);
1339   llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1340                                                    AttributeList.end());
1341 
1342   llvm::BasicBlock *InvokeDest = 0;
1343   if (!(Attrs.getFnAttributes() & llvm::Attribute::NoUnwind))
1344     InvokeDest = getInvokeDest();
1345 
1346   llvm::CallSite CS;
1347   if (!InvokeDest) {
1348     CS = Builder.CreateCall(Callee, Args.data(), Args.data()+Args.size());
1349   } else {
1350     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1351     CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1352                               Args.data(), Args.data()+Args.size());
1353     EmitBlock(Cont);
1354   }
1355   if (callOrInvoke)
1356     *callOrInvoke = CS.getInstruction();
1357 
1358   CS.setAttributes(Attrs);
1359   CS.setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1360 
1361   // If the call doesn't return, finish the basic block and clear the
1362   // insertion point; this allows the rest of IRgen to discard
1363   // unreachable code.
1364   if (CS.doesNotReturn()) {
1365     Builder.CreateUnreachable();
1366     Builder.ClearInsertionPoint();
1367 
1368     // FIXME: For now, emit a dummy basic block because expr emitters in
1369     // generally are not ready to handle emitting expressions at unreachable
1370     // points.
1371     EnsureInsertPoint();
1372 
1373     // Return a reasonable RValue.
1374     return GetUndefRValue(RetTy);
1375   }
1376 
1377   llvm::Instruction *CI = CS.getInstruction();
1378   if (Builder.isNamePreserving() && !CI->getType()->isVoidTy())
1379     CI->setName("call");
1380 
1381   switch (RetAI.getKind()) {
1382   case ABIArgInfo::Indirect: {
1383     unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1384     if (RetTy->isAnyComplexType())
1385       return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
1386     if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1387       return RValue::getAggregate(Args[0]);
1388     return RValue::get(EmitLoadOfScalar(Args[0], false, Alignment, RetTy));
1389   }
1390 
1391   case ABIArgInfo::Ignore:
1392     // If we are ignoring an argument that had a result, make sure to
1393     // construct the appropriate return value for our caller.
1394     return GetUndefRValue(RetTy);
1395 
1396   case ABIArgInfo::Extend:
1397   case ABIArgInfo::Direct: {
1398     if (RetAI.getCoerceToType() == ConvertType(RetTy) &&
1399         RetAI.getDirectOffset() == 0) {
1400       if (RetTy->isAnyComplexType()) {
1401         llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1402         llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1403         return RValue::getComplex(std::make_pair(Real, Imag));
1404       }
1405       if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1406         llvm::Value *DestPtr = ReturnValue.getValue();
1407         bool DestIsVolatile = ReturnValue.isVolatile();
1408 
1409         if (!DestPtr) {
1410           DestPtr = CreateMemTemp(RetTy, "agg.tmp");
1411           DestIsVolatile = false;
1412         }
1413         Builder.CreateStore(CI, DestPtr, DestIsVolatile);
1414         return RValue::getAggregate(DestPtr);
1415       }
1416       return RValue::get(CI);
1417     }
1418 
1419     llvm::Value *DestPtr = ReturnValue.getValue();
1420     bool DestIsVolatile = ReturnValue.isVolatile();
1421 
1422     if (!DestPtr) {
1423       DestPtr = CreateMemTemp(RetTy, "coerce");
1424       DestIsVolatile = false;
1425     }
1426 
1427     // If the value is offset in memory, apply the offset now.
1428     llvm::Value *StorePtr = DestPtr;
1429     if (unsigned Offs = RetAI.getDirectOffset()) {
1430       StorePtr = Builder.CreateBitCast(StorePtr, Builder.getInt8PtrTy());
1431       StorePtr = Builder.CreateConstGEP1_32(StorePtr, Offs);
1432       StorePtr = Builder.CreateBitCast(StorePtr,
1433                          llvm::PointerType::getUnqual(RetAI.getCoerceToType()));
1434     }
1435     CreateCoercedStore(CI, StorePtr, DestIsVolatile, *this);
1436 
1437     unsigned Alignment = getContext().getTypeAlignInChars(RetTy).getQuantity();
1438     if (RetTy->isAnyComplexType())
1439       return RValue::getComplex(LoadComplexFromAddr(DestPtr, false));
1440     if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1441       return RValue::getAggregate(DestPtr);
1442     return RValue::get(EmitLoadOfScalar(DestPtr, false, Alignment, RetTy));
1443   }
1444 
1445   case ABIArgInfo::Expand:
1446     assert(0 && "Invalid ABI kind for return argument");
1447   }
1448 
1449   assert(0 && "Unhandled ABIArgInfo::Kind");
1450   return RValue::get(0);
1451 }
1452 
1453 /* VarArg handling */
1454 
1455 llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1456   return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1457 }
1458