CGCall.cpp revision e06a75f4c8ea30b6a99d5aa1dce5feda64da09c6
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 "CodeGenFunction.h"
17#include "CodeGenModule.h"
18#include "clang/Basic/TargetInfo.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/RecordLayout.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/Attributes.h"
25#include "llvm/Support/CallSite.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/Target/TargetData.h"
30
31#include "ABIInfo.h"
32
33using namespace clang;
34using namespace CodeGen;
35
36/***/
37
38// FIXME: Use iterator and sidestep silly type array creation.
39
40const
41CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionNoProtoType *FTNP) {
42  return getFunctionInfo(FTNP->getResultType(),
43                         llvm::SmallVector<QualType, 16>());
44}
45
46const
47CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionProtoType *FTP) {
48  llvm::SmallVector<QualType, 16> ArgTys;
49  // FIXME: Kill copy.
50  for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
51    ArgTys.push_back(FTP->getArgType(i));
52  return getFunctionInfo(FTP->getResultType(), ArgTys);
53}
54
55const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const FunctionDecl *FD) {
56  const FunctionType *FTy = FD->getType()->getAsFunctionType();
57  if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FTy))
58    return getFunctionInfo(FTP);
59  return getFunctionInfo(cast<FunctionNoProtoType>(FTy));
60}
61
62const CGFunctionInfo &CodeGenTypes::getFunctionInfo(const ObjCMethodDecl *MD) {
63  llvm::SmallVector<QualType, 16> ArgTys;
64  ArgTys.push_back(MD->getSelfDecl()->getType());
65  ArgTys.push_back(Context.getObjCSelType());
66  // FIXME: Kill copy?
67  for (ObjCMethodDecl::param_iterator i = MD->param_begin(),
68         e = MD->param_end(); i != e; ++i)
69    ArgTys.push_back((*i)->getType());
70  return getFunctionInfo(MD->getResultType(), ArgTys);
71}
72
73const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
74                                                    const CallArgList &Args) {
75  // FIXME: Kill copy.
76  llvm::SmallVector<QualType, 16> ArgTys;
77  for (CallArgList::const_iterator i = Args.begin(), e = Args.end();
78       i != e; ++i)
79    ArgTys.push_back(i->second);
80  return getFunctionInfo(ResTy, ArgTys);
81}
82
83const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
84                                                  const FunctionArgList &Args) {
85  // FIXME: Kill copy.
86  llvm::SmallVector<QualType, 16> ArgTys;
87  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
88       i != e; ++i)
89    ArgTys.push_back(i->second);
90  return getFunctionInfo(ResTy, ArgTys);
91}
92
93const CGFunctionInfo &CodeGenTypes::getFunctionInfo(QualType ResTy,
94                               const llvm::SmallVector<QualType, 16> &ArgTys) {
95  // Lookup or create unique function info.
96  llvm::FoldingSetNodeID ID;
97  CGFunctionInfo::Profile(ID, ResTy, ArgTys.begin(), ArgTys.end());
98
99  void *InsertPos = 0;
100  CGFunctionInfo *FI = FunctionInfos.FindNodeOrInsertPos(ID, InsertPos);
101  if (FI)
102    return *FI;
103
104  // Construct the function info.
105  FI = new CGFunctionInfo(ResTy, ArgTys);
106  FunctionInfos.InsertNode(FI, InsertPos);
107
108  // Compute ABI information.
109  getABIInfo().computeInfo(*FI, getContext());
110
111  return *FI;
112}
113
114/***/
115
116ABIInfo::~ABIInfo() {}
117
118void ABIArgInfo::dump() const {
119  fprintf(stderr, "(ABIArgInfo Kind=");
120  switch (TheKind) {
121  case Direct:
122    fprintf(stderr, "Direct");
123    break;
124  case Ignore:
125    fprintf(stderr, "Ignore");
126    break;
127  case Coerce:
128    fprintf(stderr, "Coerce Type=");
129    getCoerceToType()->print(llvm::errs());
130    break;
131  case Indirect:
132    fprintf(stderr, "Indirect Align=%d", getIndirectAlign());
133    break;
134  case Expand:
135    fprintf(stderr, "Expand");
136    break;
137  }
138  fprintf(stderr, ")\n");
139}
140
141/***/
142
143/// isEmptyStruct - Return true iff a structure has no non-empty
144/// members. Note that a structure with a flexible array member is not
145/// considered empty.
146static bool isEmptyStruct(QualType T) {
147  const RecordType *RT = T->getAsStructureType();
148  if (!RT)
149    return 0;
150  const RecordDecl *RD = RT->getDecl();
151  if (RD->hasFlexibleArrayMember())
152    return false;
153  for (RecordDecl::field_iterator i = RD->field_begin(),
154         e = RD->field_end(); i != e; ++i) {
155    const FieldDecl *FD = *i;
156    if (!isEmptyStruct(FD->getType()))
157      return false;
158  }
159  return true;
160}
161
162/// isSingleElementStruct - Determine if a structure is a "single
163/// element struct", i.e. it has exactly one non-empty field or
164/// exactly one field which is itself a single element
165/// struct. Structures with flexible array members are never
166/// considered single element structs.
167///
168/// \return The field declaration for the single non-empty field, if
169/// it exists.
170static const FieldDecl *isSingleElementStruct(QualType T) {
171  const RecordType *RT = T->getAsStructureType();
172  if (!RT)
173    return 0;
174
175  const RecordDecl *RD = RT->getDecl();
176  if (RD->hasFlexibleArrayMember())
177    return 0;
178
179  const FieldDecl *Found = 0;
180  for (RecordDecl::field_iterator i = RD->field_begin(),
181         e = RD->field_end(); i != e; ++i) {
182    const FieldDecl *FD = *i;
183    QualType FT = FD->getType();
184
185    if (isEmptyStruct(FT)) {
186      // Ignore
187    } else if (Found) {
188      return 0;
189    } else if (!CodeGenFunction::hasAggregateLLVMType(FT)) {
190      Found = FD;
191    } else {
192      Found = isSingleElementStruct(FT);
193      if (!Found)
194        return 0;
195    }
196  }
197
198  return Found;
199}
200
201static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
202  if (!Ty->getAsBuiltinType() && !Ty->isPointerType())
203    return false;
204
205  uint64_t Size = Context.getTypeSize(Ty);
206  return Size == 32 || Size == 64;
207}
208
209static bool areAllFields32Or64BitBasicType(const RecordDecl *RD,
210                                           ASTContext &Context) {
211  for (RecordDecl::field_iterator i = RD->field_begin(),
212         e = RD->field_end(); i != e; ++i) {
213    const FieldDecl *FD = *i;
214
215    if (!is32Or64BitBasicType(FD->getType(), Context))
216      return false;
217
218    // FIXME: Reject bitfields wholesale; there are two problems, we
219    // don't know how to expand them yet, and the predicate for
220    // telling if a bitfield still counts as "basic" is more
221    // complicated than what we were doing previously.
222    if (FD->isBitField())
223      return false;
224  }
225
226  return true;
227}
228
229namespace {
230/// DefaultABIInfo - The default implementation for ABI specific
231/// details. This implementation provides information which results in
232/// self-consistent and sensible LLVM IR generation, but does not
233/// conform to any particular ABI.
234class DefaultABIInfo : public ABIInfo {
235  ABIArgInfo classifyReturnType(QualType RetTy,
236                                ASTContext &Context) const;
237
238  ABIArgInfo classifyArgumentType(QualType RetTy,
239                                  ASTContext &Context) const;
240
241  virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
242    FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
243    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
244         it != ie; ++it)
245      it->info = classifyArgumentType(it->type, Context);
246  }
247
248  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
249                                 CodeGenFunction &CGF) const;
250};
251
252/// X86_32ABIInfo - The X86-32 ABI information.
253class X86_32ABIInfo : public ABIInfo {
254public:
255  ABIArgInfo classifyReturnType(QualType RetTy,
256                                ASTContext &Context) const;
257
258  ABIArgInfo classifyArgumentType(QualType RetTy,
259                                  ASTContext &Context) const;
260
261  virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
262    FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
263    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
264         it != ie; ++it)
265      it->info = classifyArgumentType(it->type, Context);
266  }
267
268  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
269                                 CodeGenFunction &CGF) const;
270};
271}
272
273ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
274                                            ASTContext &Context) const {
275  if (RetTy->isVoidType()) {
276    return ABIArgInfo::getIgnore();
277  } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
278    // Classify "single element" structs as their element type.
279    const FieldDecl *SeltFD = isSingleElementStruct(RetTy);
280    if (SeltFD) {
281      QualType SeltTy = SeltFD->getType()->getDesugaredType();
282      if (const BuiltinType *BT = SeltTy->getAsBuiltinType()) {
283        // FIXME: This is gross, it would be nice if we could just
284        // pass back SeltTy and have clients deal with it. Is it worth
285        // supporting coerce to both LLVM and clang Types?
286        if (BT->isIntegerType()) {
287          uint64_t Size = Context.getTypeSize(SeltTy);
288          return ABIArgInfo::getCoerce(llvm::IntegerType::get((unsigned) Size));
289        } else if (BT->getKind() == BuiltinType::Float) {
290          return ABIArgInfo::getCoerce(llvm::Type::FloatTy);
291        } else if (BT->getKind() == BuiltinType::Double) {
292          return ABIArgInfo::getCoerce(llvm::Type::DoubleTy);
293        }
294      } else if (SeltTy->isPointerType()) {
295        // FIXME: It would be really nice if this could come out as
296        // the proper pointer type.
297        llvm::Type *PtrTy =
298          llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
299        return ABIArgInfo::getCoerce(PtrTy);
300      }
301    }
302
303    uint64_t Size = Context.getTypeSize(RetTy);
304    if (Size == 8) {
305      return ABIArgInfo::getCoerce(llvm::Type::Int8Ty);
306    } else if (Size == 16) {
307      return ABIArgInfo::getCoerce(llvm::Type::Int16Ty);
308    } else if (Size == 32) {
309      return ABIArgInfo::getCoerce(llvm::Type::Int32Ty);
310    } else if (Size == 64) {
311      return ABIArgInfo::getCoerce(llvm::Type::Int64Ty);
312    } else {
313      return ABIArgInfo::getIndirect(0);
314    }
315  } else {
316    return ABIArgInfo::getDirect();
317  }
318}
319
320ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
321                                               ASTContext &Context) const {
322  // FIXME: Set alignment on indirect arguments.
323  if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
324    // Structures with flexible arrays are always indirect.
325    if (const RecordType *RT = Ty->getAsStructureType())
326      if (RT->getDecl()->hasFlexibleArrayMember())
327        return ABIArgInfo::getIndirect(0);
328
329    // Ignore empty structs.
330    uint64_t Size = Context.getTypeSize(Ty);
331    if (Ty->isStructureType() && Size == 0)
332      return ABIArgInfo::getIgnore();
333
334    // Expand structs with size <= 128-bits which consist only of
335    // basic types (int, long long, float, double, xxx*). This is
336    // non-recursive and does not ignore empty fields.
337    if (const RecordType *RT = Ty->getAsStructureType()) {
338      if (Context.getTypeSize(Ty) <= 4*32 &&
339          areAllFields32Or64BitBasicType(RT->getDecl(), Context))
340        return ABIArgInfo::getExpand();
341    }
342
343    return ABIArgInfo::getIndirect(0);
344  } else {
345    return ABIArgInfo::getDirect();
346  }
347}
348
349llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
350                                      CodeGenFunction &CGF) const {
351  const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
352  const llvm::Type *BPP = llvm::PointerType::getUnqual(BP);
353
354  CGBuilderTy &Builder = CGF.Builder;
355  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
356                                                       "ap");
357  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
358  llvm::Type *PTy =
359    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
360  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
361
362  uint64_t Offset =
363    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
364  llvm::Value *NextAddr =
365    Builder.CreateGEP(Addr,
366                      llvm::ConstantInt::get(llvm::Type::Int32Ty, Offset),
367                      "ap.next");
368  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
369
370  return AddrTyped;
371}
372
373namespace {
374/// X86_64ABIInfo - The X86_64 ABI information.
375class X86_64ABIInfo : public ABIInfo {
376  enum Class {
377    Integer = 0,
378    SSE,
379    SSEUp,
380    X87,
381    X87Up,
382    ComplexX87,
383    NoClass,
384    Memory
385  };
386
387  /// merge - Implement the X86_64 ABI merging algorithm.
388  ///
389  /// Merge an accumulating classification \arg Accum with a field
390  /// classification \arg Field.
391  ///
392  /// \param Accum - The accumulating classification. This should
393  /// always be either NoClass or the result of a previous merge
394  /// call. In addition, this should never be Memory (the caller
395  /// should just return Memory for the aggregate).
396  Class merge(Class Accum, Class Field) const;
397
398  /// classify - Determine the x86_64 register classes in which the
399  /// given type T should be passed.
400  ///
401  /// \param Lo - The classification for the parts of the type
402  /// residing in the low word of the containing object.
403  ///
404  /// \param Hi - The classification for the parts of the type
405  /// residing in the high word of the containing object.
406  ///
407  /// \param OffsetBase - The bit offset of this type in the
408  /// containing object.  Some parameters are classified different
409  /// depending on whether they straddle an eightbyte boundary.
410  ///
411  /// If a word is unused its result will be NoClass; if a type should
412  /// be passed in Memory then at least the classification of \arg Lo
413  /// will be Memory.
414  ///
415  /// The \arg Lo class will be NoClass iff the argument is ignored.
416  ///
417  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
418  /// also be ComplexX87.
419  void classify(QualType T, ASTContext &Context, uint64_t OffsetBase,
420                Class &Lo, Class &Hi) const;
421
422  /// getCoerceResult - Given a source type \arg Ty and an LLVM type
423  /// to coerce to, chose the best way to pass Ty in the same place
424  /// that \arg CoerceTo would be passed, but while keeping the
425  /// emitted code as simple as possible.
426  ///
427  /// FIXME: Note, this should be cleaned up to just take an
428  /// enumeration of all the ways we might want to pass things,
429  /// instead of constructing an LLVM type. This makes this code more
430  /// explicit, and it makes it clearer that we are also doing this
431  /// for correctness in the case of passing scalar types.
432  ABIArgInfo getCoerceResult(QualType Ty,
433                             const llvm::Type *CoerceTo,
434                             ASTContext &Context) const;
435
436  ABIArgInfo classifyReturnType(QualType RetTy,
437                                ASTContext &Context) const;
438
439  ABIArgInfo classifyArgumentType(QualType Ty,
440                                  ASTContext &Context,
441                                  unsigned &neededInt,
442                                  unsigned &neededSSE) const;
443
444public:
445  virtual void computeInfo(CGFunctionInfo &FI, ASTContext &Context) const;
446
447  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
448                                 CodeGenFunction &CGF) const;
449};
450}
451
452X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum,
453                                          Class Field) const {
454  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
455  // classified recursively so that always two fields are
456  // considered. The resulting class is calculated according to
457  // the classes of the fields in the eightbyte:
458  //
459  // (a) If both classes are equal, this is the resulting class.
460  //
461  // (b) If one of the classes is NO_CLASS, the resulting class is
462  // the other class.
463  //
464  // (c) If one of the classes is MEMORY, the result is the MEMORY
465  // class.
466  //
467  // (d) If one of the classes is INTEGER, the result is the
468  // INTEGER.
469  //
470  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
471  // MEMORY is used as class.
472  //
473  // (f) Otherwise class SSE is used.
474
475  // Accum should never be memory (we should have returned) or
476  // ComplexX87 (because this cannot be passed in a structure).
477  assert((Accum != Memory && Accum != ComplexX87) &&
478         "Invalid accumulated classification during merge.");
479  if (Accum == Field || Field == NoClass)
480    return Accum;
481  else if (Field == Memory)
482    return Memory;
483  else if (Accum == NoClass)
484    return Field;
485  else if (Accum == Integer || Field == Integer)
486    return Integer;
487  else if (Field == X87 || Field == X87Up || Field == ComplexX87)
488    return Memory;
489  else
490    return SSE;
491}
492
493void X86_64ABIInfo::classify(QualType Ty,
494                             ASTContext &Context,
495                             uint64_t OffsetBase,
496                             Class &Lo, Class &Hi) const {
497  // FIXME: This code can be simplified by introducing a simple value
498  // class for Class pairs with appropriate constructor methods for
499  // the various situations.
500
501  // FIXME: Some of the split computations are wrong; unaligned
502  // vectors shouldn't be passed in registers for example, so there is
503  // no chance they can straddle an eightbyte. Verify & simplify.
504
505  Lo = Hi = NoClass;
506
507  Class &Current = OffsetBase < 64 ? Lo : Hi;
508  Current = Memory;
509
510  if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
511    BuiltinType::Kind k = BT->getKind();
512
513    if (k == BuiltinType::Void) {
514      Current = NoClass;
515    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
516      Current = Integer;
517    } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
518      Current = SSE;
519    } else if (k == BuiltinType::LongDouble) {
520      Lo = X87;
521      Hi = X87Up;
522    }
523    // FIXME: _Decimal32 and _Decimal64 are SSE.
524    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
525    // FIXME: __int128 is (Integer, Integer).
526  } else if (const EnumType *ET = Ty->getAsEnumType()) {
527    // Classify the underlying integer type.
528    classify(ET->getDecl()->getIntegerType(), Context, OffsetBase, Lo, Hi);
529  } else if (Ty->hasPointerRepresentation()) {
530    Current = Integer;
531  } else if (const VectorType *VT = Ty->getAsVectorType()) {
532    uint64_t Size = Context.getTypeSize(VT);
533    if (Size == 32) {
534      // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
535      // float> as integer.
536      Current = Integer;
537
538      // If this type crosses an eightbyte boundary, it should be
539      // split.
540      uint64_t EB_Real = (OffsetBase) / 64;
541      uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
542      if (EB_Real != EB_Imag)
543        Hi = Lo;
544    } else if (Size == 64) {
545      // gcc passes <1 x double> in memory. :(
546      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
547        return;
548
549      // gcc passes <1 x long long> as INTEGER.
550      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong))
551        Current = Integer;
552      else
553        Current = SSE;
554
555      // If this type crosses an eightbyte boundary, it should be
556      // split.
557      if (OffsetBase && OffsetBase != 64)
558        Hi = Lo;
559    } else if (Size == 128) {
560      Lo = SSE;
561      Hi = SSEUp;
562    }
563  } else if (const ComplexType *CT = Ty->getAsComplexType()) {
564    QualType ET = Context.getCanonicalType(CT->getElementType());
565
566    uint64_t Size = Context.getTypeSize(Ty);
567    if (ET->isIntegralType()) {
568      if (Size <= 64)
569        Current = Integer;
570      else if (Size <= 128)
571        Lo = Hi = Integer;
572    } else if (ET == Context.FloatTy)
573      Current = SSE;
574    else if (ET == Context.DoubleTy)
575      Lo = Hi = SSE;
576    else if (ET == Context.LongDoubleTy)
577      Current = ComplexX87;
578
579    // If this complex type crosses an eightbyte boundary then it
580    // should be split.
581    uint64_t EB_Real = (OffsetBase) / 64;
582    uint64_t EB_Imag = (OffsetBase + Context.getTypeSize(ET)) / 64;
583    if (Hi == NoClass && EB_Real != EB_Imag)
584      Hi = Lo;
585  } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
586    // Arrays are treated like structures.
587
588    uint64_t Size = Context.getTypeSize(Ty);
589
590    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
591    // than two eightbytes, ..., it has class MEMORY.
592    if (Size > 128)
593      return;
594
595    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
596    // fields, it has class MEMORY.
597    //
598    // Only need to check alignment of array base.
599    if (OffsetBase % Context.getTypeAlign(AT->getElementType()))
600      return;
601
602    // Otherwise implement simplified merge. We could be smarter about
603    // this, but it isn't worth it and would be harder to verify.
604    Current = NoClass;
605    uint64_t EltSize = Context.getTypeSize(AT->getElementType());
606    uint64_t ArraySize = AT->getSize().getZExtValue();
607    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
608      Class FieldLo, FieldHi;
609      classify(AT->getElementType(), Context, Offset, FieldLo, FieldHi);
610      Lo = merge(Lo, FieldLo);
611      Hi = merge(Hi, FieldHi);
612      if (Lo == Memory || Hi == Memory)
613        break;
614    }
615
616    // Do post merger cleanup (see below). Only case we worry about is Memory.
617    if (Hi == Memory)
618      Lo = Memory;
619    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
620  } else if (const RecordType *RT = Ty->getAsRecordType()) {
621    uint64_t Size = Context.getTypeSize(Ty);
622
623    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
624    // than two eightbytes, ..., it has class MEMORY.
625    if (Size > 128)
626      return;
627
628    const RecordDecl *RD = RT->getDecl();
629
630    // Assume variable sized types are passed in memory.
631    if (RD->hasFlexibleArrayMember())
632      return;
633
634    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
635
636    // Reset Lo class, this will be recomputed.
637    Current = NoClass;
638    unsigned idx = 0;
639    for (RecordDecl::field_iterator i = RD->field_begin(),
640           e = RD->field_end(); i != e; ++i, ++idx) {
641      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
642      bool BitField = i->isBitField();
643
644      // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
645      // fields, it has class MEMORY.
646      //
647      // Note, skip this test for bitfields, see below.
648      if (!BitField && Offset % Context.getTypeAlign(i->getType())) {
649        Lo = Memory;
650        return;
651      }
652
653      // Classify this field.
654      //
655      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
656      // exceeds a single eightbyte, each is classified
657      // separately. Each eightbyte gets initialized to class
658      // NO_CLASS.
659      Class FieldLo, FieldHi;
660
661      // Bitfields require special handling, they do not force the
662      // structure to be passed in memory even if unaligned, and
663      // therefore they can straddle an eightbyte.
664      if (BitField) {
665        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
666        uint64_t Size =
667          i->getBitWidth()->getIntegerConstantExprValue(Context).getZExtValue();
668
669        uint64_t EB_Lo = Offset / 64;
670        uint64_t EB_Hi = (Offset + Size - 1) / 64;
671        FieldLo = FieldHi = NoClass;
672        if (EB_Lo) {
673          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
674          FieldLo = NoClass;
675          FieldHi = Integer;
676        } else {
677          FieldLo = Integer;
678          FieldHi = EB_Hi ? Integer : NoClass;
679        }
680      } else
681        classify(i->getType(), Context, Offset, FieldLo, FieldHi);
682      Lo = merge(Lo, FieldLo);
683      Hi = merge(Hi, FieldHi);
684      if (Lo == Memory || Hi == Memory)
685        break;
686    }
687
688    // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
689    //
690    // (a) If one of the classes is MEMORY, the whole argument is
691    // passed in memory.
692    //
693    // (b) If SSEUP is not preceeded by SSE, it is converted to SSE.
694
695    // The first of these conditions is guaranteed by how we implement
696    // the merge (just bail).
697    //
698    // The second condition occurs in the case of unions; for example
699    // union { _Complex double; unsigned; }.
700    if (Hi == Memory)
701      Lo = Memory;
702    if (Hi == SSEUp && Lo != SSE)
703      Hi = SSE;
704  }
705}
706
707ABIArgInfo X86_64ABIInfo::getCoerceResult(QualType Ty,
708                                          const llvm::Type *CoerceTo,
709                                          ASTContext &Context) const {
710  if (CoerceTo == llvm::Type::Int64Ty) {
711    // Integer and pointer types will end up in a general purpose
712    // register.
713    if (Ty->isIntegralType() || Ty->isPointerType())
714      return ABIArgInfo::getDirect();
715
716  } else if (CoerceTo == llvm::Type::DoubleTy) {
717    // FIXME: It would probably be better to make CGFunctionInfo only
718    // map using canonical types than to canonize here.
719    QualType CTy = Context.getCanonicalType(Ty);
720
721    // Float and double end up in a single SSE reg.
722    if (CTy == Context.FloatTy || CTy == Context.DoubleTy)
723      return ABIArgInfo::getDirect();
724
725  }
726
727  return ABIArgInfo::getCoerce(CoerceTo);
728}
729
730ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy,
731                                            ASTContext &Context) const {
732  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
733  // classification algorithm.
734  X86_64ABIInfo::Class Lo, Hi;
735  classify(RetTy, Context, 0, Lo, Hi);
736
737  // Check some invariants.
738  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
739  assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
740  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
741
742  const llvm::Type *ResType = 0;
743  switch (Lo) {
744  case NoClass:
745    return ABIArgInfo::getIgnore();
746
747  case SSEUp:
748  case X87Up:
749    assert(0 && "Invalid classification for lo word.");
750
751    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
752    // hidden argument.
753  case Memory:
754    return ABIArgInfo::getIndirect(0);
755
756    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
757    // available register of the sequence %rax, %rdx is used.
758  case Integer:
759    ResType = llvm::Type::Int64Ty; break;
760
761    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
762    // available SSE register of the sequence %xmm0, %xmm1 is used.
763  case SSE:
764    ResType = llvm::Type::DoubleTy; break;
765
766    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
767    // returned on the X87 stack in %st0 as 80-bit x87 number.
768  case X87:
769    ResType = llvm::Type::X86_FP80Ty; break;
770
771    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
772    // part of the value is returned in %st0 and the imaginary part in
773    // %st1.
774  case ComplexX87:
775    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
776    ResType = llvm::StructType::get(llvm::Type::X86_FP80Ty,
777                                    llvm::Type::X86_FP80Ty,
778                                    NULL);
779    break;
780  }
781
782  switch (Hi) {
783    // Memory was handled previously and X87 should
784    // never occur as a hi class.
785  case Memory:
786  case X87:
787    assert(0 && "Invalid classification for hi word.");
788
789  case ComplexX87: // Previously handled.
790  case NoClass: break;
791
792  case Integer:
793    ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
794    break;
795  case SSE:
796    ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
797    break;
798
799    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
800    // is passed in the upper half of the last used SSE register.
801    //
802    // SSEUP should always be preceeded by SSE, just widen.
803  case SSEUp:
804    assert(Lo == SSE && "Unexpected SSEUp classification.");
805    ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
806    break;
807
808    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
809    // returned together with the previous X87 value in %st0.
810  case X87Up:
811    // If X87Up is preceeded by X87, we don't need to do
812    // anything. However, in some cases with unions it may not be
813    // preceeded by X87. In such situations we follow gcc and pass the
814    // extra bits in an SSE reg.
815    if (Lo != X87)
816      ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
817    break;
818  }
819
820  return getCoerceResult(RetTy, ResType, Context);
821}
822
823ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
824                                               unsigned &neededInt,
825                                               unsigned &neededSSE) const {
826  X86_64ABIInfo::Class Lo, Hi;
827  classify(Ty, Context, 0, Lo, Hi);
828
829  // Check some invariants.
830  // FIXME: Enforce these by construction.
831  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
832  assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
833  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
834
835  neededInt = 0;
836  neededSSE = 0;
837  const llvm::Type *ResType = 0;
838  switch (Lo) {
839  case NoClass:
840    return ABIArgInfo::getIgnore();
841
842    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
843    // on the stack.
844  case Memory:
845
846    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
847    // COMPLEX_X87, it is passed in memory.
848  case X87:
849  case ComplexX87:
850    return ABIArgInfo::getIndirect(0);
851
852  case SSEUp:
853  case X87Up:
854    assert(0 && "Invalid classification for lo word.");
855
856    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
857    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
858    // and %r9 is used.
859  case Integer:
860    ++neededInt;
861    ResType = llvm::Type::Int64Ty;
862    break;
863
864    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
865    // available SSE register is used, the registers are taken in the
866    // order from %xmm0 to %xmm7.
867  case SSE:
868    ++neededSSE;
869    ResType = llvm::Type::DoubleTy;
870    break;
871  }
872
873  switch (Hi) {
874    // Memory was handled previously, ComplexX87 and X87 should
875    // never occur as hi classes, and X87Up must be preceed by X87,
876    // which is passed in memory.
877  case Memory:
878  case X87:
879  case ComplexX87:
880    assert(0 && "Invalid classification for hi word.");
881    break;
882
883  case NoClass: break;
884  case Integer:
885    ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
886    ++neededInt;
887    break;
888
889    // X87Up generally doesn't occur here (long double is passed in
890    // memory), except in situations involving unions.
891  case X87Up:
892  case SSE:
893    ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
894    ++neededSSE;
895    break;
896
897    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
898    // eightbyte is passed in the upper half of the last used SSE
899    // register.
900  case SSEUp:
901    assert(Lo == SSE && "Unexpected SSEUp classification.");
902    ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
903    break;
904  }
905
906  return getCoerceResult(Ty, ResType, Context);
907}
908
909void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
910  FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
911
912  // Keep track of the number of assigned registers.
913  unsigned freeIntRegs = 6, freeSSERegs = 8;
914
915  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
916  // get assigned (in left-to-right order) for passing as follows...
917  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
918       it != ie; ++it) {
919    unsigned neededInt, neededSSE;
920    it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
921
922    // AMD64-ABI 3.2.3p3: If there are no registers available for any
923    // eightbyte of an argument, the whole argument is passed on the
924    // stack. If registers have already been assigned for some
925    // eightbytes of such an argument, the assignments get reverted.
926    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
927      freeIntRegs -= neededInt;
928      freeSSERegs -= neededSSE;
929    } else {
930      it->info = ABIArgInfo::getIndirect(0);
931    }
932  }
933}
934
935static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
936                                        QualType Ty,
937                                        CodeGenFunction &CGF) {
938  llvm::Value *overflow_arg_area_p =
939    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
940  llvm::Value *overflow_arg_area =
941    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
942
943  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
944  // byte boundary if alignment needed by type exceeds 8 byte boundary.
945  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
946  if (Align > 8) {
947    // Note that we follow the ABI & gcc here, even though the type
948    // could in theory have an alignment greater than 16. This case
949    // shouldn't ever matter in practice.
950
951    // overflow_arg_area = (overflow_arg_area + 15) & ~15;
952    llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
953    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
954    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
955                                                    llvm::Type::Int64Ty);
956    llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
957    overflow_arg_area =
958      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
959                                 overflow_arg_area->getType(),
960                                 "overflow_arg_area.align");
961  }
962
963  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
964  const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
965  llvm::Value *Res =
966    CGF.Builder.CreateBitCast(overflow_arg_area,
967                              llvm::PointerType::getUnqual(LTy));
968
969  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
970  // l->overflow_arg_area + sizeof(type).
971  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
972  // an 8 byte boundary.
973
974  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
975  llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
976                                               (SizeInBytes + 7)  & ~7);
977  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
978                                            "overflow_arg_area.next");
979  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
980
981  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
982  return Res;
983}
984
985llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
986                                      CodeGenFunction &CGF) const {
987  // Assume that va_list type is correct; should be pointer to LLVM type:
988  // struct {
989  //   i32 gp_offset;
990  //   i32 fp_offset;
991  //   i8* overflow_arg_area;
992  //   i8* reg_save_area;
993  // };
994  unsigned neededInt, neededSSE;
995  ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
996                                       neededInt, neededSSE);
997
998  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
999  // in the registers. If not go to step 7.
1000  if (!neededInt && !neededSSE)
1001    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1002
1003  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
1004  // general purpose registers needed to pass type and num_fp to hold
1005  // the number of floating point registers needed.
1006
1007  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
1008  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
1009  // l->fp_offset > 304 - num_fp * 16 go to step 7.
1010  //
1011  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
1012  // register save space).
1013
1014  llvm::Value *InRegs = 0;
1015  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
1016  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
1017  if (neededInt) {
1018    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
1019    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
1020    InRegs =
1021      CGF.Builder.CreateICmpULE(gp_offset,
1022                                llvm::ConstantInt::get(llvm::Type::Int32Ty,
1023                                                       48 - neededInt * 8),
1024                                "fits_in_gp");
1025  }
1026
1027  if (neededSSE) {
1028    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1029    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1030    llvm::Value *FitsInFP =
1031      CGF.Builder.CreateICmpULE(fp_offset,
1032                                llvm::ConstantInt::get(llvm::Type::Int32Ty,
1033                                                       176 - neededSSE * 16),
1034                                "fits_in_fp");
1035    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
1036  }
1037
1038  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1039  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1040  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1041  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1042
1043  // Emit code to load the value if it was passed in registers.
1044
1045  CGF.EmitBlock(InRegBlock);
1046
1047  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1048  // an offset of l->gp_offset and/or l->fp_offset. This may require
1049  // copying to a temporary location in case the parameter is passed
1050  // in different register classes or requires an alignment greater
1051  // than 8 for general purpose registers and 16 for XMM registers.
1052  //
1053  // FIXME: This really results in shameful code when we end up
1054  // needing to collect arguments from different places; often what
1055  // should result in a simple assembling of a structure from
1056  // scattered addresses has many more loads than necessary. Can we
1057  // clean this up?
1058  const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1059  llvm::Value *RegAddr =
1060    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1061                           "reg_save_area");
1062  if (neededInt && neededSSE) {
1063    // FIXME: Cleanup.
1064    assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1065    const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1066    llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1067    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1068    const llvm::Type *TyLo = ST->getElementType(0);
1069    const llvm::Type *TyHi = ST->getElementType(1);
1070    assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1071           "Unexpected ABI info for mixed regs");
1072    const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1073    const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1074    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1075    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1076    llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1077    llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1078    llvm::Value *V =
1079      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1080    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1081    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1082    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1083
1084    RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
1085  } else if (neededInt) {
1086    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1087    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1088                                        llvm::PointerType::getUnqual(LTy));
1089  } else {
1090    if (neededSSE == 1) {
1091      RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1092      RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1093                                          llvm::PointerType::getUnqual(LTy));
1094    } else {
1095      assert(neededSSE == 2 && "Invalid number of needed registers!");
1096      // SSE registers are spaced 16 bytes apart in the register save
1097      // area, we need to collect the two eightbytes together.
1098      llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1099      llvm::Value *RegAddrHi =
1100        CGF.Builder.CreateGEP(RegAddrLo,
1101                              llvm::ConstantInt::get(llvm::Type::Int32Ty, 16));
1102      const llvm::Type *DblPtrTy =
1103        llvm::PointerType::getUnqual(llvm::Type::DoubleTy);
1104      const llvm::StructType *ST = llvm::StructType::get(llvm::Type::DoubleTy,
1105                                                         llvm::Type::DoubleTy,
1106                                                         NULL);
1107      llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
1108      V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
1109                                                           DblPtrTy));
1110      CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1111      V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
1112                                                           DblPtrTy));
1113      CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1114      RegAddr = CGF.Builder.CreateBitCast(Tmp,
1115                                          llvm::PointerType::getUnqual(LTy));
1116    }
1117  }
1118
1119  // AMD64-ABI 3.5.7p5: Step 5. Set:
1120  // l->gp_offset = l->gp_offset + num_gp * 8
1121  // l->fp_offset = l->fp_offset + num_fp * 16.
1122  if (neededInt) {
1123    llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1124                                                 neededInt * 8);
1125    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1126                            gp_offset_p);
1127  }
1128  if (neededSSE) {
1129    llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1130                                                 neededSSE * 16);
1131    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1132                            fp_offset_p);
1133  }
1134  CGF.EmitBranch(ContBlock);
1135
1136  // Emit code to load the value if it was passed in memory.
1137
1138  CGF.EmitBlock(InMemBlock);
1139  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1140
1141  // Return the appropriate result.
1142
1143  CGF.EmitBlock(ContBlock);
1144  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1145                                                 "vaarg.addr");
1146  ResAddr->reserveOperandSpace(2);
1147  ResAddr->addIncoming(RegAddr, InRegBlock);
1148  ResAddr->addIncoming(MemAddr, InMemBlock);
1149
1150  return ResAddr;
1151}
1152
1153ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
1154                                              ASTContext &Context) const {
1155  if (RetTy->isVoidType()) {
1156    return ABIArgInfo::getIgnore();
1157  } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1158    return ABIArgInfo::getIndirect(0);
1159  } else {
1160    return ABIArgInfo::getDirect();
1161  }
1162}
1163
1164ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
1165                                                ASTContext &Context) const {
1166  if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1167    return ABIArgInfo::getIndirect(0);
1168  } else {
1169    return ABIArgInfo::getDirect();
1170  }
1171}
1172
1173llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1174                                       CodeGenFunction &CGF) const {
1175  return 0;
1176}
1177
1178const ABIInfo &CodeGenTypes::getABIInfo() const {
1179  if (TheABIInfo)
1180    return *TheABIInfo;
1181
1182  // For now we just cache this in the CodeGenTypes and don't bother
1183  // to free it.
1184  const char *TargetPrefix = getContext().Target.getTargetPrefix();
1185  if (strcmp(TargetPrefix, "x86") == 0) {
1186    switch (getContext().Target.getPointerWidth(0)) {
1187    case 32:
1188      return *(TheABIInfo = new X86_32ABIInfo());
1189    case 64:
1190      return *(TheABIInfo = new X86_64ABIInfo());
1191    }
1192  }
1193
1194  return *(TheABIInfo = new DefaultABIInfo);
1195}
1196
1197/***/
1198
1199CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1200                               const llvm::SmallVector<QualType, 16> &ArgTys) {
1201  NumArgs = ArgTys.size();
1202  Args = new ArgInfo[1 + NumArgs];
1203  Args[0].type = ResTy;
1204  for (unsigned i = 0; i < NumArgs; ++i)
1205    Args[1 + i].type = ArgTys[i];
1206}
1207
1208/***/
1209
1210void CodeGenTypes::GetExpandedTypes(QualType Ty,
1211                                    std::vector<const llvm::Type*> &ArgTys) {
1212  const RecordType *RT = Ty->getAsStructureType();
1213  assert(RT && "Can only expand structure types.");
1214  const RecordDecl *RD = RT->getDecl();
1215  assert(!RD->hasFlexibleArrayMember() &&
1216         "Cannot expand structure with flexible array.");
1217
1218  for (RecordDecl::field_iterator i = RD->field_begin(),
1219         e = RD->field_end(); i != e; ++i) {
1220    const FieldDecl *FD = *i;
1221    assert(!FD->isBitField() &&
1222           "Cannot expand structure with bit-field members.");
1223
1224    QualType FT = FD->getType();
1225    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1226      GetExpandedTypes(FT, ArgTys);
1227    } else {
1228      ArgTys.push_back(ConvertType(FT));
1229    }
1230  }
1231}
1232
1233llvm::Function::arg_iterator
1234CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1235                                    llvm::Function::arg_iterator AI) {
1236  const RecordType *RT = Ty->getAsStructureType();
1237  assert(RT && "Can only expand structure types.");
1238
1239  RecordDecl *RD = RT->getDecl();
1240  assert(LV.isSimple() &&
1241         "Unexpected non-simple lvalue during struct expansion.");
1242  llvm::Value *Addr = LV.getAddress();
1243  for (RecordDecl::field_iterator i = RD->field_begin(),
1244         e = RD->field_end(); i != e; ++i) {
1245    FieldDecl *FD = *i;
1246    QualType FT = FD->getType();
1247
1248    // FIXME: What are the right qualifiers here?
1249    LValue LV = EmitLValueForField(Addr, FD, false, 0);
1250    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1251      AI = ExpandTypeFromArgs(FT, LV, AI);
1252    } else {
1253      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1254      ++AI;
1255    }
1256  }
1257
1258  return AI;
1259}
1260
1261void
1262CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1263                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
1264  const RecordType *RT = Ty->getAsStructureType();
1265  assert(RT && "Can only expand structure types.");
1266
1267  RecordDecl *RD = RT->getDecl();
1268  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1269  llvm::Value *Addr = RV.getAggregateAddr();
1270  for (RecordDecl::field_iterator i = RD->field_begin(),
1271         e = RD->field_end(); i != e; ++i) {
1272    FieldDecl *FD = *i;
1273    QualType FT = FD->getType();
1274
1275    // FIXME: What are the right qualifiers here?
1276    LValue LV = EmitLValueForField(Addr, FD, false, 0);
1277    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1278      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1279    } else {
1280      RValue RV = EmitLoadOfLValue(LV, FT);
1281      assert(RV.isScalar() &&
1282             "Unexpected non-scalar rvalue during struct expansion.");
1283      Args.push_back(RV.getScalarVal());
1284    }
1285  }
1286}
1287
1288/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1289/// a pointer to an object of type \arg Ty.
1290///
1291/// This safely handles the case when the src type is smaller than the
1292/// destination type; in this situation the values of bits which not
1293/// present in the src are undefined.
1294static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1295                                      const llvm::Type *Ty,
1296                                      CodeGenFunction &CGF) {
1297  const llvm::Type *SrcTy =
1298    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1299  uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1300  uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1301
1302  // If load is legal, just bitcast the src pointer.
1303  if (SrcSize == DstSize) {
1304    llvm::Value *Casted =
1305      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1306    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1307    // FIXME: Use better alignment / avoid requiring aligned load.
1308    Load->setAlignment(1);
1309    return Load;
1310  } else {
1311    assert(SrcSize < DstSize && "Coercion is losing source bits!");
1312
1313    // Otherwise do coercion through memory. This is stupid, but
1314    // simple.
1315    llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1316    llvm::Value *Casted =
1317      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1318    llvm::StoreInst *Store =
1319      CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1320    // FIXME: Use better alignment / avoid requiring aligned store.
1321    Store->setAlignment(1);
1322    return CGF.Builder.CreateLoad(Tmp);
1323  }
1324}
1325
1326/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1327/// where the source and destination may have different types.
1328///
1329/// This safely handles the case when the src type is larger than the
1330/// destination type; the upper bits of the src will be lost.
1331static void CreateCoercedStore(llvm::Value *Src,
1332                               llvm::Value *DstPtr,
1333                               CodeGenFunction &CGF) {
1334  const llvm::Type *SrcTy = Src->getType();
1335  const llvm::Type *DstTy =
1336    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1337
1338  uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1339  uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1340
1341  // If store is legal, just bitcast the src pointer.
1342  if (SrcSize == DstSize) {
1343    llvm::Value *Casted =
1344      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1345    // FIXME: Use better alignment / avoid requiring aligned store.
1346    CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
1347  } else {
1348    assert(SrcSize > DstSize && "Coercion is missing bits!");
1349
1350    // Otherwise do coercion through memory. This is stupid, but
1351    // simple.
1352    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1353    CGF.Builder.CreateStore(Src, Tmp);
1354    llvm::Value *Casted =
1355      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1356    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1357    // FIXME: Use better alignment / avoid requiring aligned load.
1358    Load->setAlignment(1);
1359    CGF.Builder.CreateStore(Load, DstPtr);
1360  }
1361}
1362
1363/***/
1364
1365bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
1366  return FI.getReturnInfo().isIndirect();
1367}
1368
1369const llvm::FunctionType *
1370CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
1371  std::vector<const llvm::Type*> ArgTys;
1372
1373  const llvm::Type *ResultType = 0;
1374
1375  QualType RetTy = FI.getReturnType();
1376  const ABIArgInfo &RetAI = FI.getReturnInfo();
1377  switch (RetAI.getKind()) {
1378  case ABIArgInfo::Expand:
1379    assert(0 && "Invalid ABI kind for return argument");
1380
1381  case ABIArgInfo::Direct:
1382    ResultType = ConvertType(RetTy);
1383    break;
1384
1385  case ABIArgInfo::Indirect: {
1386    assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
1387    ResultType = llvm::Type::VoidTy;
1388    const llvm::Type *STy = ConvertType(RetTy);
1389    ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1390    break;
1391  }
1392
1393  case ABIArgInfo::Ignore:
1394    ResultType = llvm::Type::VoidTy;
1395    break;
1396
1397  case ABIArgInfo::Coerce:
1398    ResultType = RetAI.getCoerceToType();
1399    break;
1400  }
1401
1402  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1403         ie = FI.arg_end(); it != ie; ++it) {
1404    const ABIArgInfo &AI = it->info;
1405
1406    switch (AI.getKind()) {
1407    case ABIArgInfo::Ignore:
1408      break;
1409
1410    case ABIArgInfo::Coerce:
1411      ArgTys.push_back(AI.getCoerceToType());
1412      break;
1413
1414    case ABIArgInfo::Indirect: {
1415      // indirect arguments are always on the stack, which is addr space #0.
1416      const llvm::Type *LTy = ConvertTypeForMem(it->type);
1417      ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
1418      break;
1419    }
1420
1421    case ABIArgInfo::Direct:
1422      ArgTys.push_back(ConvertType(it->type));
1423      break;
1424
1425    case ABIArgInfo::Expand:
1426      GetExpandedTypes(it->type, ArgTys);
1427      break;
1428    }
1429  }
1430
1431  return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
1432}
1433
1434void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
1435                                           const Decl *TargetDecl,
1436                                           AttributeListType &PAL) {
1437  unsigned FuncAttrs = 0;
1438  unsigned RetAttrs = 0;
1439
1440  if (TargetDecl) {
1441    if (TargetDecl->getAttr<NoThrowAttr>())
1442      FuncAttrs |= llvm::Attribute::NoUnwind;
1443    if (TargetDecl->getAttr<NoReturnAttr>())
1444      FuncAttrs |= llvm::Attribute::NoReturn;
1445    if (TargetDecl->getAttr<PureAttr>())
1446      FuncAttrs |= llvm::Attribute::ReadOnly;
1447    if (TargetDecl->getAttr<ConstAttr>())
1448      FuncAttrs |= llvm::Attribute::ReadNone;
1449  }
1450
1451  QualType RetTy = FI.getReturnType();
1452  unsigned Index = 1;
1453  const ABIArgInfo &RetAI = FI.getReturnInfo();
1454  switch (RetAI.getKind()) {
1455  case ABIArgInfo::Direct:
1456    if (RetTy->isPromotableIntegerType()) {
1457      if (RetTy->isSignedIntegerType()) {
1458        RetAttrs |= llvm::Attribute::SExt;
1459      } else if (RetTy->isUnsignedIntegerType()) {
1460        RetAttrs |= llvm::Attribute::ZExt;
1461      }
1462    }
1463    break;
1464
1465  case ABIArgInfo::Indirect:
1466    PAL.push_back(llvm::AttributeWithIndex::get(Index,
1467                                                llvm::Attribute::StructRet |
1468                                                llvm::Attribute::NoAlias));
1469    ++Index;
1470    break;
1471
1472  case ABIArgInfo::Ignore:
1473  case ABIArgInfo::Coerce:
1474    break;
1475
1476  case ABIArgInfo::Expand:
1477    assert(0 && "Invalid ABI kind for return argument");
1478  }
1479
1480  if (RetAttrs)
1481    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
1482  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1483         ie = FI.arg_end(); it != ie; ++it) {
1484    QualType ParamType = it->type;
1485    const ABIArgInfo &AI = it->info;
1486    unsigned Attributes = 0;
1487
1488    switch (AI.getKind()) {
1489    case ABIArgInfo::Coerce:
1490      break;
1491
1492    case ABIArgInfo::Indirect:
1493      Attributes |= llvm::Attribute::ByVal;
1494      Attributes |=
1495        llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
1496      break;
1497
1498    case ABIArgInfo::Direct:
1499      if (ParamType->isPromotableIntegerType()) {
1500        if (ParamType->isSignedIntegerType()) {
1501          Attributes |= llvm::Attribute::SExt;
1502        } else if (ParamType->isUnsignedIntegerType()) {
1503          Attributes |= llvm::Attribute::ZExt;
1504        }
1505      }
1506      break;
1507
1508    case ABIArgInfo::Ignore:
1509      // Skip increment, no matching LLVM parameter.
1510      continue;
1511
1512    case ABIArgInfo::Expand: {
1513      std::vector<const llvm::Type*> Tys;
1514      // FIXME: This is rather inefficient. Do we ever actually need
1515      // to do anything here? The result should be just reconstructed
1516      // on the other side, so extension should be a non-issue.
1517      getTypes().GetExpandedTypes(ParamType, Tys);
1518      Index += Tys.size();
1519      continue;
1520    }
1521    }
1522
1523    if (Attributes)
1524      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
1525    ++Index;
1526  }
1527  if (FuncAttrs)
1528    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1529}
1530
1531void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1532                                         llvm::Function *Fn,
1533                                         const FunctionArgList &Args) {
1534  // FIXME: We no longer need the types from FunctionArgList; lift up
1535  // and simplify.
1536
1537  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
1538  llvm::Function::arg_iterator AI = Fn->arg_begin();
1539
1540  // Name the struct return argument.
1541  if (CGM.ReturnTypeUsesSret(FI)) {
1542    AI->setName("agg.result");
1543    ++AI;
1544  }
1545
1546  assert(FI.arg_size() == Args.size() &&
1547         "Mismatch between function signature & arguments.");
1548  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
1549  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1550       i != e; ++i, ++info_it) {
1551    const VarDecl *Arg = i->first;
1552    QualType Ty = info_it->type;
1553    const ABIArgInfo &ArgI = info_it->info;
1554
1555    switch (ArgI.getKind()) {
1556    case ABIArgInfo::Indirect: {
1557      llvm::Value* V = AI;
1558      if (hasAggregateLLVMType(Ty)) {
1559        // Do nothing, aggregates and complex variables are accessed by
1560        // reference.
1561      } else {
1562        // Load scalar value from indirect argument.
1563        V = EmitLoadOfScalar(V, false, Ty);
1564        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1565          // This must be a promotion, for something like
1566          // "void a(x) short x; {..."
1567          V = EmitScalarConversion(V, Ty, Arg->getType());
1568        }
1569      }
1570      EmitParmDecl(*Arg, V);
1571      break;
1572    }
1573
1574    case ABIArgInfo::Direct: {
1575      assert(AI != Fn->arg_end() && "Argument mismatch!");
1576      llvm::Value* V = AI;
1577      if (hasAggregateLLVMType(Ty)) {
1578        // Create a temporary alloca to hold the argument; the rest of
1579        // codegen expects to access aggregates & complex values by
1580        // reference.
1581        V = CreateTempAlloca(ConvertTypeForMem(Ty));
1582        Builder.CreateStore(AI, V);
1583      } else {
1584        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1585          // This must be a promotion, for something like
1586          // "void a(x) short x; {..."
1587          V = EmitScalarConversion(V, Ty, Arg->getType());
1588        }
1589      }
1590      EmitParmDecl(*Arg, V);
1591      break;
1592    }
1593
1594    case ABIArgInfo::Expand: {
1595      // If this structure was expanded into multiple arguments then
1596      // we need to create a temporary and reconstruct it from the
1597      // arguments.
1598      std::string Name = Arg->getNameAsString();
1599      llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
1600                                           (Name + ".addr").c_str());
1601      // FIXME: What are the right qualifiers here?
1602      llvm::Function::arg_iterator End =
1603        ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1604      EmitParmDecl(*Arg, Temp);
1605
1606      // Name the arguments used in expansion and increment AI.
1607      unsigned Index = 0;
1608      for (; AI != End; ++AI, ++Index)
1609        AI->setName(Name + "." + llvm::utostr(Index));
1610      continue;
1611    }
1612
1613    case ABIArgInfo::Ignore:
1614      // Initialize the local variable appropriately.
1615      if (hasAggregateLLVMType(Ty)) {
1616        EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
1617      } else {
1618        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1619      }
1620
1621      // Skip increment, no matching LLVM parameter.
1622      continue;
1623
1624    case ABIArgInfo::Coerce: {
1625      assert(AI != Fn->arg_end() && "Argument mismatch!");
1626      // FIXME: This is very wasteful; EmitParmDecl is just going to
1627      // drop the result in a new alloca anyway, so we could just
1628      // store into that directly if we broke the abstraction down
1629      // more.
1630      llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
1631      CreateCoercedStore(AI, V, *this);
1632      // Match to what EmitParmDecl is expecting for this type.
1633      if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1634        V = EmitLoadOfScalar(V, false, Ty);
1635        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1636          // This must be a promotion, for something like
1637          // "void a(x) short x; {..."
1638          V = EmitScalarConversion(V, Ty, Arg->getType());
1639        }
1640      }
1641      EmitParmDecl(*Arg, V);
1642      break;
1643    }
1644    }
1645
1646    ++AI;
1647  }
1648  assert(AI == Fn->arg_end() && "Argument mismatch!");
1649}
1650
1651void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
1652                                         llvm::Value *ReturnValue) {
1653  llvm::Value *RV = 0;
1654
1655  // Functions with no result always return void.
1656  if (ReturnValue) {
1657    QualType RetTy = FI.getReturnType();
1658    const ABIArgInfo &RetAI = FI.getReturnInfo();
1659
1660    switch (RetAI.getKind()) {
1661    case ABIArgInfo::Indirect:
1662      if (RetTy->isAnyComplexType()) {
1663        ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1664        StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1665      } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1666        EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1667      } else {
1668        EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1669                          false);
1670      }
1671      break;
1672
1673    case ABIArgInfo::Direct:
1674      // The internal return value temp always will have
1675      // pointer-to-return-type type.
1676      RV = Builder.CreateLoad(ReturnValue);
1677      break;
1678
1679    case ABIArgInfo::Ignore:
1680      break;
1681
1682    case ABIArgInfo::Coerce:
1683      RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
1684      break;
1685
1686    case ABIArgInfo::Expand:
1687      assert(0 && "Invalid ABI kind for return argument");
1688    }
1689  }
1690
1691  if (RV) {
1692    Builder.CreateRet(RV);
1693  } else {
1694    Builder.CreateRetVoid();
1695  }
1696}
1697
1698RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1699                                 llvm::Value *Callee,
1700                                 const CallArgList &CallArgs,
1701                                 const Decl *TargetDecl) {
1702  // FIXME: We no longer need the types from CallArgs; lift up and
1703  // simplify.
1704  llvm::SmallVector<llvm::Value*, 16> Args;
1705
1706  // Handle struct-return functions by passing a pointer to the
1707  // location that we would like to return into.
1708  QualType RetTy = CallInfo.getReturnType();
1709  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
1710  if (CGM.ReturnTypeUsesSret(CallInfo)) {
1711    // Create a temporary alloca to hold the result of the call. :(
1712    Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
1713  }
1714
1715  assert(CallInfo.arg_size() == CallArgs.size() &&
1716         "Mismatch between function signature & arguments.");
1717  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
1718  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1719       I != E; ++I, ++info_it) {
1720    const ABIArgInfo &ArgInfo = info_it->info;
1721    RValue RV = I->first;
1722
1723    switch (ArgInfo.getKind()) {
1724    case ABIArgInfo::Indirect:
1725      if (RV.isScalar() || RV.isComplex()) {
1726        // Make a temporary alloca to pass the argument.
1727        Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
1728        if (RV.isScalar())
1729          EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
1730        else
1731          StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1732      } else {
1733        Args.push_back(RV.getAggregateAddr());
1734      }
1735      break;
1736
1737    case ABIArgInfo::Direct:
1738      if (RV.isScalar()) {
1739        Args.push_back(RV.getScalarVal());
1740      } else if (RV.isComplex()) {
1741        llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1742        Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1743        Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1744        Args.push_back(Tmp);
1745      } else {
1746        Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1747      }
1748      break;
1749
1750    case ABIArgInfo::Ignore:
1751      break;
1752
1753    case ABIArgInfo::Coerce: {
1754      // FIXME: Avoid the conversion through memory if possible.
1755      llvm::Value *SrcPtr;
1756      if (RV.isScalar()) {
1757        SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
1758        EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
1759      } else if (RV.isComplex()) {
1760        SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
1761        StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1762      } else
1763        SrcPtr = RV.getAggregateAddr();
1764      Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1765                                       *this));
1766      break;
1767    }
1768
1769    case ABIArgInfo::Expand:
1770      ExpandTypeToArgs(I->second, RV, Args);
1771      break;
1772    }
1773  }
1774
1775  llvm::BasicBlock *InvokeDest = getInvokeDest();
1776  CodeGen::AttributeListType AttributeList;
1777  CGM.ConstructAttributeList(CallInfo, TargetDecl, AttributeList);
1778  llvm::AttrListPtr Attrs = llvm::AttrListPtr::get(AttributeList.begin(),
1779                                                   AttributeList.end());
1780
1781  llvm::CallSite CS;
1782  if (!InvokeDest || (Attrs.getFnAttributes() & llvm::Attribute::NoUnwind)) {
1783    CS = Builder.CreateCall(Callee, &Args[0], &Args[0]+Args.size());
1784  } else {
1785    llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
1786    CS = Builder.CreateInvoke(Callee, Cont, InvokeDest,
1787                              &Args[0], &Args[0]+Args.size());
1788    EmitBlock(Cont);
1789  }
1790
1791  CS.setAttributes(Attrs);
1792  if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1793    CS.setCallingConv(F->getCallingConv());
1794
1795  // If the call doesn't return, finish the basic block and clear the
1796  // insertion point; this allows the rest of IRgen to discard
1797  // unreachable code.
1798  if (CS.doesNotReturn()) {
1799    Builder.CreateUnreachable();
1800    Builder.ClearInsertionPoint();
1801
1802    // FIXME: For now, emit a dummy basic block because expr
1803    // emitters in generally are not ready to handle emitting
1804    // expressions at unreachable points.
1805    EnsureInsertPoint();
1806
1807    // Return a reasonable RValue.
1808    return GetUndefRValue(RetTy);
1809  }
1810
1811  llvm::Instruction *CI = CS.getInstruction();
1812  if (CI->getType() != llvm::Type::VoidTy)
1813    CI->setName("call");
1814
1815  switch (RetAI.getKind()) {
1816  case ABIArgInfo::Indirect:
1817    if (RetTy->isAnyComplexType())
1818      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
1819    else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1820      return RValue::getAggregate(Args[0]);
1821    else
1822      return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
1823
1824  case ABIArgInfo::Direct:
1825    if (RetTy->isAnyComplexType()) {
1826      llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1827      llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1828      return RValue::getComplex(std::make_pair(Real, Imag));
1829    } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1830      llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
1831      Builder.CreateStore(CI, V);
1832      return RValue::getAggregate(V);
1833    } else
1834      return RValue::get(CI);
1835
1836  case ABIArgInfo::Ignore:
1837    // If we are ignoring an argument that had a result, make sure to
1838    // construct the appropriate return value for our caller.
1839    return GetUndefRValue(RetTy);
1840
1841  case ABIArgInfo::Coerce: {
1842    // FIXME: Avoid the conversion through memory if possible.
1843    llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
1844    CreateCoercedStore(CI, V, *this);
1845    if (RetTy->isAnyComplexType())
1846      return RValue::getComplex(LoadComplexFromAddr(V, false));
1847    else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1848      return RValue::getAggregate(V);
1849    else
1850      return RValue::get(EmitLoadOfScalar(V, false, RetTy));
1851  }
1852
1853  case ABIArgInfo::Expand:
1854    assert(0 && "Invalid ABI kind for return argument");
1855  }
1856
1857  assert(0 && "Unhandled ABIArgInfo::Kind");
1858  return RValue::get(0);
1859}
1860
1861/* VarArg handling */
1862
1863llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1864  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1865}
1866