CGCall.cpp revision dd81d44fac2631e6ca9fe1f52366aea0f51b03e1
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  /// be NoClass.
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 == NoClass && "Unexpected ComplexX87 classification.");
755    ResType = llvm::VectorType::get(llvm::Type::X86_FP80Ty, 2);
756    break;
757  }
758
759  switch (Hi) {
760    // Memory was handled previously, and ComplexX87 and X87 should
761    // never occur as hi classes.
762  case Memory:
763  case X87:
764  case ComplexX87:
765    assert(0 && "Invalid classification for hi word.");
766
767  case NoClass: break;
768  case Integer:
769    ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
770    break;
771  case SSE:
772    ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
773    break;
774
775    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
776    // is passed in the upper half of the last used SSE register.
777    //
778    // SSEUP should always be preceeded by SSE, just widen.
779  case SSEUp:
780    assert(Lo == SSE && "Unexpected SSEUp classification.");
781    ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
782    break;
783
784    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
785    // returned together with the previous X87 value in %st0.
786    //
787    // X87UP should always be preceeded by X87, so we don't need to do
788    // anything here.
789  case X87Up:
790    assert(Lo == X87 && "Unexpected X87Up classification.");
791    break;
792  }
793
794  return getCoerceResult(RetTy, ResType, Context);
795}
796
797ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, ASTContext &Context,
798                                               unsigned &neededInt,
799                                               unsigned &neededSSE) const {
800  X86_64ABIInfo::Class Lo, Hi;
801  classify(Ty, Context, 0, Lo, Hi);
802
803  // Check some invariants.
804  // FIXME: Enforce these by construction.
805  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
806  assert((Lo != NoClass || Hi == NoClass) && "Invalid null classification.");
807  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
808
809  neededInt = 0;
810  neededSSE = 0;
811  const llvm::Type *ResType = 0;
812  switch (Lo) {
813  case NoClass:
814    return ABIArgInfo::getIgnore();
815
816    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
817    // on the stack.
818  case Memory:
819
820    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
821    // COMPLEX_X87, it is passed in memory.
822  case X87:
823  case ComplexX87:
824    // Choose appropriate in memory type.
825    if (CodeGenFunction::hasAggregateLLVMType(Ty))
826      return ABIArgInfo::getIndirect(0);
827    else
828      return ABIArgInfo::getDirect();
829
830  case SSEUp:
831  case X87Up:
832    assert(0 && "Invalid classification for lo word.");
833
834    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
835    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
836    // and %r9 is used.
837  case Integer:
838    ++neededInt;
839    ResType = llvm::Type::Int64Ty;
840    break;
841
842    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
843    // available SSE register is used, the registers are taken in the
844    // order from %xmm0 to %xmm7.
845  case SSE:
846    ++neededSSE;
847    ResType = llvm::Type::DoubleTy;
848    break;
849  }
850
851  switch (Hi) {
852    // Memory was handled previously, ComplexX87 and X87 should
853    // never occur as hi classes, and X87Up must be preceed by X87,
854    // which is passed in memory.
855  case Memory:
856  case X87:
857  case X87Up:
858  case ComplexX87:
859    assert(0 && "Invalid classification for hi word.");
860
861  case NoClass: break;
862  case Integer:
863    ResType = llvm::StructType::get(ResType, llvm::Type::Int64Ty, NULL);
864    ++neededInt;
865    break;
866  case SSE:
867    ResType = llvm::StructType::get(ResType, llvm::Type::DoubleTy, NULL);
868    ++neededSSE;
869    break;
870
871    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
872    // eightbyte is passed in the upper half of the last used SSE
873    // register.
874  case SSEUp:
875    assert(Lo == SSE && "Unexpected SSEUp classification.");
876    ResType = llvm::VectorType::get(llvm::Type::DoubleTy, 2);
877    break;
878  }
879
880  return getCoerceResult(Ty, ResType, Context);
881}
882
883void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI, ASTContext &Context) const {
884  FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), Context);
885
886  // Keep track of the number of assigned registers.
887  unsigned freeIntRegs = 6, freeSSERegs = 8;
888
889  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
890  // get assigned (in left-to-right order) for passing as follows...
891  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
892       it != ie; ++it) {
893    unsigned neededInt, neededSSE;
894    it->info = classifyArgumentType(it->type, Context, neededInt, neededSSE);
895
896    // AMD64-ABI 3.2.3p3: If there are no registers available for any
897    // eightbyte of an argument, the whole argument is passed on the
898    // stack. If registers have already been assigned for some
899    // eightbytes of such an argument, the assignments get reverted.
900    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
901      freeIntRegs -= neededInt;
902      freeSSERegs -= neededSSE;
903    } else {
904      // Choose appropriate in memory type.
905      if (CodeGenFunction::hasAggregateLLVMType(it->type))
906        it->info = ABIArgInfo::getIndirect(0);
907      else
908        it->info = ABIArgInfo::getDirect();
909    }
910  }
911}
912
913static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
914                                        QualType Ty,
915                                        CodeGenFunction &CGF) {
916  llvm::Value *overflow_arg_area_p =
917    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
918  llvm::Value *overflow_arg_area =
919    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
920
921  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
922  // byte boundary if alignment needed by type exceeds 8 byte boundary.
923  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
924  if (Align > 8) {
925    // Note that we follow the ABI & gcc here, even though the type
926    // could in theory have an alignment greater than 16. This case
927    // shouldn't ever matter in practice.
928
929    // overflow_arg_area = (overflow_arg_area + 15) & ~15;
930    llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, 15);
931    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
932    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
933                                                    llvm::Type::Int64Ty);
934    llvm::Value *Mask = llvm::ConstantInt::get(llvm::Type::Int64Ty, ~15LL);
935    overflow_arg_area =
936      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
937                                 overflow_arg_area->getType(),
938                                 "overflow_arg_area.align");
939  }
940
941  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
942  const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
943  llvm::Value *Res =
944    CGF.Builder.CreateBitCast(overflow_arg_area,
945                              llvm::PointerType::getUnqual(LTy));
946
947  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
948  // l->overflow_arg_area + sizeof(type).
949  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
950  // an 8 byte boundary.
951
952  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
953  llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
954                                               (SizeInBytes + 7)  & ~7);
955  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
956                                            "overflow_arg_area.next");
957  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
958
959  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
960  return Res;
961}
962
963llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
964                                      CodeGenFunction &CGF) const {
965  // Assume that va_list type is correct; should be pointer to LLVM type:
966  // struct {
967  //   i32 gp_offset;
968  //   i32 fp_offset;
969  //   i8* overflow_arg_area;
970  //   i8* reg_save_area;
971  // };
972  unsigned neededInt, neededSSE;
973  ABIArgInfo AI = classifyArgumentType(Ty, CGF.getContext(),
974                                       neededInt, neededSSE);
975
976  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
977  // in the registers. If not go to step 7.
978  if (!neededInt && !neededSSE)
979    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
980
981  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
982  // general purpose registers needed to pass type and num_fp to hold
983  // the number of floating point registers needed.
984
985  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
986  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
987  // l->fp_offset > 304 - num_fp * 16 go to step 7.
988  //
989  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
990  // register save space).
991
992  llvm::Value *InRegs = 0;
993  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
994  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
995  if (neededInt) {
996    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
997    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
998    InRegs =
999      CGF.Builder.CreateICmpULE(gp_offset,
1000                                llvm::ConstantInt::get(llvm::Type::Int32Ty,
1001                                                       48 - neededInt * 8),
1002                                "fits_in_gp");
1003  }
1004
1005  if (neededSSE) {
1006    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
1007    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
1008    llvm::Value *FitsInFP =
1009      CGF.Builder.CreateICmpULE(fp_offset,
1010                                llvm::ConstantInt::get(llvm::Type::Int32Ty,
1011                                                       176 - neededSSE * 8),
1012                                "fits_in_fp");
1013    InRegs = InRegs ? CGF.Builder.CreateOr(InRegs, FitsInFP) : FitsInFP;
1014  }
1015
1016  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
1017  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
1018  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
1019  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
1020
1021  // Emit code to load the value if it was passed in registers.
1022
1023  CGF.EmitBlock(InRegBlock);
1024
1025  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
1026  // an offset of l->gp_offset and/or l->fp_offset. This may require
1027  // copying to a temporary location in case the parameter is passed
1028  // in different register classes or requires an alignment greater
1029  // than 8 for general purpose registers and 16 for XMM registers.
1030  const llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
1031  llvm::Value *RegAddr =
1032    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
1033                           "reg_save_area");
1034  if (neededInt && neededSSE) {
1035    // FIXME: Cleanup.
1036    assert(AI.isCoerce() && "Unexpected ABI info for mixed regs");
1037    const llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
1038    llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
1039    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
1040    const llvm::Type *TyLo = ST->getElementType(0);
1041    const llvm::Type *TyHi = ST->getElementType(1);
1042    assert((TyLo->isFloatingPoint() ^ TyHi->isFloatingPoint()) &&
1043           "Unexpected ABI info for mixed regs");
1044    const llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
1045    const llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
1046    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1047    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1048    llvm::Value *RegLoAddr = TyLo->isFloatingPoint() ? FPAddr : GPAddr;
1049    llvm::Value *RegHiAddr = TyLo->isFloatingPoint() ? GPAddr : FPAddr;
1050    llvm::Value *V =
1051      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
1052    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
1053    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
1054    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
1055
1056    RegAddr = CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(LTy));
1057  } else if (neededInt) {
1058    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
1059    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1060                                        llvm::PointerType::getUnqual(LTy));
1061  } else {
1062    RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
1063    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
1064                                        llvm::PointerType::getUnqual(LTy));
1065  }
1066
1067  // AMD64-ABI 3.5.7p5: Step 5. Set:
1068  // l->gp_offset = l->gp_offset + num_gp * 8
1069  // l->fp_offset = l->fp_offset + num_fp * 16.
1070  if (neededInt) {
1071    llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1072                                                 neededInt * 8);
1073    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
1074                            gp_offset_p);
1075  }
1076  if (neededSSE) {
1077    llvm::Value *Offset = llvm::ConstantInt::get(llvm::Type::Int32Ty,
1078                                                 neededSSE * 16);
1079    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
1080                            fp_offset_p);
1081  }
1082  CGF.EmitBranch(ContBlock);
1083
1084  // Emit code to load the value if it was passed in memory.
1085
1086  CGF.EmitBlock(InMemBlock);
1087  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
1088
1089  // Return the appropriate result.
1090
1091  CGF.EmitBlock(ContBlock);
1092  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(),
1093                                                 "vaarg.addr");
1094  ResAddr->reserveOperandSpace(2);
1095  ResAddr->addIncoming(RegAddr, InRegBlock);
1096  ResAddr->addIncoming(MemAddr, InMemBlock);
1097
1098  return ResAddr;
1099}
1100
1101ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy,
1102                                              ASTContext &Context) const {
1103  if (RetTy->isVoidType()) {
1104    return ABIArgInfo::getIgnore();
1105  } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1106    return ABIArgInfo::getIndirect(0);
1107  } else {
1108    return ABIArgInfo::getDirect();
1109  }
1110}
1111
1112ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty,
1113                                                ASTContext &Context) const {
1114  if (CodeGenFunction::hasAggregateLLVMType(Ty)) {
1115    return ABIArgInfo::getIndirect(0);
1116  } else {
1117    return ABIArgInfo::getDirect();
1118  }
1119}
1120
1121llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1122                                       CodeGenFunction &CGF) const {
1123  return 0;
1124}
1125
1126const ABIInfo &CodeGenTypes::getABIInfo() const {
1127  if (TheABIInfo)
1128    return *TheABIInfo;
1129
1130  // For now we just cache this in the CodeGenTypes and don't bother
1131  // to free it.
1132  const char *TargetPrefix = getContext().Target.getTargetPrefix();
1133  if (strcmp(TargetPrefix, "x86") == 0) {
1134    switch (getContext().Target.getPointerWidth(0)) {
1135    case 32:
1136      return *(TheABIInfo = new X86_32ABIInfo());
1137    case 64:
1138      return *(TheABIInfo = new X86_64ABIInfo());
1139    }
1140  }
1141
1142  return *(TheABIInfo = new DefaultABIInfo);
1143}
1144
1145/***/
1146
1147CGFunctionInfo::CGFunctionInfo(QualType ResTy,
1148                               const llvm::SmallVector<QualType, 16> &ArgTys) {
1149  NumArgs = ArgTys.size();
1150  Args = new ArgInfo[1 + NumArgs];
1151  Args[0].type = ResTy;
1152  for (unsigned i = 0; i < NumArgs; ++i)
1153    Args[1 + i].type = ArgTys[i];
1154}
1155
1156/***/
1157
1158void CodeGenTypes::GetExpandedTypes(QualType Ty,
1159                                    std::vector<const llvm::Type*> &ArgTys) {
1160  const RecordType *RT = Ty->getAsStructureType();
1161  assert(RT && "Can only expand structure types.");
1162  const RecordDecl *RD = RT->getDecl();
1163  assert(!RD->hasFlexibleArrayMember() &&
1164         "Cannot expand structure with flexible array.");
1165
1166  for (RecordDecl::field_iterator i = RD->field_begin(),
1167         e = RD->field_end(); i != e; ++i) {
1168    const FieldDecl *FD = *i;
1169    assert(!FD->isBitField() &&
1170           "Cannot expand structure with bit-field members.");
1171
1172    QualType FT = FD->getType();
1173    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1174      GetExpandedTypes(FT, ArgTys);
1175    } else {
1176      ArgTys.push_back(ConvertType(FT));
1177    }
1178  }
1179}
1180
1181llvm::Function::arg_iterator
1182CodeGenFunction::ExpandTypeFromArgs(QualType Ty, LValue LV,
1183                                    llvm::Function::arg_iterator AI) {
1184  const RecordType *RT = Ty->getAsStructureType();
1185  assert(RT && "Can only expand structure types.");
1186
1187  RecordDecl *RD = RT->getDecl();
1188  assert(LV.isSimple() &&
1189         "Unexpected non-simple lvalue during struct expansion.");
1190  llvm::Value *Addr = LV.getAddress();
1191  for (RecordDecl::field_iterator i = RD->field_begin(),
1192         e = RD->field_end(); i != e; ++i) {
1193    FieldDecl *FD = *i;
1194    QualType FT = FD->getType();
1195
1196    // FIXME: What are the right qualifiers here?
1197    LValue LV = EmitLValueForField(Addr, FD, false, 0);
1198    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1199      AI = ExpandTypeFromArgs(FT, LV, AI);
1200    } else {
1201      EmitStoreThroughLValue(RValue::get(AI), LV, FT);
1202      ++AI;
1203    }
1204  }
1205
1206  return AI;
1207}
1208
1209void
1210CodeGenFunction::ExpandTypeToArgs(QualType Ty, RValue RV,
1211                                  llvm::SmallVector<llvm::Value*, 16> &Args) {
1212  const RecordType *RT = Ty->getAsStructureType();
1213  assert(RT && "Can only expand structure types.");
1214
1215  RecordDecl *RD = RT->getDecl();
1216  assert(RV.isAggregate() && "Unexpected rvalue during struct expansion");
1217  llvm::Value *Addr = RV.getAggregateAddr();
1218  for (RecordDecl::field_iterator i = RD->field_begin(),
1219         e = RD->field_end(); i != e; ++i) {
1220    FieldDecl *FD = *i;
1221    QualType FT = FD->getType();
1222
1223    // FIXME: What are the right qualifiers here?
1224    LValue LV = EmitLValueForField(Addr, FD, false, 0);
1225    if (CodeGenFunction::hasAggregateLLVMType(FT)) {
1226      ExpandTypeToArgs(FT, RValue::getAggregate(LV.getAddress()), Args);
1227    } else {
1228      RValue RV = EmitLoadOfLValue(LV, FT);
1229      assert(RV.isScalar() &&
1230             "Unexpected non-scalar rvalue during struct expansion.");
1231      Args.push_back(RV.getScalarVal());
1232    }
1233  }
1234}
1235
1236/// CreateCoercedLoad - Create a load from \arg SrcPtr interpreted as
1237/// a pointer to an object of type \arg Ty.
1238///
1239/// This safely handles the case when the src type is smaller than the
1240/// destination type; in this situation the values of bits which not
1241/// present in the src are undefined.
1242static llvm::Value *CreateCoercedLoad(llvm::Value *SrcPtr,
1243                                      const llvm::Type *Ty,
1244                                      CodeGenFunction &CGF) {
1245  const llvm::Type *SrcTy =
1246    cast<llvm::PointerType>(SrcPtr->getType())->getElementType();
1247  uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1248  uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(Ty);
1249
1250  // If load is legal, just bitcast the src pointer.
1251  if (SrcSize == DstSize) {
1252    llvm::Value *Casted =
1253      CGF.Builder.CreateBitCast(SrcPtr, llvm::PointerType::getUnqual(Ty));
1254    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1255    // FIXME: Use better alignment / avoid requiring aligned load.
1256    Load->setAlignment(1);
1257    return Load;
1258  } else {
1259    assert(SrcSize < DstSize && "Coercion is losing source bits!");
1260
1261    // Otherwise do coercion through memory. This is stupid, but
1262    // simple.
1263    llvm::Value *Tmp = CGF.CreateTempAlloca(Ty);
1264    llvm::Value *Casted =
1265      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(SrcTy));
1266    llvm::StoreInst *Store =
1267      CGF.Builder.CreateStore(CGF.Builder.CreateLoad(SrcPtr), Casted);
1268    // FIXME: Use better alignment / avoid requiring aligned store.
1269    Store->setAlignment(1);
1270    return CGF.Builder.CreateLoad(Tmp);
1271  }
1272}
1273
1274/// CreateCoercedStore - Create a store to \arg DstPtr from \arg Src,
1275/// where the source and destination may have different types.
1276///
1277/// This safely handles the case when the src type is larger than the
1278/// destination type; the upper bits of the src will be lost.
1279static void CreateCoercedStore(llvm::Value *Src,
1280                               llvm::Value *DstPtr,
1281                               CodeGenFunction &CGF) {
1282  const llvm::Type *SrcTy = Src->getType();
1283  const llvm::Type *DstTy =
1284    cast<llvm::PointerType>(DstPtr->getType())->getElementType();
1285
1286  uint64_t SrcSize = CGF.CGM.getTargetData().getTypePaddedSize(SrcTy);
1287  uint64_t DstSize = CGF.CGM.getTargetData().getTypePaddedSize(DstTy);
1288
1289  // If store is legal, just bitcast the src pointer.
1290  if (SrcSize == DstSize) {
1291    llvm::Value *Casted =
1292      CGF.Builder.CreateBitCast(DstPtr, llvm::PointerType::getUnqual(SrcTy));
1293    // FIXME: Use better alignment / avoid requiring aligned store.
1294    CGF.Builder.CreateStore(Src, Casted)->setAlignment(1);
1295  } else {
1296    assert(SrcSize > DstSize && "Coercion is missing bits!");
1297
1298    // Otherwise do coercion through memory. This is stupid, but
1299    // simple.
1300    llvm::Value *Tmp = CGF.CreateTempAlloca(SrcTy);
1301    CGF.Builder.CreateStore(Src, Tmp);
1302    llvm::Value *Casted =
1303      CGF.Builder.CreateBitCast(Tmp, llvm::PointerType::getUnqual(DstTy));
1304    llvm::LoadInst *Load = CGF.Builder.CreateLoad(Casted);
1305    // FIXME: Use better alignment / avoid requiring aligned load.
1306    Load->setAlignment(1);
1307    CGF.Builder.CreateStore(Load, DstPtr);
1308  }
1309}
1310
1311/***/
1312
1313bool CodeGenModule::ReturnTypeUsesSret(const CGFunctionInfo &FI) {
1314  return FI.getReturnInfo().isIndirect();
1315}
1316
1317const llvm::FunctionType *
1318CodeGenTypes::GetFunctionType(const CGFunctionInfo &FI, bool IsVariadic) {
1319  std::vector<const llvm::Type*> ArgTys;
1320
1321  const llvm::Type *ResultType = 0;
1322
1323  QualType RetTy = FI.getReturnType();
1324  const ABIArgInfo &RetAI = FI.getReturnInfo();
1325  switch (RetAI.getKind()) {
1326  case ABIArgInfo::Expand:
1327    assert(0 && "Invalid ABI kind for return argument");
1328
1329  case ABIArgInfo::Direct:
1330    ResultType = ConvertType(RetTy);
1331    break;
1332
1333  case ABIArgInfo::Indirect: {
1334    assert(!RetAI.getIndirectAlign() && "Align unused on indirect return.");
1335    ResultType = llvm::Type::VoidTy;
1336    const llvm::Type *STy = ConvertType(RetTy);
1337    ArgTys.push_back(llvm::PointerType::get(STy, RetTy.getAddressSpace()));
1338    break;
1339  }
1340
1341  case ABIArgInfo::Ignore:
1342    ResultType = llvm::Type::VoidTy;
1343    break;
1344
1345  case ABIArgInfo::Coerce:
1346    ResultType = RetAI.getCoerceToType();
1347    break;
1348  }
1349
1350  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1351         ie = FI.arg_end(); it != ie; ++it) {
1352    const ABIArgInfo &AI = it->info;
1353
1354    switch (AI.getKind()) {
1355    case ABIArgInfo::Ignore:
1356      break;
1357
1358    case ABIArgInfo::Coerce:
1359      ArgTys.push_back(AI.getCoerceToType());
1360      break;
1361
1362    case ABIArgInfo::Indirect: {
1363      // indirect arguments are always on the stack, which is addr space #0.
1364      const llvm::Type *LTy = ConvertTypeForMem(it->type);
1365      ArgTys.push_back(llvm::PointerType::getUnqual(LTy));
1366      break;
1367    }
1368
1369    case ABIArgInfo::Direct:
1370      ArgTys.push_back(ConvertType(it->type));
1371      break;
1372
1373    case ABIArgInfo::Expand:
1374      GetExpandedTypes(it->type, ArgTys);
1375      break;
1376    }
1377  }
1378
1379  return llvm::FunctionType::get(ResultType, ArgTys, IsVariadic);
1380}
1381
1382void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI,
1383                                           const Decl *TargetDecl,
1384                                           AttributeListType &PAL) {
1385  unsigned FuncAttrs = 0;
1386  unsigned RetAttrs = 0;
1387
1388  if (TargetDecl) {
1389    if (TargetDecl->getAttr<NoThrowAttr>())
1390      FuncAttrs |= llvm::Attribute::NoUnwind;
1391    if (TargetDecl->getAttr<NoReturnAttr>())
1392      FuncAttrs |= llvm::Attribute::NoReturn;
1393    if (TargetDecl->getAttr<PureAttr>())
1394      FuncAttrs |= llvm::Attribute::ReadOnly;
1395    if (TargetDecl->getAttr<ConstAttr>())
1396      FuncAttrs |= llvm::Attribute::ReadNone;
1397  }
1398
1399  QualType RetTy = FI.getReturnType();
1400  unsigned Index = 1;
1401  const ABIArgInfo &RetAI = FI.getReturnInfo();
1402  switch (RetAI.getKind()) {
1403  case ABIArgInfo::Direct:
1404    if (RetTy->isPromotableIntegerType()) {
1405      if (RetTy->isSignedIntegerType()) {
1406        RetAttrs |= llvm::Attribute::SExt;
1407      } else if (RetTy->isUnsignedIntegerType()) {
1408        RetAttrs |= llvm::Attribute::ZExt;
1409      }
1410    }
1411    break;
1412
1413  case ABIArgInfo::Indirect:
1414    PAL.push_back(llvm::AttributeWithIndex::get(Index,
1415                                                llvm::Attribute::StructRet |
1416                                                llvm::Attribute::NoAlias));
1417    ++Index;
1418    break;
1419
1420  case ABIArgInfo::Ignore:
1421  case ABIArgInfo::Coerce:
1422    break;
1423
1424  case ABIArgInfo::Expand:
1425    assert(0 && "Invalid ABI kind for return argument");
1426  }
1427
1428  if (RetAttrs)
1429    PAL.push_back(llvm::AttributeWithIndex::get(0, RetAttrs));
1430  for (CGFunctionInfo::const_arg_iterator it = FI.arg_begin(),
1431         ie = FI.arg_end(); it != ie; ++it) {
1432    QualType ParamType = it->type;
1433    const ABIArgInfo &AI = it->info;
1434    unsigned Attributes = 0;
1435
1436    switch (AI.getKind()) {
1437    case ABIArgInfo::Coerce:
1438      break;
1439
1440    case ABIArgInfo::Indirect:
1441      Attributes |= llvm::Attribute::ByVal;
1442      Attributes |=
1443        llvm::Attribute::constructAlignmentFromInt(AI.getIndirectAlign());
1444      break;
1445
1446    case ABIArgInfo::Direct:
1447      if (ParamType->isPromotableIntegerType()) {
1448        if (ParamType->isSignedIntegerType()) {
1449          Attributes |= llvm::Attribute::SExt;
1450        } else if (ParamType->isUnsignedIntegerType()) {
1451          Attributes |= llvm::Attribute::ZExt;
1452        }
1453      }
1454      break;
1455
1456    case ABIArgInfo::Ignore:
1457      // Skip increment, no matching LLVM parameter.
1458      continue;
1459
1460    case ABIArgInfo::Expand: {
1461      std::vector<const llvm::Type*> Tys;
1462      // FIXME: This is rather inefficient. Do we ever actually need
1463      // to do anything here? The result should be just reconstructed
1464      // on the other side, so extension should be a non-issue.
1465      getTypes().GetExpandedTypes(ParamType, Tys);
1466      Index += Tys.size();
1467      continue;
1468    }
1469    }
1470
1471    if (Attributes)
1472      PAL.push_back(llvm::AttributeWithIndex::get(Index, Attributes));
1473    ++Index;
1474  }
1475  if (FuncAttrs)
1476    PAL.push_back(llvm::AttributeWithIndex::get(~0, FuncAttrs));
1477
1478}
1479
1480void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
1481                                         llvm::Function *Fn,
1482                                         const FunctionArgList &Args) {
1483  // FIXME: We no longer need the types from FunctionArgList; lift up
1484  // and simplify.
1485
1486  // Emit allocs for param decls.  Give the LLVM Argument nodes names.
1487  llvm::Function::arg_iterator AI = Fn->arg_begin();
1488
1489  // Name the struct return argument.
1490  if (CGM.ReturnTypeUsesSret(FI)) {
1491    AI->setName("agg.result");
1492    ++AI;
1493  }
1494
1495  assert(FI.arg_size() == Args.size() &&
1496         "Mismatch between function signature & arguments.");
1497  CGFunctionInfo::const_arg_iterator info_it = FI.arg_begin();
1498  for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end();
1499       i != e; ++i, ++info_it) {
1500    const VarDecl *Arg = i->first;
1501    QualType Ty = info_it->type;
1502    const ABIArgInfo &ArgI = info_it->info;
1503
1504    switch (ArgI.getKind()) {
1505    case ABIArgInfo::Indirect: {
1506      llvm::Value* V = AI;
1507      if (hasAggregateLLVMType(Ty)) {
1508        // Do nothing, aggregates and complex variables are accessed by
1509        // reference.
1510      } else {
1511        // Load scalar value from indirect argument.
1512        V = EmitLoadOfScalar(V, false, Ty);
1513        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1514          // This must be a promotion, for something like
1515          // "void a(x) short x; {..."
1516          V = EmitScalarConversion(V, Ty, Arg->getType());
1517        }
1518      }
1519      EmitParmDecl(*Arg, V);
1520      break;
1521    }
1522
1523    case ABIArgInfo::Direct: {
1524      assert(AI != Fn->arg_end() && "Argument mismatch!");
1525      llvm::Value* V = AI;
1526      if (hasAggregateLLVMType(Ty)) {
1527        // Create a temporary alloca to hold the argument; the rest of
1528        // codegen expects to access aggregates & complex values by
1529        // reference.
1530        V = CreateTempAlloca(ConvertTypeForMem(Ty));
1531        Builder.CreateStore(AI, V);
1532      } else {
1533        if (!getContext().typesAreCompatible(Ty, Arg->getType())) {
1534          // This must be a promotion, for something like
1535          // "void a(x) short x; {..."
1536          V = EmitScalarConversion(V, Ty, Arg->getType());
1537        }
1538      }
1539      EmitParmDecl(*Arg, V);
1540      break;
1541    }
1542
1543    case ABIArgInfo::Expand: {
1544      // If this structure was expanded into multiple arguments then
1545      // we need to create a temporary and reconstruct it from the
1546      // arguments.
1547      std::string Name = Arg->getNameAsString();
1548      llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(Ty),
1549                                           (Name + ".addr").c_str());
1550      // FIXME: What are the right qualifiers here?
1551      llvm::Function::arg_iterator End =
1552        ExpandTypeFromArgs(Ty, LValue::MakeAddr(Temp,0), AI);
1553      EmitParmDecl(*Arg, Temp);
1554
1555      // Name the arguments used in expansion and increment AI.
1556      unsigned Index = 0;
1557      for (; AI != End; ++AI, ++Index)
1558        AI->setName(Name + "." + llvm::utostr(Index));
1559      continue;
1560    }
1561
1562    case ABIArgInfo::Ignore:
1563      // Initialize the local variable appropriately.
1564      if (hasAggregateLLVMType(Ty)) {
1565        EmitParmDecl(*Arg, CreateTempAlloca(ConvertTypeForMem(Ty)));
1566      } else {
1567        EmitParmDecl(*Arg, llvm::UndefValue::get(ConvertType(Arg->getType())));
1568      }
1569
1570      // Skip increment, no matching LLVM parameter.
1571      continue;
1572
1573    case ABIArgInfo::Coerce: {
1574      assert(AI != Fn->arg_end() && "Argument mismatch!");
1575      // FIXME: This is very wasteful; EmitParmDecl is just going to
1576      // drop the result in a new alloca anyway, so we could just
1577      // store into that directly if we broke the abstraction down
1578      // more.
1579      llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(Ty), "coerce");
1580      CreateCoercedStore(AI, V, *this);
1581      // Match to what EmitParmDecl is expecting for this type.
1582      if (!CodeGenFunction::hasAggregateLLVMType(Ty)) {
1583        V = EmitLoadOfScalar(V, false, Ty);
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
1595    ++AI;
1596  }
1597  assert(AI == Fn->arg_end() && "Argument mismatch!");
1598}
1599
1600void CodeGenFunction::EmitFunctionEpilog(const CGFunctionInfo &FI,
1601                                         llvm::Value *ReturnValue) {
1602  llvm::Value *RV = 0;
1603
1604  // Functions with no result always return void.
1605  if (ReturnValue) {
1606    QualType RetTy = FI.getReturnType();
1607    const ABIArgInfo &RetAI = FI.getReturnInfo();
1608
1609    switch (RetAI.getKind()) {
1610    case ABIArgInfo::Indirect:
1611      if (RetTy->isAnyComplexType()) {
1612        ComplexPairTy RT = LoadComplexFromAddr(ReturnValue, false);
1613        StoreComplexToAddr(RT, CurFn->arg_begin(), false);
1614      } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1615        EmitAggregateCopy(CurFn->arg_begin(), ReturnValue, RetTy);
1616      } else {
1617        EmitStoreOfScalar(Builder.CreateLoad(ReturnValue), CurFn->arg_begin(),
1618                          false);
1619      }
1620      break;
1621
1622    case ABIArgInfo::Direct:
1623      // The internal return value temp always will have
1624      // pointer-to-return-type type.
1625      RV = Builder.CreateLoad(ReturnValue);
1626      break;
1627
1628    case ABIArgInfo::Ignore:
1629      break;
1630
1631    case ABIArgInfo::Coerce:
1632      RV = CreateCoercedLoad(ReturnValue, RetAI.getCoerceToType(), *this);
1633      break;
1634
1635    case ABIArgInfo::Expand:
1636      assert(0 && "Invalid ABI kind for return argument");
1637    }
1638  }
1639
1640  if (RV) {
1641    Builder.CreateRet(RV);
1642  } else {
1643    Builder.CreateRetVoid();
1644  }
1645}
1646
1647RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
1648                                 llvm::Value *Callee,
1649                                 const CallArgList &CallArgs) {
1650  // FIXME: We no longer need the types from CallArgs; lift up and
1651  // simplify.
1652  llvm::SmallVector<llvm::Value*, 16> Args;
1653
1654  // Handle struct-return functions by passing a pointer to the
1655  // location that we would like to return into.
1656  QualType RetTy = CallInfo.getReturnType();
1657  const ABIArgInfo &RetAI = CallInfo.getReturnInfo();
1658  if (CGM.ReturnTypeUsesSret(CallInfo)) {
1659    // Create a temporary alloca to hold the result of the call. :(
1660    Args.push_back(CreateTempAlloca(ConvertTypeForMem(RetTy)));
1661  }
1662
1663  assert(CallInfo.arg_size() == CallArgs.size() &&
1664         "Mismatch between function signature & arguments.");
1665  CGFunctionInfo::const_arg_iterator info_it = CallInfo.arg_begin();
1666  for (CallArgList::const_iterator I = CallArgs.begin(), E = CallArgs.end();
1667       I != E; ++I, ++info_it) {
1668    const ABIArgInfo &ArgInfo = info_it->info;
1669    RValue RV = I->first;
1670
1671    switch (ArgInfo.getKind()) {
1672    case ABIArgInfo::Indirect:
1673      if (RV.isScalar() || RV.isComplex()) {
1674        // Make a temporary alloca to pass the argument.
1675        Args.push_back(CreateTempAlloca(ConvertTypeForMem(I->second)));
1676        if (RV.isScalar())
1677          EmitStoreOfScalar(RV.getScalarVal(), Args.back(), false);
1678        else
1679          StoreComplexToAddr(RV.getComplexVal(), Args.back(), false);
1680      } else {
1681        Args.push_back(RV.getAggregateAddr());
1682      }
1683      break;
1684
1685    case ABIArgInfo::Direct:
1686      if (RV.isScalar()) {
1687        Args.push_back(RV.getScalarVal());
1688      } else if (RV.isComplex()) {
1689        llvm::Value *Tmp = llvm::UndefValue::get(ConvertType(I->second));
1690        Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().first, 0);
1691        Tmp = Builder.CreateInsertValue(Tmp, RV.getComplexVal().second, 1);
1692        Args.push_back(Tmp);
1693      } else {
1694        Args.push_back(Builder.CreateLoad(RV.getAggregateAddr()));
1695      }
1696      break;
1697
1698    case ABIArgInfo::Ignore:
1699      break;
1700
1701    case ABIArgInfo::Coerce: {
1702      // FIXME: Avoid the conversion through memory if possible.
1703      llvm::Value *SrcPtr;
1704      if (RV.isScalar()) {
1705        SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
1706        EmitStoreOfScalar(RV.getScalarVal(), SrcPtr, false);
1707      } else if (RV.isComplex()) {
1708        SrcPtr = CreateTempAlloca(ConvertTypeForMem(I->second), "coerce");
1709        StoreComplexToAddr(RV.getComplexVal(), SrcPtr, false);
1710      } else
1711        SrcPtr = RV.getAggregateAddr();
1712      Args.push_back(CreateCoercedLoad(SrcPtr, ArgInfo.getCoerceToType(),
1713                                       *this));
1714      break;
1715    }
1716
1717    case ABIArgInfo::Expand:
1718      ExpandTypeToArgs(I->second, RV, Args);
1719      break;
1720    }
1721  }
1722
1723  llvm::CallInst *CI = Builder.CreateCall(Callee,&Args[0],&Args[0]+Args.size());
1724
1725  // FIXME: Provide TargetDecl so nounwind, noreturn, etc, etc get set.
1726  CodeGen::AttributeListType AttributeList;
1727  CGM.ConstructAttributeList(CallInfo, 0, AttributeList);
1728  CI->setAttributes(llvm::AttrListPtr::get(AttributeList.begin(),
1729                                           AttributeList.size()));
1730
1731  if (const llvm::Function *F = dyn_cast<llvm::Function>(Callee))
1732    CI->setCallingConv(F->getCallingConv());
1733  if (CI->getType() != llvm::Type::VoidTy)
1734    CI->setName("call");
1735
1736  switch (RetAI.getKind()) {
1737  case ABIArgInfo::Indirect:
1738    if (RetTy->isAnyComplexType())
1739      return RValue::getComplex(LoadComplexFromAddr(Args[0], false));
1740    else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1741      return RValue::getAggregate(Args[0]);
1742    else
1743      return RValue::get(EmitLoadOfScalar(Args[0], false, RetTy));
1744
1745  case ABIArgInfo::Direct:
1746    if (RetTy->isAnyComplexType()) {
1747      llvm::Value *Real = Builder.CreateExtractValue(CI, 0);
1748      llvm::Value *Imag = Builder.CreateExtractValue(CI, 1);
1749      return RValue::getComplex(std::make_pair(Real, Imag));
1750    } else if (CodeGenFunction::hasAggregateLLVMType(RetTy)) {
1751      llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "agg.tmp");
1752      Builder.CreateStore(CI, V);
1753      return RValue::getAggregate(V);
1754    } else
1755      return RValue::get(CI);
1756
1757  case ABIArgInfo::Ignore:
1758    // If we are ignoring an argument that had a result, make sure to
1759    // construct the appropriate return value for our caller.
1760    return GetUndefRValue(RetTy);
1761
1762  case ABIArgInfo::Coerce: {
1763    // FIXME: Avoid the conversion through memory if possible.
1764    llvm::Value *V = CreateTempAlloca(ConvertTypeForMem(RetTy), "coerce");
1765    CreateCoercedStore(CI, V, *this);
1766    if (RetTy->isAnyComplexType())
1767      return RValue::getComplex(LoadComplexFromAddr(V, false));
1768    else if (CodeGenFunction::hasAggregateLLVMType(RetTy))
1769      return RValue::getAggregate(V);
1770    else
1771      return RValue::get(EmitLoadOfScalar(V, false, RetTy));
1772  }
1773
1774  case ABIArgInfo::Expand:
1775    assert(0 && "Invalid ABI kind for return argument");
1776  }
1777
1778  assert(0 && "Unhandled ABIArgInfo::Kind");
1779  return RValue::get(0);
1780}
1781
1782/* VarArg handling */
1783
1784llvm::Value *CodeGenFunction::EmitVAArg(llvm::Value *VAListAddr, QualType Ty) {
1785  return CGM.getTypes().getABIInfo().EmitVAArg(VAListAddr, Ty, *this);
1786}
1787