TargetInfo.cpp revision 5f15f4daf0e225ea8463fe2ef522c009c95ffc9c
1//===---- TargetInfo.cpp - Encapsulate target 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 "TargetInfo.h"
16#include "ABIInfo.h"
17#include "CGCXXABI.h"
18#include "CodeGenFunction.h"
19#include "clang/AST/RecordLayout.h"
20#include "clang/Frontend/CodeGenOptions.h"
21#include "llvm/ADT/Triple.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Type.h"
24#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26using namespace CodeGen;
27
28static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
29                               llvm::Value *Array,
30                               llvm::Value *Value,
31                               unsigned FirstIndex,
32                               unsigned LastIndex) {
33  // Alternatively, we could emit this as a loop in the source.
34  for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
35    llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
36    Builder.CreateStore(Value, Cell);
37  }
38}
39
40static bool isAggregateTypeForABI(QualType T) {
41  return !CodeGenFunction::hasScalarEvaluationKind(T) ||
42         T->isMemberFunctionPointerType();
43}
44
45ABIInfo::~ABIInfo() {}
46
47static bool isRecordReturnIndirect(const RecordType *RT, CodeGen::CodeGenTypes &CGT) {
48  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
49  if (!RD)
50    return false;
51  return CGT.CGM.getCXXABI().isReturnTypeIndirect(RD);
52}
53
54
55static bool isRecordReturnIndirect(QualType T, CodeGen::CodeGenTypes &CGT) {
56  const RecordType *RT = T->getAs<RecordType>();
57  if (!RT)
58    return false;
59  return isRecordReturnIndirect(RT, CGT);
60}
61
62static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
63                                              CodeGen::CodeGenTypes &CGT) {
64  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
65  if (!RD)
66    return CGCXXABI::RAA_Default;
67  return CGT.CGM.getCXXABI().getRecordArgABI(RD);
68}
69
70static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
71                                              CodeGen::CodeGenTypes &CGT) {
72  const RecordType *RT = T->getAs<RecordType>();
73  if (!RT)
74    return CGCXXABI::RAA_Default;
75  return getRecordArgABI(RT, CGT);
76}
77
78ASTContext &ABIInfo::getContext() const {
79  return CGT.getContext();
80}
81
82llvm::LLVMContext &ABIInfo::getVMContext() const {
83  return CGT.getLLVMContext();
84}
85
86const llvm::DataLayout &ABIInfo::getDataLayout() const {
87  return CGT.getDataLayout();
88}
89
90const TargetInfo &ABIInfo::getTarget() const {
91  return CGT.getTarget();
92}
93
94void ABIArgInfo::dump() const {
95  raw_ostream &OS = llvm::errs();
96  OS << "(ABIArgInfo Kind=";
97  switch (TheKind) {
98  case Direct:
99    OS << "Direct Type=";
100    if (llvm::Type *Ty = getCoerceToType())
101      Ty->print(OS);
102    else
103      OS << "null";
104    break;
105  case Extend:
106    OS << "Extend";
107    break;
108  case Ignore:
109    OS << "Ignore";
110    break;
111  case Indirect:
112    OS << "Indirect Align=" << getIndirectAlign()
113       << " ByVal=" << getIndirectByVal()
114       << " Realign=" << getIndirectRealign();
115    break;
116  case Expand:
117    OS << "Expand";
118    break;
119  }
120  OS << ")\n";
121}
122
123TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
124
125// If someone can figure out a general rule for this, that would be great.
126// It's probably just doomed to be platform-dependent, though.
127unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
128  // Verified for:
129  //   x86-64     FreeBSD, Linux, Darwin
130  //   x86-32     FreeBSD, Linux, Darwin
131  //   PowerPC    Linux, Darwin
132  //   ARM        Darwin (*not* EABI)
133  //   AArch64    Linux
134  return 32;
135}
136
137bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
138                                     const FunctionNoProtoType *fnType) const {
139  // The following conventions are known to require this to be false:
140  //   x86_stdcall
141  //   MIPS
142  // For everything else, we just prefer false unless we opt out.
143  return false;
144}
145
146void
147TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
148                                             llvm::SmallString<24> &Opt) const {
149  // This assumes the user is passing a library name like "rt" instead of a
150  // filename like "librt.a/so", and that they don't care whether it's static or
151  // dynamic.
152  Opt = "-l";
153  Opt += Lib;
154}
155
156static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
157
158/// isEmptyField - Return true iff a the field is "empty", that is it
159/// is an unnamed bit-field or an (array of) empty record(s).
160static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
161                         bool AllowArrays) {
162  if (FD->isUnnamedBitfield())
163    return true;
164
165  QualType FT = FD->getType();
166
167  // Constant arrays of empty records count as empty, strip them off.
168  // Constant arrays of zero length always count as empty.
169  if (AllowArrays)
170    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
171      if (AT->getSize() == 0)
172        return true;
173      FT = AT->getElementType();
174    }
175
176  const RecordType *RT = FT->getAs<RecordType>();
177  if (!RT)
178    return false;
179
180  // C++ record fields are never empty, at least in the Itanium ABI.
181  //
182  // FIXME: We should use a predicate for whether this behavior is true in the
183  // current ABI.
184  if (isa<CXXRecordDecl>(RT->getDecl()))
185    return false;
186
187  return isEmptyRecord(Context, FT, AllowArrays);
188}
189
190/// isEmptyRecord - Return true iff a structure contains only empty
191/// fields. Note that a structure with a flexible array member is not
192/// considered empty.
193static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
194  const RecordType *RT = T->getAs<RecordType>();
195  if (!RT)
196    return 0;
197  const RecordDecl *RD = RT->getDecl();
198  if (RD->hasFlexibleArrayMember())
199    return false;
200
201  // If this is a C++ record, check the bases first.
202  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
203    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
204           e = CXXRD->bases_end(); i != e; ++i)
205      if (!isEmptyRecord(Context, i->getType(), true))
206        return false;
207
208  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
209         i != e; ++i)
210    if (!isEmptyField(Context, *i, AllowArrays))
211      return false;
212  return true;
213}
214
215/// isSingleElementStruct - Determine if a structure is a "single
216/// element struct", i.e. it has exactly one non-empty field or
217/// exactly one field which is itself a single element
218/// struct. Structures with flexible array members are never
219/// considered single element structs.
220///
221/// \return The field declaration for the single non-empty field, if
222/// it exists.
223static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
224  const RecordType *RT = T->getAsStructureType();
225  if (!RT)
226    return 0;
227
228  const RecordDecl *RD = RT->getDecl();
229  if (RD->hasFlexibleArrayMember())
230    return 0;
231
232  const Type *Found = 0;
233
234  // If this is a C++ record, check the bases first.
235  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
236    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
237           e = CXXRD->bases_end(); i != e; ++i) {
238      // Ignore empty records.
239      if (isEmptyRecord(Context, i->getType(), true))
240        continue;
241
242      // If we already found an element then this isn't a single-element struct.
243      if (Found)
244        return 0;
245
246      // If this is non-empty and not a single element struct, the composite
247      // cannot be a single element struct.
248      Found = isSingleElementStruct(i->getType(), Context);
249      if (!Found)
250        return 0;
251    }
252  }
253
254  // Check for single element.
255  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
256         i != e; ++i) {
257    const FieldDecl *FD = *i;
258    QualType FT = FD->getType();
259
260    // Ignore empty fields.
261    if (isEmptyField(Context, FD, true))
262      continue;
263
264    // If we already found an element then this isn't a single-element
265    // struct.
266    if (Found)
267      return 0;
268
269    // Treat single element arrays as the element.
270    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
271      if (AT->getSize().getZExtValue() != 1)
272        break;
273      FT = AT->getElementType();
274    }
275
276    if (!isAggregateTypeForABI(FT)) {
277      Found = FT.getTypePtr();
278    } else {
279      Found = isSingleElementStruct(FT, Context);
280      if (!Found)
281        return 0;
282    }
283  }
284
285  // We don't consider a struct a single-element struct if it has
286  // padding beyond the element type.
287  if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
288    return 0;
289
290  return Found;
291}
292
293static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
294  // Treat complex types as the element type.
295  if (const ComplexType *CTy = Ty->getAs<ComplexType>())
296    Ty = CTy->getElementType();
297
298  // Check for a type which we know has a simple scalar argument-passing
299  // convention without any padding.  (We're specifically looking for 32
300  // and 64-bit integer and integer-equivalents, float, and double.)
301  if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
302      !Ty->isEnumeralType() && !Ty->isBlockPointerType())
303    return false;
304
305  uint64_t Size = Context.getTypeSize(Ty);
306  return Size == 32 || Size == 64;
307}
308
309/// canExpandIndirectArgument - Test whether an argument type which is to be
310/// passed indirectly (on the stack) would have the equivalent layout if it was
311/// expanded into separate arguments. If so, we prefer to do the latter to avoid
312/// inhibiting optimizations.
313///
314// FIXME: This predicate is missing many cases, currently it just follows
315// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
316// should probably make this smarter, or better yet make the LLVM backend
317// capable of handling it.
318static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
319  // We can only expand structure types.
320  const RecordType *RT = Ty->getAs<RecordType>();
321  if (!RT)
322    return false;
323
324  // We can only expand (C) structures.
325  //
326  // FIXME: This needs to be generalized to handle classes as well.
327  const RecordDecl *RD = RT->getDecl();
328  if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
329    return false;
330
331  uint64_t Size = 0;
332
333  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
334         i != e; ++i) {
335    const FieldDecl *FD = *i;
336
337    if (!is32Or64BitBasicType(FD->getType(), Context))
338      return false;
339
340    // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
341    // how to expand them yet, and the predicate for telling if a bitfield still
342    // counts as "basic" is more complicated than what we were doing previously.
343    if (FD->isBitField())
344      return false;
345
346    Size += Context.getTypeSize(FD->getType());
347  }
348
349  // Make sure there are not any holes in the struct.
350  if (Size != Context.getTypeSize(Ty))
351    return false;
352
353  return true;
354}
355
356namespace {
357/// DefaultABIInfo - The default implementation for ABI specific
358/// details. This implementation provides information which results in
359/// self-consistent and sensible LLVM IR generation, but does not
360/// conform to any particular ABI.
361class DefaultABIInfo : public ABIInfo {
362public:
363  DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
364
365  ABIArgInfo classifyReturnType(QualType RetTy) const;
366  ABIArgInfo classifyArgumentType(QualType RetTy) const;
367
368  virtual void computeInfo(CGFunctionInfo &FI) const {
369    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
370    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
371         it != ie; ++it)
372      it->info = classifyArgumentType(it->type);
373  }
374
375  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
376                                 CodeGenFunction &CGF) const;
377};
378
379class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
380public:
381  DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
382    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
383};
384
385llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
386                                       CodeGenFunction &CGF) const {
387  return 0;
388}
389
390ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
391  if (isAggregateTypeForABI(Ty)) {
392    // Records with non trivial destructors/constructors should not be passed
393    // by value.
394    if (isRecordReturnIndirect(Ty, CGT))
395      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
396
397    return ABIArgInfo::getIndirect(0);
398  }
399
400  // Treat an enum type as its underlying type.
401  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
402    Ty = EnumTy->getDecl()->getIntegerType();
403
404  return (Ty->isPromotableIntegerType() ?
405          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
406}
407
408ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
409  if (RetTy->isVoidType())
410    return ABIArgInfo::getIgnore();
411
412  if (isAggregateTypeForABI(RetTy))
413    return ABIArgInfo::getIndirect(0);
414
415  // Treat an enum type as its underlying type.
416  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
417    RetTy = EnumTy->getDecl()->getIntegerType();
418
419  return (RetTy->isPromotableIntegerType() ?
420          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
421}
422
423//===----------------------------------------------------------------------===//
424// le32/PNaCl bitcode ABI Implementation
425//
426// This is a simplified version of the x86_32 ABI.  Arguments and return values
427// are always passed on the stack.
428//===----------------------------------------------------------------------===//
429
430class PNaClABIInfo : public ABIInfo {
431 public:
432  PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
433
434  ABIArgInfo classifyReturnType(QualType RetTy) const;
435  ABIArgInfo classifyArgumentType(QualType RetTy) const;
436
437  virtual void computeInfo(CGFunctionInfo &FI) const;
438  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
439                                 CodeGenFunction &CGF) const;
440};
441
442class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
443 public:
444  PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
445    : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
446};
447
448void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
449    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
450
451    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
452         it != ie; ++it)
453      it->info = classifyArgumentType(it->type);
454  }
455
456llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
457                                       CodeGenFunction &CGF) const {
458  return 0;
459}
460
461/// \brief Classify argument of given type \p Ty.
462ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
463  if (isAggregateTypeForABI(Ty)) {
464    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
465      return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
466    return ABIArgInfo::getIndirect(0);
467  } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
468    // Treat an enum type as its underlying type.
469    Ty = EnumTy->getDecl()->getIntegerType();
470  } else if (Ty->isFloatingType()) {
471    // Floating-point types don't go inreg.
472    return ABIArgInfo::getDirect();
473  }
474
475  return (Ty->isPromotableIntegerType() ?
476          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
477}
478
479ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
480  if (RetTy->isVoidType())
481    return ABIArgInfo::getIgnore();
482
483  // In the PNaCl ABI we always return records/structures on the stack.
484  if (isAggregateTypeForABI(RetTy))
485    return ABIArgInfo::getIndirect(0);
486
487  // Treat an enum type as its underlying type.
488  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
489    RetTy = EnumTy->getDecl()->getIntegerType();
490
491  return (RetTy->isPromotableIntegerType() ?
492          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
493}
494
495/// IsX86_MMXType - Return true if this is an MMX type.
496bool IsX86_MMXType(llvm::Type *IRType) {
497  // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
498  return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
499    cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
500    IRType->getScalarSizeInBits() != 64;
501}
502
503static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
504                                          StringRef Constraint,
505                                          llvm::Type* Ty) {
506  if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
507    if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
508      // Invalid MMX constraint
509      return 0;
510    }
511
512    return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
513  }
514
515  // No operation needed
516  return Ty;
517}
518
519//===----------------------------------------------------------------------===//
520// X86-32 ABI Implementation
521//===----------------------------------------------------------------------===//
522
523/// X86_32ABIInfo - The X86-32 ABI information.
524class X86_32ABIInfo : public ABIInfo {
525  enum Class {
526    Integer,
527    Float
528  };
529
530  static const unsigned MinABIStackAlignInBytes = 4;
531
532  bool IsDarwinVectorABI;
533  bool IsSmallStructInRegABI;
534  bool IsWin32StructABI;
535  unsigned DefaultNumRegisterParameters;
536
537  static bool isRegisterSize(unsigned Size) {
538    return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
539  }
540
541  static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
542                                          unsigned callingConvention);
543
544  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
545  /// such that the argument will be passed in memory.
546  ABIArgInfo getIndirectResult(QualType Ty, bool ByVal,
547                               unsigned &FreeRegs) const;
548
549  /// \brief Return the alignment to use for the given type on the stack.
550  unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
551
552  Class classify(QualType Ty) const;
553  ABIArgInfo classifyReturnType(QualType RetTy,
554                                unsigned callingConvention) const;
555  ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs,
556                                  bool IsFastCall) const;
557  bool shouldUseInReg(QualType Ty, unsigned &FreeRegs,
558                      bool IsFastCall, bool &NeedsPadding) const;
559
560public:
561
562  virtual void computeInfo(CGFunctionInfo &FI) const;
563  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
564                                 CodeGenFunction &CGF) const;
565
566  X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w,
567                unsigned r)
568    : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
569      IsWin32StructABI(w), DefaultNumRegisterParameters(r) {}
570};
571
572class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
573public:
574  X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
575      bool d, bool p, bool w, unsigned r)
576    :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {}
577
578  static bool isStructReturnInRegABI(
579      const llvm::Triple &Triple, const CodeGenOptions &Opts);
580
581  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
582                           CodeGen::CodeGenModule &CGM) const;
583
584  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
585    // Darwin uses different dwarf register numbers for EH.
586    if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
587    return 4;
588  }
589
590  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
591                               llvm::Value *Address) const;
592
593  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
594                                  StringRef Constraint,
595                                  llvm::Type* Ty) const {
596    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
597  }
598
599};
600
601}
602
603/// shouldReturnTypeInRegister - Determine if the given type should be
604/// passed in a register (for the Darwin ABI).
605bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
606                                               ASTContext &Context,
607                                               unsigned callingConvention) {
608  uint64_t Size = Context.getTypeSize(Ty);
609
610  // Type must be register sized.
611  if (!isRegisterSize(Size))
612    return false;
613
614  if (Ty->isVectorType()) {
615    // 64- and 128- bit vectors inside structures are not returned in
616    // registers.
617    if (Size == 64 || Size == 128)
618      return false;
619
620    return true;
621  }
622
623  // If this is a builtin, pointer, enum, complex type, member pointer, or
624  // member function pointer it is ok.
625  if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
626      Ty->isAnyComplexType() || Ty->isEnumeralType() ||
627      Ty->isBlockPointerType() || Ty->isMemberPointerType())
628    return true;
629
630  // Arrays are treated like records.
631  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
632    return shouldReturnTypeInRegister(AT->getElementType(), Context,
633                                      callingConvention);
634
635  // Otherwise, it must be a record type.
636  const RecordType *RT = Ty->getAs<RecordType>();
637  if (!RT) return false;
638
639  // FIXME: Traverse bases here too.
640
641  // For thiscall conventions, structures will never be returned in
642  // a register.  This is for compatibility with the MSVC ABI
643  if (callingConvention == llvm::CallingConv::X86_ThisCall &&
644      RT->isStructureType()) {
645    return false;
646  }
647
648  // Structure types are passed in register if all fields would be
649  // passed in a register.
650  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
651         e = RT->getDecl()->field_end(); i != e; ++i) {
652    const FieldDecl *FD = *i;
653
654    // Empty fields are ignored.
655    if (isEmptyField(Context, FD, true))
656      continue;
657
658    // Check fields recursively.
659    if (!shouldReturnTypeInRegister(FD->getType(), Context,
660                                    callingConvention))
661      return false;
662  }
663  return true;
664}
665
666ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
667                                            unsigned callingConvention) const {
668  if (RetTy->isVoidType())
669    return ABIArgInfo::getIgnore();
670
671  if (const VectorType *VT = RetTy->getAs<VectorType>()) {
672    // On Darwin, some vectors are returned in registers.
673    if (IsDarwinVectorABI) {
674      uint64_t Size = getContext().getTypeSize(RetTy);
675
676      // 128-bit vectors are a special case; they are returned in
677      // registers and we need to make sure to pick a type the LLVM
678      // backend will like.
679      if (Size == 128)
680        return ABIArgInfo::getDirect(llvm::VectorType::get(
681                  llvm::Type::getInt64Ty(getVMContext()), 2));
682
683      // Always return in register if it fits in a general purpose
684      // register, or if it is 64 bits and has a single element.
685      if ((Size == 8 || Size == 16 || Size == 32) ||
686          (Size == 64 && VT->getNumElements() == 1))
687        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
688                                                            Size));
689
690      return ABIArgInfo::getIndirect(0);
691    }
692
693    return ABIArgInfo::getDirect();
694  }
695
696  if (isAggregateTypeForABI(RetTy)) {
697    if (const RecordType *RT = RetTy->getAs<RecordType>()) {
698      if (isRecordReturnIndirect(RT, CGT))
699        return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
700
701      // Structures with flexible arrays are always indirect.
702      if (RT->getDecl()->hasFlexibleArrayMember())
703        return ABIArgInfo::getIndirect(0);
704    }
705
706    // If specified, structs and unions are always indirect.
707    if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
708      return ABIArgInfo::getIndirect(0);
709
710    // Small structures which are register sized are generally returned
711    // in a register.
712    if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
713                                                  callingConvention)) {
714      uint64_t Size = getContext().getTypeSize(RetTy);
715
716      // As a special-case, if the struct is a "single-element" struct, and
717      // the field is of type "float" or "double", return it in a
718      // floating-point register. (MSVC does not apply this special case.)
719      // We apply a similar transformation for pointer types to improve the
720      // quality of the generated IR.
721      if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
722        if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
723            || SeltTy->hasPointerRepresentation())
724          return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
725
726      // FIXME: We should be able to narrow this integer in cases with dead
727      // padding.
728      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
729    }
730
731    return ABIArgInfo::getIndirect(0);
732  }
733
734  // Treat an enum type as its underlying type.
735  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
736    RetTy = EnumTy->getDecl()->getIntegerType();
737
738  return (RetTy->isPromotableIntegerType() ?
739          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
740}
741
742static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
743  return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
744}
745
746static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
747  const RecordType *RT = Ty->getAs<RecordType>();
748  if (!RT)
749    return 0;
750  const RecordDecl *RD = RT->getDecl();
751
752  // If this is a C++ record, check the bases first.
753  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
754    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
755           e = CXXRD->bases_end(); i != e; ++i)
756      if (!isRecordWithSSEVectorType(Context, i->getType()))
757        return false;
758
759  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
760       i != e; ++i) {
761    QualType FT = i->getType();
762
763    if (isSSEVectorType(Context, FT))
764      return true;
765
766    if (isRecordWithSSEVectorType(Context, FT))
767      return true;
768  }
769
770  return false;
771}
772
773unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
774                                                 unsigned Align) const {
775  // Otherwise, if the alignment is less than or equal to the minimum ABI
776  // alignment, just use the default; the backend will handle this.
777  if (Align <= MinABIStackAlignInBytes)
778    return 0; // Use default alignment.
779
780  // On non-Darwin, the stack type alignment is always 4.
781  if (!IsDarwinVectorABI) {
782    // Set explicit alignment, since we may need to realign the top.
783    return MinABIStackAlignInBytes;
784  }
785
786  // Otherwise, if the type contains an SSE vector type, the alignment is 16.
787  if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
788                      isRecordWithSSEVectorType(getContext(), Ty)))
789    return 16;
790
791  return MinABIStackAlignInBytes;
792}
793
794ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
795                                            unsigned &FreeRegs) const {
796  if (!ByVal) {
797    if (FreeRegs) {
798      --FreeRegs; // Non byval indirects just use one pointer.
799      return ABIArgInfo::getIndirectInReg(0, false);
800    }
801    return ABIArgInfo::getIndirect(0, false);
802  }
803
804  // Compute the byval alignment.
805  unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
806  unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
807  if (StackAlign == 0)
808    return ABIArgInfo::getIndirect(4);
809
810  // If the stack alignment is less than the type alignment, realign the
811  // argument.
812  if (StackAlign < TypeAlign)
813    return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
814                                   /*Realign=*/true);
815
816  return ABIArgInfo::getIndirect(StackAlign);
817}
818
819X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
820  const Type *T = isSingleElementStruct(Ty, getContext());
821  if (!T)
822    T = Ty.getTypePtr();
823
824  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
825    BuiltinType::Kind K = BT->getKind();
826    if (K == BuiltinType::Float || K == BuiltinType::Double)
827      return Float;
828  }
829  return Integer;
830}
831
832bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs,
833                                   bool IsFastCall, bool &NeedsPadding) const {
834  NeedsPadding = false;
835  Class C = classify(Ty);
836  if (C == Float)
837    return false;
838
839  unsigned Size = getContext().getTypeSize(Ty);
840  unsigned SizeInRegs = (Size + 31) / 32;
841
842  if (SizeInRegs == 0)
843    return false;
844
845  if (SizeInRegs > FreeRegs) {
846    FreeRegs = 0;
847    return false;
848  }
849
850  FreeRegs -= SizeInRegs;
851
852  if (IsFastCall) {
853    if (Size > 32)
854      return false;
855
856    if (Ty->isIntegralOrEnumerationType())
857      return true;
858
859    if (Ty->isPointerType())
860      return true;
861
862    if (Ty->isReferenceType())
863      return true;
864
865    if (FreeRegs)
866      NeedsPadding = true;
867
868    return false;
869  }
870
871  return true;
872}
873
874ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
875                                               unsigned &FreeRegs,
876                                               bool IsFastCall) const {
877  // FIXME: Set alignment on indirect arguments.
878  if (isAggregateTypeForABI(Ty)) {
879    if (const RecordType *RT = Ty->getAs<RecordType>()) {
880      if (IsWin32StructABI)
881        return getIndirectResult(Ty, true, FreeRegs);
882
883      if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
884        return getIndirectResult(Ty, RAA == CGCXXABI::RAA_DirectInMemory, FreeRegs);
885
886      // Structures with flexible arrays are always indirect.
887      if (RT->getDecl()->hasFlexibleArrayMember())
888        return getIndirectResult(Ty, true, FreeRegs);
889    }
890
891    // Ignore empty structs/unions.
892    if (isEmptyRecord(getContext(), Ty, true))
893      return ABIArgInfo::getIgnore();
894
895    llvm::LLVMContext &LLVMContext = getVMContext();
896    llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
897    bool NeedsPadding;
898    if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) {
899      unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
900      SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
901      llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
902      return ABIArgInfo::getDirectInReg(Result);
903    }
904    llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0;
905
906    // Expand small (<= 128-bit) record types when we know that the stack layout
907    // of those arguments will match the struct. This is important because the
908    // LLVM backend isn't smart enough to remove byval, which inhibits many
909    // optimizations.
910    if (getContext().getTypeSize(Ty) <= 4*32 &&
911        canExpandIndirectArgument(Ty, getContext()))
912      return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType);
913
914    return getIndirectResult(Ty, true, FreeRegs);
915  }
916
917  if (const VectorType *VT = Ty->getAs<VectorType>()) {
918    // On Darwin, some vectors are passed in memory, we handle this by passing
919    // it as an i8/i16/i32/i64.
920    if (IsDarwinVectorABI) {
921      uint64_t Size = getContext().getTypeSize(Ty);
922      if ((Size == 8 || Size == 16 || Size == 32) ||
923          (Size == 64 && VT->getNumElements() == 1))
924        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
925                                                            Size));
926    }
927
928    if (IsX86_MMXType(CGT.ConvertType(Ty)))
929      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
930
931    return ABIArgInfo::getDirect();
932  }
933
934
935  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
936    Ty = EnumTy->getDecl()->getIntegerType();
937
938  bool NeedsPadding;
939  bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding);
940
941  if (Ty->isPromotableIntegerType()) {
942    if (InReg)
943      return ABIArgInfo::getExtendInReg();
944    return ABIArgInfo::getExtend();
945  }
946  if (InReg)
947    return ABIArgInfo::getDirectInReg();
948  return ABIArgInfo::getDirect();
949}
950
951void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
952  FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
953                                          FI.getCallingConvention());
954
955  unsigned CC = FI.getCallingConvention();
956  bool IsFastCall = CC == llvm::CallingConv::X86_FastCall;
957  unsigned FreeRegs;
958  if (IsFastCall)
959    FreeRegs = 2;
960  else if (FI.getHasRegParm())
961    FreeRegs = FI.getRegParm();
962  else
963    FreeRegs = DefaultNumRegisterParameters;
964
965  // If the return value is indirect, then the hidden argument is consuming one
966  // integer register.
967  if (FI.getReturnInfo().isIndirect() && FreeRegs) {
968    --FreeRegs;
969    ABIArgInfo &Old = FI.getReturnInfo();
970    Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
971                                       Old.getIndirectByVal(),
972                                       Old.getIndirectRealign());
973  }
974
975  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
976       it != ie; ++it)
977    it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall);
978}
979
980llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
981                                      CodeGenFunction &CGF) const {
982  llvm::Type *BPP = CGF.Int8PtrPtrTy;
983
984  CGBuilderTy &Builder = CGF.Builder;
985  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
986                                                       "ap");
987  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
988
989  // Compute if the address needs to be aligned
990  unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
991  Align = getTypeStackAlignInBytes(Ty, Align);
992  Align = std::max(Align, 4U);
993  if (Align > 4) {
994    // addr = (addr + align - 1) & -align;
995    llvm::Value *Offset =
996      llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
997    Addr = CGF.Builder.CreateGEP(Addr, Offset);
998    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
999                                                    CGF.Int32Ty);
1000    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
1001    Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
1002                                      Addr->getType(),
1003                                      "ap.cur.aligned");
1004  }
1005
1006  llvm::Type *PTy =
1007    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
1008  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
1009
1010  uint64_t Offset =
1011    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
1012  llvm::Value *NextAddr =
1013    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
1014                      "ap.next");
1015  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
1016
1017  return AddrTyped;
1018}
1019
1020void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
1021                                                  llvm::GlobalValue *GV,
1022                                            CodeGen::CodeGenModule &CGM) const {
1023  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1024    if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1025      // Get the LLVM function.
1026      llvm::Function *Fn = cast<llvm::Function>(GV);
1027
1028      // Now add the 'alignstack' attribute with a value of 16.
1029      llvm::AttrBuilder B;
1030      B.addStackAlignmentAttr(16);
1031      Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1032                      llvm::AttributeSet::get(CGM.getLLVMContext(),
1033                                              llvm::AttributeSet::FunctionIndex,
1034                                              B));
1035    }
1036  }
1037}
1038
1039bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1040                                               CodeGen::CodeGenFunction &CGF,
1041                                               llvm::Value *Address) const {
1042  CodeGen::CGBuilderTy &Builder = CGF.Builder;
1043
1044  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
1045
1046  // 0-7 are the eight integer registers;  the order is different
1047  //   on Darwin (for EH), but the range is the same.
1048  // 8 is %eip.
1049  AssignToArrayRange(Builder, Address, Four8, 0, 8);
1050
1051  if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
1052    // 12-16 are st(0..4).  Not sure why we stop at 4.
1053    // These have size 16, which is sizeof(long double) on
1054    // platforms with 8-byte alignment for that type.
1055    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
1056    AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
1057
1058  } else {
1059    // 9 is %eflags, which doesn't get a size on Darwin for some
1060    // reason.
1061    Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
1062
1063    // 11-16 are st(0..5).  Not sure why we stop at 5.
1064    // These have size 12, which is sizeof(long double) on
1065    // platforms with 4-byte alignment for that type.
1066    llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
1067    AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1068  }
1069
1070  return false;
1071}
1072
1073//===----------------------------------------------------------------------===//
1074// X86-64 ABI Implementation
1075//===----------------------------------------------------------------------===//
1076
1077
1078namespace {
1079/// X86_64ABIInfo - The X86_64 ABI information.
1080class X86_64ABIInfo : public ABIInfo {
1081  enum Class {
1082    Integer = 0,
1083    SSE,
1084    SSEUp,
1085    X87,
1086    X87Up,
1087    ComplexX87,
1088    NoClass,
1089    Memory
1090  };
1091
1092  /// merge - Implement the X86_64 ABI merging algorithm.
1093  ///
1094  /// Merge an accumulating classification \arg Accum with a field
1095  /// classification \arg Field.
1096  ///
1097  /// \param Accum - The accumulating classification. This should
1098  /// always be either NoClass or the result of a previous merge
1099  /// call. In addition, this should never be Memory (the caller
1100  /// should just return Memory for the aggregate).
1101  static Class merge(Class Accum, Class Field);
1102
1103  /// postMerge - Implement the X86_64 ABI post merging algorithm.
1104  ///
1105  /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1106  /// final MEMORY or SSE classes when necessary.
1107  ///
1108  /// \param AggregateSize - The size of the current aggregate in
1109  /// the classification process.
1110  ///
1111  /// \param Lo - The classification for the parts of the type
1112  /// residing in the low word of the containing object.
1113  ///
1114  /// \param Hi - The classification for the parts of the type
1115  /// residing in the higher words of the containing object.
1116  ///
1117  void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1118
1119  /// classify - Determine the x86_64 register classes in which the
1120  /// given type T should be passed.
1121  ///
1122  /// \param Lo - The classification for the parts of the type
1123  /// residing in the low word of the containing object.
1124  ///
1125  /// \param Hi - The classification for the parts of the type
1126  /// residing in the high word of the containing object.
1127  ///
1128  /// \param OffsetBase - The bit offset of this type in the
1129  /// containing object.  Some parameters are classified different
1130  /// depending on whether they straddle an eightbyte boundary.
1131  ///
1132  /// \param isNamedArg - Whether the argument in question is a "named"
1133  /// argument, as used in AMD64-ABI 3.5.7.
1134  ///
1135  /// If a word is unused its result will be NoClass; if a type should
1136  /// be passed in Memory then at least the classification of \arg Lo
1137  /// will be Memory.
1138  ///
1139  /// The \arg Lo class will be NoClass iff the argument is ignored.
1140  ///
1141  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1142  /// also be ComplexX87.
1143  void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1144                bool isNamedArg) const;
1145
1146  llvm::Type *GetByteVectorType(QualType Ty) const;
1147  llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1148                                 unsigned IROffset, QualType SourceTy,
1149                                 unsigned SourceOffset) const;
1150  llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1151                                     unsigned IROffset, QualType SourceTy,
1152                                     unsigned SourceOffset) const;
1153
1154  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1155  /// such that the argument will be returned in memory.
1156  ABIArgInfo getIndirectReturnResult(QualType Ty) const;
1157
1158  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1159  /// such that the argument will be passed in memory.
1160  ///
1161  /// \param freeIntRegs - The number of free integer registers remaining
1162  /// available.
1163  ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
1164
1165  ABIArgInfo classifyReturnType(QualType RetTy) const;
1166
1167  ABIArgInfo classifyArgumentType(QualType Ty,
1168                                  unsigned freeIntRegs,
1169                                  unsigned &neededInt,
1170                                  unsigned &neededSSE,
1171                                  bool isNamedArg) const;
1172
1173  bool IsIllegalVectorType(QualType Ty) const;
1174
1175  /// The 0.98 ABI revision clarified a lot of ambiguities,
1176  /// unfortunately in ways that were not always consistent with
1177  /// certain previous compilers.  In particular, platforms which
1178  /// required strict binary compatibility with older versions of GCC
1179  /// may need to exempt themselves.
1180  bool honorsRevision0_98() const {
1181    return !getTarget().getTriple().isOSDarwin();
1182  }
1183
1184  bool HasAVX;
1185  // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1186  // 64-bit hardware.
1187  bool Has64BitPointers;
1188
1189public:
1190  X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
1191      ABIInfo(CGT), HasAVX(hasavx),
1192      Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
1193  }
1194
1195  bool isPassedUsingAVXType(QualType type) const {
1196    unsigned neededInt, neededSSE;
1197    // The freeIntRegs argument doesn't matter here.
1198    ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1199                                           /*isNamedArg*/true);
1200    if (info.isDirect()) {
1201      llvm::Type *ty = info.getCoerceToType();
1202      if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1203        return (vectorTy->getBitWidth() > 128);
1204    }
1205    return false;
1206  }
1207
1208  virtual void computeInfo(CGFunctionInfo &FI) const;
1209
1210  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1211                                 CodeGenFunction &CGF) const;
1212};
1213
1214/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
1215class WinX86_64ABIInfo : public ABIInfo {
1216
1217  ABIArgInfo classify(QualType Ty, bool IsReturnType) const;
1218
1219public:
1220  WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1221
1222  virtual void computeInfo(CGFunctionInfo &FI) const;
1223
1224  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1225                                 CodeGenFunction &CGF) const;
1226};
1227
1228class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1229public:
1230  X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
1231      : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
1232
1233  const X86_64ABIInfo &getABIInfo() const {
1234    return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1235  }
1236
1237  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1238    return 7;
1239  }
1240
1241  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1242                               llvm::Value *Address) const {
1243    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
1244
1245    // 0-15 are the 16 integer registers.
1246    // 16 is %rip.
1247    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1248    return false;
1249  }
1250
1251  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1252                                  StringRef Constraint,
1253                                  llvm::Type* Ty) const {
1254    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1255  }
1256
1257  bool isNoProtoCallVariadic(const CallArgList &args,
1258                             const FunctionNoProtoType *fnType) const {
1259    // The default CC on x86-64 sets %al to the number of SSA
1260    // registers used, and GCC sets this when calling an unprototyped
1261    // function, so we override the default behavior.  However, don't do
1262    // that when AVX types are involved: the ABI explicitly states it is
1263    // undefined, and it doesn't work in practice because of how the ABI
1264    // defines varargs anyway.
1265    if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
1266      bool HasAVXType = false;
1267      for (CallArgList::const_iterator
1268             it = args.begin(), ie = args.end(); it != ie; ++it) {
1269        if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1270          HasAVXType = true;
1271          break;
1272        }
1273      }
1274
1275      if (!HasAVXType)
1276        return true;
1277    }
1278
1279    return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
1280  }
1281
1282};
1283
1284static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1285  // If the argument does not end in .lib, automatically add the suffix. This
1286  // matches the behavior of MSVC.
1287  std::string ArgStr = Lib;
1288  if (Lib.size() <= 4 ||
1289      Lib.substr(Lib.size() - 4).compare_lower(".lib") != 0) {
1290    ArgStr += ".lib";
1291  }
1292  return ArgStr;
1293}
1294
1295class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1296public:
1297  WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1298        bool d, bool p, bool w, unsigned RegParms)
1299    : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {}
1300
1301  void getDependentLibraryOption(llvm::StringRef Lib,
1302                                 llvm::SmallString<24> &Opt) const {
1303    Opt = "/DEFAULTLIB:";
1304    Opt += qualifyWindowsLibrary(Lib);
1305  }
1306
1307  void getDetectMismatchOption(llvm::StringRef Name,
1308                               llvm::StringRef Value,
1309                               llvm::SmallString<32> &Opt) const {
1310    Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
1311  }
1312};
1313
1314class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1315public:
1316  WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1317    : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1318
1319  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1320    return 7;
1321  }
1322
1323  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1324                               llvm::Value *Address) const {
1325    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
1326
1327    // 0-15 are the 16 integer registers.
1328    // 16 is %rip.
1329    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1330    return false;
1331  }
1332
1333  void getDependentLibraryOption(llvm::StringRef Lib,
1334                                 llvm::SmallString<24> &Opt) const {
1335    Opt = "/DEFAULTLIB:";
1336    Opt += qualifyWindowsLibrary(Lib);
1337  }
1338
1339  void getDetectMismatchOption(llvm::StringRef Name,
1340                               llvm::StringRef Value,
1341                               llvm::SmallString<32> &Opt) const {
1342    Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
1343  }
1344};
1345
1346}
1347
1348void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
1349                              Class &Hi) const {
1350  // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
1351  //
1352  // (a) If one of the classes is Memory, the whole argument is passed in
1353  //     memory.
1354  //
1355  // (b) If X87UP is not preceded by X87, the whole argument is passed in
1356  //     memory.
1357  //
1358  // (c) If the size of the aggregate exceeds two eightbytes and the first
1359  //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
1360  //     argument is passed in memory. NOTE: This is necessary to keep the
1361  //     ABI working for processors that don't support the __m256 type.
1362  //
1363  // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
1364  //
1365  // Some of these are enforced by the merging logic.  Others can arise
1366  // only with unions; for example:
1367  //   union { _Complex double; unsigned; }
1368  //
1369  // Note that clauses (b) and (c) were added in 0.98.
1370  //
1371  if (Hi == Memory)
1372    Lo = Memory;
1373  if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
1374    Lo = Memory;
1375  if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
1376    Lo = Memory;
1377  if (Hi == SSEUp && Lo != SSE)
1378    Hi = SSE;
1379}
1380
1381X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
1382  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1383  // classified recursively so that always two fields are
1384  // considered. The resulting class is calculated according to
1385  // the classes of the fields in the eightbyte:
1386  //
1387  // (a) If both classes are equal, this is the resulting class.
1388  //
1389  // (b) If one of the classes is NO_CLASS, the resulting class is
1390  // the other class.
1391  //
1392  // (c) If one of the classes is MEMORY, the result is the MEMORY
1393  // class.
1394  //
1395  // (d) If one of the classes is INTEGER, the result is the
1396  // INTEGER.
1397  //
1398  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1399  // MEMORY is used as class.
1400  //
1401  // (f) Otherwise class SSE is used.
1402
1403  // Accum should never be memory (we should have returned) or
1404  // ComplexX87 (because this cannot be passed in a structure).
1405  assert((Accum != Memory && Accum != ComplexX87) &&
1406         "Invalid accumulated classification during merge.");
1407  if (Accum == Field || Field == NoClass)
1408    return Accum;
1409  if (Field == Memory)
1410    return Memory;
1411  if (Accum == NoClass)
1412    return Field;
1413  if (Accum == Integer || Field == Integer)
1414    return Integer;
1415  if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
1416      Accum == X87 || Accum == X87Up)
1417    return Memory;
1418  return SSE;
1419}
1420
1421void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
1422                             Class &Lo, Class &Hi, bool isNamedArg) const {
1423  // FIXME: This code can be simplified by introducing a simple value class for
1424  // Class pairs with appropriate constructor methods for the various
1425  // situations.
1426
1427  // FIXME: Some of the split computations are wrong; unaligned vectors
1428  // shouldn't be passed in registers for example, so there is no chance they
1429  // can straddle an eightbyte. Verify & simplify.
1430
1431  Lo = Hi = NoClass;
1432
1433  Class &Current = OffsetBase < 64 ? Lo : Hi;
1434  Current = Memory;
1435
1436  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1437    BuiltinType::Kind k = BT->getKind();
1438
1439    if (k == BuiltinType::Void) {
1440      Current = NoClass;
1441    } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1442      Lo = Integer;
1443      Hi = Integer;
1444    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1445      Current = Integer;
1446    } else if ((k == BuiltinType::Float || k == BuiltinType::Double) ||
1447               (k == BuiltinType::LongDouble &&
1448                getTarget().getTriple().getOS() == llvm::Triple::NaCl)) {
1449      Current = SSE;
1450    } else if (k == BuiltinType::LongDouble) {
1451      Lo = X87;
1452      Hi = X87Up;
1453    }
1454    // FIXME: _Decimal32 and _Decimal64 are SSE.
1455    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
1456    return;
1457  }
1458
1459  if (const EnumType *ET = Ty->getAs<EnumType>()) {
1460    // Classify the underlying integer type.
1461    classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
1462    return;
1463  }
1464
1465  if (Ty->hasPointerRepresentation()) {
1466    Current = Integer;
1467    return;
1468  }
1469
1470  if (Ty->isMemberPointerType()) {
1471    if (Ty->isMemberFunctionPointerType() && Has64BitPointers)
1472      Lo = Hi = Integer;
1473    else
1474      Current = Integer;
1475    return;
1476  }
1477
1478  if (const VectorType *VT = Ty->getAs<VectorType>()) {
1479    uint64_t Size = getContext().getTypeSize(VT);
1480    if (Size == 32) {
1481      // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1482      // float> as integer.
1483      Current = Integer;
1484
1485      // If this type crosses an eightbyte boundary, it should be
1486      // split.
1487      uint64_t EB_Real = (OffsetBase) / 64;
1488      uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1489      if (EB_Real != EB_Imag)
1490        Hi = Lo;
1491    } else if (Size == 64) {
1492      // gcc passes <1 x double> in memory. :(
1493      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1494        return;
1495
1496      // gcc passes <1 x long long> as INTEGER.
1497      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
1498          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
1499          VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
1500          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
1501        Current = Integer;
1502      else
1503        Current = SSE;
1504
1505      // If this type crosses an eightbyte boundary, it should be
1506      // split.
1507      if (OffsetBase && OffsetBase != 64)
1508        Hi = Lo;
1509    } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) {
1510      // Arguments of 256-bits are split into four eightbyte chunks. The
1511      // least significant one belongs to class SSE and all the others to class
1512      // SSEUP. The original Lo and Hi design considers that types can't be
1513      // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
1514      // This design isn't correct for 256-bits, but since there're no cases
1515      // where the upper parts would need to be inspected, avoid adding
1516      // complexity and just consider Hi to match the 64-256 part.
1517      //
1518      // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
1519      // registers if they are "named", i.e. not part of the "..." of a
1520      // variadic function.
1521      Lo = SSE;
1522      Hi = SSEUp;
1523    }
1524    return;
1525  }
1526
1527  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
1528    QualType ET = getContext().getCanonicalType(CT->getElementType());
1529
1530    uint64_t Size = getContext().getTypeSize(Ty);
1531    if (ET->isIntegralOrEnumerationType()) {
1532      if (Size <= 64)
1533        Current = Integer;
1534      else if (Size <= 128)
1535        Lo = Hi = Integer;
1536    } else if (ET == getContext().FloatTy)
1537      Current = SSE;
1538    else if (ET == getContext().DoubleTy ||
1539             (ET == getContext().LongDoubleTy &&
1540              getTarget().getTriple().getOS() == llvm::Triple::NaCl))
1541      Lo = Hi = SSE;
1542    else if (ET == getContext().LongDoubleTy)
1543      Current = ComplexX87;
1544
1545    // If this complex type crosses an eightbyte boundary then it
1546    // should be split.
1547    uint64_t EB_Real = (OffsetBase) / 64;
1548    uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
1549    if (Hi == NoClass && EB_Real != EB_Imag)
1550      Hi = Lo;
1551
1552    return;
1553  }
1554
1555  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
1556    // Arrays are treated like structures.
1557
1558    uint64_t Size = getContext().getTypeSize(Ty);
1559
1560    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1561    // than four eightbytes, ..., it has class MEMORY.
1562    if (Size > 256)
1563      return;
1564
1565    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1566    // fields, it has class MEMORY.
1567    //
1568    // Only need to check alignment of array base.
1569    if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
1570      return;
1571
1572    // Otherwise implement simplified merge. We could be smarter about
1573    // this, but it isn't worth it and would be harder to verify.
1574    Current = NoClass;
1575    uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
1576    uint64_t ArraySize = AT->getSize().getZExtValue();
1577
1578    // The only case a 256-bit wide vector could be used is when the array
1579    // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1580    // to work for sizes wider than 128, early check and fallback to memory.
1581    if (Size > 128 && EltSize != 256)
1582      return;
1583
1584    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1585      Class FieldLo, FieldHi;
1586      classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
1587      Lo = merge(Lo, FieldLo);
1588      Hi = merge(Hi, FieldHi);
1589      if (Lo == Memory || Hi == Memory)
1590        break;
1591    }
1592
1593    postMerge(Size, Lo, Hi);
1594    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
1595    return;
1596  }
1597
1598  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1599    uint64_t Size = getContext().getTypeSize(Ty);
1600
1601    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
1602    // than four eightbytes, ..., it has class MEMORY.
1603    if (Size > 256)
1604      return;
1605
1606    // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
1607    // copy constructor or a non-trivial destructor, it is passed by invisible
1608    // reference.
1609    if (getRecordArgABI(RT, CGT))
1610      return;
1611
1612    const RecordDecl *RD = RT->getDecl();
1613
1614    // Assume variable sized types are passed in memory.
1615    if (RD->hasFlexibleArrayMember())
1616      return;
1617
1618    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1619
1620    // Reset Lo class, this will be recomputed.
1621    Current = NoClass;
1622
1623    // If this is a C++ record, classify the bases first.
1624    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1625      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1626             e = CXXRD->bases_end(); i != e; ++i) {
1627        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1628               "Unexpected base class!");
1629        const CXXRecordDecl *Base =
1630          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1631
1632        // Classify this field.
1633        //
1634        // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1635        // single eightbyte, each is classified separately. Each eightbyte gets
1636        // initialized to class NO_CLASS.
1637        Class FieldLo, FieldHi;
1638        uint64_t Offset =
1639          OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
1640        classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
1641        Lo = merge(Lo, FieldLo);
1642        Hi = merge(Hi, FieldHi);
1643        if (Lo == Memory || Hi == Memory)
1644          break;
1645      }
1646    }
1647
1648    // Classify the fields one at a time, merging the results.
1649    unsigned idx = 0;
1650    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1651           i != e; ++i, ++idx) {
1652      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1653      bool BitField = i->isBitField();
1654
1655      // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1656      // four eightbytes, or it contains unaligned fields, it has class MEMORY.
1657      //
1658      // The only case a 256-bit wide vector could be used is when the struct
1659      // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1660      // to work for sizes wider than 128, early check and fallback to memory.
1661      //
1662      if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1663        Lo = Memory;
1664        return;
1665      }
1666      // Note, skip this test for bit-fields, see below.
1667      if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
1668        Lo = Memory;
1669        return;
1670      }
1671
1672      // Classify this field.
1673      //
1674      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1675      // exceeds a single eightbyte, each is classified
1676      // separately. Each eightbyte gets initialized to class
1677      // NO_CLASS.
1678      Class FieldLo, FieldHi;
1679
1680      // Bit-fields require special handling, they do not force the
1681      // structure to be passed in memory even if unaligned, and
1682      // therefore they can straddle an eightbyte.
1683      if (BitField) {
1684        // Ignore padding bit-fields.
1685        if (i->isUnnamedBitfield())
1686          continue;
1687
1688        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1689        uint64_t Size = i->getBitWidthValue(getContext());
1690
1691        uint64_t EB_Lo = Offset / 64;
1692        uint64_t EB_Hi = (Offset + Size - 1) / 64;
1693        FieldLo = FieldHi = NoClass;
1694        if (EB_Lo) {
1695          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1696          FieldLo = NoClass;
1697          FieldHi = Integer;
1698        } else {
1699          FieldLo = Integer;
1700          FieldHi = EB_Hi ? Integer : NoClass;
1701        }
1702      } else
1703        classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
1704      Lo = merge(Lo, FieldLo);
1705      Hi = merge(Hi, FieldHi);
1706      if (Lo == Memory || Hi == Memory)
1707        break;
1708    }
1709
1710    postMerge(Size, Lo, Hi);
1711  }
1712}
1713
1714ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
1715  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1716  // place naturally.
1717  if (!isAggregateTypeForABI(Ty)) {
1718    // Treat an enum type as its underlying type.
1719    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1720      Ty = EnumTy->getDecl()->getIntegerType();
1721
1722    return (Ty->isPromotableIntegerType() ?
1723            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1724  }
1725
1726  return ABIArgInfo::getIndirect(0);
1727}
1728
1729bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1730  if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1731    uint64_t Size = getContext().getTypeSize(VecTy);
1732    unsigned LargestVector = HasAVX ? 256 : 128;
1733    if (Size <= 64 || Size > LargestVector)
1734      return true;
1735  }
1736
1737  return false;
1738}
1739
1740ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1741                                            unsigned freeIntRegs) const {
1742  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1743  // place naturally.
1744  //
1745  // This assumption is optimistic, as there could be free registers available
1746  // when we need to pass this argument in memory, and LLVM could try to pass
1747  // the argument in the free register. This does not seem to happen currently,
1748  // but this code would be much safer if we could mark the argument with
1749  // 'onstack'. See PR12193.
1750  if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
1751    // Treat an enum type as its underlying type.
1752    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1753      Ty = EnumTy->getDecl()->getIntegerType();
1754
1755    return (Ty->isPromotableIntegerType() ?
1756            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1757  }
1758
1759  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
1760    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
1761
1762  // Compute the byval alignment. We specify the alignment of the byval in all
1763  // cases so that the mid-level optimizer knows the alignment of the byval.
1764  unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1765
1766  // Attempt to avoid passing indirect results using byval when possible. This
1767  // is important for good codegen.
1768  //
1769  // We do this by coercing the value into a scalar type which the backend can
1770  // handle naturally (i.e., without using byval).
1771  //
1772  // For simplicity, we currently only do this when we have exhausted all of the
1773  // free integer registers. Doing this when there are free integer registers
1774  // would require more care, as we would have to ensure that the coerced value
1775  // did not claim the unused register. That would require either reording the
1776  // arguments to the function (so that any subsequent inreg values came first),
1777  // or only doing this optimization when there were no following arguments that
1778  // might be inreg.
1779  //
1780  // We currently expect it to be rare (particularly in well written code) for
1781  // arguments to be passed on the stack when there are still free integer
1782  // registers available (this would typically imply large structs being passed
1783  // by value), so this seems like a fair tradeoff for now.
1784  //
1785  // We can revisit this if the backend grows support for 'onstack' parameter
1786  // attributes. See PR12193.
1787  if (freeIntRegs == 0) {
1788    uint64_t Size = getContext().getTypeSize(Ty);
1789
1790    // If this type fits in an eightbyte, coerce it into the matching integral
1791    // type, which will end up on the stack (with alignment 8).
1792    if (Align == 8 && Size <= 64)
1793      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1794                                                          Size));
1795  }
1796
1797  return ABIArgInfo::getIndirect(Align);
1798}
1799
1800/// GetByteVectorType - The ABI specifies that a value should be passed in an
1801/// full vector XMM/YMM register.  Pick an LLVM IR type that will be passed as a
1802/// vector register.
1803llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
1804  llvm::Type *IRType = CGT.ConvertType(Ty);
1805
1806  // Wrapper structs that just contain vectors are passed just like vectors,
1807  // strip them off if present.
1808  llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
1809  while (STy && STy->getNumElements() == 1) {
1810    IRType = STy->getElementType(0);
1811    STy = dyn_cast<llvm::StructType>(IRType);
1812  }
1813
1814  // If the preferred type is a 16-byte vector, prefer to pass it.
1815  if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
1816    llvm::Type *EltTy = VT->getElementType();
1817    unsigned BitWidth = VT->getBitWidth();
1818    if ((BitWidth >= 128 && BitWidth <= 256) &&
1819        (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
1820         EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
1821         EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
1822         EltTy->isIntegerTy(128)))
1823      return VT;
1824  }
1825
1826  return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
1827}
1828
1829/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1830/// is known to either be off the end of the specified type or being in
1831/// alignment padding.  The user type specified is known to be at most 128 bits
1832/// in size, and have passed through X86_64ABIInfo::classify with a successful
1833/// classification that put one of the two halves in the INTEGER class.
1834///
1835/// It is conservatively correct to return false.
1836static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1837                                  unsigned EndBit, ASTContext &Context) {
1838  // If the bytes being queried are off the end of the type, there is no user
1839  // data hiding here.  This handles analysis of builtins, vectors and other
1840  // types that don't contain interesting padding.
1841  unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1842  if (TySize <= StartBit)
1843    return true;
1844
1845  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1846    unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1847    unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1848
1849    // Check each element to see if the element overlaps with the queried range.
1850    for (unsigned i = 0; i != NumElts; ++i) {
1851      // If the element is after the span we care about, then we're done..
1852      unsigned EltOffset = i*EltSize;
1853      if (EltOffset >= EndBit) break;
1854
1855      unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1856      if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1857                                 EndBit-EltOffset, Context))
1858        return false;
1859    }
1860    // If it overlaps no elements, then it is safe to process as padding.
1861    return true;
1862  }
1863
1864  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1865    const RecordDecl *RD = RT->getDecl();
1866    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1867
1868    // If this is a C++ record, check the bases first.
1869    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1870      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1871           e = CXXRD->bases_end(); i != e; ++i) {
1872        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1873               "Unexpected base class!");
1874        const CXXRecordDecl *Base =
1875          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1876
1877        // If the base is after the span we care about, ignore it.
1878        unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
1879        if (BaseOffset >= EndBit) continue;
1880
1881        unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1882        if (!BitsContainNoUserData(i->getType(), BaseStart,
1883                                   EndBit-BaseOffset, Context))
1884          return false;
1885      }
1886    }
1887
1888    // Verify that no field has data that overlaps the region of interest.  Yes
1889    // this could be sped up a lot by being smarter about queried fields,
1890    // however we're only looking at structs up to 16 bytes, so we don't care
1891    // much.
1892    unsigned idx = 0;
1893    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1894         i != e; ++i, ++idx) {
1895      unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
1896
1897      // If we found a field after the region we care about, then we're done.
1898      if (FieldOffset >= EndBit) break;
1899
1900      unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1901      if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1902                                 Context))
1903        return false;
1904    }
1905
1906    // If nothing in this record overlapped the area of interest, then we're
1907    // clean.
1908    return true;
1909  }
1910
1911  return false;
1912}
1913
1914/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
1915/// float member at the specified offset.  For example, {int,{float}} has a
1916/// float at offset 4.  It is conservatively correct for this routine to return
1917/// false.
1918static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
1919                                  const llvm::DataLayout &TD) {
1920  // Base case if we find a float.
1921  if (IROffset == 0 && IRType->isFloatTy())
1922    return true;
1923
1924  // If this is a struct, recurse into the field at the specified offset.
1925  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1926    const llvm::StructLayout *SL = TD.getStructLayout(STy);
1927    unsigned Elt = SL->getElementContainingOffset(IROffset);
1928    IROffset -= SL->getElementOffset(Elt);
1929    return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
1930  }
1931
1932  // If this is an array, recurse into the field at the specified offset.
1933  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
1934    llvm::Type *EltTy = ATy->getElementType();
1935    unsigned EltSize = TD.getTypeAllocSize(EltTy);
1936    IROffset -= IROffset/EltSize*EltSize;
1937    return ContainsFloatAtOffset(EltTy, IROffset, TD);
1938  }
1939
1940  return false;
1941}
1942
1943
1944/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1945/// low 8 bytes of an XMM register, corresponding to the SSE class.
1946llvm::Type *X86_64ABIInfo::
1947GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
1948                   QualType SourceTy, unsigned SourceOffset) const {
1949  // The only three choices we have are either double, <2 x float>, or float. We
1950  // pass as float if the last 4 bytes is just padding.  This happens for
1951  // structs that contain 3 floats.
1952  if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1953                            SourceOffset*8+64, getContext()))
1954    return llvm::Type::getFloatTy(getVMContext());
1955
1956  // We want to pass as <2 x float> if the LLVM IR type contains a float at
1957  // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
1958  // case.
1959  if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
1960      ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
1961    return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
1962
1963  return llvm::Type::getDoubleTy(getVMContext());
1964}
1965
1966
1967/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
1968/// an 8-byte GPR.  This means that we either have a scalar or we are talking
1969/// about the high or low part of an up-to-16-byte struct.  This routine picks
1970/// the best LLVM IR type to represent this, which may be i64 or may be anything
1971/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1972/// etc).
1973///
1974/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1975/// the source type.  IROffset is an offset in bytes into the LLVM IR type that
1976/// the 8-byte value references.  PrefType may be null.
1977///
1978/// SourceTy is the source level type for the entire argument.  SourceOffset is
1979/// an offset into this that we're processing (which is always either 0 or 8).
1980///
1981llvm::Type *X86_64ABIInfo::
1982GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
1983                       QualType SourceTy, unsigned SourceOffset) const {
1984  // If we're dealing with an un-offset LLVM IR type, then it means that we're
1985  // returning an 8-byte unit starting with it.  See if we can safely use it.
1986  if (IROffset == 0) {
1987    // Pointers and int64's always fill the 8-byte unit.
1988    if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
1989        IRType->isIntegerTy(64))
1990      return IRType;
1991
1992    // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1993    // goodness in the source type is just tail padding.  This is allowed to
1994    // kick in for struct {double,int} on the int, but not on
1995    // struct{double,int,int} because we wouldn't return the second int.  We
1996    // have to do this analysis on the source type because we can't depend on
1997    // unions being lowered a specific way etc.
1998    if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1999        IRType->isIntegerTy(32) ||
2000        (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2001      unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2002          cast<llvm::IntegerType>(IRType)->getBitWidth();
2003
2004      if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2005                                SourceOffset*8+64, getContext()))
2006        return IRType;
2007    }
2008  }
2009
2010  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
2011    // If this is a struct, recurse into the field at the specified offset.
2012    const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
2013    if (IROffset < SL->getSizeInBytes()) {
2014      unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2015      IROffset -= SL->getElementOffset(FieldIdx);
2016
2017      return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2018                                    SourceTy, SourceOffset);
2019    }
2020  }
2021
2022  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2023    llvm::Type *EltTy = ATy->getElementType();
2024    unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
2025    unsigned EltOffset = IROffset/EltSize*EltSize;
2026    return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2027                                  SourceOffset);
2028  }
2029
2030  // Okay, we don't have any better idea of what to pass, so we pass this in an
2031  // integer register that isn't too big to fit the rest of the struct.
2032  unsigned TySizeInBytes =
2033    (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
2034
2035  assert(TySizeInBytes != SourceOffset && "Empty field?");
2036
2037  // It is always safe to classify this as an integer type up to i64 that
2038  // isn't larger than the structure.
2039  return llvm::IntegerType::get(getVMContext(),
2040                                std::min(TySizeInBytes-SourceOffset, 8U)*8);
2041}
2042
2043
2044/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2045/// be used as elements of a two register pair to pass or return, return a
2046/// first class aggregate to represent them.  For example, if the low part of
2047/// a by-value argument should be passed as i32* and the high part as float,
2048/// return {i32*, float}.
2049static llvm::Type *
2050GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
2051                           const llvm::DataLayout &TD) {
2052  // In order to correctly satisfy the ABI, we need to the high part to start
2053  // at offset 8.  If the high and low parts we inferred are both 4-byte types
2054  // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2055  // the second element at offset 8.  Check for this:
2056  unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2057  unsigned HiAlign = TD.getABITypeAlignment(Hi);
2058  unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign);
2059  assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
2060
2061  // To handle this, we have to increase the size of the low part so that the
2062  // second element will start at an 8 byte offset.  We can't increase the size
2063  // of the second element because it might make us access off the end of the
2064  // struct.
2065  if (HiStart != 8) {
2066    // There are only two sorts of types the ABI generation code can produce for
2067    // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
2068    // Promote these to a larger type.
2069    if (Lo->isFloatTy())
2070      Lo = llvm::Type::getDoubleTy(Lo->getContext());
2071    else {
2072      assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
2073      Lo = llvm::Type::getInt64Ty(Lo->getContext());
2074    }
2075  }
2076
2077  llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
2078
2079
2080  // Verify that the second element is at an 8-byte offset.
2081  assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2082         "Invalid x86-64 argument pair!");
2083  return Result;
2084}
2085
2086ABIArgInfo X86_64ABIInfo::
2087classifyReturnType(QualType RetTy) const {
2088  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2089  // classification algorithm.
2090  X86_64ABIInfo::Class Lo, Hi;
2091  classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
2092
2093  // Check some invariants.
2094  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2095  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2096
2097  llvm::Type *ResType = 0;
2098  switch (Lo) {
2099  case NoClass:
2100    if (Hi == NoClass)
2101      return ABIArgInfo::getIgnore();
2102    // If the low part is just padding, it takes no register, leave ResType
2103    // null.
2104    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2105           "Unknown missing lo part");
2106    break;
2107
2108  case SSEUp:
2109  case X87Up:
2110    llvm_unreachable("Invalid classification for lo word.");
2111
2112    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2113    // hidden argument.
2114  case Memory:
2115    return getIndirectReturnResult(RetTy);
2116
2117    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2118    // available register of the sequence %rax, %rdx is used.
2119  case Integer:
2120    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
2121
2122    // If we have a sign or zero extended integer, make sure to return Extend
2123    // so that the parameter gets the right LLVM IR attributes.
2124    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2125      // Treat an enum type as its underlying type.
2126      if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2127        RetTy = EnumTy->getDecl()->getIntegerType();
2128
2129      if (RetTy->isIntegralOrEnumerationType() &&
2130          RetTy->isPromotableIntegerType())
2131        return ABIArgInfo::getExtend();
2132    }
2133    break;
2134
2135    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2136    // available SSE register of the sequence %xmm0, %xmm1 is used.
2137  case SSE:
2138    ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
2139    break;
2140
2141    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2142    // returned on the X87 stack in %st0 as 80-bit x87 number.
2143  case X87:
2144    ResType = llvm::Type::getX86_FP80Ty(getVMContext());
2145    break;
2146
2147    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2148    // part of the value is returned in %st0 and the imaginary part in
2149    // %st1.
2150  case ComplexX87:
2151    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
2152    ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
2153                                    llvm::Type::getX86_FP80Ty(getVMContext()),
2154                                    NULL);
2155    break;
2156  }
2157
2158  llvm::Type *HighPart = 0;
2159  switch (Hi) {
2160    // Memory was handled previously and X87 should
2161    // never occur as a hi class.
2162  case Memory:
2163  case X87:
2164    llvm_unreachable("Invalid classification for hi word.");
2165
2166  case ComplexX87: // Previously handled.
2167  case NoClass:
2168    break;
2169
2170  case Integer:
2171    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2172    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
2173      return ABIArgInfo::getDirect(HighPart, 8);
2174    break;
2175  case SSE:
2176    HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2177    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
2178      return ABIArgInfo::getDirect(HighPart, 8);
2179    break;
2180
2181    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
2182    // is passed in the next available eightbyte chunk if the last used
2183    // vector register.
2184    //
2185    // SSEUP should always be preceded by SSE, just widen.
2186  case SSEUp:
2187    assert(Lo == SSE && "Unexpected SSEUp classification.");
2188    ResType = GetByteVectorType(RetTy);
2189    break;
2190
2191    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2192    // returned together with the previous X87 value in %st0.
2193  case X87Up:
2194    // If X87Up is preceded by X87, we don't need to do
2195    // anything. However, in some cases with unions it may not be
2196    // preceded by X87. In such situations we follow gcc and pass the
2197    // extra bits in an SSE reg.
2198    if (Lo != X87) {
2199      HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2200      if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
2201        return ABIArgInfo::getDirect(HighPart, 8);
2202    }
2203    break;
2204  }
2205
2206  // If a high part was specified, merge it together with the low part.  It is
2207  // known to pass in the high eightbyte of the result.  We do this by forming a
2208  // first class struct aggregate with the high and low part: {low, high}
2209  if (HighPart)
2210    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
2211
2212  return ABIArgInfo::getDirect(ResType);
2213}
2214
2215ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2216  QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2217  bool isNamedArg)
2218  const
2219{
2220  X86_64ABIInfo::Class Lo, Hi;
2221  classify(Ty, 0, Lo, Hi, isNamedArg);
2222
2223  // Check some invariants.
2224  // FIXME: Enforce these by construction.
2225  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2226  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2227
2228  neededInt = 0;
2229  neededSSE = 0;
2230  llvm::Type *ResType = 0;
2231  switch (Lo) {
2232  case NoClass:
2233    if (Hi == NoClass)
2234      return ABIArgInfo::getIgnore();
2235    // If the low part is just padding, it takes no register, leave ResType
2236    // null.
2237    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2238           "Unknown missing lo part");
2239    break;
2240
2241    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2242    // on the stack.
2243  case Memory:
2244
2245    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2246    // COMPLEX_X87, it is passed in memory.
2247  case X87:
2248  case ComplexX87:
2249    if (getRecordArgABI(Ty, CGT) == CGCXXABI::RAA_Indirect)
2250      ++neededInt;
2251    return getIndirectResult(Ty, freeIntRegs);
2252
2253  case SSEUp:
2254  case X87Up:
2255    llvm_unreachable("Invalid classification for lo word.");
2256
2257    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2258    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2259    // and %r9 is used.
2260  case Integer:
2261    ++neededInt;
2262
2263    // Pick an 8-byte type based on the preferred type.
2264    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
2265
2266    // If we have a sign or zero extended integer, make sure to return Extend
2267    // so that the parameter gets the right LLVM IR attributes.
2268    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2269      // Treat an enum type as its underlying type.
2270      if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2271        Ty = EnumTy->getDecl()->getIntegerType();
2272
2273      if (Ty->isIntegralOrEnumerationType() &&
2274          Ty->isPromotableIntegerType())
2275        return ABIArgInfo::getExtend();
2276    }
2277
2278    break;
2279
2280    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2281    // available SSE register is used, the registers are taken in the
2282    // order from %xmm0 to %xmm7.
2283  case SSE: {
2284    llvm::Type *IRType = CGT.ConvertType(Ty);
2285    ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
2286    ++neededSSE;
2287    break;
2288  }
2289  }
2290
2291  llvm::Type *HighPart = 0;
2292  switch (Hi) {
2293    // Memory was handled previously, ComplexX87 and X87 should
2294    // never occur as hi classes, and X87Up must be preceded by X87,
2295    // which is passed in memory.
2296  case Memory:
2297  case X87:
2298  case ComplexX87:
2299    llvm_unreachable("Invalid classification for hi word.");
2300
2301  case NoClass: break;
2302
2303  case Integer:
2304    ++neededInt;
2305    // Pick an 8-byte type based on the preferred type.
2306    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2307
2308    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2309      return ABIArgInfo::getDirect(HighPart, 8);
2310    break;
2311
2312    // X87Up generally doesn't occur here (long double is passed in
2313    // memory), except in situations involving unions.
2314  case X87Up:
2315  case SSE:
2316    HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2317
2318    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2319      return ABIArgInfo::getDirect(HighPart, 8);
2320
2321    ++neededSSE;
2322    break;
2323
2324    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2325    // eightbyte is passed in the upper half of the last used SSE
2326    // register.  This only happens when 128-bit vectors are passed.
2327  case SSEUp:
2328    assert(Lo == SSE && "Unexpected SSEUp classification");
2329    ResType = GetByteVectorType(Ty);
2330    break;
2331  }
2332
2333  // If a high part was specified, merge it together with the low part.  It is
2334  // known to pass in the high eightbyte of the result.  We do this by forming a
2335  // first class struct aggregate with the high and low part: {low, high}
2336  if (HighPart)
2337    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
2338
2339  return ABIArgInfo::getDirect(ResType);
2340}
2341
2342void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2343
2344  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2345
2346  // Keep track of the number of assigned registers.
2347  unsigned freeIntRegs = 6, freeSSERegs = 8;
2348
2349  // If the return value is indirect, then the hidden argument is consuming one
2350  // integer register.
2351  if (FI.getReturnInfo().isIndirect())
2352    --freeIntRegs;
2353
2354  bool isVariadic = FI.isVariadic();
2355  unsigned numRequiredArgs = 0;
2356  if (isVariadic)
2357    numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs();
2358
2359  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2360  // get assigned (in left-to-right order) for passing as follows...
2361  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2362       it != ie; ++it) {
2363    bool isNamedArg = true;
2364    if (isVariadic)
2365      isNamedArg = (it - FI.arg_begin()) <
2366                    static_cast<signed>(numRequiredArgs);
2367
2368    unsigned neededInt, neededSSE;
2369    it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2370                                    neededSSE, isNamedArg);
2371
2372    // AMD64-ABI 3.2.3p3: If there are no registers available for any
2373    // eightbyte of an argument, the whole argument is passed on the
2374    // stack. If registers have already been assigned for some
2375    // eightbytes of such an argument, the assignments get reverted.
2376    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
2377      freeIntRegs -= neededInt;
2378      freeSSERegs -= neededSSE;
2379    } else {
2380      it->info = getIndirectResult(it->type, freeIntRegs);
2381    }
2382  }
2383}
2384
2385static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2386                                        QualType Ty,
2387                                        CodeGenFunction &CGF) {
2388  llvm::Value *overflow_arg_area_p =
2389    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2390  llvm::Value *overflow_arg_area =
2391    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2392
2393  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2394  // byte boundary if alignment needed by type exceeds 8 byte boundary.
2395  // It isn't stated explicitly in the standard, but in practice we use
2396  // alignment greater than 16 where necessary.
2397  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2398  if (Align > 8) {
2399    // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
2400    llvm::Value *Offset =
2401      llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
2402    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2403    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
2404                                                    CGF.Int64Ty);
2405    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
2406    overflow_arg_area =
2407      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2408                                 overflow_arg_area->getType(),
2409                                 "overflow_arg_area.align");
2410  }
2411
2412  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
2413  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2414  llvm::Value *Res =
2415    CGF.Builder.CreateBitCast(overflow_arg_area,
2416                              llvm::PointerType::getUnqual(LTy));
2417
2418  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2419  // l->overflow_arg_area + sizeof(type).
2420  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2421  // an 8 byte boundary.
2422
2423  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
2424  llvm::Value *Offset =
2425      llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
2426  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2427                                            "overflow_arg_area.next");
2428  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2429
2430  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2431  return Res;
2432}
2433
2434llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2435                                      CodeGenFunction &CGF) const {
2436  // Assume that va_list type is correct; should be pointer to LLVM type:
2437  // struct {
2438  //   i32 gp_offset;
2439  //   i32 fp_offset;
2440  //   i8* overflow_arg_area;
2441  //   i8* reg_save_area;
2442  // };
2443  unsigned neededInt, neededSSE;
2444
2445  Ty = CGF.getContext().getCanonicalType(Ty);
2446  ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
2447                                       /*isNamedArg*/false);
2448
2449  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2450  // in the registers. If not go to step 7.
2451  if (!neededInt && !neededSSE)
2452    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2453
2454  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2455  // general purpose registers needed to pass type and num_fp to hold
2456  // the number of floating point registers needed.
2457
2458  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2459  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2460  // l->fp_offset > 304 - num_fp * 16 go to step 7.
2461  //
2462  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2463  // register save space).
2464
2465  llvm::Value *InRegs = 0;
2466  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2467  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2468  if (neededInt) {
2469    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2470    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
2471    InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
2472    InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
2473  }
2474
2475  if (neededSSE) {
2476    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2477    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2478    llvm::Value *FitsInFP =
2479      llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
2480    FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
2481    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2482  }
2483
2484  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2485  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2486  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2487  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2488
2489  // Emit code to load the value if it was passed in registers.
2490
2491  CGF.EmitBlock(InRegBlock);
2492
2493  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2494  // an offset of l->gp_offset and/or l->fp_offset. This may require
2495  // copying to a temporary location in case the parameter is passed
2496  // in different register classes or requires an alignment greater
2497  // than 8 for general purpose registers and 16 for XMM registers.
2498  //
2499  // FIXME: This really results in shameful code when we end up needing to
2500  // collect arguments from different places; often what should result in a
2501  // simple assembling of a structure from scattered addresses has many more
2502  // loads than necessary. Can we clean this up?
2503  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2504  llvm::Value *RegAddr =
2505    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2506                           "reg_save_area");
2507  if (neededInt && neededSSE) {
2508    // FIXME: Cleanup.
2509    assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
2510    llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2511    llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2512    Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
2513    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
2514    llvm::Type *TyLo = ST->getElementType(0);
2515    llvm::Type *TyHi = ST->getElementType(1);
2516    assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
2517           "Unexpected ABI info for mixed regs");
2518    llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
2519    llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
2520    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2521    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2522    llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2523    llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
2524    llvm::Value *V =
2525      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2526    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2527    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2528    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2529
2530    RegAddr = CGF.Builder.CreateBitCast(Tmp,
2531                                        llvm::PointerType::getUnqual(LTy));
2532  } else if (neededInt) {
2533    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2534    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2535                                        llvm::PointerType::getUnqual(LTy));
2536
2537    // Copy to a temporary if necessary to ensure the appropriate alignment.
2538    std::pair<CharUnits, CharUnits> SizeAlign =
2539        CGF.getContext().getTypeInfoInChars(Ty);
2540    uint64_t TySize = SizeAlign.first.getQuantity();
2541    unsigned TyAlign = SizeAlign.second.getQuantity();
2542    if (TyAlign > 8) {
2543      llvm::Value *Tmp = CGF.CreateMemTemp(Ty);
2544      CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false);
2545      RegAddr = Tmp;
2546    }
2547  } else if (neededSSE == 1) {
2548    RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2549    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2550                                        llvm::PointerType::getUnqual(LTy));
2551  } else {
2552    assert(neededSSE == 2 && "Invalid number of needed registers!");
2553    // SSE registers are spaced 16 bytes apart in the register save
2554    // area, we need to collect the two eightbytes together.
2555    llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2556    llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
2557    llvm::Type *DoubleTy = CGF.DoubleTy;
2558    llvm::Type *DblPtrTy =
2559      llvm::PointerType::getUnqual(DoubleTy);
2560    llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL);
2561    llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty);
2562    Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo());
2563    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2564                                                         DblPtrTy));
2565    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2566    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2567                                                         DblPtrTy));
2568    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2569    RegAddr = CGF.Builder.CreateBitCast(Tmp,
2570                                        llvm::PointerType::getUnqual(LTy));
2571  }
2572
2573  // AMD64-ABI 3.5.7p5: Step 5. Set:
2574  // l->gp_offset = l->gp_offset + num_gp * 8
2575  // l->fp_offset = l->fp_offset + num_fp * 16.
2576  if (neededInt) {
2577    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
2578    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2579                            gp_offset_p);
2580  }
2581  if (neededSSE) {
2582    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
2583    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2584                            fp_offset_p);
2585  }
2586  CGF.EmitBranch(ContBlock);
2587
2588  // Emit code to load the value if it was passed in memory.
2589
2590  CGF.EmitBlock(InMemBlock);
2591  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2592
2593  // Return the appropriate result.
2594
2595  CGF.EmitBlock(ContBlock);
2596  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
2597                                                 "vaarg.addr");
2598  ResAddr->addIncoming(RegAddr, InRegBlock);
2599  ResAddr->addIncoming(MemAddr, InMemBlock);
2600  return ResAddr;
2601}
2602
2603ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const {
2604
2605  if (Ty->isVoidType())
2606    return ABIArgInfo::getIgnore();
2607
2608  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2609    Ty = EnumTy->getDecl()->getIntegerType();
2610
2611  uint64_t Size = getContext().getTypeSize(Ty);
2612
2613  if (const RecordType *RT = Ty->getAs<RecordType>()) {
2614    if (IsReturnType) {
2615      if (isRecordReturnIndirect(RT, CGT))
2616        return ABIArgInfo::getIndirect(0, false);
2617    } else {
2618      if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, CGT))
2619        return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2620    }
2621
2622    if (RT->getDecl()->hasFlexibleArrayMember())
2623      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2624
2625    // FIXME: mingw-w64-gcc emits 128-bit struct as i128
2626    if (Size == 128 && getTarget().getTriple().getOS() == llvm::Triple::MinGW32)
2627      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2628                                                          Size));
2629
2630    // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
2631    // not 1, 2, 4, or 8 bytes, must be passed by reference."
2632    if (Size <= 64 &&
2633        (Size & (Size - 1)) == 0)
2634      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2635                                                          Size));
2636
2637    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2638  }
2639
2640  if (Ty->isPromotableIntegerType())
2641    return ABIArgInfo::getExtend();
2642
2643  return ABIArgInfo::getDirect();
2644}
2645
2646void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2647
2648  QualType RetTy = FI.getReturnType();
2649  FI.getReturnInfo() = classify(RetTy, true);
2650
2651  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2652       it != ie; ++it)
2653    it->info = classify(it->type, false);
2654}
2655
2656llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2657                                      CodeGenFunction &CGF) const {
2658  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2659
2660  CGBuilderTy &Builder = CGF.Builder;
2661  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2662                                                       "ap");
2663  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2664  llvm::Type *PTy =
2665    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2666  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2667
2668  uint64_t Offset =
2669    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2670  llvm::Value *NextAddr =
2671    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2672                      "ap.next");
2673  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2674
2675  return AddrTyped;
2676}
2677
2678namespace {
2679
2680class NaClX86_64ABIInfo : public ABIInfo {
2681 public:
2682  NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2683      : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {}
2684  virtual void computeInfo(CGFunctionInfo &FI) const;
2685  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2686                                 CodeGenFunction &CGF) const;
2687 private:
2688  PNaClABIInfo PInfo;  // Used for generating calls with pnaclcall callingconv.
2689  X86_64ABIInfo NInfo; // Used for everything else.
2690};
2691
2692class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo  {
2693 public:
2694  NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
2695      : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {}
2696};
2697
2698}
2699
2700void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2701  if (FI.getASTCallingConvention() == CC_PnaclCall)
2702    PInfo.computeInfo(FI);
2703  else
2704    NInfo.computeInfo(FI);
2705}
2706
2707llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2708                                          CodeGenFunction &CGF) const {
2709  // Always use the native convention; calling pnacl-style varargs functions
2710  // is unuspported.
2711  return NInfo.EmitVAArg(VAListAddr, Ty, CGF);
2712}
2713
2714
2715// PowerPC-32
2716
2717namespace {
2718class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2719public:
2720  PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2721
2722  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2723    // This is recovered from gcc output.
2724    return 1; // r1 is the dedicated stack pointer
2725  }
2726
2727  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2728                               llvm::Value *Address) const;
2729};
2730
2731}
2732
2733bool
2734PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2735                                                llvm::Value *Address) const {
2736  // This is calculated from the LLVM and GCC tables and verified
2737  // against gcc output.  AFAIK all ABIs use the same encoding.
2738
2739  CodeGen::CGBuilderTy &Builder = CGF.Builder;
2740
2741  llvm::IntegerType *i8 = CGF.Int8Ty;
2742  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2743  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2744  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2745
2746  // 0-31: r0-31, the 4-byte general-purpose registers
2747  AssignToArrayRange(Builder, Address, Four8, 0, 31);
2748
2749  // 32-63: fp0-31, the 8-byte floating-point registers
2750  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2751
2752  // 64-76 are various 4-byte special-purpose registers:
2753  // 64: mq
2754  // 65: lr
2755  // 66: ctr
2756  // 67: ap
2757  // 68-75 cr0-7
2758  // 76: xer
2759  AssignToArrayRange(Builder, Address, Four8, 64, 76);
2760
2761  // 77-108: v0-31, the 16-byte vector registers
2762  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2763
2764  // 109: vrsave
2765  // 110: vscr
2766  // 111: spe_acc
2767  // 112: spefscr
2768  // 113: sfp
2769  AssignToArrayRange(Builder, Address, Four8, 109, 113);
2770
2771  return false;
2772}
2773
2774// PowerPC-64
2775
2776namespace {
2777/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
2778class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
2779
2780public:
2781  PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
2782
2783  bool isPromotableTypeForABI(QualType Ty) const;
2784
2785  ABIArgInfo classifyReturnType(QualType RetTy) const;
2786  ABIArgInfo classifyArgumentType(QualType Ty) const;
2787
2788  // TODO: We can add more logic to computeInfo to improve performance.
2789  // Example: For aggregate arguments that fit in a register, we could
2790  // use getDirectInReg (as is done below for structs containing a single
2791  // floating-point value) to avoid pushing them to memory on function
2792  // entry.  This would require changing the logic in PPCISelLowering
2793  // when lowering the parameters in the caller and args in the callee.
2794  virtual void computeInfo(CGFunctionInfo &FI) const {
2795    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2796    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2797         it != ie; ++it) {
2798      // We rely on the default argument classification for the most part.
2799      // One exception:  An aggregate containing a single floating-point
2800      // or vector item must be passed in a register if one is available.
2801      const Type *T = isSingleElementStruct(it->type, getContext());
2802      if (T) {
2803        const BuiltinType *BT = T->getAs<BuiltinType>();
2804        if (T->isVectorType() || (BT && BT->isFloatingPoint())) {
2805          QualType QT(T, 0);
2806          it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
2807          continue;
2808        }
2809      }
2810      it->info = classifyArgumentType(it->type);
2811    }
2812  }
2813
2814  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr,
2815                                 QualType Ty,
2816                                 CodeGenFunction &CGF) const;
2817};
2818
2819class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
2820public:
2821  PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT)
2822    : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {}
2823
2824  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2825    // This is recovered from gcc output.
2826    return 1; // r1 is the dedicated stack pointer
2827  }
2828
2829  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2830                               llvm::Value *Address) const;
2831};
2832
2833class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2834public:
2835  PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
2836
2837  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2838    // This is recovered from gcc output.
2839    return 1; // r1 is the dedicated stack pointer
2840  }
2841
2842  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2843                               llvm::Value *Address) const;
2844};
2845
2846}
2847
2848// Return true if the ABI requires Ty to be passed sign- or zero-
2849// extended to 64 bits.
2850bool
2851PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
2852  // Treat an enum type as its underlying type.
2853  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2854    Ty = EnumTy->getDecl()->getIntegerType();
2855
2856  // Promotable integer types are required to be promoted by the ABI.
2857  if (Ty->isPromotableIntegerType())
2858    return true;
2859
2860  // In addition to the usual promotable integer types, we also need to
2861  // extend all 32-bit types, since the ABI requires promotion to 64 bits.
2862  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2863    switch (BT->getKind()) {
2864    case BuiltinType::Int:
2865    case BuiltinType::UInt:
2866      return true;
2867    default:
2868      break;
2869    }
2870
2871  return false;
2872}
2873
2874ABIArgInfo
2875PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
2876  if (Ty->isAnyComplexType())
2877    return ABIArgInfo::getDirect();
2878
2879  if (isAggregateTypeForABI(Ty)) {
2880    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
2881      return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
2882
2883    return ABIArgInfo::getIndirect(0);
2884  }
2885
2886  return (isPromotableTypeForABI(Ty) ?
2887          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2888}
2889
2890ABIArgInfo
2891PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
2892  if (RetTy->isVoidType())
2893    return ABIArgInfo::getIgnore();
2894
2895  if (RetTy->isAnyComplexType())
2896    return ABIArgInfo::getDirect();
2897
2898  if (isAggregateTypeForABI(RetTy))
2899    return ABIArgInfo::getIndirect(0);
2900
2901  return (isPromotableTypeForABI(RetTy) ?
2902          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2903}
2904
2905// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
2906llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr,
2907                                           QualType Ty,
2908                                           CodeGenFunction &CGF) const {
2909  llvm::Type *BP = CGF.Int8PtrTy;
2910  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2911
2912  CGBuilderTy &Builder = CGF.Builder;
2913  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2914  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2915
2916  // Update the va_list pointer.  The pointer should be bumped by the
2917  // size of the object.  We can trust getTypeSize() except for a complex
2918  // type whose base type is smaller than a doubleword.  For these, the
2919  // size of the object is 16 bytes; see below for further explanation.
2920  unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8;
2921  QualType BaseTy;
2922  unsigned CplxBaseSize = 0;
2923
2924  if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
2925    BaseTy = CTy->getElementType();
2926    CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8;
2927    if (CplxBaseSize < 8)
2928      SizeInBytes = 16;
2929  }
2930
2931  unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8);
2932  llvm::Value *NextAddr =
2933    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset),
2934                      "ap.next");
2935  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2936
2937  // If we have a complex type and the base type is smaller than 8 bytes,
2938  // the ABI calls for the real and imaginary parts to be right-adjusted
2939  // in separate doublewords.  However, Clang expects us to produce a
2940  // pointer to a structure with the two parts packed tightly.  So generate
2941  // loads of the real and imaginary parts relative to the va_list pointer,
2942  // and store them to a temporary structure.
2943  if (CplxBaseSize && CplxBaseSize < 8) {
2944    llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2945    llvm::Value *ImagAddr = RealAddr;
2946    RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize));
2947    ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize));
2948    llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy));
2949    RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy);
2950    ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy);
2951    llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal");
2952    llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag");
2953    llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty),
2954                                            "vacplx");
2955    llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real");
2956    llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag");
2957    Builder.CreateStore(Real, RealPtr, false);
2958    Builder.CreateStore(Imag, ImagPtr, false);
2959    return Ptr;
2960  }
2961
2962  // If the argument is smaller than 8 bytes, it is right-adjusted in
2963  // its doubleword slot.  Adjust the pointer to pick it up from the
2964  // correct offset.
2965  if (SizeInBytes < 8) {
2966    llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty);
2967    AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes));
2968    Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2969  }
2970
2971  llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2972  return Builder.CreateBitCast(Addr, PTy);
2973}
2974
2975static bool
2976PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2977                              llvm::Value *Address) {
2978  // This is calculated from the LLVM and GCC tables and verified
2979  // against gcc output.  AFAIK all ABIs use the same encoding.
2980
2981  CodeGen::CGBuilderTy &Builder = CGF.Builder;
2982
2983  llvm::IntegerType *i8 = CGF.Int8Ty;
2984  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2985  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2986  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2987
2988  // 0-31: r0-31, the 8-byte general-purpose registers
2989  AssignToArrayRange(Builder, Address, Eight8, 0, 31);
2990
2991  // 32-63: fp0-31, the 8-byte floating-point registers
2992  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2993
2994  // 64-76 are various 4-byte special-purpose registers:
2995  // 64: mq
2996  // 65: lr
2997  // 66: ctr
2998  // 67: ap
2999  // 68-75 cr0-7
3000  // 76: xer
3001  AssignToArrayRange(Builder, Address, Four8, 64, 76);
3002
3003  // 77-108: v0-31, the 16-byte vector registers
3004  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3005
3006  // 109: vrsave
3007  // 110: vscr
3008  // 111: spe_acc
3009  // 112: spefscr
3010  // 113: sfp
3011  AssignToArrayRange(Builder, Address, Four8, 109, 113);
3012
3013  return false;
3014}
3015
3016bool
3017PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
3018  CodeGen::CodeGenFunction &CGF,
3019  llvm::Value *Address) const {
3020
3021  return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3022}
3023
3024bool
3025PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3026                                                llvm::Value *Address) const {
3027
3028  return PPC64_initDwarfEHRegSizeTable(CGF, Address);
3029}
3030
3031//===----------------------------------------------------------------------===//
3032// ARM ABI Implementation
3033//===----------------------------------------------------------------------===//
3034
3035namespace {
3036
3037class ARMABIInfo : public ABIInfo {
3038public:
3039  enum ABIKind {
3040    APCS = 0,
3041    AAPCS = 1,
3042    AAPCS_VFP
3043  };
3044
3045private:
3046  ABIKind Kind;
3047
3048public:
3049  ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
3050    setRuntimeCC();
3051  }
3052
3053  bool isEABI() const {
3054    StringRef Env = getTarget().getTriple().getEnvironmentName();
3055    return (Env == "gnueabi" || Env == "eabi" ||
3056            Env == "android" || Env == "androideabi");
3057  }
3058
3059private:
3060  ABIKind getABIKind() const { return Kind; }
3061
3062  ABIArgInfo classifyReturnType(QualType RetTy) const;
3063  ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs,
3064                                  unsigned &AllocatedVFP,
3065                                  bool &IsHA) const;
3066  bool isIllegalVectorType(QualType Ty) const;
3067
3068  virtual void computeInfo(CGFunctionInfo &FI) const;
3069
3070  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3071                                 CodeGenFunction &CGF) const;
3072
3073  llvm::CallingConv::ID getLLVMDefaultCC() const;
3074  llvm::CallingConv::ID getABIDefaultCC() const;
3075  void setRuntimeCC();
3076};
3077
3078class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
3079public:
3080  ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
3081    :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
3082
3083  const ARMABIInfo &getABIInfo() const {
3084    return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
3085  }
3086
3087  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3088    return 13;
3089  }
3090
3091  StringRef getARCRetainAutoreleasedReturnValueMarker() const {
3092    return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
3093  }
3094
3095  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3096                               llvm::Value *Address) const {
3097    llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
3098
3099    // 0-15 are the 16 integer registers.
3100    AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
3101    return false;
3102  }
3103
3104  unsigned getSizeOfUnwindException() const {
3105    if (getABIInfo().isEABI()) return 88;
3106    return TargetCodeGenInfo::getSizeOfUnwindException();
3107  }
3108};
3109
3110}
3111
3112void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3113  // To correctly handle Homogeneous Aggregate, we need to keep track of the
3114  // VFP registers allocated so far.
3115  // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3116  // VFP registers of the appropriate type unallocated then the argument is
3117  // allocated to the lowest-numbered sequence of such registers.
3118  // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3119  // unallocated are marked as unavailable.
3120  unsigned AllocatedVFP = 0;
3121  int VFPRegs[16] = { 0 };
3122  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3123  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3124       it != ie; ++it) {
3125    unsigned PreAllocation = AllocatedVFP;
3126    bool IsHA = false;
3127    // 6.1.2.3 There is one VFP co-processor register class using registers
3128    // s0-s15 (d0-d7) for passing arguments.
3129    const unsigned NumVFPs = 16;
3130    it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA);
3131    // If we do not have enough VFP registers for the HA, any VFP registers
3132    // that are unallocated are marked as unavailable. To achieve this, we add
3133    // padding of (NumVFPs - PreAllocation) floats.
3134    if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) {
3135      llvm::Type *PaddingTy = llvm::ArrayType::get(
3136          llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation);
3137      it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy);
3138    }
3139  }
3140
3141  // Always honor user-specified calling convention.
3142  if (FI.getCallingConvention() != llvm::CallingConv::C)
3143    return;
3144
3145  llvm::CallingConv::ID cc = getRuntimeCC();
3146  if (cc != llvm::CallingConv::C)
3147    FI.setEffectiveCallingConvention(cc);
3148}
3149
3150/// Return the default calling convention that LLVM will use.
3151llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
3152  // The default calling convention that LLVM will infer.
3153  if (getTarget().getTriple().getEnvironmentName()=="gnueabihf")
3154    return llvm::CallingConv::ARM_AAPCS_VFP;
3155  else if (isEABI())
3156    return llvm::CallingConv::ARM_AAPCS;
3157  else
3158    return llvm::CallingConv::ARM_APCS;
3159}
3160
3161/// Return the calling convention that our ABI would like us to use
3162/// as the C calling convention.
3163llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
3164  switch (getABIKind()) {
3165  case APCS: return llvm::CallingConv::ARM_APCS;
3166  case AAPCS: return llvm::CallingConv::ARM_AAPCS;
3167  case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
3168  }
3169  llvm_unreachable("bad ABI kind");
3170}
3171
3172void ARMABIInfo::setRuntimeCC() {
3173  assert(getRuntimeCC() == llvm::CallingConv::C);
3174
3175  // Don't muddy up the IR with a ton of explicit annotations if
3176  // they'd just match what LLVM will infer from the triple.
3177  llvm::CallingConv::ID abiCC = getABIDefaultCC();
3178  if (abiCC != getLLVMDefaultCC())
3179    RuntimeCC = abiCC;
3180}
3181
3182/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
3183/// aggregate.  If HAMembers is non-null, the number of base elements
3184/// contained in the type is returned through it; this is used for the
3185/// recursive calls that check aggregate component types.
3186static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
3187                                   ASTContext &Context,
3188                                   uint64_t *HAMembers = 0) {
3189  uint64_t Members = 0;
3190  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3191    if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
3192      return false;
3193    Members *= AT->getSize().getZExtValue();
3194  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3195    const RecordDecl *RD = RT->getDecl();
3196    if (RD->hasFlexibleArrayMember())
3197      return false;
3198
3199    Members = 0;
3200    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3201         i != e; ++i) {
3202      const FieldDecl *FD = *i;
3203      uint64_t FldMembers;
3204      if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
3205        return false;
3206
3207      Members = (RD->isUnion() ?
3208                 std::max(Members, FldMembers) : Members + FldMembers);
3209    }
3210  } else {
3211    Members = 1;
3212    if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3213      Members = 2;
3214      Ty = CT->getElementType();
3215    }
3216
3217    // Homogeneous aggregates for AAPCS-VFP must have base types of float,
3218    // double, or 64-bit or 128-bit vectors.
3219    if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3220      if (BT->getKind() != BuiltinType::Float &&
3221          BT->getKind() != BuiltinType::Double &&
3222          BT->getKind() != BuiltinType::LongDouble)
3223        return false;
3224    } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
3225      unsigned VecSize = Context.getTypeSize(VT);
3226      if (VecSize != 64 && VecSize != 128)
3227        return false;
3228    } else {
3229      return false;
3230    }
3231
3232    // The base type must be the same for all members.  Vector types of the
3233    // same total size are treated as being equivalent here.
3234    const Type *TyPtr = Ty.getTypePtr();
3235    if (!Base)
3236      Base = TyPtr;
3237    if (Base != TyPtr &&
3238        (!Base->isVectorType() || !TyPtr->isVectorType() ||
3239         Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
3240      return false;
3241  }
3242
3243  // Homogeneous Aggregates can have at most 4 members of the base type.
3244  if (HAMembers)
3245    *HAMembers = Members;
3246
3247  return (Members > 0 && Members <= 4);
3248}
3249
3250/// markAllocatedVFPs - update VFPRegs according to the alignment and
3251/// number of VFP registers (unit is S register) requested.
3252static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP,
3253                              unsigned Alignment,
3254                              unsigned NumRequired) {
3255  // Early Exit.
3256  if (AllocatedVFP >= 16)
3257    return;
3258  // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive
3259  // VFP registers of the appropriate type unallocated then the argument is
3260  // allocated to the lowest-numbered sequence of such registers.
3261  for (unsigned I = 0; I < 16; I += Alignment) {
3262    bool FoundSlot = true;
3263    for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3264      if (J >= 16 || VFPRegs[J]) {
3265         FoundSlot = false;
3266         break;
3267      }
3268    if (FoundSlot) {
3269      for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++)
3270        VFPRegs[J] = 1;
3271      AllocatedVFP += NumRequired;
3272      return;
3273    }
3274  }
3275  // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are
3276  // unallocated are marked as unavailable.
3277  for (unsigned I = 0; I < 16; I++)
3278    VFPRegs[I] = 1;
3279  AllocatedVFP = 17; // We do not have enough VFP registers.
3280}
3281
3282ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs,
3283                                            unsigned &AllocatedVFP,
3284                                            bool &IsHA) const {
3285  // We update number of allocated VFPs according to
3286  // 6.1.2.1 The following argument types are VFP CPRCs:
3287  //   A single-precision floating-point type (including promoted
3288  //   half-precision types); A double-precision floating-point type;
3289  //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
3290  //   with a Base Type of a single- or double-precision floating-point type,
3291  //   64-bit containerized vectors or 128-bit containerized vectors with one
3292  //   to four Elements.
3293
3294  // Handle illegal vector types here.
3295  if (isIllegalVectorType(Ty)) {
3296    uint64_t Size = getContext().getTypeSize(Ty);
3297    if (Size <= 32) {
3298      llvm::Type *ResType =
3299          llvm::Type::getInt32Ty(getVMContext());
3300      return ABIArgInfo::getDirect(ResType);
3301    }
3302    if (Size == 64) {
3303      llvm::Type *ResType = llvm::VectorType::get(
3304          llvm::Type::getInt32Ty(getVMContext()), 2);
3305      markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
3306      return ABIArgInfo::getDirect(ResType);
3307    }
3308    if (Size == 128) {
3309      llvm::Type *ResType = llvm::VectorType::get(
3310          llvm::Type::getInt32Ty(getVMContext()), 4);
3311      markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4);
3312      return ABIArgInfo::getDirect(ResType);
3313    }
3314    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3315  }
3316  // Update VFPRegs for legal vector types.
3317  if (const VectorType *VT = Ty->getAs<VectorType>()) {
3318    uint64_t Size = getContext().getTypeSize(VT);
3319    // Size of a legal vector should be power of 2 and above 64.
3320    markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32);
3321  }
3322  // Update VFPRegs for floating point types.
3323  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3324    if (BT->getKind() == BuiltinType::Half ||
3325        BT->getKind() == BuiltinType::Float)
3326      markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1);
3327    if (BT->getKind() == BuiltinType::Double ||
3328        BT->getKind() == BuiltinType::LongDouble)
3329      markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2);
3330  }
3331
3332  if (!isAggregateTypeForABI(Ty)) {
3333    // Treat an enum type as its underlying type.
3334    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3335      Ty = EnumTy->getDecl()->getIntegerType();
3336
3337    return (Ty->isPromotableIntegerType() ?
3338            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3339  }
3340
3341  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
3342    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3343
3344  // Ignore empty records.
3345  if (isEmptyRecord(getContext(), Ty, true))
3346    return ABIArgInfo::getIgnore();
3347
3348  if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
3349    // Homogeneous Aggregates need to be expanded when we can fit the aggregate
3350    // into VFP registers.
3351    const Type *Base = 0;
3352    uint64_t Members = 0;
3353    if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) {
3354      assert(Base && "Base class should be set for homogeneous aggregate");
3355      // Base can be a floating-point or a vector.
3356      if (Base->isVectorType()) {
3357        // ElementSize is in number of floats.
3358        unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4;
3359        markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize,
3360                          Members * ElementSize);
3361      } else if (Base->isSpecificBuiltinType(BuiltinType::Float))
3362        markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members);
3363      else {
3364        assert(Base->isSpecificBuiltinType(BuiltinType::Double) ||
3365               Base->isSpecificBuiltinType(BuiltinType::LongDouble));
3366        markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2);
3367      }
3368      IsHA = true;
3369      return ABIArgInfo::getExpand();
3370    }
3371  }
3372
3373  // Support byval for ARM.
3374  // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
3375  // most 8-byte. We realign the indirect argument if type alignment is bigger
3376  // than ABI alignment.
3377  uint64_t ABIAlign = 4;
3378  uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
3379  if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3380      getABIKind() == ARMABIInfo::AAPCS)
3381    ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3382  if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
3383    return ABIArgInfo::getIndirect(0, /*ByVal=*/true,
3384           /*Realign=*/TyAlign > ABIAlign);
3385  }
3386
3387  // Otherwise, pass by coercing to a structure of the appropriate size.
3388  llvm::Type* ElemTy;
3389  unsigned SizeRegs;
3390  // FIXME: Try to match the types of the arguments more accurately where
3391  // we can.
3392  if (getContext().getTypeAlign(Ty) <= 32) {
3393    ElemTy = llvm::Type::getInt32Ty(getVMContext());
3394    SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
3395  } else {
3396    ElemTy = llvm::Type::getInt64Ty(getVMContext());
3397    SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
3398  }
3399
3400  llvm::Type *STy =
3401    llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
3402  return ABIArgInfo::getDirect(STy);
3403}
3404
3405static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
3406                              llvm::LLVMContext &VMContext) {
3407  // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
3408  // is called integer-like if its size is less than or equal to one word, and
3409  // the offset of each of its addressable sub-fields is zero.
3410
3411  uint64_t Size = Context.getTypeSize(Ty);
3412
3413  // Check that the type fits in a word.
3414  if (Size > 32)
3415    return false;
3416
3417  // FIXME: Handle vector types!
3418  if (Ty->isVectorType())
3419    return false;
3420
3421  // Float types are never treated as "integer like".
3422  if (Ty->isRealFloatingType())
3423    return false;
3424
3425  // If this is a builtin or pointer type then it is ok.
3426  if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
3427    return true;
3428
3429  // Small complex integer types are "integer like".
3430  if (const ComplexType *CT = Ty->getAs<ComplexType>())
3431    return isIntegerLikeType(CT->getElementType(), Context, VMContext);
3432
3433  // Single element and zero sized arrays should be allowed, by the definition
3434  // above, but they are not.
3435
3436  // Otherwise, it must be a record type.
3437  const RecordType *RT = Ty->getAs<RecordType>();
3438  if (!RT) return false;
3439
3440  // Ignore records with flexible arrays.
3441  const RecordDecl *RD = RT->getDecl();
3442  if (RD->hasFlexibleArrayMember())
3443    return false;
3444
3445  // Check that all sub-fields are at offset 0, and are themselves "integer
3446  // like".
3447  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3448
3449  bool HadField = false;
3450  unsigned idx = 0;
3451  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3452       i != e; ++i, ++idx) {
3453    const FieldDecl *FD = *i;
3454
3455    // Bit-fields are not addressable, we only need to verify they are "integer
3456    // like". We still have to disallow a subsequent non-bitfield, for example:
3457    //   struct { int : 0; int x }
3458    // is non-integer like according to gcc.
3459    if (FD->isBitField()) {
3460      if (!RD->isUnion())
3461        HadField = true;
3462
3463      if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3464        return false;
3465
3466      continue;
3467    }
3468
3469    // Check if this field is at offset 0.
3470    if (Layout.getFieldOffset(idx) != 0)
3471      return false;
3472
3473    if (!isIntegerLikeType(FD->getType(), Context, VMContext))
3474      return false;
3475
3476    // Only allow at most one field in a structure. This doesn't match the
3477    // wording above, but follows gcc in situations with a field following an
3478    // empty structure.
3479    if (!RD->isUnion()) {
3480      if (HadField)
3481        return false;
3482
3483      HadField = true;
3484    }
3485  }
3486
3487  return true;
3488}
3489
3490ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
3491  if (RetTy->isVoidType())
3492    return ABIArgInfo::getIgnore();
3493
3494  // Large vector types should be returned via memory.
3495  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
3496    return ABIArgInfo::getIndirect(0);
3497
3498  if (!isAggregateTypeForABI(RetTy)) {
3499    // Treat an enum type as its underlying type.
3500    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3501      RetTy = EnumTy->getDecl()->getIntegerType();
3502
3503    return (RetTy->isPromotableIntegerType() ?
3504            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3505  }
3506
3507  // Structures with either a non-trivial destructor or a non-trivial
3508  // copy constructor are always indirect.
3509  if (isRecordReturnIndirect(RetTy, CGT))
3510    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3511
3512  // Are we following APCS?
3513  if (getABIKind() == APCS) {
3514    if (isEmptyRecord(getContext(), RetTy, false))
3515      return ABIArgInfo::getIgnore();
3516
3517    // Complex types are all returned as packed integers.
3518    //
3519    // FIXME: Consider using 2 x vector types if the back end handles them
3520    // correctly.
3521    if (RetTy->isAnyComplexType())
3522      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
3523                                              getContext().getTypeSize(RetTy)));
3524
3525    // Integer like structures are returned in r0.
3526    if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
3527      // Return in the smallest viable integer type.
3528      uint64_t Size = getContext().getTypeSize(RetTy);
3529      if (Size <= 8)
3530        return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3531      if (Size <= 16)
3532        return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3533      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3534    }
3535
3536    // Otherwise return in memory.
3537    return ABIArgInfo::getIndirect(0);
3538  }
3539
3540  // Otherwise this is an AAPCS variant.
3541
3542  if (isEmptyRecord(getContext(), RetTy, true))
3543    return ABIArgInfo::getIgnore();
3544
3545  // Check for homogeneous aggregates with AAPCS-VFP.
3546  if (getABIKind() == AAPCS_VFP) {
3547    const Type *Base = 0;
3548    if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3549      assert(Base && "Base class should be set for homogeneous aggregate");
3550      // Homogeneous Aggregates are returned directly.
3551      return ABIArgInfo::getDirect();
3552    }
3553  }
3554
3555  // Aggregates <= 4 bytes are returned in r0; other aggregates
3556  // are returned indirectly.
3557  uint64_t Size = getContext().getTypeSize(RetTy);
3558  if (Size <= 32) {
3559    // Return in the smallest viable integer type.
3560    if (Size <= 8)
3561      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
3562    if (Size <= 16)
3563      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3564    return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
3565  }
3566
3567  return ABIArgInfo::getIndirect(0);
3568}
3569
3570/// isIllegalVector - check whether Ty is an illegal vector type.
3571bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
3572  if (const VectorType *VT = Ty->getAs<VectorType>()) {
3573    // Check whether VT is legal.
3574    unsigned NumElements = VT->getNumElements();
3575    uint64_t Size = getContext().getTypeSize(VT);
3576    // NumElements should be power of 2.
3577    if ((NumElements & (NumElements - 1)) != 0)
3578      return true;
3579    // Size should be greater than 32 bits.
3580    return Size <= 32;
3581  }
3582  return false;
3583}
3584
3585llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3586                                   CodeGenFunction &CGF) const {
3587  llvm::Type *BP = CGF.Int8PtrTy;
3588  llvm::Type *BPP = CGF.Int8PtrPtrTy;
3589
3590  CGBuilderTy &Builder = CGF.Builder;
3591  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3592  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3593
3594  if (isEmptyRecord(getContext(), Ty, true)) {
3595    // These are ignored for parameter passing purposes.
3596    llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3597    return Builder.CreateBitCast(Addr, PTy);
3598  }
3599
3600  uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8;
3601  uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
3602  bool IsIndirect = false;
3603
3604  // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
3605  // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
3606  if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
3607      getABIKind() == ARMABIInfo::AAPCS)
3608    TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
3609  else
3610    TyAlign = 4;
3611  // Use indirect if size of the illegal vector is bigger than 16 bytes.
3612  if (isIllegalVectorType(Ty) && Size > 16) {
3613    IsIndirect = true;
3614    Size = 4;
3615    TyAlign = 4;
3616  }
3617
3618  // Handle address alignment for ABI alignment > 4 bytes.
3619  if (TyAlign > 4) {
3620    assert((TyAlign & (TyAlign - 1)) == 0 &&
3621           "Alignment is not power of 2!");
3622    llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3623    AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3624    AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
3625    Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align");
3626  }
3627
3628  uint64_t Offset =
3629    llvm::RoundUpToAlignment(Size, 4);
3630  llvm::Value *NextAddr =
3631    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3632                      "ap.next");
3633  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3634
3635  if (IsIndirect)
3636    Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP));
3637  else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) {
3638    // We can't directly cast ap.cur to pointer to a vector type, since ap.cur
3639    // may not be correctly aligned for the vector type. We create an aligned
3640    // temporary space and copy the content over from ap.cur to the temporary
3641    // space. This is necessary if the natural alignment of the type is greater
3642    // than the ABI alignment.
3643    llvm::Type *I8PtrTy = Builder.getInt8PtrTy();
3644    CharUnits CharSize = getContext().getTypeSizeInChars(Ty);
3645    llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty),
3646                                                    "var.align");
3647    llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy);
3648    llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy);
3649    Builder.CreateMemCpy(Dst, Src,
3650        llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()),
3651        TyAlign, false);
3652    Addr = AlignedTemp; //The content is in aligned location.
3653  }
3654  llvm::Type *PTy =
3655    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3656  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3657
3658  return AddrTyped;
3659}
3660
3661namespace {
3662
3663class NaClARMABIInfo : public ABIInfo {
3664 public:
3665  NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3666      : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {}
3667  virtual void computeInfo(CGFunctionInfo &FI) const;
3668  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3669                                 CodeGenFunction &CGF) const;
3670 private:
3671  PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv.
3672  ARMABIInfo NInfo; // Used for everything else.
3673};
3674
3675class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo  {
3676 public:
3677  NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind)
3678      : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {}
3679};
3680
3681}
3682
3683void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
3684  if (FI.getASTCallingConvention() == CC_PnaclCall)
3685    PInfo.computeInfo(FI);
3686  else
3687    static_cast<const ABIInfo&>(NInfo).computeInfo(FI);
3688}
3689
3690llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3691                                       CodeGenFunction &CGF) const {
3692  // Always use the native convention; calling pnacl-style varargs functions
3693  // is unsupported.
3694  return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF);
3695}
3696
3697//===----------------------------------------------------------------------===//
3698// AArch64 ABI Implementation
3699//===----------------------------------------------------------------------===//
3700
3701namespace {
3702
3703class AArch64ABIInfo : public ABIInfo {
3704public:
3705  AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3706
3707private:
3708  // The AArch64 PCS is explicit about return types and argument types being
3709  // handled identically, so we don't need to draw a distinction between
3710  // Argument and Return classification.
3711  ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs,
3712                                 int &FreeVFPRegs) const;
3713
3714  ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt,
3715                        llvm::Type *DirectTy = 0) const;
3716
3717  virtual void computeInfo(CGFunctionInfo &FI) const;
3718
3719  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3720                                 CodeGenFunction &CGF) const;
3721};
3722
3723class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
3724public:
3725  AArch64TargetCodeGenInfo(CodeGenTypes &CGT)
3726    :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {}
3727
3728  const AArch64ABIInfo &getABIInfo() const {
3729    return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
3730  }
3731
3732  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
3733    return 31;
3734  }
3735
3736  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3737                               llvm::Value *Address) const {
3738    // 0-31 are x0-x30 and sp: 8 bytes each
3739    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
3740    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31);
3741
3742    // 64-95 are v0-v31: 16 bytes each
3743    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
3744    AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95);
3745
3746    return false;
3747  }
3748
3749};
3750
3751}
3752
3753void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3754  int FreeIntRegs = 8, FreeVFPRegs = 8;
3755
3756  FI.getReturnInfo() = classifyGenericType(FI.getReturnType(),
3757                                           FreeIntRegs, FreeVFPRegs);
3758
3759  FreeIntRegs = FreeVFPRegs = 8;
3760  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3761       it != ie; ++it) {
3762    it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs);
3763
3764  }
3765}
3766
3767ABIArgInfo
3768AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded,
3769                           bool IsInt, llvm::Type *DirectTy) const {
3770  if (FreeRegs >= RegsNeeded) {
3771    FreeRegs -= RegsNeeded;
3772    return ABIArgInfo::getDirect(DirectTy);
3773  }
3774
3775  llvm::Type *Padding = 0;
3776
3777  // We need padding so that later arguments don't get filled in anyway. That
3778  // wouldn't happen if only ByVal arguments followed in the same category, but
3779  // a large structure will simply seem to be a pointer as far as LLVM is
3780  // concerned.
3781  if (FreeRegs > 0) {
3782    if (IsInt)
3783      Padding = llvm::Type::getInt64Ty(getVMContext());
3784    else
3785      Padding = llvm::Type::getFloatTy(getVMContext());
3786
3787    // Either [N x i64] or [N x float].
3788    Padding = llvm::ArrayType::get(Padding, FreeRegs);
3789    FreeRegs = 0;
3790  }
3791
3792  return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8,
3793                                 /*IsByVal=*/ true, /*Realign=*/ false,
3794                                 Padding);
3795}
3796
3797
3798ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty,
3799                                               int &FreeIntRegs,
3800                                               int &FreeVFPRegs) const {
3801  // Can only occurs for return, but harmless otherwise.
3802  if (Ty->isVoidType())
3803    return ABIArgInfo::getIgnore();
3804
3805  // Large vector types should be returned via memory. There's no such concept
3806  // in the ABI, but they'd be over 16 bytes anyway so no matter how they're
3807  // classified they'd go into memory (see B.3).
3808  if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) {
3809    if (FreeIntRegs > 0)
3810      --FreeIntRegs;
3811    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3812  }
3813
3814  // All non-aggregate LLVM types have a concrete ABI representation so they can
3815  // be passed directly. After this block we're guaranteed to be in a
3816  // complicated case.
3817  if (!isAggregateTypeForABI(Ty)) {
3818    // Treat an enum type as its underlying type.
3819    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3820      Ty = EnumTy->getDecl()->getIntegerType();
3821
3822    if (Ty->isFloatingType() || Ty->isVectorType())
3823      return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false);
3824
3825    assert(getContext().getTypeSize(Ty) <= 128 &&
3826           "unexpectedly large scalar type");
3827
3828    int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1;
3829
3830    // If the type may need padding registers to ensure "alignment", we must be
3831    // careful when this is accounted for. Increasing the effective size covers
3832    // all cases.
3833    if (getContext().getTypeAlign(Ty) == 128)
3834      RegsNeeded += FreeIntRegs % 2 != 0;
3835
3836    return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true);
3837  }
3838
3839  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
3840    if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect)
3841      --FreeIntRegs;
3842    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
3843  }
3844
3845  if (isEmptyRecord(getContext(), Ty, true)) {
3846    if (!getContext().getLangOpts().CPlusPlus) {
3847      // Empty structs outside C++ mode are a GNU extension, so no ABI can
3848      // possibly tell us what to do. It turns out (I believe) that GCC ignores
3849      // the object for parameter-passsing purposes.
3850      return ABIArgInfo::getIgnore();
3851    }
3852
3853    // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode
3854    // description of va_arg in the PCS require that an empty struct does
3855    // actually occupy space for parameter-passing. I'm hoping for a
3856    // clarification giving an explicit paragraph to point to in future.
3857    return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true,
3858                      llvm::Type::getInt8Ty(getVMContext()));
3859  }
3860
3861  // Homogeneous vector aggregates get passed in registers or on the stack.
3862  const Type *Base = 0;
3863  uint64_t NumMembers = 0;
3864  if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) {
3865    assert(Base && "Base class should be set for homogeneous aggregate");
3866    // Homogeneous aggregates are passed and returned directly.
3867    return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers,
3868                      /*IsInt=*/ false);
3869  }
3870
3871  uint64_t Size = getContext().getTypeSize(Ty);
3872  if (Size <= 128) {
3873    // Small structs can use the same direct type whether they're in registers
3874    // or on the stack.
3875    llvm::Type *BaseTy;
3876    unsigned NumBases;
3877    int SizeInRegs = (Size + 63) / 64;
3878
3879    if (getContext().getTypeAlign(Ty) == 128) {
3880      BaseTy = llvm::Type::getIntNTy(getVMContext(), 128);
3881      NumBases = 1;
3882
3883      // If the type may need padding registers to ensure "alignment", we must
3884      // be careful when this is accounted for. Increasing the effective size
3885      // covers all cases.
3886      SizeInRegs += FreeIntRegs % 2 != 0;
3887    } else {
3888      BaseTy = llvm::Type::getInt64Ty(getVMContext());
3889      NumBases = SizeInRegs;
3890    }
3891    llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases);
3892
3893    return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs,
3894                      /*IsInt=*/ true, DirectTy);
3895  }
3896
3897  // If the aggregate is > 16 bytes, it's passed and returned indirectly. In
3898  // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere.
3899  --FreeIntRegs;
3900  return ABIArgInfo::getIndirect(0, /* byVal = */ false);
3901}
3902
3903llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3904                                       CodeGenFunction &CGF) const {
3905  // The AArch64 va_list type and handling is specified in the Procedure Call
3906  // Standard, section B.4:
3907  //
3908  // struct {
3909  //   void *__stack;
3910  //   void *__gr_top;
3911  //   void *__vr_top;
3912  //   int __gr_offs;
3913  //   int __vr_offs;
3914  // };
3915
3916  assert(!CGF.CGM.getDataLayout().isBigEndian()
3917         && "va_arg not implemented for big-endian AArch64");
3918
3919  int FreeIntRegs = 8, FreeVFPRegs = 8;
3920  Ty = CGF.getContext().getCanonicalType(Ty);
3921  ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs);
3922
3923  llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
3924  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3925  llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
3926  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3927
3928  llvm::Value *reg_offs_p = 0, *reg_offs = 0;
3929  int reg_top_index;
3930  int RegSize;
3931  if (FreeIntRegs < 8) {
3932    assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs");
3933    // 3 is the field number of __gr_offs
3934    reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
3935    reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
3936    reg_top_index = 1; // field number for __gr_top
3937    RegSize = 8 * (8 - FreeIntRegs);
3938  } else {
3939    assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs");
3940    // 4 is the field number of __vr_offs.
3941    reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
3942    reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
3943    reg_top_index = 2; // field number for __vr_top
3944    RegSize = 16 * (8 - FreeVFPRegs);
3945  }
3946
3947  //=======================================
3948  // Find out where argument was passed
3949  //=======================================
3950
3951  // If reg_offs >= 0 we're already using the stack for this type of
3952  // argument. We don't want to keep updating reg_offs (in case it overflows,
3953  // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
3954  // whatever they get).
3955  llvm::Value *UsingStack = 0;
3956  UsingStack = CGF.Builder.CreateICmpSGE(reg_offs,
3957                                         llvm::ConstantInt::get(CGF.Int32Ty, 0));
3958
3959  CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
3960
3961  // Otherwise, at least some kind of argument could go in these registers, the
3962  // quesiton is whether this particular type is too big.
3963  CGF.EmitBlock(MaybeRegBlock);
3964
3965  // Integer arguments may need to correct register alignment (for example a
3966  // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
3967  // align __gr_offs to calculate the potential address.
3968  if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
3969    int Align = getContext().getTypeAlign(Ty) / 8;
3970
3971    reg_offs = CGF.Builder.CreateAdd(reg_offs,
3972                                 llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
3973                                 "align_regoffs");
3974    reg_offs = CGF.Builder.CreateAnd(reg_offs,
3975                                    llvm::ConstantInt::get(CGF.Int32Ty, -Align),
3976                                    "aligned_regoffs");
3977  }
3978
3979  // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
3980  llvm::Value *NewOffset = 0;
3981  NewOffset = CGF.Builder.CreateAdd(reg_offs,
3982                                    llvm::ConstantInt::get(CGF.Int32Ty, RegSize),
3983                                    "new_reg_offs");
3984  CGF.Builder.CreateStore(NewOffset, reg_offs_p);
3985
3986  // Now we're in a position to decide whether this argument really was in
3987  // registers or not.
3988  llvm::Value *InRegs = 0;
3989  InRegs = CGF.Builder.CreateICmpSLE(NewOffset,
3990                                     llvm::ConstantInt::get(CGF.Int32Ty, 0),
3991                                     "inreg");
3992
3993  CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
3994
3995  //=======================================
3996  // Argument was in registers
3997  //=======================================
3998
3999  // Now we emit the code for if the argument was originally passed in
4000  // registers. First start the appropriate block:
4001  CGF.EmitBlock(InRegBlock);
4002
4003  llvm::Value *reg_top_p = 0, *reg_top = 0;
4004  reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
4005  reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4006  llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs);
4007  llvm::Value *RegAddr = 0;
4008  llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4009
4010  if (!AI.isDirect()) {
4011    // If it's been passed indirectly (actually a struct), whatever we find from
4012    // stored registers or on the stack will actually be a struct **.
4013    MemTy = llvm::PointerType::getUnqual(MemTy);
4014  }
4015
4016  const Type *Base = 0;
4017  uint64_t NumMembers;
4018  if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)
4019      && NumMembers > 1) {
4020    // Homogeneous aggregates passed in registers will have their elements split
4021    // and stored 16-bytes apart regardless of size (they're notionally in qN,
4022    // qN+1, ...). We reload and store into a temporary local variable
4023    // contiguously.
4024    assert(AI.isDirect() && "Homogeneous aggregates should be passed directly");
4025    llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4026    llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4027    llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy);
4028
4029    for (unsigned i = 0; i < NumMembers; ++i) {
4030      llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i);
4031      llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset);
4032      LoadAddr = CGF.Builder.CreateBitCast(LoadAddr,
4033                                           llvm::PointerType::getUnqual(BaseTy));
4034      llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i);
4035
4036      llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4037      CGF.Builder.CreateStore(Elem, StoreAddr);
4038    }
4039
4040    RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy);
4041  } else {
4042    // Otherwise the object is contiguous in memory
4043    RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy);
4044  }
4045
4046  CGF.EmitBranch(ContBlock);
4047
4048  //=======================================
4049  // Argument was on the stack
4050  //=======================================
4051  CGF.EmitBlock(OnStackBlock);
4052
4053  llvm::Value *stack_p = 0, *OnStackAddr = 0;
4054  stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
4055  OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack");
4056
4057  // Again, stack arguments may need realigmnent. In this case both integer and
4058  // floating-point ones might be affected.
4059  if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) {
4060    int Align = getContext().getTypeAlign(Ty) / 8;
4061
4062    OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty);
4063
4064    OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr,
4065                                 llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4066                                 "align_stack");
4067    OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr,
4068                                    llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4069                                    "align_stack");
4070
4071    OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy);
4072  }
4073
4074  uint64_t StackSize;
4075  if (AI.isDirect())
4076    StackSize = getContext().getTypeSize(Ty) / 8;
4077  else
4078    StackSize = 8;
4079
4080  // All stack slots are 8 bytes
4081  StackSize = llvm::RoundUpToAlignment(StackSize, 8);
4082
4083  llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize);
4084  llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC,
4085                                                "new_stack");
4086
4087  // Write the new value of __stack for the next call to va_arg
4088  CGF.Builder.CreateStore(NewStack, stack_p);
4089
4090  OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy);
4091
4092  CGF.EmitBranch(ContBlock);
4093
4094  //=======================================
4095  // Tidy up
4096  //=======================================
4097  CGF.EmitBlock(ContBlock);
4098
4099  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr");
4100  ResAddr->addIncoming(RegAddr, InRegBlock);
4101  ResAddr->addIncoming(OnStackAddr, OnStackBlock);
4102
4103  if (AI.isDirect())
4104    return ResAddr;
4105
4106  return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr");
4107}
4108
4109//===----------------------------------------------------------------------===//
4110// NVPTX ABI Implementation
4111//===----------------------------------------------------------------------===//
4112
4113namespace {
4114
4115class NVPTXABIInfo : public ABIInfo {
4116public:
4117  NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4118
4119  ABIArgInfo classifyReturnType(QualType RetTy) const;
4120  ABIArgInfo classifyArgumentType(QualType Ty) const;
4121
4122  virtual void computeInfo(CGFunctionInfo &FI) const;
4123  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4124                                 CodeGenFunction &CFG) const;
4125};
4126
4127class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
4128public:
4129  NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
4130    : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
4131
4132  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4133                                   CodeGen::CodeGenModule &M) const;
4134private:
4135  static void addKernelMetadata(llvm::Function *F);
4136};
4137
4138ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
4139  if (RetTy->isVoidType())
4140    return ABIArgInfo::getIgnore();
4141  if (isAggregateTypeForABI(RetTy))
4142    return ABIArgInfo::getIndirect(0);
4143  return ABIArgInfo::getDirect();
4144}
4145
4146ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
4147  if (isAggregateTypeForABI(Ty))
4148    return ABIArgInfo::getIndirect(0);
4149
4150  return ABIArgInfo::getDirect();
4151}
4152
4153void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
4154  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4155  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4156       it != ie; ++it)
4157    it->info = classifyArgumentType(it->type);
4158
4159  // Always honor user-specified calling convention.
4160  if (FI.getCallingConvention() != llvm::CallingConv::C)
4161    return;
4162
4163  FI.setEffectiveCallingConvention(getRuntimeCC());
4164}
4165
4166llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4167                                     CodeGenFunction &CFG) const {
4168  llvm_unreachable("NVPTX does not support varargs");
4169}
4170
4171void NVPTXTargetCodeGenInfo::
4172SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4173                    CodeGen::CodeGenModule &M) const{
4174  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4175  if (!FD) return;
4176
4177  llvm::Function *F = cast<llvm::Function>(GV);
4178
4179  // Perform special handling in OpenCL mode
4180  if (M.getLangOpts().OpenCL) {
4181    // Use OpenCL function attributes to check for kernel functions
4182    // By default, all functions are device functions
4183    if (FD->hasAttr<OpenCLKernelAttr>()) {
4184      // OpenCL __kernel functions get kernel metadata
4185      addKernelMetadata(F);
4186      // And kernel functions are not subject to inlining
4187      F->addFnAttr(llvm::Attribute::NoInline);
4188    }
4189  }
4190
4191  // Perform special handling in CUDA mode.
4192  if (M.getLangOpts().CUDA) {
4193    // CUDA __global__ functions get a kernel metadata entry.  Since
4194    // __global__ functions cannot be called from the device, we do not
4195    // need to set the noinline attribute.
4196    if (FD->getAttr<CUDAGlobalAttr>())
4197      addKernelMetadata(F);
4198  }
4199}
4200
4201void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) {
4202  llvm::Module *M = F->getParent();
4203  llvm::LLVMContext &Ctx = M->getContext();
4204
4205  // Get "nvvm.annotations" metadata node
4206  llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
4207
4208  // Create !{<func-ref>, metadata !"kernel", i32 1} node
4209  llvm::SmallVector<llvm::Value *, 3> MDVals;
4210  MDVals.push_back(F);
4211  MDVals.push_back(llvm::MDString::get(Ctx, "kernel"));
4212  MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1));
4213
4214  // Append metadata to nvvm.annotations
4215  MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
4216}
4217
4218}
4219
4220//===----------------------------------------------------------------------===//
4221// SystemZ ABI Implementation
4222//===----------------------------------------------------------------------===//
4223
4224namespace {
4225
4226class SystemZABIInfo : public ABIInfo {
4227public:
4228  SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4229
4230  bool isPromotableIntegerType(QualType Ty) const;
4231  bool isCompoundType(QualType Ty) const;
4232  bool isFPArgumentType(QualType Ty) const;
4233
4234  ABIArgInfo classifyReturnType(QualType RetTy) const;
4235  ABIArgInfo classifyArgumentType(QualType ArgTy) const;
4236
4237  virtual void computeInfo(CGFunctionInfo &FI) const {
4238    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4239    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4240         it != ie; ++it)
4241      it->info = classifyArgumentType(it->type);
4242  }
4243
4244  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4245                                 CodeGenFunction &CGF) const;
4246};
4247
4248class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
4249public:
4250  SystemZTargetCodeGenInfo(CodeGenTypes &CGT)
4251    : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {}
4252};
4253
4254}
4255
4256bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
4257  // Treat an enum type as its underlying type.
4258  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4259    Ty = EnumTy->getDecl()->getIntegerType();
4260
4261  // Promotable integer types are required to be promoted by the ABI.
4262  if (Ty->isPromotableIntegerType())
4263    return true;
4264
4265  // 32-bit values must also be promoted.
4266  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4267    switch (BT->getKind()) {
4268    case BuiltinType::Int:
4269    case BuiltinType::UInt:
4270      return true;
4271    default:
4272      return false;
4273    }
4274  return false;
4275}
4276
4277bool SystemZABIInfo::isCompoundType(QualType Ty) const {
4278  return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty);
4279}
4280
4281bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
4282  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4283    switch (BT->getKind()) {
4284    case BuiltinType::Float:
4285    case BuiltinType::Double:
4286      return true;
4287    default:
4288      return false;
4289    }
4290
4291  if (const RecordType *RT = Ty->getAsStructureType()) {
4292    const RecordDecl *RD = RT->getDecl();
4293    bool Found = false;
4294
4295    // If this is a C++ record, check the bases first.
4296    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
4297      for (CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(),
4298             E = CXXRD->bases_end(); I != E; ++I) {
4299        QualType Base = I->getType();
4300
4301        // Empty bases don't affect things either way.
4302        if (isEmptyRecord(getContext(), Base, true))
4303          continue;
4304
4305        if (Found)
4306          return false;
4307        Found = isFPArgumentType(Base);
4308        if (!Found)
4309          return false;
4310      }
4311
4312    // Check the fields.
4313    for (RecordDecl::field_iterator I = RD->field_begin(),
4314           E = RD->field_end(); I != E; ++I) {
4315      const FieldDecl *FD = *I;
4316
4317      // Empty bitfields don't affect things either way.
4318      // Unlike isSingleElementStruct(), empty structure and array fields
4319      // do count.  So do anonymous bitfields that aren't zero-sized.
4320      if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
4321        return true;
4322
4323      // Unlike isSingleElementStruct(), arrays do not count.
4324      // Nested isFPArgumentType structures still do though.
4325      if (Found)
4326        return false;
4327      Found = isFPArgumentType(FD->getType());
4328      if (!Found)
4329        return false;
4330    }
4331
4332    // Unlike isSingleElementStruct(), trailing padding is allowed.
4333    // An 8-byte aligned struct s { float f; } is passed as a double.
4334    return Found;
4335  }
4336
4337  return false;
4338}
4339
4340llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4341                                       CodeGenFunction &CGF) const {
4342  // Assume that va_list type is correct; should be pointer to LLVM type:
4343  // struct {
4344  //   i64 __gpr;
4345  //   i64 __fpr;
4346  //   i8 *__overflow_arg_area;
4347  //   i8 *__reg_save_area;
4348  // };
4349
4350  // Every argument occupies 8 bytes and is passed by preference in either
4351  // GPRs or FPRs.
4352  Ty = CGF.getContext().getCanonicalType(Ty);
4353  ABIArgInfo AI = classifyArgumentType(Ty);
4354  bool InFPRs = isFPArgumentType(Ty);
4355
4356  llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
4357  bool IsIndirect = AI.isIndirect();
4358  unsigned UnpaddedBitSize;
4359  if (IsIndirect) {
4360    APTy = llvm::PointerType::getUnqual(APTy);
4361    UnpaddedBitSize = 64;
4362  } else
4363    UnpaddedBitSize = getContext().getTypeSize(Ty);
4364  unsigned PaddedBitSize = 64;
4365  assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size.");
4366
4367  unsigned PaddedSize = PaddedBitSize / 8;
4368  unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8;
4369
4370  unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding;
4371  if (InFPRs) {
4372    MaxRegs = 4; // Maximum of 4 FPR arguments
4373    RegCountField = 1; // __fpr
4374    RegSaveIndex = 16; // save offset for f0
4375    RegPadding = 0; // floats are passed in the high bits of an FPR
4376  } else {
4377    MaxRegs = 5; // Maximum of 5 GPR arguments
4378    RegCountField = 0; // __gpr
4379    RegSaveIndex = 2; // save offset for r2
4380    RegPadding = Padding; // values are passed in the low bits of a GPR
4381  }
4382
4383  llvm::Value *RegCountPtr =
4384    CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
4385  llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
4386  llvm::Type *IndexTy = RegCount->getType();
4387  llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
4388  llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
4389						  "fits_in_regs");
4390
4391  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4392  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
4393  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4394  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
4395
4396  // Emit code to load the value if it was passed in registers.
4397  CGF.EmitBlock(InRegBlock);
4398
4399  // Work out the address of an argument register.
4400  llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize);
4401  llvm::Value *ScaledRegCount =
4402    CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
4403  llvm::Value *RegBase =
4404    llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding);
4405  llvm::Value *RegOffset =
4406    CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
4407  llvm::Value *RegSaveAreaPtr =
4408    CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
4409  llvm::Value *RegSaveArea =
4410    CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
4411  llvm::Value *RawRegAddr =
4412    CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr");
4413  llvm::Value *RegAddr =
4414    CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr");
4415
4416  // Update the register count
4417  llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
4418  llvm::Value *NewRegCount =
4419    CGF.Builder.CreateAdd(RegCount, One, "reg_count");
4420  CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
4421  CGF.EmitBranch(ContBlock);
4422
4423  // Emit code to load the value if it was passed in memory.
4424  CGF.EmitBlock(InMemBlock);
4425
4426  // Work out the address of a stack argument.
4427  llvm::Value *OverflowArgAreaPtr =
4428    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
4429  llvm::Value *OverflowArgArea =
4430    CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area");
4431  llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding);
4432  llvm::Value *RawMemAddr =
4433    CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr");
4434  llvm::Value *MemAddr =
4435    CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr");
4436
4437  // Update overflow_arg_area_ptr pointer
4438  llvm::Value *NewOverflowArgArea =
4439    CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area");
4440  CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
4441  CGF.EmitBranch(ContBlock);
4442
4443  // Return the appropriate result.
4444  CGF.EmitBlock(ContBlock);
4445  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr");
4446  ResAddr->addIncoming(RegAddr, InRegBlock);
4447  ResAddr->addIncoming(MemAddr, InMemBlock);
4448
4449  if (IsIndirect)
4450    return CGF.Builder.CreateLoad(ResAddr, "indirect_arg");
4451
4452  return ResAddr;
4453}
4454
4455bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
4456    const llvm::Triple &Triple, const CodeGenOptions &Opts) {
4457  assert(Triple.getArch() == llvm::Triple::x86);
4458
4459  switch (Opts.getStructReturnConvention()) {
4460  case CodeGenOptions::SRCK_Default:
4461    break;
4462  case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
4463    return false;
4464  case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
4465    return true;
4466  }
4467
4468  if (Triple.isOSDarwin())
4469    return true;
4470
4471  switch (Triple.getOS()) {
4472  case llvm::Triple::Cygwin:
4473  case llvm::Triple::MinGW32:
4474  case llvm::Triple::AuroraUX:
4475  case llvm::Triple::DragonFly:
4476  case llvm::Triple::FreeBSD:
4477  case llvm::Triple::OpenBSD:
4478  case llvm::Triple::Bitrig:
4479  case llvm::Triple::Win32:
4480    return true;
4481  default:
4482    return false;
4483  }
4484}
4485
4486ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
4487  if (RetTy->isVoidType())
4488    return ABIArgInfo::getIgnore();
4489  if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
4490    return ABIArgInfo::getIndirect(0);
4491  return (isPromotableIntegerType(RetTy) ?
4492          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4493}
4494
4495ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
4496  // Handle the generic C++ ABI.
4497  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
4498    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4499
4500  // Integers and enums are extended to full register width.
4501  if (isPromotableIntegerType(Ty))
4502    return ABIArgInfo::getExtend();
4503
4504  // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
4505  uint64_t Size = getContext().getTypeSize(Ty);
4506  if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
4507    return ABIArgInfo::getIndirect(0);
4508
4509  // Handle small structures.
4510  if (const RecordType *RT = Ty->getAs<RecordType>()) {
4511    // Structures with flexible arrays have variable length, so really
4512    // fail the size test above.
4513    const RecordDecl *RD = RT->getDecl();
4514    if (RD->hasFlexibleArrayMember())
4515      return ABIArgInfo::getIndirect(0);
4516
4517    // The structure is passed as an unextended integer, a float, or a double.
4518    llvm::Type *PassTy;
4519    if (isFPArgumentType(Ty)) {
4520      assert(Size == 32 || Size == 64);
4521      if (Size == 32)
4522        PassTy = llvm::Type::getFloatTy(getVMContext());
4523      else
4524        PassTy = llvm::Type::getDoubleTy(getVMContext());
4525    } else
4526      PassTy = llvm::IntegerType::get(getVMContext(), Size);
4527    return ABIArgInfo::getDirect(PassTy);
4528  }
4529
4530  // Non-structure compounds are passed indirectly.
4531  if (isCompoundType(Ty))
4532    return ABIArgInfo::getIndirect(0);
4533
4534  return ABIArgInfo::getDirect(0);
4535}
4536
4537//===----------------------------------------------------------------------===//
4538// MSP430 ABI Implementation
4539//===----------------------------------------------------------------------===//
4540
4541namespace {
4542
4543class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
4544public:
4545  MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
4546    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
4547  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4548                           CodeGen::CodeGenModule &M) const;
4549};
4550
4551}
4552
4553void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4554                                                  llvm::GlobalValue *GV,
4555                                             CodeGen::CodeGenModule &M) const {
4556  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4557    if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
4558      // Handle 'interrupt' attribute:
4559      llvm::Function *F = cast<llvm::Function>(GV);
4560
4561      // Step 1: Set ISR calling convention.
4562      F->setCallingConv(llvm::CallingConv::MSP430_INTR);
4563
4564      // Step 2: Add attributes goodness.
4565      F->addFnAttr(llvm::Attribute::NoInline);
4566
4567      // Step 3: Emit ISR vector alias.
4568      unsigned Num = attr->getNumber() / 2;
4569      new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
4570                            "__isr_" + Twine(Num),
4571                            GV, &M.getModule());
4572    }
4573  }
4574}
4575
4576//===----------------------------------------------------------------------===//
4577// MIPS ABI Implementation.  This works for both little-endian and
4578// big-endian variants.
4579//===----------------------------------------------------------------------===//
4580
4581namespace {
4582class MipsABIInfo : public ABIInfo {
4583  bool IsO32;
4584  unsigned MinABIStackAlignInBytes, StackAlignInBytes;
4585  void CoerceToIntArgs(uint64_t TySize,
4586                       SmallVectorImpl<llvm::Type *> &ArgList) const;
4587  llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
4588  llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
4589  llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
4590public:
4591  MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
4592    ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
4593    StackAlignInBytes(IsO32 ? 8 : 16) {}
4594
4595  ABIArgInfo classifyReturnType(QualType RetTy) const;
4596  ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
4597  virtual void computeInfo(CGFunctionInfo &FI) const;
4598  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4599                                 CodeGenFunction &CGF) const;
4600};
4601
4602class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
4603  unsigned SizeOfUnwindException;
4604public:
4605  MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
4606    : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
4607      SizeOfUnwindException(IsO32 ? 24 : 32) {}
4608
4609  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4610    return 29;
4611  }
4612
4613  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4614                           CodeGen::CodeGenModule &CGM) const {
4615    const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4616    if (!FD) return;
4617    llvm::Function *Fn = cast<llvm::Function>(GV);
4618    if (FD->hasAttr<Mips16Attr>()) {
4619      Fn->addFnAttr("mips16");
4620    }
4621    else if (FD->hasAttr<NoMips16Attr>()) {
4622      Fn->addFnAttr("nomips16");
4623    }
4624  }
4625
4626  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4627                               llvm::Value *Address) const;
4628
4629  unsigned getSizeOfUnwindException() const {
4630    return SizeOfUnwindException;
4631  }
4632};
4633}
4634
4635void MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
4636                                  SmallVectorImpl<llvm::Type *> &ArgList) const {
4637  llvm::IntegerType *IntTy =
4638    llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4639
4640  // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
4641  for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
4642    ArgList.push_back(IntTy);
4643
4644  // If necessary, add one more integer type to ArgList.
4645  unsigned R = TySize % (MinABIStackAlignInBytes * 8);
4646
4647  if (R)
4648    ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
4649}
4650
4651// In N32/64, an aligned double precision floating point field is passed in
4652// a register.
4653llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
4654  SmallVector<llvm::Type*, 8> ArgList, IntArgList;
4655
4656  if (IsO32) {
4657    CoerceToIntArgs(TySize, ArgList);
4658    return llvm::StructType::get(getVMContext(), ArgList);
4659  }
4660
4661  if (Ty->isComplexType())
4662    return CGT.ConvertType(Ty);
4663
4664  const RecordType *RT = Ty->getAs<RecordType>();
4665
4666  // Unions/vectors are passed in integer registers.
4667  if (!RT || !RT->isStructureOrClassType()) {
4668    CoerceToIntArgs(TySize, ArgList);
4669    return llvm::StructType::get(getVMContext(), ArgList);
4670  }
4671
4672  const RecordDecl *RD = RT->getDecl();
4673  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4674  assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
4675
4676  uint64_t LastOffset = 0;
4677  unsigned idx = 0;
4678  llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
4679
4680  // Iterate over fields in the struct/class and check if there are any aligned
4681  // double fields.
4682  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
4683       i != e; ++i, ++idx) {
4684    const QualType Ty = i->getType();
4685    const BuiltinType *BT = Ty->getAs<BuiltinType>();
4686
4687    if (!BT || BT->getKind() != BuiltinType::Double)
4688      continue;
4689
4690    uint64_t Offset = Layout.getFieldOffset(idx);
4691    if (Offset % 64) // Ignore doubles that are not aligned.
4692      continue;
4693
4694    // Add ((Offset - LastOffset) / 64) args of type i64.
4695    for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
4696      ArgList.push_back(I64);
4697
4698    // Add double type.
4699    ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
4700    LastOffset = Offset + 64;
4701  }
4702
4703  CoerceToIntArgs(TySize - LastOffset, IntArgList);
4704  ArgList.append(IntArgList.begin(), IntArgList.end());
4705
4706  return llvm::StructType::get(getVMContext(), ArgList);
4707}
4708
4709llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
4710  assert((Offset % MinABIStackAlignInBytes) == 0);
4711
4712  if ((Align - 1) & Offset)
4713    return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
4714
4715  return 0;
4716}
4717
4718ABIArgInfo
4719MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
4720  uint64_t OrigOffset = Offset;
4721  uint64_t TySize = getContext().getTypeSize(Ty);
4722  uint64_t Align = getContext().getTypeAlign(Ty) / 8;
4723
4724  Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
4725                   (uint64_t)StackAlignInBytes);
4726  Offset = llvm::RoundUpToAlignment(Offset, Align);
4727  Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
4728
4729  if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
4730    // Ignore empty aggregates.
4731    if (TySize == 0)
4732      return ABIArgInfo::getIgnore();
4733
4734    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT)) {
4735      Offset = OrigOffset + MinABIStackAlignInBytes;
4736      return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
4737    }
4738
4739    // If we have reached here, aggregates are passed directly by coercing to
4740    // another structure type. Padding is inserted if the offset of the
4741    // aggregate is unaligned.
4742    return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
4743                                 getPaddingType(Align, OrigOffset));
4744  }
4745
4746  // Treat an enum type as its underlying type.
4747  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4748    Ty = EnumTy->getDecl()->getIntegerType();
4749
4750  if (Ty->isPromotableIntegerType())
4751    return ABIArgInfo::getExtend();
4752
4753  return ABIArgInfo::getDirect(0, 0,
4754                               IsO32 ? 0 : getPaddingType(Align, OrigOffset));
4755}
4756
4757llvm::Type*
4758MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
4759  const RecordType *RT = RetTy->getAs<RecordType>();
4760  SmallVector<llvm::Type*, 8> RTList;
4761
4762  if (RT && RT->isStructureOrClassType()) {
4763    const RecordDecl *RD = RT->getDecl();
4764    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
4765    unsigned FieldCnt = Layout.getFieldCount();
4766
4767    // N32/64 returns struct/classes in floating point registers if the
4768    // following conditions are met:
4769    // 1. The size of the struct/class is no larger than 128-bit.
4770    // 2. The struct/class has one or two fields all of which are floating
4771    //    point types.
4772    // 3. The offset of the first field is zero (this follows what gcc does).
4773    //
4774    // Any other composite results are returned in integer registers.
4775    //
4776    if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
4777      RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
4778      for (; b != e; ++b) {
4779        const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
4780
4781        if (!BT || !BT->isFloatingPoint())
4782          break;
4783
4784        RTList.push_back(CGT.ConvertType(b->getType()));
4785      }
4786
4787      if (b == e)
4788        return llvm::StructType::get(getVMContext(), RTList,
4789                                     RD->hasAttr<PackedAttr>());
4790
4791      RTList.clear();
4792    }
4793  }
4794
4795  CoerceToIntArgs(Size, RTList);
4796  return llvm::StructType::get(getVMContext(), RTList);
4797}
4798
4799ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
4800  uint64_t Size = getContext().getTypeSize(RetTy);
4801
4802  if (RetTy->isVoidType() || Size == 0)
4803    return ABIArgInfo::getIgnore();
4804
4805  if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
4806    if (isRecordReturnIndirect(RetTy, CGT))
4807      return ABIArgInfo::getIndirect(0);
4808
4809    if (Size <= 128) {
4810      if (RetTy->isAnyComplexType())
4811        return ABIArgInfo::getDirect();
4812
4813      // O32 returns integer vectors in registers.
4814      if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
4815        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4816
4817      if (!IsO32)
4818        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
4819    }
4820
4821    return ABIArgInfo::getIndirect(0);
4822  }
4823
4824  // Treat an enum type as its underlying type.
4825  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4826    RetTy = EnumTy->getDecl()->getIntegerType();
4827
4828  return (RetTy->isPromotableIntegerType() ?
4829          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4830}
4831
4832void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
4833  ABIArgInfo &RetInfo = FI.getReturnInfo();
4834  RetInfo = classifyReturnType(FI.getReturnType());
4835
4836  // Check if a pointer to an aggregate is passed as a hidden argument.
4837  uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
4838
4839  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4840       it != ie; ++it)
4841    it->info = classifyArgumentType(it->type, Offset);
4842}
4843
4844llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4845                                    CodeGenFunction &CGF) const {
4846  llvm::Type *BP = CGF.Int8PtrTy;
4847  llvm::Type *BPP = CGF.Int8PtrPtrTy;
4848
4849  CGBuilderTy &Builder = CGF.Builder;
4850  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
4851  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
4852  int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
4853  llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
4854  llvm::Value *AddrTyped;
4855  unsigned PtrWidth = getTarget().getPointerWidth(0);
4856  llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
4857
4858  if (TypeAlign > MinABIStackAlignInBytes) {
4859    llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
4860    llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
4861    llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
4862    llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
4863    llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
4864    AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
4865  }
4866  else
4867    AddrTyped = Builder.CreateBitCast(Addr, PTy);
4868
4869  llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
4870  TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
4871  uint64_t Offset =
4872    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
4873  llvm::Value *NextAddr =
4874    Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
4875                      "ap.next");
4876  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
4877
4878  return AddrTyped;
4879}
4880
4881bool
4882MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4883                                               llvm::Value *Address) const {
4884  // This information comes from gcc's implementation, which seems to
4885  // as canonical as it gets.
4886
4887  // Everything on MIPS is 4 bytes.  Double-precision FP registers
4888  // are aliased to pairs of single-precision FP registers.
4889  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
4890
4891  // 0-31 are the general purpose registers, $0 - $31.
4892  // 32-63 are the floating-point registers, $f0 - $f31.
4893  // 64 and 65 are the multiply/divide registers, $hi and $lo.
4894  // 66 is the (notional, I think) register for signal-handler return.
4895  AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
4896
4897  // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
4898  // They are one bit wide and ignored here.
4899
4900  // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
4901  // (coprocessor 1 is the FP unit)
4902  // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
4903  // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
4904  // 176-181 are the DSP accumulator registers.
4905  AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
4906  return false;
4907}
4908
4909//===----------------------------------------------------------------------===//
4910// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
4911// Currently subclassed only to implement custom OpenCL C function attribute
4912// handling.
4913//===----------------------------------------------------------------------===//
4914
4915namespace {
4916
4917class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4918public:
4919  TCETargetCodeGenInfo(CodeGenTypes &CGT)
4920    : DefaultTargetCodeGenInfo(CGT) {}
4921
4922  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4923                                   CodeGen::CodeGenModule &M) const;
4924};
4925
4926void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
4927                                               llvm::GlobalValue *GV,
4928                                               CodeGen::CodeGenModule &M) const {
4929  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
4930  if (!FD) return;
4931
4932  llvm::Function *F = cast<llvm::Function>(GV);
4933
4934  if (M.getLangOpts().OpenCL) {
4935    if (FD->hasAttr<OpenCLKernelAttr>()) {
4936      // OpenCL C Kernel functions are not subject to inlining
4937      F->addFnAttr(llvm::Attribute::NoInline);
4938
4939      if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
4940
4941        // Convert the reqd_work_group_size() attributes to metadata.
4942        llvm::LLVMContext &Context = F->getContext();
4943        llvm::NamedMDNode *OpenCLMetadata =
4944            M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
4945
4946        SmallVector<llvm::Value*, 5> Operands;
4947        Operands.push_back(F);
4948
4949        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4950                             llvm::APInt(32,
4951                             FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
4952        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4953                             llvm::APInt(32,
4954                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
4955        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
4956                             llvm::APInt(32,
4957                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
4958
4959        // Add a boolean constant operand for "required" (true) or "hint" (false)
4960        // for implementing the work_group_size_hint attr later. Currently
4961        // always true as the hint is not yet implemented.
4962        Operands.push_back(llvm::ConstantInt::getTrue(Context));
4963        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
4964      }
4965    }
4966  }
4967}
4968
4969}
4970
4971//===----------------------------------------------------------------------===//
4972// Hexagon ABI Implementation
4973//===----------------------------------------------------------------------===//
4974
4975namespace {
4976
4977class HexagonABIInfo : public ABIInfo {
4978
4979
4980public:
4981  HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
4982
4983private:
4984
4985  ABIArgInfo classifyReturnType(QualType RetTy) const;
4986  ABIArgInfo classifyArgumentType(QualType RetTy) const;
4987
4988  virtual void computeInfo(CGFunctionInfo &FI) const;
4989
4990  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4991                                 CodeGenFunction &CGF) const;
4992};
4993
4994class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
4995public:
4996  HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
4997    :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
4998
4999  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
5000    return 29;
5001  }
5002};
5003
5004}
5005
5006void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
5007  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5008  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5009       it != ie; ++it)
5010    it->info = classifyArgumentType(it->type);
5011}
5012
5013ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
5014  if (!isAggregateTypeForABI(Ty)) {
5015    // Treat an enum type as its underlying type.
5016    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5017      Ty = EnumTy->getDecl()->getIntegerType();
5018
5019    return (Ty->isPromotableIntegerType() ?
5020            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5021  }
5022
5023  // Ignore empty records.
5024  if (isEmptyRecord(getContext(), Ty, true))
5025    return ABIArgInfo::getIgnore();
5026
5027  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, CGT))
5028    return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory);
5029
5030  uint64_t Size = getContext().getTypeSize(Ty);
5031  if (Size > 64)
5032    return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5033    // Pass in the smallest viable integer type.
5034  else if (Size > 32)
5035      return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5036  else if (Size > 16)
5037      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5038  else if (Size > 8)
5039      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5040  else
5041      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5042}
5043
5044ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
5045  if (RetTy->isVoidType())
5046    return ABIArgInfo::getIgnore();
5047
5048  // Large vector types should be returned via memory.
5049  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
5050    return ABIArgInfo::getIndirect(0);
5051
5052  if (!isAggregateTypeForABI(RetTy)) {
5053    // Treat an enum type as its underlying type.
5054    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5055      RetTy = EnumTy->getDecl()->getIntegerType();
5056
5057    return (RetTy->isPromotableIntegerType() ?
5058            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5059  }
5060
5061  // Structures with either a non-trivial destructor or a non-trivial
5062  // copy constructor are always indirect.
5063  if (isRecordReturnIndirect(RetTy, CGT))
5064    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5065
5066  if (isEmptyRecord(getContext(), RetTy, true))
5067    return ABIArgInfo::getIgnore();
5068
5069  // Aggregates <= 8 bytes are returned in r0; other aggregates
5070  // are returned indirectly.
5071  uint64_t Size = getContext().getTypeSize(RetTy);
5072  if (Size <= 64) {
5073    // Return in the smallest viable integer type.
5074    if (Size <= 8)
5075      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5076    if (Size <= 16)
5077      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5078    if (Size <= 32)
5079      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5080    return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
5081  }
5082
5083  return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
5084}
5085
5086llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5087                                       CodeGenFunction &CGF) const {
5088  // FIXME: Need to handle alignment
5089  llvm::Type *BPP = CGF.Int8PtrPtrTy;
5090
5091  CGBuilderTy &Builder = CGF.Builder;
5092  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
5093                                                       "ap");
5094  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5095  llvm::Type *PTy =
5096    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
5097  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
5098
5099  uint64_t Offset =
5100    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
5101  llvm::Value *NextAddr =
5102    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
5103                      "ap.next");
5104  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
5105
5106  return AddrTyped;
5107}
5108
5109
5110//===----------------------------------------------------------------------===//
5111// SPARC v9 ABI Implementation.
5112// Based on the SPARC Compliance Definition version 2.4.1.
5113//
5114// Function arguments a mapped to a nominal "parameter array" and promoted to
5115// registers depending on their type. Each argument occupies 8 or 16 bytes in
5116// the array, structs larger than 16 bytes are passed indirectly.
5117//
5118// One case requires special care:
5119//
5120//   struct mixed {
5121//     int i;
5122//     float f;
5123//   };
5124//
5125// When a struct mixed is passed by value, it only occupies 8 bytes in the
5126// parameter array, but the int is passed in an integer register, and the float
5127// is passed in a floating point register. This is represented as two arguments
5128// with the LLVM IR inreg attribute:
5129//
5130//   declare void f(i32 inreg %i, float inreg %f)
5131//
5132// The code generator will only allocate 4 bytes from the parameter array for
5133// the inreg arguments. All other arguments are allocated a multiple of 8
5134// bytes.
5135//
5136namespace {
5137class SparcV9ABIInfo : public ABIInfo {
5138public:
5139  SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5140
5141private:
5142  ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
5143  virtual void computeInfo(CGFunctionInfo &FI) const;
5144  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5145                                 CodeGenFunction &CGF) const;
5146
5147  // Coercion type builder for structs passed in registers. The coercion type
5148  // serves two purposes:
5149  //
5150  // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
5151  //    in registers.
5152  // 2. Expose aligned floating point elements as first-level elements, so the
5153  //    code generator knows to pass them in floating point registers.
5154  //
5155  // We also compute the InReg flag which indicates that the struct contains
5156  // aligned 32-bit floats.
5157  //
5158  struct CoerceBuilder {
5159    llvm::LLVMContext &Context;
5160    const llvm::DataLayout &DL;
5161    SmallVector<llvm::Type*, 8> Elems;
5162    uint64_t Size;
5163    bool InReg;
5164
5165    CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
5166      : Context(c), DL(dl), Size(0), InReg(false) {}
5167
5168    // Pad Elems with integers until Size is ToSize.
5169    void pad(uint64_t ToSize) {
5170      assert(ToSize >= Size && "Cannot remove elements");
5171      if (ToSize == Size)
5172        return;
5173
5174      // Finish the current 64-bit word.
5175      uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
5176      if (Aligned > Size && Aligned <= ToSize) {
5177        Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
5178        Size = Aligned;
5179      }
5180
5181      // Add whole 64-bit words.
5182      while (Size + 64 <= ToSize) {
5183        Elems.push_back(llvm::Type::getInt64Ty(Context));
5184        Size += 64;
5185      }
5186
5187      // Final in-word padding.
5188      if (Size < ToSize) {
5189        Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
5190        Size = ToSize;
5191      }
5192    }
5193
5194    // Add a floating point element at Offset.
5195    void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
5196      // Unaligned floats are treated as integers.
5197      if (Offset % Bits)
5198        return;
5199      // The InReg flag is only required if there are any floats < 64 bits.
5200      if (Bits < 64)
5201        InReg = true;
5202      pad(Offset);
5203      Elems.push_back(Ty);
5204      Size = Offset + Bits;
5205    }
5206
5207    // Add a struct type to the coercion type, starting at Offset (in bits).
5208    void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
5209      const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
5210      for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
5211        llvm::Type *ElemTy = StrTy->getElementType(i);
5212        uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
5213        switch (ElemTy->getTypeID()) {
5214        case llvm::Type::StructTyID:
5215          addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
5216          break;
5217        case llvm::Type::FloatTyID:
5218          addFloat(ElemOffset, ElemTy, 32);
5219          break;
5220        case llvm::Type::DoubleTyID:
5221          addFloat(ElemOffset, ElemTy, 64);
5222          break;
5223        case llvm::Type::FP128TyID:
5224          addFloat(ElemOffset, ElemTy, 128);
5225          break;
5226        case llvm::Type::PointerTyID:
5227          if (ElemOffset % 64 == 0) {
5228            pad(ElemOffset);
5229            Elems.push_back(ElemTy);
5230            Size += 64;
5231          }
5232          break;
5233        default:
5234          break;
5235        }
5236      }
5237    }
5238
5239    // Check if Ty is a usable substitute for the coercion type.
5240    bool isUsableType(llvm::StructType *Ty) const {
5241      if (Ty->getNumElements() != Elems.size())
5242        return false;
5243      for (unsigned i = 0, e = Elems.size(); i != e; ++i)
5244        if (Elems[i] != Ty->getElementType(i))
5245          return false;
5246      return true;
5247    }
5248
5249    // Get the coercion type as a literal struct type.
5250    llvm::Type *getType() const {
5251      if (Elems.size() == 1)
5252        return Elems.front();
5253      else
5254        return llvm::StructType::get(Context, Elems);
5255    }
5256  };
5257};
5258} // end anonymous namespace
5259
5260ABIArgInfo
5261SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
5262  if (Ty->isVoidType())
5263    return ABIArgInfo::getIgnore();
5264
5265  uint64_t Size = getContext().getTypeSize(Ty);
5266
5267  // Anything too big to fit in registers is passed with an explicit indirect
5268  // pointer / sret pointer.
5269  if (Size > SizeLimit)
5270    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5271
5272  // Treat an enum type as its underlying type.
5273  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5274    Ty = EnumTy->getDecl()->getIntegerType();
5275
5276  // Integer types smaller than a register are extended.
5277  if (Size < 64 && Ty->isIntegerType())
5278    return ABIArgInfo::getExtend();
5279
5280  // Other non-aggregates go in registers.
5281  if (!isAggregateTypeForABI(Ty))
5282    return ABIArgInfo::getDirect();
5283
5284  // This is a small aggregate type that should be passed in registers.
5285  // Build a coercion type from the LLVM struct type.
5286  llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
5287  if (!StrTy)
5288    return ABIArgInfo::getDirect();
5289
5290  CoerceBuilder CB(getVMContext(), getDataLayout());
5291  CB.addStruct(0, StrTy);
5292  CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
5293
5294  // Try to use the original type for coercion.
5295  llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
5296
5297  if (CB.InReg)
5298    return ABIArgInfo::getDirectInReg(CoerceTy);
5299  else
5300    return ABIArgInfo::getDirect(CoerceTy);
5301}
5302
5303llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
5304                                       CodeGenFunction &CGF) const {
5305  ABIArgInfo AI = classifyType(Ty, 16 * 8);
5306  llvm::Type *ArgTy = CGT.ConvertType(Ty);
5307  if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
5308    AI.setCoerceToType(ArgTy);
5309
5310  llvm::Type *BPP = CGF.Int8PtrPtrTy;
5311  CGBuilderTy &Builder = CGF.Builder;
5312  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
5313  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
5314  llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
5315  llvm::Value *ArgAddr;
5316  unsigned Stride;
5317
5318  switch (AI.getKind()) {
5319  case ABIArgInfo::Expand:
5320    llvm_unreachable("Unsupported ABI kind for va_arg");
5321
5322  case ABIArgInfo::Extend:
5323    Stride = 8;
5324    ArgAddr = Builder
5325      .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy),
5326                          "extend");
5327    break;
5328
5329  case ABIArgInfo::Direct:
5330    Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
5331    ArgAddr = Addr;
5332    break;
5333
5334  case ABIArgInfo::Indirect:
5335    Stride = 8;
5336    ArgAddr = Builder.CreateBitCast(Addr,
5337                                    llvm::PointerType::getUnqual(ArgPtrTy),
5338                                    "indirect");
5339    ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg");
5340    break;
5341
5342  case ABIArgInfo::Ignore:
5343    return llvm::UndefValue::get(ArgPtrTy);
5344  }
5345
5346  // Update VAList.
5347  Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next");
5348  Builder.CreateStore(Addr, VAListAddrAsBPP);
5349
5350  return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr");
5351}
5352
5353void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
5354  FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
5355  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
5356       it != ie; ++it)
5357    it->info = classifyType(it->type, 16 * 8);
5358}
5359
5360namespace {
5361class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
5362public:
5363  SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
5364    : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
5365};
5366} // end anonymous namespace
5367
5368
5369//===----------------------------------------------------------------------===//
5370// Xcore ABI Implementation
5371//===----------------------------------------------------------------------===//
5372namespace {
5373class XcoreTargetCodeGenInfo : public TargetCodeGenInfo {
5374public:
5375  XcoreTargetCodeGenInfo(CodeGenTypes &CGT)
5376    :TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
5377};
5378} // end anonymous namespace
5379
5380
5381//===----------------------------------------------------------------------===//
5382// Driver code
5383//===----------------------------------------------------------------------===//
5384
5385const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
5386  if (TheTargetCodeGenInfo)
5387    return *TheTargetCodeGenInfo;
5388
5389  const llvm::Triple &Triple = getTarget().getTriple();
5390  switch (Triple.getArch()) {
5391  default:
5392    return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
5393
5394  case llvm::Triple::le32:
5395    return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
5396  case llvm::Triple::mips:
5397  case llvm::Triple::mipsel:
5398    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
5399
5400  case llvm::Triple::mips64:
5401  case llvm::Triple::mips64el:
5402    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
5403
5404  case llvm::Triple::aarch64:
5405    return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types));
5406
5407  case llvm::Triple::arm:
5408  case llvm::Triple::thumb:
5409    {
5410      ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
5411      if (strcmp(getTarget().getABI(), "apcs-gnu") == 0)
5412        Kind = ARMABIInfo::APCS;
5413      else if (CodeGenOpts.FloatABI == "hard" ||
5414               (CodeGenOpts.FloatABI != "soft" &&
5415                Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
5416        Kind = ARMABIInfo::AAPCS_VFP;
5417
5418      switch (Triple.getOS()) {
5419        case llvm::Triple::NaCl:
5420          return *(TheTargetCodeGenInfo =
5421                   new NaClARMTargetCodeGenInfo(Types, Kind));
5422        default:
5423          return *(TheTargetCodeGenInfo =
5424                   new ARMTargetCodeGenInfo(Types, Kind));
5425      }
5426    }
5427
5428  case llvm::Triple::ppc:
5429    return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
5430  case llvm::Triple::ppc64:
5431    if (Triple.isOSBinFormatELF())
5432      return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5433    else
5434      return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
5435  case llvm::Triple::ppc64le:
5436    assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
5437    return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types));
5438
5439  case llvm::Triple::nvptx:
5440  case llvm::Triple::nvptx64:
5441    return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
5442
5443  case llvm::Triple::msp430:
5444    return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
5445
5446  case llvm::Triple::systemz:
5447    return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types));
5448
5449  case llvm::Triple::tce:
5450    return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
5451
5452  case llvm::Triple::x86: {
5453    bool IsDarwinVectorABI = Triple.isOSDarwin();
5454    bool IsSmallStructInRegABI =
5455        X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
5456    bool IsWin32FloatStructABI = (Triple.getOS() == llvm::Triple::Win32);
5457
5458    if (Triple.getOS() == llvm::Triple::Win32) {
5459      return *(TheTargetCodeGenInfo =
5460               new WinX86_32TargetCodeGenInfo(Types,
5461                                              IsDarwinVectorABI, IsSmallStructInRegABI,
5462                                              IsWin32FloatStructABI,
5463                                              CodeGenOpts.NumRegisterParameters));
5464    } else {
5465      return *(TheTargetCodeGenInfo =
5466               new X86_32TargetCodeGenInfo(Types,
5467                                           IsDarwinVectorABI, IsSmallStructInRegABI,
5468                                           IsWin32FloatStructABI,
5469                                           CodeGenOpts.NumRegisterParameters));
5470    }
5471  }
5472
5473  case llvm::Triple::x86_64: {
5474    bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0;
5475
5476    switch (Triple.getOS()) {
5477    case llvm::Triple::Win32:
5478    case llvm::Triple::MinGW32:
5479    case llvm::Triple::Cygwin:
5480      return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
5481    case llvm::Triple::NaCl:
5482      return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types,
5483                                                                      HasAVX));
5484    default:
5485      return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
5486                                                                  HasAVX));
5487    }
5488  }
5489  case llvm::Triple::hexagon:
5490    return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
5491  case llvm::Triple::sparcv9:
5492    return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
5493  case llvm::Triple::xcore:
5494    return *(TheTargetCodeGenInfo = new XcoreTargetCodeGenInfo(Types));
5495
5496  }
5497}
5498