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