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