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