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