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 "CGValue.h"
19#include "CodeGenFunction.h"
20#include "clang/AST/RecordLayout.h"
21#include "clang/CodeGen/CGFunctionInfo.h"
22#include "clang/Frontend/CodeGenOptions.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Triple.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/Type.h"
27#include "llvm/Support/raw_ostream.h"
28#include <algorithm>    // std::sort
29
30using namespace clang;
31using namespace CodeGen;
32
33// Helper for coercing an aggregate argument or return value into an
34// integer array of the same size (including padding).
35//
36// This is needed for RenderScript on ARM targets. The RenderScript
37// compiler assumes that the size of the argument / return value in
38// the IR is the same as the size of the corresponding qualified
39// type. It is necessary to coerce the aggregate type into an
40// array. We cannot pass a struct directly as an argument because
41// clang's struct passing logic breaks up the struct into its
42// constitutent fields.
43//
44// Ty          - The argument / return value type
45// Context     - The associated ASTContext
46// LLVMContext - The associated LLVMContext
47static ABIArgInfo coerceToIntArray(QualType Ty,
48                                   ASTContext &Context,
49                                   llvm::LLVMContext &LLVMContext) {
50  // Alignment and Size are measured in bits.
51  const uint64_t Size = Context.getTypeSize(Ty);
52  const uint64_t Alignment = Context.getTypeAlign(Ty);
53  llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
54  const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
55  return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
56}
57
58static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
59                               llvm::Value *Array,
60                               llvm::Value *Value,
61                               unsigned FirstIndex,
62                               unsigned LastIndex) {
63  // Alternatively, we could emit this as a loop in the source.
64  for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
65    llvm::Value *Cell =
66        Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
67    Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
68  }
69}
70
71static bool isAggregateTypeForABI(QualType T) {
72  return !CodeGenFunction::hasScalarEvaluationKind(T) ||
73         T->isMemberFunctionPointerType();
74}
75
76ABIArgInfo
77ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
78                                 llvm::Type *Padding) const {
79  return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
80                                 ByRef, Realign, Padding);
81}
82
83ABIArgInfo
84ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
85  return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
86                                      /*ByRef*/ false, Realign);
87}
88
89Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
90                             QualType Ty) const {
91  return Address::invalid();
92}
93
94ABIInfo::~ABIInfo() {}
95
96static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
97                                              CGCXXABI &CXXABI) {
98  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
99  if (!RD)
100    return CGCXXABI::RAA_Default;
101  return CXXABI.getRecordArgABI(RD);
102}
103
104static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
105                                              CGCXXABI &CXXABI) {
106  const RecordType *RT = T->getAs<RecordType>();
107  if (!RT)
108    return CGCXXABI::RAA_Default;
109  return getRecordArgABI(RT, CXXABI);
110}
111
112/// Pass transparent unions as if they were the type of the first element. Sema
113/// should ensure that all elements of the union have the same "machine type".
114static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
115  if (const RecordType *UT = Ty->getAsUnionType()) {
116    const RecordDecl *UD = UT->getDecl();
117    if (UD->hasAttr<TransparentUnionAttr>()) {
118      assert(!UD->field_empty() && "sema created an empty transparent union");
119      return UD->field_begin()->getType();
120    }
121  }
122  return Ty;
123}
124
125CGCXXABI &ABIInfo::getCXXABI() const {
126  return CGT.getCXXABI();
127}
128
129ASTContext &ABIInfo::getContext() const {
130  return CGT.getContext();
131}
132
133llvm::LLVMContext &ABIInfo::getVMContext() const {
134  return CGT.getLLVMContext();
135}
136
137const llvm::DataLayout &ABIInfo::getDataLayout() const {
138  return CGT.getDataLayout();
139}
140
141const TargetInfo &ABIInfo::getTarget() const {
142  return CGT.getTarget();
143}
144
145bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
146  return false;
147}
148
149bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
150                                                uint64_t Members) const {
151  return false;
152}
153
154bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
155  return false;
156}
157
158void ABIArgInfo::dump() const {
159  raw_ostream &OS = llvm::errs();
160  OS << "(ABIArgInfo Kind=";
161  switch (TheKind) {
162  case Direct:
163    OS << "Direct Type=";
164    if (llvm::Type *Ty = getCoerceToType())
165      Ty->print(OS);
166    else
167      OS << "null";
168    break;
169  case Extend:
170    OS << "Extend";
171    break;
172  case Ignore:
173    OS << "Ignore";
174    break;
175  case InAlloca:
176    OS << "InAlloca Offset=" << getInAllocaFieldIndex();
177    break;
178  case Indirect:
179    OS << "Indirect Align=" << getIndirectAlign().getQuantity()
180       << " ByVal=" << getIndirectByVal()
181       << " Realign=" << getIndirectRealign();
182    break;
183  case Expand:
184    OS << "Expand";
185    break;
186  }
187  OS << ")\n";
188}
189
190// Dynamically round a pointer up to a multiple of the given alignment.
191static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
192                                                  llvm::Value *Ptr,
193                                                  CharUnits Align) {
194  llvm::Value *PtrAsInt = Ptr;
195  // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
196  PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
197  PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
198        llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
199  PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
200           llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
201  PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
202                                        Ptr->getType(),
203                                        Ptr->getName() + ".aligned");
204  return PtrAsInt;
205}
206
207/// Emit va_arg for a platform using the common void* representation,
208/// where arguments are simply emitted in an array of slots on the stack.
209///
210/// This version implements the core direct-value passing rules.
211///
212/// \param SlotSize - The size and alignment of a stack slot.
213///   Each argument will be allocated to a multiple of this number of
214///   slots, and all the slots will be aligned to this value.
215/// \param AllowHigherAlign - The slot alignment is not a cap;
216///   an argument type with an alignment greater than the slot size
217///   will be emitted on a higher-alignment address, potentially
218///   leaving one or more empty slots behind as padding.  If this
219///   is false, the returned address might be less-aligned than
220///   DirectAlign.
221static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
222                                      Address VAListAddr,
223                                      llvm::Type *DirectTy,
224                                      CharUnits DirectSize,
225                                      CharUnits DirectAlign,
226                                      CharUnits SlotSize,
227                                      bool AllowHigherAlign) {
228  // Cast the element type to i8* if necessary.  Some platforms define
229  // va_list as a struct containing an i8* instead of just an i8*.
230  if (VAListAddr.getElementType() != CGF.Int8PtrTy)
231    VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
232
233  llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
234
235  // If the CC aligns values higher than the slot size, do so if needed.
236  Address Addr = Address::invalid();
237  if (AllowHigherAlign && DirectAlign > SlotSize) {
238    Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
239                                                 DirectAlign);
240  } else {
241    Addr = Address(Ptr, SlotSize);
242  }
243
244  // Advance the pointer past the argument, then store that back.
245  CharUnits FullDirectSize = DirectSize.RoundUpToAlignment(SlotSize);
246  llvm::Value *NextPtr =
247    CGF.Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), FullDirectSize,
248                                           "argp.next");
249  CGF.Builder.CreateStore(NextPtr, VAListAddr);
250
251  // If the argument is smaller than a slot, and this is a big-endian
252  // target, the argument will be right-adjusted in its slot.
253  if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian()) {
254    Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
255  }
256
257  Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
258  return Addr;
259}
260
261/// Emit va_arg for a platform using the common void* representation,
262/// where arguments are simply emitted in an array of slots on the stack.
263///
264/// \param IsIndirect - Values of this type are passed indirectly.
265/// \param ValueInfo - The size and alignment of this type, generally
266///   computed with getContext().getTypeInfoInChars(ValueTy).
267/// \param SlotSizeAndAlign - The size and alignment of a stack slot.
268///   Each argument will be allocated to a multiple of this number of
269///   slots, and all the slots will be aligned to this value.
270/// \param AllowHigherAlign - The slot alignment is not a cap;
271///   an argument type with an alignment greater than the slot size
272///   will be emitted on a higher-alignment address, potentially
273///   leaving one or more empty slots behind as padding.
274static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
275                                QualType ValueTy, bool IsIndirect,
276                                std::pair<CharUnits, CharUnits> ValueInfo,
277                                CharUnits SlotSizeAndAlign,
278                                bool AllowHigherAlign) {
279  // The size and alignment of the value that was passed directly.
280  CharUnits DirectSize, DirectAlign;
281  if (IsIndirect) {
282    DirectSize = CGF.getPointerSize();
283    DirectAlign = CGF.getPointerAlign();
284  } else {
285    DirectSize = ValueInfo.first;
286    DirectAlign = ValueInfo.second;
287  }
288
289  // Cast the address we've calculated to the right type.
290  llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
291  if (IsIndirect)
292    DirectTy = DirectTy->getPointerTo(0);
293
294  Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
295                                        DirectSize, DirectAlign,
296                                        SlotSizeAndAlign,
297                                        AllowHigherAlign);
298
299  if (IsIndirect) {
300    Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
301  }
302
303  return Addr;
304
305}
306
307static Address emitMergePHI(CodeGenFunction &CGF,
308                            Address Addr1, llvm::BasicBlock *Block1,
309                            Address Addr2, llvm::BasicBlock *Block2,
310                            const llvm::Twine &Name = "") {
311  assert(Addr1.getType() == Addr2.getType());
312  llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
313  PHI->addIncoming(Addr1.getPointer(), Block1);
314  PHI->addIncoming(Addr2.getPointer(), Block2);
315  CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
316  return Address(PHI, Align);
317}
318
319TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
320
321// If someone can figure out a general rule for this, that would be great.
322// It's probably just doomed to be platform-dependent, though.
323unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
324  // Verified for:
325  //   x86-64     FreeBSD, Linux, Darwin
326  //   x86-32     FreeBSD, Linux, Darwin
327  //   PowerPC    Linux, Darwin
328  //   ARM        Darwin (*not* EABI)
329  //   AArch64    Linux
330  return 32;
331}
332
333bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
334                                     const FunctionNoProtoType *fnType) const {
335  // The following conventions are known to require this to be false:
336  //   x86_stdcall
337  //   MIPS
338  // For everything else, we just prefer false unless we opt out.
339  return false;
340}
341
342void
343TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
344                                             llvm::SmallString<24> &Opt) const {
345  // This assumes the user is passing a library name like "rt" instead of a
346  // filename like "librt.a/so", and that they don't care whether it's static or
347  // dynamic.
348  Opt = "-l";
349  Opt += Lib;
350}
351
352static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
353
354/// isEmptyField - Return true iff a the field is "empty", that is it
355/// is an unnamed bit-field or an (array of) empty record(s).
356static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
357                         bool AllowArrays) {
358  if (FD->isUnnamedBitfield())
359    return true;
360
361  QualType FT = FD->getType();
362
363  // Constant arrays of empty records count as empty, strip them off.
364  // Constant arrays of zero length always count as empty.
365  if (AllowArrays)
366    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
367      if (AT->getSize() == 0)
368        return true;
369      FT = AT->getElementType();
370    }
371
372  const RecordType *RT = FT->getAs<RecordType>();
373  if (!RT)
374    return false;
375
376  // C++ record fields are never empty, at least in the Itanium ABI.
377  //
378  // FIXME: We should use a predicate for whether this behavior is true in the
379  // current ABI.
380  if (isa<CXXRecordDecl>(RT->getDecl()))
381    return false;
382
383  return isEmptyRecord(Context, FT, AllowArrays);
384}
385
386/// isEmptyRecord - Return true iff a structure contains only empty
387/// fields. Note that a structure with a flexible array member is not
388/// considered empty.
389static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
390  const RecordType *RT = T->getAs<RecordType>();
391  if (!RT)
392    return 0;
393  const RecordDecl *RD = RT->getDecl();
394  if (RD->hasFlexibleArrayMember())
395    return false;
396
397  // If this is a C++ record, check the bases first.
398  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
399    for (const auto &I : CXXRD->bases())
400      if (!isEmptyRecord(Context, I.getType(), true))
401        return false;
402
403  for (const auto *I : RD->fields())
404    if (!isEmptyField(Context, I, AllowArrays))
405      return false;
406  return true;
407}
408
409/// isSingleElementStruct - Determine if a structure is a "single
410/// element struct", i.e. it has exactly one non-empty field or
411/// exactly one field which is itself a single element
412/// struct. Structures with flexible array members are never
413/// considered single element structs.
414///
415/// \return The field declaration for the single non-empty field, if
416/// it exists.
417static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
418  const RecordType *RT = T->getAs<RecordType>();
419  if (!RT)
420    return nullptr;
421
422  const RecordDecl *RD = RT->getDecl();
423  if (RD->hasFlexibleArrayMember())
424    return nullptr;
425
426  const Type *Found = nullptr;
427
428  // If this is a C++ record, check the bases first.
429  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
430    for (const auto &I : CXXRD->bases()) {
431      // Ignore empty records.
432      if (isEmptyRecord(Context, I.getType(), true))
433        continue;
434
435      // If we already found an element then this isn't a single-element struct.
436      if (Found)
437        return nullptr;
438
439      // If this is non-empty and not a single element struct, the composite
440      // cannot be a single element struct.
441      Found = isSingleElementStruct(I.getType(), Context);
442      if (!Found)
443        return nullptr;
444    }
445  }
446
447  // Check for single element.
448  for (const auto *FD : RD->fields()) {
449    QualType FT = FD->getType();
450
451    // Ignore empty fields.
452    if (isEmptyField(Context, FD, true))
453      continue;
454
455    // If we already found an element then this isn't a single-element
456    // struct.
457    if (Found)
458      return nullptr;
459
460    // Treat single element arrays as the element.
461    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
462      if (AT->getSize().getZExtValue() != 1)
463        break;
464      FT = AT->getElementType();
465    }
466
467    if (!isAggregateTypeForABI(FT)) {
468      Found = FT.getTypePtr();
469    } else {
470      Found = isSingleElementStruct(FT, Context);
471      if (!Found)
472        return nullptr;
473    }
474  }
475
476  // We don't consider a struct a single-element struct if it has
477  // padding beyond the element type.
478  if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
479    return nullptr;
480
481  return Found;
482}
483
484static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
485  // Treat complex types as the element type.
486  if (const ComplexType *CTy = Ty->getAs<ComplexType>())
487    Ty = CTy->getElementType();
488
489  // Check for a type which we know has a simple scalar argument-passing
490  // convention without any padding.  (We're specifically looking for 32
491  // and 64-bit integer and integer-equivalents, float, and double.)
492  if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
493      !Ty->isEnumeralType() && !Ty->isBlockPointerType())
494    return false;
495
496  uint64_t Size = Context.getTypeSize(Ty);
497  return Size == 32 || Size == 64;
498}
499
500/// canExpandIndirectArgument - Test whether an argument type which is to be
501/// passed indirectly (on the stack) would have the equivalent layout if it was
502/// expanded into separate arguments. If so, we prefer to do the latter to avoid
503/// inhibiting optimizations.
504///
505// FIXME: This predicate is missing many cases, currently it just follows
506// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
507// should probably make this smarter, or better yet make the LLVM backend
508// capable of handling it.
509static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
510  // We can only expand structure types.
511  const RecordType *RT = Ty->getAs<RecordType>();
512  if (!RT)
513    return false;
514
515  // We can only expand (C) structures.
516  //
517  // FIXME: This needs to be generalized to handle classes as well.
518  const RecordDecl *RD = RT->getDecl();
519  if (!RD->isStruct())
520    return false;
521
522  // We try to expand CLike CXXRecordDecl.
523  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
524    if (!CXXRD->isCLike())
525      return false;
526  }
527
528  uint64_t Size = 0;
529
530  for (const auto *FD : RD->fields()) {
531    if (!is32Or64BitBasicType(FD->getType(), Context))
532      return false;
533
534    // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
535    // how to expand them yet, and the predicate for telling if a bitfield still
536    // counts as "basic" is more complicated than what we were doing previously.
537    if (FD->isBitField())
538      return false;
539
540    Size += Context.getTypeSize(FD->getType());
541  }
542
543  // Make sure there are not any holes in the struct.
544  if (Size != Context.getTypeSize(Ty))
545    return false;
546
547  return true;
548}
549
550namespace {
551/// DefaultABIInfo - The default implementation for ABI specific
552/// details. This implementation provides information which results in
553/// self-consistent and sensible LLVM IR generation, but does not
554/// conform to any particular ABI.
555class DefaultABIInfo : public ABIInfo {
556public:
557  DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
558
559  ABIArgInfo classifyReturnType(QualType RetTy) const;
560  ABIArgInfo classifyArgumentType(QualType RetTy) const;
561
562  void computeInfo(CGFunctionInfo &FI) const override {
563    if (!getCXXABI().classifyReturnType(FI))
564      FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
565    for (auto &I : FI.arguments())
566      I.info = classifyArgumentType(I.type);
567  }
568
569  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
570                    QualType Ty) const override;
571};
572
573class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
574public:
575  DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
576    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
577};
578
579Address DefaultABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
580                                  QualType Ty) const {
581  return Address::invalid();
582}
583
584ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
585  Ty = useFirstFieldIfTransparentUnion(Ty);
586
587  if (isAggregateTypeForABI(Ty)) {
588    // Records with non-trivial destructors/copy-constructors should not be
589    // passed by value.
590    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
591      return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
592
593    return getNaturalAlignIndirect(Ty);
594  }
595
596  // Treat an enum type as its underlying type.
597  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
598    Ty = EnumTy->getDecl()->getIntegerType();
599
600  return (Ty->isPromotableIntegerType() ?
601          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
602}
603
604ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
605  if (RetTy->isVoidType())
606    return ABIArgInfo::getIgnore();
607
608  if (isAggregateTypeForABI(RetTy))
609    return getNaturalAlignIndirect(RetTy);
610
611  // Treat an enum type as its underlying type.
612  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
613    RetTy = EnumTy->getDecl()->getIntegerType();
614
615  return (RetTy->isPromotableIntegerType() ?
616          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
617}
618
619//===----------------------------------------------------------------------===//
620// WebAssembly ABI Implementation
621//
622// This is a very simple ABI that relies a lot on DefaultABIInfo.
623//===----------------------------------------------------------------------===//
624
625class WebAssemblyABIInfo final : public DefaultABIInfo {
626public:
627  explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
628      : DefaultABIInfo(CGT) {}
629
630private:
631  ABIArgInfo classifyReturnType(QualType RetTy) const;
632  ABIArgInfo classifyArgumentType(QualType Ty) const;
633
634  // DefaultABIInfo's classifyReturnType and classifyArgumentType are
635  // non-virtual, but computeInfo is virtual, so we overload that.
636  void computeInfo(CGFunctionInfo &FI) const override {
637    if (!getCXXABI().classifyReturnType(FI))
638      FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
639    for (auto &Arg : FI.arguments())
640      Arg.info = classifyArgumentType(Arg.type);
641  }
642};
643
644class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
645public:
646  explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
647      : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
648};
649
650/// \brief Classify argument of given type \p Ty.
651ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
652  Ty = useFirstFieldIfTransparentUnion(Ty);
653
654  if (isAggregateTypeForABI(Ty)) {
655    // Records with non-trivial destructors/copy-constructors should not be
656    // passed by value.
657    if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
658      return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
659    // Ignore empty structs/unions.
660    if (isEmptyRecord(getContext(), Ty, true))
661      return ABIArgInfo::getIgnore();
662    // Lower single-element structs to just pass a regular value. TODO: We
663    // could do reasonable-size multiple-element structs too, using getExpand(),
664    // though watch out for things like bitfields.
665    if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
666      return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
667  }
668
669  // Otherwise just do the default thing.
670  return DefaultABIInfo::classifyArgumentType(Ty);
671}
672
673ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
674  if (isAggregateTypeForABI(RetTy)) {
675    // Records with non-trivial destructors/copy-constructors should not be
676    // returned by value.
677    if (!getRecordArgABI(RetTy, getCXXABI())) {
678      // Ignore empty structs/unions.
679      if (isEmptyRecord(getContext(), RetTy, true))
680        return ABIArgInfo::getIgnore();
681      // Lower single-element structs to just return a regular value. TODO: We
682      // could do reasonable-size multiple-element structs too, using
683      // ABIArgInfo::getDirect().
684      if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
685        return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
686    }
687  }
688
689  // Otherwise just do the default thing.
690  return DefaultABIInfo::classifyReturnType(RetTy);
691}
692
693//===----------------------------------------------------------------------===//
694// le32/PNaCl bitcode ABI Implementation
695//
696// This is a simplified version of the x86_32 ABI.  Arguments and return values
697// are always passed on the stack.
698//===----------------------------------------------------------------------===//
699
700class PNaClABIInfo : public ABIInfo {
701 public:
702  PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
703
704  ABIArgInfo classifyReturnType(QualType RetTy) const;
705  ABIArgInfo classifyArgumentType(QualType RetTy) const;
706
707  void computeInfo(CGFunctionInfo &FI) const override;
708  Address EmitVAArg(CodeGenFunction &CGF,
709                    Address VAListAddr, QualType Ty) const override;
710};
711
712class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
713 public:
714  PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
715    : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
716};
717
718void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
719  if (!getCXXABI().classifyReturnType(FI))
720    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
721
722  for (auto &I : FI.arguments())
723    I.info = classifyArgumentType(I.type);
724}
725
726Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
727                                QualType Ty) const {
728  return Address::invalid();
729}
730
731/// \brief Classify argument of given type \p Ty.
732ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
733  if (isAggregateTypeForABI(Ty)) {
734    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
735      return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
736    return getNaturalAlignIndirect(Ty);
737  } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
738    // Treat an enum type as its underlying type.
739    Ty = EnumTy->getDecl()->getIntegerType();
740  } else if (Ty->isFloatingType()) {
741    // Floating-point types don't go inreg.
742    return ABIArgInfo::getDirect();
743  }
744
745  return (Ty->isPromotableIntegerType() ?
746          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
747}
748
749ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
750  if (RetTy->isVoidType())
751    return ABIArgInfo::getIgnore();
752
753  // In the PNaCl ABI we always return records/structures on the stack.
754  if (isAggregateTypeForABI(RetTy))
755    return getNaturalAlignIndirect(RetTy);
756
757  // Treat an enum type as its underlying type.
758  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
759    RetTy = EnumTy->getDecl()->getIntegerType();
760
761  return (RetTy->isPromotableIntegerType() ?
762          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
763}
764
765/// IsX86_MMXType - Return true if this is an MMX type.
766bool IsX86_MMXType(llvm::Type *IRType) {
767  // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
768  return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
769    cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
770    IRType->getScalarSizeInBits() != 64;
771}
772
773static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
774                                          StringRef Constraint,
775                                          llvm::Type* Ty) {
776  if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) {
777    if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
778      // Invalid MMX constraint
779      return nullptr;
780    }
781
782    return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
783  }
784
785  // No operation needed
786  return Ty;
787}
788
789/// Returns true if this type can be passed in SSE registers with the
790/// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
791static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
792  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
793    if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half)
794      return true;
795  } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
796    // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
797    // registers specially.
798    unsigned VecSize = Context.getTypeSize(VT);
799    if (VecSize == 128 || VecSize == 256 || VecSize == 512)
800      return true;
801  }
802  return false;
803}
804
805/// Returns true if this aggregate is small enough to be passed in SSE registers
806/// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
807static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
808  return NumMembers <= 4;
809}
810
811//===----------------------------------------------------------------------===//
812// X86-32 ABI Implementation
813//===----------------------------------------------------------------------===//
814
815/// \brief Similar to llvm::CCState, but for Clang.
816struct CCState {
817  CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
818
819  unsigned CC;
820  unsigned FreeRegs;
821  unsigned FreeSSERegs;
822};
823
824/// X86_32ABIInfo - The X86-32 ABI information.
825class X86_32ABIInfo : public ABIInfo {
826  enum Class {
827    Integer,
828    Float
829  };
830
831  static const unsigned MinABIStackAlignInBytes = 4;
832
833  bool IsDarwinVectorABI;
834  bool IsRetSmallStructInRegABI;
835  bool IsWin32StructABI;
836  bool IsSoftFloatABI;
837  bool IsMCUABI;
838  unsigned DefaultNumRegisterParameters;
839
840  static bool isRegisterSize(unsigned Size) {
841    return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
842  }
843
844  bool isHomogeneousAggregateBaseType(QualType Ty) const override {
845    // FIXME: Assumes vectorcall is in use.
846    return isX86VectorTypeForVectorCall(getContext(), Ty);
847  }
848
849  bool isHomogeneousAggregateSmallEnough(const Type *Ty,
850                                         uint64_t NumMembers) const override {
851    // FIXME: Assumes vectorcall is in use.
852    return isX86VectorCallAggregateSmallEnough(NumMembers);
853  }
854
855  bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
856
857  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
858  /// such that the argument will be passed in memory.
859  ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
860
861  ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
862
863  /// \brief Return the alignment to use for the given type on the stack.
864  unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
865
866  Class classify(QualType Ty) const;
867  ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
868  ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
869  bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const;
870
871  /// \brief Rewrite the function info so that all memory arguments use
872  /// inalloca.
873  void rewriteWithInAlloca(CGFunctionInfo &FI) const;
874
875  void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
876                           CharUnits &StackOffset, ABIArgInfo &Info,
877                           QualType Type) const;
878
879public:
880
881  void computeInfo(CGFunctionInfo &FI) const override;
882  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
883                    QualType Ty) const override;
884
885  X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
886                bool RetSmallStructInRegABI, bool Win32StructABI,
887                unsigned NumRegisterParameters, bool SoftFloatABI)
888    : ABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
889      IsRetSmallStructInRegABI(RetSmallStructInRegABI),
890      IsWin32StructABI(Win32StructABI),
891      IsSoftFloatABI(SoftFloatABI),
892      IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
893      DefaultNumRegisterParameters(NumRegisterParameters) {}
894};
895
896class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
897public:
898  X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
899                          bool RetSmallStructInRegABI, bool Win32StructABI,
900                          unsigned NumRegisterParameters, bool SoftFloatABI)
901      : TargetCodeGenInfo(new X86_32ABIInfo(
902            CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
903            NumRegisterParameters, SoftFloatABI)) {}
904
905  static bool isStructReturnInRegABI(
906      const llvm::Triple &Triple, const CodeGenOptions &Opts);
907
908  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
909                           CodeGen::CodeGenModule &CGM) const override;
910
911  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
912    // Darwin uses different dwarf register numbers for EH.
913    if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
914    return 4;
915  }
916
917  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
918                               llvm::Value *Address) const override;
919
920  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
921                                  StringRef Constraint,
922                                  llvm::Type* Ty) const override {
923    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
924  }
925
926  void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
927                                std::string &Constraints,
928                                std::vector<llvm::Type *> &ResultRegTypes,
929                                std::vector<llvm::Type *> &ResultTruncRegTypes,
930                                std::vector<LValue> &ResultRegDests,
931                                std::string &AsmString,
932                                unsigned NumOutputs) const override;
933
934  llvm::Constant *
935  getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
936    unsigned Sig = (0xeb << 0) |  // jmp rel8
937                   (0x06 << 8) |  //           .+0x08
938                   ('F' << 16) |
939                   ('T' << 24);
940    return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
941  }
942};
943
944}
945
946/// Rewrite input constraint references after adding some output constraints.
947/// In the case where there is one output and one input and we add one output,
948/// we need to replace all operand references greater than or equal to 1:
949///     mov $0, $1
950///     mov eax, $1
951/// The result will be:
952///     mov $0, $2
953///     mov eax, $2
954static void rewriteInputConstraintReferences(unsigned FirstIn,
955                                             unsigned NumNewOuts,
956                                             std::string &AsmString) {
957  std::string Buf;
958  llvm::raw_string_ostream OS(Buf);
959  size_t Pos = 0;
960  while (Pos < AsmString.size()) {
961    size_t DollarStart = AsmString.find('$', Pos);
962    if (DollarStart == std::string::npos)
963      DollarStart = AsmString.size();
964    size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
965    if (DollarEnd == std::string::npos)
966      DollarEnd = AsmString.size();
967    OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
968    Pos = DollarEnd;
969    size_t NumDollars = DollarEnd - DollarStart;
970    if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
971      // We have an operand reference.
972      size_t DigitStart = Pos;
973      size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
974      if (DigitEnd == std::string::npos)
975        DigitEnd = AsmString.size();
976      StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
977      unsigned OperandIndex;
978      if (!OperandStr.getAsInteger(10, OperandIndex)) {
979        if (OperandIndex >= FirstIn)
980          OperandIndex += NumNewOuts;
981        OS << OperandIndex;
982      } else {
983        OS << OperandStr;
984      }
985      Pos = DigitEnd;
986    }
987  }
988  AsmString = std::move(OS.str());
989}
990
991/// Add output constraints for EAX:EDX because they are return registers.
992void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
993    CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
994    std::vector<llvm::Type *> &ResultRegTypes,
995    std::vector<llvm::Type *> &ResultTruncRegTypes,
996    std::vector<LValue> &ResultRegDests, std::string &AsmString,
997    unsigned NumOutputs) const {
998  uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
999
1000  // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1001  // larger.
1002  if (!Constraints.empty())
1003    Constraints += ',';
1004  if (RetWidth <= 32) {
1005    Constraints += "={eax}";
1006    ResultRegTypes.push_back(CGF.Int32Ty);
1007  } else {
1008    // Use the 'A' constraint for EAX:EDX.
1009    Constraints += "=A";
1010    ResultRegTypes.push_back(CGF.Int64Ty);
1011  }
1012
1013  // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1014  llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1015  ResultTruncRegTypes.push_back(CoerceTy);
1016
1017  // Coerce the integer by bitcasting the return slot pointer.
1018  ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(),
1019                                                  CoerceTy->getPointerTo()));
1020  ResultRegDests.push_back(ReturnSlot);
1021
1022  rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1023}
1024
1025/// shouldReturnTypeInRegister - Determine if the given type should be
1026/// returned in a register (for the Darwin and MCU ABI).
1027bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1028                                               ASTContext &Context) const {
1029  uint64_t Size = Context.getTypeSize(Ty);
1030
1031  // Type must be register sized.
1032  if (!isRegisterSize(Size))
1033    return false;
1034
1035  if (Ty->isVectorType()) {
1036    // 64- and 128- bit vectors inside structures are not returned in
1037    // registers.
1038    if (Size == 64 || Size == 128)
1039      return false;
1040
1041    return true;
1042  }
1043
1044  // If this is a builtin, pointer, enum, complex type, member pointer, or
1045  // member function pointer it is ok.
1046  if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
1047      Ty->isAnyComplexType() || Ty->isEnumeralType() ||
1048      Ty->isBlockPointerType() || Ty->isMemberPointerType())
1049    return true;
1050
1051  // Arrays are treated like records.
1052  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
1053    return shouldReturnTypeInRegister(AT->getElementType(), Context);
1054
1055  // Otherwise, it must be a record type.
1056  const RecordType *RT = Ty->getAs<RecordType>();
1057  if (!RT) return false;
1058
1059  // FIXME: Traverse bases here too.
1060
1061  // Structure types are passed in register if all fields would be
1062  // passed in a register.
1063  for (const auto *FD : RT->getDecl()->fields()) {
1064    // Empty fields are ignored.
1065    if (isEmptyField(Context, FD, true))
1066      continue;
1067
1068    // Check fields recursively.
1069    if (!shouldReturnTypeInRegister(FD->getType(), Context))
1070      return false;
1071  }
1072  return true;
1073}
1074
1075ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
1076  // If the return value is indirect, then the hidden argument is consuming one
1077  // integer register.
1078  if (State.FreeRegs) {
1079    --State.FreeRegs;
1080    return getNaturalAlignIndirectInReg(RetTy);
1081  }
1082  return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
1083}
1084
1085ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1086                                             CCState &State) const {
1087  if (RetTy->isVoidType())
1088    return ABIArgInfo::getIgnore();
1089
1090  const Type *Base = nullptr;
1091  uint64_t NumElts = 0;
1092  if (State.CC == llvm::CallingConv::X86_VectorCall &&
1093      isHomogeneousAggregate(RetTy, Base, NumElts)) {
1094    // The LLVM struct type for such an aggregate should lower properly.
1095    return ABIArgInfo::getDirect();
1096  }
1097
1098  if (const VectorType *VT = RetTy->getAs<VectorType>()) {
1099    // On Darwin, some vectors are returned in registers.
1100    if (IsDarwinVectorABI) {
1101      uint64_t Size = getContext().getTypeSize(RetTy);
1102
1103      // 128-bit vectors are a special case; they are returned in
1104      // registers and we need to make sure to pick a type the LLVM
1105      // backend will like.
1106      if (Size == 128)
1107        return ABIArgInfo::getDirect(llvm::VectorType::get(
1108                  llvm::Type::getInt64Ty(getVMContext()), 2));
1109
1110      // Always return in register if it fits in a general purpose
1111      // register, or if it is 64 bits and has a single element.
1112      if ((Size == 8 || Size == 16 || Size == 32) ||
1113          (Size == 64 && VT->getNumElements() == 1))
1114        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1115                                                            Size));
1116
1117      return getIndirectReturnResult(RetTy, State);
1118    }
1119
1120    return ABIArgInfo::getDirect();
1121  }
1122
1123  if (isAggregateTypeForABI(RetTy)) {
1124    if (const RecordType *RT = RetTy->getAs<RecordType>()) {
1125      // Structures with flexible arrays are always indirect.
1126      if (RT->getDecl()->hasFlexibleArrayMember())
1127        return getIndirectReturnResult(RetTy, State);
1128    }
1129
1130    // If specified, structs and unions are always indirect.
1131    if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1132      return getIndirectReturnResult(RetTy, State);
1133
1134    // Small structures which are register sized are generally returned
1135    // in a register.
1136    if (shouldReturnTypeInRegister(RetTy, getContext())) {
1137      uint64_t Size = getContext().getTypeSize(RetTy);
1138
1139      // As a special-case, if the struct is a "single-element" struct, and
1140      // the field is of type "float" or "double", return it in a
1141      // floating-point register. (MSVC does not apply this special case.)
1142      // We apply a similar transformation for pointer types to improve the
1143      // quality of the generated IR.
1144      if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
1145        if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
1146            || SeltTy->hasPointerRepresentation())
1147          return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1148
1149      // FIXME: We should be able to narrow this integer in cases with dead
1150      // padding.
1151      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
1152    }
1153
1154    return getIndirectReturnResult(RetTy, State);
1155  }
1156
1157  // Treat an enum type as its underlying type.
1158  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1159    RetTy = EnumTy->getDecl()->getIntegerType();
1160
1161  return (RetTy->isPromotableIntegerType() ?
1162          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1163}
1164
1165static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1166  return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1167}
1168
1169static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1170  const RecordType *RT = Ty->getAs<RecordType>();
1171  if (!RT)
1172    return 0;
1173  const RecordDecl *RD = RT->getDecl();
1174
1175  // If this is a C++ record, check the bases first.
1176  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1177    for (const auto &I : CXXRD->bases())
1178      if (!isRecordWithSSEVectorType(Context, I.getType()))
1179        return false;
1180
1181  for (const auto *i : RD->fields()) {
1182    QualType FT = i->getType();
1183
1184    if (isSSEVectorType(Context, FT))
1185      return true;
1186
1187    if (isRecordWithSSEVectorType(Context, FT))
1188      return true;
1189  }
1190
1191  return false;
1192}
1193
1194unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1195                                                 unsigned Align) const {
1196  // Otherwise, if the alignment is less than or equal to the minimum ABI
1197  // alignment, just use the default; the backend will handle this.
1198  if (Align <= MinABIStackAlignInBytes)
1199    return 0; // Use default alignment.
1200
1201  // On non-Darwin, the stack type alignment is always 4.
1202  if (!IsDarwinVectorABI) {
1203    // Set explicit alignment, since we may need to realign the top.
1204    return MinABIStackAlignInBytes;
1205  }
1206
1207  // Otherwise, if the type contains an SSE vector type, the alignment is 16.
1208  if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1209                      isRecordWithSSEVectorType(getContext(), Ty)))
1210    return 16;
1211
1212  return MinABIStackAlignInBytes;
1213}
1214
1215ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
1216                                            CCState &State) const {
1217  if (!ByVal) {
1218    if (State.FreeRegs) {
1219      --State.FreeRegs; // Non-byval indirects just use one pointer.
1220      return getNaturalAlignIndirectInReg(Ty);
1221    }
1222    return getNaturalAlignIndirect(Ty, false);
1223  }
1224
1225  // Compute the byval alignment.
1226  unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1227  unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1228  if (StackAlign == 0)
1229    return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
1230
1231  // If the stack alignment is less than the type alignment, realign the
1232  // argument.
1233  bool Realign = TypeAlign > StackAlign;
1234  return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1235                                 /*ByVal=*/true, Realign);
1236}
1237
1238X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1239  const Type *T = isSingleElementStruct(Ty, getContext());
1240  if (!T)
1241    T = Ty.getTypePtr();
1242
1243  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1244    BuiltinType::Kind K = BT->getKind();
1245    if (K == BuiltinType::Float || K == BuiltinType::Double)
1246      return Float;
1247  }
1248  return Integer;
1249}
1250
1251bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State,
1252                                   bool &NeedsPadding) const {
1253  NeedsPadding = false;
1254  if (!IsSoftFloatABI) {
1255    Class C = classify(Ty);
1256    if (C == Float)
1257      return false;
1258  }
1259
1260  unsigned Size = getContext().getTypeSize(Ty);
1261  unsigned SizeInRegs = (Size + 31) / 32;
1262
1263  if (SizeInRegs == 0)
1264    return false;
1265
1266  if (!IsMCUABI) {
1267    if (SizeInRegs > State.FreeRegs) {
1268      State.FreeRegs = 0;
1269      return false;
1270    }
1271  } else {
1272    // The MCU psABI allows passing parameters in-reg even if there are
1273    // earlier parameters that are passed on the stack. Also,
1274    // it does not allow passing >8-byte structs in-register,
1275    // even if there are 3 free registers available.
1276    if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1277      return false;
1278  }
1279
1280  State.FreeRegs -= SizeInRegs;
1281
1282  if (State.CC == llvm::CallingConv::X86_FastCall ||
1283      State.CC == llvm::CallingConv::X86_VectorCall) {
1284    if (Size > 32)
1285      return false;
1286
1287    if (Ty->isIntegralOrEnumerationType())
1288      return true;
1289
1290    if (Ty->isPointerType())
1291      return true;
1292
1293    if (Ty->isReferenceType())
1294      return true;
1295
1296    if (State.FreeRegs)
1297      NeedsPadding = true;
1298
1299    return false;
1300  }
1301
1302  return true;
1303}
1304
1305ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1306                                               CCState &State) const {
1307  // FIXME: Set alignment on indirect arguments.
1308
1309  Ty = useFirstFieldIfTransparentUnion(Ty);
1310
1311  // Check with the C++ ABI first.
1312  const RecordType *RT = Ty->getAs<RecordType>();
1313  if (RT) {
1314    CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1315    if (RAA == CGCXXABI::RAA_Indirect) {
1316      return getIndirectResult(Ty, false, State);
1317    } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1318      // The field index doesn't matter, we'll fix it up later.
1319      return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1320    }
1321  }
1322
1323  // vectorcall adds the concept of a homogenous vector aggregate, similar
1324  // to other targets.
1325  const Type *Base = nullptr;
1326  uint64_t NumElts = 0;
1327  if (State.CC == llvm::CallingConv::X86_VectorCall &&
1328      isHomogeneousAggregate(Ty, Base, NumElts)) {
1329    if (State.FreeSSERegs >= NumElts) {
1330      State.FreeSSERegs -= NumElts;
1331      if (Ty->isBuiltinType() || Ty->isVectorType())
1332        return ABIArgInfo::getDirect();
1333      return ABIArgInfo::getExpand();
1334    }
1335    return getIndirectResult(Ty, /*ByVal=*/false, State);
1336  }
1337
1338  if (isAggregateTypeForABI(Ty)) {
1339    if (RT) {
1340      // Structs are always byval on win32, regardless of what they contain.
1341      if (IsWin32StructABI)
1342        return getIndirectResult(Ty, true, State);
1343
1344      // Structures with flexible arrays are always indirect.
1345      if (RT->getDecl()->hasFlexibleArrayMember())
1346        return getIndirectResult(Ty, true, State);
1347    }
1348
1349    // Ignore empty structs/unions.
1350    if (isEmptyRecord(getContext(), Ty, true))
1351      return ABIArgInfo::getIgnore();
1352
1353    llvm::LLVMContext &LLVMContext = getVMContext();
1354    llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1355    bool NeedsPadding;
1356    if (shouldUseInReg(Ty, State, NeedsPadding)) {
1357      unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
1358      SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1359      llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1360      return ABIArgInfo::getDirectInReg(Result);
1361    }
1362    llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1363
1364    // Expand small (<= 128-bit) record types when we know that the stack layout
1365    // of those arguments will match the struct. This is important because the
1366    // LLVM backend isn't smart enough to remove byval, which inhibits many
1367    // optimizations.
1368    if (getContext().getTypeSize(Ty) <= 4*32 &&
1369        canExpandIndirectArgument(Ty, getContext()))
1370      return ABIArgInfo::getExpandWithPadding(
1371          State.CC == llvm::CallingConv::X86_FastCall ||
1372              State.CC == llvm::CallingConv::X86_VectorCall,
1373          PaddingType);
1374
1375    return getIndirectResult(Ty, true, State);
1376  }
1377
1378  if (const VectorType *VT = Ty->getAs<VectorType>()) {
1379    // On Darwin, some vectors are passed in memory, we handle this by passing
1380    // it as an i8/i16/i32/i64.
1381    if (IsDarwinVectorABI) {
1382      uint64_t Size = getContext().getTypeSize(Ty);
1383      if ((Size == 8 || Size == 16 || Size == 32) ||
1384          (Size == 64 && VT->getNumElements() == 1))
1385        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1386                                                            Size));
1387    }
1388
1389    if (IsX86_MMXType(CGT.ConvertType(Ty)))
1390      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1391
1392    return ABIArgInfo::getDirect();
1393  }
1394
1395
1396  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1397    Ty = EnumTy->getDecl()->getIntegerType();
1398
1399  bool NeedsPadding;
1400  bool InReg = shouldUseInReg(Ty, State, NeedsPadding);
1401
1402  if (Ty->isPromotableIntegerType()) {
1403    if (InReg)
1404      return ABIArgInfo::getExtendInReg();
1405    return ABIArgInfo::getExtend();
1406  }
1407  if (InReg)
1408    return ABIArgInfo::getDirectInReg();
1409  return ABIArgInfo::getDirect();
1410}
1411
1412void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1413  CCState State(FI.getCallingConvention());
1414  if (State.CC == llvm::CallingConv::X86_FastCall)
1415    State.FreeRegs = 2;
1416  else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1417    State.FreeRegs = 2;
1418    State.FreeSSERegs = 6;
1419  } else if (FI.getHasRegParm())
1420    State.FreeRegs = FI.getRegParm();
1421  else if (IsMCUABI)
1422    State.FreeRegs = 3;
1423  else
1424    State.FreeRegs = DefaultNumRegisterParameters;
1425
1426  if (!getCXXABI().classifyReturnType(FI)) {
1427    FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1428  } else if (FI.getReturnInfo().isIndirect()) {
1429    // The C++ ABI is not aware of register usage, so we have to check if the
1430    // return value was sret and put it in a register ourselves if appropriate.
1431    if (State.FreeRegs) {
1432      --State.FreeRegs;  // The sret parameter consumes a register.
1433      FI.getReturnInfo().setInReg(true);
1434    }
1435  }
1436
1437  // The chain argument effectively gives us another free register.
1438  if (FI.isChainCall())
1439    ++State.FreeRegs;
1440
1441  bool UsedInAlloca = false;
1442  for (auto &I : FI.arguments()) {
1443    I.info = classifyArgumentType(I.type, State);
1444    UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1445  }
1446
1447  // If we needed to use inalloca for any argument, do a second pass and rewrite
1448  // all the memory arguments to use inalloca.
1449  if (UsedInAlloca)
1450    rewriteWithInAlloca(FI);
1451}
1452
1453void
1454X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1455                                   CharUnits &StackOffset, ABIArgInfo &Info,
1456                                   QualType Type) const {
1457  // Arguments are always 4-byte-aligned.
1458  CharUnits FieldAlign = CharUnits::fromQuantity(4);
1459
1460  assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
1461  Info = ABIArgInfo::getInAlloca(FrameFields.size());
1462  FrameFields.push_back(CGT.ConvertTypeForMem(Type));
1463  StackOffset += getContext().getTypeSizeInChars(Type);
1464
1465  // Insert padding bytes to respect alignment.
1466  CharUnits FieldEnd = StackOffset;
1467  StackOffset = FieldEnd.RoundUpToAlignment(FieldAlign);
1468  if (StackOffset != FieldEnd) {
1469    CharUnits NumBytes = StackOffset - FieldEnd;
1470    llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1471    Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
1472    FrameFields.push_back(Ty);
1473  }
1474}
1475
1476static bool isArgInAlloca(const ABIArgInfo &Info) {
1477  // Leave ignored and inreg arguments alone.
1478  switch (Info.getKind()) {
1479  case ABIArgInfo::InAlloca:
1480    return true;
1481  case ABIArgInfo::Indirect:
1482    assert(Info.getIndirectByVal());
1483    return true;
1484  case ABIArgInfo::Ignore:
1485    return false;
1486  case ABIArgInfo::Direct:
1487  case ABIArgInfo::Extend:
1488  case ABIArgInfo::Expand:
1489    if (Info.getInReg())
1490      return false;
1491    return true;
1492  }
1493  llvm_unreachable("invalid enum");
1494}
1495
1496void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1497  assert(IsWin32StructABI && "inalloca only supported on win32");
1498
1499  // Build a packed struct type for all of the arguments in memory.
1500  SmallVector<llvm::Type *, 6> FrameFields;
1501
1502  // The stack alignment is always 4.
1503  CharUnits StackAlign = CharUnits::fromQuantity(4);
1504
1505  CharUnits StackOffset;
1506  CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1507
1508  // Put 'this' into the struct before 'sret', if necessary.
1509  bool IsThisCall =
1510      FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1511  ABIArgInfo &Ret = FI.getReturnInfo();
1512  if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1513      isArgInAlloca(I->info)) {
1514    addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1515    ++I;
1516  }
1517
1518  // Put the sret parameter into the inalloca struct if it's in memory.
1519  if (Ret.isIndirect() && !Ret.getInReg()) {
1520    CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1521    addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
1522    // On Windows, the hidden sret parameter is always returned in eax.
1523    Ret.setInAllocaSRet(IsWin32StructABI);
1524  }
1525
1526  // Skip the 'this' parameter in ecx.
1527  if (IsThisCall)
1528    ++I;
1529
1530  // Put arguments passed in memory into the struct.
1531  for (; I != E; ++I) {
1532    if (isArgInAlloca(I->info))
1533      addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1534  }
1535
1536  FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
1537                                        /*isPacked=*/true),
1538                  StackAlign);
1539}
1540
1541Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1542                                 Address VAListAddr, QualType Ty) const {
1543
1544  auto TypeInfo = getContext().getTypeInfoInChars(Ty);
1545
1546  // x86-32 changes the alignment of certain arguments on the stack.
1547  //
1548  // Just messing with TypeInfo like this works because we never pass
1549  // anything indirectly.
1550  TypeInfo.second = CharUnits::fromQuantity(
1551                getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
1552
1553  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1554                          TypeInfo, CharUnits::fromQuantity(4),
1555                          /*AllowHigherAlign*/ true);
1556}
1557
1558bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1559    const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1560  assert(Triple.getArch() == llvm::Triple::x86);
1561
1562  switch (Opts.getStructReturnConvention()) {
1563  case CodeGenOptions::SRCK_Default:
1564    break;
1565  case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
1566    return false;
1567  case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
1568    return true;
1569  }
1570
1571  if (Triple.isOSDarwin() || Triple.isOSIAMCU())
1572    return true;
1573
1574  switch (Triple.getOS()) {
1575  case llvm::Triple::DragonFly:
1576  case llvm::Triple::FreeBSD:
1577  case llvm::Triple::OpenBSD:
1578  case llvm::Triple::Bitrig:
1579  case llvm::Triple::Win32:
1580    return true;
1581  default:
1582    return false;
1583  }
1584}
1585
1586void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
1587                                                  llvm::GlobalValue *GV,
1588                                            CodeGen::CodeGenModule &CGM) const {
1589  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1590    if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1591      // Get the LLVM function.
1592      llvm::Function *Fn = cast<llvm::Function>(GV);
1593
1594      // Now add the 'alignstack' attribute with a value of 16.
1595      llvm::AttrBuilder B;
1596      B.addStackAlignmentAttr(16);
1597      Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
1598                      llvm::AttributeSet::get(CGM.getLLVMContext(),
1599                                              llvm::AttributeSet::FunctionIndex,
1600                                              B));
1601    }
1602  }
1603}
1604
1605bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
1606                                               CodeGen::CodeGenFunction &CGF,
1607                                               llvm::Value *Address) const {
1608  CodeGen::CGBuilderTy &Builder = CGF.Builder;
1609
1610  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
1611
1612  // 0-7 are the eight integer registers;  the order is different
1613  //   on Darwin (for EH), but the range is the same.
1614  // 8 is %eip.
1615  AssignToArrayRange(Builder, Address, Four8, 0, 8);
1616
1617  if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
1618    // 12-16 are st(0..4).  Not sure why we stop at 4.
1619    // These have size 16, which is sizeof(long double) on
1620    // platforms with 8-byte alignment for that type.
1621    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
1622    AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
1623
1624  } else {
1625    // 9 is %eflags, which doesn't get a size on Darwin for some
1626    // reason.
1627    Builder.CreateAlignedStore(
1628        Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
1629                               CharUnits::One());
1630
1631    // 11-16 are st(0..5).  Not sure why we stop at 5.
1632    // These have size 12, which is sizeof(long double) on
1633    // platforms with 4-byte alignment for that type.
1634    llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
1635    AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1636  }
1637
1638  return false;
1639}
1640
1641//===----------------------------------------------------------------------===//
1642// X86-64 ABI Implementation
1643//===----------------------------------------------------------------------===//
1644
1645
1646namespace {
1647/// The AVX ABI level for X86 targets.
1648enum class X86AVXABILevel {
1649  None,
1650  AVX,
1651  AVX512
1652};
1653
1654/// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
1655static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
1656  switch (AVXLevel) {
1657  case X86AVXABILevel::AVX512:
1658    return 512;
1659  case X86AVXABILevel::AVX:
1660    return 256;
1661  case X86AVXABILevel::None:
1662    return 128;
1663  }
1664  llvm_unreachable("Unknown AVXLevel");
1665}
1666
1667/// X86_64ABIInfo - The X86_64 ABI information.
1668class X86_64ABIInfo : public ABIInfo {
1669  enum Class {
1670    Integer = 0,
1671    SSE,
1672    SSEUp,
1673    X87,
1674    X87Up,
1675    ComplexX87,
1676    NoClass,
1677    Memory
1678  };
1679
1680  /// merge - Implement the X86_64 ABI merging algorithm.
1681  ///
1682  /// Merge an accumulating classification \arg Accum with a field
1683  /// classification \arg Field.
1684  ///
1685  /// \param Accum - The accumulating classification. This should
1686  /// always be either NoClass or the result of a previous merge
1687  /// call. In addition, this should never be Memory (the caller
1688  /// should just return Memory for the aggregate).
1689  static Class merge(Class Accum, Class Field);
1690
1691  /// postMerge - Implement the X86_64 ABI post merging algorithm.
1692  ///
1693  /// Post merger cleanup, reduces a malformed Hi and Lo pair to
1694  /// final MEMORY or SSE classes when necessary.
1695  ///
1696  /// \param AggregateSize - The size of the current aggregate in
1697  /// the classification process.
1698  ///
1699  /// \param Lo - The classification for the parts of the type
1700  /// residing in the low word of the containing object.
1701  ///
1702  /// \param Hi - The classification for the parts of the type
1703  /// residing in the higher words of the containing object.
1704  ///
1705  void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
1706
1707  /// classify - Determine the x86_64 register classes in which the
1708  /// given type T should be passed.
1709  ///
1710  /// \param Lo - The classification for the parts of the type
1711  /// residing in the low word of the containing object.
1712  ///
1713  /// \param Hi - The classification for the parts of the type
1714  /// residing in the high word of the containing object.
1715  ///
1716  /// \param OffsetBase - The bit offset of this type in the
1717  /// containing object.  Some parameters are classified different
1718  /// depending on whether they straddle an eightbyte boundary.
1719  ///
1720  /// \param isNamedArg - Whether the argument in question is a "named"
1721  /// argument, as used in AMD64-ABI 3.5.7.
1722  ///
1723  /// If a word is unused its result will be NoClass; if a type should
1724  /// be passed in Memory then at least the classification of \arg Lo
1725  /// will be Memory.
1726  ///
1727  /// The \arg Lo class will be NoClass iff the argument is ignored.
1728  ///
1729  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1730  /// also be ComplexX87.
1731  void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
1732                bool isNamedArg) const;
1733
1734  llvm::Type *GetByteVectorType(QualType Ty) const;
1735  llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
1736                                 unsigned IROffset, QualType SourceTy,
1737                                 unsigned SourceOffset) const;
1738  llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
1739                                     unsigned IROffset, QualType SourceTy,
1740                                     unsigned SourceOffset) const;
1741
1742  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1743  /// such that the argument will be returned in memory.
1744  ABIArgInfo getIndirectReturnResult(QualType Ty) const;
1745
1746  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1747  /// such that the argument will be passed in memory.
1748  ///
1749  /// \param freeIntRegs - The number of free integer registers remaining
1750  /// available.
1751  ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
1752
1753  ABIArgInfo classifyReturnType(QualType RetTy) const;
1754
1755  ABIArgInfo classifyArgumentType(QualType Ty,
1756                                  unsigned freeIntRegs,
1757                                  unsigned &neededInt,
1758                                  unsigned &neededSSE,
1759                                  bool isNamedArg) const;
1760
1761  bool IsIllegalVectorType(QualType Ty) const;
1762
1763  /// The 0.98 ABI revision clarified a lot of ambiguities,
1764  /// unfortunately in ways that were not always consistent with
1765  /// certain previous compilers.  In particular, platforms which
1766  /// required strict binary compatibility with older versions of GCC
1767  /// may need to exempt themselves.
1768  bool honorsRevision0_98() const {
1769    return !getTarget().getTriple().isOSDarwin();
1770  }
1771
1772  X86AVXABILevel AVXLevel;
1773  // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
1774  // 64-bit hardware.
1775  bool Has64BitPointers;
1776
1777public:
1778  X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
1779      ABIInfo(CGT), AVXLevel(AVXLevel),
1780      Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
1781  }
1782
1783  bool isPassedUsingAVXType(QualType type) const {
1784    unsigned neededInt, neededSSE;
1785    // The freeIntRegs argument doesn't matter here.
1786    ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
1787                                           /*isNamedArg*/true);
1788    if (info.isDirect()) {
1789      llvm::Type *ty = info.getCoerceToType();
1790      if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1791        return (vectorTy->getBitWidth() > 128);
1792    }
1793    return false;
1794  }
1795
1796  void computeInfo(CGFunctionInfo &FI) const override;
1797
1798  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1799                    QualType Ty) const override;
1800  Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
1801                      QualType Ty) const override;
1802
1803  bool has64BitPointers() const {
1804    return Has64BitPointers;
1805  }
1806};
1807
1808/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
1809class WinX86_64ABIInfo : public ABIInfo {
1810public:
1811  WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT)
1812      : ABIInfo(CGT),
1813        IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
1814
1815  void computeInfo(CGFunctionInfo &FI) const override;
1816
1817  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1818                    QualType Ty) const override;
1819
1820  bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1821    // FIXME: Assumes vectorcall is in use.
1822    return isX86VectorTypeForVectorCall(getContext(), Ty);
1823  }
1824
1825  bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1826                                         uint64_t NumMembers) const override {
1827    // FIXME: Assumes vectorcall is in use.
1828    return isX86VectorCallAggregateSmallEnough(NumMembers);
1829  }
1830
1831private:
1832  ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs,
1833                      bool IsReturnType) const;
1834
1835  bool IsMingw64;
1836};
1837
1838class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1839public:
1840  X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
1841      : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
1842
1843  const X86_64ABIInfo &getABIInfo() const {
1844    return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1845  }
1846
1847  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1848    return 7;
1849  }
1850
1851  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1852                               llvm::Value *Address) const override {
1853    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
1854
1855    // 0-15 are the 16 integer registers.
1856    // 16 is %rip.
1857    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1858    return false;
1859  }
1860
1861  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1862                                  StringRef Constraint,
1863                                  llvm::Type* Ty) const override {
1864    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1865  }
1866
1867  bool isNoProtoCallVariadic(const CallArgList &args,
1868                             const FunctionNoProtoType *fnType) const override {
1869    // The default CC on x86-64 sets %al to the number of SSA
1870    // registers used, and GCC sets this when calling an unprototyped
1871    // function, so we override the default behavior.  However, don't do
1872    // that when AVX types are involved: the ABI explicitly states it is
1873    // undefined, and it doesn't work in practice because of how the ABI
1874    // defines varargs anyway.
1875    if (fnType->getCallConv() == CC_C) {
1876      bool HasAVXType = false;
1877      for (CallArgList::const_iterator
1878             it = args.begin(), ie = args.end(); it != ie; ++it) {
1879        if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1880          HasAVXType = true;
1881          break;
1882        }
1883      }
1884
1885      if (!HasAVXType)
1886        return true;
1887    }
1888
1889    return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
1890  }
1891
1892  llvm::Constant *
1893  getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1894    unsigned Sig;
1895    if (getABIInfo().has64BitPointers())
1896      Sig = (0xeb << 0) |  // jmp rel8
1897            (0x0a << 8) |  //           .+0x0c
1898            ('F' << 16) |
1899            ('T' << 24);
1900    else
1901      Sig = (0xeb << 0) |  // jmp rel8
1902            (0x06 << 8) |  //           .+0x08
1903            ('F' << 16) |
1904            ('T' << 24);
1905    return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1906  }
1907};
1908
1909class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo {
1910public:
1911  PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
1912    : X86_64TargetCodeGenInfo(CGT, AVXLevel) {}
1913
1914  void getDependentLibraryOption(llvm::StringRef Lib,
1915                                 llvm::SmallString<24> &Opt) const override {
1916    Opt = "\01";
1917    // If the argument contains a space, enclose it in quotes.
1918    if (Lib.find(" ") != StringRef::npos)
1919      Opt += "\"" + Lib.str() + "\"";
1920    else
1921      Opt += Lib;
1922  }
1923};
1924
1925static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
1926  // If the argument does not end in .lib, automatically add the suffix.
1927  // If the argument contains a space, enclose it in quotes.
1928  // This matches the behavior of MSVC.
1929  bool Quote = (Lib.find(" ") != StringRef::npos);
1930  std::string ArgStr = Quote ? "\"" : "";
1931  ArgStr += Lib;
1932  if (!Lib.endswith_lower(".lib"))
1933    ArgStr += ".lib";
1934  ArgStr += Quote ? "\"" : "";
1935  return ArgStr;
1936}
1937
1938class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
1939public:
1940  WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1941        bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
1942        unsigned NumRegisterParameters)
1943    : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
1944        Win32StructABI, NumRegisterParameters, false) {}
1945
1946  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1947                           CodeGen::CodeGenModule &CGM) const override;
1948
1949  void getDependentLibraryOption(llvm::StringRef Lib,
1950                                 llvm::SmallString<24> &Opt) const override {
1951    Opt = "/DEFAULTLIB:";
1952    Opt += qualifyWindowsLibrary(Lib);
1953  }
1954
1955  void getDetectMismatchOption(llvm::StringRef Name,
1956                               llvm::StringRef Value,
1957                               llvm::SmallString<32> &Opt) const override {
1958    Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
1959  }
1960};
1961
1962static void addStackProbeSizeTargetAttribute(const Decl *D,
1963                                             llvm::GlobalValue *GV,
1964                                             CodeGen::CodeGenModule &CGM) {
1965  if (D && isa<FunctionDecl>(D)) {
1966    if (CGM.getCodeGenOpts().StackProbeSize != 4096) {
1967      llvm::Function *Fn = cast<llvm::Function>(GV);
1968
1969      Fn->addFnAttr("stack-probe-size",
1970                    llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
1971    }
1972  }
1973}
1974
1975void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D,
1976                                                     llvm::GlobalValue *GV,
1977                                            CodeGen::CodeGenModule &CGM) const {
1978  X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
1979
1980  addStackProbeSizeTargetAttribute(D, GV, CGM);
1981}
1982
1983class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1984public:
1985  WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
1986                             X86AVXABILevel AVXLevel)
1987      : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1988
1989  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1990                           CodeGen::CodeGenModule &CGM) const override;
1991
1992  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1993    return 7;
1994  }
1995
1996  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1997                               llvm::Value *Address) const override {
1998    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
1999
2000    // 0-15 are the 16 integer registers.
2001    // 16 is %rip.
2002    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2003    return false;
2004  }
2005
2006  void getDependentLibraryOption(llvm::StringRef Lib,
2007                                 llvm::SmallString<24> &Opt) const override {
2008    Opt = "/DEFAULTLIB:";
2009    Opt += qualifyWindowsLibrary(Lib);
2010  }
2011
2012  void getDetectMismatchOption(llvm::StringRef Name,
2013                               llvm::StringRef Value,
2014                               llvm::SmallString<32> &Opt) const override {
2015    Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2016  }
2017};
2018
2019void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D,
2020                                                     llvm::GlobalValue *GV,
2021                                            CodeGen::CodeGenModule &CGM) const {
2022  TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2023
2024  addStackProbeSizeTargetAttribute(D, GV, CGM);
2025}
2026}
2027
2028void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2029                              Class &Hi) const {
2030  // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2031  //
2032  // (a) If one of the classes is Memory, the whole argument is passed in
2033  //     memory.
2034  //
2035  // (b) If X87UP is not preceded by X87, the whole argument is passed in
2036  //     memory.
2037  //
2038  // (c) If the size of the aggregate exceeds two eightbytes and the first
2039  //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2040  //     argument is passed in memory. NOTE: This is necessary to keep the
2041  //     ABI working for processors that don't support the __m256 type.
2042  //
2043  // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2044  //
2045  // Some of these are enforced by the merging logic.  Others can arise
2046  // only with unions; for example:
2047  //   union { _Complex double; unsigned; }
2048  //
2049  // Note that clauses (b) and (c) were added in 0.98.
2050  //
2051  if (Hi == Memory)
2052    Lo = Memory;
2053  if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2054    Lo = Memory;
2055  if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2056    Lo = Memory;
2057  if (Hi == SSEUp && Lo != SSE)
2058    Hi = SSE;
2059}
2060
2061X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
2062  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2063  // classified recursively so that always two fields are
2064  // considered. The resulting class is calculated according to
2065  // the classes of the fields in the eightbyte:
2066  //
2067  // (a) If both classes are equal, this is the resulting class.
2068  //
2069  // (b) If one of the classes is NO_CLASS, the resulting class is
2070  // the other class.
2071  //
2072  // (c) If one of the classes is MEMORY, the result is the MEMORY
2073  // class.
2074  //
2075  // (d) If one of the classes is INTEGER, the result is the
2076  // INTEGER.
2077  //
2078  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2079  // MEMORY is used as class.
2080  //
2081  // (f) Otherwise class SSE is used.
2082
2083  // Accum should never be memory (we should have returned) or
2084  // ComplexX87 (because this cannot be passed in a structure).
2085  assert((Accum != Memory && Accum != ComplexX87) &&
2086         "Invalid accumulated classification during merge.");
2087  if (Accum == Field || Field == NoClass)
2088    return Accum;
2089  if (Field == Memory)
2090    return Memory;
2091  if (Accum == NoClass)
2092    return Field;
2093  if (Accum == Integer || Field == Integer)
2094    return Integer;
2095  if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2096      Accum == X87 || Accum == X87Up)
2097    return Memory;
2098  return SSE;
2099}
2100
2101void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
2102                             Class &Lo, Class &Hi, bool isNamedArg) const {
2103  // FIXME: This code can be simplified by introducing a simple value class for
2104  // Class pairs with appropriate constructor methods for the various
2105  // situations.
2106
2107  // FIXME: Some of the split computations are wrong; unaligned vectors
2108  // shouldn't be passed in registers for example, so there is no chance they
2109  // can straddle an eightbyte. Verify & simplify.
2110
2111  Lo = Hi = NoClass;
2112
2113  Class &Current = OffsetBase < 64 ? Lo : Hi;
2114  Current = Memory;
2115
2116  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2117    BuiltinType::Kind k = BT->getKind();
2118
2119    if (k == BuiltinType::Void) {
2120      Current = NoClass;
2121    } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2122      Lo = Integer;
2123      Hi = Integer;
2124    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2125      Current = Integer;
2126    } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
2127      Current = SSE;
2128    } else if (k == BuiltinType::LongDouble) {
2129      const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2130      if (LDF == &llvm::APFloat::IEEEquad) {
2131        Lo = SSE;
2132        Hi = SSEUp;
2133      } else if (LDF == &llvm::APFloat::x87DoubleExtended) {
2134        Lo = X87;
2135        Hi = X87Up;
2136      } else if (LDF == &llvm::APFloat::IEEEdouble) {
2137        Current = SSE;
2138      } else
2139        llvm_unreachable("unexpected long double representation!");
2140    }
2141    // FIXME: _Decimal32 and _Decimal64 are SSE.
2142    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
2143    return;
2144  }
2145
2146  if (const EnumType *ET = Ty->getAs<EnumType>()) {
2147    // Classify the underlying integer type.
2148    classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
2149    return;
2150  }
2151
2152  if (Ty->hasPointerRepresentation()) {
2153    Current = Integer;
2154    return;
2155  }
2156
2157  if (Ty->isMemberPointerType()) {
2158    if (Ty->isMemberFunctionPointerType()) {
2159      if (Has64BitPointers) {
2160        // If Has64BitPointers, this is an {i64, i64}, so classify both
2161        // Lo and Hi now.
2162        Lo = Hi = Integer;
2163      } else {
2164        // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2165        // straddles an eightbyte boundary, Hi should be classified as well.
2166        uint64_t EB_FuncPtr = (OffsetBase) / 64;
2167        uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2168        if (EB_FuncPtr != EB_ThisAdj) {
2169          Lo = Hi = Integer;
2170        } else {
2171          Current = Integer;
2172        }
2173      }
2174    } else {
2175      Current = Integer;
2176    }
2177    return;
2178  }
2179
2180  if (const VectorType *VT = Ty->getAs<VectorType>()) {
2181    uint64_t Size = getContext().getTypeSize(VT);
2182    if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2183      // gcc passes the following as integer:
2184      // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2185      // 2 bytes - <2 x char>, <1 x short>
2186      // 1 byte  - <1 x char>
2187      Current = Integer;
2188
2189      // If this type crosses an eightbyte boundary, it should be
2190      // split.
2191      uint64_t EB_Lo = (OffsetBase) / 64;
2192      uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2193      if (EB_Lo != EB_Hi)
2194        Hi = Lo;
2195    } else if (Size == 64) {
2196      // gcc passes <1 x double> in memory. :(
2197      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
2198        return;
2199
2200      // gcc passes <1 x long long> as INTEGER.
2201      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
2202          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2203          VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
2204          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
2205        Current = Integer;
2206      else
2207        Current = SSE;
2208
2209      // If this type crosses an eightbyte boundary, it should be
2210      // split.
2211      if (OffsetBase && OffsetBase != 64)
2212        Hi = Lo;
2213    } else if (Size == 128 ||
2214               (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
2215      // Arguments of 256-bits are split into four eightbyte chunks. The
2216      // least significant one belongs to class SSE and all the others to class
2217      // SSEUP. The original Lo and Hi design considers that types can't be
2218      // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2219      // This design isn't correct for 256-bits, but since there're no cases
2220      // where the upper parts would need to be inspected, avoid adding
2221      // complexity and just consider Hi to match the 64-256 part.
2222      //
2223      // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2224      // registers if they are "named", i.e. not part of the "..." of a
2225      // variadic function.
2226      //
2227      // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2228      // split into eight eightbyte chunks, one SSE and seven SSEUP.
2229      Lo = SSE;
2230      Hi = SSEUp;
2231    }
2232    return;
2233  }
2234
2235  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2236    QualType ET = getContext().getCanonicalType(CT->getElementType());
2237
2238    uint64_t Size = getContext().getTypeSize(Ty);
2239    if (ET->isIntegralOrEnumerationType()) {
2240      if (Size <= 64)
2241        Current = Integer;
2242      else if (Size <= 128)
2243        Lo = Hi = Integer;
2244    } else if (ET == getContext().FloatTy) {
2245      Current = SSE;
2246    } else if (ET == getContext().DoubleTy) {
2247      Lo = Hi = SSE;
2248    } else if (ET == getContext().LongDoubleTy) {
2249      const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2250      if (LDF == &llvm::APFloat::IEEEquad)
2251        Current = Memory;
2252      else if (LDF == &llvm::APFloat::x87DoubleExtended)
2253        Current = ComplexX87;
2254      else if (LDF == &llvm::APFloat::IEEEdouble)
2255        Lo = Hi = SSE;
2256      else
2257        llvm_unreachable("unexpected long double representation!");
2258    }
2259
2260    // If this complex type crosses an eightbyte boundary then it
2261    // should be split.
2262    uint64_t EB_Real = (OffsetBase) / 64;
2263    uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
2264    if (Hi == NoClass && EB_Real != EB_Imag)
2265      Hi = Lo;
2266
2267    return;
2268  }
2269
2270  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2271    // Arrays are treated like structures.
2272
2273    uint64_t Size = getContext().getTypeSize(Ty);
2274
2275    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2276    // than four eightbytes, ..., it has class MEMORY.
2277    if (Size > 256)
2278      return;
2279
2280    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2281    // fields, it has class MEMORY.
2282    //
2283    // Only need to check alignment of array base.
2284    if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
2285      return;
2286
2287    // Otherwise implement simplified merge. We could be smarter about
2288    // this, but it isn't worth it and would be harder to verify.
2289    Current = NoClass;
2290    uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
2291    uint64_t ArraySize = AT->getSize().getZExtValue();
2292
2293    // The only case a 256-bit wide vector could be used is when the array
2294    // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2295    // to work for sizes wider than 128, early check and fallback to memory.
2296    if (Size > 128 && EltSize != 256)
2297      return;
2298
2299    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2300      Class FieldLo, FieldHi;
2301      classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
2302      Lo = merge(Lo, FieldLo);
2303      Hi = merge(Hi, FieldHi);
2304      if (Lo == Memory || Hi == Memory)
2305        break;
2306    }
2307
2308    postMerge(Size, Lo, Hi);
2309    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
2310    return;
2311  }
2312
2313  if (const RecordType *RT = Ty->getAs<RecordType>()) {
2314    uint64_t Size = getContext().getTypeSize(Ty);
2315
2316    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2317    // than four eightbytes, ..., it has class MEMORY.
2318    if (Size > 256)
2319      return;
2320
2321    // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2322    // copy constructor or a non-trivial destructor, it is passed by invisible
2323    // reference.
2324    if (getRecordArgABI(RT, getCXXABI()))
2325      return;
2326
2327    const RecordDecl *RD = RT->getDecl();
2328
2329    // Assume variable sized types are passed in memory.
2330    if (RD->hasFlexibleArrayMember())
2331      return;
2332
2333    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2334
2335    // Reset Lo class, this will be recomputed.
2336    Current = NoClass;
2337
2338    // If this is a C++ record, classify the bases first.
2339    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2340      for (const auto &I : CXXRD->bases()) {
2341        assert(!I.isVirtual() && !I.getType()->isDependentType() &&
2342               "Unexpected base class!");
2343        const CXXRecordDecl *Base =
2344          cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
2345
2346        // Classify this field.
2347        //
2348        // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2349        // single eightbyte, each is classified separately. Each eightbyte gets
2350        // initialized to class NO_CLASS.
2351        Class FieldLo, FieldHi;
2352        uint64_t Offset =
2353          OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
2354        classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
2355        Lo = merge(Lo, FieldLo);
2356        Hi = merge(Hi, FieldHi);
2357        if (Lo == Memory || Hi == Memory) {
2358          postMerge(Size, Lo, Hi);
2359          return;
2360        }
2361      }
2362    }
2363
2364    // Classify the fields one at a time, merging the results.
2365    unsigned idx = 0;
2366    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2367           i != e; ++i, ++idx) {
2368      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2369      bool BitField = i->isBitField();
2370
2371      // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2372      // four eightbytes, or it contains unaligned fields, it has class MEMORY.
2373      //
2374      // The only case a 256-bit wide vector could be used is when the struct
2375      // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2376      // to work for sizes wider than 128, early check and fallback to memory.
2377      //
2378      if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
2379        Lo = Memory;
2380        postMerge(Size, Lo, Hi);
2381        return;
2382      }
2383      // Note, skip this test for bit-fields, see below.
2384      if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
2385        Lo = Memory;
2386        postMerge(Size, Lo, Hi);
2387        return;
2388      }
2389
2390      // Classify this field.
2391      //
2392      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2393      // exceeds a single eightbyte, each is classified
2394      // separately. Each eightbyte gets initialized to class
2395      // NO_CLASS.
2396      Class FieldLo, FieldHi;
2397
2398      // Bit-fields require special handling, they do not force the
2399      // structure to be passed in memory even if unaligned, and
2400      // therefore they can straddle an eightbyte.
2401      if (BitField) {
2402        // Ignore padding bit-fields.
2403        if (i->isUnnamedBitfield())
2404          continue;
2405
2406        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2407        uint64_t Size = i->getBitWidthValue(getContext());
2408
2409        uint64_t EB_Lo = Offset / 64;
2410        uint64_t EB_Hi = (Offset + Size - 1) / 64;
2411
2412        if (EB_Lo) {
2413          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2414          FieldLo = NoClass;
2415          FieldHi = Integer;
2416        } else {
2417          FieldLo = Integer;
2418          FieldHi = EB_Hi ? Integer : NoClass;
2419        }
2420      } else
2421        classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
2422      Lo = merge(Lo, FieldLo);
2423      Hi = merge(Hi, FieldHi);
2424      if (Lo == Memory || Hi == Memory)
2425        break;
2426    }
2427
2428    postMerge(Size, Lo, Hi);
2429  }
2430}
2431
2432ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
2433  // If this is a scalar LLVM value then assume LLVM will pass it in the right
2434  // place naturally.
2435  if (!isAggregateTypeForABI(Ty)) {
2436    // Treat an enum type as its underlying type.
2437    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2438      Ty = EnumTy->getDecl()->getIntegerType();
2439
2440    return (Ty->isPromotableIntegerType() ?
2441            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2442  }
2443
2444  return getNaturalAlignIndirect(Ty);
2445}
2446
2447bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2448  if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2449    uint64_t Size = getContext().getTypeSize(VecTy);
2450    unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
2451    if (Size <= 64 || Size > LargestVector)
2452      return true;
2453  }
2454
2455  return false;
2456}
2457
2458ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2459                                            unsigned freeIntRegs) const {
2460  // If this is a scalar LLVM value then assume LLVM will pass it in the right
2461  // place naturally.
2462  //
2463  // This assumption is optimistic, as there could be free registers available
2464  // when we need to pass this argument in memory, and LLVM could try to pass
2465  // the argument in the free register. This does not seem to happen currently,
2466  // but this code would be much safer if we could mark the argument with
2467  // 'onstack'. See PR12193.
2468  if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
2469    // Treat an enum type as its underlying type.
2470    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2471      Ty = EnumTy->getDecl()->getIntegerType();
2472
2473    return (Ty->isPromotableIntegerType() ?
2474            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2475  }
2476
2477  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
2478    return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
2479
2480  // Compute the byval alignment. We specify the alignment of the byval in all
2481  // cases so that the mid-level optimizer knows the alignment of the byval.
2482  unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
2483
2484  // Attempt to avoid passing indirect results using byval when possible. This
2485  // is important for good codegen.
2486  //
2487  // We do this by coercing the value into a scalar type which the backend can
2488  // handle naturally (i.e., without using byval).
2489  //
2490  // For simplicity, we currently only do this when we have exhausted all of the
2491  // free integer registers. Doing this when there are free integer registers
2492  // would require more care, as we would have to ensure that the coerced value
2493  // did not claim the unused register. That would require either reording the
2494  // arguments to the function (so that any subsequent inreg values came first),
2495  // or only doing this optimization when there were no following arguments that
2496  // might be inreg.
2497  //
2498  // We currently expect it to be rare (particularly in well written code) for
2499  // arguments to be passed on the stack when there are still free integer
2500  // registers available (this would typically imply large structs being passed
2501  // by value), so this seems like a fair tradeoff for now.
2502  //
2503  // We can revisit this if the backend grows support for 'onstack' parameter
2504  // attributes. See PR12193.
2505  if (freeIntRegs == 0) {
2506    uint64_t Size = getContext().getTypeSize(Ty);
2507
2508    // If this type fits in an eightbyte, coerce it into the matching integral
2509    // type, which will end up on the stack (with alignment 8).
2510    if (Align == 8 && Size <= 64)
2511      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2512                                                          Size));
2513  }
2514
2515  return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
2516}
2517
2518/// The ABI specifies that a value should be passed in a full vector XMM/YMM
2519/// register. Pick an LLVM IR type that will be passed as a vector register.
2520llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
2521  // Wrapper structs/arrays that only contain vectors are passed just like
2522  // vectors; strip them off if present.
2523  if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
2524    Ty = QualType(InnerTy, 0);
2525
2526  llvm::Type *IRType = CGT.ConvertType(Ty);
2527  if (isa<llvm::VectorType>(IRType) ||
2528      IRType->getTypeID() == llvm::Type::FP128TyID)
2529    return IRType;
2530
2531  // We couldn't find the preferred IR vector type for 'Ty'.
2532  uint64_t Size = getContext().getTypeSize(Ty);
2533  assert((Size == 128 || Size == 256) && "Invalid type found!");
2534
2535  // Return a LLVM IR vector type based on the size of 'Ty'.
2536  return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
2537                               Size / 64);
2538}
2539
2540/// BitsContainNoUserData - Return true if the specified [start,end) bit range
2541/// is known to either be off the end of the specified type or being in
2542/// alignment padding.  The user type specified is known to be at most 128 bits
2543/// in size, and have passed through X86_64ABIInfo::classify with a successful
2544/// classification that put one of the two halves in the INTEGER class.
2545///
2546/// It is conservatively correct to return false.
2547static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
2548                                  unsigned EndBit, ASTContext &Context) {
2549  // If the bytes being queried are off the end of the type, there is no user
2550  // data hiding here.  This handles analysis of builtins, vectors and other
2551  // types that don't contain interesting padding.
2552  unsigned TySize = (unsigned)Context.getTypeSize(Ty);
2553  if (TySize <= StartBit)
2554    return true;
2555
2556  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2557    unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
2558    unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
2559
2560    // Check each element to see if the element overlaps with the queried range.
2561    for (unsigned i = 0; i != NumElts; ++i) {
2562      // If the element is after the span we care about, then we're done..
2563      unsigned EltOffset = i*EltSize;
2564      if (EltOffset >= EndBit) break;
2565
2566      unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
2567      if (!BitsContainNoUserData(AT->getElementType(), EltStart,
2568                                 EndBit-EltOffset, Context))
2569        return false;
2570    }
2571    // If it overlaps no elements, then it is safe to process as padding.
2572    return true;
2573  }
2574
2575  if (const RecordType *RT = Ty->getAs<RecordType>()) {
2576    const RecordDecl *RD = RT->getDecl();
2577    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2578
2579    // If this is a C++ record, check the bases first.
2580    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2581      for (const auto &I : CXXRD->bases()) {
2582        assert(!I.isVirtual() && !I.getType()->isDependentType() &&
2583               "Unexpected base class!");
2584        const CXXRecordDecl *Base =
2585          cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
2586
2587        // If the base is after the span we care about, ignore it.
2588        unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
2589        if (BaseOffset >= EndBit) continue;
2590
2591        unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
2592        if (!BitsContainNoUserData(I.getType(), BaseStart,
2593                                   EndBit-BaseOffset, Context))
2594          return false;
2595      }
2596    }
2597
2598    // Verify that no field has data that overlaps the region of interest.  Yes
2599    // this could be sped up a lot by being smarter about queried fields,
2600    // however we're only looking at structs up to 16 bytes, so we don't care
2601    // much.
2602    unsigned idx = 0;
2603    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2604         i != e; ++i, ++idx) {
2605      unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
2606
2607      // If we found a field after the region we care about, then we're done.
2608      if (FieldOffset >= EndBit) break;
2609
2610      unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
2611      if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
2612                                 Context))
2613        return false;
2614    }
2615
2616    // If nothing in this record overlapped the area of interest, then we're
2617    // clean.
2618    return true;
2619  }
2620
2621  return false;
2622}
2623
2624/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
2625/// float member at the specified offset.  For example, {int,{float}} has a
2626/// float at offset 4.  It is conservatively correct for this routine to return
2627/// false.
2628static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
2629                                  const llvm::DataLayout &TD) {
2630  // Base case if we find a float.
2631  if (IROffset == 0 && IRType->isFloatTy())
2632    return true;
2633
2634  // If this is a struct, recurse into the field at the specified offset.
2635  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
2636    const llvm::StructLayout *SL = TD.getStructLayout(STy);
2637    unsigned Elt = SL->getElementContainingOffset(IROffset);
2638    IROffset -= SL->getElementOffset(Elt);
2639    return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
2640  }
2641
2642  // If this is an array, recurse into the field at the specified offset.
2643  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2644    llvm::Type *EltTy = ATy->getElementType();
2645    unsigned EltSize = TD.getTypeAllocSize(EltTy);
2646    IROffset -= IROffset/EltSize*EltSize;
2647    return ContainsFloatAtOffset(EltTy, IROffset, TD);
2648  }
2649
2650  return false;
2651}
2652
2653
2654/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
2655/// low 8 bytes of an XMM register, corresponding to the SSE class.
2656llvm::Type *X86_64ABIInfo::
2657GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
2658                   QualType SourceTy, unsigned SourceOffset) const {
2659  // The only three choices we have are either double, <2 x float>, or float. We
2660  // pass as float if the last 4 bytes is just padding.  This happens for
2661  // structs that contain 3 floats.
2662  if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
2663                            SourceOffset*8+64, getContext()))
2664    return llvm::Type::getFloatTy(getVMContext());
2665
2666  // We want to pass as <2 x float> if the LLVM IR type contains a float at
2667  // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
2668  // case.
2669  if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
2670      ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
2671    return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
2672
2673  return llvm::Type::getDoubleTy(getVMContext());
2674}
2675
2676
2677/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
2678/// an 8-byte GPR.  This means that we either have a scalar or we are talking
2679/// about the high or low part of an up-to-16-byte struct.  This routine picks
2680/// the best LLVM IR type to represent this, which may be i64 or may be anything
2681/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
2682/// etc).
2683///
2684/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
2685/// the source type.  IROffset is an offset in bytes into the LLVM IR type that
2686/// the 8-byte value references.  PrefType may be null.
2687///
2688/// SourceTy is the source-level type for the entire argument.  SourceOffset is
2689/// an offset into this that we're processing (which is always either 0 or 8).
2690///
2691llvm::Type *X86_64ABIInfo::
2692GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
2693                       QualType SourceTy, unsigned SourceOffset) const {
2694  // If we're dealing with an un-offset LLVM IR type, then it means that we're
2695  // returning an 8-byte unit starting with it.  See if we can safely use it.
2696  if (IROffset == 0) {
2697    // Pointers and int64's always fill the 8-byte unit.
2698    if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
2699        IRType->isIntegerTy(64))
2700      return IRType;
2701
2702    // If we have a 1/2/4-byte integer, we can use it only if the rest of the
2703    // goodness in the source type is just tail padding.  This is allowed to
2704    // kick in for struct {double,int} on the int, but not on
2705    // struct{double,int,int} because we wouldn't return the second int.  We
2706    // have to do this analysis on the source type because we can't depend on
2707    // unions being lowered a specific way etc.
2708    if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
2709        IRType->isIntegerTy(32) ||
2710        (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
2711      unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
2712          cast<llvm::IntegerType>(IRType)->getBitWidth();
2713
2714      if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
2715                                SourceOffset*8+64, getContext()))
2716        return IRType;
2717    }
2718  }
2719
2720  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
2721    // If this is a struct, recurse into the field at the specified offset.
2722    const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
2723    if (IROffset < SL->getSizeInBytes()) {
2724      unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
2725      IROffset -= SL->getElementOffset(FieldIdx);
2726
2727      return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
2728                                    SourceTy, SourceOffset);
2729    }
2730  }
2731
2732  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
2733    llvm::Type *EltTy = ATy->getElementType();
2734    unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
2735    unsigned EltOffset = IROffset/EltSize*EltSize;
2736    return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
2737                                  SourceOffset);
2738  }
2739
2740  // Okay, we don't have any better idea of what to pass, so we pass this in an
2741  // integer register that isn't too big to fit the rest of the struct.
2742  unsigned TySizeInBytes =
2743    (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
2744
2745  assert(TySizeInBytes != SourceOffset && "Empty field?");
2746
2747  // It is always safe to classify this as an integer type up to i64 that
2748  // isn't larger than the structure.
2749  return llvm::IntegerType::get(getVMContext(),
2750                                std::min(TySizeInBytes-SourceOffset, 8U)*8);
2751}
2752
2753
2754/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
2755/// be used as elements of a two register pair to pass or return, return a
2756/// first class aggregate to represent them.  For example, if the low part of
2757/// a by-value argument should be passed as i32* and the high part as float,
2758/// return {i32*, float}.
2759static llvm::Type *
2760GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
2761                           const llvm::DataLayout &TD) {
2762  // In order to correctly satisfy the ABI, we need to the high part to start
2763  // at offset 8.  If the high and low parts we inferred are both 4-byte types
2764  // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
2765  // the second element at offset 8.  Check for this:
2766  unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
2767  unsigned HiAlign = TD.getABITypeAlignment(Hi);
2768  unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign);
2769  assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
2770
2771  // To handle this, we have to increase the size of the low part so that the
2772  // second element will start at an 8 byte offset.  We can't increase the size
2773  // of the second element because it might make us access off the end of the
2774  // struct.
2775  if (HiStart != 8) {
2776    // There are usually two sorts of types the ABI generation code can produce
2777    // for the low part of a pair that aren't 8 bytes in size: float or
2778    // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
2779    // NaCl).
2780    // Promote these to a larger type.
2781    if (Lo->isFloatTy())
2782      Lo = llvm::Type::getDoubleTy(Lo->getContext());
2783    else {
2784      assert((Lo->isIntegerTy() || Lo->isPointerTy())
2785             && "Invalid/unknown lo type");
2786      Lo = llvm::Type::getInt64Ty(Lo->getContext());
2787    }
2788  }
2789
2790  llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr);
2791
2792
2793  // Verify that the second element is at an 8-byte offset.
2794  assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
2795         "Invalid x86-64 argument pair!");
2796  return Result;
2797}
2798
2799ABIArgInfo X86_64ABIInfo::
2800classifyReturnType(QualType RetTy) const {
2801  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
2802  // classification algorithm.
2803  X86_64ABIInfo::Class Lo, Hi;
2804  classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
2805
2806  // Check some invariants.
2807  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2808  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2809
2810  llvm::Type *ResType = nullptr;
2811  switch (Lo) {
2812  case NoClass:
2813    if (Hi == NoClass)
2814      return ABIArgInfo::getIgnore();
2815    // If the low part is just padding, it takes no register, leave ResType
2816    // null.
2817    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2818           "Unknown missing lo part");
2819    break;
2820
2821  case SSEUp:
2822  case X87Up:
2823    llvm_unreachable("Invalid classification for lo word.");
2824
2825    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
2826    // hidden argument.
2827  case Memory:
2828    return getIndirectReturnResult(RetTy);
2829
2830    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
2831    // available register of the sequence %rax, %rdx is used.
2832  case Integer:
2833    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
2834
2835    // If we have a sign or zero extended integer, make sure to return Extend
2836    // so that the parameter gets the right LLVM IR attributes.
2837    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2838      // Treat an enum type as its underlying type.
2839      if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2840        RetTy = EnumTy->getDecl()->getIntegerType();
2841
2842      if (RetTy->isIntegralOrEnumerationType() &&
2843          RetTy->isPromotableIntegerType())
2844        return ABIArgInfo::getExtend();
2845    }
2846    break;
2847
2848    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2849    // available SSE register of the sequence %xmm0, %xmm1 is used.
2850  case SSE:
2851    ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
2852    break;
2853
2854    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2855    // returned on the X87 stack in %st0 as 80-bit x87 number.
2856  case X87:
2857    ResType = llvm::Type::getX86_FP80Ty(getVMContext());
2858    break;
2859
2860    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2861    // part of the value is returned in %st0 and the imaginary part in
2862    // %st1.
2863  case ComplexX87:
2864    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
2865    ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
2866                                    llvm::Type::getX86_FP80Ty(getVMContext()),
2867                                    nullptr);
2868    break;
2869  }
2870
2871  llvm::Type *HighPart = nullptr;
2872  switch (Hi) {
2873    // Memory was handled previously and X87 should
2874    // never occur as a hi class.
2875  case Memory:
2876  case X87:
2877    llvm_unreachable("Invalid classification for hi word.");
2878
2879  case ComplexX87: // Previously handled.
2880  case NoClass:
2881    break;
2882
2883  case Integer:
2884    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2885    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
2886      return ABIArgInfo::getDirect(HighPart, 8);
2887    break;
2888  case SSE:
2889    HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2890    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
2891      return ABIArgInfo::getDirect(HighPart, 8);
2892    break;
2893
2894    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
2895    // is passed in the next available eightbyte chunk if the last used
2896    // vector register.
2897    //
2898    // SSEUP should always be preceded by SSE, just widen.
2899  case SSEUp:
2900    assert(Lo == SSE && "Unexpected SSEUp classification.");
2901    ResType = GetByteVectorType(RetTy);
2902    break;
2903
2904    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2905    // returned together with the previous X87 value in %st0.
2906  case X87Up:
2907    // If X87Up is preceded by X87, we don't need to do
2908    // anything. However, in some cases with unions it may not be
2909    // preceded by X87. In such situations we follow gcc and pass the
2910    // extra bits in an SSE reg.
2911    if (Lo != X87) {
2912      HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
2913      if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
2914        return ABIArgInfo::getDirect(HighPart, 8);
2915    }
2916    break;
2917  }
2918
2919  // If a high part was specified, merge it together with the low part.  It is
2920  // known to pass in the high eightbyte of the result.  We do this by forming a
2921  // first class struct aggregate with the high and low part: {low, high}
2922  if (HighPart)
2923    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
2924
2925  return ABIArgInfo::getDirect(ResType);
2926}
2927
2928ABIArgInfo X86_64ABIInfo::classifyArgumentType(
2929  QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
2930  bool isNamedArg)
2931  const
2932{
2933  Ty = useFirstFieldIfTransparentUnion(Ty);
2934
2935  X86_64ABIInfo::Class Lo, Hi;
2936  classify(Ty, 0, Lo, Hi, isNamedArg);
2937
2938  // Check some invariants.
2939  // FIXME: Enforce these by construction.
2940  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2941  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2942
2943  neededInt = 0;
2944  neededSSE = 0;
2945  llvm::Type *ResType = nullptr;
2946  switch (Lo) {
2947  case NoClass:
2948    if (Hi == NoClass)
2949      return ABIArgInfo::getIgnore();
2950    // If the low part is just padding, it takes no register, leave ResType
2951    // null.
2952    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2953           "Unknown missing lo part");
2954    break;
2955
2956    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2957    // on the stack.
2958  case Memory:
2959
2960    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2961    // COMPLEX_X87, it is passed in memory.
2962  case X87:
2963  case ComplexX87:
2964    if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
2965      ++neededInt;
2966    return getIndirectResult(Ty, freeIntRegs);
2967
2968  case SSEUp:
2969  case X87Up:
2970    llvm_unreachable("Invalid classification for lo word.");
2971
2972    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2973    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2974    // and %r9 is used.
2975  case Integer:
2976    ++neededInt;
2977
2978    // Pick an 8-byte type based on the preferred type.
2979    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
2980
2981    // If we have a sign or zero extended integer, make sure to return Extend
2982    // so that the parameter gets the right LLVM IR attributes.
2983    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2984      // Treat an enum type as its underlying type.
2985      if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2986        Ty = EnumTy->getDecl()->getIntegerType();
2987
2988      if (Ty->isIntegralOrEnumerationType() &&
2989          Ty->isPromotableIntegerType())
2990        return ABIArgInfo::getExtend();
2991    }
2992
2993    break;
2994
2995    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2996    // available SSE register is used, the registers are taken in the
2997    // order from %xmm0 to %xmm7.
2998  case SSE: {
2999    llvm::Type *IRType = CGT.ConvertType(Ty);
3000    ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3001    ++neededSSE;
3002    break;
3003  }
3004  }
3005
3006  llvm::Type *HighPart = nullptr;
3007  switch (Hi) {
3008    // Memory was handled previously, ComplexX87 and X87 should
3009    // never occur as hi classes, and X87Up must be preceded by X87,
3010    // which is passed in memory.
3011  case Memory:
3012  case X87:
3013  case ComplexX87:
3014    llvm_unreachable("Invalid classification for hi word.");
3015
3016  case NoClass: break;
3017
3018  case Integer:
3019    ++neededInt;
3020    // Pick an 8-byte type based on the preferred type.
3021    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3022
3023    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3024      return ABIArgInfo::getDirect(HighPart, 8);
3025    break;
3026
3027    // X87Up generally doesn't occur here (long double is passed in
3028    // memory), except in situations involving unions.
3029  case X87Up:
3030  case SSE:
3031    HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3032
3033    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3034      return ABIArgInfo::getDirect(HighPart, 8);
3035
3036    ++neededSSE;
3037    break;
3038
3039    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3040    // eightbyte is passed in the upper half of the last used SSE
3041    // register.  This only happens when 128-bit vectors are passed.
3042  case SSEUp:
3043    assert(Lo == SSE && "Unexpected SSEUp classification");
3044    ResType = GetByteVectorType(Ty);
3045    break;
3046  }
3047
3048  // If a high part was specified, merge it together with the low part.  It is
3049  // known to pass in the high eightbyte of the result.  We do this by forming a
3050  // first class struct aggregate with the high and low part: {low, high}
3051  if (HighPart)
3052    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3053
3054  return ABIArgInfo::getDirect(ResType);
3055}
3056
3057void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3058
3059  if (!getCXXABI().classifyReturnType(FI))
3060    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3061
3062  // Keep track of the number of assigned registers.
3063  unsigned freeIntRegs = 6, freeSSERegs = 8;
3064
3065  // If the return value is indirect, then the hidden argument is consuming one
3066  // integer register.
3067  if (FI.getReturnInfo().isIndirect())
3068    --freeIntRegs;
3069
3070  // The chain argument effectively gives us another free register.
3071  if (FI.isChainCall())
3072    ++freeIntRegs;
3073
3074  unsigned NumRequiredArgs = FI.getNumRequiredArgs();
3075  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3076  // get assigned (in left-to-right order) for passing as follows...
3077  unsigned ArgNo = 0;
3078  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3079       it != ie; ++it, ++ArgNo) {
3080    bool IsNamedArg = ArgNo < NumRequiredArgs;
3081
3082    unsigned neededInt, neededSSE;
3083    it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
3084                                    neededSSE, IsNamedArg);
3085
3086    // AMD64-ABI 3.2.3p3: If there are no registers available for any
3087    // eightbyte of an argument, the whole argument is passed on the
3088    // stack. If registers have already been assigned for some
3089    // eightbytes of such an argument, the assignments get reverted.
3090    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
3091      freeIntRegs -= neededInt;
3092      freeSSERegs -= neededSSE;
3093    } else {
3094      it->info = getIndirectResult(it->type, freeIntRegs);
3095    }
3096  }
3097}
3098
3099static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3100                                         Address VAListAddr, QualType Ty) {
3101  Address overflow_arg_area_p = CGF.Builder.CreateStructGEP(
3102      VAListAddr, 2, CharUnits::fromQuantity(8), "overflow_arg_area_p");
3103  llvm::Value *overflow_arg_area =
3104    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3105
3106  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3107  // byte boundary if alignment needed by type exceeds 8 byte boundary.
3108  // It isn't stated explicitly in the standard, but in practice we use
3109  // alignment greater than 16 where necessary.
3110  CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3111  if (Align > CharUnits::fromQuantity(8)) {
3112    overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3113                                                      Align);
3114  }
3115
3116  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
3117  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3118  llvm::Value *Res =
3119    CGF.Builder.CreateBitCast(overflow_arg_area,
3120                              llvm::PointerType::getUnqual(LTy));
3121
3122  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3123  // l->overflow_arg_area + sizeof(type).
3124  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3125  // an 8 byte boundary.
3126
3127  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
3128  llvm::Value *Offset =
3129      llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
3130  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3131                                            "overflow_arg_area.next");
3132  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3133
3134  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
3135  return Address(Res, Align);
3136}
3137
3138Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3139                                 QualType Ty) const {
3140  // Assume that va_list type is correct; should be pointer to LLVM type:
3141  // struct {
3142  //   i32 gp_offset;
3143  //   i32 fp_offset;
3144  //   i8* overflow_arg_area;
3145  //   i8* reg_save_area;
3146  // };
3147  unsigned neededInt, neededSSE;
3148
3149  Ty = getContext().getCanonicalType(Ty);
3150  ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
3151                                       /*isNamedArg*/false);
3152
3153  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3154  // in the registers. If not go to step 7.
3155  if (!neededInt && !neededSSE)
3156    return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3157
3158  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3159  // general purpose registers needed to pass type and num_fp to hold
3160  // the number of floating point registers needed.
3161
3162  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3163  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3164  // l->fp_offset > 304 - num_fp * 16 go to step 7.
3165  //
3166  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3167  // register save space).
3168
3169  llvm::Value *InRegs = nullptr;
3170  Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3171  llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
3172  if (neededInt) {
3173    gp_offset_p =
3174        CGF.Builder.CreateStructGEP(VAListAddr, 0, CharUnits::Zero(),
3175                                    "gp_offset_p");
3176    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
3177    InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3178    InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
3179  }
3180
3181  if (neededSSE) {
3182    fp_offset_p =
3183        CGF.Builder.CreateStructGEP(VAListAddr, 1, CharUnits::fromQuantity(4),
3184                                    "fp_offset_p");
3185    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3186    llvm::Value *FitsInFP =
3187      llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3188    FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
3189    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3190  }
3191
3192  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3193  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3194  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3195  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3196
3197  // Emit code to load the value if it was passed in registers.
3198
3199  CGF.EmitBlock(InRegBlock);
3200
3201  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3202  // an offset of l->gp_offset and/or l->fp_offset. This may require
3203  // copying to a temporary location in case the parameter is passed
3204  // in different register classes or requires an alignment greater
3205  // than 8 for general purpose registers and 16 for XMM registers.
3206  //
3207  // FIXME: This really results in shameful code when we end up needing to
3208  // collect arguments from different places; often what should result in a
3209  // simple assembling of a structure from scattered addresses has many more
3210  // loads than necessary. Can we clean this up?
3211  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3212  llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3213      CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(16)),
3214                                  "reg_save_area");
3215
3216  Address RegAddr = Address::invalid();
3217  if (neededInt && neededSSE) {
3218    // FIXME: Cleanup.
3219    assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
3220    llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
3221    Address Tmp = CGF.CreateMemTemp(Ty);
3222    Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3223    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
3224    llvm::Type *TyLo = ST->getElementType(0);
3225    llvm::Type *TyHi = ST->getElementType(1);
3226    assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
3227           "Unexpected ABI info for mixed regs");
3228    llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3229    llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
3230    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3231    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
3232    llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3233    llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
3234
3235    // Copy the first element.
3236    llvm::Value *V =
3237      CGF.Builder.CreateDefaultAlignedLoad(
3238                               CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
3239    CGF.Builder.CreateStore(V,
3240                    CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3241
3242    // Copy the second element.
3243    V = CGF.Builder.CreateDefaultAlignedLoad(
3244                               CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
3245    CharUnits Offset = CharUnits::fromQuantity(
3246                   getDataLayout().getStructLayout(ST)->getElementOffset(1));
3247    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1, Offset));
3248
3249    RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
3250  } else if (neededInt) {
3251    RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3252                      CharUnits::fromQuantity(8));
3253    RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
3254
3255    // Copy to a temporary if necessary to ensure the appropriate alignment.
3256    std::pair<CharUnits, CharUnits> SizeAlign =
3257        getContext().getTypeInfoInChars(Ty);
3258    uint64_t TySize = SizeAlign.first.getQuantity();
3259    CharUnits TyAlign = SizeAlign.second;
3260
3261    // Copy into a temporary if the type is more aligned than the
3262    // register save area.
3263    if (TyAlign.getQuantity() > 8) {
3264      Address Tmp = CGF.CreateMemTemp(Ty);
3265      CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
3266      RegAddr = Tmp;
3267    }
3268
3269  } else if (neededSSE == 1) {
3270    RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3271                      CharUnits::fromQuantity(16));
3272    RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
3273  } else {
3274    assert(neededSSE == 2 && "Invalid number of needed registers!");
3275    // SSE registers are spaced 16 bytes apart in the register save
3276    // area, we need to collect the two eightbytes together.
3277    // The ABI isn't explicit about this, but it seems reasonable
3278    // to assume that the slots are 16-byte aligned, since the stack is
3279    // naturally 16-byte aligned and the prologue is expected to store
3280    // all the SSE registers to the RSA.
3281    Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3282                                CharUnits::fromQuantity(16));
3283    Address RegAddrHi =
3284      CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3285                                             CharUnits::fromQuantity(16));
3286    llvm::Type *DoubleTy = CGF.DoubleTy;
3287    llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr);
3288    llvm::Value *V;
3289    Address Tmp = CGF.CreateMemTemp(Ty);
3290    Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3291    V = CGF.Builder.CreateLoad(
3292                   CGF.Builder.CreateElementBitCast(RegAddrLo, DoubleTy));
3293    CGF.Builder.CreateStore(V,
3294                   CGF.Builder.CreateStructGEP(Tmp, 0, CharUnits::Zero()));
3295    V = CGF.Builder.CreateLoad(
3296                   CGF.Builder.CreateElementBitCast(RegAddrHi, DoubleTy));
3297    CGF.Builder.CreateStore(V,
3298          CGF.Builder.CreateStructGEP(Tmp, 1, CharUnits::fromQuantity(8)));
3299
3300    RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
3301  }
3302
3303  // AMD64-ABI 3.5.7p5: Step 5. Set:
3304  // l->gp_offset = l->gp_offset + num_gp * 8
3305  // l->fp_offset = l->fp_offset + num_fp * 16.
3306  if (neededInt) {
3307    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
3308    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3309                            gp_offset_p);
3310  }
3311  if (neededSSE) {
3312    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
3313    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3314                            fp_offset_p);
3315  }
3316  CGF.EmitBranch(ContBlock);
3317
3318  // Emit code to load the value if it was passed in memory.
3319
3320  CGF.EmitBlock(InMemBlock);
3321  Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3322
3323  // Return the appropriate result.
3324
3325  CGF.EmitBlock(ContBlock);
3326  Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3327                                 "vaarg.addr");
3328  return ResAddr;
3329}
3330
3331Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3332                                   QualType Ty) const {
3333  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3334                          CGF.getContext().getTypeInfoInChars(Ty),
3335                          CharUnits::fromQuantity(8),
3336                          /*allowHigherAlign*/ false);
3337}
3338
3339ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
3340                                      bool IsReturnType) const {
3341
3342  if (Ty->isVoidType())
3343    return ABIArgInfo::getIgnore();
3344
3345  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3346    Ty = EnumTy->getDecl()->getIntegerType();
3347
3348  TypeInfo Info = getContext().getTypeInfo(Ty);
3349  uint64_t Width = Info.Width;
3350  CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
3351
3352  const RecordType *RT = Ty->getAs<RecordType>();
3353  if (RT) {
3354    if (!IsReturnType) {
3355      if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
3356        return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3357    }
3358
3359    if (RT->getDecl()->hasFlexibleArrayMember())
3360      return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
3361
3362  }
3363
3364  // vectorcall adds the concept of a homogenous vector aggregate, similar to
3365  // other targets.
3366  const Type *Base = nullptr;
3367  uint64_t NumElts = 0;
3368  if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) {
3369    if (FreeSSERegs >= NumElts) {
3370      FreeSSERegs -= NumElts;
3371      if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3372        return ABIArgInfo::getDirect();
3373      return ABIArgInfo::getExpand();
3374    }
3375    return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3376  }
3377
3378
3379  if (Ty->isMemberPointerType()) {
3380    // If the member pointer is represented by an LLVM int or ptr, pass it
3381    // directly.
3382    llvm::Type *LLTy = CGT.ConvertType(Ty);
3383    if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3384      return ABIArgInfo::getDirect();
3385  }
3386
3387  if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
3388    // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3389    // not 1, 2, 4, or 8 bytes, must be passed by reference."
3390    if (Width > 64 || !llvm::isPowerOf2_64(Width))
3391      return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
3392
3393    // Otherwise, coerce it to a small integer.
3394    return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
3395  }
3396
3397  // Bool type is always extended to the ABI, other builtin types are not
3398  // extended.
3399  const BuiltinType *BT = Ty->getAs<BuiltinType>();
3400  if (BT && BT->getKind() == BuiltinType::Bool)
3401    return ABIArgInfo::getExtend();
3402
3403  // Mingw64 GCC uses the old 80 bit extended precision floating point unit. It
3404  // passes them indirectly through memory.
3405  if (IsMingw64 && BT && BT->getKind() == BuiltinType::LongDouble) {
3406    const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
3407    if (LDF == &llvm::APFloat::x87DoubleExtended)
3408      return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3409  }
3410
3411  return ABIArgInfo::getDirect();
3412}
3413
3414void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3415  bool IsVectorCall =
3416      FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall;
3417
3418  // We can use up to 4 SSE return registers with vectorcall.
3419  unsigned FreeSSERegs = IsVectorCall ? 4 : 0;
3420  if (!getCXXABI().classifyReturnType(FI))
3421    FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true);
3422
3423  // We can use up to 6 SSE register parameters with vectorcall.
3424  FreeSSERegs = IsVectorCall ? 6 : 0;
3425  for (auto &I : FI.arguments())
3426    I.info = classify(I.type, FreeSSERegs, false);
3427}
3428
3429Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3430                                    QualType Ty) const {
3431  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3432                          CGF.getContext().getTypeInfoInChars(Ty),
3433                          CharUnits::fromQuantity(8),
3434                          /*allowHigherAlign*/ false);
3435}
3436
3437// PowerPC-32
3438namespace {
3439/// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
3440class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
3441bool IsSoftFloatABI;
3442public:
3443  PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
3444      : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
3445
3446  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3447                    QualType Ty) const override;
3448};
3449
3450class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
3451public:
3452  PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
3453      : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
3454
3455  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
3456    // This is recovered from gcc output.
3457    return 1; // r1 is the dedicated stack pointer
3458  }
3459
3460  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3461                               llvm::Value *Address) const override;
3462};
3463
3464}
3465
3466Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
3467                                      QualType Ty) const {
3468  if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
3469    // TODO: Implement this. For now ignore.
3470    (void)CTy;
3471    return Address::invalid();
3472  }
3473
3474  // struct __va_list_tag {
3475  //   unsigned char gpr;
3476  //   unsigned char fpr;
3477  //   unsigned short reserved;
3478  //   void *overflow_arg_area;
3479  //   void *reg_save_area;
3480  // };
3481
3482  bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
3483  bool isInt =
3484      Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
3485  bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
3486
3487  // All aggregates are passed indirectly?  That doesn't seem consistent
3488  // with the argument-lowering code.
3489  bool isIndirect = Ty->isAggregateType();
3490
3491  CGBuilderTy &Builder = CGF.Builder;
3492
3493  // The calling convention either uses 1-2 GPRs or 1 FPR.
3494  Address NumRegsAddr = Address::invalid();
3495  if (isInt || IsSoftFloatABI) {
3496    NumRegsAddr = Builder.CreateStructGEP(VAList, 0, CharUnits::Zero(), "gpr");
3497  } else {
3498    NumRegsAddr = Builder.CreateStructGEP(VAList, 1, CharUnits::One(), "fpr");
3499  }
3500
3501  llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
3502
3503  // "Align" the register count when TY is i64.
3504  if (isI64 || (isF64 && IsSoftFloatABI)) {
3505    NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
3506    NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
3507  }
3508
3509  llvm::Value *CC =
3510      Builder.CreateICmpULT(NumRegs, Builder.getInt8(8), "cond");
3511
3512  llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
3513  llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
3514  llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
3515
3516  Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
3517
3518  llvm::Type *DirectTy = CGF.ConvertType(Ty);
3519  if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
3520
3521  // Case 1: consume registers.
3522  Address RegAddr = Address::invalid();
3523  {
3524    CGF.EmitBlock(UsingRegs);
3525
3526    Address RegSaveAreaPtr =
3527      Builder.CreateStructGEP(VAList, 4, CharUnits::fromQuantity(8));
3528    RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
3529                      CharUnits::fromQuantity(8));
3530    assert(RegAddr.getElementType() == CGF.Int8Ty);
3531
3532    // Floating-point registers start after the general-purpose registers.
3533    if (!(isInt || IsSoftFloatABI)) {
3534      RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
3535                                                   CharUnits::fromQuantity(32));
3536    }
3537
3538    // Get the address of the saved value by scaling the number of
3539    // registers we've used by the number of
3540    CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
3541    llvm::Value *RegOffset =
3542      Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
3543    RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
3544                                            RegAddr.getPointer(), RegOffset),
3545                      RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
3546    RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
3547
3548    // Increase the used-register count.
3549    NumRegs =
3550      Builder.CreateAdd(NumRegs,
3551                        Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
3552    Builder.CreateStore(NumRegs, NumRegsAddr);
3553
3554    CGF.EmitBranch(Cont);
3555  }
3556
3557  // Case 2: consume space in the overflow area.
3558  Address MemAddr = Address::invalid();
3559  {
3560    CGF.EmitBlock(UsingOverflow);
3561
3562    // Everything in the overflow area is rounded up to a size of at least 4.
3563    CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
3564
3565    CharUnits Size;
3566    if (!isIndirect) {
3567      auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
3568      Size = TypeInfo.first.RoundUpToAlignment(OverflowAreaAlign);
3569    } else {
3570      Size = CGF.getPointerSize();
3571    }
3572
3573    Address OverflowAreaAddr =
3574      Builder.CreateStructGEP(VAList, 3, CharUnits::fromQuantity(4));
3575    Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
3576                         OverflowAreaAlign);
3577    // Round up address of argument to alignment
3578    CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3579    if (Align > OverflowAreaAlign) {
3580      llvm::Value *Ptr = OverflowArea.getPointer();
3581      OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
3582                                                           Align);
3583    }
3584
3585    MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
3586
3587    // Increase the overflow area.
3588    OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
3589    Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
3590    CGF.EmitBranch(Cont);
3591  }
3592
3593  CGF.EmitBlock(Cont);
3594
3595  // Merge the cases with a phi.
3596  Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
3597                                "vaarg.addr");
3598
3599  // Load the pointer if the argument was passed indirectly.
3600  if (isIndirect) {
3601    Result = Address(Builder.CreateLoad(Result, "aggr"),
3602                     getContext().getTypeAlignInChars(Ty));
3603  }
3604
3605  return Result;
3606}
3607
3608bool
3609PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3610                                                llvm::Value *Address) const {
3611  // This is calculated from the LLVM and GCC tables and verified
3612  // against gcc output.  AFAIK all ABIs use the same encoding.
3613
3614  CodeGen::CGBuilderTy &Builder = CGF.Builder;
3615
3616  llvm::IntegerType *i8 = CGF.Int8Ty;
3617  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
3618  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
3619  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
3620
3621  // 0-31: r0-31, the 4-byte general-purpose registers
3622  AssignToArrayRange(Builder, Address, Four8, 0, 31);
3623
3624  // 32-63: fp0-31, the 8-byte floating-point registers
3625  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
3626
3627  // 64-76 are various 4-byte special-purpose registers:
3628  // 64: mq
3629  // 65: lr
3630  // 66: ctr
3631  // 67: ap
3632  // 68-75 cr0-7
3633  // 76: xer
3634  AssignToArrayRange(Builder, Address, Four8, 64, 76);
3635
3636  // 77-108: v0-31, the 16-byte vector registers
3637  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
3638
3639  // 109: vrsave
3640  // 110: vscr
3641  // 111: spe_acc
3642  // 112: spefscr
3643  // 113: sfp
3644  AssignToArrayRange(Builder, Address, Four8, 109, 113);
3645
3646  return false;
3647}
3648
3649// PowerPC-64
3650
3651namespace {
3652/// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
3653class PPC64_SVR4_ABIInfo : public DefaultABIInfo {
3654public:
3655  enum ABIKind {
3656    ELFv1 = 0,
3657    ELFv2
3658  };
3659
3660private:
3661  static const unsigned GPRBits = 64;
3662  ABIKind Kind;
3663  bool HasQPX;
3664
3665  // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
3666  // will be passed in a QPX register.
3667  bool IsQPXVectorTy(const Type *Ty) const {
3668    if (!HasQPX)
3669      return false;
3670
3671    if (const VectorType *VT = Ty->getAs<VectorType>()) {
3672      unsigned NumElements = VT->getNumElements();
3673      if (NumElements == 1)
3674        return false;
3675
3676      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
3677        if (getContext().getTypeSize(Ty) <= 256)
3678          return true;
3679      } else if (VT->getElementType()->
3680                   isSpecificBuiltinType(BuiltinType::Float)) {
3681        if (getContext().getTypeSize(Ty) <= 128)
3682          return true;
3683      }
3684    }
3685
3686    return false;
3687  }
3688
3689  bool IsQPXVectorTy(QualType Ty) const {
3690    return IsQPXVectorTy(Ty.getTypePtr());
3691  }
3692
3693public:
3694  PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX)
3695    : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {}
3696
3697  bool isPromotableTypeForABI(QualType Ty) const;
3698  CharUnits getParamTypeAlignment(QualType Ty) const;
3699
3700  ABIArgInfo classifyReturnType(QualType RetTy) const;
3701  ABIArgInfo classifyArgumentType(QualType Ty) const;
3702
3703  bool isHomogeneousAggregateBaseType(QualType Ty) const override;
3704  bool isHomogeneousAggregateSmallEnough(const Type *Ty,
3705                                         uint64_t Members) const override;
3706
3707  // TODO: We can add more logic to computeInfo to improve performance.
3708  // Example: For aggregate arguments that fit in a register, we could
3709  // use getDirectInReg (as is done below for structs containing a single
3710  // floating-point value) to avoid pushing them to memory on function
3711  // entry.  This would require changing the logic in PPCISelLowering
3712  // when lowering the parameters in the caller and args in the callee.
3713  void computeInfo(CGFunctionInfo &FI) const override {
3714    if (!getCXXABI().classifyReturnType(FI))
3715      FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3716    for (auto &I : FI.arguments()) {
3717      // We rely on the default argument classification for the most part.
3718      // One exception:  An aggregate containing a single floating-point
3719      // or vector item must be passed in a register if one is available.
3720      const Type *T = isSingleElementStruct(I.type, getContext());
3721      if (T) {
3722        const BuiltinType *BT = T->getAs<BuiltinType>();
3723        if (IsQPXVectorTy(T) ||
3724            (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
3725            (BT && BT->isFloatingPoint())) {
3726          QualType QT(T, 0);
3727          I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
3728          continue;
3729        }
3730      }
3731      I.info = classifyArgumentType(I.type);
3732    }
3733  }
3734
3735  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3736                    QualType Ty) const override;
3737};
3738
3739class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
3740
3741public:
3742  PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
3743                               PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX)
3744      : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {}
3745
3746  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
3747    // This is recovered from gcc output.
3748    return 1; // r1 is the dedicated stack pointer
3749  }
3750
3751  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3752                               llvm::Value *Address) const override;
3753};
3754
3755class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
3756public:
3757  PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
3758
3759  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
3760    // This is recovered from gcc output.
3761    return 1; // r1 is the dedicated stack pointer
3762  }
3763
3764  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3765                               llvm::Value *Address) const override;
3766};
3767
3768}
3769
3770// Return true if the ABI requires Ty to be passed sign- or zero-
3771// extended to 64 bits.
3772bool
3773PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
3774  // Treat an enum type as its underlying type.
3775  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3776    Ty = EnumTy->getDecl()->getIntegerType();
3777
3778  // Promotable integer types are required to be promoted by the ABI.
3779  if (Ty->isPromotableIntegerType())
3780    return true;
3781
3782  // In addition to the usual promotable integer types, we also need to
3783  // extend all 32-bit types, since the ABI requires promotion to 64 bits.
3784  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3785    switch (BT->getKind()) {
3786    case BuiltinType::Int:
3787    case BuiltinType::UInt:
3788      return true;
3789    default:
3790      break;
3791    }
3792
3793  return false;
3794}
3795
3796/// isAlignedParamType - Determine whether a type requires 16-byte or
3797/// higher alignment in the parameter area.  Always returns at least 8.
3798CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
3799  // Complex types are passed just like their elements.
3800  if (const ComplexType *CTy = Ty->getAs<ComplexType>())
3801    Ty = CTy->getElementType();
3802
3803  // Only vector types of size 16 bytes need alignment (larger types are
3804  // passed via reference, smaller types are not aligned).
3805  if (IsQPXVectorTy(Ty)) {
3806    if (getContext().getTypeSize(Ty) > 128)
3807      return CharUnits::fromQuantity(32);
3808
3809    return CharUnits::fromQuantity(16);
3810  } else if (Ty->isVectorType()) {
3811    return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
3812  }
3813
3814  // For single-element float/vector structs, we consider the whole type
3815  // to have the same alignment requirements as its single element.
3816  const Type *AlignAsType = nullptr;
3817  const Type *EltType = isSingleElementStruct(Ty, getContext());
3818  if (EltType) {
3819    const BuiltinType *BT = EltType->getAs<BuiltinType>();
3820    if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
3821         getContext().getTypeSize(EltType) == 128) ||
3822        (BT && BT->isFloatingPoint()))
3823      AlignAsType = EltType;
3824  }
3825
3826  // Likewise for ELFv2 homogeneous aggregates.
3827  const Type *Base = nullptr;
3828  uint64_t Members = 0;
3829  if (!AlignAsType && Kind == ELFv2 &&
3830      isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
3831    AlignAsType = Base;
3832
3833  // With special case aggregates, only vector base types need alignment.
3834  if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
3835    if (getContext().getTypeSize(AlignAsType) > 128)
3836      return CharUnits::fromQuantity(32);
3837
3838    return CharUnits::fromQuantity(16);
3839  } else if (AlignAsType) {
3840    return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
3841  }
3842
3843  // Otherwise, we only need alignment for any aggregate type that
3844  // has an alignment requirement of >= 16 bytes.
3845  if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
3846    if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
3847      return CharUnits::fromQuantity(32);
3848    return CharUnits::fromQuantity(16);
3849  }
3850
3851  return CharUnits::fromQuantity(8);
3852}
3853
3854/// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
3855/// aggregate.  Base is set to the base element type, and Members is set
3856/// to the number of base elements.
3857bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
3858                                     uint64_t &Members) const {
3859  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
3860    uint64_t NElements = AT->getSize().getZExtValue();
3861    if (NElements == 0)
3862      return false;
3863    if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
3864      return false;
3865    Members *= NElements;
3866  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
3867    const RecordDecl *RD = RT->getDecl();
3868    if (RD->hasFlexibleArrayMember())
3869      return false;
3870
3871    Members = 0;
3872
3873    // If this is a C++ record, check the bases first.
3874    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3875      for (const auto &I : CXXRD->bases()) {
3876        // Ignore empty records.
3877        if (isEmptyRecord(getContext(), I.getType(), true))
3878          continue;
3879
3880        uint64_t FldMembers;
3881        if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
3882          return false;
3883
3884        Members += FldMembers;
3885      }
3886    }
3887
3888    for (const auto *FD : RD->fields()) {
3889      // Ignore (non-zero arrays of) empty records.
3890      QualType FT = FD->getType();
3891      while (const ConstantArrayType *AT =
3892             getContext().getAsConstantArrayType(FT)) {
3893        if (AT->getSize().getZExtValue() == 0)
3894          return false;
3895        FT = AT->getElementType();
3896      }
3897      if (isEmptyRecord(getContext(), FT, true))
3898        continue;
3899
3900      // For compatibility with GCC, ignore empty bitfields in C++ mode.
3901      if (getContext().getLangOpts().CPlusPlus &&
3902          FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
3903        continue;
3904
3905      uint64_t FldMembers;
3906      if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
3907        return false;
3908
3909      Members = (RD->isUnion() ?
3910                 std::max(Members, FldMembers) : Members + FldMembers);
3911    }
3912
3913    if (!Base)
3914      return false;
3915
3916    // Ensure there is no padding.
3917    if (getContext().getTypeSize(Base) * Members !=
3918        getContext().getTypeSize(Ty))
3919      return false;
3920  } else {
3921    Members = 1;
3922    if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
3923      Members = 2;
3924      Ty = CT->getElementType();
3925    }
3926
3927    // Most ABIs only support float, double, and some vector type widths.
3928    if (!isHomogeneousAggregateBaseType(Ty))
3929      return false;
3930
3931    // The base type must be the same for all members.  Types that
3932    // agree in both total size and mode (float vs. vector) are
3933    // treated as being equivalent here.
3934    const Type *TyPtr = Ty.getTypePtr();
3935    if (!Base)
3936      Base = TyPtr;
3937
3938    if (Base->isVectorType() != TyPtr->isVectorType() ||
3939        getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
3940      return false;
3941  }
3942  return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
3943}
3944
3945bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
3946  // Homogeneous aggregates for ELFv2 must have base types of float,
3947  // double, long double, or 128-bit vectors.
3948  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
3949    if (BT->getKind() == BuiltinType::Float ||
3950        BT->getKind() == BuiltinType::Double ||
3951        BT->getKind() == BuiltinType::LongDouble)
3952      return true;
3953  }
3954  if (const VectorType *VT = Ty->getAs<VectorType>()) {
3955    if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
3956      return true;
3957  }
3958  return false;
3959}
3960
3961bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
3962    const Type *Base, uint64_t Members) const {
3963  // Vector types require one register, floating point types require one
3964  // or two registers depending on their size.
3965  uint32_t NumRegs =
3966      Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64;
3967
3968  // Homogeneous Aggregates may occupy at most 8 registers.
3969  return Members * NumRegs <= 8;
3970}
3971
3972ABIArgInfo
3973PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
3974  Ty = useFirstFieldIfTransparentUnion(Ty);
3975
3976  if (Ty->isAnyComplexType())
3977    return ABIArgInfo::getDirect();
3978
3979  // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
3980  // or via reference (larger than 16 bytes).
3981  if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
3982    uint64_t Size = getContext().getTypeSize(Ty);
3983    if (Size > 128)
3984      return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
3985    else if (Size < 128) {
3986      llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
3987      return ABIArgInfo::getDirect(CoerceTy);
3988    }
3989  }
3990
3991  if (isAggregateTypeForABI(Ty)) {
3992    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
3993      return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3994
3995    uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
3996    uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
3997
3998    // ELFv2 homogeneous aggregates are passed as array types.
3999    const Type *Base = nullptr;
4000    uint64_t Members = 0;
4001    if (Kind == ELFv2 &&
4002        isHomogeneousAggregate(Ty, Base, Members)) {
4003      llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4004      llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4005      return ABIArgInfo::getDirect(CoerceTy);
4006    }
4007
4008    // If an aggregate may end up fully in registers, we do not
4009    // use the ByVal method, but pass the aggregate as array.
4010    // This is usually beneficial since we avoid forcing the
4011    // back-end to store the argument to memory.
4012    uint64_t Bits = getContext().getTypeSize(Ty);
4013    if (Bits > 0 && Bits <= 8 * GPRBits) {
4014      llvm::Type *CoerceTy;
4015
4016      // Types up to 8 bytes are passed as integer type (which will be
4017      // properly aligned in the argument save area doubleword).
4018      if (Bits <= GPRBits)
4019        CoerceTy = llvm::IntegerType::get(getVMContext(),
4020                                          llvm::RoundUpToAlignment(Bits, 8));
4021      // Larger types are passed as arrays, with the base type selected
4022      // according to the required alignment in the save area.
4023      else {
4024        uint64_t RegBits = ABIAlign * 8;
4025        uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits;
4026        llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4027        CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4028      }
4029
4030      return ABIArgInfo::getDirect(CoerceTy);
4031    }
4032
4033    // All other aggregates are passed ByVal.
4034    return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4035                                   /*ByVal=*/true,
4036                                   /*Realign=*/TyAlign > ABIAlign);
4037  }
4038
4039  return (isPromotableTypeForABI(Ty) ?
4040          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4041}
4042
4043ABIArgInfo
4044PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4045  if (RetTy->isVoidType())
4046    return ABIArgInfo::getIgnore();
4047
4048  if (RetTy->isAnyComplexType())
4049    return ABIArgInfo::getDirect();
4050
4051  // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4052  // or via reference (larger than 16 bytes).
4053  if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
4054    uint64_t Size = getContext().getTypeSize(RetTy);
4055    if (Size > 128)
4056      return getNaturalAlignIndirect(RetTy);
4057    else if (Size < 128) {
4058      llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4059      return ABIArgInfo::getDirect(CoerceTy);
4060    }
4061  }
4062
4063  if (isAggregateTypeForABI(RetTy)) {
4064    // ELFv2 homogeneous aggregates are returned as array types.
4065    const Type *Base = nullptr;
4066    uint64_t Members = 0;
4067    if (Kind == ELFv2 &&
4068        isHomogeneousAggregate(RetTy, Base, Members)) {
4069      llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4070      llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4071      return ABIArgInfo::getDirect(CoerceTy);
4072    }
4073
4074    // ELFv2 small aggregates are returned in up to two registers.
4075    uint64_t Bits = getContext().getTypeSize(RetTy);
4076    if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4077      if (Bits == 0)
4078        return ABIArgInfo::getIgnore();
4079
4080      llvm::Type *CoerceTy;
4081      if (Bits > GPRBits) {
4082        CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
4083        CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr);
4084      } else
4085        CoerceTy = llvm::IntegerType::get(getVMContext(),
4086                                          llvm::RoundUpToAlignment(Bits, 8));
4087      return ABIArgInfo::getDirect(CoerceTy);
4088    }
4089
4090    // All other aggregates are returned indirectly.
4091    return getNaturalAlignIndirect(RetTy);
4092  }
4093
4094  return (isPromotableTypeForABI(RetTy) ?
4095          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4096}
4097
4098// Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
4099Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4100                                      QualType Ty) const {
4101  auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4102  TypeInfo.second = getParamTypeAlignment(Ty);
4103
4104  CharUnits SlotSize = CharUnits::fromQuantity(8);
4105
4106  // If we have a complex type and the base type is smaller than 8 bytes,
4107  // the ABI calls for the real and imaginary parts to be right-adjusted
4108  // in separate doublewords.  However, Clang expects us to produce a
4109  // pointer to a structure with the two parts packed tightly.  So generate
4110  // loads of the real and imaginary parts relative to the va_list pointer,
4111  // and store them to a temporary structure.
4112  if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4113    CharUnits EltSize = TypeInfo.first / 2;
4114    if (EltSize < SlotSize) {
4115      Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4116                                            SlotSize * 2, SlotSize,
4117                                            SlotSize, /*AllowHigher*/ true);
4118
4119      Address RealAddr = Addr;
4120      Address ImagAddr = RealAddr;
4121      if (CGF.CGM.getDataLayout().isBigEndian()) {
4122        RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4123                                                          SlotSize - EltSize);
4124        ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4125                                                      2 * SlotSize - EltSize);
4126      } else {
4127        ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4128      }
4129
4130      llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4131      RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4132      ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4133      llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4134      llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4135
4136      Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4137      CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4138                             /*init*/ true);
4139      return Temp;
4140    }
4141  }
4142
4143  // Otherwise, just use the general rule.
4144  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4145                          TypeInfo, SlotSize, /*AllowHigher*/ true);
4146}
4147
4148static bool
4149PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4150                              llvm::Value *Address) {
4151  // This is calculated from the LLVM and GCC tables and verified
4152  // against gcc output.  AFAIK all ABIs use the same encoding.
4153
4154  CodeGen::CGBuilderTy &Builder = CGF.Builder;
4155
4156  llvm::IntegerType *i8 = CGF.Int8Ty;
4157  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4158  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4159  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4160
4161  // 0-31: r0-31, the 8-byte general-purpose registers
4162  AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4163
4164  // 32-63: fp0-31, the 8-byte floating-point registers
4165  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4166
4167  // 64-76 are various 4-byte special-purpose registers:
4168  // 64: mq
4169  // 65: lr
4170  // 66: ctr
4171  // 67: ap
4172  // 68-75 cr0-7
4173  // 76: xer
4174  AssignToArrayRange(Builder, Address, Four8, 64, 76);
4175
4176  // 77-108: v0-31, the 16-byte vector registers
4177  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4178
4179  // 109: vrsave
4180  // 110: vscr
4181  // 111: spe_acc
4182  // 112: spefscr
4183  // 113: sfp
4184  AssignToArrayRange(Builder, Address, Four8, 109, 113);
4185
4186  return false;
4187}
4188
4189bool
4190PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4191  CodeGen::CodeGenFunction &CGF,
4192  llvm::Value *Address) const {
4193
4194  return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4195}
4196
4197bool
4198PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4199                                                llvm::Value *Address) const {
4200
4201  return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4202}
4203
4204//===----------------------------------------------------------------------===//
4205// AArch64 ABI Implementation
4206//===----------------------------------------------------------------------===//
4207
4208namespace {
4209
4210class AArch64ABIInfo : public ABIInfo {
4211public:
4212  enum ABIKind {
4213    AAPCS = 0,
4214    DarwinPCS
4215  };
4216
4217private:
4218  ABIKind Kind;
4219
4220public:
4221  AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {}
4222
4223private:
4224  ABIKind getABIKind() const { return Kind; }
4225  bool isDarwinPCS() const { return Kind == DarwinPCS; }
4226
4227  ABIArgInfo classifyReturnType(QualType RetTy) const;
4228  ABIArgInfo classifyArgumentType(QualType RetTy) const;
4229  bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4230  bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4231                                         uint64_t Members) const override;
4232
4233  bool isIllegalVectorType(QualType Ty) const;
4234
4235  void computeInfo(CGFunctionInfo &FI) const override {
4236    if (!getCXXABI().classifyReturnType(FI))
4237      FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4238
4239    for (auto &it : FI.arguments())
4240      it.info = classifyArgumentType(it.type);
4241  }
4242
4243  Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4244                          CodeGenFunction &CGF) const;
4245
4246  Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
4247                         CodeGenFunction &CGF) const;
4248
4249  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4250                    QualType Ty) const override {
4251    return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
4252                         : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
4253  }
4254};
4255
4256class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
4257public:
4258  AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
4259      : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
4260
4261  StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
4262    return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue";
4263  }
4264
4265  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4266    return 31;
4267  }
4268
4269  bool doesReturnSlotInterfereWithArgs() const override { return false; }
4270};
4271}
4272
4273ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
4274  Ty = useFirstFieldIfTransparentUnion(Ty);
4275
4276  // Handle illegal vector types here.
4277  if (isIllegalVectorType(Ty)) {
4278    uint64_t Size = getContext().getTypeSize(Ty);
4279    // Android promotes <2 x i8> to i16, not i32
4280    if (Size <= 16) {
4281      llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
4282      return ABIArgInfo::getDirect(ResType);
4283    }
4284    if (Size == 32) {
4285      llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
4286      return ABIArgInfo::getDirect(ResType);
4287    }
4288    if (Size == 64) {
4289      llvm::Type *ResType =
4290          llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
4291      return ABIArgInfo::getDirect(ResType);
4292    }
4293    if (Size == 128) {
4294      llvm::Type *ResType =
4295          llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
4296      return ABIArgInfo::getDirect(ResType);
4297    }
4298    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4299  }
4300
4301  if (!isAggregateTypeForABI(Ty)) {
4302    // Treat an enum type as its underlying type.
4303    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4304      Ty = EnumTy->getDecl()->getIntegerType();
4305
4306    return (Ty->isPromotableIntegerType() && isDarwinPCS()
4307                ? ABIArgInfo::getExtend()
4308                : ABIArgInfo::getDirect());
4309  }
4310
4311  // Structures with either a non-trivial destructor or a non-trivial
4312  // copy constructor are always indirect.
4313  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
4314    return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
4315                                     CGCXXABI::RAA_DirectInMemory);
4316  }
4317
4318  // Empty records are always ignored on Darwin, but actually passed in C++ mode
4319  // elsewhere for GNU compatibility.
4320  if (isEmptyRecord(getContext(), Ty, true)) {
4321    if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
4322      return ABIArgInfo::getIgnore();
4323
4324    return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
4325  }
4326
4327  // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
4328  const Type *Base = nullptr;
4329  uint64_t Members = 0;
4330  if (isHomogeneousAggregate(Ty, Base, Members)) {
4331    return ABIArgInfo::getDirect(
4332        llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
4333  }
4334
4335  // Aggregates <= 16 bytes are passed directly in registers or on the stack.
4336  uint64_t Size = getContext().getTypeSize(Ty);
4337  if (Size <= 128) {
4338    if (getContext().getLangOpts().Renderscript) {
4339      return coerceToIntArray(Ty, getContext(), getVMContext());
4340    }
4341    unsigned Alignment = getContext().getTypeAlign(Ty);
4342    Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
4343
4344    // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4345    // For aggregates with 16-byte alignment, we use i128.
4346    if (Alignment < 128 && Size == 128) {
4347      llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4348      return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4349    }
4350    return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4351  }
4352
4353  return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4354}
4355
4356ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const {
4357  if (RetTy->isVoidType())
4358    return ABIArgInfo::getIgnore();
4359
4360  // Large vector types should be returned via memory.
4361  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
4362    return getNaturalAlignIndirect(RetTy);
4363
4364  if (!isAggregateTypeForABI(RetTy)) {
4365    // Treat an enum type as its underlying type.
4366    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4367      RetTy = EnumTy->getDecl()->getIntegerType();
4368
4369    return (RetTy->isPromotableIntegerType() && isDarwinPCS()
4370                ? ABIArgInfo::getExtend()
4371                : ABIArgInfo::getDirect());
4372  }
4373
4374  if (isEmptyRecord(getContext(), RetTy, true))
4375    return ABIArgInfo::getIgnore();
4376
4377  const Type *Base = nullptr;
4378  uint64_t Members = 0;
4379  if (isHomogeneousAggregate(RetTy, Base, Members))
4380    // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
4381    return ABIArgInfo::getDirect();
4382
4383  // Aggregates <= 16 bytes are returned directly in registers or on the stack.
4384  uint64_t Size = getContext().getTypeSize(RetTy);
4385  if (Size <= 128) {
4386    if (getContext().getLangOpts().Renderscript) {
4387      return coerceToIntArray(RetTy, getContext(), getVMContext());
4388    }
4389    unsigned Alignment = getContext().getTypeAlign(RetTy);
4390    Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes
4391
4392    // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
4393    // For aggregates with 16-byte alignment, we use i128.
4394    if (Alignment < 128 && Size == 128) {
4395      llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
4396      return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
4397    }
4398    return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
4399  }
4400
4401  return getNaturalAlignIndirect(RetTy);
4402}
4403
4404/// isIllegalVectorType - check whether the vector type is legal for AArch64.
4405bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
4406  if (const VectorType *VT = Ty->getAs<VectorType>()) {
4407    // Check whether VT is legal.
4408    unsigned NumElements = VT->getNumElements();
4409    uint64_t Size = getContext().getTypeSize(VT);
4410    // NumElements should be power of 2 between 1 and 16.
4411    if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16)
4412      return true;
4413    return Size != 64 && (Size != 128 || NumElements == 1);
4414  }
4415  return false;
4416}
4417
4418bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4419  // Homogeneous aggregates for AAPCS64 must have base types of a floating
4420  // point type or a short-vector type. This is the same as the 32-bit ABI,
4421  // but with the difference that any floating-point type is allowed,
4422  // including __fp16.
4423  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4424    if (BT->isFloatingPoint())
4425      return true;
4426  } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
4427    unsigned VecSize = getContext().getTypeSize(VT);
4428    if (VecSize == 64 || VecSize == 128)
4429      return true;
4430  }
4431  return false;
4432}
4433
4434bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
4435                                                       uint64_t Members) const {
4436  return Members <= 4;
4437}
4438
4439Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
4440                                            QualType Ty,
4441                                            CodeGenFunction &CGF) const {
4442  ABIArgInfo AI = classifyArgumentType(Ty);
4443  bool IsIndirect = AI.isIndirect();
4444
4445  llvm::Type *BaseTy = CGF.ConvertType(Ty);
4446  if (IsIndirect)
4447    BaseTy = llvm::PointerType::getUnqual(BaseTy);
4448  else if (AI.getCoerceToType())
4449    BaseTy = AI.getCoerceToType();
4450
4451  unsigned NumRegs = 1;
4452  if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
4453    BaseTy = ArrTy->getElementType();
4454    NumRegs = ArrTy->getNumElements();
4455  }
4456  bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
4457
4458  // The AArch64 va_list type and handling is specified in the Procedure Call
4459  // Standard, section B.4:
4460  //
4461  // struct {
4462  //   void *__stack;
4463  //   void *__gr_top;
4464  //   void *__vr_top;
4465  //   int __gr_offs;
4466  //   int __vr_offs;
4467  // };
4468
4469  llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
4470  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
4471  llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
4472  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
4473
4474  auto TyInfo = getContext().getTypeInfoInChars(Ty);
4475  CharUnits TyAlign = TyInfo.second;
4476
4477  Address reg_offs_p = Address::invalid();
4478  llvm::Value *reg_offs = nullptr;
4479  int reg_top_index;
4480  CharUnits reg_top_offset;
4481  int RegSize = IsIndirect ? 8 : TyInfo.first.getQuantity();
4482  if (!IsFPR) {
4483    // 3 is the field number of __gr_offs
4484    reg_offs_p =
4485        CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
4486                                    "gr_offs_p");
4487    reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
4488    reg_top_index = 1; // field number for __gr_top
4489    reg_top_offset = CharUnits::fromQuantity(8);
4490    RegSize = llvm::RoundUpToAlignment(RegSize, 8);
4491  } else {
4492    // 4 is the field number of __vr_offs.
4493    reg_offs_p =
4494        CGF.Builder.CreateStructGEP(VAListAddr, 4, CharUnits::fromQuantity(28),
4495                                    "vr_offs_p");
4496    reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
4497    reg_top_index = 2; // field number for __vr_top
4498    reg_top_offset = CharUnits::fromQuantity(16);
4499    RegSize = 16 * NumRegs;
4500  }
4501
4502  //=======================================
4503  // Find out where argument was passed
4504  //=======================================
4505
4506  // If reg_offs >= 0 we're already using the stack for this type of
4507  // argument. We don't want to keep updating reg_offs (in case it overflows,
4508  // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
4509  // whatever they get).
4510  llvm::Value *UsingStack = nullptr;
4511  UsingStack = CGF.Builder.CreateICmpSGE(
4512      reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
4513
4514  CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
4515
4516  // Otherwise, at least some kind of argument could go in these registers, the
4517  // question is whether this particular type is too big.
4518  CGF.EmitBlock(MaybeRegBlock);
4519
4520  // Integer arguments may need to correct register alignment (for example a
4521  // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
4522  // align __gr_offs to calculate the potential address.
4523  if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
4524    int Align = TyAlign.getQuantity();
4525
4526    reg_offs = CGF.Builder.CreateAdd(
4527        reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
4528        "align_regoffs");
4529    reg_offs = CGF.Builder.CreateAnd(
4530        reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
4531        "aligned_regoffs");
4532  }
4533
4534  // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
4535  // The fact that this is done unconditionally reflects the fact that
4536  // allocating an argument to the stack also uses up all the remaining
4537  // registers of the appropriate kind.
4538  llvm::Value *NewOffset = nullptr;
4539  NewOffset = CGF.Builder.CreateAdd(
4540      reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
4541  CGF.Builder.CreateStore(NewOffset, reg_offs_p);
4542
4543  // Now we're in a position to decide whether this argument really was in
4544  // registers or not.
4545  llvm::Value *InRegs = nullptr;
4546  InRegs = CGF.Builder.CreateICmpSLE(
4547      NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
4548
4549  CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
4550
4551  //=======================================
4552  // Argument was in registers
4553  //=======================================
4554
4555  // Now we emit the code for if the argument was originally passed in
4556  // registers. First start the appropriate block:
4557  CGF.EmitBlock(InRegBlock);
4558
4559  llvm::Value *reg_top = nullptr;
4560  Address reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index,
4561                                                  reg_top_offset, "reg_top_p");
4562  reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
4563  Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
4564                   CharUnits::fromQuantity(IsFPR ? 16 : 8));
4565  Address RegAddr = Address::invalid();
4566  llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
4567
4568  if (IsIndirect) {
4569    // If it's been passed indirectly (actually a struct), whatever we find from
4570    // stored registers or on the stack will actually be a struct **.
4571    MemTy = llvm::PointerType::getUnqual(MemTy);
4572  }
4573
4574  const Type *Base = nullptr;
4575  uint64_t NumMembers = 0;
4576  bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
4577  if (IsHFA && NumMembers > 1) {
4578    // Homogeneous aggregates passed in registers will have their elements split
4579    // and stored 16-bytes apart regardless of size (they're notionally in qN,
4580    // qN+1, ...). We reload and store into a temporary local variable
4581    // contiguously.
4582    assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
4583    auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
4584    llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
4585    llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
4586    Address Tmp = CGF.CreateTempAlloca(HFATy,
4587                                       std::max(TyAlign, BaseTyInfo.second));
4588
4589    // On big-endian platforms, the value will be right-aligned in its slot.
4590    int Offset = 0;
4591    if (CGF.CGM.getDataLayout().isBigEndian() &&
4592        BaseTyInfo.first.getQuantity() < 16)
4593      Offset = 16 - BaseTyInfo.first.getQuantity();
4594
4595    for (unsigned i = 0; i < NumMembers; ++i) {
4596      CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
4597      Address LoadAddr =
4598        CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
4599      LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
4600
4601      Address StoreAddr =
4602        CGF.Builder.CreateConstArrayGEP(Tmp, i, BaseTyInfo.first);
4603
4604      llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
4605      CGF.Builder.CreateStore(Elem, StoreAddr);
4606    }
4607
4608    RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
4609  } else {
4610    // Otherwise the object is contiguous in memory.
4611
4612    // It might be right-aligned in its slot.
4613    CharUnits SlotSize = BaseAddr.getAlignment();
4614    if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
4615        (IsHFA || !isAggregateTypeForABI(Ty)) &&
4616        TyInfo.first < SlotSize) {
4617      CharUnits Offset = SlotSize - TyInfo.first;
4618      BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
4619    }
4620
4621    RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
4622  }
4623
4624  CGF.EmitBranch(ContBlock);
4625
4626  //=======================================
4627  // Argument was on the stack
4628  //=======================================
4629  CGF.EmitBlock(OnStackBlock);
4630
4631  Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0,
4632                                                CharUnits::Zero(), "stack_p");
4633  llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
4634
4635  // Again, stack arguments may need realignment. In this case both integer and
4636  // floating-point ones might be affected.
4637  if (!IsIndirect && TyAlign.getQuantity() > 8) {
4638    int Align = TyAlign.getQuantity();
4639
4640    OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
4641
4642    OnStackPtr = CGF.Builder.CreateAdd(
4643        OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
4644        "align_stack");
4645    OnStackPtr = CGF.Builder.CreateAnd(
4646        OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
4647        "align_stack");
4648
4649    OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
4650  }
4651  Address OnStackAddr(OnStackPtr,
4652                      std::max(CharUnits::fromQuantity(8), TyAlign));
4653
4654  // All stack slots are multiples of 8 bytes.
4655  CharUnits StackSlotSize = CharUnits::fromQuantity(8);
4656  CharUnits StackSize;
4657  if (IsIndirect)
4658    StackSize = StackSlotSize;
4659  else
4660    StackSize = TyInfo.first.RoundUpToAlignment(StackSlotSize);
4661
4662  llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
4663  llvm::Value *NewStack =
4664      CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
4665
4666  // Write the new value of __stack for the next call to va_arg
4667  CGF.Builder.CreateStore(NewStack, stack_p);
4668
4669  if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
4670      TyInfo.first < StackSlotSize) {
4671    CharUnits Offset = StackSlotSize - TyInfo.first;
4672    OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
4673  }
4674
4675  OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
4676
4677  CGF.EmitBranch(ContBlock);
4678
4679  //=======================================
4680  // Tidy up
4681  //=======================================
4682  CGF.EmitBlock(ContBlock);
4683
4684  Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
4685                                 OnStackAddr, OnStackBlock, "vaargs.addr");
4686
4687  if (IsIndirect)
4688    return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
4689                   TyInfo.second);
4690
4691  return ResAddr;
4692}
4693
4694Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
4695                                        CodeGenFunction &CGF) const {
4696  // The backend's lowering doesn't support va_arg for aggregates or
4697  // illegal vector types.  Lower VAArg here for these cases and use
4698  // the LLVM va_arg instruction for everything else.
4699  if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
4700    return Address::invalid();
4701
4702  CharUnits SlotSize = CharUnits::fromQuantity(8);
4703
4704  // Empty records are ignored for parameter passing purposes.
4705  if (isEmptyRecord(getContext(), Ty, true)) {
4706    Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
4707    Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
4708    return Addr;
4709  }
4710
4711  // The size of the actual thing passed, which might end up just
4712  // being a pointer for indirect types.
4713  auto TyInfo = getContext().getTypeInfoInChars(Ty);
4714
4715  // Arguments bigger than 16 bytes which aren't homogeneous
4716  // aggregates should be passed indirectly.
4717  bool IsIndirect = false;
4718  if (TyInfo.first.getQuantity() > 16) {
4719    const Type *Base = nullptr;
4720    uint64_t Members = 0;
4721    IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
4722  }
4723
4724  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4725                          TyInfo, SlotSize, /*AllowHigherAlign*/ true);
4726}
4727
4728//===----------------------------------------------------------------------===//
4729// ARM ABI Implementation
4730//===----------------------------------------------------------------------===//
4731
4732namespace {
4733
4734class ARMABIInfo : public ABIInfo {
4735public:
4736  enum ABIKind {
4737    APCS = 0,
4738    AAPCS = 1,
4739    AAPCS_VFP = 2,
4740    AAPCS16_VFP = 3,
4741  };
4742
4743private:
4744  ABIKind Kind;
4745
4746public:
4747  ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {
4748    setCCs();
4749  }
4750
4751  bool isEABI() const {
4752    switch (getTarget().getTriple().getEnvironment()) {
4753    case llvm::Triple::Android:
4754    case llvm::Triple::EABI:
4755    case llvm::Triple::EABIHF:
4756    case llvm::Triple::GNUEABI:
4757    case llvm::Triple::GNUEABIHF:
4758      return true;
4759    default:
4760      return false;
4761    }
4762  }
4763
4764  bool isEABIHF() const {
4765    switch (getTarget().getTriple().getEnvironment()) {
4766    case llvm::Triple::EABIHF:
4767    case llvm::Triple::GNUEABIHF:
4768      return true;
4769    default:
4770      return false;
4771    }
4772  }
4773
4774  bool isAndroid() const {
4775    return (getTarget().getTriple().getEnvironment() ==
4776            llvm::Triple::Android || getContext().getLangOpts().Renderscript);
4777  }
4778
4779  ABIKind getABIKind() const { return Kind; }
4780
4781private:
4782  ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const;
4783  ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const;
4784  bool isIllegalVectorType(QualType Ty) const;
4785
4786  bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4787  bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4788                                         uint64_t Members) const override;
4789
4790  void computeInfo(CGFunctionInfo &FI) const override;
4791
4792  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4793                    QualType Ty) const override;
4794
4795  llvm::CallingConv::ID getLLVMDefaultCC() const;
4796  llvm::CallingConv::ID getABIDefaultCC() const;
4797  void setCCs();
4798};
4799
4800class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
4801public:
4802  ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
4803    :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
4804
4805  const ARMABIInfo &getABIInfo() const {
4806    return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
4807  }
4808
4809  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4810    return 13;
4811  }
4812
4813  StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
4814    return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
4815  }
4816
4817  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4818                               llvm::Value *Address) const override {
4819    llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
4820
4821    // 0-15 are the 16 integer registers.
4822    AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
4823    return false;
4824  }
4825
4826  unsigned getSizeOfUnwindException() const override {
4827    if (getABIInfo().isEABI()) return 88;
4828    return TargetCodeGenInfo::getSizeOfUnwindException();
4829  }
4830
4831  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4832                           CodeGen::CodeGenModule &CGM) const override {
4833    const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
4834    if (!FD)
4835      return;
4836
4837    const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
4838    if (!Attr)
4839      return;
4840
4841    const char *Kind;
4842    switch (Attr->getInterrupt()) {
4843    case ARMInterruptAttr::Generic: Kind = ""; break;
4844    case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
4845    case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
4846    case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
4847    case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
4848    case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
4849    }
4850
4851    llvm::Function *Fn = cast<llvm::Function>(GV);
4852
4853    Fn->addFnAttr("interrupt", Kind);
4854
4855    ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
4856    if (ABI == ARMABIInfo::APCS)
4857      return;
4858
4859    // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
4860    // however this is not necessarily true on taking any interrupt. Instruct
4861    // the backend to perform a realignment as part of the function prologue.
4862    llvm::AttrBuilder B;
4863    B.addStackAlignmentAttr(8);
4864    Fn->addAttributes(llvm::AttributeSet::FunctionIndex,
4865                      llvm::AttributeSet::get(CGM.getLLVMContext(),
4866                                              llvm::AttributeSet::FunctionIndex,
4867                                              B));
4868  }
4869};
4870
4871class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
4872  void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV,
4873                                        CodeGen::CodeGenModule &CGM) const;
4874
4875public:
4876  WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
4877      : ARMTargetCodeGenInfo(CGT, K) {}
4878
4879  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
4880                           CodeGen::CodeGenModule &CGM) const override;
4881};
4882
4883void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute(
4884    const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
4885  if (!isa<FunctionDecl>(D))
4886    return;
4887  if (CGM.getCodeGenOpts().StackProbeSize == 4096)
4888    return;
4889
4890  llvm::Function *F = cast<llvm::Function>(GV);
4891  F->addFnAttr("stack-probe-size",
4892               llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
4893}
4894
4895void WindowsARMTargetCodeGenInfo::setTargetAttributes(
4896    const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
4897  ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
4898  addStackProbeSizeTargetAttribute(D, GV, CGM);
4899}
4900}
4901
4902void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
4903  if (!getCXXABI().classifyReturnType(FI))
4904    FI.getReturnInfo() =
4905        classifyReturnType(FI.getReturnType(), FI.isVariadic());
4906
4907  for (auto &I : FI.arguments())
4908    I.info = classifyArgumentType(I.type, FI.isVariadic());
4909
4910  // Always honor user-specified calling convention.
4911  if (FI.getCallingConvention() != llvm::CallingConv::C)
4912    return;
4913
4914  llvm::CallingConv::ID cc = getRuntimeCC();
4915  if (cc != llvm::CallingConv::C)
4916    FI.setEffectiveCallingConvention(cc);
4917}
4918
4919/// Return the default calling convention that LLVM will use.
4920llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
4921  // The default calling convention that LLVM will infer.
4922  if (isEABIHF() || getTarget().getTriple().isWatchOS())
4923    return llvm::CallingConv::ARM_AAPCS_VFP;
4924  else if (isEABI())
4925    return llvm::CallingConv::ARM_AAPCS;
4926  else
4927    return llvm::CallingConv::ARM_APCS;
4928}
4929
4930/// Return the calling convention that our ABI would like us to use
4931/// as the C calling convention.
4932llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
4933  switch (getABIKind()) {
4934  case APCS: return llvm::CallingConv::ARM_APCS;
4935  case AAPCS: return llvm::CallingConv::ARM_AAPCS;
4936  case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
4937  case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
4938  }
4939  llvm_unreachable("bad ABI kind");
4940}
4941
4942void ARMABIInfo::setCCs() {
4943  assert(getRuntimeCC() == llvm::CallingConv::C);
4944
4945  // Don't muddy up the IR with a ton of explicit annotations if
4946  // they'd just match what LLVM will infer from the triple.
4947  llvm::CallingConv::ID abiCC = getABIDefaultCC();
4948  if (abiCC != getLLVMDefaultCC())
4949    RuntimeCC = abiCC;
4950
4951  // AAPCS apparently requires runtime support functions to be soft-float, but
4952  // that's almost certainly for historic reasons (Thumb1 not supporting VFP
4953  // most likely). It's more convenient for AAPCS16_VFP to be hard-float.
4954  switch (getABIKind()) {
4955  case APCS:
4956  case AAPCS16_VFP:
4957    if (abiCC != getLLVMDefaultCC())
4958      BuiltinCC = abiCC;
4959    break;
4960  case AAPCS:
4961  case AAPCS_VFP:
4962    BuiltinCC = llvm::CallingConv::ARM_AAPCS;
4963    break;
4964  }
4965}
4966
4967ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty,
4968                                            bool isVariadic) const {
4969  // 6.1.2.1 The following argument types are VFP CPRCs:
4970  //   A single-precision floating-point type (including promoted
4971  //   half-precision types); A double-precision floating-point type;
4972  //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
4973  //   with a Base Type of a single- or double-precision floating-point type,
4974  //   64-bit containerized vectors or 128-bit containerized vectors with one
4975  //   to four Elements.
4976  bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic;
4977
4978  Ty = useFirstFieldIfTransparentUnion(Ty);
4979
4980  // Handle illegal vector types here.
4981  if (isIllegalVectorType(Ty)) {
4982    uint64_t Size = getContext().getTypeSize(Ty);
4983    if (Size <= 32) {
4984      llvm::Type *ResType =
4985          llvm::Type::getInt32Ty(getVMContext());
4986      return ABIArgInfo::getDirect(ResType);
4987    }
4988    if (Size == 64) {
4989      llvm::Type *ResType = llvm::VectorType::get(
4990          llvm::Type::getInt32Ty(getVMContext()), 2);
4991      return ABIArgInfo::getDirect(ResType);
4992    }
4993    if (Size == 128) {
4994      llvm::Type *ResType = llvm::VectorType::get(
4995          llvm::Type::getInt32Ty(getVMContext()), 4);
4996      return ABIArgInfo::getDirect(ResType);
4997    }
4998    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4999  }
5000
5001  // __fp16 gets passed as if it were an int or float, but with the top 16 bits
5002  // unspecified. This is not done for OpenCL as it handles the half type
5003  // natively, and does not need to interwork with AAPCS code.
5004  if (Ty->isHalfType() && !getContext().getLangOpts().HalfArgsAndReturns) {
5005    llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5006      llvm::Type::getFloatTy(getVMContext()) :
5007      llvm::Type::getInt32Ty(getVMContext());
5008    return ABIArgInfo::getDirect(ResType);
5009  }
5010
5011  if (!isAggregateTypeForABI(Ty)) {
5012    // Treat an enum type as its underlying type.
5013    if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
5014      Ty = EnumTy->getDecl()->getIntegerType();
5015    }
5016
5017    return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5018                                          : ABIArgInfo::getDirect());
5019  }
5020
5021  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5022    return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5023  }
5024
5025  // Ignore empty records.
5026  if (isEmptyRecord(getContext(), Ty, true))
5027    return ABIArgInfo::getIgnore();
5028
5029  if (IsEffectivelyAAPCS_VFP) {
5030    // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5031    // into VFP registers.
5032    const Type *Base = nullptr;
5033    uint64_t Members = 0;
5034    if (isHomogeneousAggregate(Ty, Base, Members)) {
5035      assert(Base && "Base class should be set for homogeneous aggregate");
5036      // Base can be a floating-point or a vector.
5037      return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
5038    }
5039  } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5040    // WatchOS does have homogeneous aggregates. Note that we intentionally use
5041    // this convention even for a variadic function: the backend will use GPRs
5042    // if needed.
5043    const Type *Base = nullptr;
5044    uint64_t Members = 0;
5045    if (isHomogeneousAggregate(Ty, Base, Members)) {
5046      assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5047      llvm::Type *Ty =
5048        llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5049      return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5050    }
5051  }
5052
5053  if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5054      getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5055    // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5056    // bigger than 128-bits, they get placed in space allocated by the caller,
5057    // and a pointer is passed.
5058    return ABIArgInfo::getIndirect(
5059        CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
5060  }
5061
5062  // Support byval for ARM.
5063  // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5064  // most 8-byte. We realign the indirect argument if type alignment is bigger
5065  // than ABI alignment.
5066  uint64_t ABIAlign = 4;
5067  uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8;
5068  if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5069       getABIKind() == ARMABIInfo::AAPCS)
5070    ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
5071
5072  if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
5073    assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
5074    return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5075                                   /*ByVal=*/true,
5076                                   /*Realign=*/TyAlign > ABIAlign);
5077  }
5078
5079  if (getContext().getLangOpts().Renderscript) {
5080    return coerceToIntArray(Ty, getContext(), getVMContext());
5081  }
5082
5083  // Otherwise, pass by coercing to a structure of the appropriate size.
5084  llvm::Type* ElemTy;
5085  unsigned SizeRegs;
5086  // FIXME: Try to match the types of the arguments more accurately where
5087  // we can.
5088  if (getContext().getTypeAlign(Ty) <= 32) {
5089    ElemTy = llvm::Type::getInt32Ty(getVMContext());
5090    SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
5091  } else {
5092    ElemTy = llvm::Type::getInt64Ty(getVMContext());
5093    SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
5094  }
5095
5096  return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
5097}
5098
5099static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
5100                              llvm::LLVMContext &VMContext) {
5101  // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
5102  // is called integer-like if its size is less than or equal to one word, and
5103  // the offset of each of its addressable sub-fields is zero.
5104
5105  uint64_t Size = Context.getTypeSize(Ty);
5106
5107  // Check that the type fits in a word.
5108  if (Size > 32)
5109    return false;
5110
5111  // FIXME: Handle vector types!
5112  if (Ty->isVectorType())
5113    return false;
5114
5115  // Float types are never treated as "integer like".
5116  if (Ty->isRealFloatingType())
5117    return false;
5118
5119  // If this is a builtin or pointer type then it is ok.
5120  if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
5121    return true;
5122
5123  // Small complex integer types are "integer like".
5124  if (const ComplexType *CT = Ty->getAs<ComplexType>())
5125    return isIntegerLikeType(CT->getElementType(), Context, VMContext);
5126
5127  // Single element and zero sized arrays should be allowed, by the definition
5128  // above, but they are not.
5129
5130  // Otherwise, it must be a record type.
5131  const RecordType *RT = Ty->getAs<RecordType>();
5132  if (!RT) return false;
5133
5134  // Ignore records with flexible arrays.
5135  const RecordDecl *RD = RT->getDecl();
5136  if (RD->hasFlexibleArrayMember())
5137    return false;
5138
5139  // Check that all sub-fields are at offset 0, and are themselves "integer
5140  // like".
5141  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
5142
5143  bool HadField = false;
5144  unsigned idx = 0;
5145  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
5146       i != e; ++i, ++idx) {
5147    const FieldDecl *FD = *i;
5148
5149    // Bit-fields are not addressable, we only need to verify they are "integer
5150    // like". We still have to disallow a subsequent non-bitfield, for example:
5151    //   struct { int : 0; int x }
5152    // is non-integer like according to gcc.
5153    if (FD->isBitField()) {
5154      if (!RD->isUnion())
5155        HadField = true;
5156
5157      if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5158        return false;
5159
5160      continue;
5161    }
5162
5163    // Check if this field is at offset 0.
5164    if (Layout.getFieldOffset(idx) != 0)
5165      return false;
5166
5167    if (!isIntegerLikeType(FD->getType(), Context, VMContext))
5168      return false;
5169
5170    // Only allow at most one field in a structure. This doesn't match the
5171    // wording above, but follows gcc in situations with a field following an
5172    // empty structure.
5173    if (!RD->isUnion()) {
5174      if (HadField)
5175        return false;
5176
5177      HadField = true;
5178    }
5179  }
5180
5181  return true;
5182}
5183
5184ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy,
5185                                          bool isVariadic) const {
5186  bool IsEffectivelyAAPCS_VFP =
5187      (getABIKind() == AAPCS_VFP || getABIKind() == AAPCS16_VFP) && !isVariadic;
5188
5189  if (RetTy->isVoidType())
5190    return ABIArgInfo::getIgnore();
5191
5192  // Large vector types should be returned via memory.
5193  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) {
5194    return getNaturalAlignIndirect(RetTy);
5195  }
5196
5197  // __fp16 gets returned as if it were an int or float, but with the top 16
5198  // bits unspecified. This is not done for OpenCL as it handles the half type
5199  // natively, and does not need to interwork with AAPCS code.
5200  if (RetTy->isHalfType() && !getContext().getLangOpts().HalfArgsAndReturns) {
5201    llvm::Type *ResType = IsEffectivelyAAPCS_VFP ?
5202      llvm::Type::getFloatTy(getVMContext()) :
5203      llvm::Type::getInt32Ty(getVMContext());
5204    return ABIArgInfo::getDirect(ResType);
5205  }
5206
5207  if (!isAggregateTypeForABI(RetTy)) {
5208    // Treat an enum type as its underlying type.
5209    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5210      RetTy = EnumTy->getDecl()->getIntegerType();
5211
5212    return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend()
5213                                            : ABIArgInfo::getDirect();
5214  }
5215
5216  // Are we following APCS?
5217  if (getABIKind() == APCS) {
5218    if (isEmptyRecord(getContext(), RetTy, false))
5219      return ABIArgInfo::getIgnore();
5220
5221    // Complex types are all returned as packed integers.
5222    //
5223    // FIXME: Consider using 2 x vector types if the back end handles them
5224    // correctly.
5225    if (RetTy->isAnyComplexType())
5226      return ABIArgInfo::getDirect(llvm::IntegerType::get(
5227          getVMContext(), getContext().getTypeSize(RetTy)));
5228
5229    // Integer like structures are returned in r0.
5230    if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
5231      // Return in the smallest viable integer type.
5232      uint64_t Size = getContext().getTypeSize(RetTy);
5233      if (Size <= 8)
5234        return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5235      if (Size <= 16)
5236        return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5237      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5238    }
5239
5240    // Otherwise return in memory.
5241    return getNaturalAlignIndirect(RetTy);
5242  }
5243
5244  // Otherwise this is an AAPCS variant.
5245
5246  if (isEmptyRecord(getContext(), RetTy, true))
5247    return ABIArgInfo::getIgnore();
5248
5249  // Check for homogeneous aggregates with AAPCS-VFP.
5250  if (IsEffectivelyAAPCS_VFP) {
5251    const Type *Base = nullptr;
5252    uint64_t Members = 0;
5253    if (isHomogeneousAggregate(RetTy, Base, Members)) {
5254      assert(Base && "Base class should be set for homogeneous aggregate");
5255      // Homogeneous Aggregates are returned directly.
5256      return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
5257    }
5258  }
5259
5260  // Aggregates <= 4 bytes are returned in r0; other aggregates
5261  // are returned indirectly.
5262  uint64_t Size = getContext().getTypeSize(RetTy);
5263  if (Size <= 32) {
5264    if (getContext().getLangOpts().Renderscript) {
5265      return coerceToIntArray(RetTy, getContext(), getVMContext());
5266    }
5267    if (getDataLayout().isBigEndian())
5268      // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
5269      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5270
5271    // Return in the smallest viable integer type.
5272    if (Size <= 8)
5273      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5274    if (Size <= 16)
5275      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
5276    return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
5277  } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
5278    llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
5279    llvm::Type *CoerceTy =
5280        llvm::ArrayType::get(Int32Ty, llvm::RoundUpToAlignment(Size, 32) / 32);
5281    return ABIArgInfo::getDirect(CoerceTy);
5282  }
5283
5284  return getNaturalAlignIndirect(RetTy);
5285}
5286
5287/// isIllegalVector - check whether Ty is an illegal vector type.
5288bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
5289  if (const VectorType *VT = Ty->getAs<VectorType> ()) {
5290    if (isAndroid()) {
5291      // Android shipped using Clang 3.1, which supported a slightly different
5292      // vector ABI. The primary differences were that 3-element vector types
5293      // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
5294      // accepts that legacy behavior for Android only.
5295      // Check whether VT is legal.
5296      unsigned NumElements = VT->getNumElements();
5297      // NumElements should be power of 2 or equal to 3.
5298      if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
5299        return true;
5300    } else {
5301      // Check whether VT is legal.
5302      unsigned NumElements = VT->getNumElements();
5303      uint64_t Size = getContext().getTypeSize(VT);
5304      // NumElements should be power of 2.
5305      if (!llvm::isPowerOf2_32(NumElements))
5306        return true;
5307      // Size should be greater than 32 bits.
5308      return Size <= 32;
5309    }
5310  }
5311  return false;
5312}
5313
5314bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5315  // Homogeneous aggregates for AAPCS-VFP must have base types of float,
5316  // double, or 64-bit or 128-bit vectors.
5317  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5318    if (BT->getKind() == BuiltinType::Float ||
5319        BT->getKind() == BuiltinType::Double ||
5320        BT->getKind() == BuiltinType::LongDouble)
5321      return true;
5322  } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5323    unsigned VecSize = getContext().getTypeSize(VT);
5324    if (VecSize == 64 || VecSize == 128)
5325      return true;
5326  }
5327  return false;
5328}
5329
5330bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5331                                                   uint64_t Members) const {
5332  return Members <= 4;
5333}
5334
5335Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5336                              QualType Ty) const {
5337  CharUnits SlotSize = CharUnits::fromQuantity(4);
5338
5339  // Empty records are ignored for parameter passing purposes.
5340  if (isEmptyRecord(getContext(), Ty, true)) {
5341    Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
5342    Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5343    return Addr;
5344  }
5345
5346  auto TyInfo = getContext().getTypeInfoInChars(Ty);
5347  CharUnits TyAlignForABI = TyInfo.second;
5348
5349  // Use indirect if size of the illegal vector is bigger than 16 bytes.
5350  bool IsIndirect = false;
5351  const Type *Base = nullptr;
5352  uint64_t Members = 0;
5353  if (TyInfo.first > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
5354    IsIndirect = true;
5355
5356  // ARMv7k passes structs bigger than 16 bytes indirectly, in space
5357  // allocated by the caller.
5358  } else if (TyInfo.first > CharUnits::fromQuantity(16) &&
5359             getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5360             !isHomogeneousAggregate(Ty, Base, Members)) {
5361    IsIndirect = true;
5362
5363  // Otherwise, bound the type's ABI alignment.
5364  // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
5365  // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
5366  // Our callers should be prepared to handle an under-aligned address.
5367  } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5368             getABIKind() == ARMABIInfo::AAPCS) {
5369    TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5370    TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
5371  } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5372    // ARMv7k allows type alignment up to 16 bytes.
5373    TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
5374    TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
5375  } else {
5376    TyAlignForABI = CharUnits::fromQuantity(4);
5377  }
5378  TyInfo.second = TyAlignForABI;
5379
5380  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
5381                          SlotSize, /*AllowHigherAlign*/ true);
5382}
5383
5384//===----------------------------------------------------------------------===//
5385// NVPTX ABI Implementation
5386//===----------------------------------------------------------------------===//
5387
5388namespace {
5389
5390class NVPTXABIInfo : public ABIInfo {
5391public:
5392  NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
5393
5394  ABIArgInfo classifyReturnType(QualType RetTy) const;
5395  ABIArgInfo classifyArgumentType(QualType Ty) const;
5396
5397  void computeInfo(CGFunctionInfo &FI) const override;
5398  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5399                    QualType Ty) const override;
5400};
5401
5402class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
5403public:
5404  NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
5405    : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
5406
5407  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5408                           CodeGen::CodeGenModule &M) const override;
5409private:
5410  // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
5411  // resulting MDNode to the nvvm.annotations MDNode.
5412  static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
5413};
5414
5415ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
5416  if (RetTy->isVoidType())
5417    return ABIArgInfo::getIgnore();
5418
5419  // note: this is different from default ABI
5420  if (!RetTy->isScalarType())
5421    return ABIArgInfo::getDirect();
5422
5423  // Treat an enum type as its underlying type.
5424  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5425    RetTy = EnumTy->getDecl()->getIntegerType();
5426
5427  return (RetTy->isPromotableIntegerType() ?
5428          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5429}
5430
5431ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
5432  // Treat an enum type as its underlying type.
5433  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5434    Ty = EnumTy->getDecl()->getIntegerType();
5435
5436  // Return aggregates type as indirect by value
5437  if (isAggregateTypeForABI(Ty))
5438    return getNaturalAlignIndirect(Ty, /* byval */ true);
5439
5440  return (Ty->isPromotableIntegerType() ?
5441          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5442}
5443
5444void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
5445  if (!getCXXABI().classifyReturnType(FI))
5446    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5447  for (auto &I : FI.arguments())
5448    I.info = classifyArgumentType(I.type);
5449
5450  // Always honor user-specified calling convention.
5451  if (FI.getCallingConvention() != llvm::CallingConv::C)
5452    return;
5453
5454  FI.setEffectiveCallingConvention(getRuntimeCC());
5455}
5456
5457Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5458                                QualType Ty) const {
5459  llvm_unreachable("NVPTX does not support varargs");
5460}
5461
5462void NVPTXTargetCodeGenInfo::
5463setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5464                    CodeGen::CodeGenModule &M) const{
5465  const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5466  if (!FD) return;
5467
5468  llvm::Function *F = cast<llvm::Function>(GV);
5469
5470  // Perform special handling in OpenCL mode
5471  if (M.getLangOpts().OpenCL) {
5472    // Use OpenCL function attributes to check for kernel functions
5473    // By default, all functions are device functions
5474    if (FD->hasAttr<OpenCLKernelAttr>()) {
5475      // OpenCL __kernel functions get kernel metadata
5476      // Create !{<func-ref>, metadata !"kernel", i32 1} node
5477      addNVVMMetadata(F, "kernel", 1);
5478      // And kernel functions are not subject to inlining
5479      F->addFnAttr(llvm::Attribute::NoInline);
5480    }
5481  }
5482
5483  // Perform special handling in CUDA mode.
5484  if (M.getLangOpts().CUDA) {
5485    // CUDA __global__ functions get a kernel metadata entry.  Since
5486    // __global__ functions cannot be called from the device, we do not
5487    // need to set the noinline attribute.
5488    if (FD->hasAttr<CUDAGlobalAttr>()) {
5489      // Create !{<func-ref>, metadata !"kernel", i32 1} node
5490      addNVVMMetadata(F, "kernel", 1);
5491    }
5492    if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
5493      // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
5494      llvm::APSInt MaxThreads(32);
5495      MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
5496      if (MaxThreads > 0)
5497        addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
5498
5499      // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
5500      // not specified in __launch_bounds__ or if the user specified a 0 value,
5501      // we don't have to add a PTX directive.
5502      if (Attr->getMinBlocks()) {
5503        llvm::APSInt MinBlocks(32);
5504        MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
5505        if (MinBlocks > 0)
5506          // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
5507          addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
5508      }
5509    }
5510  }
5511}
5512
5513void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
5514                                             int Operand) {
5515  llvm::Module *M = F->getParent();
5516  llvm::LLVMContext &Ctx = M->getContext();
5517
5518  // Get "nvvm.annotations" metadata node
5519  llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
5520
5521  llvm::Metadata *MDVals[] = {
5522      llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
5523      llvm::ConstantAsMetadata::get(
5524          llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
5525  // Append metadata to nvvm.annotations
5526  MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
5527}
5528}
5529
5530//===----------------------------------------------------------------------===//
5531// SystemZ ABI Implementation
5532//===----------------------------------------------------------------------===//
5533
5534namespace {
5535
5536class SystemZABIInfo : public ABIInfo {
5537  bool HasVector;
5538
5539public:
5540  SystemZABIInfo(CodeGenTypes &CGT, bool HV)
5541    : ABIInfo(CGT), HasVector(HV) {}
5542
5543  bool isPromotableIntegerType(QualType Ty) const;
5544  bool isCompoundType(QualType Ty) const;
5545  bool isVectorArgumentType(QualType Ty) const;
5546  bool isFPArgumentType(QualType Ty) const;
5547  QualType GetSingleElementType(QualType Ty) const;
5548
5549  ABIArgInfo classifyReturnType(QualType RetTy) const;
5550  ABIArgInfo classifyArgumentType(QualType ArgTy) const;
5551
5552  void computeInfo(CGFunctionInfo &FI) const override {
5553    if (!getCXXABI().classifyReturnType(FI))
5554      FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
5555    for (auto &I : FI.arguments())
5556      I.info = classifyArgumentType(I.type);
5557  }
5558
5559  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5560                    QualType Ty) const override;
5561};
5562
5563class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
5564public:
5565  SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
5566    : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
5567};
5568
5569}
5570
5571bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
5572  // Treat an enum type as its underlying type.
5573  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5574    Ty = EnumTy->getDecl()->getIntegerType();
5575
5576  // Promotable integer types are required to be promoted by the ABI.
5577  if (Ty->isPromotableIntegerType())
5578    return true;
5579
5580  // 32-bit values must also be promoted.
5581  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5582    switch (BT->getKind()) {
5583    case BuiltinType::Int:
5584    case BuiltinType::UInt:
5585      return true;
5586    default:
5587      return false;
5588    }
5589  return false;
5590}
5591
5592bool SystemZABIInfo::isCompoundType(QualType Ty) const {
5593  return (Ty->isAnyComplexType() ||
5594          Ty->isVectorType() ||
5595          isAggregateTypeForABI(Ty));
5596}
5597
5598bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
5599  return (HasVector &&
5600          Ty->isVectorType() &&
5601          getContext().getTypeSize(Ty) <= 128);
5602}
5603
5604bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
5605  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
5606    switch (BT->getKind()) {
5607    case BuiltinType::Float:
5608    case BuiltinType::Double:
5609      return true;
5610    default:
5611      return false;
5612    }
5613
5614  return false;
5615}
5616
5617QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
5618  if (const RecordType *RT = Ty->getAsStructureType()) {
5619    const RecordDecl *RD = RT->getDecl();
5620    QualType Found;
5621
5622    // If this is a C++ record, check the bases first.
5623    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
5624      for (const auto &I : CXXRD->bases()) {
5625        QualType Base = I.getType();
5626
5627        // Empty bases don't affect things either way.
5628        if (isEmptyRecord(getContext(), Base, true))
5629          continue;
5630
5631        if (!Found.isNull())
5632          return Ty;
5633        Found = GetSingleElementType(Base);
5634      }
5635
5636    // Check the fields.
5637    for (const auto *FD : RD->fields()) {
5638      // For compatibility with GCC, ignore empty bitfields in C++ mode.
5639      // Unlike isSingleElementStruct(), empty structure and array fields
5640      // do count.  So do anonymous bitfields that aren't zero-sized.
5641      if (getContext().getLangOpts().CPlusPlus &&
5642          FD->isBitField() && FD->getBitWidthValue(getContext()) == 0)
5643        continue;
5644
5645      // Unlike isSingleElementStruct(), arrays do not count.
5646      // Nested structures still do though.
5647      if (!Found.isNull())
5648        return Ty;
5649      Found = GetSingleElementType(FD->getType());
5650    }
5651
5652    // Unlike isSingleElementStruct(), trailing padding is allowed.
5653    // An 8-byte aligned struct s { float f; } is passed as a double.
5654    if (!Found.isNull())
5655      return Found;
5656  }
5657
5658  return Ty;
5659}
5660
5661Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5662                                  QualType Ty) const {
5663  // Assume that va_list type is correct; should be pointer to LLVM type:
5664  // struct {
5665  //   i64 __gpr;
5666  //   i64 __fpr;
5667  //   i8 *__overflow_arg_area;
5668  //   i8 *__reg_save_area;
5669  // };
5670
5671  // Every non-vector argument occupies 8 bytes and is passed by preference
5672  // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
5673  // always passed on the stack.
5674  Ty = getContext().getCanonicalType(Ty);
5675  auto TyInfo = getContext().getTypeInfoInChars(Ty);
5676  llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
5677  llvm::Type *DirectTy = ArgTy;
5678  ABIArgInfo AI = classifyArgumentType(Ty);
5679  bool IsIndirect = AI.isIndirect();
5680  bool InFPRs = false;
5681  bool IsVector = false;
5682  CharUnits UnpaddedSize;
5683  CharUnits DirectAlign;
5684  if (IsIndirect) {
5685    DirectTy = llvm::PointerType::getUnqual(DirectTy);
5686    UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
5687  } else {
5688    if (AI.getCoerceToType())
5689      ArgTy = AI.getCoerceToType();
5690    InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
5691    IsVector = ArgTy->isVectorTy();
5692    UnpaddedSize = TyInfo.first;
5693    DirectAlign = TyInfo.second;
5694  }
5695  CharUnits PaddedSize = CharUnits::fromQuantity(8);
5696  if (IsVector && UnpaddedSize > PaddedSize)
5697    PaddedSize = CharUnits::fromQuantity(16);
5698  assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
5699
5700  CharUnits Padding = (PaddedSize - UnpaddedSize);
5701
5702  llvm::Type *IndexTy = CGF.Int64Ty;
5703  llvm::Value *PaddedSizeV =
5704    llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
5705
5706  if (IsVector) {
5707    // Work out the address of a vector argument on the stack.
5708    // Vector arguments are always passed in the high bits of a
5709    // single (8 byte) or double (16 byte) stack slot.
5710    Address OverflowArgAreaPtr =
5711      CGF.Builder.CreateStructGEP(VAListAddr, 2, CharUnits::fromQuantity(16),
5712                                  "overflow_arg_area_ptr");
5713    Address OverflowArgArea =
5714      Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
5715              TyInfo.second);
5716    Address MemAddr =
5717      CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
5718
5719    // Update overflow_arg_area_ptr pointer
5720    llvm::Value *NewOverflowArgArea =
5721      CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
5722                            "overflow_arg_area");
5723    CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5724
5725    return MemAddr;
5726  }
5727
5728  assert(PaddedSize.getQuantity() == 8);
5729
5730  unsigned MaxRegs, RegCountField, RegSaveIndex;
5731  CharUnits RegPadding;
5732  if (InFPRs) {
5733    MaxRegs = 4; // Maximum of 4 FPR arguments
5734    RegCountField = 1; // __fpr
5735    RegSaveIndex = 16; // save offset for f0
5736    RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
5737  } else {
5738    MaxRegs = 5; // Maximum of 5 GPR arguments
5739    RegCountField = 0; // __gpr
5740    RegSaveIndex = 2; // save offset for r2
5741    RegPadding = Padding; // values are passed in the low bits of a GPR
5742  }
5743
5744  Address RegCountPtr = CGF.Builder.CreateStructGEP(
5745      VAListAddr, RegCountField, RegCountField * CharUnits::fromQuantity(8),
5746      "reg_count_ptr");
5747  llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
5748  llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
5749  llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
5750                                                 "fits_in_regs");
5751
5752  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5753  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
5754  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5755  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
5756
5757  // Emit code to load the value if it was passed in registers.
5758  CGF.EmitBlock(InRegBlock);
5759
5760  // Work out the address of an argument register.
5761  llvm::Value *ScaledRegCount =
5762    CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
5763  llvm::Value *RegBase =
5764    llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
5765                                      + RegPadding.getQuantity());
5766  llvm::Value *RegOffset =
5767    CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
5768  Address RegSaveAreaPtr =
5769      CGF.Builder.CreateStructGEP(VAListAddr, 3, CharUnits::fromQuantity(24),
5770                                  "reg_save_area_ptr");
5771  llvm::Value *RegSaveArea =
5772    CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
5773  Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
5774                                           "raw_reg_addr"),
5775                     PaddedSize);
5776  Address RegAddr =
5777    CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
5778
5779  // Update the register count
5780  llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
5781  llvm::Value *NewRegCount =
5782    CGF.Builder.CreateAdd(RegCount, One, "reg_count");
5783  CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
5784  CGF.EmitBranch(ContBlock);
5785
5786  // Emit code to load the value if it was passed in memory.
5787  CGF.EmitBlock(InMemBlock);
5788
5789  // Work out the address of a stack argument.
5790  Address OverflowArgAreaPtr = CGF.Builder.CreateStructGEP(
5791      VAListAddr, 2, CharUnits::fromQuantity(16), "overflow_arg_area_ptr");
5792  Address OverflowArgArea =
5793    Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
5794            PaddedSize);
5795  Address RawMemAddr =
5796    CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
5797  Address MemAddr =
5798    CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
5799
5800  // Update overflow_arg_area_ptr pointer
5801  llvm::Value *NewOverflowArgArea =
5802    CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
5803                          "overflow_arg_area");
5804  CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
5805  CGF.EmitBranch(ContBlock);
5806
5807  // Return the appropriate result.
5808  CGF.EmitBlock(ContBlock);
5809  Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
5810                                 MemAddr, InMemBlock, "va_arg.addr");
5811
5812  if (IsIndirect)
5813    ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
5814                      TyInfo.second);
5815
5816  return ResAddr;
5817}
5818
5819ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
5820  if (RetTy->isVoidType())
5821    return ABIArgInfo::getIgnore();
5822  if (isVectorArgumentType(RetTy))
5823    return ABIArgInfo::getDirect();
5824  if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
5825    return getNaturalAlignIndirect(RetTy);
5826  return (isPromotableIntegerType(RetTy) ?
5827          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
5828}
5829
5830ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
5831  // Handle the generic C++ ABI.
5832  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
5833    return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5834
5835  // Integers and enums are extended to full register width.
5836  if (isPromotableIntegerType(Ty))
5837    return ABIArgInfo::getExtend();
5838
5839  // Handle vector types and vector-like structure types.  Note that
5840  // as opposed to float-like structure types, we do not allow any
5841  // padding for vector-like structures, so verify the sizes match.
5842  uint64_t Size = getContext().getTypeSize(Ty);
5843  QualType SingleElementTy = GetSingleElementType(Ty);
5844  if (isVectorArgumentType(SingleElementTy) &&
5845      getContext().getTypeSize(SingleElementTy) == Size)
5846    return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
5847
5848  // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
5849  if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
5850    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5851
5852  // Handle small structures.
5853  if (const RecordType *RT = Ty->getAs<RecordType>()) {
5854    // Structures with flexible arrays have variable length, so really
5855    // fail the size test above.
5856    const RecordDecl *RD = RT->getDecl();
5857    if (RD->hasFlexibleArrayMember())
5858      return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5859
5860    // The structure is passed as an unextended integer, a float, or a double.
5861    llvm::Type *PassTy;
5862    if (isFPArgumentType(SingleElementTy)) {
5863      assert(Size == 32 || Size == 64);
5864      if (Size == 32)
5865        PassTy = llvm::Type::getFloatTy(getVMContext());
5866      else
5867        PassTy = llvm::Type::getDoubleTy(getVMContext());
5868    } else
5869      PassTy = llvm::IntegerType::get(getVMContext(), Size);
5870    return ABIArgInfo::getDirect(PassTy);
5871  }
5872
5873  // Non-structure compounds are passed indirectly.
5874  if (isCompoundType(Ty))
5875    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5876
5877  return ABIArgInfo::getDirect(nullptr);
5878}
5879
5880//===----------------------------------------------------------------------===//
5881// MSP430 ABI Implementation
5882//===----------------------------------------------------------------------===//
5883
5884namespace {
5885
5886class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
5887public:
5888  MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
5889    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
5890  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5891                           CodeGen::CodeGenModule &M) const override;
5892};
5893
5894}
5895
5896void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D,
5897                                                  llvm::GlobalValue *GV,
5898                                             CodeGen::CodeGenModule &M) const {
5899  if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
5900    if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
5901      // Handle 'interrupt' attribute:
5902      llvm::Function *F = cast<llvm::Function>(GV);
5903
5904      // Step 1: Set ISR calling convention.
5905      F->setCallingConv(llvm::CallingConv::MSP430_INTR);
5906
5907      // Step 2: Add attributes goodness.
5908      F->addFnAttr(llvm::Attribute::NoInline);
5909
5910      // Step 3: Emit ISR vector alias.
5911      unsigned Num = attr->getNumber() / 2;
5912      llvm::GlobalAlias::create(llvm::Function::ExternalLinkage,
5913                                "__isr_" + Twine(Num), F);
5914    }
5915  }
5916}
5917
5918//===----------------------------------------------------------------------===//
5919// MIPS ABI Implementation.  This works for both little-endian and
5920// big-endian variants.
5921//===----------------------------------------------------------------------===//
5922
5923namespace {
5924class MipsABIInfo : public ABIInfo {
5925  bool IsO32;
5926  unsigned MinABIStackAlignInBytes, StackAlignInBytes;
5927  void CoerceToIntArgs(uint64_t TySize,
5928                       SmallVectorImpl<llvm::Type *> &ArgList) const;
5929  llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
5930  llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
5931  llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
5932public:
5933  MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
5934    ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
5935    StackAlignInBytes(IsO32 ? 8 : 16) {}
5936
5937  ABIArgInfo classifyReturnType(QualType RetTy) const;
5938  ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
5939  void computeInfo(CGFunctionInfo &FI) const override;
5940  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5941                    QualType Ty) const override;
5942  bool shouldSignExtUnsignedType(QualType Ty) const override;
5943};
5944
5945class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
5946  unsigned SizeOfUnwindException;
5947public:
5948  MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
5949    : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
5950      SizeOfUnwindException(IsO32 ? 24 : 32) {}
5951
5952  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
5953    return 29;
5954  }
5955
5956  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5957                           CodeGen::CodeGenModule &CGM) const override {
5958    const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5959    if (!FD) return;
5960    llvm::Function *Fn = cast<llvm::Function>(GV);
5961    if (FD->hasAttr<Mips16Attr>()) {
5962      Fn->addFnAttr("mips16");
5963    }
5964    else if (FD->hasAttr<NoMips16Attr>()) {
5965      Fn->addFnAttr("nomips16");
5966    }
5967
5968    const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
5969    if (!Attr)
5970      return;
5971
5972    const char *Kind;
5973    switch (Attr->getInterrupt()) {
5974    case MipsInterruptAttr::eic:     Kind = "eic"; break;
5975    case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
5976    case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
5977    case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
5978    case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
5979    case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
5980    case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
5981    case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
5982    case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
5983    }
5984
5985    Fn->addFnAttr("interrupt", Kind);
5986
5987  }
5988
5989  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5990                               llvm::Value *Address) const override;
5991
5992  unsigned getSizeOfUnwindException() const override {
5993    return SizeOfUnwindException;
5994  }
5995};
5996}
5997
5998void MipsABIInfo::CoerceToIntArgs(
5999    uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
6000  llvm::IntegerType *IntTy =
6001    llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
6002
6003  // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
6004  for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
6005    ArgList.push_back(IntTy);
6006
6007  // If necessary, add one more integer type to ArgList.
6008  unsigned R = TySize % (MinABIStackAlignInBytes * 8);
6009
6010  if (R)
6011    ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
6012}
6013
6014// In N32/64, an aligned double precision floating point field is passed in
6015// a register.
6016llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
6017  SmallVector<llvm::Type*, 8> ArgList, IntArgList;
6018
6019  if (IsO32) {
6020    CoerceToIntArgs(TySize, ArgList);
6021    return llvm::StructType::get(getVMContext(), ArgList);
6022  }
6023
6024  if (Ty->isComplexType())
6025    return CGT.ConvertType(Ty);
6026
6027  const RecordType *RT = Ty->getAs<RecordType>();
6028
6029  // Unions/vectors are passed in integer registers.
6030  if (!RT || !RT->isStructureOrClassType()) {
6031    CoerceToIntArgs(TySize, ArgList);
6032    return llvm::StructType::get(getVMContext(), ArgList);
6033  }
6034
6035  const RecordDecl *RD = RT->getDecl();
6036  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6037  assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
6038
6039  uint64_t LastOffset = 0;
6040  unsigned idx = 0;
6041  llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
6042
6043  // Iterate over fields in the struct/class and check if there are any aligned
6044  // double fields.
6045  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6046       i != e; ++i, ++idx) {
6047    const QualType Ty = i->getType();
6048    const BuiltinType *BT = Ty->getAs<BuiltinType>();
6049
6050    if (!BT || BT->getKind() != BuiltinType::Double)
6051      continue;
6052
6053    uint64_t Offset = Layout.getFieldOffset(idx);
6054    if (Offset % 64) // Ignore doubles that are not aligned.
6055      continue;
6056
6057    // Add ((Offset - LastOffset) / 64) args of type i64.
6058    for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
6059      ArgList.push_back(I64);
6060
6061    // Add double type.
6062    ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
6063    LastOffset = Offset + 64;
6064  }
6065
6066  CoerceToIntArgs(TySize - LastOffset, IntArgList);
6067  ArgList.append(IntArgList.begin(), IntArgList.end());
6068
6069  return llvm::StructType::get(getVMContext(), ArgList);
6070}
6071
6072llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
6073                                        uint64_t Offset) const {
6074  if (OrigOffset + MinABIStackAlignInBytes > Offset)
6075    return nullptr;
6076
6077  return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
6078}
6079
6080ABIArgInfo
6081MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
6082  Ty = useFirstFieldIfTransparentUnion(Ty);
6083
6084  uint64_t OrigOffset = Offset;
6085  uint64_t TySize = getContext().getTypeSize(Ty);
6086  uint64_t Align = getContext().getTypeAlign(Ty) / 8;
6087
6088  Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
6089                   (uint64_t)StackAlignInBytes);
6090  unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align);
6091  Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
6092
6093  if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
6094    // Ignore empty aggregates.
6095    if (TySize == 0)
6096      return ABIArgInfo::getIgnore();
6097
6098    if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
6099      Offset = OrigOffset + MinABIStackAlignInBytes;
6100      return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6101    }
6102
6103    // If we have reached here, aggregates are passed directly by coercing to
6104    // another structure type. Padding is inserted if the offset of the
6105    // aggregate is unaligned.
6106    ABIArgInfo ArgInfo =
6107        ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
6108                              getPaddingType(OrigOffset, CurrOffset));
6109    ArgInfo.setInReg(true);
6110    return ArgInfo;
6111  }
6112
6113  // Treat an enum type as its underlying type.
6114  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6115    Ty = EnumTy->getDecl()->getIntegerType();
6116
6117  // All integral types are promoted to the GPR width.
6118  if (Ty->isIntegralOrEnumerationType())
6119    return ABIArgInfo::getExtend();
6120
6121  return ABIArgInfo::getDirect(
6122      nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
6123}
6124
6125llvm::Type*
6126MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
6127  const RecordType *RT = RetTy->getAs<RecordType>();
6128  SmallVector<llvm::Type*, 8> RTList;
6129
6130  if (RT && RT->isStructureOrClassType()) {
6131    const RecordDecl *RD = RT->getDecl();
6132    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
6133    unsigned FieldCnt = Layout.getFieldCount();
6134
6135    // N32/64 returns struct/classes in floating point registers if the
6136    // following conditions are met:
6137    // 1. The size of the struct/class is no larger than 128-bit.
6138    // 2. The struct/class has one or two fields all of which are floating
6139    //    point types.
6140    // 3. The offset of the first field is zero (this follows what gcc does).
6141    //
6142    // Any other composite results are returned in integer registers.
6143    //
6144    if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
6145      RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
6146      for (; b != e; ++b) {
6147        const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
6148
6149        if (!BT || !BT->isFloatingPoint())
6150          break;
6151
6152        RTList.push_back(CGT.ConvertType(b->getType()));
6153      }
6154
6155      if (b == e)
6156        return llvm::StructType::get(getVMContext(), RTList,
6157                                     RD->hasAttr<PackedAttr>());
6158
6159      RTList.clear();
6160    }
6161  }
6162
6163  CoerceToIntArgs(Size, RTList);
6164  return llvm::StructType::get(getVMContext(), RTList);
6165}
6166
6167ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
6168  uint64_t Size = getContext().getTypeSize(RetTy);
6169
6170  if (RetTy->isVoidType())
6171    return ABIArgInfo::getIgnore();
6172
6173  // O32 doesn't treat zero-sized structs differently from other structs.
6174  // However, N32/N64 ignores zero sized return values.
6175  if (!IsO32 && Size == 0)
6176    return ABIArgInfo::getIgnore();
6177
6178  if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
6179    if (Size <= 128) {
6180      if (RetTy->isAnyComplexType())
6181        return ABIArgInfo::getDirect();
6182
6183      // O32 returns integer vectors in registers and N32/N64 returns all small
6184      // aggregates in registers.
6185      if (!IsO32 ||
6186          (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
6187        ABIArgInfo ArgInfo =
6188            ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
6189        ArgInfo.setInReg(true);
6190        return ArgInfo;
6191      }
6192    }
6193
6194    return getNaturalAlignIndirect(RetTy);
6195  }
6196
6197  // Treat an enum type as its underlying type.
6198  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6199    RetTy = EnumTy->getDecl()->getIntegerType();
6200
6201  return (RetTy->isPromotableIntegerType() ?
6202          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6203}
6204
6205void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
6206  ABIArgInfo &RetInfo = FI.getReturnInfo();
6207  if (!getCXXABI().classifyReturnType(FI))
6208    RetInfo = classifyReturnType(FI.getReturnType());
6209
6210  // Check if a pointer to an aggregate is passed as a hidden argument.
6211  uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
6212
6213  for (auto &I : FI.arguments())
6214    I.info = classifyArgumentType(I.type, Offset);
6215}
6216
6217Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6218                               QualType OrigTy) const {
6219  QualType Ty = OrigTy;
6220
6221  // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
6222  // Pointers are also promoted in the same way but this only matters for N32.
6223  unsigned SlotSizeInBits = IsO32 ? 32 : 64;
6224  unsigned PtrWidth = getTarget().getPointerWidth(0);
6225  bool DidPromote = false;
6226  if ((Ty->isIntegerType() &&
6227          getContext().getIntWidth(Ty) < SlotSizeInBits) ||
6228      (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
6229    DidPromote = true;
6230    Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
6231                                            Ty->isSignedIntegerType());
6232  }
6233
6234  auto TyInfo = getContext().getTypeInfoInChars(Ty);
6235
6236  // The alignment of things in the argument area is never larger than
6237  // StackAlignInBytes.
6238  TyInfo.second =
6239    std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
6240
6241  // MinABIStackAlignInBytes is the size of argument slots on the stack.
6242  CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
6243
6244  Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6245                          TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
6246
6247
6248  // If there was a promotion, "unpromote" into a temporary.
6249  // TODO: can we just use a pointer into a subset of the original slot?
6250  if (DidPromote) {
6251    Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
6252    llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
6253
6254    // Truncate down to the right width.
6255    llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
6256                                                 : CGF.IntPtrTy);
6257    llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
6258    if (OrigTy->isPointerType())
6259      V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
6260
6261    CGF.Builder.CreateStore(V, Temp);
6262    Addr = Temp;
6263  }
6264
6265  return Addr;
6266}
6267
6268bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const {
6269  int TySize = getContext().getTypeSize(Ty);
6270
6271  // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
6272  if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
6273    return true;
6274
6275  return false;
6276}
6277
6278bool
6279MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6280                                               llvm::Value *Address) const {
6281  // This information comes from gcc's implementation, which seems to
6282  // as canonical as it gets.
6283
6284  // Everything on MIPS is 4 bytes.  Double-precision FP registers
6285  // are aliased to pairs of single-precision FP registers.
6286  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
6287
6288  // 0-31 are the general purpose registers, $0 - $31.
6289  // 32-63 are the floating-point registers, $f0 - $f31.
6290  // 64 and 65 are the multiply/divide registers, $hi and $lo.
6291  // 66 is the (notional, I think) register for signal-handler return.
6292  AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
6293
6294  // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
6295  // They are one bit wide and ignored here.
6296
6297  // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
6298  // (coprocessor 1 is the FP unit)
6299  // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
6300  // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
6301  // 176-181 are the DSP accumulator registers.
6302  AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
6303  return false;
6304}
6305
6306//===----------------------------------------------------------------------===//
6307// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
6308// Currently subclassed only to implement custom OpenCL C function attribute
6309// handling.
6310//===----------------------------------------------------------------------===//
6311
6312namespace {
6313
6314class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
6315public:
6316  TCETargetCodeGenInfo(CodeGenTypes &CGT)
6317    : DefaultTargetCodeGenInfo(CGT) {}
6318
6319  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6320                           CodeGen::CodeGenModule &M) const override;
6321};
6322
6323void TCETargetCodeGenInfo::setTargetAttributes(
6324    const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
6325  const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6326  if (!FD) return;
6327
6328  llvm::Function *F = cast<llvm::Function>(GV);
6329
6330  if (M.getLangOpts().OpenCL) {
6331    if (FD->hasAttr<OpenCLKernelAttr>()) {
6332      // OpenCL C Kernel functions are not subject to inlining
6333      F->addFnAttr(llvm::Attribute::NoInline);
6334      const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
6335      if (Attr) {
6336        // Convert the reqd_work_group_size() attributes to metadata.
6337        llvm::LLVMContext &Context = F->getContext();
6338        llvm::NamedMDNode *OpenCLMetadata =
6339            M.getModule().getOrInsertNamedMetadata(
6340                "opencl.kernel_wg_size_info");
6341
6342        SmallVector<llvm::Metadata *, 5> Operands;
6343        Operands.push_back(llvm::ConstantAsMetadata::get(F));
6344
6345        Operands.push_back(
6346            llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6347                M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
6348        Operands.push_back(
6349            llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6350                M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
6351        Operands.push_back(
6352            llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
6353                M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
6354
6355        // Add a boolean constant operand for "required" (true) or "hint"
6356        // (false) for implementing the work_group_size_hint attr later.
6357        // Currently always true as the hint is not yet implemented.
6358        Operands.push_back(
6359            llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
6360        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
6361      }
6362    }
6363  }
6364}
6365
6366}
6367
6368//===----------------------------------------------------------------------===//
6369// Hexagon ABI Implementation
6370//===----------------------------------------------------------------------===//
6371
6372namespace {
6373
6374class HexagonABIInfo : public ABIInfo {
6375
6376
6377public:
6378  HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6379
6380private:
6381
6382  ABIArgInfo classifyReturnType(QualType RetTy) const;
6383  ABIArgInfo classifyArgumentType(QualType RetTy) const;
6384
6385  void computeInfo(CGFunctionInfo &FI) const override;
6386
6387  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6388                    QualType Ty) const override;
6389};
6390
6391class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
6392public:
6393  HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
6394    :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
6395
6396  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6397    return 29;
6398  }
6399};
6400
6401}
6402
6403void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
6404  if (!getCXXABI().classifyReturnType(FI))
6405    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6406  for (auto &I : FI.arguments())
6407    I.info = classifyArgumentType(I.type);
6408}
6409
6410ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
6411  if (!isAggregateTypeForABI(Ty)) {
6412    // Treat an enum type as its underlying type.
6413    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6414      Ty = EnumTy->getDecl()->getIntegerType();
6415
6416    return (Ty->isPromotableIntegerType() ?
6417            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6418  }
6419
6420  // Ignore empty records.
6421  if (isEmptyRecord(getContext(), Ty, true))
6422    return ABIArgInfo::getIgnore();
6423
6424  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
6425    return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6426
6427  uint64_t Size = getContext().getTypeSize(Ty);
6428  if (Size > 64)
6429    return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
6430    // Pass in the smallest viable integer type.
6431  else if (Size > 32)
6432      return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6433  else if (Size > 16)
6434      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6435  else if (Size > 8)
6436      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6437  else
6438      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6439}
6440
6441ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
6442  if (RetTy->isVoidType())
6443    return ABIArgInfo::getIgnore();
6444
6445  // Large vector types should be returned via memory.
6446  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
6447    return getNaturalAlignIndirect(RetTy);
6448
6449  if (!isAggregateTypeForABI(RetTy)) {
6450    // Treat an enum type as its underlying type.
6451    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6452      RetTy = EnumTy->getDecl()->getIntegerType();
6453
6454    return (RetTy->isPromotableIntegerType() ?
6455            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
6456  }
6457
6458  if (isEmptyRecord(getContext(), RetTy, true))
6459    return ABIArgInfo::getIgnore();
6460
6461  // Aggregates <= 8 bytes are returned in r0; other aggregates
6462  // are returned indirectly.
6463  uint64_t Size = getContext().getTypeSize(RetTy);
6464  if (Size <= 64) {
6465    // Return in the smallest viable integer type.
6466    if (Size <= 8)
6467      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6468    if (Size <= 16)
6469      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6470    if (Size <= 32)
6471      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6472    return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
6473  }
6474
6475  return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
6476}
6477
6478Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6479                                  QualType Ty) const {
6480  // FIXME: Someone needs to audit that this handle alignment correctly.
6481  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
6482                          getContext().getTypeInfoInChars(Ty),
6483                          CharUnits::fromQuantity(4),
6484                          /*AllowHigherAlign*/ true);
6485}
6486
6487//===----------------------------------------------------------------------===//
6488// AMDGPU ABI Implementation
6489//===----------------------------------------------------------------------===//
6490
6491namespace {
6492
6493class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
6494public:
6495  AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
6496    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
6497  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6498                           CodeGen::CodeGenModule &M) const override;
6499};
6500
6501}
6502
6503void AMDGPUTargetCodeGenInfo::setTargetAttributes(
6504  const Decl *D,
6505  llvm::GlobalValue *GV,
6506  CodeGen::CodeGenModule &M) const {
6507  const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6508  if (!FD)
6509    return;
6510
6511  if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
6512    llvm::Function *F = cast<llvm::Function>(GV);
6513    uint32_t NumVGPR = Attr->getNumVGPR();
6514    if (NumVGPR != 0)
6515      F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR));
6516  }
6517
6518  if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
6519    llvm::Function *F = cast<llvm::Function>(GV);
6520    unsigned NumSGPR = Attr->getNumSGPR();
6521    if (NumSGPR != 0)
6522      F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR));
6523  }
6524}
6525
6526
6527//===----------------------------------------------------------------------===//
6528// SPARC v9 ABI Implementation.
6529// Based on the SPARC Compliance Definition version 2.4.1.
6530//
6531// Function arguments a mapped to a nominal "parameter array" and promoted to
6532// registers depending on their type. Each argument occupies 8 or 16 bytes in
6533// the array, structs larger than 16 bytes are passed indirectly.
6534//
6535// One case requires special care:
6536//
6537//   struct mixed {
6538//     int i;
6539//     float f;
6540//   };
6541//
6542// When a struct mixed is passed by value, it only occupies 8 bytes in the
6543// parameter array, but the int is passed in an integer register, and the float
6544// is passed in a floating point register. This is represented as two arguments
6545// with the LLVM IR inreg attribute:
6546//
6547//   declare void f(i32 inreg %i, float inreg %f)
6548//
6549// The code generator will only allocate 4 bytes from the parameter array for
6550// the inreg arguments. All other arguments are allocated a multiple of 8
6551// bytes.
6552//
6553namespace {
6554class SparcV9ABIInfo : public ABIInfo {
6555public:
6556  SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6557
6558private:
6559  ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
6560  void computeInfo(CGFunctionInfo &FI) const override;
6561  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6562                    QualType Ty) const override;
6563
6564  // Coercion type builder for structs passed in registers. The coercion type
6565  // serves two purposes:
6566  //
6567  // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
6568  //    in registers.
6569  // 2. Expose aligned floating point elements as first-level elements, so the
6570  //    code generator knows to pass them in floating point registers.
6571  //
6572  // We also compute the InReg flag which indicates that the struct contains
6573  // aligned 32-bit floats.
6574  //
6575  struct CoerceBuilder {
6576    llvm::LLVMContext &Context;
6577    const llvm::DataLayout &DL;
6578    SmallVector<llvm::Type*, 8> Elems;
6579    uint64_t Size;
6580    bool InReg;
6581
6582    CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
6583      : Context(c), DL(dl), Size(0), InReg(false) {}
6584
6585    // Pad Elems with integers until Size is ToSize.
6586    void pad(uint64_t ToSize) {
6587      assert(ToSize >= Size && "Cannot remove elements");
6588      if (ToSize == Size)
6589        return;
6590
6591      // Finish the current 64-bit word.
6592      uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64);
6593      if (Aligned > Size && Aligned <= ToSize) {
6594        Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
6595        Size = Aligned;
6596      }
6597
6598      // Add whole 64-bit words.
6599      while (Size + 64 <= ToSize) {
6600        Elems.push_back(llvm::Type::getInt64Ty(Context));
6601        Size += 64;
6602      }
6603
6604      // Final in-word padding.
6605      if (Size < ToSize) {
6606        Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
6607        Size = ToSize;
6608      }
6609    }
6610
6611    // Add a floating point element at Offset.
6612    void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
6613      // Unaligned floats are treated as integers.
6614      if (Offset % Bits)
6615        return;
6616      // The InReg flag is only required if there are any floats < 64 bits.
6617      if (Bits < 64)
6618        InReg = true;
6619      pad(Offset);
6620      Elems.push_back(Ty);
6621      Size = Offset + Bits;
6622    }
6623
6624    // Add a struct type to the coercion type, starting at Offset (in bits).
6625    void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
6626      const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
6627      for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
6628        llvm::Type *ElemTy = StrTy->getElementType(i);
6629        uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
6630        switch (ElemTy->getTypeID()) {
6631        case llvm::Type::StructTyID:
6632          addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
6633          break;
6634        case llvm::Type::FloatTyID:
6635          addFloat(ElemOffset, ElemTy, 32);
6636          break;
6637        case llvm::Type::DoubleTyID:
6638          addFloat(ElemOffset, ElemTy, 64);
6639          break;
6640        case llvm::Type::FP128TyID:
6641          addFloat(ElemOffset, ElemTy, 128);
6642          break;
6643        case llvm::Type::PointerTyID:
6644          if (ElemOffset % 64 == 0) {
6645            pad(ElemOffset);
6646            Elems.push_back(ElemTy);
6647            Size += 64;
6648          }
6649          break;
6650        default:
6651          break;
6652        }
6653      }
6654    }
6655
6656    // Check if Ty is a usable substitute for the coercion type.
6657    bool isUsableType(llvm::StructType *Ty) const {
6658      return llvm::makeArrayRef(Elems) == Ty->elements();
6659    }
6660
6661    // Get the coercion type as a literal struct type.
6662    llvm::Type *getType() const {
6663      if (Elems.size() == 1)
6664        return Elems.front();
6665      else
6666        return llvm::StructType::get(Context, Elems);
6667    }
6668  };
6669};
6670} // end anonymous namespace
6671
6672ABIArgInfo
6673SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
6674  if (Ty->isVoidType())
6675    return ABIArgInfo::getIgnore();
6676
6677  uint64_t Size = getContext().getTypeSize(Ty);
6678
6679  // Anything too big to fit in registers is passed with an explicit indirect
6680  // pointer / sret pointer.
6681  if (Size > SizeLimit)
6682    return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6683
6684  // Treat an enum type as its underlying type.
6685  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6686    Ty = EnumTy->getDecl()->getIntegerType();
6687
6688  // Integer types smaller than a register are extended.
6689  if (Size < 64 && Ty->isIntegerType())
6690    return ABIArgInfo::getExtend();
6691
6692  // Other non-aggregates go in registers.
6693  if (!isAggregateTypeForABI(Ty))
6694    return ABIArgInfo::getDirect();
6695
6696  // If a C++ object has either a non-trivial copy constructor or a non-trivial
6697  // destructor, it is passed with an explicit indirect pointer / sret pointer.
6698  if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
6699    return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6700
6701  // This is a small aggregate type that should be passed in registers.
6702  // Build a coercion type from the LLVM struct type.
6703  llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
6704  if (!StrTy)
6705    return ABIArgInfo::getDirect();
6706
6707  CoerceBuilder CB(getVMContext(), getDataLayout());
6708  CB.addStruct(0, StrTy);
6709  CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64));
6710
6711  // Try to use the original type for coercion.
6712  llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
6713
6714  if (CB.InReg)
6715    return ABIArgInfo::getDirectInReg(CoerceTy);
6716  else
6717    return ABIArgInfo::getDirect(CoerceTy);
6718}
6719
6720Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6721                                  QualType Ty) const {
6722  ABIArgInfo AI = classifyType(Ty, 16 * 8);
6723  llvm::Type *ArgTy = CGT.ConvertType(Ty);
6724  if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
6725    AI.setCoerceToType(ArgTy);
6726
6727  CharUnits SlotSize = CharUnits::fromQuantity(8);
6728
6729  CGBuilderTy &Builder = CGF.Builder;
6730  Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
6731  llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
6732
6733  auto TypeInfo = getContext().getTypeInfoInChars(Ty);
6734
6735  Address ArgAddr = Address::invalid();
6736  CharUnits Stride;
6737  switch (AI.getKind()) {
6738  case ABIArgInfo::Expand:
6739  case ABIArgInfo::InAlloca:
6740    llvm_unreachable("Unsupported ABI kind for va_arg");
6741
6742  case ABIArgInfo::Extend: {
6743    Stride = SlotSize;
6744    CharUnits Offset = SlotSize - TypeInfo.first;
6745    ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
6746    break;
6747  }
6748
6749  case ABIArgInfo::Direct: {
6750    auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
6751    Stride = CharUnits::fromQuantity(AllocSize).RoundUpToAlignment(SlotSize);
6752    ArgAddr = Addr;
6753    break;
6754  }
6755
6756  case ABIArgInfo::Indirect:
6757    Stride = SlotSize;
6758    ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
6759    ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
6760                      TypeInfo.second);
6761    break;
6762
6763  case ABIArgInfo::Ignore:
6764    return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
6765  }
6766
6767  // Update VAList.
6768  llvm::Value *NextPtr =
6769    Builder.CreateConstInBoundsByteGEP(Addr.getPointer(), Stride, "ap.next");
6770  Builder.CreateStore(NextPtr, VAListAddr);
6771
6772  return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
6773}
6774
6775void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
6776  FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
6777  for (auto &I : FI.arguments())
6778    I.info = classifyType(I.type, 16 * 8);
6779}
6780
6781namespace {
6782class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
6783public:
6784  SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
6785    : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
6786
6787  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
6788    return 14;
6789  }
6790
6791  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6792                               llvm::Value *Address) const override;
6793};
6794} // end anonymous namespace
6795
6796bool
6797SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
6798                                                llvm::Value *Address) const {
6799  // This is calculated from the LLVM and GCC tables and verified
6800  // against gcc output.  AFAIK all ABIs use the same encoding.
6801
6802  CodeGen::CGBuilderTy &Builder = CGF.Builder;
6803
6804  llvm::IntegerType *i8 = CGF.Int8Ty;
6805  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
6806  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
6807
6808  // 0-31: the 8-byte general-purpose registers
6809  AssignToArrayRange(Builder, Address, Eight8, 0, 31);
6810
6811  // 32-63: f0-31, the 4-byte floating-point registers
6812  AssignToArrayRange(Builder, Address, Four8, 32, 63);
6813
6814  //   Y   = 64
6815  //   PSR = 65
6816  //   WIM = 66
6817  //   TBR = 67
6818  //   PC  = 68
6819  //   NPC = 69
6820  //   FSR = 70
6821  //   CSR = 71
6822  AssignToArrayRange(Builder, Address, Eight8, 64, 71);
6823
6824  // 72-87: d0-15, the 8-byte floating-point registers
6825  AssignToArrayRange(Builder, Address, Eight8, 72, 87);
6826
6827  return false;
6828}
6829
6830
6831//===----------------------------------------------------------------------===//
6832// XCore ABI Implementation
6833//===----------------------------------------------------------------------===//
6834
6835namespace {
6836
6837/// A SmallStringEnc instance is used to build up the TypeString by passing
6838/// it by reference between functions that append to it.
6839typedef llvm::SmallString<128> SmallStringEnc;
6840
6841/// TypeStringCache caches the meta encodings of Types.
6842///
6843/// The reason for caching TypeStrings is two fold:
6844///   1. To cache a type's encoding for later uses;
6845///   2. As a means to break recursive member type inclusion.
6846///
6847/// A cache Entry can have a Status of:
6848///   NonRecursive:   The type encoding is not recursive;
6849///   Recursive:      The type encoding is recursive;
6850///   Incomplete:     An incomplete TypeString;
6851///   IncompleteUsed: An incomplete TypeString that has been used in a
6852///                   Recursive type encoding.
6853///
6854/// A NonRecursive entry will have all of its sub-members expanded as fully
6855/// as possible. Whilst it may contain types which are recursive, the type
6856/// itself is not recursive and thus its encoding may be safely used whenever
6857/// the type is encountered.
6858///
6859/// A Recursive entry will have all of its sub-members expanded as fully as
6860/// possible. The type itself is recursive and it may contain other types which
6861/// are recursive. The Recursive encoding must not be used during the expansion
6862/// of a recursive type's recursive branch. For simplicity the code uses
6863/// IncompleteCount to reject all usage of Recursive encodings for member types.
6864///
6865/// An Incomplete entry is always a RecordType and only encodes its
6866/// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
6867/// are placed into the cache during type expansion as a means to identify and
6868/// handle recursive inclusion of types as sub-members. If there is recursion
6869/// the entry becomes IncompleteUsed.
6870///
6871/// During the expansion of a RecordType's members:
6872///
6873///   If the cache contains a NonRecursive encoding for the member type, the
6874///   cached encoding is used;
6875///
6876///   If the cache contains a Recursive encoding for the member type, the
6877///   cached encoding is 'Swapped' out, as it may be incorrect, and...
6878///
6879///   If the member is a RecordType, an Incomplete encoding is placed into the
6880///   cache to break potential recursive inclusion of itself as a sub-member;
6881///
6882///   Once a member RecordType has been expanded, its temporary incomplete
6883///   entry is removed from the cache. If a Recursive encoding was swapped out
6884///   it is swapped back in;
6885///
6886///   If an incomplete entry is used to expand a sub-member, the incomplete
6887///   entry is marked as IncompleteUsed. The cache keeps count of how many
6888///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
6889///
6890///   If a member's encoding is found to be a NonRecursive or Recursive viz:
6891///   IncompleteUsedCount==0, the member's encoding is added to the cache.
6892///   Else the member is part of a recursive type and thus the recursion has
6893///   been exited too soon for the encoding to be correct for the member.
6894///
6895class TypeStringCache {
6896  enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
6897  struct Entry {
6898    std::string Str;     // The encoded TypeString for the type.
6899    enum Status State;   // Information about the encoding in 'Str'.
6900    std::string Swapped; // A temporary place holder for a Recursive encoding
6901                         // during the expansion of RecordType's members.
6902  };
6903  std::map<const IdentifierInfo *, struct Entry> Map;
6904  unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
6905  unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
6906public:
6907  TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
6908  void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
6909  bool removeIncomplete(const IdentifierInfo *ID);
6910  void addIfComplete(const IdentifierInfo *ID, StringRef Str,
6911                     bool IsRecursive);
6912  StringRef lookupStr(const IdentifierInfo *ID);
6913};
6914
6915/// TypeString encodings for enum & union fields must be order.
6916/// FieldEncoding is a helper for this ordering process.
6917class FieldEncoding {
6918  bool HasName;
6919  std::string Enc;
6920public:
6921  FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
6922  StringRef str() {return Enc.c_str();}
6923  bool operator<(const FieldEncoding &rhs) const {
6924    if (HasName != rhs.HasName) return HasName;
6925    return Enc < rhs.Enc;
6926  }
6927};
6928
6929class XCoreABIInfo : public DefaultABIInfo {
6930public:
6931  XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
6932  Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6933                    QualType Ty) const override;
6934};
6935
6936class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
6937  mutable TypeStringCache TSC;
6938public:
6939  XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
6940    :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
6941  void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
6942                    CodeGen::CodeGenModule &M) const override;
6943};
6944
6945} // End anonymous namespace.
6946
6947Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6948                                QualType Ty) const {
6949  CGBuilderTy &Builder = CGF.Builder;
6950
6951  // Get the VAList.
6952  CharUnits SlotSize = CharUnits::fromQuantity(4);
6953  Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
6954
6955  // Handle the argument.
6956  ABIArgInfo AI = classifyArgumentType(Ty);
6957  CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
6958  llvm::Type *ArgTy = CGT.ConvertType(Ty);
6959  if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
6960    AI.setCoerceToType(ArgTy);
6961  llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
6962
6963  Address Val = Address::invalid();
6964  CharUnits ArgSize = CharUnits::Zero();
6965  switch (AI.getKind()) {
6966  case ABIArgInfo::Expand:
6967  case ABIArgInfo::InAlloca:
6968    llvm_unreachable("Unsupported ABI kind for va_arg");
6969  case ABIArgInfo::Ignore:
6970    Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
6971    ArgSize = CharUnits::Zero();
6972    break;
6973  case ABIArgInfo::Extend:
6974  case ABIArgInfo::Direct:
6975    Val = Builder.CreateBitCast(AP, ArgPtrTy);
6976    ArgSize = CharUnits::fromQuantity(
6977                       getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
6978    ArgSize = ArgSize.RoundUpToAlignment(SlotSize);
6979    break;
6980  case ABIArgInfo::Indirect:
6981    Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
6982    Val = Address(Builder.CreateLoad(Val), TypeAlign);
6983    ArgSize = SlotSize;
6984    break;
6985  }
6986
6987  // Increment the VAList.
6988  if (!ArgSize.isZero()) {
6989    llvm::Value *APN =
6990      Builder.CreateConstInBoundsByteGEP(AP.getPointer(), ArgSize);
6991    Builder.CreateStore(APN, VAListAddr);
6992  }
6993
6994  return Val;
6995}
6996
6997/// During the expansion of a RecordType, an incomplete TypeString is placed
6998/// into the cache as a means to identify and break recursion.
6999/// If there is a Recursive encoding in the cache, it is swapped out and will
7000/// be reinserted by removeIncomplete().
7001/// All other types of encoding should have been used rather than arriving here.
7002void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
7003                                    std::string StubEnc) {
7004  if (!ID)
7005    return;
7006  Entry &E = Map[ID];
7007  assert( (E.Str.empty() || E.State == Recursive) &&
7008         "Incorrectly use of addIncomplete");
7009  assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
7010  E.Swapped.swap(E.Str); // swap out the Recursive
7011  E.Str.swap(StubEnc);
7012  E.State = Incomplete;
7013  ++IncompleteCount;
7014}
7015
7016/// Once the RecordType has been expanded, the temporary incomplete TypeString
7017/// must be removed from the cache.
7018/// If a Recursive was swapped out by addIncomplete(), it will be replaced.
7019/// Returns true if the RecordType was defined recursively.
7020bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
7021  if (!ID)
7022    return false;
7023  auto I = Map.find(ID);
7024  assert(I != Map.end() && "Entry not present");
7025  Entry &E = I->second;
7026  assert( (E.State == Incomplete ||
7027           E.State == IncompleteUsed) &&
7028         "Entry must be an incomplete type");
7029  bool IsRecursive = false;
7030  if (E.State == IncompleteUsed) {
7031    // We made use of our Incomplete encoding, thus we are recursive.
7032    IsRecursive = true;
7033    --IncompleteUsedCount;
7034  }
7035  if (E.Swapped.empty())
7036    Map.erase(I);
7037  else {
7038    // Swap the Recursive back.
7039    E.Swapped.swap(E.Str);
7040    E.Swapped.clear();
7041    E.State = Recursive;
7042  }
7043  --IncompleteCount;
7044  return IsRecursive;
7045}
7046
7047/// Add the encoded TypeString to the cache only if it is NonRecursive or
7048/// Recursive (viz: all sub-members were expanded as fully as possible).
7049void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
7050                                    bool IsRecursive) {
7051  if (!ID || IncompleteUsedCount)
7052    return; // No key or it is is an incomplete sub-type so don't add.
7053  Entry &E = Map[ID];
7054  if (IsRecursive && !E.Str.empty()) {
7055    assert(E.State==Recursive && E.Str.size() == Str.size() &&
7056           "This is not the same Recursive entry");
7057    // The parent container was not recursive after all, so we could have used
7058    // this Recursive sub-member entry after all, but we assumed the worse when
7059    // we started viz: IncompleteCount!=0.
7060    return;
7061  }
7062  assert(E.Str.empty() && "Entry already present");
7063  E.Str = Str.str();
7064  E.State = IsRecursive? Recursive : NonRecursive;
7065}
7066
7067/// Return a cached TypeString encoding for the ID. If there isn't one, or we
7068/// are recursively expanding a type (IncompleteCount != 0) and the cached
7069/// encoding is Recursive, return an empty StringRef.
7070StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
7071  if (!ID)
7072    return StringRef();   // We have no key.
7073  auto I = Map.find(ID);
7074  if (I == Map.end())
7075    return StringRef();   // We have no encoding.
7076  Entry &E = I->second;
7077  if (E.State == Recursive && IncompleteCount)
7078    return StringRef();   // We don't use Recursive encodings for member types.
7079
7080  if (E.State == Incomplete) {
7081    // The incomplete type is being used to break out of recursion.
7082    E.State = IncompleteUsed;
7083    ++IncompleteUsedCount;
7084  }
7085  return E.Str.c_str();
7086}
7087
7088/// The XCore ABI includes a type information section that communicates symbol
7089/// type information to the linker. The linker uses this information to verify
7090/// safety/correctness of things such as array bound and pointers et al.
7091/// The ABI only requires C (and XC) language modules to emit TypeStrings.
7092/// This type information (TypeString) is emitted into meta data for all global
7093/// symbols: definitions, declarations, functions & variables.
7094///
7095/// The TypeString carries type, qualifier, name, size & value details.
7096/// Please see 'Tools Development Guide' section 2.16.2 for format details:
7097/// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
7098/// The output is tested by test/CodeGen/xcore-stringtype.c.
7099///
7100static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7101                          CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
7102
7103/// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
7104void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
7105                                          CodeGen::CodeGenModule &CGM) const {
7106  SmallStringEnc Enc;
7107  if (getTypeString(Enc, D, CGM, TSC)) {
7108    llvm::LLVMContext &Ctx = CGM.getModule().getContext();
7109    llvm::SmallVector<llvm::Metadata *, 2> MDVals;
7110    MDVals.push_back(llvm::ConstantAsMetadata::get(GV));
7111    MDVals.push_back(llvm::MDString::get(Ctx, Enc.str()));
7112    llvm::NamedMDNode *MD =
7113      CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
7114    MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
7115  }
7116}
7117
7118static bool appendType(SmallStringEnc &Enc, QualType QType,
7119                       const CodeGen::CodeGenModule &CGM,
7120                       TypeStringCache &TSC);
7121
7122/// Helper function for appendRecordType().
7123/// Builds a SmallVector containing the encoded field types in declaration
7124/// order.
7125static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
7126                             const RecordDecl *RD,
7127                             const CodeGen::CodeGenModule &CGM,
7128                             TypeStringCache &TSC) {
7129  for (const auto *Field : RD->fields()) {
7130    SmallStringEnc Enc;
7131    Enc += "m(";
7132    Enc += Field->getName();
7133    Enc += "){";
7134    if (Field->isBitField()) {
7135      Enc += "b(";
7136      llvm::raw_svector_ostream OS(Enc);
7137      OS << Field->getBitWidthValue(CGM.getContext());
7138      Enc += ':';
7139    }
7140    if (!appendType(Enc, Field->getType(), CGM, TSC))
7141      return false;
7142    if (Field->isBitField())
7143      Enc += ')';
7144    Enc += '}';
7145    FE.emplace_back(!Field->getName().empty(), Enc);
7146  }
7147  return true;
7148}
7149
7150/// Appends structure and union types to Enc and adds encoding to cache.
7151/// Recursively calls appendType (via extractFieldType) for each field.
7152/// Union types have their fields ordered according to the ABI.
7153static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
7154                             const CodeGen::CodeGenModule &CGM,
7155                             TypeStringCache &TSC, const IdentifierInfo *ID) {
7156  // Append the cached TypeString if we have one.
7157  StringRef TypeString = TSC.lookupStr(ID);
7158  if (!TypeString.empty()) {
7159    Enc += TypeString;
7160    return true;
7161  }
7162
7163  // Start to emit an incomplete TypeString.
7164  size_t Start = Enc.size();
7165  Enc += (RT->isUnionType()? 'u' : 's');
7166  Enc += '(';
7167  if (ID)
7168    Enc += ID->getName();
7169  Enc += "){";
7170
7171  // We collect all encoded fields and order as necessary.
7172  bool IsRecursive = false;
7173  const RecordDecl *RD = RT->getDecl()->getDefinition();
7174  if (RD && !RD->field_empty()) {
7175    // An incomplete TypeString stub is placed in the cache for this RecordType
7176    // so that recursive calls to this RecordType will use it whilst building a
7177    // complete TypeString for this RecordType.
7178    SmallVector<FieldEncoding, 16> FE;
7179    std::string StubEnc(Enc.substr(Start).str());
7180    StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
7181    TSC.addIncomplete(ID, std::move(StubEnc));
7182    if (!extractFieldType(FE, RD, CGM, TSC)) {
7183      (void) TSC.removeIncomplete(ID);
7184      return false;
7185    }
7186    IsRecursive = TSC.removeIncomplete(ID);
7187    // The ABI requires unions to be sorted but not structures.
7188    // See FieldEncoding::operator< for sort algorithm.
7189    if (RT->isUnionType())
7190      std::sort(FE.begin(), FE.end());
7191    // We can now complete the TypeString.
7192    unsigned E = FE.size();
7193    for (unsigned I = 0; I != E; ++I) {
7194      if (I)
7195        Enc += ',';
7196      Enc += FE[I].str();
7197    }
7198  }
7199  Enc += '}';
7200  TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
7201  return true;
7202}
7203
7204/// Appends enum types to Enc and adds the encoding to the cache.
7205static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
7206                           TypeStringCache &TSC,
7207                           const IdentifierInfo *ID) {
7208  // Append the cached TypeString if we have one.
7209  StringRef TypeString = TSC.lookupStr(ID);
7210  if (!TypeString.empty()) {
7211    Enc += TypeString;
7212    return true;
7213  }
7214
7215  size_t Start = Enc.size();
7216  Enc += "e(";
7217  if (ID)
7218    Enc += ID->getName();
7219  Enc += "){";
7220
7221  // We collect all encoded enumerations and order them alphanumerically.
7222  if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
7223    SmallVector<FieldEncoding, 16> FE;
7224    for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
7225         ++I) {
7226      SmallStringEnc EnumEnc;
7227      EnumEnc += "m(";
7228      EnumEnc += I->getName();
7229      EnumEnc += "){";
7230      I->getInitVal().toString(EnumEnc);
7231      EnumEnc += '}';
7232      FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
7233    }
7234    std::sort(FE.begin(), FE.end());
7235    unsigned E = FE.size();
7236    for (unsigned I = 0; I != E; ++I) {
7237      if (I)
7238        Enc += ',';
7239      Enc += FE[I].str();
7240    }
7241  }
7242  Enc += '}';
7243  TSC.addIfComplete(ID, Enc.substr(Start), false);
7244  return true;
7245}
7246
7247/// Appends type's qualifier to Enc.
7248/// This is done prior to appending the type's encoding.
7249static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
7250  // Qualifiers are emitted in alphabetical order.
7251  static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
7252  int Lookup = 0;
7253  if (QT.isConstQualified())
7254    Lookup += 1<<0;
7255  if (QT.isRestrictQualified())
7256    Lookup += 1<<1;
7257  if (QT.isVolatileQualified())
7258    Lookup += 1<<2;
7259  Enc += Table[Lookup];
7260}
7261
7262/// Appends built-in types to Enc.
7263static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
7264  const char *EncType;
7265  switch (BT->getKind()) {
7266    case BuiltinType::Void:
7267      EncType = "0";
7268      break;
7269    case BuiltinType::Bool:
7270      EncType = "b";
7271      break;
7272    case BuiltinType::Char_U:
7273      EncType = "uc";
7274      break;
7275    case BuiltinType::UChar:
7276      EncType = "uc";
7277      break;
7278    case BuiltinType::SChar:
7279      EncType = "sc";
7280      break;
7281    case BuiltinType::UShort:
7282      EncType = "us";
7283      break;
7284    case BuiltinType::Short:
7285      EncType = "ss";
7286      break;
7287    case BuiltinType::UInt:
7288      EncType = "ui";
7289      break;
7290    case BuiltinType::Int:
7291      EncType = "si";
7292      break;
7293    case BuiltinType::ULong:
7294      EncType = "ul";
7295      break;
7296    case BuiltinType::Long:
7297      EncType = "sl";
7298      break;
7299    case BuiltinType::ULongLong:
7300      EncType = "ull";
7301      break;
7302    case BuiltinType::LongLong:
7303      EncType = "sll";
7304      break;
7305    case BuiltinType::Float:
7306      EncType = "ft";
7307      break;
7308    case BuiltinType::Double:
7309      EncType = "d";
7310      break;
7311    case BuiltinType::LongDouble:
7312      EncType = "ld";
7313      break;
7314    default:
7315      return false;
7316  }
7317  Enc += EncType;
7318  return true;
7319}
7320
7321/// Appends a pointer encoding to Enc before calling appendType for the pointee.
7322static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
7323                              const CodeGen::CodeGenModule &CGM,
7324                              TypeStringCache &TSC) {
7325  Enc += "p(";
7326  if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
7327    return false;
7328  Enc += ')';
7329  return true;
7330}
7331
7332/// Appends array encoding to Enc before calling appendType for the element.
7333static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
7334                            const ArrayType *AT,
7335                            const CodeGen::CodeGenModule &CGM,
7336                            TypeStringCache &TSC, StringRef NoSizeEnc) {
7337  if (AT->getSizeModifier() != ArrayType::Normal)
7338    return false;
7339  Enc += "a(";
7340  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
7341    CAT->getSize().toStringUnsigned(Enc);
7342  else
7343    Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
7344  Enc += ':';
7345  // The Qualifiers should be attached to the type rather than the array.
7346  appendQualifier(Enc, QT);
7347  if (!appendType(Enc, AT->getElementType(), CGM, TSC))
7348    return false;
7349  Enc += ')';
7350  return true;
7351}
7352
7353/// Appends a function encoding to Enc, calling appendType for the return type
7354/// and the arguments.
7355static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
7356                             const CodeGen::CodeGenModule &CGM,
7357                             TypeStringCache &TSC) {
7358  Enc += "f{";
7359  if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
7360    return false;
7361  Enc += "}(";
7362  if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
7363    // N.B. we are only interested in the adjusted param types.
7364    auto I = FPT->param_type_begin();
7365    auto E = FPT->param_type_end();
7366    if (I != E) {
7367      do {
7368        if (!appendType(Enc, *I, CGM, TSC))
7369          return false;
7370        ++I;
7371        if (I != E)
7372          Enc += ',';
7373      } while (I != E);
7374      if (FPT->isVariadic())
7375        Enc += ",va";
7376    } else {
7377      if (FPT->isVariadic())
7378        Enc += "va";
7379      else
7380        Enc += '0';
7381    }
7382  }
7383  Enc += ')';
7384  return true;
7385}
7386
7387/// Handles the type's qualifier before dispatching a call to handle specific
7388/// type encodings.
7389static bool appendType(SmallStringEnc &Enc, QualType QType,
7390                       const CodeGen::CodeGenModule &CGM,
7391                       TypeStringCache &TSC) {
7392
7393  QualType QT = QType.getCanonicalType();
7394
7395  if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
7396    // The Qualifiers should be attached to the type rather than the array.
7397    // Thus we don't call appendQualifier() here.
7398    return appendArrayType(Enc, QT, AT, CGM, TSC, "");
7399
7400  appendQualifier(Enc, QT);
7401
7402  if (const BuiltinType *BT = QT->getAs<BuiltinType>())
7403    return appendBuiltinType(Enc, BT);
7404
7405  if (const PointerType *PT = QT->getAs<PointerType>())
7406    return appendPointerType(Enc, PT, CGM, TSC);
7407
7408  if (const EnumType *ET = QT->getAs<EnumType>())
7409    return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
7410
7411  if (const RecordType *RT = QT->getAsStructureType())
7412    return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7413
7414  if (const RecordType *RT = QT->getAsUnionType())
7415    return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
7416
7417  if (const FunctionType *FT = QT->getAs<FunctionType>())
7418    return appendFunctionType(Enc, FT, CGM, TSC);
7419
7420  return false;
7421}
7422
7423static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
7424                          CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
7425  if (!D)
7426    return false;
7427
7428  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7429    if (FD->getLanguageLinkage() != CLanguageLinkage)
7430      return false;
7431    return appendType(Enc, FD->getType(), CGM, TSC);
7432  }
7433
7434  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
7435    if (VD->getLanguageLinkage() != CLanguageLinkage)
7436      return false;
7437    QualType QT = VD->getType().getCanonicalType();
7438    if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
7439      // Global ArrayTypes are given a size of '*' if the size is unknown.
7440      // The Qualifiers should be attached to the type rather than the array.
7441      // Thus we don't call appendQualifier() here.
7442      return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
7443    }
7444    return appendType(Enc, QT, CGM, TSC);
7445  }
7446  return false;
7447}
7448
7449
7450//===----------------------------------------------------------------------===//
7451// Driver code
7452//===----------------------------------------------------------------------===//
7453
7454const llvm::Triple &CodeGenModule::getTriple() const {
7455  return getTarget().getTriple();
7456}
7457
7458bool CodeGenModule::supportsCOMDAT() const {
7459  return !getTriple().isOSBinFormatMachO();
7460}
7461
7462const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
7463  if (TheTargetCodeGenInfo)
7464    return *TheTargetCodeGenInfo;
7465
7466  const llvm::Triple &Triple = getTarget().getTriple();
7467  switch (Triple.getArch()) {
7468  default:
7469    return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
7470
7471  case llvm::Triple::le32:
7472    return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
7473  case llvm::Triple::mips:
7474  case llvm::Triple::mipsel:
7475    if (Triple.getOS() == llvm::Triple::NaCl)
7476      return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
7477    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
7478
7479  case llvm::Triple::mips64:
7480  case llvm::Triple::mips64el:
7481    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
7482
7483  case llvm::Triple::aarch64:
7484  case llvm::Triple::aarch64_be: {
7485    AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
7486    if (getTarget().getABI() == "darwinpcs")
7487      Kind = AArch64ABIInfo::DarwinPCS;
7488
7489    return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind));
7490  }
7491
7492  case llvm::Triple::wasm32:
7493  case llvm::Triple::wasm64:
7494    return *(TheTargetCodeGenInfo = new WebAssemblyTargetCodeGenInfo(Types));
7495
7496  case llvm::Triple::arm:
7497  case llvm::Triple::armeb:
7498  case llvm::Triple::thumb:
7499  case llvm::Triple::thumbeb:
7500    {
7501      if (Triple.getOS() == llvm::Triple::Win32) {
7502        TheTargetCodeGenInfo =
7503            new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP);
7504        return *TheTargetCodeGenInfo;
7505      }
7506
7507      ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
7508      StringRef ABIStr = getTarget().getABI();
7509      if (ABIStr == "apcs-gnu")
7510        Kind = ARMABIInfo::APCS;
7511      else if (ABIStr == "aapcs16")
7512        Kind = ARMABIInfo::AAPCS16_VFP;
7513      else if (CodeGenOpts.FloatABI == "hard" ||
7514               (CodeGenOpts.FloatABI != "soft" &&
7515                Triple.getEnvironment() == llvm::Triple::GNUEABIHF))
7516        Kind = ARMABIInfo::AAPCS_VFP;
7517
7518      return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
7519    }
7520
7521  case llvm::Triple::ppc:
7522    return *(TheTargetCodeGenInfo =
7523             new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft"));
7524  case llvm::Triple::ppc64:
7525    if (Triple.isOSBinFormatELF()) {
7526      PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
7527      if (getTarget().getABI() == "elfv2")
7528        Kind = PPC64_SVR4_ABIInfo::ELFv2;
7529      bool HasQPX = getTarget().getABI() == "elfv1-qpx";
7530
7531      return *(TheTargetCodeGenInfo =
7532               new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
7533    } else
7534      return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
7535  case llvm::Triple::ppc64le: {
7536    assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
7537    PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
7538    if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
7539      Kind = PPC64_SVR4_ABIInfo::ELFv1;
7540    bool HasQPX = getTarget().getABI() == "elfv1-qpx";
7541
7542    return *(TheTargetCodeGenInfo =
7543             new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX));
7544  }
7545
7546  case llvm::Triple::nvptx:
7547  case llvm::Triple::nvptx64:
7548    return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
7549
7550  case llvm::Triple::msp430:
7551    return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
7552
7553  case llvm::Triple::systemz: {
7554    bool HasVector = getTarget().getABI() == "vector";
7555    return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types,
7556                                                                 HasVector));
7557  }
7558
7559  case llvm::Triple::tce:
7560    return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
7561
7562  case llvm::Triple::x86: {
7563    bool IsDarwinVectorABI = Triple.isOSDarwin();
7564    bool RetSmallStructInRegABI =
7565        X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
7566    bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
7567
7568    if (Triple.getOS() == llvm::Triple::Win32) {
7569      return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo(
7570                   Types, IsDarwinVectorABI, RetSmallStructInRegABI,
7571                   IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
7572    } else {
7573      return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo(
7574                   Types, IsDarwinVectorABI, RetSmallStructInRegABI,
7575                   IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
7576                   CodeGenOpts.FloatABI == "soft"));
7577    }
7578  }
7579
7580  case llvm::Triple::x86_64: {
7581    StringRef ABI = getTarget().getABI();
7582    X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 :
7583                               ABI == "avx" ? X86AVXABILevel::AVX :
7584                               X86AVXABILevel::None);
7585
7586    switch (Triple.getOS()) {
7587    case llvm::Triple::Win32:
7588      return *(TheTargetCodeGenInfo =
7589                   new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
7590    case llvm::Triple::PS4:
7591      return *(TheTargetCodeGenInfo =
7592                   new PS4TargetCodeGenInfo(Types, AVXLevel));
7593    default:
7594      return *(TheTargetCodeGenInfo =
7595                   new X86_64TargetCodeGenInfo(Types, AVXLevel));
7596    }
7597  }
7598  case llvm::Triple::hexagon:
7599    return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
7600  case llvm::Triple::r600:
7601    return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
7602  case llvm::Triple::amdgcn:
7603    return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types));
7604  case llvm::Triple::sparcv9:
7605    return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types));
7606  case llvm::Triple::xcore:
7607    return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types));
7608  }
7609}
7610