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