1 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // These classes wrap the information about a call or function
10 // definition used to handle ABI compliancy.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "TargetInfo.h"
15 #include "ABIInfo.h"
16 #include "CGBlocks.h"
17 #include "CGCXXABI.h"
18 #include "CGValue.h"
19 #include "CodeGenFunction.h"
20 #include "clang/AST/Attr.h"
21 #include "clang/AST/RecordLayout.h"
22 #include "clang/Basic/CodeGenOptions.h"
23 #include "clang/CodeGen/CGFunctionInfo.h"
24 #include "clang/CodeGen/SwiftCallingConv.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/ADT/Triple.h"
28 #include "llvm/ADT/Twine.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <algorithm> // std::sort
33 
34 using namespace clang;
35 using namespace CodeGen;
36 
37 // Helper for coercing an aggregate argument or return value into an integer
38 // array of the same size (including padding) and alignment.  This alternate
39 // coercion happens only for the RenderScript ABI and can be removed after
40 // runtimes that rely on it are no longer supported.
41 //
42 // RenderScript assumes that the size of the argument / return value in the IR
43 // is the same as the size of the corresponding qualified type. This helper
44 // coerces the aggregate type into an array of the same size (including
45 // padding).  This coercion is used in lieu of expansion of struct members or
46 // other canonical coercions that return a coerced-type of larger size.
47 //
48 // Ty          - The argument / return value type
49 // Context     - The associated ASTContext
50 // LLVMContext - The associated LLVMContext
51 static ABIArgInfo coerceToIntArray(QualType Ty,
52                                    ASTContext &Context,
53                                    llvm::LLVMContext &LLVMContext) {
54   // Alignment and Size are measured in bits.
55   const uint64_t Size = Context.getTypeSize(Ty);
56   const uint64_t Alignment = Context.getTypeAlign(Ty);
57   llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment);
58   const uint64_t NumElements = (Size + Alignment - 1) / Alignment;
59   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
60 }
61 
62 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
63                                llvm::Value *Array,
64                                llvm::Value *Value,
65                                unsigned FirstIndex,
66                                unsigned LastIndex) {
67   // Alternatively, we could emit this as a loop in the source.
68   for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
69     llvm::Value *Cell =
70         Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I);
71     Builder.CreateAlignedStore(Value, Cell, CharUnits::One());
72   }
73 }
74 
75 static bool isAggregateTypeForABI(QualType T) {
76   return !CodeGenFunction::hasScalarEvaluationKind(T) ||
77          T->isMemberFunctionPointerType();
78 }
79 
80 ABIArgInfo
81 ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign,
82                                  llvm::Type *Padding) const {
83   return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty),
84                                  ByRef, Realign, Padding);
85 }
86 
87 ABIArgInfo
88 ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const {
89   return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty),
90                                       /*ByRef*/ false, Realign);
91 }
92 
93 Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
94                              QualType Ty) const {
95   return Address::invalid();
96 }
97 
98 ABIInfo::~ABIInfo() {}
99 
100 /// Does the given lowering require more than the given number of
101 /// registers when expanded?
102 ///
103 /// This is intended to be the basis of a reasonable basic implementation
104 /// of should{Pass,Return}IndirectlyForSwift.
105 ///
106 /// For most targets, a limit of four total registers is reasonable; this
107 /// limits the amount of code required in order to move around the value
108 /// in case it wasn't produced immediately prior to the call by the caller
109 /// (or wasn't produced in exactly the right registers) or isn't used
110 /// immediately within the callee.  But some targets may need to further
111 /// limit the register count due to an inability to support that many
112 /// return registers.
113 static bool occupiesMoreThan(CodeGenTypes &cgt,
114                              ArrayRef<llvm::Type*> scalarTypes,
115                              unsigned maxAllRegisters) {
116   unsigned intCount = 0, fpCount = 0;
117   for (llvm::Type *type : scalarTypes) {
118     if (type->isPointerTy()) {
119       intCount++;
120     } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) {
121       auto ptrWidth = cgt.getTarget().getPointerWidth(0);
122       intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth;
123     } else {
124       assert(type->isVectorTy() || type->isFloatingPointTy());
125       fpCount++;
126     }
127   }
128 
129   return (intCount + fpCount > maxAllRegisters);
130 }
131 
132 bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
133                                              llvm::Type *eltTy,
134                                              unsigned numElts) const {
135   // The default implementation of this assumes that the target guarantees
136   // 128-bit SIMD support but nothing more.
137   return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16);
138 }
139 
140 static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT,
141                                               CGCXXABI &CXXABI) {
142   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
143   if (!RD) {
144     if (!RT->getDecl()->canPassInRegisters())
145       return CGCXXABI::RAA_Indirect;
146     return CGCXXABI::RAA_Default;
147   }
148   return CXXABI.getRecordArgABI(RD);
149 }
150 
151 static CGCXXABI::RecordArgABI getRecordArgABI(QualType T,
152                                               CGCXXABI &CXXABI) {
153   const RecordType *RT = T->getAs<RecordType>();
154   if (!RT)
155     return CGCXXABI::RAA_Default;
156   return getRecordArgABI(RT, CXXABI);
157 }
158 
159 static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI,
160                                const ABIInfo &Info) {
161   QualType Ty = FI.getReturnType();
162 
163   if (const auto *RT = Ty->getAs<RecordType>())
164     if (!isa<CXXRecordDecl>(RT->getDecl()) &&
165         !RT->getDecl()->canPassInRegisters()) {
166       FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty);
167       return true;
168     }
169 
170   return CXXABI.classifyReturnType(FI);
171 }
172 
173 /// Pass transparent unions as if they were the type of the first element. Sema
174 /// should ensure that all elements of the union have the same "machine type".
175 static QualType useFirstFieldIfTransparentUnion(QualType Ty) {
176   if (const RecordType *UT = Ty->getAsUnionType()) {
177     const RecordDecl *UD = UT->getDecl();
178     if (UD->hasAttr<TransparentUnionAttr>()) {
179       assert(!UD->field_empty() && "sema created an empty transparent union");
180       return UD->field_begin()->getType();
181     }
182   }
183   return Ty;
184 }
185 
186 CGCXXABI &ABIInfo::getCXXABI() const {
187   return CGT.getCXXABI();
188 }
189 
190 ASTContext &ABIInfo::getContext() const {
191   return CGT.getContext();
192 }
193 
194 llvm::LLVMContext &ABIInfo::getVMContext() const {
195   return CGT.getLLVMContext();
196 }
197 
198 const llvm::DataLayout &ABIInfo::getDataLayout() const {
199   return CGT.getDataLayout();
200 }
201 
202 const TargetInfo &ABIInfo::getTarget() const {
203   return CGT.getTarget();
204 }
205 
206 const CodeGenOptions &ABIInfo::getCodeGenOpts() const {
207   return CGT.getCodeGenOpts();
208 }
209 
210 bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); }
211 
212 bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
213   return false;
214 }
215 
216 bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
217                                                 uint64_t Members) const {
218   return false;
219 }
220 
221 LLVM_DUMP_METHOD void ABIArgInfo::dump() const {
222   raw_ostream &OS = llvm::errs();
223   OS << "(ABIArgInfo Kind=";
224   switch (TheKind) {
225   case Direct:
226     OS << "Direct Type=";
227     if (llvm::Type *Ty = getCoerceToType())
228       Ty->print(OS);
229     else
230       OS << "null";
231     break;
232   case Extend:
233     OS << "Extend";
234     break;
235   case Ignore:
236     OS << "Ignore";
237     break;
238   case InAlloca:
239     OS << "InAlloca Offset=" << getInAllocaFieldIndex();
240     break;
241   case Indirect:
242     OS << "Indirect Align=" << getIndirectAlign().getQuantity()
243        << " ByVal=" << getIndirectByVal()
244        << " Realign=" << getIndirectRealign();
245     break;
246   case Expand:
247     OS << "Expand";
248     break;
249   case CoerceAndExpand:
250     OS << "CoerceAndExpand Type=";
251     getCoerceAndExpandType()->print(OS);
252     break;
253   }
254   OS << ")\n";
255 }
256 
257 // Dynamically round a pointer up to a multiple of the given alignment.
258 static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF,
259                                                   llvm::Value *Ptr,
260                                                   CharUnits Align) {
261   llvm::Value *PtrAsInt = Ptr;
262   // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align;
263   PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy);
264   PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt,
265         llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1));
266   PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt,
267            llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity()));
268   PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt,
269                                         Ptr->getType(),
270                                         Ptr->getName() + ".aligned");
271   return PtrAsInt;
272 }
273 
274 /// Emit va_arg for a platform using the common void* representation,
275 /// where arguments are simply emitted in an array of slots on the stack.
276 ///
277 /// This version implements the core direct-value passing rules.
278 ///
279 /// \param SlotSize - The size and alignment of a stack slot.
280 ///   Each argument will be allocated to a multiple of this number of
281 ///   slots, and all the slots will be aligned to this value.
282 /// \param AllowHigherAlign - The slot alignment is not a cap;
283 ///   an argument type with an alignment greater than the slot size
284 ///   will be emitted on a higher-alignment address, potentially
285 ///   leaving one or more empty slots behind as padding.  If this
286 ///   is false, the returned address might be less-aligned than
287 ///   DirectAlign.
288 static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF,
289                                       Address VAListAddr,
290                                       llvm::Type *DirectTy,
291                                       CharUnits DirectSize,
292                                       CharUnits DirectAlign,
293                                       CharUnits SlotSize,
294                                       bool AllowHigherAlign) {
295   // Cast the element type to i8* if necessary.  Some platforms define
296   // va_list as a struct containing an i8* instead of just an i8*.
297   if (VAListAddr.getElementType() != CGF.Int8PtrTy)
298     VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy);
299 
300   llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur");
301 
302   // If the CC aligns values higher than the slot size, do so if needed.
303   Address Addr = Address::invalid();
304   if (AllowHigherAlign && DirectAlign > SlotSize) {
305     Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign),
306                                                  DirectAlign);
307   } else {
308     Addr = Address(Ptr, SlotSize);
309   }
310 
311   // Advance the pointer past the argument, then store that back.
312   CharUnits FullDirectSize = DirectSize.alignTo(SlotSize);
313   Address NextPtr =
314       CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next");
315   CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
316 
317   // If the argument is smaller than a slot, and this is a big-endian
318   // target, the argument will be right-adjusted in its slot.
319   if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() &&
320       !DirectTy->isStructTy()) {
321     Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize);
322   }
323 
324   Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy);
325   return Addr;
326 }
327 
328 /// Emit va_arg for a platform using the common void* representation,
329 /// where arguments are simply emitted in an array of slots on the stack.
330 ///
331 /// \param IsIndirect - Values of this type are passed indirectly.
332 /// \param ValueInfo - The size and alignment of this type, generally
333 ///   computed with getContext().getTypeInfoInChars(ValueTy).
334 /// \param SlotSizeAndAlign - The size and alignment of a stack slot.
335 ///   Each argument will be allocated to a multiple of this number of
336 ///   slots, and all the slots will be aligned to this value.
337 /// \param AllowHigherAlign - The slot alignment is not a cap;
338 ///   an argument type with an alignment greater than the slot size
339 ///   will be emitted on a higher-alignment address, potentially
340 ///   leaving one or more empty slots behind as padding.
341 static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr,
342                                 QualType ValueTy, bool IsIndirect,
343                                 std::pair<CharUnits, CharUnits> ValueInfo,
344                                 CharUnits SlotSizeAndAlign,
345                                 bool AllowHigherAlign) {
346   // The size and alignment of the value that was passed directly.
347   CharUnits DirectSize, DirectAlign;
348   if (IsIndirect) {
349     DirectSize = CGF.getPointerSize();
350     DirectAlign = CGF.getPointerAlign();
351   } else {
352     DirectSize = ValueInfo.first;
353     DirectAlign = ValueInfo.second;
354   }
355 
356   // Cast the address we've calculated to the right type.
357   llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy);
358   if (IsIndirect)
359     DirectTy = DirectTy->getPointerTo(0);
360 
361   Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy,
362                                         DirectSize, DirectAlign,
363                                         SlotSizeAndAlign,
364                                         AllowHigherAlign);
365 
366   if (IsIndirect) {
367     Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second);
368   }
369 
370   return Addr;
371 
372 }
373 
374 static Address emitMergePHI(CodeGenFunction &CGF,
375                             Address Addr1, llvm::BasicBlock *Block1,
376                             Address Addr2, llvm::BasicBlock *Block2,
377                             const llvm::Twine &Name = "") {
378   assert(Addr1.getType() == Addr2.getType());
379   llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name);
380   PHI->addIncoming(Addr1.getPointer(), Block1);
381   PHI->addIncoming(Addr2.getPointer(), Block2);
382   CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment());
383   return Address(PHI, Align);
384 }
385 
386 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
387 
388 // If someone can figure out a general rule for this, that would be great.
389 // It's probably just doomed to be platform-dependent, though.
390 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
391   // Verified for:
392   //   x86-64     FreeBSD, Linux, Darwin
393   //   x86-32     FreeBSD, Linux, Darwin
394   //   PowerPC    Linux, Darwin
395   //   ARM        Darwin (*not* EABI)
396   //   AArch64    Linux
397   return 32;
398 }
399 
400 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
401                                      const FunctionNoProtoType *fnType) const {
402   // The following conventions are known to require this to be false:
403   //   x86_stdcall
404   //   MIPS
405   // For everything else, we just prefer false unless we opt out.
406   return false;
407 }
408 
409 void
410 TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib,
411                                              llvm::SmallString<24> &Opt) const {
412   // This assumes the user is passing a library name like "rt" instead of a
413   // filename like "librt.a/so", and that they don't care whether it's static or
414   // dynamic.
415   Opt = "-l";
416   Opt += Lib;
417 }
418 
419 unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const {
420   // OpenCL kernels are called via an explicit runtime API with arguments
421   // set with clSetKernelArg(), not as normal sub-functions.
422   // Return SPIR_KERNEL by default as the kernel calling convention to
423   // ensure the fingerprint is fixed such way that each OpenCL argument
424   // gets one matching argument in the produced kernel function argument
425   // list to enable feasible implementation of clSetKernelArg() with
426   // aggregates etc. In case we would use the default C calling conv here,
427   // clSetKernelArg() might break depending on the target-specific
428   // conventions; different targets might split structs passed as values
429   // to multiple function arguments etc.
430   return llvm::CallingConv::SPIR_KERNEL;
431 }
432 
433 llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
434     llvm::PointerType *T, QualType QT) const {
435   return llvm::ConstantPointerNull::get(T);
436 }
437 
438 LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
439                                                    const VarDecl *D) const {
440   assert(!CGM.getLangOpts().OpenCL &&
441          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
442          "Address space agnostic languages only");
443   return D ? D->getType().getAddressSpace() : LangAS::Default;
444 }
445 
446 llvm::Value *TargetCodeGenInfo::performAddrSpaceCast(
447     CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr,
448     LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const {
449   // Since target may map different address spaces in AST to the same address
450   // space, an address space conversion may end up as a bitcast.
451   if (auto *C = dyn_cast<llvm::Constant>(Src))
452     return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy);
453   // Try to preserve the source's name to make IR more readable.
454   return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
455       Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : "");
456 }
457 
458 llvm::Constant *
459 TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src,
460                                         LangAS SrcAddr, LangAS DestAddr,
461                                         llvm::Type *DestTy) const {
462   // Since target may map different address spaces in AST to the same address
463   // space, an address space conversion may end up as a bitcast.
464   return llvm::ConstantExpr::getPointerCast(Src, DestTy);
465 }
466 
467 llvm::SyncScope::ID
468 TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
469                                       SyncScope Scope,
470                                       llvm::AtomicOrdering Ordering,
471                                       llvm::LLVMContext &Ctx) const {
472   return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */
473 }
474 
475 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
476 
477 /// isEmptyField - Return true iff a the field is "empty", that is it
478 /// is an unnamed bit-field or an (array of) empty record(s).
479 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
480                          bool AllowArrays) {
481   if (FD->isUnnamedBitfield())
482     return true;
483 
484   QualType FT = FD->getType();
485 
486   // Constant arrays of empty records count as empty, strip them off.
487   // Constant arrays of zero length always count as empty.
488   if (AllowArrays)
489     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
490       if (AT->getSize() == 0)
491         return true;
492       FT = AT->getElementType();
493     }
494 
495   const RecordType *RT = FT->getAs<RecordType>();
496   if (!RT)
497     return false;
498 
499   // C++ record fields are never empty, at least in the Itanium ABI.
500   //
501   // FIXME: We should use a predicate for whether this behavior is true in the
502   // current ABI.
503   if (isa<CXXRecordDecl>(RT->getDecl()))
504     return false;
505 
506   return isEmptyRecord(Context, FT, AllowArrays);
507 }
508 
509 /// isEmptyRecord - Return true iff a structure contains only empty
510 /// fields. Note that a structure with a flexible array member is not
511 /// considered empty.
512 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
513   const RecordType *RT = T->getAs<RecordType>();
514   if (!RT)
515     return false;
516   const RecordDecl *RD = RT->getDecl();
517   if (RD->hasFlexibleArrayMember())
518     return false;
519 
520   // If this is a C++ record, check the bases first.
521   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
522     for (const auto &I : CXXRD->bases())
523       if (!isEmptyRecord(Context, I.getType(), true))
524         return false;
525 
526   for (const auto *I : RD->fields())
527     if (!isEmptyField(Context, I, AllowArrays))
528       return false;
529   return true;
530 }
531 
532 /// isSingleElementStruct - Determine if a structure is a "single
533 /// element struct", i.e. it has exactly one non-empty field or
534 /// exactly one field which is itself a single element
535 /// struct. Structures with flexible array members are never
536 /// considered single element structs.
537 ///
538 /// \return The field declaration for the single non-empty field, if
539 /// it exists.
540 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
541   const RecordType *RT = T->getAs<RecordType>();
542   if (!RT)
543     return nullptr;
544 
545   const RecordDecl *RD = RT->getDecl();
546   if (RD->hasFlexibleArrayMember())
547     return nullptr;
548 
549   const Type *Found = nullptr;
550 
551   // If this is a C++ record, check the bases first.
552   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
553     for (const auto &I : CXXRD->bases()) {
554       // Ignore empty records.
555       if (isEmptyRecord(Context, I.getType(), true))
556         continue;
557 
558       // If we already found an element then this isn't a single-element struct.
559       if (Found)
560         return nullptr;
561 
562       // If this is non-empty and not a single element struct, the composite
563       // cannot be a single element struct.
564       Found = isSingleElementStruct(I.getType(), Context);
565       if (!Found)
566         return nullptr;
567     }
568   }
569 
570   // Check for single element.
571   for (const auto *FD : RD->fields()) {
572     QualType FT = FD->getType();
573 
574     // Ignore empty fields.
575     if (isEmptyField(Context, FD, true))
576       continue;
577 
578     // If we already found an element then this isn't a single-element
579     // struct.
580     if (Found)
581       return nullptr;
582 
583     // Treat single element arrays as the element.
584     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
585       if (AT->getSize().getZExtValue() != 1)
586         break;
587       FT = AT->getElementType();
588     }
589 
590     if (!isAggregateTypeForABI(FT)) {
591       Found = FT.getTypePtr();
592     } else {
593       Found = isSingleElementStruct(FT, Context);
594       if (!Found)
595         return nullptr;
596     }
597   }
598 
599   // We don't consider a struct a single-element struct if it has
600   // padding beyond the element type.
601   if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
602     return nullptr;
603 
604   return Found;
605 }
606 
607 namespace {
608 Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
609                        const ABIArgInfo &AI) {
610   // This default implementation defers to the llvm backend's va_arg
611   // instruction. It can handle only passing arguments directly
612   // (typically only handled in the backend for primitive types), or
613   // aggregates passed indirectly by pointer (NOTE: if the "byval"
614   // flag has ABI impact in the callee, this implementation cannot
615   // work.)
616 
617   // Only a few cases are covered here at the moment -- those needed
618   // by the default abi.
619   llvm::Value *Val;
620 
621   if (AI.isIndirect()) {
622     assert(!AI.getPaddingType() &&
623            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
624     assert(
625         !AI.getIndirectRealign() &&
626         "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!");
627 
628     auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty);
629     CharUnits TyAlignForABI = TyInfo.second;
630 
631     llvm::Type *BaseTy =
632         llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty));
633     llvm::Value *Addr =
634         CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy);
635     return Address(Addr, TyAlignForABI);
636   } else {
637     assert((AI.isDirect() || AI.isExtend()) &&
638            "Unexpected ArgInfo Kind in generic VAArg emitter!");
639 
640     assert(!AI.getInReg() &&
641            "Unexpected InReg seen in arginfo in generic VAArg emitter!");
642     assert(!AI.getPaddingType() &&
643            "Unexpected PaddingType seen in arginfo in generic VAArg emitter!");
644     assert(!AI.getDirectOffset() &&
645            "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!");
646     assert(!AI.getCoerceToType() &&
647            "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!");
648 
649     Address Temp = CGF.CreateMemTemp(Ty, "varet");
650     Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty));
651     CGF.Builder.CreateStore(Val, Temp);
652     return Temp;
653   }
654 }
655 
656 /// DefaultABIInfo - The default implementation for ABI specific
657 /// details. This implementation provides information which results in
658 /// self-consistent and sensible LLVM IR generation, but does not
659 /// conform to any particular ABI.
660 class DefaultABIInfo : public ABIInfo {
661 public:
662   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
663 
664   ABIArgInfo classifyReturnType(QualType RetTy) const;
665   ABIArgInfo classifyArgumentType(QualType RetTy) const;
666 
667   void computeInfo(CGFunctionInfo &FI) const override {
668     if (!getCXXABI().classifyReturnType(FI))
669       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
670     for (auto &I : FI.arguments())
671       I.info = classifyArgumentType(I.type);
672   }
673 
674   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
675                     QualType Ty) const override {
676     return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty));
677   }
678 };
679 
680 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
681 public:
682   DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
683     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
684 };
685 
686 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
687   Ty = useFirstFieldIfTransparentUnion(Ty);
688 
689   if (isAggregateTypeForABI(Ty)) {
690     // Records with non-trivial destructors/copy-constructors should not be
691     // passed by value.
692     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
693       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
694 
695     return getNaturalAlignIndirect(Ty);
696   }
697 
698   // Treat an enum type as its underlying type.
699   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
700     Ty = EnumTy->getDecl()->getIntegerType();
701 
702   return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
703                                         : ABIArgInfo::getDirect());
704 }
705 
706 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
707   if (RetTy->isVoidType())
708     return ABIArgInfo::getIgnore();
709 
710   if (isAggregateTypeForABI(RetTy))
711     return getNaturalAlignIndirect(RetTy);
712 
713   // Treat an enum type as its underlying type.
714   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
715     RetTy = EnumTy->getDecl()->getIntegerType();
716 
717   return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
718                                            : ABIArgInfo::getDirect());
719 }
720 
721 //===----------------------------------------------------------------------===//
722 // WebAssembly ABI Implementation
723 //
724 // This is a very simple ABI that relies a lot on DefaultABIInfo.
725 //===----------------------------------------------------------------------===//
726 
727 class WebAssemblyABIInfo final : public SwiftABIInfo {
728   DefaultABIInfo defaultInfo;
729 
730 public:
731   explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT)
732       : SwiftABIInfo(CGT), defaultInfo(CGT) {}
733 
734 private:
735   ABIArgInfo classifyReturnType(QualType RetTy) const;
736   ABIArgInfo classifyArgumentType(QualType Ty) const;
737 
738   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
739   // non-virtual, but computeInfo and EmitVAArg are virtual, so we
740   // overload them.
741   void computeInfo(CGFunctionInfo &FI) const override {
742     if (!getCXXABI().classifyReturnType(FI))
743       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
744     for (auto &Arg : FI.arguments())
745       Arg.info = classifyArgumentType(Arg.type);
746   }
747 
748   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
749                     QualType Ty) const override;
750 
751   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
752                                     bool asReturnValue) const override {
753     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
754   }
755 
756   bool isSwiftErrorInRegister() const override {
757     return false;
758   }
759 };
760 
761 class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
762 public:
763   explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
764       : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
765 
766   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
767                            CodeGen::CodeGenModule &CGM) const override {
768     TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
769     if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
770       if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) {
771         llvm::Function *Fn = cast<llvm::Function>(GV);
772         llvm::AttrBuilder B;
773         B.addAttribute("wasm-import-module", Attr->getImportModule());
774         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
775       }
776       if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) {
777         llvm::Function *Fn = cast<llvm::Function>(GV);
778         llvm::AttrBuilder B;
779         B.addAttribute("wasm-import-name", Attr->getImportName());
780         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
781       }
782       if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) {
783         llvm::Function *Fn = cast<llvm::Function>(GV);
784         llvm::AttrBuilder B;
785         B.addAttribute("wasm-export-name", Attr->getExportName());
786         Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
787       }
788     }
789 
790     if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
791       llvm::Function *Fn = cast<llvm::Function>(GV);
792       if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
793         Fn->addFnAttr("no-prototype");
794     }
795   }
796 };
797 
798 /// Classify argument of given type \p Ty.
799 ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const {
800   Ty = useFirstFieldIfTransparentUnion(Ty);
801 
802   if (isAggregateTypeForABI(Ty)) {
803     // Records with non-trivial destructors/copy-constructors should not be
804     // passed by value.
805     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
806       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
807     // Ignore empty structs/unions.
808     if (isEmptyRecord(getContext(), Ty, true))
809       return ABIArgInfo::getIgnore();
810     // Lower single-element structs to just pass a regular value. TODO: We
811     // could do reasonable-size multiple-element structs too, using getExpand(),
812     // though watch out for things like bitfields.
813     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
814       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
815   }
816 
817   // Otherwise just do the default thing.
818   return defaultInfo.classifyArgumentType(Ty);
819 }
820 
821 ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const {
822   if (isAggregateTypeForABI(RetTy)) {
823     // Records with non-trivial destructors/copy-constructors should not be
824     // returned by value.
825     if (!getRecordArgABI(RetTy, getCXXABI())) {
826       // Ignore empty structs/unions.
827       if (isEmptyRecord(getContext(), RetTy, true))
828         return ABIArgInfo::getIgnore();
829       // Lower single-element structs to just return a regular value. TODO: We
830       // could do reasonable-size multiple-element structs too, using
831       // ABIArgInfo::getDirect().
832       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
833         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
834     }
835   }
836 
837   // Otherwise just do the default thing.
838   return defaultInfo.classifyReturnType(RetTy);
839 }
840 
841 Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
842                                       QualType Ty) const {
843   bool IsIndirect = isAggregateTypeForABI(Ty) &&
844                     !isEmptyRecord(getContext(), Ty, true) &&
845                     !isSingleElementStruct(Ty, getContext());
846   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
847                           getContext().getTypeInfoInChars(Ty),
848                           CharUnits::fromQuantity(4),
849                           /*AllowHigherAlign=*/true);
850 }
851 
852 //===----------------------------------------------------------------------===//
853 // le32/PNaCl bitcode ABI Implementation
854 //
855 // This is a simplified version of the x86_32 ABI.  Arguments and return values
856 // are always passed on the stack.
857 //===----------------------------------------------------------------------===//
858 
859 class PNaClABIInfo : public ABIInfo {
860  public:
861   PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
862 
863   ABIArgInfo classifyReturnType(QualType RetTy) const;
864   ABIArgInfo classifyArgumentType(QualType RetTy) const;
865 
866   void computeInfo(CGFunctionInfo &FI) const override;
867   Address EmitVAArg(CodeGenFunction &CGF,
868                     Address VAListAddr, QualType Ty) const override;
869 };
870 
871 class PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
872  public:
873   PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
874     : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
875 };
876 
877 void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
878   if (!getCXXABI().classifyReturnType(FI))
879     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
880 
881   for (auto &I : FI.arguments())
882     I.info = classifyArgumentType(I.type);
883 }
884 
885 Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
886                                 QualType Ty) const {
887   // The PNaCL ABI is a bit odd, in that varargs don't use normal
888   // function classification. Structs get passed directly for varargs
889   // functions, through a rewriting transform in
890   // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows
891   // this target to actually support a va_arg instructions with an
892   // aggregate type, unlike other targets.
893   return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
894 }
895 
896 /// Classify argument of given type \p Ty.
897 ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const {
898   if (isAggregateTypeForABI(Ty)) {
899     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
900       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
901     return getNaturalAlignIndirect(Ty);
902   } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
903     // Treat an enum type as its underlying type.
904     Ty = EnumTy->getDecl()->getIntegerType();
905   } else if (Ty->isFloatingType()) {
906     // Floating-point types don't go inreg.
907     return ABIArgInfo::getDirect();
908   }
909 
910   return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
911                                         : ABIArgInfo::getDirect());
912 }
913 
914 ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
915   if (RetTy->isVoidType())
916     return ABIArgInfo::getIgnore();
917 
918   // In the PNaCl ABI we always return records/structures on the stack.
919   if (isAggregateTypeForABI(RetTy))
920     return getNaturalAlignIndirect(RetTy);
921 
922   // Treat an enum type as its underlying type.
923   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
924     RetTy = EnumTy->getDecl()->getIntegerType();
925 
926   return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
927                                            : ABIArgInfo::getDirect());
928 }
929 
930 /// IsX86_MMXType - Return true if this is an MMX type.
931 bool IsX86_MMXType(llvm::Type *IRType) {
932   // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>.
933   return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
934     cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
935     IRType->getScalarSizeInBits() != 64;
936 }
937 
938 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
939                                           StringRef Constraint,
940                                           llvm::Type* Ty) {
941   bool IsMMXCons = llvm::StringSwitch<bool>(Constraint)
942                      .Cases("y", "&y", "^Ym", true)
943                      .Default(false);
944   if (IsMMXCons && Ty->isVectorTy()) {
945     if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) {
946       // Invalid MMX constraint
947       return nullptr;
948     }
949 
950     return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
951   }
952 
953   // No operation needed
954   return Ty;
955 }
956 
957 /// Returns true if this type can be passed in SSE registers with the
958 /// X86_VectorCall calling convention. Shared between x86_32 and x86_64.
959 static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) {
960   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
961     if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) {
962       if (BT->getKind() == BuiltinType::LongDouble) {
963         if (&Context.getTargetInfo().getLongDoubleFormat() ==
964             &llvm::APFloat::x87DoubleExtended())
965           return false;
966       }
967       return true;
968     }
969   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
970     // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX
971     // registers specially.
972     unsigned VecSize = Context.getTypeSize(VT);
973     if (VecSize == 128 || VecSize == 256 || VecSize == 512)
974       return true;
975   }
976   return false;
977 }
978 
979 /// Returns true if this aggregate is small enough to be passed in SSE registers
980 /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64.
981 static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) {
982   return NumMembers <= 4;
983 }
984 
985 /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86.
986 static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) {
987   auto AI = ABIArgInfo::getDirect(T);
988   AI.setInReg(true);
989   AI.setCanBeFlattened(false);
990   return AI;
991 }
992 
993 //===----------------------------------------------------------------------===//
994 // X86-32 ABI Implementation
995 //===----------------------------------------------------------------------===//
996 
997 /// Similar to llvm::CCState, but for Clang.
998 struct CCState {
999   CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {}
1000 
1001   unsigned CC;
1002   unsigned FreeRegs;
1003   unsigned FreeSSERegs;
1004 };
1005 
1006 enum {
1007   // Vectorcall only allows the first 6 parameters to be passed in registers.
1008   VectorcallMaxParamNumAsReg = 6
1009 };
1010 
1011 /// X86_32ABIInfo - The X86-32 ABI information.
1012 class X86_32ABIInfo : public SwiftABIInfo {
1013   enum Class {
1014     Integer,
1015     Float
1016   };
1017 
1018   static const unsigned MinABIStackAlignInBytes = 4;
1019 
1020   bool IsDarwinVectorABI;
1021   bool IsRetSmallStructInRegABI;
1022   bool IsWin32StructABI;
1023   bool IsSoftFloatABI;
1024   bool IsMCUABI;
1025   unsigned DefaultNumRegisterParameters;
1026 
1027   static bool isRegisterSize(unsigned Size) {
1028     return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
1029   }
1030 
1031   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
1032     // FIXME: Assumes vectorcall is in use.
1033     return isX86VectorTypeForVectorCall(getContext(), Ty);
1034   }
1035 
1036   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
1037                                          uint64_t NumMembers) const override {
1038     // FIXME: Assumes vectorcall is in use.
1039     return isX86VectorCallAggregateSmallEnough(NumMembers);
1040   }
1041 
1042   bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const;
1043 
1044   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1045   /// such that the argument will be passed in memory.
1046   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
1047 
1048   ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const;
1049 
1050   /// Return the alignment to use for the given type on the stack.
1051   unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
1052 
1053   Class classify(QualType Ty) const;
1054   ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const;
1055   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
1056 
1057   /// Updates the number of available free registers, returns
1058   /// true if any registers were allocated.
1059   bool updateFreeRegs(QualType Ty, CCState &State) const;
1060 
1061   bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg,
1062                                 bool &NeedsPadding) const;
1063   bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const;
1064 
1065   bool canExpandIndirectArgument(QualType Ty) const;
1066 
1067   /// Rewrite the function info so that all memory arguments use
1068   /// inalloca.
1069   void rewriteWithInAlloca(CGFunctionInfo &FI) const;
1070 
1071   void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1072                            CharUnits &StackOffset, ABIArgInfo &Info,
1073                            QualType Type) const;
1074   void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1075                              bool &UsedInAlloca) const;
1076 
1077 public:
1078 
1079   void computeInfo(CGFunctionInfo &FI) const override;
1080   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
1081                     QualType Ty) const override;
1082 
1083   X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1084                 bool RetSmallStructInRegABI, bool Win32StructABI,
1085                 unsigned NumRegisterParameters, bool SoftFloatABI)
1086     : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI),
1087       IsRetSmallStructInRegABI(RetSmallStructInRegABI),
1088       IsWin32StructABI(Win32StructABI),
1089       IsSoftFloatABI(SoftFloatABI),
1090       IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()),
1091       DefaultNumRegisterParameters(NumRegisterParameters) {}
1092 
1093   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
1094                                     bool asReturnValue) const override {
1095     // LLVM's x86-32 lowering currently only assigns up to three
1096     // integer registers and three fp registers.  Oddly, it'll use up to
1097     // four vector registers for vectors, but those can overlap with the
1098     // scalar registers.
1099     return occupiesMoreThan(CGT, scalars, /*total*/ 3);
1100   }
1101 
1102   bool isSwiftErrorInRegister() const override {
1103     // x86-32 lowering does not support passing swifterror in a register.
1104     return false;
1105   }
1106 };
1107 
1108 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
1109 public:
1110   X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI,
1111                           bool RetSmallStructInRegABI, bool Win32StructABI,
1112                           unsigned NumRegisterParameters, bool SoftFloatABI)
1113       : TargetCodeGenInfo(new X86_32ABIInfo(
1114             CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI,
1115             NumRegisterParameters, SoftFloatABI)) {}
1116 
1117   static bool isStructReturnInRegABI(
1118       const llvm::Triple &Triple, const CodeGenOptions &Opts);
1119 
1120   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
1121                            CodeGen::CodeGenModule &CGM) const override;
1122 
1123   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
1124     // Darwin uses different dwarf register numbers for EH.
1125     if (CGM.getTarget().getTriple().isOSDarwin()) return 5;
1126     return 4;
1127   }
1128 
1129   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1130                                llvm::Value *Address) const override;
1131 
1132   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
1133                                   StringRef Constraint,
1134                                   llvm::Type* Ty) const override {
1135     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
1136   }
1137 
1138   void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue,
1139                                 std::string &Constraints,
1140                                 std::vector<llvm::Type *> &ResultRegTypes,
1141                                 std::vector<llvm::Type *> &ResultTruncRegTypes,
1142                                 std::vector<LValue> &ResultRegDests,
1143                                 std::string &AsmString,
1144                                 unsigned NumOutputs) const override;
1145 
1146   llvm::Constant *
1147   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
1148     unsigned Sig = (0xeb << 0) |  // jmp rel8
1149                    (0x06 << 8) |  //           .+0x08
1150                    ('v' << 16) |
1151                    ('2' << 24);
1152     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
1153   }
1154 
1155   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
1156     return "movl\t%ebp, %ebp"
1157            "\t\t// marker for objc_retainAutoreleaseReturnValue";
1158   }
1159 };
1160 
1161 }
1162 
1163 /// Rewrite input constraint references after adding some output constraints.
1164 /// In the case where there is one output and one input and we add one output,
1165 /// we need to replace all operand references greater than or equal to 1:
1166 ///     mov $0, $1
1167 ///     mov eax, $1
1168 /// The result will be:
1169 ///     mov $0, $2
1170 ///     mov eax, $2
1171 static void rewriteInputConstraintReferences(unsigned FirstIn,
1172                                              unsigned NumNewOuts,
1173                                              std::string &AsmString) {
1174   std::string Buf;
1175   llvm::raw_string_ostream OS(Buf);
1176   size_t Pos = 0;
1177   while (Pos < AsmString.size()) {
1178     size_t DollarStart = AsmString.find('$', Pos);
1179     if (DollarStart == std::string::npos)
1180       DollarStart = AsmString.size();
1181     size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart);
1182     if (DollarEnd == std::string::npos)
1183       DollarEnd = AsmString.size();
1184     OS << StringRef(&AsmString[Pos], DollarEnd - Pos);
1185     Pos = DollarEnd;
1186     size_t NumDollars = DollarEnd - DollarStart;
1187     if (NumDollars % 2 != 0 && Pos < AsmString.size()) {
1188       // We have an operand reference.
1189       size_t DigitStart = Pos;
1190       size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart);
1191       if (DigitEnd == std::string::npos)
1192         DigitEnd = AsmString.size();
1193       StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart);
1194       unsigned OperandIndex;
1195       if (!OperandStr.getAsInteger(10, OperandIndex)) {
1196         if (OperandIndex >= FirstIn)
1197           OperandIndex += NumNewOuts;
1198         OS << OperandIndex;
1199       } else {
1200         OS << OperandStr;
1201       }
1202       Pos = DigitEnd;
1203     }
1204   }
1205   AsmString = std::move(OS.str());
1206 }
1207 
1208 /// Add output constraints for EAX:EDX because they are return registers.
1209 void X86_32TargetCodeGenInfo::addReturnRegisterOutputs(
1210     CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints,
1211     std::vector<llvm::Type *> &ResultRegTypes,
1212     std::vector<llvm::Type *> &ResultTruncRegTypes,
1213     std::vector<LValue> &ResultRegDests, std::string &AsmString,
1214     unsigned NumOutputs) const {
1215   uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType());
1216 
1217   // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is
1218   // larger.
1219   if (!Constraints.empty())
1220     Constraints += ',';
1221   if (RetWidth <= 32) {
1222     Constraints += "={eax}";
1223     ResultRegTypes.push_back(CGF.Int32Ty);
1224   } else {
1225     // Use the 'A' constraint for EAX:EDX.
1226     Constraints += "=A";
1227     ResultRegTypes.push_back(CGF.Int64Ty);
1228   }
1229 
1230   // Truncate EAX or EAX:EDX to an integer of the appropriate size.
1231   llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth);
1232   ResultTruncRegTypes.push_back(CoerceTy);
1233 
1234   // Coerce the integer by bitcasting the return slot pointer.
1235   ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(CGF),
1236                                                   CoerceTy->getPointerTo()));
1237   ResultRegDests.push_back(ReturnSlot);
1238 
1239   rewriteInputConstraintReferences(NumOutputs, 1, AsmString);
1240 }
1241 
1242 /// shouldReturnTypeInRegister - Determine if the given type should be
1243 /// returned in a register (for the Darwin and MCU ABI).
1244 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
1245                                                ASTContext &Context) const {
1246   uint64_t Size = Context.getTypeSize(Ty);
1247 
1248   // For i386, type must be register sized.
1249   // For the MCU ABI, it only needs to be <= 8-byte
1250   if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size)))
1251    return false;
1252 
1253   if (Ty->isVectorType()) {
1254     // 64- and 128- bit vectors inside structures are not returned in
1255     // registers.
1256     if (Size == 64 || Size == 128)
1257       return false;
1258 
1259     return true;
1260   }
1261 
1262   // If this is a builtin, pointer, enum, complex type, member pointer, or
1263   // member function pointer it is ok.
1264   if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
1265       Ty->isAnyComplexType() || Ty->isEnumeralType() ||
1266       Ty->isBlockPointerType() || Ty->isMemberPointerType())
1267     return true;
1268 
1269   // Arrays are treated like records.
1270   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
1271     return shouldReturnTypeInRegister(AT->getElementType(), Context);
1272 
1273   // Otherwise, it must be a record type.
1274   const RecordType *RT = Ty->getAs<RecordType>();
1275   if (!RT) return false;
1276 
1277   // FIXME: Traverse bases here too.
1278 
1279   // Structure types are passed in register if all fields would be
1280   // passed in a register.
1281   for (const auto *FD : RT->getDecl()->fields()) {
1282     // Empty fields are ignored.
1283     if (isEmptyField(Context, FD, true))
1284       continue;
1285 
1286     // Check fields recursively.
1287     if (!shouldReturnTypeInRegister(FD->getType(), Context))
1288       return false;
1289   }
1290   return true;
1291 }
1292 
1293 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
1294   // Treat complex types as the element type.
1295   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
1296     Ty = CTy->getElementType();
1297 
1298   // Check for a type which we know has a simple scalar argument-passing
1299   // convention without any padding.  (We're specifically looking for 32
1300   // and 64-bit integer and integer-equivalents, float, and double.)
1301   if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
1302       !Ty->isEnumeralType() && !Ty->isBlockPointerType())
1303     return false;
1304 
1305   uint64_t Size = Context.getTypeSize(Ty);
1306   return Size == 32 || Size == 64;
1307 }
1308 
1309 static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD,
1310                           uint64_t &Size) {
1311   for (const auto *FD : RD->fields()) {
1312     // Scalar arguments on the stack get 4 byte alignment on x86. If the
1313     // argument is smaller than 32-bits, expanding the struct will create
1314     // alignment padding.
1315     if (!is32Or64BitBasicType(FD->getType(), Context))
1316       return false;
1317 
1318     // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
1319     // how to expand them yet, and the predicate for telling if a bitfield still
1320     // counts as "basic" is more complicated than what we were doing previously.
1321     if (FD->isBitField())
1322       return false;
1323 
1324     Size += Context.getTypeSize(FD->getType());
1325   }
1326   return true;
1327 }
1328 
1329 static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD,
1330                                  uint64_t &Size) {
1331   // Don't do this if there are any non-empty bases.
1332   for (const CXXBaseSpecifier &Base : RD->bases()) {
1333     if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(),
1334                               Size))
1335       return false;
1336   }
1337   if (!addFieldSizes(Context, RD, Size))
1338     return false;
1339   return true;
1340 }
1341 
1342 /// Test whether an argument type which is to be passed indirectly (on the
1343 /// stack) would have the equivalent layout if it was expanded into separate
1344 /// arguments. If so, we prefer to do the latter to avoid inhibiting
1345 /// optimizations.
1346 bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const {
1347   // We can only expand structure types.
1348   const RecordType *RT = Ty->getAs<RecordType>();
1349   if (!RT)
1350     return false;
1351   const RecordDecl *RD = RT->getDecl();
1352   uint64_t Size = 0;
1353   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1354     if (!IsWin32StructABI) {
1355       // On non-Windows, we have to conservatively match our old bitcode
1356       // prototypes in order to be ABI-compatible at the bitcode level.
1357       if (!CXXRD->isCLike())
1358         return false;
1359     } else {
1360       // Don't do this for dynamic classes.
1361       if (CXXRD->isDynamicClass())
1362         return false;
1363     }
1364     if (!addBaseAndFieldSizes(getContext(), CXXRD, Size))
1365       return false;
1366   } else {
1367     if (!addFieldSizes(getContext(), RD, Size))
1368       return false;
1369   }
1370 
1371   // We can do this if there was no alignment padding.
1372   return Size == getContext().getTypeSize(Ty);
1373 }
1374 
1375 ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const {
1376   // If the return value is indirect, then the hidden argument is consuming one
1377   // integer register.
1378   if (State.FreeRegs) {
1379     --State.FreeRegs;
1380     if (!IsMCUABI)
1381       return getNaturalAlignIndirectInReg(RetTy);
1382   }
1383   return getNaturalAlignIndirect(RetTy, /*ByVal=*/false);
1384 }
1385 
1386 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
1387                                              CCState &State) const {
1388   if (RetTy->isVoidType())
1389     return ABIArgInfo::getIgnore();
1390 
1391   const Type *Base = nullptr;
1392   uint64_t NumElts = 0;
1393   if ((State.CC == llvm::CallingConv::X86_VectorCall ||
1394        State.CC == llvm::CallingConv::X86_RegCall) &&
1395       isHomogeneousAggregate(RetTy, Base, NumElts)) {
1396     // The LLVM struct type for such an aggregate should lower properly.
1397     return ABIArgInfo::getDirect();
1398   }
1399 
1400   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
1401     // On Darwin, some vectors are returned in registers.
1402     if (IsDarwinVectorABI) {
1403       uint64_t Size = getContext().getTypeSize(RetTy);
1404 
1405       // 128-bit vectors are a special case; they are returned in
1406       // registers and we need to make sure to pick a type the LLVM
1407       // backend will like.
1408       if (Size == 128)
1409         return ABIArgInfo::getDirect(llvm::VectorType::get(
1410                   llvm::Type::getInt64Ty(getVMContext()), 2));
1411 
1412       // Always return in register if it fits in a general purpose
1413       // register, or if it is 64 bits and has a single element.
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       return getIndirectReturnResult(RetTy, State);
1420     }
1421 
1422     return ABIArgInfo::getDirect();
1423   }
1424 
1425   if (isAggregateTypeForABI(RetTy)) {
1426     if (const RecordType *RT = RetTy->getAs<RecordType>()) {
1427       // Structures with flexible arrays are always indirect.
1428       if (RT->getDecl()->hasFlexibleArrayMember())
1429         return getIndirectReturnResult(RetTy, State);
1430     }
1431 
1432     // If specified, structs and unions are always indirect.
1433     if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType())
1434       return getIndirectReturnResult(RetTy, State);
1435 
1436     // Ignore empty structs/unions.
1437     if (isEmptyRecord(getContext(), RetTy, true))
1438       return ABIArgInfo::getIgnore();
1439 
1440     // Small structures which are register sized are generally returned
1441     // in a register.
1442     if (shouldReturnTypeInRegister(RetTy, getContext())) {
1443       uint64_t Size = getContext().getTypeSize(RetTy);
1444 
1445       // As a special-case, if the struct is a "single-element" struct, and
1446       // the field is of type "float" or "double", return it in a
1447       // floating-point register. (MSVC does not apply this special case.)
1448       // We apply a similar transformation for pointer types to improve the
1449       // quality of the generated IR.
1450       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
1451         if ((!IsWin32StructABI && SeltTy->isRealFloatingType())
1452             || SeltTy->hasPointerRepresentation())
1453           return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
1454 
1455       // FIXME: We should be able to narrow this integer in cases with dead
1456       // padding.
1457       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
1458     }
1459 
1460     return getIndirectReturnResult(RetTy, State);
1461   }
1462 
1463   // Treat an enum type as its underlying type.
1464   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1465     RetTy = EnumTy->getDecl()->getIntegerType();
1466 
1467   return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
1468                                            : ABIArgInfo::getDirect());
1469 }
1470 
1471 static bool isSSEVectorType(ASTContext &Context, QualType Ty) {
1472   return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
1473 }
1474 
1475 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
1476   const RecordType *RT = Ty->getAs<RecordType>();
1477   if (!RT)
1478     return 0;
1479   const RecordDecl *RD = RT->getDecl();
1480 
1481   // If this is a C++ record, check the bases first.
1482   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
1483     for (const auto &I : CXXRD->bases())
1484       if (!isRecordWithSSEVectorType(Context, I.getType()))
1485         return false;
1486 
1487   for (const auto *i : RD->fields()) {
1488     QualType FT = i->getType();
1489 
1490     if (isSSEVectorType(Context, FT))
1491       return true;
1492 
1493     if (isRecordWithSSEVectorType(Context, FT))
1494       return true;
1495   }
1496 
1497   return false;
1498 }
1499 
1500 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
1501                                                  unsigned Align) const {
1502   // Otherwise, if the alignment is less than or equal to the minimum ABI
1503   // alignment, just use the default; the backend will handle this.
1504   if (Align <= MinABIStackAlignInBytes)
1505     return 0; // Use default alignment.
1506 
1507   // On non-Darwin, the stack type alignment is always 4.
1508   if (!IsDarwinVectorABI) {
1509     // Set explicit alignment, since we may need to realign the top.
1510     return MinABIStackAlignInBytes;
1511   }
1512 
1513   // Otherwise, if the type contains an SSE vector type, the alignment is 16.
1514   if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
1515                       isRecordWithSSEVectorType(getContext(), Ty)))
1516     return 16;
1517 
1518   return MinABIStackAlignInBytes;
1519 }
1520 
1521 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal,
1522                                             CCState &State) const {
1523   if (!ByVal) {
1524     if (State.FreeRegs) {
1525       --State.FreeRegs; // Non-byval indirects just use one pointer.
1526       if (!IsMCUABI)
1527         return getNaturalAlignIndirectInReg(Ty);
1528     }
1529     return getNaturalAlignIndirect(Ty, false);
1530   }
1531 
1532   // Compute the byval alignment.
1533   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
1534   unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
1535   if (StackAlign == 0)
1536     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true);
1537 
1538   // If the stack alignment is less than the type alignment, realign the
1539   // argument.
1540   bool Realign = TypeAlign > StackAlign;
1541   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign),
1542                                  /*ByVal=*/true, Realign);
1543 }
1544 
1545 X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
1546   const Type *T = isSingleElementStruct(Ty, getContext());
1547   if (!T)
1548     T = Ty.getTypePtr();
1549 
1550   if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
1551     BuiltinType::Kind K = BT->getKind();
1552     if (K == BuiltinType::Float || K == BuiltinType::Double)
1553       return Float;
1554   }
1555   return Integer;
1556 }
1557 
1558 bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const {
1559   if (!IsSoftFloatABI) {
1560     Class C = classify(Ty);
1561     if (C == Float)
1562       return false;
1563   }
1564 
1565   unsigned Size = getContext().getTypeSize(Ty);
1566   unsigned SizeInRegs = (Size + 31) / 32;
1567 
1568   if (SizeInRegs == 0)
1569     return false;
1570 
1571   if (!IsMCUABI) {
1572     if (SizeInRegs > State.FreeRegs) {
1573       State.FreeRegs = 0;
1574       return false;
1575     }
1576   } else {
1577     // The MCU psABI allows passing parameters in-reg even if there are
1578     // earlier parameters that are passed on the stack. Also,
1579     // it does not allow passing >8-byte structs in-register,
1580     // even if there are 3 free registers available.
1581     if (SizeInRegs > State.FreeRegs || SizeInRegs > 2)
1582       return false;
1583   }
1584 
1585   State.FreeRegs -= SizeInRegs;
1586   return true;
1587 }
1588 
1589 bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State,
1590                                              bool &InReg,
1591                                              bool &NeedsPadding) const {
1592   // On Windows, aggregates other than HFAs are never passed in registers, and
1593   // they do not consume register slots. Homogenous floating-point aggregates
1594   // (HFAs) have already been dealt with at this point.
1595   if (IsWin32StructABI && isAggregateTypeForABI(Ty))
1596     return false;
1597 
1598   NeedsPadding = false;
1599   InReg = !IsMCUABI;
1600 
1601   if (!updateFreeRegs(Ty, State))
1602     return false;
1603 
1604   if (IsMCUABI)
1605     return true;
1606 
1607   if (State.CC == llvm::CallingConv::X86_FastCall ||
1608       State.CC == llvm::CallingConv::X86_VectorCall ||
1609       State.CC == llvm::CallingConv::X86_RegCall) {
1610     if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs)
1611       NeedsPadding = true;
1612 
1613     return false;
1614   }
1615 
1616   return true;
1617 }
1618 
1619 bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const {
1620   if (!updateFreeRegs(Ty, State))
1621     return false;
1622 
1623   if (IsMCUABI)
1624     return false;
1625 
1626   if (State.CC == llvm::CallingConv::X86_FastCall ||
1627       State.CC == llvm::CallingConv::X86_VectorCall ||
1628       State.CC == llvm::CallingConv::X86_RegCall) {
1629     if (getContext().getTypeSize(Ty) > 32)
1630       return false;
1631 
1632     return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() ||
1633         Ty->isReferenceType());
1634   }
1635 
1636   return true;
1637 }
1638 
1639 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty,
1640                                                CCState &State) const {
1641   // FIXME: Set alignment on indirect arguments.
1642 
1643   Ty = useFirstFieldIfTransparentUnion(Ty);
1644 
1645   // Check with the C++ ABI first.
1646   const RecordType *RT = Ty->getAs<RecordType>();
1647   if (RT) {
1648     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
1649     if (RAA == CGCXXABI::RAA_Indirect) {
1650       return getIndirectResult(Ty, false, State);
1651     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
1652       // The field index doesn't matter, we'll fix it up later.
1653       return ABIArgInfo::getInAlloca(/*FieldIndex=*/0);
1654     }
1655   }
1656 
1657   // Regcall uses the concept of a homogenous vector aggregate, similar
1658   // to other targets.
1659   const Type *Base = nullptr;
1660   uint64_t NumElts = 0;
1661   if (State.CC == llvm::CallingConv::X86_RegCall &&
1662       isHomogeneousAggregate(Ty, Base, NumElts)) {
1663 
1664     if (State.FreeSSERegs >= NumElts) {
1665       State.FreeSSERegs -= NumElts;
1666       if (Ty->isBuiltinType() || Ty->isVectorType())
1667         return ABIArgInfo::getDirect();
1668       return ABIArgInfo::getExpand();
1669     }
1670     return getIndirectResult(Ty, /*ByVal=*/false, State);
1671   }
1672 
1673   if (isAggregateTypeForABI(Ty)) {
1674     // Structures with flexible arrays are always indirect.
1675     // FIXME: This should not be byval!
1676     if (RT && RT->getDecl()->hasFlexibleArrayMember())
1677       return getIndirectResult(Ty, true, State);
1678 
1679     // Ignore empty structs/unions on non-Windows.
1680     if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true))
1681       return ABIArgInfo::getIgnore();
1682 
1683     llvm::LLVMContext &LLVMContext = getVMContext();
1684     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
1685     bool NeedsPadding = false;
1686     bool InReg;
1687     if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) {
1688       unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
1689       SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32);
1690       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
1691       if (InReg)
1692         return ABIArgInfo::getDirectInReg(Result);
1693       else
1694         return ABIArgInfo::getDirect(Result);
1695     }
1696     llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr;
1697 
1698     // Expand small (<= 128-bit) record types when we know that the stack layout
1699     // of those arguments will match the struct. This is important because the
1700     // LLVM backend isn't smart enough to remove byval, which inhibits many
1701     // optimizations.
1702     // Don't do this for the MCU if there are still free integer registers
1703     // (see X86_64 ABI for full explanation).
1704     if (getContext().getTypeSize(Ty) <= 4 * 32 &&
1705         (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty))
1706       return ABIArgInfo::getExpandWithPadding(
1707           State.CC == llvm::CallingConv::X86_FastCall ||
1708               State.CC == llvm::CallingConv::X86_VectorCall ||
1709               State.CC == llvm::CallingConv::X86_RegCall,
1710           PaddingType);
1711 
1712     return getIndirectResult(Ty, true, State);
1713   }
1714 
1715   if (const VectorType *VT = Ty->getAs<VectorType>()) {
1716     // On Darwin, some vectors are passed in memory, we handle this by passing
1717     // it as an i8/i16/i32/i64.
1718     if (IsDarwinVectorABI) {
1719       uint64_t Size = getContext().getTypeSize(Ty);
1720       if ((Size == 8 || Size == 16 || Size == 32) ||
1721           (Size == 64 && VT->getNumElements() == 1))
1722         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1723                                                             Size));
1724     }
1725 
1726     if (IsX86_MMXType(CGT.ConvertType(Ty)))
1727       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64));
1728 
1729     return ABIArgInfo::getDirect();
1730   }
1731 
1732 
1733   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1734     Ty = EnumTy->getDecl()->getIntegerType();
1735 
1736   bool InReg = shouldPrimitiveUseInReg(Ty, State);
1737 
1738   if (Ty->isPromotableIntegerType()) {
1739     if (InReg)
1740       return ABIArgInfo::getExtendInReg(Ty);
1741     return ABIArgInfo::getExtend(Ty);
1742   }
1743 
1744   if (InReg)
1745     return ABIArgInfo::getDirectInReg();
1746   return ABIArgInfo::getDirect();
1747 }
1748 
1749 void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State,
1750                                           bool &UsedInAlloca) const {
1751   // Vectorcall x86 works subtly different than in x64, so the format is
1752   // a bit different than the x64 version.  First, all vector types (not HVAs)
1753   // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers.
1754   // This differs from the x64 implementation, where the first 6 by INDEX get
1755   // registers.
1756   // After that, integers AND HVAs are assigned Left to Right in the same pass.
1757   // Integers are passed as ECX/EDX if one is available (in order).  HVAs will
1758   // first take up the remaining YMM/XMM registers. If insufficient registers
1759   // remain but an integer register (ECX/EDX) is available, it will be passed
1760   // in that, else, on the stack.
1761   for (auto &I : FI.arguments()) {
1762     // First pass do all the vector types.
1763     const Type *Base = nullptr;
1764     uint64_t NumElts = 0;
1765     const QualType& Ty = I.type;
1766     if ((Ty->isVectorType() || Ty->isBuiltinType()) &&
1767         isHomogeneousAggregate(Ty, Base, NumElts)) {
1768       if (State.FreeSSERegs >= NumElts) {
1769         State.FreeSSERegs -= NumElts;
1770         I.info = ABIArgInfo::getDirect();
1771       } else {
1772         I.info = classifyArgumentType(Ty, State);
1773       }
1774       UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1775     }
1776   }
1777 
1778   for (auto &I : FI.arguments()) {
1779     // Second pass, do the rest!
1780     const Type *Base = nullptr;
1781     uint64_t NumElts = 0;
1782     const QualType& Ty = I.type;
1783     bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts);
1784 
1785     if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) {
1786       // Assign true HVAs (non vector/native FP types).
1787       if (State.FreeSSERegs >= NumElts) {
1788         State.FreeSSERegs -= NumElts;
1789         I.info = getDirectX86Hva();
1790       } else {
1791         I.info = getIndirectResult(Ty, /*ByVal=*/false, State);
1792       }
1793     } else if (!IsHva) {
1794       // Assign all Non-HVAs, so this will exclude Vector/FP args.
1795       I.info = classifyArgumentType(Ty, State);
1796       UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1797     }
1798   }
1799 }
1800 
1801 void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
1802   CCState State(FI.getCallingConvention());
1803   if (IsMCUABI)
1804     State.FreeRegs = 3;
1805   else if (State.CC == llvm::CallingConv::X86_FastCall)
1806     State.FreeRegs = 2;
1807   else if (State.CC == llvm::CallingConv::X86_VectorCall) {
1808     State.FreeRegs = 2;
1809     State.FreeSSERegs = 6;
1810   } else if (FI.getHasRegParm())
1811     State.FreeRegs = FI.getRegParm();
1812   else if (State.CC == llvm::CallingConv::X86_RegCall) {
1813     State.FreeRegs = 5;
1814     State.FreeSSERegs = 8;
1815   } else
1816     State.FreeRegs = DefaultNumRegisterParameters;
1817 
1818   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
1819     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State);
1820   } else if (FI.getReturnInfo().isIndirect()) {
1821     // The C++ ABI is not aware of register usage, so we have to check if the
1822     // return value was sret and put it in a register ourselves if appropriate.
1823     if (State.FreeRegs) {
1824       --State.FreeRegs;  // The sret parameter consumes a register.
1825       if (!IsMCUABI)
1826         FI.getReturnInfo().setInReg(true);
1827     }
1828   }
1829 
1830   // The chain argument effectively gives us another free register.
1831   if (FI.isChainCall())
1832     ++State.FreeRegs;
1833 
1834   bool UsedInAlloca = false;
1835   if (State.CC == llvm::CallingConv::X86_VectorCall) {
1836     computeVectorCallArgs(FI, State, UsedInAlloca);
1837   } else {
1838     // If not vectorcall, revert to normal behavior.
1839     for (auto &I : FI.arguments()) {
1840       I.info = classifyArgumentType(I.type, State);
1841       UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca);
1842     }
1843   }
1844 
1845   // If we needed to use inalloca for any argument, do a second pass and rewrite
1846   // all the memory arguments to use inalloca.
1847   if (UsedInAlloca)
1848     rewriteWithInAlloca(FI);
1849 }
1850 
1851 void
1852 X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields,
1853                                    CharUnits &StackOffset, ABIArgInfo &Info,
1854                                    QualType Type) const {
1855   // Arguments are always 4-byte-aligned.
1856   CharUnits FieldAlign = CharUnits::fromQuantity(4);
1857 
1858   assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct");
1859   Info = ABIArgInfo::getInAlloca(FrameFields.size());
1860   FrameFields.push_back(CGT.ConvertTypeForMem(Type));
1861   StackOffset += getContext().getTypeSizeInChars(Type);
1862 
1863   // Insert padding bytes to respect alignment.
1864   CharUnits FieldEnd = StackOffset;
1865   StackOffset = FieldEnd.alignTo(FieldAlign);
1866   if (StackOffset != FieldEnd) {
1867     CharUnits NumBytes = StackOffset - FieldEnd;
1868     llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext());
1869     Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity());
1870     FrameFields.push_back(Ty);
1871   }
1872 }
1873 
1874 static bool isArgInAlloca(const ABIArgInfo &Info) {
1875   // Leave ignored and inreg arguments alone.
1876   switch (Info.getKind()) {
1877   case ABIArgInfo::InAlloca:
1878     return true;
1879   case ABIArgInfo::Indirect:
1880     assert(Info.getIndirectByVal());
1881     return true;
1882   case ABIArgInfo::Ignore:
1883     return false;
1884   case ABIArgInfo::Direct:
1885   case ABIArgInfo::Extend:
1886     if (Info.getInReg())
1887       return false;
1888     return true;
1889   case ABIArgInfo::Expand:
1890   case ABIArgInfo::CoerceAndExpand:
1891     // These are aggregate types which are never passed in registers when
1892     // inalloca is involved.
1893     return true;
1894   }
1895   llvm_unreachable("invalid enum");
1896 }
1897 
1898 void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const {
1899   assert(IsWin32StructABI && "inalloca only supported on win32");
1900 
1901   // Build a packed struct type for all of the arguments in memory.
1902   SmallVector<llvm::Type *, 6> FrameFields;
1903 
1904   // The stack alignment is always 4.
1905   CharUnits StackAlign = CharUnits::fromQuantity(4);
1906 
1907   CharUnits StackOffset;
1908   CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end();
1909 
1910   // Put 'this' into the struct before 'sret', if necessary.
1911   bool IsThisCall =
1912       FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall;
1913   ABIArgInfo &Ret = FI.getReturnInfo();
1914   if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall &&
1915       isArgInAlloca(I->info)) {
1916     addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1917     ++I;
1918   }
1919 
1920   // Put the sret parameter into the inalloca struct if it's in memory.
1921   if (Ret.isIndirect() && !Ret.getInReg()) {
1922     CanQualType PtrTy = getContext().getPointerType(FI.getReturnType());
1923     addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy);
1924     // On Windows, the hidden sret parameter is always returned in eax.
1925     Ret.setInAllocaSRet(IsWin32StructABI);
1926   }
1927 
1928   // Skip the 'this' parameter in ecx.
1929   if (IsThisCall)
1930     ++I;
1931 
1932   // Put arguments passed in memory into the struct.
1933   for (; I != E; ++I) {
1934     if (isArgInAlloca(I->info))
1935       addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type);
1936   }
1937 
1938   FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields,
1939                                         /*isPacked=*/true),
1940                   StackAlign);
1941 }
1942 
1943 Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF,
1944                                  Address VAListAddr, QualType Ty) const {
1945 
1946   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
1947 
1948   // x86-32 changes the alignment of certain arguments on the stack.
1949   //
1950   // Just messing with TypeInfo like this works because we never pass
1951   // anything indirectly.
1952   TypeInfo.second = CharUnits::fromQuantity(
1953                 getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity()));
1954 
1955   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
1956                           TypeInfo, CharUnits::fromQuantity(4),
1957                           /*AllowHigherAlign*/ true);
1958 }
1959 
1960 bool X86_32TargetCodeGenInfo::isStructReturnInRegABI(
1961     const llvm::Triple &Triple, const CodeGenOptions &Opts) {
1962   assert(Triple.getArch() == llvm::Triple::x86);
1963 
1964   switch (Opts.getStructReturnConvention()) {
1965   case CodeGenOptions::SRCK_Default:
1966     break;
1967   case CodeGenOptions::SRCK_OnStack:  // -fpcc-struct-return
1968     return false;
1969   case CodeGenOptions::SRCK_InRegs:  // -freg-struct-return
1970     return true;
1971   }
1972 
1973   if (Triple.isOSDarwin() || Triple.isOSIAMCU())
1974     return true;
1975 
1976   switch (Triple.getOS()) {
1977   case llvm::Triple::DragonFly:
1978   case llvm::Triple::FreeBSD:
1979   case llvm::Triple::OpenBSD:
1980   case llvm::Triple::Win32:
1981     return true;
1982   default:
1983     return false;
1984   }
1985 }
1986 
1987 void X86_32TargetCodeGenInfo::setTargetAttributes(
1988     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
1989   if (GV->isDeclaration())
1990     return;
1991   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1992     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
1993       llvm::Function *Fn = cast<llvm::Function>(GV);
1994       Fn->addFnAttr("stackrealign");
1995     }
1996     if (FD->hasAttr<AnyX86InterruptAttr>()) {
1997       llvm::Function *Fn = cast<llvm::Function>(GV);
1998       Fn->setCallingConv(llvm::CallingConv::X86_INTR);
1999     }
2000   }
2001 }
2002 
2003 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
2004                                                CodeGen::CodeGenFunction &CGF,
2005                                                llvm::Value *Address) const {
2006   CodeGen::CGBuilderTy &Builder = CGF.Builder;
2007 
2008   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
2009 
2010   // 0-7 are the eight integer registers;  the order is different
2011   //   on Darwin (for EH), but the range is the same.
2012   // 8 is %eip.
2013   AssignToArrayRange(Builder, Address, Four8, 0, 8);
2014 
2015   if (CGF.CGM.getTarget().getTriple().isOSDarwin()) {
2016     // 12-16 are st(0..4).  Not sure why we stop at 4.
2017     // These have size 16, which is sizeof(long double) on
2018     // platforms with 8-byte alignment for that type.
2019     llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
2020     AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
2021 
2022   } else {
2023     // 9 is %eflags, which doesn't get a size on Darwin for some
2024     // reason.
2025     Builder.CreateAlignedStore(
2026         Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9),
2027                                CharUnits::One());
2028 
2029     // 11-16 are st(0..5).  Not sure why we stop at 5.
2030     // These have size 12, which is sizeof(long double) on
2031     // platforms with 4-byte alignment for that type.
2032     llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
2033     AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
2034   }
2035 
2036   return false;
2037 }
2038 
2039 //===----------------------------------------------------------------------===//
2040 // X86-64 ABI Implementation
2041 //===----------------------------------------------------------------------===//
2042 
2043 
2044 namespace {
2045 /// The AVX ABI level for X86 targets.
2046 enum class X86AVXABILevel {
2047   None,
2048   AVX,
2049   AVX512
2050 };
2051 
2052 /// \p returns the size in bits of the largest (native) vector for \p AVXLevel.
2053 static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) {
2054   switch (AVXLevel) {
2055   case X86AVXABILevel::AVX512:
2056     return 512;
2057   case X86AVXABILevel::AVX:
2058     return 256;
2059   case X86AVXABILevel::None:
2060     return 128;
2061   }
2062   llvm_unreachable("Unknown AVXLevel");
2063 }
2064 
2065 /// X86_64ABIInfo - The X86_64 ABI information.
2066 class X86_64ABIInfo : public SwiftABIInfo {
2067   enum Class {
2068     Integer = 0,
2069     SSE,
2070     SSEUp,
2071     X87,
2072     X87Up,
2073     ComplexX87,
2074     NoClass,
2075     Memory
2076   };
2077 
2078   /// merge - Implement the X86_64 ABI merging algorithm.
2079   ///
2080   /// Merge an accumulating classification \arg Accum with a field
2081   /// classification \arg Field.
2082   ///
2083   /// \param Accum - The accumulating classification. This should
2084   /// always be either NoClass or the result of a previous merge
2085   /// call. In addition, this should never be Memory (the caller
2086   /// should just return Memory for the aggregate).
2087   static Class merge(Class Accum, Class Field);
2088 
2089   /// postMerge - Implement the X86_64 ABI post merging algorithm.
2090   ///
2091   /// Post merger cleanup, reduces a malformed Hi and Lo pair to
2092   /// final MEMORY or SSE classes when necessary.
2093   ///
2094   /// \param AggregateSize - The size of the current aggregate in
2095   /// the classification process.
2096   ///
2097   /// \param Lo - The classification for the parts of the type
2098   /// residing in the low word of the containing object.
2099   ///
2100   /// \param Hi - The classification for the parts of the type
2101   /// residing in the higher words of the containing object.
2102   ///
2103   void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
2104 
2105   /// classify - Determine the x86_64 register classes in which the
2106   /// given type T should be passed.
2107   ///
2108   /// \param Lo - The classification for the parts of the type
2109   /// residing in the low word of the containing object.
2110   ///
2111   /// \param Hi - The classification for the parts of the type
2112   /// residing in the high word of the containing object.
2113   ///
2114   /// \param OffsetBase - The bit offset of this type in the
2115   /// containing object.  Some parameters are classified different
2116   /// depending on whether they straddle an eightbyte boundary.
2117   ///
2118   /// \param isNamedArg - Whether the argument in question is a "named"
2119   /// argument, as used in AMD64-ABI 3.5.7.
2120   ///
2121   /// If a word is unused its result will be NoClass; if a type should
2122   /// be passed in Memory then at least the classification of \arg Lo
2123   /// will be Memory.
2124   ///
2125   /// The \arg Lo class will be NoClass iff the argument is ignored.
2126   ///
2127   /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
2128   /// also be ComplexX87.
2129   void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi,
2130                 bool isNamedArg) const;
2131 
2132   llvm::Type *GetByteVectorType(QualType Ty) const;
2133   llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
2134                                  unsigned IROffset, QualType SourceTy,
2135                                  unsigned SourceOffset) const;
2136   llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
2137                                      unsigned IROffset, QualType SourceTy,
2138                                      unsigned SourceOffset) const;
2139 
2140   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2141   /// such that the argument will be returned in memory.
2142   ABIArgInfo getIndirectReturnResult(QualType Ty) const;
2143 
2144   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
2145   /// such that the argument will be passed in memory.
2146   ///
2147   /// \param freeIntRegs - The number of free integer registers remaining
2148   /// available.
2149   ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
2150 
2151   ABIArgInfo classifyReturnType(QualType RetTy) const;
2152 
2153   ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs,
2154                                   unsigned &neededInt, unsigned &neededSSE,
2155                                   bool isNamedArg) const;
2156 
2157   ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt,
2158                                        unsigned &NeededSSE) const;
2159 
2160   ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
2161                                            unsigned &NeededSSE) const;
2162 
2163   bool IsIllegalVectorType(QualType Ty) const;
2164 
2165   /// The 0.98 ABI revision clarified a lot of ambiguities,
2166   /// unfortunately in ways that were not always consistent with
2167   /// certain previous compilers.  In particular, platforms which
2168   /// required strict binary compatibility with older versions of GCC
2169   /// may need to exempt themselves.
2170   bool honorsRevision0_98() const {
2171     return !getTarget().getTriple().isOSDarwin();
2172   }
2173 
2174   /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to
2175   /// classify it as INTEGER (for compatibility with older clang compilers).
2176   bool classifyIntegerMMXAsSSE() const {
2177     // Clang <= 3.8 did not do this.
2178     if (getContext().getLangOpts().getClangABICompat() <=
2179         LangOptions::ClangABI::Ver3_8)
2180       return false;
2181 
2182     const llvm::Triple &Triple = getTarget().getTriple();
2183     if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4)
2184       return false;
2185     if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10)
2186       return false;
2187     return true;
2188   }
2189 
2190   // GCC classifies vectors of __int128 as memory.
2191   bool passInt128VectorsInMem() const {
2192     // Clang <= 9.0 did not do this.
2193     if (getContext().getLangOpts().getClangABICompat() <=
2194         LangOptions::ClangABI::Ver9)
2195       return false;
2196 
2197     const llvm::Triple &T = getTarget().getTriple();
2198     return T.isOSLinux() || T.isOSNetBSD();
2199   }
2200 
2201   X86AVXABILevel AVXLevel;
2202   // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on
2203   // 64-bit hardware.
2204   bool Has64BitPointers;
2205 
2206 public:
2207   X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) :
2208       SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2209       Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) {
2210   }
2211 
2212   bool isPassedUsingAVXType(QualType type) const {
2213     unsigned neededInt, neededSSE;
2214     // The freeIntRegs argument doesn't matter here.
2215     ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE,
2216                                            /*isNamedArg*/true);
2217     if (info.isDirect()) {
2218       llvm::Type *ty = info.getCoerceToType();
2219       if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
2220         return (vectorTy->getBitWidth() > 128);
2221     }
2222     return false;
2223   }
2224 
2225   void computeInfo(CGFunctionInfo &FI) const override;
2226 
2227   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2228                     QualType Ty) const override;
2229   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
2230                       QualType Ty) const override;
2231 
2232   bool has64BitPointers() const {
2233     return Has64BitPointers;
2234   }
2235 
2236   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
2237                                     bool asReturnValue) const override {
2238     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2239   }
2240   bool isSwiftErrorInRegister() const override {
2241     return true;
2242   }
2243 };
2244 
2245 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
2246 class WinX86_64ABIInfo : public SwiftABIInfo {
2247 public:
2248   WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2249       : SwiftABIInfo(CGT), AVXLevel(AVXLevel),
2250         IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {}
2251 
2252   void computeInfo(CGFunctionInfo &FI) const override;
2253 
2254   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
2255                     QualType Ty) const override;
2256 
2257   bool isHomogeneousAggregateBaseType(QualType Ty) const override {
2258     // FIXME: Assumes vectorcall is in use.
2259     return isX86VectorTypeForVectorCall(getContext(), Ty);
2260   }
2261 
2262   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
2263                                          uint64_t NumMembers) const override {
2264     // FIXME: Assumes vectorcall is in use.
2265     return isX86VectorCallAggregateSmallEnough(NumMembers);
2266   }
2267 
2268   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars,
2269                                     bool asReturnValue) const override {
2270     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
2271   }
2272 
2273   bool isSwiftErrorInRegister() const override {
2274     return true;
2275   }
2276 
2277 private:
2278   ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType,
2279                       bool IsVectorCall, bool IsRegCall) const;
2280   ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
2281                                       const ABIArgInfo &current) const;
2282   void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs,
2283                              bool IsVectorCall, bool IsRegCall) const;
2284 
2285   X86AVXABILevel AVXLevel;
2286 
2287   bool IsMingw64;
2288 };
2289 
2290 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2291 public:
2292   X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel)
2293       : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {}
2294 
2295   const X86_64ABIInfo &getABIInfo() const {
2296     return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
2297   }
2298 
2299   /// Disable tail call on x86-64. The epilogue code before the tail jump blocks
2300   /// the autoreleaseRV/retainRV optimization.
2301   bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override {
2302     return true;
2303   }
2304 
2305   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2306     return 7;
2307   }
2308 
2309   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2310                                llvm::Value *Address) const override {
2311     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2312 
2313     // 0-15 are the 16 integer registers.
2314     // 16 is %rip.
2315     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2316     return false;
2317   }
2318 
2319   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
2320                                   StringRef Constraint,
2321                                   llvm::Type* Ty) const override {
2322     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
2323   }
2324 
2325   bool isNoProtoCallVariadic(const CallArgList &args,
2326                              const FunctionNoProtoType *fnType) const override {
2327     // The default CC on x86-64 sets %al to the number of SSA
2328     // registers used, and GCC sets this when calling an unprototyped
2329     // function, so we override the default behavior.  However, don't do
2330     // that when AVX types are involved: the ABI explicitly states it is
2331     // undefined, and it doesn't work in practice because of how the ABI
2332     // defines varargs anyway.
2333     if (fnType->getCallConv() == CC_C) {
2334       bool HasAVXType = false;
2335       for (CallArgList::const_iterator
2336              it = args.begin(), ie = args.end(); it != ie; ++it) {
2337         if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
2338           HasAVXType = true;
2339           break;
2340         }
2341       }
2342 
2343       if (!HasAVXType)
2344         return true;
2345     }
2346 
2347     return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
2348   }
2349 
2350   llvm::Constant *
2351   getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override {
2352     unsigned Sig = (0xeb << 0) | // jmp rel8
2353                    (0x06 << 8) | //           .+0x08
2354                    ('v' << 16) |
2355                    ('2' << 24);
2356     return llvm::ConstantInt::get(CGM.Int32Ty, Sig);
2357   }
2358 
2359   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2360                            CodeGen::CodeGenModule &CGM) const override {
2361     if (GV->isDeclaration())
2362       return;
2363     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2364       if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2365         llvm::Function *Fn = cast<llvm::Function>(GV);
2366         Fn->addFnAttr("stackrealign");
2367       }
2368       if (FD->hasAttr<AnyX86InterruptAttr>()) {
2369         llvm::Function *Fn = cast<llvm::Function>(GV);
2370         Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2371       }
2372     }
2373   }
2374 };
2375 
2376 static std::string qualifyWindowsLibrary(llvm::StringRef Lib) {
2377   // If the argument does not end in .lib, automatically add the suffix.
2378   // If the argument contains a space, enclose it in quotes.
2379   // This matches the behavior of MSVC.
2380   bool Quote = (Lib.find(" ") != StringRef::npos);
2381   std::string ArgStr = Quote ? "\"" : "";
2382   ArgStr += Lib;
2383   if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a"))
2384     ArgStr += ".lib";
2385   ArgStr += Quote ? "\"" : "";
2386   return ArgStr;
2387 }
2388 
2389 class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo {
2390 public:
2391   WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2392         bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI,
2393         unsigned NumRegisterParameters)
2394     : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI,
2395         Win32StructABI, NumRegisterParameters, false) {}
2396 
2397   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2398                            CodeGen::CodeGenModule &CGM) const override;
2399 
2400   void getDependentLibraryOption(llvm::StringRef Lib,
2401                                  llvm::SmallString<24> &Opt) const override {
2402     Opt = "/DEFAULTLIB:";
2403     Opt += qualifyWindowsLibrary(Lib);
2404   }
2405 
2406   void getDetectMismatchOption(llvm::StringRef Name,
2407                                llvm::StringRef Value,
2408                                llvm::SmallString<32> &Opt) const override {
2409     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2410   }
2411 };
2412 
2413 static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2414                                           CodeGen::CodeGenModule &CGM) {
2415   if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) {
2416 
2417     if (CGM.getCodeGenOpts().StackProbeSize != 4096)
2418       Fn->addFnAttr("stack-probe-size",
2419                     llvm::utostr(CGM.getCodeGenOpts().StackProbeSize));
2420     if (CGM.getCodeGenOpts().NoStackArgProbe)
2421       Fn->addFnAttr("no-stack-arg-probe");
2422   }
2423 }
2424 
2425 void WinX86_32TargetCodeGenInfo::setTargetAttributes(
2426     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2427   X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2428   if (GV->isDeclaration())
2429     return;
2430   addStackProbeTargetAttributes(D, GV, CGM);
2431 }
2432 
2433 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
2434 public:
2435   WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
2436                              X86AVXABILevel AVXLevel)
2437       : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT, AVXLevel)) {}
2438 
2439   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2440                            CodeGen::CodeGenModule &CGM) const override;
2441 
2442   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
2443     return 7;
2444   }
2445 
2446   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2447                                llvm::Value *Address) const override {
2448     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
2449 
2450     // 0-15 are the 16 integer registers.
2451     // 16 is %rip.
2452     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
2453     return false;
2454   }
2455 
2456   void getDependentLibraryOption(llvm::StringRef Lib,
2457                                  llvm::SmallString<24> &Opt) const override {
2458     Opt = "/DEFAULTLIB:";
2459     Opt += qualifyWindowsLibrary(Lib);
2460   }
2461 
2462   void getDetectMismatchOption(llvm::StringRef Name,
2463                                llvm::StringRef Value,
2464                                llvm::SmallString<32> &Opt) const override {
2465     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
2466   }
2467 };
2468 
2469 void WinX86_64TargetCodeGenInfo::setTargetAttributes(
2470     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
2471   TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
2472   if (GV->isDeclaration())
2473     return;
2474   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
2475     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
2476       llvm::Function *Fn = cast<llvm::Function>(GV);
2477       Fn->addFnAttr("stackrealign");
2478     }
2479     if (FD->hasAttr<AnyX86InterruptAttr>()) {
2480       llvm::Function *Fn = cast<llvm::Function>(GV);
2481       Fn->setCallingConv(llvm::CallingConv::X86_INTR);
2482     }
2483   }
2484 
2485   addStackProbeTargetAttributes(D, GV, CGM);
2486 }
2487 }
2488 
2489 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
2490                               Class &Hi) const {
2491   // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
2492   //
2493   // (a) If one of the classes is Memory, the whole argument is passed in
2494   //     memory.
2495   //
2496   // (b) If X87UP is not preceded by X87, the whole argument is passed in
2497   //     memory.
2498   //
2499   // (c) If the size of the aggregate exceeds two eightbytes and the first
2500   //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
2501   //     argument is passed in memory. NOTE: This is necessary to keep the
2502   //     ABI working for processors that don't support the __m256 type.
2503   //
2504   // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
2505   //
2506   // Some of these are enforced by the merging logic.  Others can arise
2507   // only with unions; for example:
2508   //   union { _Complex double; unsigned; }
2509   //
2510   // Note that clauses (b) and (c) were added in 0.98.
2511   //
2512   if (Hi == Memory)
2513     Lo = Memory;
2514   if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
2515     Lo = Memory;
2516   if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
2517     Lo = Memory;
2518   if (Hi == SSEUp && Lo != SSE)
2519     Hi = SSE;
2520 }
2521 
2522 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
2523   // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
2524   // classified recursively so that always two fields are
2525   // considered. The resulting class is calculated according to
2526   // the classes of the fields in the eightbyte:
2527   //
2528   // (a) If both classes are equal, this is the resulting class.
2529   //
2530   // (b) If one of the classes is NO_CLASS, the resulting class is
2531   // the other class.
2532   //
2533   // (c) If one of the classes is MEMORY, the result is the MEMORY
2534   // class.
2535   //
2536   // (d) If one of the classes is INTEGER, the result is the
2537   // INTEGER.
2538   //
2539   // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
2540   // MEMORY is used as class.
2541   //
2542   // (f) Otherwise class SSE is used.
2543 
2544   // Accum should never be memory (we should have returned) or
2545   // ComplexX87 (because this cannot be passed in a structure).
2546   assert((Accum != Memory && Accum != ComplexX87) &&
2547          "Invalid accumulated classification during merge.");
2548   if (Accum == Field || Field == NoClass)
2549     return Accum;
2550   if (Field == Memory)
2551     return Memory;
2552   if (Accum == NoClass)
2553     return Field;
2554   if (Accum == Integer || Field == Integer)
2555     return Integer;
2556   if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
2557       Accum == X87 || Accum == X87Up)
2558     return Memory;
2559   return SSE;
2560 }
2561 
2562 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
2563                              Class &Lo, Class &Hi, bool isNamedArg) const {
2564   // FIXME: This code can be simplified by introducing a simple value class for
2565   // Class pairs with appropriate constructor methods for the various
2566   // situations.
2567 
2568   // FIXME: Some of the split computations are wrong; unaligned vectors
2569   // shouldn't be passed in registers for example, so there is no chance they
2570   // can straddle an eightbyte. Verify & simplify.
2571 
2572   Lo = Hi = NoClass;
2573 
2574   Class &Current = OffsetBase < 64 ? Lo : Hi;
2575   Current = Memory;
2576 
2577   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2578     BuiltinType::Kind k = BT->getKind();
2579 
2580     if (k == BuiltinType::Void) {
2581       Current = NoClass;
2582     } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
2583       Lo = Integer;
2584       Hi = Integer;
2585     } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
2586       Current = Integer;
2587     } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
2588       Current = SSE;
2589     } else if (k == BuiltinType::LongDouble) {
2590       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2591       if (LDF == &llvm::APFloat::IEEEquad()) {
2592         Lo = SSE;
2593         Hi = SSEUp;
2594       } else if (LDF == &llvm::APFloat::x87DoubleExtended()) {
2595         Lo = X87;
2596         Hi = X87Up;
2597       } else if (LDF == &llvm::APFloat::IEEEdouble()) {
2598         Current = SSE;
2599       } else
2600         llvm_unreachable("unexpected long double representation!");
2601     }
2602     // FIXME: _Decimal32 and _Decimal64 are SSE.
2603     // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
2604     return;
2605   }
2606 
2607   if (const EnumType *ET = Ty->getAs<EnumType>()) {
2608     // Classify the underlying integer type.
2609     classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg);
2610     return;
2611   }
2612 
2613   if (Ty->hasPointerRepresentation()) {
2614     Current = Integer;
2615     return;
2616   }
2617 
2618   if (Ty->isMemberPointerType()) {
2619     if (Ty->isMemberFunctionPointerType()) {
2620       if (Has64BitPointers) {
2621         // If Has64BitPointers, this is an {i64, i64}, so classify both
2622         // Lo and Hi now.
2623         Lo = Hi = Integer;
2624       } else {
2625         // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that
2626         // straddles an eightbyte boundary, Hi should be classified as well.
2627         uint64_t EB_FuncPtr = (OffsetBase) / 64;
2628         uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64;
2629         if (EB_FuncPtr != EB_ThisAdj) {
2630           Lo = Hi = Integer;
2631         } else {
2632           Current = Integer;
2633         }
2634       }
2635     } else {
2636       Current = Integer;
2637     }
2638     return;
2639   }
2640 
2641   if (const VectorType *VT = Ty->getAs<VectorType>()) {
2642     uint64_t Size = getContext().getTypeSize(VT);
2643     if (Size == 1 || Size == 8 || Size == 16 || Size == 32) {
2644       // gcc passes the following as integer:
2645       // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float>
2646       // 2 bytes - <2 x char>, <1 x short>
2647       // 1 byte  - <1 x char>
2648       Current = Integer;
2649 
2650       // If this type crosses an eightbyte boundary, it should be
2651       // split.
2652       uint64_t EB_Lo = (OffsetBase) / 64;
2653       uint64_t EB_Hi = (OffsetBase + Size - 1) / 64;
2654       if (EB_Lo != EB_Hi)
2655         Hi = Lo;
2656     } else if (Size == 64) {
2657       QualType ElementType = VT->getElementType();
2658 
2659       // gcc passes <1 x double> in memory. :(
2660       if (ElementType->isSpecificBuiltinType(BuiltinType::Double))
2661         return;
2662 
2663       // gcc passes <1 x long long> as SSE but clang used to unconditionally
2664       // pass them as integer.  For platforms where clang is the de facto
2665       // platform compiler, we must continue to use integer.
2666       if (!classifyIntegerMMXAsSSE() &&
2667           (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) ||
2668            ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) ||
2669            ElementType->isSpecificBuiltinType(BuiltinType::Long) ||
2670            ElementType->isSpecificBuiltinType(BuiltinType::ULong)))
2671         Current = Integer;
2672       else
2673         Current = SSE;
2674 
2675       // If this type crosses an eightbyte boundary, it should be
2676       // split.
2677       if (OffsetBase && OffsetBase != 64)
2678         Hi = Lo;
2679     } else if (Size == 128 ||
2680                (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) {
2681       QualType ElementType = VT->getElementType();
2682 
2683       // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :(
2684       if (passInt128VectorsInMem() && Size != 128 &&
2685           (ElementType->isSpecificBuiltinType(BuiltinType::Int128) ||
2686            ElementType->isSpecificBuiltinType(BuiltinType::UInt128)))
2687         return;
2688 
2689       // Arguments of 256-bits are split into four eightbyte chunks. The
2690       // least significant one belongs to class SSE and all the others to class
2691       // SSEUP. The original Lo and Hi design considers that types can't be
2692       // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
2693       // This design isn't correct for 256-bits, but since there're no cases
2694       // where the upper parts would need to be inspected, avoid adding
2695       // complexity and just consider Hi to match the 64-256 part.
2696       //
2697       // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in
2698       // registers if they are "named", i.e. not part of the "..." of a
2699       // variadic function.
2700       //
2701       // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are
2702       // split into eight eightbyte chunks, one SSE and seven SSEUP.
2703       Lo = SSE;
2704       Hi = SSEUp;
2705     }
2706     return;
2707   }
2708 
2709   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2710     QualType ET = getContext().getCanonicalType(CT->getElementType());
2711 
2712     uint64_t Size = getContext().getTypeSize(Ty);
2713     if (ET->isIntegralOrEnumerationType()) {
2714       if (Size <= 64)
2715         Current = Integer;
2716       else if (Size <= 128)
2717         Lo = Hi = Integer;
2718     } else if (ET == getContext().FloatTy) {
2719       Current = SSE;
2720     } else if (ET == getContext().DoubleTy) {
2721       Lo = Hi = SSE;
2722     } else if (ET == getContext().LongDoubleTy) {
2723       const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
2724       if (LDF == &llvm::APFloat::IEEEquad())
2725         Current = Memory;
2726       else if (LDF == &llvm::APFloat::x87DoubleExtended())
2727         Current = ComplexX87;
2728       else if (LDF == &llvm::APFloat::IEEEdouble())
2729         Lo = Hi = SSE;
2730       else
2731         llvm_unreachable("unexpected long double representation!");
2732     }
2733 
2734     // If this complex type crosses an eightbyte boundary then it
2735     // should be split.
2736     uint64_t EB_Real = (OffsetBase) / 64;
2737     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
2738     if (Hi == NoClass && EB_Real != EB_Imag)
2739       Hi = Lo;
2740 
2741     return;
2742   }
2743 
2744   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
2745     // Arrays are treated like structures.
2746 
2747     uint64_t Size = getContext().getTypeSize(Ty);
2748 
2749     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2750     // than eight eightbytes, ..., it has class MEMORY.
2751     if (Size > 512)
2752       return;
2753 
2754     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
2755     // fields, it has class MEMORY.
2756     //
2757     // Only need to check alignment of array base.
2758     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
2759       return;
2760 
2761     // Otherwise implement simplified merge. We could be smarter about
2762     // this, but it isn't worth it and would be harder to verify.
2763     Current = NoClass;
2764     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
2765     uint64_t ArraySize = AT->getSize().getZExtValue();
2766 
2767     // The only case a 256-bit wide vector could be used is when the array
2768     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2769     // to work for sizes wider than 128, early check and fallback to memory.
2770     //
2771     if (Size > 128 &&
2772         (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel)))
2773       return;
2774 
2775     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
2776       Class FieldLo, FieldHi;
2777       classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg);
2778       Lo = merge(Lo, FieldLo);
2779       Hi = merge(Hi, FieldHi);
2780       if (Lo == Memory || Hi == Memory)
2781         break;
2782     }
2783 
2784     postMerge(Size, Lo, Hi);
2785     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
2786     return;
2787   }
2788 
2789   if (const RecordType *RT = Ty->getAs<RecordType>()) {
2790     uint64_t Size = getContext().getTypeSize(Ty);
2791 
2792     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
2793     // than eight eightbytes, ..., it has class MEMORY.
2794     if (Size > 512)
2795       return;
2796 
2797     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
2798     // copy constructor or a non-trivial destructor, it is passed by invisible
2799     // reference.
2800     if (getRecordArgABI(RT, getCXXABI()))
2801       return;
2802 
2803     const RecordDecl *RD = RT->getDecl();
2804 
2805     // Assume variable sized types are passed in memory.
2806     if (RD->hasFlexibleArrayMember())
2807       return;
2808 
2809     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2810 
2811     // Reset Lo class, this will be recomputed.
2812     Current = NoClass;
2813 
2814     // If this is a C++ record, classify the bases first.
2815     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2816       for (const auto &I : CXXRD->bases()) {
2817         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
2818                "Unexpected base class!");
2819         const auto *Base =
2820             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2821 
2822         // Classify this field.
2823         //
2824         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
2825         // single eightbyte, each is classified separately. Each eightbyte gets
2826         // initialized to class NO_CLASS.
2827         Class FieldLo, FieldHi;
2828         uint64_t Offset =
2829           OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
2830         classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg);
2831         Lo = merge(Lo, FieldLo);
2832         Hi = merge(Hi, FieldHi);
2833         if (Lo == Memory || Hi == Memory) {
2834           postMerge(Size, Lo, Hi);
2835           return;
2836         }
2837       }
2838     }
2839 
2840     // Classify the fields one at a time, merging the results.
2841     unsigned idx = 0;
2842     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2843            i != e; ++i, ++idx) {
2844       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2845       bool BitField = i->isBitField();
2846 
2847       // Ignore padding bit-fields.
2848       if (BitField && i->isUnnamedBitfield())
2849         continue;
2850 
2851       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
2852       // four eightbytes, or it contains unaligned fields, it has class MEMORY.
2853       //
2854       // The only case a 256-bit wide vector could be used is when the struct
2855       // contains a single 256-bit element. Since Lo and Hi logic isn't extended
2856       // to work for sizes wider than 128, early check and fallback to memory.
2857       //
2858       if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) ||
2859                          Size > getNativeVectorSizeForAVXABI(AVXLevel))) {
2860         Lo = Memory;
2861         postMerge(Size, Lo, Hi);
2862         return;
2863       }
2864       // Note, skip this test for bit-fields, see below.
2865       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
2866         Lo = Memory;
2867         postMerge(Size, Lo, Hi);
2868         return;
2869       }
2870 
2871       // Classify this field.
2872       //
2873       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
2874       // exceeds a single eightbyte, each is classified
2875       // separately. Each eightbyte gets initialized to class
2876       // NO_CLASS.
2877       Class FieldLo, FieldHi;
2878 
2879       // Bit-fields require special handling, they do not force the
2880       // structure to be passed in memory even if unaligned, and
2881       // therefore they can straddle an eightbyte.
2882       if (BitField) {
2883         assert(!i->isUnnamedBitfield());
2884         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
2885         uint64_t Size = i->getBitWidthValue(getContext());
2886 
2887         uint64_t EB_Lo = Offset / 64;
2888         uint64_t EB_Hi = (Offset + Size - 1) / 64;
2889 
2890         if (EB_Lo) {
2891           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
2892           FieldLo = NoClass;
2893           FieldHi = Integer;
2894         } else {
2895           FieldLo = Integer;
2896           FieldHi = EB_Hi ? Integer : NoClass;
2897         }
2898       } else
2899         classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg);
2900       Lo = merge(Lo, FieldLo);
2901       Hi = merge(Hi, FieldHi);
2902       if (Lo == Memory || Hi == Memory)
2903         break;
2904     }
2905 
2906     postMerge(Size, Lo, Hi);
2907   }
2908 }
2909 
2910 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
2911   // If this is a scalar LLVM value then assume LLVM will pass it in the right
2912   // place naturally.
2913   if (!isAggregateTypeForABI(Ty)) {
2914     // Treat an enum type as its underlying type.
2915     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2916       Ty = EnumTy->getDecl()->getIntegerType();
2917 
2918     return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
2919                                           : ABIArgInfo::getDirect());
2920   }
2921 
2922   return getNaturalAlignIndirect(Ty);
2923 }
2924 
2925 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
2926   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
2927     uint64_t Size = getContext().getTypeSize(VecTy);
2928     unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel);
2929     if (Size <= 64 || Size > LargestVector)
2930       return true;
2931     QualType EltTy = VecTy->getElementType();
2932     if (passInt128VectorsInMem() &&
2933         (EltTy->isSpecificBuiltinType(BuiltinType::Int128) ||
2934          EltTy->isSpecificBuiltinType(BuiltinType::UInt128)))
2935       return true;
2936   }
2937 
2938   return false;
2939 }
2940 
2941 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
2942                                             unsigned freeIntRegs) const {
2943   // If this is a scalar LLVM value then assume LLVM will pass it in the right
2944   // place naturally.
2945   //
2946   // This assumption is optimistic, as there could be free registers available
2947   // when we need to pass this argument in memory, and LLVM could try to pass
2948   // the argument in the free register. This does not seem to happen currently,
2949   // but this code would be much safer if we could mark the argument with
2950   // 'onstack'. See PR12193.
2951   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
2952     // Treat an enum type as its underlying type.
2953     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2954       Ty = EnumTy->getDecl()->getIntegerType();
2955 
2956     return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
2957                                           : ABIArgInfo::getDirect());
2958   }
2959 
2960   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
2961     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
2962 
2963   // Compute the byval alignment. We specify the alignment of the byval in all
2964   // cases so that the mid-level optimizer knows the alignment of the byval.
2965   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
2966 
2967   // Attempt to avoid passing indirect results using byval when possible. This
2968   // is important for good codegen.
2969   //
2970   // We do this by coercing the value into a scalar type which the backend can
2971   // handle naturally (i.e., without using byval).
2972   //
2973   // For simplicity, we currently only do this when we have exhausted all of the
2974   // free integer registers. Doing this when there are free integer registers
2975   // would require more care, as we would have to ensure that the coerced value
2976   // did not claim the unused register. That would require either reording the
2977   // arguments to the function (so that any subsequent inreg values came first),
2978   // or only doing this optimization when there were no following arguments that
2979   // might be inreg.
2980   //
2981   // We currently expect it to be rare (particularly in well written code) for
2982   // arguments to be passed on the stack when there are still free integer
2983   // registers available (this would typically imply large structs being passed
2984   // by value), so this seems like a fair tradeoff for now.
2985   //
2986   // We can revisit this if the backend grows support for 'onstack' parameter
2987   // attributes. See PR12193.
2988   if (freeIntRegs == 0) {
2989     uint64_t Size = getContext().getTypeSize(Ty);
2990 
2991     // If this type fits in an eightbyte, coerce it into the matching integral
2992     // type, which will end up on the stack (with alignment 8).
2993     if (Align == 8 && Size <= 64)
2994       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2995                                                           Size));
2996   }
2997 
2998   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align));
2999 }
3000 
3001 /// The ABI specifies that a value should be passed in a full vector XMM/YMM
3002 /// register. Pick an LLVM IR type that will be passed as a vector register.
3003 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
3004   // Wrapper structs/arrays that only contain vectors are passed just like
3005   // vectors; strip them off if present.
3006   if (const Type *InnerTy = isSingleElementStruct(Ty, getContext()))
3007     Ty = QualType(InnerTy, 0);
3008 
3009   llvm::Type *IRType = CGT.ConvertType(Ty);
3010   if (isa<llvm::VectorType>(IRType)) {
3011     // Don't pass vXi128 vectors in their native type, the backend can't
3012     // legalize them.
3013     if (passInt128VectorsInMem() &&
3014         IRType->getVectorElementType()->isIntegerTy(128)) {
3015       // Use a vXi64 vector.
3016       uint64_t Size = getContext().getTypeSize(Ty);
3017       return llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()),
3018                                    Size / 64);
3019     }
3020 
3021     return IRType;
3022   }
3023 
3024   if (IRType->getTypeID() == llvm::Type::FP128TyID)
3025     return IRType;
3026 
3027   // We couldn't find the preferred IR vector type for 'Ty'.
3028   uint64_t Size = getContext().getTypeSize(Ty);
3029   assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!");
3030 
3031 
3032   // Return a LLVM IR vector type based on the size of 'Ty'.
3033   return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()),
3034                                Size / 64);
3035 }
3036 
3037 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
3038 /// is known to either be off the end of the specified type or being in
3039 /// alignment padding.  The user type specified is known to be at most 128 bits
3040 /// in size, and have passed through X86_64ABIInfo::classify with a successful
3041 /// classification that put one of the two halves in the INTEGER class.
3042 ///
3043 /// It is conservatively correct to return false.
3044 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
3045                                   unsigned EndBit, ASTContext &Context) {
3046   // If the bytes being queried are off the end of the type, there is no user
3047   // data hiding here.  This handles analysis of builtins, vectors and other
3048   // types that don't contain interesting padding.
3049   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
3050   if (TySize <= StartBit)
3051     return true;
3052 
3053   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
3054     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
3055     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
3056 
3057     // Check each element to see if the element overlaps with the queried range.
3058     for (unsigned i = 0; i != NumElts; ++i) {
3059       // If the element is after the span we care about, then we're done..
3060       unsigned EltOffset = i*EltSize;
3061       if (EltOffset >= EndBit) break;
3062 
3063       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
3064       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
3065                                  EndBit-EltOffset, Context))
3066         return false;
3067     }
3068     // If it overlaps no elements, then it is safe to process as padding.
3069     return true;
3070   }
3071 
3072   if (const RecordType *RT = Ty->getAs<RecordType>()) {
3073     const RecordDecl *RD = RT->getDecl();
3074     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3075 
3076     // If this is a C++ record, check the bases first.
3077     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
3078       for (const auto &I : CXXRD->bases()) {
3079         assert(!I.isVirtual() && !I.getType()->isDependentType() &&
3080                "Unexpected base class!");
3081         const auto *Base =
3082             cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
3083 
3084         // If the base is after the span we care about, ignore it.
3085         unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
3086         if (BaseOffset >= EndBit) continue;
3087 
3088         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
3089         if (!BitsContainNoUserData(I.getType(), BaseStart,
3090                                    EndBit-BaseOffset, Context))
3091           return false;
3092       }
3093     }
3094 
3095     // Verify that no field has data that overlaps the region of interest.  Yes
3096     // this could be sped up a lot by being smarter about queried fields,
3097     // however we're only looking at structs up to 16 bytes, so we don't care
3098     // much.
3099     unsigned idx = 0;
3100     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3101          i != e; ++i, ++idx) {
3102       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
3103 
3104       // If we found a field after the region we care about, then we're done.
3105       if (FieldOffset >= EndBit) break;
3106 
3107       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
3108       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
3109                                  Context))
3110         return false;
3111     }
3112 
3113     // If nothing in this record overlapped the area of interest, then we're
3114     // clean.
3115     return true;
3116   }
3117 
3118   return false;
3119 }
3120 
3121 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
3122 /// float member at the specified offset.  For example, {int,{float}} has a
3123 /// float at offset 4.  It is conservatively correct for this routine to return
3124 /// false.
3125 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
3126                                   const llvm::DataLayout &TD) {
3127   // Base case if we find a float.
3128   if (IROffset == 0 && IRType->isFloatTy())
3129     return true;
3130 
3131   // If this is a struct, recurse into the field at the specified offset.
3132   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3133     const llvm::StructLayout *SL = TD.getStructLayout(STy);
3134     unsigned Elt = SL->getElementContainingOffset(IROffset);
3135     IROffset -= SL->getElementOffset(Elt);
3136     return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
3137   }
3138 
3139   // If this is an array, recurse into the field at the specified offset.
3140   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3141     llvm::Type *EltTy = ATy->getElementType();
3142     unsigned EltSize = TD.getTypeAllocSize(EltTy);
3143     IROffset -= IROffset/EltSize*EltSize;
3144     return ContainsFloatAtOffset(EltTy, IROffset, TD);
3145   }
3146 
3147   return false;
3148 }
3149 
3150 
3151 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
3152 /// low 8 bytes of an XMM register, corresponding to the SSE class.
3153 llvm::Type *X86_64ABIInfo::
3154 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3155                    QualType SourceTy, unsigned SourceOffset) const {
3156   // The only three choices we have are either double, <2 x float>, or float. We
3157   // pass as float if the last 4 bytes is just padding.  This happens for
3158   // structs that contain 3 floats.
3159   if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
3160                             SourceOffset*8+64, getContext()))
3161     return llvm::Type::getFloatTy(getVMContext());
3162 
3163   // We want to pass as <2 x float> if the LLVM IR type contains a float at
3164   // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
3165   // case.
3166   if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) &&
3167       ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout()))
3168     return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
3169 
3170   return llvm::Type::getDoubleTy(getVMContext());
3171 }
3172 
3173 
3174 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
3175 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
3176 /// about the high or low part of an up-to-16-byte struct.  This routine picks
3177 /// the best LLVM IR type to represent this, which may be i64 or may be anything
3178 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
3179 /// etc).
3180 ///
3181 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
3182 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
3183 /// the 8-byte value references.  PrefType may be null.
3184 ///
3185 /// SourceTy is the source-level type for the entire argument.  SourceOffset is
3186 /// an offset into this that we're processing (which is always either 0 or 8).
3187 ///
3188 llvm::Type *X86_64ABIInfo::
3189 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
3190                        QualType SourceTy, unsigned SourceOffset) const {
3191   // If we're dealing with an un-offset LLVM IR type, then it means that we're
3192   // returning an 8-byte unit starting with it.  See if we can safely use it.
3193   if (IROffset == 0) {
3194     // Pointers and int64's always fill the 8-byte unit.
3195     if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) ||
3196         IRType->isIntegerTy(64))
3197       return IRType;
3198 
3199     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
3200     // goodness in the source type is just tail padding.  This is allowed to
3201     // kick in for struct {double,int} on the int, but not on
3202     // struct{double,int,int} because we wouldn't return the second int.  We
3203     // have to do this analysis on the source type because we can't depend on
3204     // unions being lowered a specific way etc.
3205     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
3206         IRType->isIntegerTy(32) ||
3207         (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) {
3208       unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 :
3209           cast<llvm::IntegerType>(IRType)->getBitWidth();
3210 
3211       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
3212                                 SourceOffset*8+64, getContext()))
3213         return IRType;
3214     }
3215   }
3216 
3217   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
3218     // If this is a struct, recurse into the field at the specified offset.
3219     const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy);
3220     if (IROffset < SL->getSizeInBytes()) {
3221       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
3222       IROffset -= SL->getElementOffset(FieldIdx);
3223 
3224       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
3225                                     SourceTy, SourceOffset);
3226     }
3227   }
3228 
3229   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
3230     llvm::Type *EltTy = ATy->getElementType();
3231     unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy);
3232     unsigned EltOffset = IROffset/EltSize*EltSize;
3233     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
3234                                   SourceOffset);
3235   }
3236 
3237   // Okay, we don't have any better idea of what to pass, so we pass this in an
3238   // integer register that isn't too big to fit the rest of the struct.
3239   unsigned TySizeInBytes =
3240     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
3241 
3242   assert(TySizeInBytes != SourceOffset && "Empty field?");
3243 
3244   // It is always safe to classify this as an integer type up to i64 that
3245   // isn't larger than the structure.
3246   return llvm::IntegerType::get(getVMContext(),
3247                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
3248 }
3249 
3250 
3251 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
3252 /// be used as elements of a two register pair to pass or return, return a
3253 /// first class aggregate to represent them.  For example, if the low part of
3254 /// a by-value argument should be passed as i32* and the high part as float,
3255 /// return {i32*, float}.
3256 static llvm::Type *
3257 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
3258                            const llvm::DataLayout &TD) {
3259   // In order to correctly satisfy the ABI, we need to the high part to start
3260   // at offset 8.  If the high and low parts we inferred are both 4-byte types
3261   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
3262   // the second element at offset 8.  Check for this:
3263   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
3264   unsigned HiAlign = TD.getABITypeAlignment(Hi);
3265   unsigned HiStart = llvm::alignTo(LoSize, HiAlign);
3266   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
3267 
3268   // To handle this, we have to increase the size of the low part so that the
3269   // second element will start at an 8 byte offset.  We can't increase the size
3270   // of the second element because it might make us access off the end of the
3271   // struct.
3272   if (HiStart != 8) {
3273     // There are usually two sorts of types the ABI generation code can produce
3274     // for the low part of a pair that aren't 8 bytes in size: float or
3275     // i8/i16/i32.  This can also include pointers when they are 32-bit (X32 and
3276     // NaCl).
3277     // Promote these to a larger type.
3278     if (Lo->isFloatTy())
3279       Lo = llvm::Type::getDoubleTy(Lo->getContext());
3280     else {
3281       assert((Lo->isIntegerTy() || Lo->isPointerTy())
3282              && "Invalid/unknown lo type");
3283       Lo = llvm::Type::getInt64Ty(Lo->getContext());
3284     }
3285   }
3286 
3287   llvm::StructType *Result = llvm::StructType::get(Lo, Hi);
3288 
3289   // Verify that the second element is at an 8-byte offset.
3290   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
3291          "Invalid x86-64 argument pair!");
3292   return Result;
3293 }
3294 
3295 ABIArgInfo X86_64ABIInfo::
3296 classifyReturnType(QualType RetTy) const {
3297   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
3298   // classification algorithm.
3299   X86_64ABIInfo::Class Lo, Hi;
3300   classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true);
3301 
3302   // Check some invariants.
3303   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3304   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3305 
3306   llvm::Type *ResType = nullptr;
3307   switch (Lo) {
3308   case NoClass:
3309     if (Hi == NoClass)
3310       return ABIArgInfo::getIgnore();
3311     // If the low part is just padding, it takes no register, leave ResType
3312     // null.
3313     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3314            "Unknown missing lo part");
3315     break;
3316 
3317   case SSEUp:
3318   case X87Up:
3319     llvm_unreachable("Invalid classification for lo word.");
3320 
3321     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
3322     // hidden argument.
3323   case Memory:
3324     return getIndirectReturnResult(RetTy);
3325 
3326     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
3327     // available register of the sequence %rax, %rdx is used.
3328   case Integer:
3329     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3330 
3331     // If we have a sign or zero extended integer, make sure to return Extend
3332     // so that the parameter gets the right LLVM IR attributes.
3333     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3334       // Treat an enum type as its underlying type.
3335       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3336         RetTy = EnumTy->getDecl()->getIntegerType();
3337 
3338       if (RetTy->isIntegralOrEnumerationType() &&
3339           RetTy->isPromotableIntegerType())
3340         return ABIArgInfo::getExtend(RetTy);
3341     }
3342     break;
3343 
3344     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
3345     // available SSE register of the sequence %xmm0, %xmm1 is used.
3346   case SSE:
3347     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
3348     break;
3349 
3350     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
3351     // returned on the X87 stack in %st0 as 80-bit x87 number.
3352   case X87:
3353     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
3354     break;
3355 
3356     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
3357     // part of the value is returned in %st0 and the imaginary part in
3358     // %st1.
3359   case ComplexX87:
3360     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
3361     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
3362                                     llvm::Type::getX86_FP80Ty(getVMContext()));
3363     break;
3364   }
3365 
3366   llvm::Type *HighPart = nullptr;
3367   switch (Hi) {
3368     // Memory was handled previously and X87 should
3369     // never occur as a hi class.
3370   case Memory:
3371   case X87:
3372     llvm_unreachable("Invalid classification for hi word.");
3373 
3374   case ComplexX87: // Previously handled.
3375   case NoClass:
3376     break;
3377 
3378   case Integer:
3379     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3380     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3381       return ABIArgInfo::getDirect(HighPart, 8);
3382     break;
3383   case SSE:
3384     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3385     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3386       return ABIArgInfo::getDirect(HighPart, 8);
3387     break;
3388 
3389     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
3390     // is passed in the next available eightbyte chunk if the last used
3391     // vector register.
3392     //
3393     // SSEUP should always be preceded by SSE, just widen.
3394   case SSEUp:
3395     assert(Lo == SSE && "Unexpected SSEUp classification.");
3396     ResType = GetByteVectorType(RetTy);
3397     break;
3398 
3399     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
3400     // returned together with the previous X87 value in %st0.
3401   case X87Up:
3402     // If X87Up is preceded by X87, we don't need to do
3403     // anything. However, in some cases with unions it may not be
3404     // preceded by X87. In such situations we follow gcc and pass the
3405     // extra bits in an SSE reg.
3406     if (Lo != X87) {
3407       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
3408       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
3409         return ABIArgInfo::getDirect(HighPart, 8);
3410     }
3411     break;
3412   }
3413 
3414   // If a high part was specified, merge it together with the low part.  It is
3415   // known to pass in the high eightbyte of the result.  We do this by forming a
3416   // first class struct aggregate with the high and low part: {low, high}
3417   if (HighPart)
3418     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3419 
3420   return ABIArgInfo::getDirect(ResType);
3421 }
3422 
3423 ABIArgInfo X86_64ABIInfo::classifyArgumentType(
3424   QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE,
3425   bool isNamedArg)
3426   const
3427 {
3428   Ty = useFirstFieldIfTransparentUnion(Ty);
3429 
3430   X86_64ABIInfo::Class Lo, Hi;
3431   classify(Ty, 0, Lo, Hi, isNamedArg);
3432 
3433   // Check some invariants.
3434   // FIXME: Enforce these by construction.
3435   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
3436   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
3437 
3438   neededInt = 0;
3439   neededSSE = 0;
3440   llvm::Type *ResType = nullptr;
3441   switch (Lo) {
3442   case NoClass:
3443     if (Hi == NoClass)
3444       return ABIArgInfo::getIgnore();
3445     // If the low part is just padding, it takes no register, leave ResType
3446     // null.
3447     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
3448            "Unknown missing lo part");
3449     break;
3450 
3451     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
3452     // on the stack.
3453   case Memory:
3454 
3455     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
3456     // COMPLEX_X87, it is passed in memory.
3457   case X87:
3458   case ComplexX87:
3459     if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect)
3460       ++neededInt;
3461     return getIndirectResult(Ty, freeIntRegs);
3462 
3463   case SSEUp:
3464   case X87Up:
3465     llvm_unreachable("Invalid classification for lo word.");
3466 
3467     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
3468     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
3469     // and %r9 is used.
3470   case Integer:
3471     ++neededInt;
3472 
3473     // Pick an 8-byte type based on the preferred type.
3474     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
3475 
3476     // If we have a sign or zero extended integer, make sure to return Extend
3477     // so that the parameter gets the right LLVM IR attributes.
3478     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
3479       // Treat an enum type as its underlying type.
3480       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3481         Ty = EnumTy->getDecl()->getIntegerType();
3482 
3483       if (Ty->isIntegralOrEnumerationType() &&
3484           Ty->isPromotableIntegerType())
3485         return ABIArgInfo::getExtend(Ty);
3486     }
3487 
3488     break;
3489 
3490     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
3491     // available SSE register is used, the registers are taken in the
3492     // order from %xmm0 to %xmm7.
3493   case SSE: {
3494     llvm::Type *IRType = CGT.ConvertType(Ty);
3495     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
3496     ++neededSSE;
3497     break;
3498   }
3499   }
3500 
3501   llvm::Type *HighPart = nullptr;
3502   switch (Hi) {
3503     // Memory was handled previously, ComplexX87 and X87 should
3504     // never occur as hi classes, and X87Up must be preceded by X87,
3505     // which is passed in memory.
3506   case Memory:
3507   case X87:
3508   case ComplexX87:
3509     llvm_unreachable("Invalid classification for hi word.");
3510 
3511   case NoClass: break;
3512 
3513   case Integer:
3514     ++neededInt;
3515     // Pick an 8-byte type based on the preferred type.
3516     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3517 
3518     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3519       return ABIArgInfo::getDirect(HighPart, 8);
3520     break;
3521 
3522     // X87Up generally doesn't occur here (long double is passed in
3523     // memory), except in situations involving unions.
3524   case X87Up:
3525   case SSE:
3526     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
3527 
3528     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
3529       return ABIArgInfo::getDirect(HighPart, 8);
3530 
3531     ++neededSSE;
3532     break;
3533 
3534     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
3535     // eightbyte is passed in the upper half of the last used SSE
3536     // register.  This only happens when 128-bit vectors are passed.
3537   case SSEUp:
3538     assert(Lo == SSE && "Unexpected SSEUp classification");
3539     ResType = GetByteVectorType(Ty);
3540     break;
3541   }
3542 
3543   // If a high part was specified, merge it together with the low part.  It is
3544   // known to pass in the high eightbyte of the result.  We do this by forming a
3545   // first class struct aggregate with the high and low part: {low, high}
3546   if (HighPart)
3547     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout());
3548 
3549   return ABIArgInfo::getDirect(ResType);
3550 }
3551 
3552 ABIArgInfo
3553 X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt,
3554                                              unsigned &NeededSSE) const {
3555   auto RT = Ty->getAs<RecordType>();
3556   assert(RT && "classifyRegCallStructType only valid with struct types");
3557 
3558   if (RT->getDecl()->hasFlexibleArrayMember())
3559     return getIndirectReturnResult(Ty);
3560 
3561   // Sum up bases
3562   if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3563     if (CXXRD->isDynamicClass()) {
3564       NeededInt = NeededSSE = 0;
3565       return getIndirectReturnResult(Ty);
3566     }
3567 
3568     for (const auto &I : CXXRD->bases())
3569       if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE)
3570               .isIndirect()) {
3571         NeededInt = NeededSSE = 0;
3572         return getIndirectReturnResult(Ty);
3573       }
3574   }
3575 
3576   // Sum up members
3577   for (const auto *FD : RT->getDecl()->fields()) {
3578     if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) {
3579       if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE)
3580               .isIndirect()) {
3581         NeededInt = NeededSSE = 0;
3582         return getIndirectReturnResult(Ty);
3583       }
3584     } else {
3585       unsigned LocalNeededInt, LocalNeededSSE;
3586       if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt,
3587                                LocalNeededSSE, true)
3588               .isIndirect()) {
3589         NeededInt = NeededSSE = 0;
3590         return getIndirectReturnResult(Ty);
3591       }
3592       NeededInt += LocalNeededInt;
3593       NeededSSE += LocalNeededSSE;
3594     }
3595   }
3596 
3597   return ABIArgInfo::getDirect();
3598 }
3599 
3600 ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty,
3601                                                     unsigned &NeededInt,
3602                                                     unsigned &NeededSSE) const {
3603 
3604   NeededInt = 0;
3605   NeededSSE = 0;
3606 
3607   return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE);
3608 }
3609 
3610 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
3611 
3612   const unsigned CallingConv = FI.getCallingConvention();
3613   // It is possible to force Win64 calling convention on any x86_64 target by
3614   // using __attribute__((ms_abi)). In such case to correctly emit Win64
3615   // compatible code delegate this call to WinX86_64ABIInfo::computeInfo.
3616   if (CallingConv == llvm::CallingConv::Win64) {
3617     WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel);
3618     Win64ABIInfo.computeInfo(FI);
3619     return;
3620   }
3621 
3622   bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall;
3623 
3624   // Keep track of the number of assigned registers.
3625   unsigned FreeIntRegs = IsRegCall ? 11 : 6;
3626   unsigned FreeSSERegs = IsRegCall ? 16 : 8;
3627   unsigned NeededInt, NeededSSE;
3628 
3629   if (!::classifyReturnType(getCXXABI(), FI, *this)) {
3630     if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() &&
3631         !FI.getReturnType()->getTypePtr()->isUnionType()) {
3632       FI.getReturnInfo() =
3633           classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE);
3634       if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3635         FreeIntRegs -= NeededInt;
3636         FreeSSERegs -= NeededSSE;
3637       } else {
3638         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3639       }
3640     } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) {
3641       // Complex Long Double Type is passed in Memory when Regcall
3642       // calling convention is used.
3643       const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>();
3644       if (getContext().getCanonicalType(CT->getElementType()) ==
3645           getContext().LongDoubleTy)
3646         FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType());
3647     } else
3648       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3649   }
3650 
3651   // If the return value is indirect, then the hidden argument is consuming one
3652   // integer register.
3653   if (FI.getReturnInfo().isIndirect())
3654     --FreeIntRegs;
3655 
3656   // The chain argument effectively gives us another free register.
3657   if (FI.isChainCall())
3658     ++FreeIntRegs;
3659 
3660   unsigned NumRequiredArgs = FI.getNumRequiredArgs();
3661   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
3662   // get assigned (in left-to-right order) for passing as follows...
3663   unsigned ArgNo = 0;
3664   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3665        it != ie; ++it, ++ArgNo) {
3666     bool IsNamedArg = ArgNo < NumRequiredArgs;
3667 
3668     if (IsRegCall && it->type->isStructureOrClassType())
3669       it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE);
3670     else
3671       it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt,
3672                                       NeededSSE, IsNamedArg);
3673 
3674     // AMD64-ABI 3.2.3p3: If there are no registers available for any
3675     // eightbyte of an argument, the whole argument is passed on the
3676     // stack. If registers have already been assigned for some
3677     // eightbytes of such an argument, the assignments get reverted.
3678     if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) {
3679       FreeIntRegs -= NeededInt;
3680       FreeSSERegs -= NeededSSE;
3681     } else {
3682       it->info = getIndirectResult(it->type, FreeIntRegs);
3683     }
3684   }
3685 }
3686 
3687 static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF,
3688                                          Address VAListAddr, QualType Ty) {
3689   Address overflow_arg_area_p =
3690       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
3691   llvm::Value *overflow_arg_area =
3692     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
3693 
3694   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
3695   // byte boundary if alignment needed by type exceeds 8 byte boundary.
3696   // It isn't stated explicitly in the standard, but in practice we use
3697   // alignment greater than 16 where necessary.
3698   CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
3699   if (Align > CharUnits::fromQuantity(8)) {
3700     overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area,
3701                                                       Align);
3702   }
3703 
3704   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
3705   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3706   llvm::Value *Res =
3707     CGF.Builder.CreateBitCast(overflow_arg_area,
3708                               llvm::PointerType::getUnqual(LTy));
3709 
3710   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
3711   // l->overflow_arg_area + sizeof(type).
3712   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
3713   // an 8 byte boundary.
3714 
3715   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
3716   llvm::Value *Offset =
3717       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
3718   overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
3719                                             "overflow_arg_area.next");
3720   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
3721 
3722   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
3723   return Address(Res, Align);
3724 }
3725 
3726 Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
3727                                  QualType Ty) const {
3728   // Assume that va_list type is correct; should be pointer to LLVM type:
3729   // struct {
3730   //   i32 gp_offset;
3731   //   i32 fp_offset;
3732   //   i8* overflow_arg_area;
3733   //   i8* reg_save_area;
3734   // };
3735   unsigned neededInt, neededSSE;
3736 
3737   Ty = getContext().getCanonicalType(Ty);
3738   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE,
3739                                        /*isNamedArg*/false);
3740 
3741   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
3742   // in the registers. If not go to step 7.
3743   if (!neededInt && !neededSSE)
3744     return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3745 
3746   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
3747   // general purpose registers needed to pass type and num_fp to hold
3748   // the number of floating point registers needed.
3749 
3750   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
3751   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
3752   // l->fp_offset > 304 - num_fp * 16 go to step 7.
3753   //
3754   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
3755   // register save space).
3756 
3757   llvm::Value *InRegs = nullptr;
3758   Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid();
3759   llvm::Value *gp_offset = nullptr, *fp_offset = nullptr;
3760   if (neededInt) {
3761     gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
3762     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
3763     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
3764     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
3765   }
3766 
3767   if (neededSSE) {
3768     fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
3769     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
3770     llvm::Value *FitsInFP =
3771       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
3772     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
3773     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
3774   }
3775 
3776   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
3777   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
3778   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
3779   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
3780 
3781   // Emit code to load the value if it was passed in registers.
3782 
3783   CGF.EmitBlock(InRegBlock);
3784 
3785   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
3786   // an offset of l->gp_offset and/or l->fp_offset. This may require
3787   // copying to a temporary location in case the parameter is passed
3788   // in different register classes or requires an alignment greater
3789   // than 8 for general purpose registers and 16 for XMM registers.
3790   //
3791   // FIXME: This really results in shameful code when we end up needing to
3792   // collect arguments from different places; often what should result in a
3793   // simple assembling of a structure from scattered addresses has many more
3794   // loads than necessary. Can we clean this up?
3795   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
3796   llvm::Value *RegSaveArea = CGF.Builder.CreateLoad(
3797       CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area");
3798 
3799   Address RegAddr = Address::invalid();
3800   if (neededInt && neededSSE) {
3801     // FIXME: Cleanup.
3802     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
3803     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
3804     Address Tmp = CGF.CreateMemTemp(Ty);
3805     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3806     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
3807     llvm::Type *TyLo = ST->getElementType(0);
3808     llvm::Type *TyHi = ST->getElementType(1);
3809     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
3810            "Unexpected ABI info for mixed regs");
3811     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
3812     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
3813     llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset);
3814     llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset);
3815     llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr;
3816     llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr;
3817 
3818     // Copy the first element.
3819     // FIXME: Our choice of alignment here and below is probably pessimistic.
3820     llvm::Value *V = CGF.Builder.CreateAlignedLoad(
3821         TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo),
3822         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo)));
3823     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
3824 
3825     // Copy the second element.
3826     V = CGF.Builder.CreateAlignedLoad(
3827         TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi),
3828         CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi)));
3829     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
3830 
3831     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
3832   } else if (neededInt) {
3833     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset),
3834                       CharUnits::fromQuantity(8));
3835     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
3836 
3837     // Copy to a temporary if necessary to ensure the appropriate alignment.
3838     std::pair<CharUnits, CharUnits> SizeAlign =
3839         getContext().getTypeInfoInChars(Ty);
3840     uint64_t TySize = SizeAlign.first.getQuantity();
3841     CharUnits TyAlign = SizeAlign.second;
3842 
3843     // Copy into a temporary if the type is more aligned than the
3844     // register save area.
3845     if (TyAlign.getQuantity() > 8) {
3846       Address Tmp = CGF.CreateMemTemp(Ty);
3847       CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false);
3848       RegAddr = Tmp;
3849     }
3850 
3851   } else if (neededSSE == 1) {
3852     RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3853                       CharUnits::fromQuantity(16));
3854     RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy);
3855   } else {
3856     assert(neededSSE == 2 && "Invalid number of needed registers!");
3857     // SSE registers are spaced 16 bytes apart in the register save
3858     // area, we need to collect the two eightbytes together.
3859     // The ABI isn't explicit about this, but it seems reasonable
3860     // to assume that the slots are 16-byte aligned, since the stack is
3861     // naturally 16-byte aligned and the prologue is expected to store
3862     // all the SSE registers to the RSA.
3863     Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset),
3864                                 CharUnits::fromQuantity(16));
3865     Address RegAddrHi =
3866       CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo,
3867                                              CharUnits::fromQuantity(16));
3868     llvm::Type *ST = AI.canHaveCoerceToType()
3869                          ? AI.getCoerceToType()
3870                          : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy);
3871     llvm::Value *V;
3872     Address Tmp = CGF.CreateMemTemp(Ty);
3873     Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST);
3874     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
3875         RegAddrLo, ST->getStructElementType(0)));
3876     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
3877     V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast(
3878         RegAddrHi, ST->getStructElementType(1)));
3879     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
3880 
3881     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy);
3882   }
3883 
3884   // AMD64-ABI 3.5.7p5: Step 5. Set:
3885   // l->gp_offset = l->gp_offset + num_gp * 8
3886   // l->fp_offset = l->fp_offset + num_fp * 16.
3887   if (neededInt) {
3888     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
3889     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
3890                             gp_offset_p);
3891   }
3892   if (neededSSE) {
3893     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
3894     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
3895                             fp_offset_p);
3896   }
3897   CGF.EmitBranch(ContBlock);
3898 
3899   // Emit code to load the value if it was passed in memory.
3900 
3901   CGF.EmitBlock(InMemBlock);
3902   Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty);
3903 
3904   // Return the appropriate result.
3905 
3906   CGF.EmitBlock(ContBlock);
3907   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock,
3908                                  "vaarg.addr");
3909   return ResAddr;
3910 }
3911 
3912 Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
3913                                    QualType Ty) const {
3914   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
3915                           CGF.getContext().getTypeInfoInChars(Ty),
3916                           CharUnits::fromQuantity(8),
3917                           /*allowHigherAlign*/ false);
3918 }
3919 
3920 ABIArgInfo
3921 WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs,
3922                                     const ABIArgInfo &current) const {
3923   // Assumes vectorCall calling convention.
3924   const Type *Base = nullptr;
3925   uint64_t NumElts = 0;
3926 
3927   if (!Ty->isBuiltinType() && !Ty->isVectorType() &&
3928       isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) {
3929     FreeSSERegs -= NumElts;
3930     return getDirectX86Hva();
3931   }
3932   return current;
3933 }
3934 
3935 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs,
3936                                       bool IsReturnType, bool IsVectorCall,
3937                                       bool IsRegCall) const {
3938 
3939   if (Ty->isVoidType())
3940     return ABIArgInfo::getIgnore();
3941 
3942   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3943     Ty = EnumTy->getDecl()->getIntegerType();
3944 
3945   TypeInfo Info = getContext().getTypeInfo(Ty);
3946   uint64_t Width = Info.Width;
3947   CharUnits Align = getContext().toCharUnitsFromBits(Info.Align);
3948 
3949   const RecordType *RT = Ty->getAs<RecordType>();
3950   if (RT) {
3951     if (!IsReturnType) {
3952       if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()))
3953         return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
3954     }
3955 
3956     if (RT->getDecl()->hasFlexibleArrayMember())
3957       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
3958 
3959   }
3960 
3961   const Type *Base = nullptr;
3962   uint64_t NumElts = 0;
3963   // vectorcall adds the concept of a homogenous vector aggregate, similar to
3964   // other targets.
3965   if ((IsVectorCall || IsRegCall) &&
3966       isHomogeneousAggregate(Ty, Base, NumElts)) {
3967     if (IsRegCall) {
3968       if (FreeSSERegs >= NumElts) {
3969         FreeSSERegs -= NumElts;
3970         if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())
3971           return ABIArgInfo::getDirect();
3972         return ABIArgInfo::getExpand();
3973       }
3974       return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3975     } else if (IsVectorCall) {
3976       if (FreeSSERegs >= NumElts &&
3977           (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) {
3978         FreeSSERegs -= NumElts;
3979         return ABIArgInfo::getDirect();
3980       } else if (IsReturnType) {
3981         return ABIArgInfo::getExpand();
3982       } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) {
3983         // HVAs are delayed and reclassified in the 2nd step.
3984         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
3985       }
3986     }
3987   }
3988 
3989   if (Ty->isMemberPointerType()) {
3990     // If the member pointer is represented by an LLVM int or ptr, pass it
3991     // directly.
3992     llvm::Type *LLTy = CGT.ConvertType(Ty);
3993     if (LLTy->isPointerTy() || LLTy->isIntegerTy())
3994       return ABIArgInfo::getDirect();
3995   }
3996 
3997   if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) {
3998     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
3999     // not 1, 2, 4, or 8 bytes, must be passed by reference."
4000     if (Width > 64 || !llvm::isPowerOf2_64(Width))
4001       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4002 
4003     // Otherwise, coerce it to a small integer.
4004     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width));
4005   }
4006 
4007   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4008     switch (BT->getKind()) {
4009     case BuiltinType::Bool:
4010       // Bool type is always extended to the ABI, other builtin types are not
4011       // extended.
4012       return ABIArgInfo::getExtend(Ty);
4013 
4014     case BuiltinType::LongDouble:
4015       // Mingw64 GCC uses the old 80 bit extended precision floating point
4016       // unit. It passes them indirectly through memory.
4017       if (IsMingw64) {
4018         const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat();
4019         if (LDF == &llvm::APFloat::x87DoubleExtended())
4020           return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4021       }
4022       break;
4023 
4024     case BuiltinType::Int128:
4025     case BuiltinType::UInt128:
4026       // If it's a parameter type, the normal ABI rule is that arguments larger
4027       // than 8 bytes are passed indirectly. GCC follows it. We follow it too,
4028       // even though it isn't particularly efficient.
4029       if (!IsReturnType)
4030         return ABIArgInfo::getIndirect(Align, /*ByVal=*/false);
4031 
4032       // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that.
4033       // Clang matches them for compatibility.
4034       return ABIArgInfo::getDirect(
4035           llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2));
4036 
4037     default:
4038       break;
4039     }
4040   }
4041 
4042   return ABIArgInfo::getDirect();
4043 }
4044 
4045 void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI,
4046                                              unsigned FreeSSERegs,
4047                                              bool IsVectorCall,
4048                                              bool IsRegCall) const {
4049   unsigned Count = 0;
4050   for (auto &I : FI.arguments()) {
4051     // Vectorcall in x64 only permits the first 6 arguments to be passed
4052     // as XMM/YMM registers.
4053     if (Count < VectorcallMaxParamNumAsReg)
4054       I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
4055     else {
4056       // Since these cannot be passed in registers, pretend no registers
4057       // are left.
4058       unsigned ZeroSSERegsAvail = 0;
4059       I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false,
4060                         IsVectorCall, IsRegCall);
4061     }
4062     ++Count;
4063   }
4064 
4065   for (auto &I : FI.arguments()) {
4066     I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info);
4067   }
4068 }
4069 
4070 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
4071   const unsigned CC = FI.getCallingConvention();
4072   bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall;
4073   bool IsRegCall = CC == llvm::CallingConv::X86_RegCall;
4074 
4075   // If __attribute__((sysv_abi)) is in use, use the SysV argument
4076   // classification rules.
4077   if (CC == llvm::CallingConv::X86_64_SysV) {
4078     X86_64ABIInfo SysVABIInfo(CGT, AVXLevel);
4079     SysVABIInfo.computeInfo(FI);
4080     return;
4081   }
4082 
4083   unsigned FreeSSERegs = 0;
4084   if (IsVectorCall) {
4085     // We can use up to 4 SSE return registers with vectorcall.
4086     FreeSSERegs = 4;
4087   } else if (IsRegCall) {
4088     // RegCall gives us 16 SSE registers.
4089     FreeSSERegs = 16;
4090   }
4091 
4092   if (!getCXXABI().classifyReturnType(FI))
4093     FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true,
4094                                   IsVectorCall, IsRegCall);
4095 
4096   if (IsVectorCall) {
4097     // We can use up to 6 SSE register parameters with vectorcall.
4098     FreeSSERegs = 6;
4099   } else if (IsRegCall) {
4100     // RegCall gives us 16 SSE registers, we can reuse the return registers.
4101     FreeSSERegs = 16;
4102   }
4103 
4104   if (IsVectorCall) {
4105     computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall);
4106   } else {
4107     for (auto &I : FI.arguments())
4108       I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall);
4109   }
4110 
4111 }
4112 
4113 Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4114                                     QualType Ty) const {
4115 
4116   bool IsIndirect = false;
4117 
4118   // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
4119   // not 1, 2, 4, or 8 bytes, must be passed by reference."
4120   if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) {
4121     uint64_t Width = getContext().getTypeSize(Ty);
4122     IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width);
4123   }
4124 
4125   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
4126                           CGF.getContext().getTypeInfoInChars(Ty),
4127                           CharUnits::fromQuantity(8),
4128                           /*allowHigherAlign*/ false);
4129 }
4130 
4131 // PowerPC-32
4132 namespace {
4133 /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information.
4134 class PPC32_SVR4_ABIInfo : public DefaultABIInfo {
4135   bool IsSoftFloatABI;
4136 
4137   CharUnits getParamTypeAlignment(QualType Ty) const;
4138 
4139 public:
4140   PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI)
4141       : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {}
4142 
4143   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4144                     QualType Ty) const override;
4145 };
4146 
4147 class PPC32TargetCodeGenInfo : public TargetCodeGenInfo {
4148 public:
4149   PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI)
4150       : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {}
4151 
4152   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4153     // This is recovered from gcc output.
4154     return 1; // r1 is the dedicated stack pointer
4155   }
4156 
4157   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4158                                llvm::Value *Address) const override;
4159 };
4160 }
4161 
4162 CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4163   // Complex types are passed just like their elements
4164   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4165     Ty = CTy->getElementType();
4166 
4167   if (Ty->isVectorType())
4168     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16
4169                                                                        : 4);
4170 
4171   // For single-element float/vector structs, we consider the whole type
4172   // to have the same alignment requirements as its single element.
4173   const Type *AlignTy = nullptr;
4174   if (const Type *EltType = isSingleElementStruct(Ty, getContext())) {
4175     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4176     if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) ||
4177         (BT && BT->isFloatingPoint()))
4178       AlignTy = EltType;
4179   }
4180 
4181   if (AlignTy)
4182     return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4);
4183   return CharUnits::fromQuantity(4);
4184 }
4185 
4186 // TODO: this implementation is now likely redundant with
4187 // DefaultABIInfo::EmitVAArg.
4188 Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList,
4189                                       QualType Ty) const {
4190   if (getTarget().getTriple().isOSDarwin()) {
4191     auto TI = getContext().getTypeInfoInChars(Ty);
4192     TI.second = getParamTypeAlignment(Ty);
4193 
4194     CharUnits SlotSize = CharUnits::fromQuantity(4);
4195     return emitVoidPtrVAArg(CGF, VAList, Ty,
4196                             classifyArgumentType(Ty).isIndirect(), TI, SlotSize,
4197                             /*AllowHigherAlign=*/true);
4198   }
4199 
4200   const unsigned OverflowLimit = 8;
4201   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4202     // TODO: Implement this. For now ignore.
4203     (void)CTy;
4204     return Address::invalid(); // FIXME?
4205   }
4206 
4207   // struct __va_list_tag {
4208   //   unsigned char gpr;
4209   //   unsigned char fpr;
4210   //   unsigned short reserved;
4211   //   void *overflow_arg_area;
4212   //   void *reg_save_area;
4213   // };
4214 
4215   bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64;
4216   bool isInt =
4217       Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType();
4218   bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64;
4219 
4220   // All aggregates are passed indirectly?  That doesn't seem consistent
4221   // with the argument-lowering code.
4222   bool isIndirect = Ty->isAggregateType();
4223 
4224   CGBuilderTy &Builder = CGF.Builder;
4225 
4226   // The calling convention either uses 1-2 GPRs or 1 FPR.
4227   Address NumRegsAddr = Address::invalid();
4228   if (isInt || IsSoftFloatABI) {
4229     NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr");
4230   } else {
4231     NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr");
4232   }
4233 
4234   llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs");
4235 
4236   // "Align" the register count when TY is i64.
4237   if (isI64 || (isF64 && IsSoftFloatABI)) {
4238     NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1));
4239     NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U));
4240   }
4241 
4242   llvm::Value *CC =
4243       Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond");
4244 
4245   llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs");
4246   llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow");
4247   llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
4248 
4249   Builder.CreateCondBr(CC, UsingRegs, UsingOverflow);
4250 
4251   llvm::Type *DirectTy = CGF.ConvertType(Ty);
4252   if (isIndirect) DirectTy = DirectTy->getPointerTo(0);
4253 
4254   // Case 1: consume registers.
4255   Address RegAddr = Address::invalid();
4256   {
4257     CGF.EmitBlock(UsingRegs);
4258 
4259     Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4);
4260     RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr),
4261                       CharUnits::fromQuantity(8));
4262     assert(RegAddr.getElementType() == CGF.Int8Ty);
4263 
4264     // Floating-point registers start after the general-purpose registers.
4265     if (!(isInt || IsSoftFloatABI)) {
4266       RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr,
4267                                                    CharUnits::fromQuantity(32));
4268     }
4269 
4270     // Get the address of the saved value by scaling the number of
4271     // registers we've used by the number of
4272     CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8);
4273     llvm::Value *RegOffset =
4274       Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity()));
4275     RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty,
4276                                             RegAddr.getPointer(), RegOffset),
4277                       RegAddr.getAlignment().alignmentOfArrayElement(RegSize));
4278     RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy);
4279 
4280     // Increase the used-register count.
4281     NumRegs =
4282       Builder.CreateAdd(NumRegs,
4283                         Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1));
4284     Builder.CreateStore(NumRegs, NumRegsAddr);
4285 
4286     CGF.EmitBranch(Cont);
4287   }
4288 
4289   // Case 2: consume space in the overflow area.
4290   Address MemAddr = Address::invalid();
4291   {
4292     CGF.EmitBlock(UsingOverflow);
4293 
4294     Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr);
4295 
4296     // Everything in the overflow area is rounded up to a size of at least 4.
4297     CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4);
4298 
4299     CharUnits Size;
4300     if (!isIndirect) {
4301       auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty);
4302       Size = TypeInfo.first.alignTo(OverflowAreaAlign);
4303     } else {
4304       Size = CGF.getPointerSize();
4305     }
4306 
4307     Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3);
4308     Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"),
4309                          OverflowAreaAlign);
4310     // Round up address of argument to alignment
4311     CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty);
4312     if (Align > OverflowAreaAlign) {
4313       llvm::Value *Ptr = OverflowArea.getPointer();
4314       OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align),
4315                                                            Align);
4316     }
4317 
4318     MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy);
4319 
4320     // Increase the overflow area.
4321     OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size);
4322     Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr);
4323     CGF.EmitBranch(Cont);
4324   }
4325 
4326   CGF.EmitBlock(Cont);
4327 
4328   // Merge the cases with a phi.
4329   Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow,
4330                                 "vaarg.addr");
4331 
4332   // Load the pointer if the argument was passed indirectly.
4333   if (isIndirect) {
4334     Result = Address(Builder.CreateLoad(Result, "aggr"),
4335                      getContext().getTypeAlignInChars(Ty));
4336   }
4337 
4338   return Result;
4339 }
4340 
4341 bool
4342 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4343                                                 llvm::Value *Address) const {
4344   // This is calculated from the LLVM and GCC tables and verified
4345   // against gcc output.  AFAIK all ABIs use the same encoding.
4346 
4347   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4348 
4349   llvm::IntegerType *i8 = CGF.Int8Ty;
4350   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4351   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4352   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4353 
4354   // 0-31: r0-31, the 4-byte general-purpose registers
4355   AssignToArrayRange(Builder, Address, Four8, 0, 31);
4356 
4357   // 32-63: fp0-31, the 8-byte floating-point registers
4358   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4359 
4360   // 64-76 are various 4-byte special-purpose registers:
4361   // 64: mq
4362   // 65: lr
4363   // 66: ctr
4364   // 67: ap
4365   // 68-75 cr0-7
4366   // 76: xer
4367   AssignToArrayRange(Builder, Address, Four8, 64, 76);
4368 
4369   // 77-108: v0-31, the 16-byte vector registers
4370   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4371 
4372   // 109: vrsave
4373   // 110: vscr
4374   // 111: spe_acc
4375   // 112: spefscr
4376   // 113: sfp
4377   AssignToArrayRange(Builder, Address, Four8, 109, 113);
4378 
4379   return false;
4380 }
4381 
4382 // PowerPC-64
4383 
4384 namespace {
4385 /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information.
4386 class PPC64_SVR4_ABIInfo : public SwiftABIInfo {
4387 public:
4388   enum ABIKind {
4389     ELFv1 = 0,
4390     ELFv2
4391   };
4392 
4393 private:
4394   static const unsigned GPRBits = 64;
4395   ABIKind Kind;
4396   bool HasQPX;
4397   bool IsSoftFloatABI;
4398 
4399   // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and
4400   // will be passed in a QPX register.
4401   bool IsQPXVectorTy(const Type *Ty) const {
4402     if (!HasQPX)
4403       return false;
4404 
4405     if (const VectorType *VT = Ty->getAs<VectorType>()) {
4406       unsigned NumElements = VT->getNumElements();
4407       if (NumElements == 1)
4408         return false;
4409 
4410       if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) {
4411         if (getContext().getTypeSize(Ty) <= 256)
4412           return true;
4413       } else if (VT->getElementType()->
4414                    isSpecificBuiltinType(BuiltinType::Float)) {
4415         if (getContext().getTypeSize(Ty) <= 128)
4416           return true;
4417       }
4418     }
4419 
4420     return false;
4421   }
4422 
4423   bool IsQPXVectorTy(QualType Ty) const {
4424     return IsQPXVectorTy(Ty.getTypePtr());
4425   }
4426 
4427 public:
4428   PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX,
4429                      bool SoftFloatABI)
4430       : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX),
4431         IsSoftFloatABI(SoftFloatABI) {}
4432 
4433   bool isPromotableTypeForABI(QualType Ty) const;
4434   CharUnits getParamTypeAlignment(QualType Ty) const;
4435 
4436   ABIArgInfo classifyReturnType(QualType RetTy) const;
4437   ABIArgInfo classifyArgumentType(QualType Ty) const;
4438 
4439   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
4440   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
4441                                          uint64_t Members) const override;
4442 
4443   // TODO: We can add more logic to computeInfo to improve performance.
4444   // Example: For aggregate arguments that fit in a register, we could
4445   // use getDirectInReg (as is done below for structs containing a single
4446   // floating-point value) to avoid pushing them to memory on function
4447   // entry.  This would require changing the logic in PPCISelLowering
4448   // when lowering the parameters in the caller and args in the callee.
4449   void computeInfo(CGFunctionInfo &FI) const override {
4450     if (!getCXXABI().classifyReturnType(FI))
4451       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4452     for (auto &I : FI.arguments()) {
4453       // We rely on the default argument classification for the most part.
4454       // One exception:  An aggregate containing a single floating-point
4455       // or vector item must be passed in a register if one is available.
4456       const Type *T = isSingleElementStruct(I.type, getContext());
4457       if (T) {
4458         const BuiltinType *BT = T->getAs<BuiltinType>();
4459         if (IsQPXVectorTy(T) ||
4460             (T->isVectorType() && getContext().getTypeSize(T) == 128) ||
4461             (BT && BT->isFloatingPoint())) {
4462           QualType QT(T, 0);
4463           I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT));
4464           continue;
4465         }
4466       }
4467       I.info = classifyArgumentType(I.type);
4468     }
4469   }
4470 
4471   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4472                     QualType Ty) const override;
4473 
4474   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
4475                                     bool asReturnValue) const override {
4476     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
4477   }
4478 
4479   bool isSwiftErrorInRegister() const override {
4480     return false;
4481   }
4482 };
4483 
4484 class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo {
4485 
4486 public:
4487   PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT,
4488                                PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX,
4489                                bool SoftFloatABI)
4490       : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX,
4491                                                  SoftFloatABI)) {}
4492 
4493   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4494     // This is recovered from gcc output.
4495     return 1; // r1 is the dedicated stack pointer
4496   }
4497 
4498   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4499                                llvm::Value *Address) const override;
4500 };
4501 
4502 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
4503 public:
4504   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
4505 
4506   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
4507     // This is recovered from gcc output.
4508     return 1; // r1 is the dedicated stack pointer
4509   }
4510 
4511   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4512                                llvm::Value *Address) const override;
4513 };
4514 
4515 }
4516 
4517 // Return true if the ABI requires Ty to be passed sign- or zero-
4518 // extended to 64 bits.
4519 bool
4520 PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const {
4521   // Treat an enum type as its underlying type.
4522   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4523     Ty = EnumTy->getDecl()->getIntegerType();
4524 
4525   // Promotable integer types are required to be promoted by the ABI.
4526   if (Ty->isPromotableIntegerType())
4527     return true;
4528 
4529   // In addition to the usual promotable integer types, we also need to
4530   // extend all 32-bit types, since the ABI requires promotion to 64 bits.
4531   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
4532     switch (BT->getKind()) {
4533     case BuiltinType::Int:
4534     case BuiltinType::UInt:
4535       return true;
4536     default:
4537       break;
4538     }
4539 
4540   return false;
4541 }
4542 
4543 /// isAlignedParamType - Determine whether a type requires 16-byte or
4544 /// higher alignment in the parameter area.  Always returns at least 8.
4545 CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const {
4546   // Complex types are passed just like their elements.
4547   if (const ComplexType *CTy = Ty->getAs<ComplexType>())
4548     Ty = CTy->getElementType();
4549 
4550   // Only vector types of size 16 bytes need alignment (larger types are
4551   // passed via reference, smaller types are not aligned).
4552   if (IsQPXVectorTy(Ty)) {
4553     if (getContext().getTypeSize(Ty) > 128)
4554       return CharUnits::fromQuantity(32);
4555 
4556     return CharUnits::fromQuantity(16);
4557   } else if (Ty->isVectorType()) {
4558     return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8);
4559   }
4560 
4561   // For single-element float/vector structs, we consider the whole type
4562   // to have the same alignment requirements as its single element.
4563   const Type *AlignAsType = nullptr;
4564   const Type *EltType = isSingleElementStruct(Ty, getContext());
4565   if (EltType) {
4566     const BuiltinType *BT = EltType->getAs<BuiltinType>();
4567     if (IsQPXVectorTy(EltType) || (EltType->isVectorType() &&
4568          getContext().getTypeSize(EltType) == 128) ||
4569         (BT && BT->isFloatingPoint()))
4570       AlignAsType = EltType;
4571   }
4572 
4573   // Likewise for ELFv2 homogeneous aggregates.
4574   const Type *Base = nullptr;
4575   uint64_t Members = 0;
4576   if (!AlignAsType && Kind == ELFv2 &&
4577       isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members))
4578     AlignAsType = Base;
4579 
4580   // With special case aggregates, only vector base types need alignment.
4581   if (AlignAsType && IsQPXVectorTy(AlignAsType)) {
4582     if (getContext().getTypeSize(AlignAsType) > 128)
4583       return CharUnits::fromQuantity(32);
4584 
4585     return CharUnits::fromQuantity(16);
4586   } else if (AlignAsType) {
4587     return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8);
4588   }
4589 
4590   // Otherwise, we only need alignment for any aggregate type that
4591   // has an alignment requirement of >= 16 bytes.
4592   if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) {
4593     if (HasQPX && getContext().getTypeAlign(Ty) >= 256)
4594       return CharUnits::fromQuantity(32);
4595     return CharUnits::fromQuantity(16);
4596   }
4597 
4598   return CharUnits::fromQuantity(8);
4599 }
4600 
4601 /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous
4602 /// aggregate.  Base is set to the base element type, and Members is set
4603 /// to the number of base elements.
4604 bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
4605                                      uint64_t &Members) const {
4606   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
4607     uint64_t NElements = AT->getSize().getZExtValue();
4608     if (NElements == 0)
4609       return false;
4610     if (!isHomogeneousAggregate(AT->getElementType(), Base, Members))
4611       return false;
4612     Members *= NElements;
4613   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
4614     const RecordDecl *RD = RT->getDecl();
4615     if (RD->hasFlexibleArrayMember())
4616       return false;
4617 
4618     Members = 0;
4619 
4620     // If this is a C++ record, check the bases first.
4621     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
4622       for (const auto &I : CXXRD->bases()) {
4623         // Ignore empty records.
4624         if (isEmptyRecord(getContext(), I.getType(), true))
4625           continue;
4626 
4627         uint64_t FldMembers;
4628         if (!isHomogeneousAggregate(I.getType(), Base, FldMembers))
4629           return false;
4630 
4631         Members += FldMembers;
4632       }
4633     }
4634 
4635     for (const auto *FD : RD->fields()) {
4636       // Ignore (non-zero arrays of) empty records.
4637       QualType FT = FD->getType();
4638       while (const ConstantArrayType *AT =
4639              getContext().getAsConstantArrayType(FT)) {
4640         if (AT->getSize().getZExtValue() == 0)
4641           return false;
4642         FT = AT->getElementType();
4643       }
4644       if (isEmptyRecord(getContext(), FT, true))
4645         continue;
4646 
4647       // For compatibility with GCC, ignore empty bitfields in C++ mode.
4648       if (getContext().getLangOpts().CPlusPlus &&
4649           FD->isZeroLengthBitField(getContext()))
4650         continue;
4651 
4652       uint64_t FldMembers;
4653       if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers))
4654         return false;
4655 
4656       Members = (RD->isUnion() ?
4657                  std::max(Members, FldMembers) : Members + FldMembers);
4658     }
4659 
4660     if (!Base)
4661       return false;
4662 
4663     // Ensure there is no padding.
4664     if (getContext().getTypeSize(Base) * Members !=
4665         getContext().getTypeSize(Ty))
4666       return false;
4667   } else {
4668     Members = 1;
4669     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
4670       Members = 2;
4671       Ty = CT->getElementType();
4672     }
4673 
4674     // Most ABIs only support float, double, and some vector type widths.
4675     if (!isHomogeneousAggregateBaseType(Ty))
4676       return false;
4677 
4678     // The base type must be the same for all members.  Types that
4679     // agree in both total size and mode (float vs. vector) are
4680     // treated as being equivalent here.
4681     const Type *TyPtr = Ty.getTypePtr();
4682     if (!Base) {
4683       Base = TyPtr;
4684       // If it's a non-power-of-2 vector, its size is already a power-of-2,
4685       // so make sure to widen it explicitly.
4686       if (const VectorType *VT = Base->getAs<VectorType>()) {
4687         QualType EltTy = VT->getElementType();
4688         unsigned NumElements =
4689             getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy);
4690         Base = getContext()
4691                    .getVectorType(EltTy, NumElements, VT->getVectorKind())
4692                    .getTypePtr();
4693       }
4694     }
4695 
4696     if (Base->isVectorType() != TyPtr->isVectorType() ||
4697         getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr))
4698       return false;
4699   }
4700   return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members);
4701 }
4702 
4703 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
4704   // Homogeneous aggregates for ELFv2 must have base types of float,
4705   // double, long double, or 128-bit vectors.
4706   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
4707     if (BT->getKind() == BuiltinType::Float ||
4708         BT->getKind() == BuiltinType::Double ||
4709         BT->getKind() == BuiltinType::LongDouble ||
4710         (getContext().getTargetInfo().hasFloat128Type() &&
4711           (BT->getKind() == BuiltinType::Float128))) {
4712       if (IsSoftFloatABI)
4713         return false;
4714       return true;
4715     }
4716   }
4717   if (const VectorType *VT = Ty->getAs<VectorType>()) {
4718     if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty))
4719       return true;
4720   }
4721   return false;
4722 }
4723 
4724 bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough(
4725     const Type *Base, uint64_t Members) const {
4726   // Vector and fp128 types require one register, other floating point types
4727   // require one or two registers depending on their size.
4728   uint32_t NumRegs =
4729       ((getContext().getTargetInfo().hasFloat128Type() &&
4730           Base->isFloat128Type()) ||
4731         Base->isVectorType()) ? 1
4732                               : (getContext().getTypeSize(Base) + 63) / 64;
4733 
4734   // Homogeneous Aggregates may occupy at most 8 registers.
4735   return Members * NumRegs <= 8;
4736 }
4737 
4738 ABIArgInfo
4739 PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const {
4740   Ty = useFirstFieldIfTransparentUnion(Ty);
4741 
4742   if (Ty->isAnyComplexType())
4743     return ABIArgInfo::getDirect();
4744 
4745   // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes)
4746   // or via reference (larger than 16 bytes).
4747   if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) {
4748     uint64_t Size = getContext().getTypeSize(Ty);
4749     if (Size > 128)
4750       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
4751     else if (Size < 128) {
4752       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4753       return ABIArgInfo::getDirect(CoerceTy);
4754     }
4755   }
4756 
4757   if (isAggregateTypeForABI(Ty)) {
4758     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
4759       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
4760 
4761     uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity();
4762     uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
4763 
4764     // ELFv2 homogeneous aggregates are passed as array types.
4765     const Type *Base = nullptr;
4766     uint64_t Members = 0;
4767     if (Kind == ELFv2 &&
4768         isHomogeneousAggregate(Ty, Base, Members)) {
4769       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4770       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4771       return ABIArgInfo::getDirect(CoerceTy);
4772     }
4773 
4774     // If an aggregate may end up fully in registers, we do not
4775     // use the ByVal method, but pass the aggregate as array.
4776     // This is usually beneficial since we avoid forcing the
4777     // back-end to store the argument to memory.
4778     uint64_t Bits = getContext().getTypeSize(Ty);
4779     if (Bits > 0 && Bits <= 8 * GPRBits) {
4780       llvm::Type *CoerceTy;
4781 
4782       // Types up to 8 bytes are passed as integer type (which will be
4783       // properly aligned in the argument save area doubleword).
4784       if (Bits <= GPRBits)
4785         CoerceTy =
4786             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
4787       // Larger types are passed as arrays, with the base type selected
4788       // according to the required alignment in the save area.
4789       else {
4790         uint64_t RegBits = ABIAlign * 8;
4791         uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits;
4792         llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits);
4793         CoerceTy = llvm::ArrayType::get(RegTy, NumRegs);
4794       }
4795 
4796       return ABIArgInfo::getDirect(CoerceTy);
4797     }
4798 
4799     // All other aggregates are passed ByVal.
4800     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
4801                                    /*ByVal=*/true,
4802                                    /*Realign=*/TyAlign > ABIAlign);
4803   }
4804 
4805   return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty)
4806                                      : ABIArgInfo::getDirect());
4807 }
4808 
4809 ABIArgInfo
4810 PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const {
4811   if (RetTy->isVoidType())
4812     return ABIArgInfo::getIgnore();
4813 
4814   if (RetTy->isAnyComplexType())
4815     return ABIArgInfo::getDirect();
4816 
4817   // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes)
4818   // or via reference (larger than 16 bytes).
4819   if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) {
4820     uint64_t Size = getContext().getTypeSize(RetTy);
4821     if (Size > 128)
4822       return getNaturalAlignIndirect(RetTy);
4823     else if (Size < 128) {
4824       llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size);
4825       return ABIArgInfo::getDirect(CoerceTy);
4826     }
4827   }
4828 
4829   if (isAggregateTypeForABI(RetTy)) {
4830     // ELFv2 homogeneous aggregates are returned as array types.
4831     const Type *Base = nullptr;
4832     uint64_t Members = 0;
4833     if (Kind == ELFv2 &&
4834         isHomogeneousAggregate(RetTy, Base, Members)) {
4835       llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0));
4836       llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members);
4837       return ABIArgInfo::getDirect(CoerceTy);
4838     }
4839 
4840     // ELFv2 small aggregates are returned in up to two registers.
4841     uint64_t Bits = getContext().getTypeSize(RetTy);
4842     if (Kind == ELFv2 && Bits <= 2 * GPRBits) {
4843       if (Bits == 0)
4844         return ABIArgInfo::getIgnore();
4845 
4846       llvm::Type *CoerceTy;
4847       if (Bits > GPRBits) {
4848         CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits);
4849         CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy);
4850       } else
4851         CoerceTy =
4852             llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8));
4853       return ABIArgInfo::getDirect(CoerceTy);
4854     }
4855 
4856     // All other aggregates are returned indirectly.
4857     return getNaturalAlignIndirect(RetTy);
4858   }
4859 
4860   return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy)
4861                                         : ABIArgInfo::getDirect());
4862 }
4863 
4864 // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine.
4865 Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
4866                                       QualType Ty) const {
4867   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
4868   TypeInfo.second = getParamTypeAlignment(Ty);
4869 
4870   CharUnits SlotSize = CharUnits::fromQuantity(8);
4871 
4872   // If we have a complex type and the base type is smaller than 8 bytes,
4873   // the ABI calls for the real and imaginary parts to be right-adjusted
4874   // in separate doublewords.  However, Clang expects us to produce a
4875   // pointer to a structure with the two parts packed tightly.  So generate
4876   // loads of the real and imaginary parts relative to the va_list pointer,
4877   // and store them to a temporary structure.
4878   if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
4879     CharUnits EltSize = TypeInfo.first / 2;
4880     if (EltSize < SlotSize) {
4881       Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty,
4882                                             SlotSize * 2, SlotSize,
4883                                             SlotSize, /*AllowHigher*/ true);
4884 
4885       Address RealAddr = Addr;
4886       Address ImagAddr = RealAddr;
4887       if (CGF.CGM.getDataLayout().isBigEndian()) {
4888         RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr,
4889                                                           SlotSize - EltSize);
4890         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr,
4891                                                       2 * SlotSize - EltSize);
4892       } else {
4893         ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize);
4894       }
4895 
4896       llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType());
4897       RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy);
4898       ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy);
4899       llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal");
4900       llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag");
4901 
4902       Address Temp = CGF.CreateMemTemp(Ty, "vacplx");
4903       CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty),
4904                              /*init*/ true);
4905       return Temp;
4906     }
4907   }
4908 
4909   // Otherwise, just use the general rule.
4910   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false,
4911                           TypeInfo, SlotSize, /*AllowHigher*/ true);
4912 }
4913 
4914 static bool
4915 PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4916                               llvm::Value *Address) {
4917   // This is calculated from the LLVM and GCC tables and verified
4918   // against gcc output.  AFAIK all ABIs use the same encoding.
4919 
4920   CodeGen::CGBuilderTy &Builder = CGF.Builder;
4921 
4922   llvm::IntegerType *i8 = CGF.Int8Ty;
4923   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
4924   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
4925   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
4926 
4927   // 0-31: r0-31, the 8-byte general-purpose registers
4928   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
4929 
4930   // 32-63: fp0-31, the 8-byte floating-point registers
4931   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
4932 
4933   // 64-67 are various 8-byte special-purpose registers:
4934   // 64: mq
4935   // 65: lr
4936   // 66: ctr
4937   // 67: ap
4938   AssignToArrayRange(Builder, Address, Eight8, 64, 67);
4939 
4940   // 68-76 are various 4-byte special-purpose registers:
4941   // 68-75 cr0-7
4942   // 76: xer
4943   AssignToArrayRange(Builder, Address, Four8, 68, 76);
4944 
4945   // 77-108: v0-31, the 16-byte vector registers
4946   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
4947 
4948   // 109: vrsave
4949   // 110: vscr
4950   // 111: spe_acc
4951   // 112: spefscr
4952   // 113: sfp
4953   // 114: tfhar
4954   // 115: tfiar
4955   // 116: texasr
4956   AssignToArrayRange(Builder, Address, Eight8, 109, 116);
4957 
4958   return false;
4959 }
4960 
4961 bool
4962 PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable(
4963   CodeGen::CodeGenFunction &CGF,
4964   llvm::Value *Address) const {
4965 
4966   return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4967 }
4968 
4969 bool
4970 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4971                                                 llvm::Value *Address) const {
4972 
4973   return PPC64_initDwarfEHRegSizeTable(CGF, Address);
4974 }
4975 
4976 //===----------------------------------------------------------------------===//
4977 // AArch64 ABI Implementation
4978 //===----------------------------------------------------------------------===//
4979 
4980 namespace {
4981 
4982 class AArch64ABIInfo : public SwiftABIInfo {
4983 public:
4984   enum ABIKind {
4985     AAPCS = 0,
4986     DarwinPCS,
4987     Win64
4988   };
4989 
4990 private:
4991   ABIKind Kind;
4992 
4993 public:
4994   AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind)
4995     : SwiftABIInfo(CGT), Kind(Kind) {}
4996 
4997 private:
4998   ABIKind getABIKind() const { return Kind; }
4999   bool isDarwinPCS() const { return Kind == DarwinPCS; }
5000 
5001   ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const;
5002   ABIArgInfo classifyArgumentType(QualType RetTy) const;
5003   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5004   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5005                                          uint64_t Members) const override;
5006 
5007   bool isIllegalVectorType(QualType Ty) const;
5008 
5009   void computeInfo(CGFunctionInfo &FI) const override {
5010     if (!::classifyReturnType(getCXXABI(), FI, *this))
5011       FI.getReturnInfo() =
5012           classifyReturnType(FI.getReturnType(), FI.isVariadic());
5013 
5014     for (auto &it : FI.arguments())
5015       it.info = classifyArgumentType(it.type);
5016   }
5017 
5018   Address EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5019                           CodeGenFunction &CGF) const;
5020 
5021   Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty,
5022                          CodeGenFunction &CGF) const;
5023 
5024   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5025                     QualType Ty) const override {
5026     return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty)
5027                          : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF)
5028                                          : EmitAAPCSVAArg(VAListAddr, Ty, CGF);
5029   }
5030 
5031   Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5032                       QualType Ty) const override;
5033 
5034   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
5035                                     bool asReturnValue) const override {
5036     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5037   }
5038   bool isSwiftErrorInRegister() const override {
5039     return true;
5040   }
5041 
5042   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5043                                  unsigned elts) const override;
5044 };
5045 
5046 class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
5047 public:
5048   AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind)
5049       : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {}
5050 
5051   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5052     return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue";
5053   }
5054 
5055   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5056     return 31;
5057   }
5058 
5059   bool doesReturnSlotInterfereWithArgs() const override { return false; }
5060 
5061   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5062                            CodeGen::CodeGenModule &CGM) const override {
5063     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5064     if (!FD)
5065       return;
5066 
5067     CodeGenOptions::SignReturnAddressScope Scope = CGM.getCodeGenOpts().getSignReturnAddress();
5068     CodeGenOptions::SignReturnAddressKeyValue Key = CGM.getCodeGenOpts().getSignReturnAddressKey();
5069     bool BranchTargetEnforcement = CGM.getCodeGenOpts().BranchTargetEnforcement;
5070     if (const auto *TA = FD->getAttr<TargetAttr>()) {
5071       ParsedTargetAttr Attr = TA->parse();
5072       if (!Attr.BranchProtection.empty()) {
5073         TargetInfo::BranchProtectionInfo BPI;
5074         StringRef Error;
5075         (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
5076                                                        BPI, Error);
5077         assert(Error.empty());
5078         Scope = BPI.SignReturnAddr;
5079         Key = BPI.SignKey;
5080         BranchTargetEnforcement = BPI.BranchTargetEnforcement;
5081       }
5082     }
5083 
5084     auto *Fn = cast<llvm::Function>(GV);
5085     if (Scope != CodeGenOptions::SignReturnAddressScope::None) {
5086       Fn->addFnAttr("sign-return-address",
5087                     Scope == CodeGenOptions::SignReturnAddressScope::All
5088                         ? "all"
5089                         : "non-leaf");
5090 
5091       Fn->addFnAttr("sign-return-address-key",
5092                     Key == CodeGenOptions::SignReturnAddressKeyValue::AKey
5093                         ? "a_key"
5094                         : "b_key");
5095     }
5096 
5097     if (BranchTargetEnforcement)
5098       Fn->addFnAttr("branch-target-enforcement");
5099   }
5100 };
5101 
5102 class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo {
5103 public:
5104   WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K)
5105       : AArch64TargetCodeGenInfo(CGT, K) {}
5106 
5107   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5108                            CodeGen::CodeGenModule &CGM) const override;
5109 
5110   void getDependentLibraryOption(llvm::StringRef Lib,
5111                                  llvm::SmallString<24> &Opt) const override {
5112     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5113   }
5114 
5115   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5116                                llvm::SmallString<32> &Opt) const override {
5117     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5118   }
5119 };
5120 
5121 void WindowsAArch64TargetCodeGenInfo::setTargetAttributes(
5122     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5123   AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5124   if (GV->isDeclaration())
5125     return;
5126   addStackProbeTargetAttributes(D, GV, CGM);
5127 }
5128 }
5129 
5130 ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const {
5131   Ty = useFirstFieldIfTransparentUnion(Ty);
5132 
5133   // Handle illegal vector types here.
5134   if (isIllegalVectorType(Ty)) {
5135     uint64_t Size = getContext().getTypeSize(Ty);
5136     // Android promotes <2 x i8> to i16, not i32
5137     if (isAndroid() && (Size <= 16)) {
5138       llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext());
5139       return ABIArgInfo::getDirect(ResType);
5140     }
5141     if (Size <= 32) {
5142       llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext());
5143       return ABIArgInfo::getDirect(ResType);
5144     }
5145     if (Size == 64) {
5146       llvm::Type *ResType =
5147           llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2);
5148       return ABIArgInfo::getDirect(ResType);
5149     }
5150     if (Size == 128) {
5151       llvm::Type *ResType =
5152           llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4);
5153       return ABIArgInfo::getDirect(ResType);
5154     }
5155     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5156   }
5157 
5158   if (!isAggregateTypeForABI(Ty)) {
5159     // Treat an enum type as its underlying type.
5160     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
5161       Ty = EnumTy->getDecl()->getIntegerType();
5162 
5163     return (Ty->isPromotableIntegerType() && isDarwinPCS()
5164                 ? ABIArgInfo::getExtend(Ty)
5165                 : ABIArgInfo::getDirect());
5166   }
5167 
5168   // Structures with either a non-trivial destructor or a non-trivial
5169   // copy constructor are always indirect.
5170   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5171     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
5172                                      CGCXXABI::RAA_DirectInMemory);
5173   }
5174 
5175   // Empty records are always ignored on Darwin, but actually passed in C++ mode
5176   // elsewhere for GNU compatibility.
5177   uint64_t Size = getContext().getTypeSize(Ty);
5178   bool IsEmpty = isEmptyRecord(getContext(), Ty, true);
5179   if (IsEmpty || Size == 0) {
5180     if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS())
5181       return ABIArgInfo::getIgnore();
5182 
5183     // GNU C mode. The only argument that gets ignored is an empty one with size
5184     // 0.
5185     if (IsEmpty && Size == 0)
5186       return ABIArgInfo::getIgnore();
5187     return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
5188   }
5189 
5190   // Homogeneous Floating-point Aggregates (HFAs) need to be expanded.
5191   const Type *Base = nullptr;
5192   uint64_t Members = 0;
5193   if (isHomogeneousAggregate(Ty, Base, Members)) {
5194     return ABIArgInfo::getDirect(
5195         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members));
5196   }
5197 
5198   // Aggregates <= 16 bytes are passed directly in registers or on the stack.
5199   if (Size <= 128) {
5200     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5201     // same size and alignment.
5202     if (getTarget().isRenderScriptTarget()) {
5203       return coerceToIntArray(Ty, getContext(), getVMContext());
5204     }
5205     unsigned Alignment;
5206     if (Kind == AArch64ABIInfo::AAPCS) {
5207       Alignment = getContext().getTypeUnadjustedAlign(Ty);
5208       Alignment = Alignment < 128 ? 64 : 128;
5209     } else {
5210       Alignment = std::max(getContext().getTypeAlign(Ty),
5211                            (unsigned)getTarget().getPointerWidth(0));
5212     }
5213     Size = llvm::alignTo(Size, Alignment);
5214 
5215     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5216     // For aggregates with 16-byte alignment, we use i128.
5217     llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment);
5218     return ABIArgInfo::getDirect(
5219         Size == Alignment ? BaseTy
5220                           : llvm::ArrayType::get(BaseTy, Size / Alignment));
5221   }
5222 
5223   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5224 }
5225 
5226 ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy,
5227                                               bool IsVariadic) const {
5228   if (RetTy->isVoidType())
5229     return ABIArgInfo::getIgnore();
5230 
5231   // Large vector types should be returned via memory.
5232   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
5233     return getNaturalAlignIndirect(RetTy);
5234 
5235   if (!isAggregateTypeForABI(RetTy)) {
5236     // Treat an enum type as its underlying type.
5237     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
5238       RetTy = EnumTy->getDecl()->getIntegerType();
5239 
5240     return (RetTy->isPromotableIntegerType() && isDarwinPCS()
5241                 ? ABIArgInfo::getExtend(RetTy)
5242                 : ABIArgInfo::getDirect());
5243   }
5244 
5245   uint64_t Size = getContext().getTypeSize(RetTy);
5246   if (isEmptyRecord(getContext(), RetTy, true) || Size == 0)
5247     return ABIArgInfo::getIgnore();
5248 
5249   const Type *Base = nullptr;
5250   uint64_t Members = 0;
5251   if (isHomogeneousAggregate(RetTy, Base, Members) &&
5252       !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 &&
5253         IsVariadic))
5254     // Homogeneous Floating-point Aggregates (HFAs) are returned directly.
5255     return ABIArgInfo::getDirect();
5256 
5257   // Aggregates <= 16 bytes are returned directly in registers or on the stack.
5258   if (Size <= 128) {
5259     // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of
5260     // same size and alignment.
5261     if (getTarget().isRenderScriptTarget()) {
5262       return coerceToIntArray(RetTy, getContext(), getVMContext());
5263     }
5264     unsigned Alignment = getContext().getTypeAlign(RetTy);
5265     Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes
5266 
5267     // We use a pair of i64 for 16-byte aggregate with 8-byte alignment.
5268     // For aggregates with 16-byte alignment, we use i128.
5269     if (Alignment < 128 && Size == 128) {
5270       llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext());
5271       return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64));
5272     }
5273     return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size));
5274   }
5275 
5276   return getNaturalAlignIndirect(RetTy);
5277 }
5278 
5279 /// isIllegalVectorType - check whether the vector type is legal for AArch64.
5280 bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const {
5281   if (const VectorType *VT = Ty->getAs<VectorType>()) {
5282     // Check whether VT is legal.
5283     unsigned NumElements = VT->getNumElements();
5284     uint64_t Size = getContext().getTypeSize(VT);
5285     // NumElements should be power of 2.
5286     if (!llvm::isPowerOf2_32(NumElements))
5287       return true;
5288 
5289     // arm64_32 has to be compatible with the ARM logic here, which allows huge
5290     // vectors for some reason.
5291     llvm::Triple Triple = getTarget().getTriple();
5292     if (Triple.getArch() == llvm::Triple::aarch64_32 &&
5293         Triple.isOSBinFormatMachO())
5294       return Size <= 32;
5295 
5296     return Size != 64 && (Size != 128 || NumElements == 1);
5297   }
5298   return false;
5299 }
5300 
5301 bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize,
5302                                                llvm::Type *eltTy,
5303                                                unsigned elts) const {
5304   if (!llvm::isPowerOf2_32(elts))
5305     return false;
5306   if (totalSize.getQuantity() != 8 &&
5307       (totalSize.getQuantity() != 16 || elts == 1))
5308     return false;
5309   return true;
5310 }
5311 
5312 bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
5313   // Homogeneous aggregates for AAPCS64 must have base types of a floating
5314   // point type or a short-vector type. This is the same as the 32-bit ABI,
5315   // but with the difference that any floating-point type is allowed,
5316   // including __fp16.
5317   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
5318     if (BT->isFloatingPoint())
5319       return true;
5320   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
5321     unsigned VecSize = getContext().getTypeSize(VT);
5322     if (VecSize == 64 || VecSize == 128)
5323       return true;
5324   }
5325   return false;
5326 }
5327 
5328 bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
5329                                                        uint64_t Members) const {
5330   return Members <= 4;
5331 }
5332 
5333 Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr,
5334                                             QualType Ty,
5335                                             CodeGenFunction &CGF) const {
5336   ABIArgInfo AI = classifyArgumentType(Ty);
5337   bool IsIndirect = AI.isIndirect();
5338 
5339   llvm::Type *BaseTy = CGF.ConvertType(Ty);
5340   if (IsIndirect)
5341     BaseTy = llvm::PointerType::getUnqual(BaseTy);
5342   else if (AI.getCoerceToType())
5343     BaseTy = AI.getCoerceToType();
5344 
5345   unsigned NumRegs = 1;
5346   if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) {
5347     BaseTy = ArrTy->getElementType();
5348     NumRegs = ArrTy->getNumElements();
5349   }
5350   bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy();
5351 
5352   // The AArch64 va_list type and handling is specified in the Procedure Call
5353   // Standard, section B.4:
5354   //
5355   // struct {
5356   //   void *__stack;
5357   //   void *__gr_top;
5358   //   void *__vr_top;
5359   //   int __gr_offs;
5360   //   int __vr_offs;
5361   // };
5362 
5363   llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg");
5364   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
5365   llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack");
5366   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
5367 
5368   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
5369   CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty);
5370 
5371   Address reg_offs_p = Address::invalid();
5372   llvm::Value *reg_offs = nullptr;
5373   int reg_top_index;
5374   int RegSize = IsIndirect ? 8 : TySize.getQuantity();
5375   if (!IsFPR) {
5376     // 3 is the field number of __gr_offs
5377     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p");
5378     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs");
5379     reg_top_index = 1; // field number for __gr_top
5380     RegSize = llvm::alignTo(RegSize, 8);
5381   } else {
5382     // 4 is the field number of __vr_offs.
5383     reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p");
5384     reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs");
5385     reg_top_index = 2; // field number for __vr_top
5386     RegSize = 16 * NumRegs;
5387   }
5388 
5389   //=======================================
5390   // Find out where argument was passed
5391   //=======================================
5392 
5393   // If reg_offs >= 0 we're already using the stack for this type of
5394   // argument. We don't want to keep updating reg_offs (in case it overflows,
5395   // though anyone passing 2GB of arguments, each at most 16 bytes, deserves
5396   // whatever they get).
5397   llvm::Value *UsingStack = nullptr;
5398   UsingStack = CGF.Builder.CreateICmpSGE(
5399       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0));
5400 
5401   CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock);
5402 
5403   // Otherwise, at least some kind of argument could go in these registers, the
5404   // question is whether this particular type is too big.
5405   CGF.EmitBlock(MaybeRegBlock);
5406 
5407   // Integer arguments may need to correct register alignment (for example a
5408   // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we
5409   // align __gr_offs to calculate the potential address.
5410   if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) {
5411     int Align = TyAlign.getQuantity();
5412 
5413     reg_offs = CGF.Builder.CreateAdd(
5414         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1),
5415         "align_regoffs");
5416     reg_offs = CGF.Builder.CreateAnd(
5417         reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align),
5418         "aligned_regoffs");
5419   }
5420 
5421   // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list.
5422   // The fact that this is done unconditionally reflects the fact that
5423   // allocating an argument to the stack also uses up all the remaining
5424   // registers of the appropriate kind.
5425   llvm::Value *NewOffset = nullptr;
5426   NewOffset = CGF.Builder.CreateAdd(
5427       reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs");
5428   CGF.Builder.CreateStore(NewOffset, reg_offs_p);
5429 
5430   // Now we're in a position to decide whether this argument really was in
5431   // registers or not.
5432   llvm::Value *InRegs = nullptr;
5433   InRegs = CGF.Builder.CreateICmpSLE(
5434       NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg");
5435 
5436   CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock);
5437 
5438   //=======================================
5439   // Argument was in registers
5440   //=======================================
5441 
5442   // Now we emit the code for if the argument was originally passed in
5443   // registers. First start the appropriate block:
5444   CGF.EmitBlock(InRegBlock);
5445 
5446   llvm::Value *reg_top = nullptr;
5447   Address reg_top_p =
5448       CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p");
5449   reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top");
5450   Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs),
5451                    CharUnits::fromQuantity(IsFPR ? 16 : 8));
5452   Address RegAddr = Address::invalid();
5453   llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty);
5454 
5455   if (IsIndirect) {
5456     // If it's been passed indirectly (actually a struct), whatever we find from
5457     // stored registers or on the stack will actually be a struct **.
5458     MemTy = llvm::PointerType::getUnqual(MemTy);
5459   }
5460 
5461   const Type *Base = nullptr;
5462   uint64_t NumMembers = 0;
5463   bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers);
5464   if (IsHFA && NumMembers > 1) {
5465     // Homogeneous aggregates passed in registers will have their elements split
5466     // and stored 16-bytes apart regardless of size (they're notionally in qN,
5467     // qN+1, ...). We reload and store into a temporary local variable
5468     // contiguously.
5469     assert(!IsIndirect && "Homogeneous aggregates should be passed directly");
5470     auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0));
5471     llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0));
5472     llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers);
5473     Address Tmp = CGF.CreateTempAlloca(HFATy,
5474                                        std::max(TyAlign, BaseTyInfo.second));
5475 
5476     // On big-endian platforms, the value will be right-aligned in its slot.
5477     int Offset = 0;
5478     if (CGF.CGM.getDataLayout().isBigEndian() &&
5479         BaseTyInfo.first.getQuantity() < 16)
5480       Offset = 16 - BaseTyInfo.first.getQuantity();
5481 
5482     for (unsigned i = 0; i < NumMembers; ++i) {
5483       CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset);
5484       Address LoadAddr =
5485         CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset);
5486       LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy);
5487 
5488       Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i);
5489 
5490       llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr);
5491       CGF.Builder.CreateStore(Elem, StoreAddr);
5492     }
5493 
5494     RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy);
5495   } else {
5496     // Otherwise the object is contiguous in memory.
5497 
5498     // It might be right-aligned in its slot.
5499     CharUnits SlotSize = BaseAddr.getAlignment();
5500     if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect &&
5501         (IsHFA || !isAggregateTypeForABI(Ty)) &&
5502         TySize < SlotSize) {
5503       CharUnits Offset = SlotSize - TySize;
5504       BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset);
5505     }
5506 
5507     RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy);
5508   }
5509 
5510   CGF.EmitBranch(ContBlock);
5511 
5512   //=======================================
5513   // Argument was on the stack
5514   //=======================================
5515   CGF.EmitBlock(OnStackBlock);
5516 
5517   Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p");
5518   llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack");
5519 
5520   // Again, stack arguments may need realignment. In this case both integer and
5521   // floating-point ones might be affected.
5522   if (!IsIndirect && TyAlign.getQuantity() > 8) {
5523     int Align = TyAlign.getQuantity();
5524 
5525     OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty);
5526 
5527     OnStackPtr = CGF.Builder.CreateAdd(
5528         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1),
5529         "align_stack");
5530     OnStackPtr = CGF.Builder.CreateAnd(
5531         OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align),
5532         "align_stack");
5533 
5534     OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy);
5535   }
5536   Address OnStackAddr(OnStackPtr,
5537                       std::max(CharUnits::fromQuantity(8), TyAlign));
5538 
5539   // All stack slots are multiples of 8 bytes.
5540   CharUnits StackSlotSize = CharUnits::fromQuantity(8);
5541   CharUnits StackSize;
5542   if (IsIndirect)
5543     StackSize = StackSlotSize;
5544   else
5545     StackSize = TySize.alignTo(StackSlotSize);
5546 
5547   llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize);
5548   llvm::Value *NewStack =
5549       CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack");
5550 
5551   // Write the new value of __stack for the next call to va_arg
5552   CGF.Builder.CreateStore(NewStack, stack_p);
5553 
5554   if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) &&
5555       TySize < StackSlotSize) {
5556     CharUnits Offset = StackSlotSize - TySize;
5557     OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset);
5558   }
5559 
5560   OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy);
5561 
5562   CGF.EmitBranch(ContBlock);
5563 
5564   //=======================================
5565   // Tidy up
5566   //=======================================
5567   CGF.EmitBlock(ContBlock);
5568 
5569   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
5570                                  OnStackAddr, OnStackBlock, "vaargs.addr");
5571 
5572   if (IsIndirect)
5573     return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"),
5574                    TyAlign);
5575 
5576   return ResAddr;
5577 }
5578 
5579 Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty,
5580                                         CodeGenFunction &CGF) const {
5581   // The backend's lowering doesn't support va_arg for aggregates or
5582   // illegal vector types.  Lower VAArg here for these cases and use
5583   // the LLVM va_arg instruction for everything else.
5584   if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty))
5585     return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect());
5586 
5587   uint64_t PointerSize = getTarget().getPointerWidth(0) / 8;
5588   CharUnits SlotSize = CharUnits::fromQuantity(PointerSize);
5589 
5590   // Empty records are ignored for parameter passing purposes.
5591   if (isEmptyRecord(getContext(), Ty, true)) {
5592     Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
5593     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
5594     return Addr;
5595   }
5596 
5597   // The size of the actual thing passed, which might end up just
5598   // being a pointer for indirect types.
5599   auto TyInfo = getContext().getTypeInfoInChars(Ty);
5600 
5601   // Arguments bigger than 16 bytes which aren't homogeneous
5602   // aggregates should be passed indirectly.
5603   bool IsIndirect = false;
5604   if (TyInfo.first.getQuantity() > 16) {
5605     const Type *Base = nullptr;
5606     uint64_t Members = 0;
5607     IsIndirect = !isHomogeneousAggregate(Ty, Base, Members);
5608   }
5609 
5610   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
5611                           TyInfo, SlotSize, /*AllowHigherAlign*/ true);
5612 }
5613 
5614 Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr,
5615                                     QualType Ty) const {
5616   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
5617                           CGF.getContext().getTypeInfoInChars(Ty),
5618                           CharUnits::fromQuantity(8),
5619                           /*allowHigherAlign*/ false);
5620 }
5621 
5622 //===----------------------------------------------------------------------===//
5623 // ARM ABI Implementation
5624 //===----------------------------------------------------------------------===//
5625 
5626 namespace {
5627 
5628 class ARMABIInfo : public SwiftABIInfo {
5629 public:
5630   enum ABIKind {
5631     APCS = 0,
5632     AAPCS = 1,
5633     AAPCS_VFP = 2,
5634     AAPCS16_VFP = 3,
5635   };
5636 
5637 private:
5638   ABIKind Kind;
5639 
5640 public:
5641   ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind)
5642       : SwiftABIInfo(CGT), Kind(_Kind) {
5643     setCCs();
5644   }
5645 
5646   bool isEABI() const {
5647     switch (getTarget().getTriple().getEnvironment()) {
5648     case llvm::Triple::Android:
5649     case llvm::Triple::EABI:
5650     case llvm::Triple::EABIHF:
5651     case llvm::Triple::GNUEABI:
5652     case llvm::Triple::GNUEABIHF:
5653     case llvm::Triple::MuslEABI:
5654     case llvm::Triple::MuslEABIHF:
5655       return true;
5656     default:
5657       return false;
5658     }
5659   }
5660 
5661   bool isEABIHF() const {
5662     switch (getTarget().getTriple().getEnvironment()) {
5663     case llvm::Triple::EABIHF:
5664     case llvm::Triple::GNUEABIHF:
5665     case llvm::Triple::MuslEABIHF:
5666       return true;
5667     default:
5668       return false;
5669     }
5670   }
5671 
5672   ABIKind getABIKind() const { return Kind; }
5673 
5674 private:
5675   ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic,
5676                                 unsigned functionCallConv) const;
5677   ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic,
5678                                   unsigned functionCallConv) const;
5679   ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base,
5680                                           uint64_t Members) const;
5681   ABIArgInfo coerceIllegalVector(QualType Ty) const;
5682   bool isIllegalVectorType(QualType Ty) const;
5683   bool containsAnyFP16Vectors(QualType Ty) const;
5684 
5685   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
5686   bool isHomogeneousAggregateSmallEnough(const Type *Ty,
5687                                          uint64_t Members) const override;
5688 
5689   bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const;
5690 
5691   void computeInfo(CGFunctionInfo &FI) const override;
5692 
5693   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
5694                     QualType Ty) const override;
5695 
5696   llvm::CallingConv::ID getLLVMDefaultCC() const;
5697   llvm::CallingConv::ID getABIDefaultCC() const;
5698   void setCCs();
5699 
5700   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
5701                                     bool asReturnValue) const override {
5702     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
5703   }
5704   bool isSwiftErrorInRegister() const override {
5705     return true;
5706   }
5707   bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy,
5708                                  unsigned elts) const override;
5709 };
5710 
5711 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
5712 public:
5713   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5714     :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
5715 
5716   const ARMABIInfo &getABIInfo() const {
5717     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
5718   }
5719 
5720   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
5721     return 13;
5722   }
5723 
5724   StringRef getARCRetainAutoreleasedReturnValueMarker() const override {
5725     return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue";
5726   }
5727 
5728   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5729                                llvm::Value *Address) const override {
5730     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
5731 
5732     // 0-15 are the 16 integer registers.
5733     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
5734     return false;
5735   }
5736 
5737   unsigned getSizeOfUnwindException() const override {
5738     if (getABIInfo().isEABI()) return 88;
5739     return TargetCodeGenInfo::getSizeOfUnwindException();
5740   }
5741 
5742   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5743                            CodeGen::CodeGenModule &CGM) const override {
5744     if (GV->isDeclaration())
5745       return;
5746     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
5747     if (!FD)
5748       return;
5749 
5750     const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();
5751     if (!Attr)
5752       return;
5753 
5754     const char *Kind;
5755     switch (Attr->getInterrupt()) {
5756     case ARMInterruptAttr::Generic: Kind = ""; break;
5757     case ARMInterruptAttr::IRQ:     Kind = "IRQ"; break;
5758     case ARMInterruptAttr::FIQ:     Kind = "FIQ"; break;
5759     case ARMInterruptAttr::SWI:     Kind = "SWI"; break;
5760     case ARMInterruptAttr::ABORT:   Kind = "ABORT"; break;
5761     case ARMInterruptAttr::UNDEF:   Kind = "UNDEF"; break;
5762     }
5763 
5764     llvm::Function *Fn = cast<llvm::Function>(GV);
5765 
5766     Fn->addFnAttr("interrupt", Kind);
5767 
5768     ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind();
5769     if (ABI == ARMABIInfo::APCS)
5770       return;
5771 
5772     // AAPCS guarantees that sp will be 8-byte aligned on any public interface,
5773     // however this is not necessarily true on taking any interrupt. Instruct
5774     // the backend to perform a realignment as part of the function prologue.
5775     llvm::AttrBuilder B;
5776     B.addStackAlignmentAttr(8);
5777     Fn->addAttributes(llvm::AttributeList::FunctionIndex, B);
5778   }
5779 };
5780 
5781 class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo {
5782 public:
5783   WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
5784       : ARMTargetCodeGenInfo(CGT, K) {}
5785 
5786   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
5787                            CodeGen::CodeGenModule &CGM) const override;
5788 
5789   void getDependentLibraryOption(llvm::StringRef Lib,
5790                                  llvm::SmallString<24> &Opt) const override {
5791     Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib);
5792   }
5793 
5794   void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value,
5795                                llvm::SmallString<32> &Opt) const override {
5796     Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\"";
5797   }
5798 };
5799 
5800 void WindowsARMTargetCodeGenInfo::setTargetAttributes(
5801     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const {
5802   ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM);
5803   if (GV->isDeclaration())
5804     return;
5805   addStackProbeTargetAttributes(D, GV, CGM);
5806 }
5807 }
5808 
5809 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
5810   if (!::classifyReturnType(getCXXABI(), FI, *this))
5811     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(),
5812                                             FI.getCallingConvention());
5813 
5814   for (auto &I : FI.arguments())
5815     I.info = classifyArgumentType(I.type, FI.isVariadic(),
5816                                   FI.getCallingConvention());
5817 
5818 
5819   // Always honor user-specified calling convention.
5820   if (FI.getCallingConvention() != llvm::CallingConv::C)
5821     return;
5822 
5823   llvm::CallingConv::ID cc = getRuntimeCC();
5824   if (cc != llvm::CallingConv::C)
5825     FI.setEffectiveCallingConvention(cc);
5826 }
5827 
5828 /// Return the default calling convention that LLVM will use.
5829 llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const {
5830   // The default calling convention that LLVM will infer.
5831   if (isEABIHF() || getTarget().getTriple().isWatchABI())
5832     return llvm::CallingConv::ARM_AAPCS_VFP;
5833   else if (isEABI())
5834     return llvm::CallingConv::ARM_AAPCS;
5835   else
5836     return llvm::CallingConv::ARM_APCS;
5837 }
5838 
5839 /// Return the calling convention that our ABI would like us to use
5840 /// as the C calling convention.
5841 llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const {
5842   switch (getABIKind()) {
5843   case APCS: return llvm::CallingConv::ARM_APCS;
5844   case AAPCS: return llvm::CallingConv::ARM_AAPCS;
5845   case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
5846   case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP;
5847   }
5848   llvm_unreachable("bad ABI kind");
5849 }
5850 
5851 void ARMABIInfo::setCCs() {
5852   assert(getRuntimeCC() == llvm::CallingConv::C);
5853 
5854   // Don't muddy up the IR with a ton of explicit annotations if
5855   // they'd just match what LLVM will infer from the triple.
5856   llvm::CallingConv::ID abiCC = getABIDefaultCC();
5857   if (abiCC != getLLVMDefaultCC())
5858     RuntimeCC = abiCC;
5859 }
5860 
5861 ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const {
5862   uint64_t Size = getContext().getTypeSize(Ty);
5863   if (Size <= 32) {
5864     llvm::Type *ResType =
5865         llvm::Type::getInt32Ty(getVMContext());
5866     return ABIArgInfo::getDirect(ResType);
5867   }
5868   if (Size == 64 || Size == 128) {
5869     llvm::Type *ResType = llvm::VectorType::get(
5870         llvm::Type::getInt32Ty(getVMContext()), Size / 32);
5871     return ABIArgInfo::getDirect(ResType);
5872   }
5873   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
5874 }
5875 
5876 ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty,
5877                                                     const Type *Base,
5878                                                     uint64_t Members) const {
5879   assert(Base && "Base class should be set for homogeneous aggregate");
5880   // Base can be a floating-point or a vector.
5881   if (const VectorType *VT = Base->getAs<VectorType>()) {
5882     // FP16 vectors should be converted to integer vectors
5883     if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) {
5884       uint64_t Size = getContext().getTypeSize(VT);
5885       llvm::Type *NewVecTy = llvm::VectorType::get(
5886           llvm::Type::getInt32Ty(getVMContext()), Size / 32);
5887       llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members);
5888       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5889     }
5890   }
5891   return ABIArgInfo::getDirect(nullptr, 0, nullptr, false);
5892 }
5893 
5894 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
5895                                             unsigned functionCallConv) const {
5896   // 6.1.2.1 The following argument types are VFP CPRCs:
5897   //   A single-precision floating-point type (including promoted
5898   //   half-precision types); A double-precision floating-point type;
5899   //   A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate
5900   //   with a Base Type of a single- or double-precision floating-point type,
5901   //   64-bit containerized vectors or 128-bit containerized vectors with one
5902   //   to four Elements.
5903   // Variadic functions should always marshal to the base standard.
5904   bool IsAAPCS_VFP =
5905       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false);
5906 
5907   Ty = useFirstFieldIfTransparentUnion(Ty);
5908 
5909   // Handle illegal vector types here.
5910   if (isIllegalVectorType(Ty))
5911     return coerceIllegalVector(Ty);
5912 
5913   // _Float16 and __fp16 get passed as if it were an int or float, but with
5914   // the top 16 bits unspecified. This is not done for OpenCL as it handles the
5915   // half type natively, and does not need to interwork with AAPCS code.
5916   if ((Ty->isFloat16Type() || Ty->isHalfType()) &&
5917       !getContext().getLangOpts().NativeHalfArgsAndReturns) {
5918     llvm::Type *ResType = IsAAPCS_VFP ?
5919       llvm::Type::getFloatTy(getVMContext()) :
5920       llvm::Type::getInt32Ty(getVMContext());
5921     return ABIArgInfo::getDirect(ResType);
5922   }
5923 
5924   if (!isAggregateTypeForABI(Ty)) {
5925     // Treat an enum type as its underlying type.
5926     if (const EnumType *EnumTy = Ty->getAs<EnumType>()) {
5927       Ty = EnumTy->getDecl()->getIntegerType();
5928     }
5929 
5930     return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
5931                                           : ABIArgInfo::getDirect());
5932   }
5933 
5934   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
5935     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
5936   }
5937 
5938   // Ignore empty records.
5939   if (isEmptyRecord(getContext(), Ty, true))
5940     return ABIArgInfo::getIgnore();
5941 
5942   if (IsAAPCS_VFP) {
5943     // Homogeneous Aggregates need to be expanded when we can fit the aggregate
5944     // into VFP registers.
5945     const Type *Base = nullptr;
5946     uint64_t Members = 0;
5947     if (isHomogeneousAggregate(Ty, Base, Members))
5948       return classifyHomogeneousAggregate(Ty, Base, Members);
5949   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
5950     // WatchOS does have homogeneous aggregates. Note that we intentionally use
5951     // this convention even for a variadic function: the backend will use GPRs
5952     // if needed.
5953     const Type *Base = nullptr;
5954     uint64_t Members = 0;
5955     if (isHomogeneousAggregate(Ty, Base, Members)) {
5956       assert(Base && Members <= 4 && "unexpected homogeneous aggregate");
5957       llvm::Type *Ty =
5958         llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members);
5959       return ABIArgInfo::getDirect(Ty, 0, nullptr, false);
5960     }
5961   }
5962 
5963   if (getABIKind() == ARMABIInfo::AAPCS16_VFP &&
5964       getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) {
5965     // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're
5966     // bigger than 128-bits, they get placed in space allocated by the caller,
5967     // and a pointer is passed.
5968     return ABIArgInfo::getIndirect(
5969         CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false);
5970   }
5971 
5972   // Support byval for ARM.
5973   // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at
5974   // most 8-byte. We realign the indirect argument if type alignment is bigger
5975   // than ABI alignment.
5976   uint64_t ABIAlign = 4;
5977   uint64_t TyAlign;
5978   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
5979       getABIKind() == ARMABIInfo::AAPCS) {
5980     TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
5981     ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
5982   } else {
5983     TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
5984   }
5985   if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) {
5986     assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval");
5987     return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign),
5988                                    /*ByVal=*/true,
5989                                    /*Realign=*/TyAlign > ABIAlign);
5990   }
5991 
5992   // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of
5993   // same size and alignment.
5994   if (getTarget().isRenderScriptTarget()) {
5995     return coerceToIntArray(Ty, getContext(), getVMContext());
5996   }
5997 
5998   // Otherwise, pass by coercing to a structure of the appropriate size.
5999   llvm::Type* ElemTy;
6000   unsigned SizeRegs;
6001   // FIXME: Try to match the types of the arguments more accurately where
6002   // we can.
6003   if (TyAlign <= 4) {
6004     ElemTy = llvm::Type::getInt32Ty(getVMContext());
6005     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
6006   } else {
6007     ElemTy = llvm::Type::getInt64Ty(getVMContext());
6008     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
6009   }
6010 
6011   return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs));
6012 }
6013 
6014 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
6015                               llvm::LLVMContext &VMContext) {
6016   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
6017   // is called integer-like if its size is less than or equal to one word, and
6018   // the offset of each of its addressable sub-fields is zero.
6019 
6020   uint64_t Size = Context.getTypeSize(Ty);
6021 
6022   // Check that the type fits in a word.
6023   if (Size > 32)
6024     return false;
6025 
6026   // FIXME: Handle vector types!
6027   if (Ty->isVectorType())
6028     return false;
6029 
6030   // Float types are never treated as "integer like".
6031   if (Ty->isRealFloatingType())
6032     return false;
6033 
6034   // If this is a builtin or pointer type then it is ok.
6035   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
6036     return true;
6037 
6038   // Small complex integer types are "integer like".
6039   if (const ComplexType *CT = Ty->getAs<ComplexType>())
6040     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
6041 
6042   // Single element and zero sized arrays should be allowed, by the definition
6043   // above, but they are not.
6044 
6045   // Otherwise, it must be a record type.
6046   const RecordType *RT = Ty->getAs<RecordType>();
6047   if (!RT) return false;
6048 
6049   // Ignore records with flexible arrays.
6050   const RecordDecl *RD = RT->getDecl();
6051   if (RD->hasFlexibleArrayMember())
6052     return false;
6053 
6054   // Check that all sub-fields are at offset 0, and are themselves "integer
6055   // like".
6056   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
6057 
6058   bool HadField = false;
6059   unsigned idx = 0;
6060   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
6061        i != e; ++i, ++idx) {
6062     const FieldDecl *FD = *i;
6063 
6064     // Bit-fields are not addressable, we only need to verify they are "integer
6065     // like". We still have to disallow a subsequent non-bitfield, for example:
6066     //   struct { int : 0; int x }
6067     // is non-integer like according to gcc.
6068     if (FD->isBitField()) {
6069       if (!RD->isUnion())
6070         HadField = true;
6071 
6072       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6073         return false;
6074 
6075       continue;
6076     }
6077 
6078     // Check if this field is at offset 0.
6079     if (Layout.getFieldOffset(idx) != 0)
6080       return false;
6081 
6082     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
6083       return false;
6084 
6085     // Only allow at most one field in a structure. This doesn't match the
6086     // wording above, but follows gcc in situations with a field following an
6087     // empty structure.
6088     if (!RD->isUnion()) {
6089       if (HadField)
6090         return false;
6091 
6092       HadField = true;
6093     }
6094   }
6095 
6096   return true;
6097 }
6098 
6099 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic,
6100                                           unsigned functionCallConv) const {
6101 
6102   // Variadic functions should always marshal to the base standard.
6103   bool IsAAPCS_VFP =
6104       !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true);
6105 
6106   if (RetTy->isVoidType())
6107     return ABIArgInfo::getIgnore();
6108 
6109   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
6110     // Large vector types should be returned via memory.
6111     if (getContext().getTypeSize(RetTy) > 128)
6112       return getNaturalAlignIndirect(RetTy);
6113     // FP16 vectors should be converted to integer vectors
6114     if (!getTarget().hasLegalHalfType() &&
6115         (VT->getElementType()->isFloat16Type() ||
6116          VT->getElementType()->isHalfType()))
6117       return coerceIllegalVector(RetTy);
6118   }
6119 
6120   // _Float16 and __fp16 get returned as if it were an int or float, but with
6121   // the top 16 bits unspecified. This is not done for OpenCL as it handles the
6122   // half type natively, and does not need to interwork with AAPCS code.
6123   if ((RetTy->isFloat16Type() || RetTy->isHalfType()) &&
6124       !getContext().getLangOpts().NativeHalfArgsAndReturns) {
6125     llvm::Type *ResType = IsAAPCS_VFP ?
6126       llvm::Type::getFloatTy(getVMContext()) :
6127       llvm::Type::getInt32Ty(getVMContext());
6128     return ABIArgInfo::getDirect(ResType);
6129   }
6130 
6131   if (!isAggregateTypeForABI(RetTy)) {
6132     // Treat an enum type as its underlying type.
6133     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6134       RetTy = EnumTy->getDecl()->getIntegerType();
6135 
6136     return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
6137                                             : ABIArgInfo::getDirect();
6138   }
6139 
6140   // Are we following APCS?
6141   if (getABIKind() == APCS) {
6142     if (isEmptyRecord(getContext(), RetTy, false))
6143       return ABIArgInfo::getIgnore();
6144 
6145     // Complex types are all returned as packed integers.
6146     //
6147     // FIXME: Consider using 2 x vector types if the back end handles them
6148     // correctly.
6149     if (RetTy->isAnyComplexType())
6150       return ABIArgInfo::getDirect(llvm::IntegerType::get(
6151           getVMContext(), getContext().getTypeSize(RetTy)));
6152 
6153     // Integer like structures are returned in r0.
6154     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
6155       // Return in the smallest viable integer type.
6156       uint64_t Size = getContext().getTypeSize(RetTy);
6157       if (Size <= 8)
6158         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6159       if (Size <= 16)
6160         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6161       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6162     }
6163 
6164     // Otherwise return in memory.
6165     return getNaturalAlignIndirect(RetTy);
6166   }
6167 
6168   // Otherwise this is an AAPCS variant.
6169 
6170   if (isEmptyRecord(getContext(), RetTy, true))
6171     return ABIArgInfo::getIgnore();
6172 
6173   // Check for homogeneous aggregates with AAPCS-VFP.
6174   if (IsAAPCS_VFP) {
6175     const Type *Base = nullptr;
6176     uint64_t Members = 0;
6177     if (isHomogeneousAggregate(RetTy, Base, Members))
6178       return classifyHomogeneousAggregate(RetTy, Base, Members);
6179   }
6180 
6181   // Aggregates <= 4 bytes are returned in r0; other aggregates
6182   // are returned indirectly.
6183   uint64_t Size = getContext().getTypeSize(RetTy);
6184   if (Size <= 32) {
6185     // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of
6186     // same size and alignment.
6187     if (getTarget().isRenderScriptTarget()) {
6188       return coerceToIntArray(RetTy, getContext(), getVMContext());
6189     }
6190     if (getDataLayout().isBigEndian())
6191       // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4)
6192       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6193 
6194     // Return in the smallest viable integer type.
6195     if (Size <= 8)
6196       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
6197     if (Size <= 16)
6198       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
6199     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
6200   } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) {
6201     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext());
6202     llvm::Type *CoerceTy =
6203         llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32);
6204     return ABIArgInfo::getDirect(CoerceTy);
6205   }
6206 
6207   return getNaturalAlignIndirect(RetTy);
6208 }
6209 
6210 /// isIllegalVector - check whether Ty is an illegal vector type.
6211 bool ARMABIInfo::isIllegalVectorType(QualType Ty) const {
6212   if (const VectorType *VT = Ty->getAs<VectorType> ()) {
6213     // On targets that don't support FP16, FP16 is expanded into float, and we
6214     // don't want the ABI to depend on whether or not FP16 is supported in
6215     // hardware. Thus return false to coerce FP16 vectors into integer vectors.
6216     if (!getTarget().hasLegalHalfType() &&
6217         (VT->getElementType()->isFloat16Type() ||
6218          VT->getElementType()->isHalfType()))
6219       return true;
6220     if (isAndroid()) {
6221       // Android shipped using Clang 3.1, which supported a slightly different
6222       // vector ABI. The primary differences were that 3-element vector types
6223       // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path
6224       // accepts that legacy behavior for Android only.
6225       // Check whether VT is legal.
6226       unsigned NumElements = VT->getNumElements();
6227       // NumElements should be power of 2 or equal to 3.
6228       if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3)
6229         return true;
6230     } else {
6231       // Check whether VT is legal.
6232       unsigned NumElements = VT->getNumElements();
6233       uint64_t Size = getContext().getTypeSize(VT);
6234       // NumElements should be power of 2.
6235       if (!llvm::isPowerOf2_32(NumElements))
6236         return true;
6237       // Size should be greater than 32 bits.
6238       return Size <= 32;
6239     }
6240   }
6241   return false;
6242 }
6243 
6244 /// Return true if a type contains any 16-bit floating point vectors
6245 bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const {
6246   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
6247     uint64_t NElements = AT->getSize().getZExtValue();
6248     if (NElements == 0)
6249       return false;
6250     return containsAnyFP16Vectors(AT->getElementType());
6251   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
6252     const RecordDecl *RD = RT->getDecl();
6253 
6254     // If this is a C++ record, check the bases first.
6255     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6256       if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) {
6257             return containsAnyFP16Vectors(B.getType());
6258           }))
6259         return true;
6260 
6261     if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) {
6262           return FD && containsAnyFP16Vectors(FD->getType());
6263         }))
6264       return true;
6265 
6266     return false;
6267   } else {
6268     if (const VectorType *VT = Ty->getAs<VectorType>())
6269       return (VT->getElementType()->isFloat16Type() ||
6270               VT->getElementType()->isHalfType());
6271     return false;
6272   }
6273 }
6274 
6275 bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize,
6276                                            llvm::Type *eltTy,
6277                                            unsigned numElts) const {
6278   if (!llvm::isPowerOf2_32(numElts))
6279     return false;
6280   unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy);
6281   if (size > 64)
6282     return false;
6283   if (vectorSize.getQuantity() != 8 &&
6284       (vectorSize.getQuantity() != 16 || numElts == 1))
6285     return false;
6286   return true;
6287 }
6288 
6289 bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
6290   // Homogeneous aggregates for AAPCS-VFP must have base types of float,
6291   // double, or 64-bit or 128-bit vectors.
6292   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
6293     if (BT->getKind() == BuiltinType::Float ||
6294         BT->getKind() == BuiltinType::Double ||
6295         BT->getKind() == BuiltinType::LongDouble)
6296       return true;
6297   } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
6298     unsigned VecSize = getContext().getTypeSize(VT);
6299     if (VecSize == 64 || VecSize == 128)
6300       return true;
6301   }
6302   return false;
6303 }
6304 
6305 bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base,
6306                                                    uint64_t Members) const {
6307   return Members <= 4;
6308 }
6309 
6310 bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention,
6311                                         bool acceptHalf) const {
6312   // Give precedence to user-specified calling conventions.
6313   if (callConvention != llvm::CallingConv::C)
6314     return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP);
6315   else
6316     return (getABIKind() == AAPCS_VFP) ||
6317            (acceptHalf && (getABIKind() == AAPCS16_VFP));
6318 }
6319 
6320 Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6321                               QualType Ty) const {
6322   CharUnits SlotSize = CharUnits::fromQuantity(4);
6323 
6324   // Empty records are ignored for parameter passing purposes.
6325   if (isEmptyRecord(getContext(), Ty, true)) {
6326     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
6327     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
6328     return Addr;
6329   }
6330 
6331   CharUnits TySize = getContext().getTypeSizeInChars(Ty);
6332   CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty);
6333 
6334   // Use indirect if size of the illegal vector is bigger than 16 bytes.
6335   bool IsIndirect = false;
6336   const Type *Base = nullptr;
6337   uint64_t Members = 0;
6338   if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) {
6339     IsIndirect = true;
6340 
6341   // ARMv7k passes structs bigger than 16 bytes indirectly, in space
6342   // allocated by the caller.
6343   } else if (TySize > CharUnits::fromQuantity(16) &&
6344              getABIKind() == ARMABIInfo::AAPCS16_VFP &&
6345              !isHomogeneousAggregate(Ty, Base, Members)) {
6346     IsIndirect = true;
6347 
6348   // Otherwise, bound the type's ABI alignment.
6349   // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for
6350   // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte.
6351   // Our callers should be prepared to handle an under-aligned address.
6352   } else if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
6353              getABIKind() == ARMABIInfo::AAPCS) {
6354     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6355     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8));
6356   } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) {
6357     // ARMv7k allows type alignment up to 16 bytes.
6358     TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4));
6359     TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16));
6360   } else {
6361     TyAlignForABI = CharUnits::fromQuantity(4);
6362   }
6363 
6364   std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI };
6365   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo,
6366                           SlotSize, /*AllowHigherAlign*/ true);
6367 }
6368 
6369 //===----------------------------------------------------------------------===//
6370 // NVPTX ABI Implementation
6371 //===----------------------------------------------------------------------===//
6372 
6373 namespace {
6374 
6375 class NVPTXABIInfo : public ABIInfo {
6376 public:
6377   NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
6378 
6379   ABIArgInfo classifyReturnType(QualType RetTy) const;
6380   ABIArgInfo classifyArgumentType(QualType Ty) const;
6381 
6382   void computeInfo(CGFunctionInfo &FI) const override;
6383   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6384                     QualType Ty) const override;
6385 };
6386 
6387 class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
6388 public:
6389   NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
6390     : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
6391 
6392   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6393                            CodeGen::CodeGenModule &M) const override;
6394   bool shouldEmitStaticExternCAliases() const override;
6395 
6396 private:
6397   // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the
6398   // resulting MDNode to the nvvm.annotations MDNode.
6399   static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand);
6400 };
6401 
6402 /// Checks if the type is unsupported directly by the current target.
6403 static bool isUnsupportedType(ASTContext &Context, QualType T) {
6404   if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type())
6405     return true;
6406   if (!Context.getTargetInfo().hasFloat128Type() &&
6407       (T->isFloat128Type() ||
6408        (T->isRealFloatingType() && Context.getTypeSize(T) == 128)))
6409     return true;
6410   if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() &&
6411       Context.getTypeSize(T) > 64)
6412     return true;
6413   if (const auto *AT = T->getAsArrayTypeUnsafe())
6414     return isUnsupportedType(Context, AT->getElementType());
6415   const auto *RT = T->getAs<RecordType>();
6416   if (!RT)
6417     return false;
6418   const RecordDecl *RD = RT->getDecl();
6419 
6420   // If this is a C++ record, check the bases first.
6421   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6422     for (const CXXBaseSpecifier &I : CXXRD->bases())
6423       if (isUnsupportedType(Context, I.getType()))
6424         return true;
6425 
6426   for (const FieldDecl *I : RD->fields())
6427     if (isUnsupportedType(Context, I->getType()))
6428       return true;
6429   return false;
6430 }
6431 
6432 /// Coerce the given type into an array with maximum allowed size of elements.
6433 static ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, ASTContext &Context,
6434                                             llvm::LLVMContext &LLVMContext,
6435                                             unsigned MaxSize) {
6436   // Alignment and Size are measured in bits.
6437   const uint64_t Size = Context.getTypeSize(Ty);
6438   const uint64_t Alignment = Context.getTypeAlign(Ty);
6439   const unsigned Div = std::min<unsigned>(MaxSize, Alignment);
6440   llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Div);
6441   const uint64_t NumElements = (Size + Div - 1) / Div;
6442   return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements));
6443 }
6444 
6445 ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
6446   if (RetTy->isVoidType())
6447     return ABIArgInfo::getIgnore();
6448 
6449   if (getContext().getLangOpts().OpenMP &&
6450       getContext().getLangOpts().OpenMPIsDevice &&
6451       isUnsupportedType(getContext(), RetTy))
6452     return coerceToIntArrayWithLimit(RetTy, getContext(), getVMContext(), 64);
6453 
6454   // note: this is different from default ABI
6455   if (!RetTy->isScalarType())
6456     return ABIArgInfo::getDirect();
6457 
6458   // Treat an enum type as its underlying type.
6459   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
6460     RetTy = EnumTy->getDecl()->getIntegerType();
6461 
6462   return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
6463                                            : ABIArgInfo::getDirect());
6464 }
6465 
6466 ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
6467   // Treat an enum type as its underlying type.
6468   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6469     Ty = EnumTy->getDecl()->getIntegerType();
6470 
6471   // Return aggregates type as indirect by value
6472   if (isAggregateTypeForABI(Ty))
6473     return getNaturalAlignIndirect(Ty, /* byval */ true);
6474 
6475   return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
6476                                         : ABIArgInfo::getDirect());
6477 }
6478 
6479 void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
6480   if (!getCXXABI().classifyReturnType(FI))
6481     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6482   for (auto &I : FI.arguments())
6483     I.info = classifyArgumentType(I.type);
6484 
6485   // Always honor user-specified calling convention.
6486   if (FI.getCallingConvention() != llvm::CallingConv::C)
6487     return;
6488 
6489   FI.setEffectiveCallingConvention(getRuntimeCC());
6490 }
6491 
6492 Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6493                                 QualType Ty) const {
6494   llvm_unreachable("NVPTX does not support varargs");
6495 }
6496 
6497 void NVPTXTargetCodeGenInfo::setTargetAttributes(
6498     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
6499   if (GV->isDeclaration())
6500     return;
6501   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
6502   if (!FD) return;
6503 
6504   llvm::Function *F = cast<llvm::Function>(GV);
6505 
6506   // Perform special handling in OpenCL mode
6507   if (M.getLangOpts().OpenCL) {
6508     // Use OpenCL function attributes to check for kernel functions
6509     // By default, all functions are device functions
6510     if (FD->hasAttr<OpenCLKernelAttr>()) {
6511       // OpenCL __kernel functions get kernel metadata
6512       // Create !{<func-ref>, metadata !"kernel", i32 1} node
6513       addNVVMMetadata(F, "kernel", 1);
6514       // And kernel functions are not subject to inlining
6515       F->addFnAttr(llvm::Attribute::NoInline);
6516     }
6517   }
6518 
6519   // Perform special handling in CUDA mode.
6520   if (M.getLangOpts().CUDA) {
6521     // CUDA __global__ functions get a kernel metadata entry.  Since
6522     // __global__ functions cannot be called from the device, we do not
6523     // need to set the noinline attribute.
6524     if (FD->hasAttr<CUDAGlobalAttr>()) {
6525       // Create !{<func-ref>, metadata !"kernel", i32 1} node
6526       addNVVMMetadata(F, "kernel", 1);
6527     }
6528     if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) {
6529       // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node
6530       llvm::APSInt MaxThreads(32);
6531       MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext());
6532       if (MaxThreads > 0)
6533         addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue());
6534 
6535       // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
6536       // not specified in __launch_bounds__ or if the user specified a 0 value,
6537       // we don't have to add a PTX directive.
6538       if (Attr->getMinBlocks()) {
6539         llvm::APSInt MinBlocks(32);
6540         MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext());
6541         if (MinBlocks > 0)
6542           // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node
6543           addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue());
6544       }
6545     }
6546   }
6547 }
6548 
6549 void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name,
6550                                              int Operand) {
6551   llvm::Module *M = F->getParent();
6552   llvm::LLVMContext &Ctx = M->getContext();
6553 
6554   // Get "nvvm.annotations" metadata node
6555   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
6556 
6557   llvm::Metadata *MDVals[] = {
6558       llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name),
6559       llvm::ConstantAsMetadata::get(
6560           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))};
6561   // Append metadata to nvvm.annotations
6562   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
6563 }
6564 
6565 bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
6566   return false;
6567 }
6568 }
6569 
6570 //===----------------------------------------------------------------------===//
6571 // SystemZ ABI Implementation
6572 //===----------------------------------------------------------------------===//
6573 
6574 namespace {
6575 
6576 class SystemZABIInfo : public SwiftABIInfo {
6577   bool HasVector;
6578 
6579 public:
6580   SystemZABIInfo(CodeGenTypes &CGT, bool HV)
6581     : SwiftABIInfo(CGT), HasVector(HV) {}
6582 
6583   bool isPromotableIntegerType(QualType Ty) const;
6584   bool isCompoundType(QualType Ty) const;
6585   bool isVectorArgumentType(QualType Ty) const;
6586   bool isFPArgumentType(QualType Ty) const;
6587   QualType GetSingleElementType(QualType Ty) const;
6588 
6589   ABIArgInfo classifyReturnType(QualType RetTy) const;
6590   ABIArgInfo classifyArgumentType(QualType ArgTy) const;
6591 
6592   void computeInfo(CGFunctionInfo &FI) const override {
6593     if (!getCXXABI().classifyReturnType(FI))
6594       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
6595     for (auto &I : FI.arguments())
6596       I.info = classifyArgumentType(I.type);
6597   }
6598 
6599   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6600                     QualType Ty) const override;
6601 
6602   bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars,
6603                                     bool asReturnValue) const override {
6604     return occupiesMoreThan(CGT, scalars, /*total*/ 4);
6605   }
6606   bool isSwiftErrorInRegister() const override {
6607     return false;
6608   }
6609 };
6610 
6611 class SystemZTargetCodeGenInfo : public TargetCodeGenInfo {
6612 public:
6613   SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector)
6614     : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {}
6615 };
6616 
6617 }
6618 
6619 bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const {
6620   // Treat an enum type as its underlying type.
6621   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
6622     Ty = EnumTy->getDecl()->getIntegerType();
6623 
6624   // Promotable integer types are required to be promoted by the ABI.
6625   if (Ty->isPromotableIntegerType())
6626     return true;
6627 
6628   // 32-bit values must also be promoted.
6629   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6630     switch (BT->getKind()) {
6631     case BuiltinType::Int:
6632     case BuiltinType::UInt:
6633       return true;
6634     default:
6635       return false;
6636     }
6637   return false;
6638 }
6639 
6640 bool SystemZABIInfo::isCompoundType(QualType Ty) const {
6641   return (Ty->isAnyComplexType() ||
6642           Ty->isVectorType() ||
6643           isAggregateTypeForABI(Ty));
6644 }
6645 
6646 bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const {
6647   return (HasVector &&
6648           Ty->isVectorType() &&
6649           getContext().getTypeSize(Ty) <= 128);
6650 }
6651 
6652 bool SystemZABIInfo::isFPArgumentType(QualType Ty) const {
6653   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
6654     switch (BT->getKind()) {
6655     case BuiltinType::Float:
6656     case BuiltinType::Double:
6657       return true;
6658     default:
6659       return false;
6660     }
6661 
6662   return false;
6663 }
6664 
6665 QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const {
6666   if (const RecordType *RT = Ty->getAsStructureType()) {
6667     const RecordDecl *RD = RT->getDecl();
6668     QualType Found;
6669 
6670     // If this is a C++ record, check the bases first.
6671     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
6672       for (const auto &I : CXXRD->bases()) {
6673         QualType Base = I.getType();
6674 
6675         // Empty bases don't affect things either way.
6676         if (isEmptyRecord(getContext(), Base, true))
6677           continue;
6678 
6679         if (!Found.isNull())
6680           return Ty;
6681         Found = GetSingleElementType(Base);
6682       }
6683 
6684     // Check the fields.
6685     for (const auto *FD : RD->fields()) {
6686       // For compatibility with GCC, ignore empty bitfields in C++ mode.
6687       // Unlike isSingleElementStruct(), empty structure and array fields
6688       // do count.  So do anonymous bitfields that aren't zero-sized.
6689       if (getContext().getLangOpts().CPlusPlus &&
6690           FD->isZeroLengthBitField(getContext()))
6691         continue;
6692 
6693       // Unlike isSingleElementStruct(), arrays do not count.
6694       // Nested structures still do though.
6695       if (!Found.isNull())
6696         return Ty;
6697       Found = GetSingleElementType(FD->getType());
6698     }
6699 
6700     // Unlike isSingleElementStruct(), trailing padding is allowed.
6701     // An 8-byte aligned struct s { float f; } is passed as a double.
6702     if (!Found.isNull())
6703       return Found;
6704   }
6705 
6706   return Ty;
6707 }
6708 
6709 Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6710                                   QualType Ty) const {
6711   // Assume that va_list type is correct; should be pointer to LLVM type:
6712   // struct {
6713   //   i64 __gpr;
6714   //   i64 __fpr;
6715   //   i8 *__overflow_arg_area;
6716   //   i8 *__reg_save_area;
6717   // };
6718 
6719   // Every non-vector argument occupies 8 bytes and is passed by preference
6720   // in either GPRs or FPRs.  Vector arguments occupy 8 or 16 bytes and are
6721   // always passed on the stack.
6722   Ty = getContext().getCanonicalType(Ty);
6723   auto TyInfo = getContext().getTypeInfoInChars(Ty);
6724   llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty);
6725   llvm::Type *DirectTy = ArgTy;
6726   ABIArgInfo AI = classifyArgumentType(Ty);
6727   bool IsIndirect = AI.isIndirect();
6728   bool InFPRs = false;
6729   bool IsVector = false;
6730   CharUnits UnpaddedSize;
6731   CharUnits DirectAlign;
6732   if (IsIndirect) {
6733     DirectTy = llvm::PointerType::getUnqual(DirectTy);
6734     UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8);
6735   } else {
6736     if (AI.getCoerceToType())
6737       ArgTy = AI.getCoerceToType();
6738     InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy();
6739     IsVector = ArgTy->isVectorTy();
6740     UnpaddedSize = TyInfo.first;
6741     DirectAlign = TyInfo.second;
6742   }
6743   CharUnits PaddedSize = CharUnits::fromQuantity(8);
6744   if (IsVector && UnpaddedSize > PaddedSize)
6745     PaddedSize = CharUnits::fromQuantity(16);
6746   assert((UnpaddedSize <= PaddedSize) && "Invalid argument size.");
6747 
6748   CharUnits Padding = (PaddedSize - UnpaddedSize);
6749 
6750   llvm::Type *IndexTy = CGF.Int64Ty;
6751   llvm::Value *PaddedSizeV =
6752     llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity());
6753 
6754   if (IsVector) {
6755     // Work out the address of a vector argument on the stack.
6756     // Vector arguments are always passed in the high bits of a
6757     // single (8 byte) or double (16 byte) stack slot.
6758     Address OverflowArgAreaPtr =
6759         CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
6760     Address OverflowArgArea =
6761       Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6762               TyInfo.second);
6763     Address MemAddr =
6764       CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr");
6765 
6766     // Update overflow_arg_area_ptr pointer
6767     llvm::Value *NewOverflowArgArea =
6768       CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6769                             "overflow_arg_area");
6770     CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6771 
6772     return MemAddr;
6773   }
6774 
6775   assert(PaddedSize.getQuantity() == 8);
6776 
6777   unsigned MaxRegs, RegCountField, RegSaveIndex;
6778   CharUnits RegPadding;
6779   if (InFPRs) {
6780     MaxRegs = 4; // Maximum of 4 FPR arguments
6781     RegCountField = 1; // __fpr
6782     RegSaveIndex = 16; // save offset for f0
6783     RegPadding = CharUnits(); // floats are passed in the high bits of an FPR
6784   } else {
6785     MaxRegs = 5; // Maximum of 5 GPR arguments
6786     RegCountField = 0; // __gpr
6787     RegSaveIndex = 2; // save offset for r2
6788     RegPadding = Padding; // values are passed in the low bits of a GPR
6789   }
6790 
6791   Address RegCountPtr =
6792       CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr");
6793   llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count");
6794   llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs);
6795   llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV,
6796                                                  "fits_in_regs");
6797 
6798   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
6799   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
6800   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
6801   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
6802 
6803   // Emit code to load the value if it was passed in registers.
6804   CGF.EmitBlock(InRegBlock);
6805 
6806   // Work out the address of an argument register.
6807   llvm::Value *ScaledRegCount =
6808     CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count");
6809   llvm::Value *RegBase =
6810     llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity()
6811                                       + RegPadding.getQuantity());
6812   llvm::Value *RegOffset =
6813     CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset");
6814   Address RegSaveAreaPtr =
6815       CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr");
6816   llvm::Value *RegSaveArea =
6817     CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area");
6818   Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset,
6819                                            "raw_reg_addr"),
6820                      PaddedSize);
6821   Address RegAddr =
6822     CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr");
6823 
6824   // Update the register count
6825   llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1);
6826   llvm::Value *NewRegCount =
6827     CGF.Builder.CreateAdd(RegCount, One, "reg_count");
6828   CGF.Builder.CreateStore(NewRegCount, RegCountPtr);
6829   CGF.EmitBranch(ContBlock);
6830 
6831   // Emit code to load the value if it was passed in memory.
6832   CGF.EmitBlock(InMemBlock);
6833 
6834   // Work out the address of a stack argument.
6835   Address OverflowArgAreaPtr =
6836       CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr");
6837   Address OverflowArgArea =
6838     Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"),
6839             PaddedSize);
6840   Address RawMemAddr =
6841     CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr");
6842   Address MemAddr =
6843     CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr");
6844 
6845   // Update overflow_arg_area_ptr pointer
6846   llvm::Value *NewOverflowArgArea =
6847     CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV,
6848                           "overflow_arg_area");
6849   CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr);
6850   CGF.EmitBranch(ContBlock);
6851 
6852   // Return the appropriate result.
6853   CGF.EmitBlock(ContBlock);
6854   Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock,
6855                                  MemAddr, InMemBlock, "va_arg.addr");
6856 
6857   if (IsIndirect)
6858     ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"),
6859                       TyInfo.second);
6860 
6861   return ResAddr;
6862 }
6863 
6864 ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const {
6865   if (RetTy->isVoidType())
6866     return ABIArgInfo::getIgnore();
6867   if (isVectorArgumentType(RetTy))
6868     return ABIArgInfo::getDirect();
6869   if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64)
6870     return getNaturalAlignIndirect(RetTy);
6871   return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy)
6872                                          : ABIArgInfo::getDirect());
6873 }
6874 
6875 ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const {
6876   // Handle the generic C++ ABI.
6877   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
6878     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
6879 
6880   // Integers and enums are extended to full register width.
6881   if (isPromotableIntegerType(Ty))
6882     return ABIArgInfo::getExtend(Ty);
6883 
6884   // Handle vector types and vector-like structure types.  Note that
6885   // as opposed to float-like structure types, we do not allow any
6886   // padding for vector-like structures, so verify the sizes match.
6887   uint64_t Size = getContext().getTypeSize(Ty);
6888   QualType SingleElementTy = GetSingleElementType(Ty);
6889   if (isVectorArgumentType(SingleElementTy) &&
6890       getContext().getTypeSize(SingleElementTy) == Size)
6891     return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy));
6892 
6893   // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly.
6894   if (Size != 8 && Size != 16 && Size != 32 && Size != 64)
6895     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6896 
6897   // Handle small structures.
6898   if (const RecordType *RT = Ty->getAs<RecordType>()) {
6899     // Structures with flexible arrays have variable length, so really
6900     // fail the size test above.
6901     const RecordDecl *RD = RT->getDecl();
6902     if (RD->hasFlexibleArrayMember())
6903       return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6904 
6905     // The structure is passed as an unextended integer, a float, or a double.
6906     llvm::Type *PassTy;
6907     if (isFPArgumentType(SingleElementTy)) {
6908       assert(Size == 32 || Size == 64);
6909       if (Size == 32)
6910         PassTy = llvm::Type::getFloatTy(getVMContext());
6911       else
6912         PassTy = llvm::Type::getDoubleTy(getVMContext());
6913     } else
6914       PassTy = llvm::IntegerType::get(getVMContext(), Size);
6915     return ABIArgInfo::getDirect(PassTy);
6916   }
6917 
6918   // Non-structure compounds are passed indirectly.
6919   if (isCompoundType(Ty))
6920     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
6921 
6922   return ABIArgInfo::getDirect(nullptr);
6923 }
6924 
6925 //===----------------------------------------------------------------------===//
6926 // MSP430 ABI Implementation
6927 //===----------------------------------------------------------------------===//
6928 
6929 namespace {
6930 
6931 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
6932 public:
6933   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
6934     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
6935   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
6936                            CodeGen::CodeGenModule &M) const override;
6937 };
6938 
6939 }
6940 
6941 void MSP430TargetCodeGenInfo::setTargetAttributes(
6942     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
6943   if (GV->isDeclaration())
6944     return;
6945   if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
6946     const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>();
6947     if (!InterruptAttr)
6948       return;
6949 
6950     // Handle 'interrupt' attribute:
6951     llvm::Function *F = cast<llvm::Function>(GV);
6952 
6953     // Step 1: Set ISR calling convention.
6954     F->setCallingConv(llvm::CallingConv::MSP430_INTR);
6955 
6956     // Step 2: Add attributes goodness.
6957     F->addFnAttr(llvm::Attribute::NoInline);
6958     F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber()));
6959   }
6960 }
6961 
6962 //===----------------------------------------------------------------------===//
6963 // MIPS ABI Implementation.  This works for both little-endian and
6964 // big-endian variants.
6965 //===----------------------------------------------------------------------===//
6966 
6967 namespace {
6968 class MipsABIInfo : public ABIInfo {
6969   bool IsO32;
6970   unsigned MinABIStackAlignInBytes, StackAlignInBytes;
6971   void CoerceToIntArgs(uint64_t TySize,
6972                        SmallVectorImpl<llvm::Type *> &ArgList) const;
6973   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
6974   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
6975   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
6976 public:
6977   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
6978     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
6979     StackAlignInBytes(IsO32 ? 8 : 16) {}
6980 
6981   ABIArgInfo classifyReturnType(QualType RetTy) const;
6982   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
6983   void computeInfo(CGFunctionInfo &FI) const override;
6984   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
6985                     QualType Ty) const override;
6986   ABIArgInfo extendType(QualType Ty) const;
6987 };
6988 
6989 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
6990   unsigned SizeOfUnwindException;
6991 public:
6992   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
6993     : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
6994       SizeOfUnwindException(IsO32 ? 24 : 32) {}
6995 
6996   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override {
6997     return 29;
6998   }
6999 
7000   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7001                            CodeGen::CodeGenModule &CGM) const override {
7002     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7003     if (!FD) return;
7004     llvm::Function *Fn = cast<llvm::Function>(GV);
7005 
7006     if (FD->hasAttr<MipsLongCallAttr>())
7007       Fn->addFnAttr("long-call");
7008     else if (FD->hasAttr<MipsShortCallAttr>())
7009       Fn->addFnAttr("short-call");
7010 
7011     // Other attributes do not have a meaning for declarations.
7012     if (GV->isDeclaration())
7013       return;
7014 
7015     if (FD->hasAttr<Mips16Attr>()) {
7016       Fn->addFnAttr("mips16");
7017     }
7018     else if (FD->hasAttr<NoMips16Attr>()) {
7019       Fn->addFnAttr("nomips16");
7020     }
7021 
7022     if (FD->hasAttr<MicroMipsAttr>())
7023       Fn->addFnAttr("micromips");
7024     else if (FD->hasAttr<NoMicroMipsAttr>())
7025       Fn->addFnAttr("nomicromips");
7026 
7027     const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>();
7028     if (!Attr)
7029       return;
7030 
7031     const char *Kind;
7032     switch (Attr->getInterrupt()) {
7033     case MipsInterruptAttr::eic:     Kind = "eic"; break;
7034     case MipsInterruptAttr::sw0:     Kind = "sw0"; break;
7035     case MipsInterruptAttr::sw1:     Kind = "sw1"; break;
7036     case MipsInterruptAttr::hw0:     Kind = "hw0"; break;
7037     case MipsInterruptAttr::hw1:     Kind = "hw1"; break;
7038     case MipsInterruptAttr::hw2:     Kind = "hw2"; break;
7039     case MipsInterruptAttr::hw3:     Kind = "hw3"; break;
7040     case MipsInterruptAttr::hw4:     Kind = "hw4"; break;
7041     case MipsInterruptAttr::hw5:     Kind = "hw5"; break;
7042     }
7043 
7044     Fn->addFnAttr("interrupt", Kind);
7045 
7046   }
7047 
7048   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7049                                llvm::Value *Address) const override;
7050 
7051   unsigned getSizeOfUnwindException() const override {
7052     return SizeOfUnwindException;
7053   }
7054 };
7055 }
7056 
7057 void MipsABIInfo::CoerceToIntArgs(
7058     uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const {
7059   llvm::IntegerType *IntTy =
7060     llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
7061 
7062   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
7063   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
7064     ArgList.push_back(IntTy);
7065 
7066   // If necessary, add one more integer type to ArgList.
7067   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
7068 
7069   if (R)
7070     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
7071 }
7072 
7073 // In N32/64, an aligned double precision floating point field is passed in
7074 // a register.
7075 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
7076   SmallVector<llvm::Type*, 8> ArgList, IntArgList;
7077 
7078   if (IsO32) {
7079     CoerceToIntArgs(TySize, ArgList);
7080     return llvm::StructType::get(getVMContext(), ArgList);
7081   }
7082 
7083   if (Ty->isComplexType())
7084     return CGT.ConvertType(Ty);
7085 
7086   const RecordType *RT = Ty->getAs<RecordType>();
7087 
7088   // Unions/vectors are passed in integer registers.
7089   if (!RT || !RT->isStructureOrClassType()) {
7090     CoerceToIntArgs(TySize, ArgList);
7091     return llvm::StructType::get(getVMContext(), ArgList);
7092   }
7093 
7094   const RecordDecl *RD = RT->getDecl();
7095   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7096   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
7097 
7098   uint64_t LastOffset = 0;
7099   unsigned idx = 0;
7100   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
7101 
7102   // Iterate over fields in the struct/class and check if there are any aligned
7103   // double fields.
7104   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
7105        i != e; ++i, ++idx) {
7106     const QualType Ty = i->getType();
7107     const BuiltinType *BT = Ty->getAs<BuiltinType>();
7108 
7109     if (!BT || BT->getKind() != BuiltinType::Double)
7110       continue;
7111 
7112     uint64_t Offset = Layout.getFieldOffset(idx);
7113     if (Offset % 64) // Ignore doubles that are not aligned.
7114       continue;
7115 
7116     // Add ((Offset - LastOffset) / 64) args of type i64.
7117     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
7118       ArgList.push_back(I64);
7119 
7120     // Add double type.
7121     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
7122     LastOffset = Offset + 64;
7123   }
7124 
7125   CoerceToIntArgs(TySize - LastOffset, IntArgList);
7126   ArgList.append(IntArgList.begin(), IntArgList.end());
7127 
7128   return llvm::StructType::get(getVMContext(), ArgList);
7129 }
7130 
7131 llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset,
7132                                         uint64_t Offset) const {
7133   if (OrigOffset + MinABIStackAlignInBytes > Offset)
7134     return nullptr;
7135 
7136   return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8);
7137 }
7138 
7139 ABIArgInfo
7140 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
7141   Ty = useFirstFieldIfTransparentUnion(Ty);
7142 
7143   uint64_t OrigOffset = Offset;
7144   uint64_t TySize = getContext().getTypeSize(Ty);
7145   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
7146 
7147   Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
7148                    (uint64_t)StackAlignInBytes);
7149   unsigned CurrOffset = llvm::alignTo(Offset, Align);
7150   Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8;
7151 
7152   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
7153     // Ignore empty aggregates.
7154     if (TySize == 0)
7155       return ABIArgInfo::getIgnore();
7156 
7157     if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
7158       Offset = OrigOffset + MinABIStackAlignInBytes;
7159       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7160     }
7161 
7162     // If we have reached here, aggregates are passed directly by coercing to
7163     // another structure type. Padding is inserted if the offset of the
7164     // aggregate is unaligned.
7165     ABIArgInfo ArgInfo =
7166         ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
7167                               getPaddingType(OrigOffset, CurrOffset));
7168     ArgInfo.setInReg(true);
7169     return ArgInfo;
7170   }
7171 
7172   // Treat an enum type as its underlying type.
7173   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7174     Ty = EnumTy->getDecl()->getIntegerType();
7175 
7176   // All integral types are promoted to the GPR width.
7177   if (Ty->isIntegralOrEnumerationType())
7178     return extendType(Ty);
7179 
7180   return ABIArgInfo::getDirect(
7181       nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset));
7182 }
7183 
7184 llvm::Type*
7185 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
7186   const RecordType *RT = RetTy->getAs<RecordType>();
7187   SmallVector<llvm::Type*, 8> RTList;
7188 
7189   if (RT && RT->isStructureOrClassType()) {
7190     const RecordDecl *RD = RT->getDecl();
7191     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
7192     unsigned FieldCnt = Layout.getFieldCount();
7193 
7194     // N32/64 returns struct/classes in floating point registers if the
7195     // following conditions are met:
7196     // 1. The size of the struct/class is no larger than 128-bit.
7197     // 2. The struct/class has one or two fields all of which are floating
7198     //    point types.
7199     // 3. The offset of the first field is zero (this follows what gcc does).
7200     //
7201     // Any other composite results are returned in integer registers.
7202     //
7203     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
7204       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
7205       for (; b != e; ++b) {
7206         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
7207 
7208         if (!BT || !BT->isFloatingPoint())
7209           break;
7210 
7211         RTList.push_back(CGT.ConvertType(b->getType()));
7212       }
7213 
7214       if (b == e)
7215         return llvm::StructType::get(getVMContext(), RTList,
7216                                      RD->hasAttr<PackedAttr>());
7217 
7218       RTList.clear();
7219     }
7220   }
7221 
7222   CoerceToIntArgs(Size, RTList);
7223   return llvm::StructType::get(getVMContext(), RTList);
7224 }
7225 
7226 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
7227   uint64_t Size = getContext().getTypeSize(RetTy);
7228 
7229   if (RetTy->isVoidType())
7230     return ABIArgInfo::getIgnore();
7231 
7232   // O32 doesn't treat zero-sized structs differently from other structs.
7233   // However, N32/N64 ignores zero sized return values.
7234   if (!IsO32 && Size == 0)
7235     return ABIArgInfo::getIgnore();
7236 
7237   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
7238     if (Size <= 128) {
7239       if (RetTy->isAnyComplexType())
7240         return ABIArgInfo::getDirect();
7241 
7242       // O32 returns integer vectors in registers and N32/N64 returns all small
7243       // aggregates in registers.
7244       if (!IsO32 ||
7245           (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) {
7246         ABIArgInfo ArgInfo =
7247             ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
7248         ArgInfo.setInReg(true);
7249         return ArgInfo;
7250       }
7251     }
7252 
7253     return getNaturalAlignIndirect(RetTy);
7254   }
7255 
7256   // Treat an enum type as its underlying type.
7257   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7258     RetTy = EnumTy->getDecl()->getIntegerType();
7259 
7260   if (RetTy->isPromotableIntegerType())
7261     return ABIArgInfo::getExtend(RetTy);
7262 
7263   if ((RetTy->isUnsignedIntegerOrEnumerationType() ||
7264       RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32)
7265     return ABIArgInfo::getSignExtend(RetTy);
7266 
7267   return ABIArgInfo::getDirect();
7268 }
7269 
7270 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
7271   ABIArgInfo &RetInfo = FI.getReturnInfo();
7272   if (!getCXXABI().classifyReturnType(FI))
7273     RetInfo = classifyReturnType(FI.getReturnType());
7274 
7275   // Check if a pointer to an aggregate is passed as a hidden argument.
7276   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
7277 
7278   for (auto &I : FI.arguments())
7279     I.info = classifyArgumentType(I.type, Offset);
7280 }
7281 
7282 Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7283                                QualType OrigTy) const {
7284   QualType Ty = OrigTy;
7285 
7286   // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64.
7287   // Pointers are also promoted in the same way but this only matters for N32.
7288   unsigned SlotSizeInBits = IsO32 ? 32 : 64;
7289   unsigned PtrWidth = getTarget().getPointerWidth(0);
7290   bool DidPromote = false;
7291   if ((Ty->isIntegerType() &&
7292           getContext().getIntWidth(Ty) < SlotSizeInBits) ||
7293       (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) {
7294     DidPromote = true;
7295     Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits,
7296                                             Ty->isSignedIntegerType());
7297   }
7298 
7299   auto TyInfo = getContext().getTypeInfoInChars(Ty);
7300 
7301   // The alignment of things in the argument area is never larger than
7302   // StackAlignInBytes.
7303   TyInfo.second =
7304     std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes));
7305 
7306   // MinABIStackAlignInBytes is the size of argument slots on the stack.
7307   CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes);
7308 
7309   Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7310                           TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true);
7311 
7312 
7313   // If there was a promotion, "unpromote" into a temporary.
7314   // TODO: can we just use a pointer into a subset of the original slot?
7315   if (DidPromote) {
7316     Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp");
7317     llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr);
7318 
7319     // Truncate down to the right width.
7320     llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType()
7321                                                  : CGF.IntPtrTy);
7322     llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy);
7323     if (OrigTy->isPointerType())
7324       V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType());
7325 
7326     CGF.Builder.CreateStore(V, Temp);
7327     Addr = Temp;
7328   }
7329 
7330   return Addr;
7331 }
7332 
7333 ABIArgInfo MipsABIInfo::extendType(QualType Ty) const {
7334   int TySize = getContext().getTypeSize(Ty);
7335 
7336   // MIPS64 ABI requires unsigned 32 bit integers to be sign extended.
7337   if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
7338     return ABIArgInfo::getSignExtend(Ty);
7339 
7340   return ABIArgInfo::getExtend(Ty);
7341 }
7342 
7343 bool
7344 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
7345                                                llvm::Value *Address) const {
7346   // This information comes from gcc's implementation, which seems to
7347   // as canonical as it gets.
7348 
7349   // Everything on MIPS is 4 bytes.  Double-precision FP registers
7350   // are aliased to pairs of single-precision FP registers.
7351   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
7352 
7353   // 0-31 are the general purpose registers, $0 - $31.
7354   // 32-63 are the floating-point registers, $f0 - $f31.
7355   // 64 and 65 are the multiply/divide registers, $hi and $lo.
7356   // 66 is the (notional, I think) register for signal-handler return.
7357   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
7358 
7359   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
7360   // They are one bit wide and ignored here.
7361 
7362   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
7363   // (coprocessor 1 is the FP unit)
7364   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
7365   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
7366   // 176-181 are the DSP accumulator registers.
7367   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
7368   return false;
7369 }
7370 
7371 //===----------------------------------------------------------------------===//
7372 // AVR ABI Implementation.
7373 //===----------------------------------------------------------------------===//
7374 
7375 namespace {
7376 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
7377 public:
7378   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
7379     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { }
7380 
7381   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7382                            CodeGen::CodeGenModule &CGM) const override {
7383     if (GV->isDeclaration())
7384       return;
7385     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
7386     if (!FD) return;
7387     auto *Fn = cast<llvm::Function>(GV);
7388 
7389     if (FD->getAttr<AVRInterruptAttr>())
7390       Fn->addFnAttr("interrupt");
7391 
7392     if (FD->getAttr<AVRSignalAttr>())
7393       Fn->addFnAttr("signal");
7394   }
7395 };
7396 }
7397 
7398 //===----------------------------------------------------------------------===//
7399 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
7400 // Currently subclassed only to implement custom OpenCL C function attribute
7401 // handling.
7402 //===----------------------------------------------------------------------===//
7403 
7404 namespace {
7405 
7406 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
7407 public:
7408   TCETargetCodeGenInfo(CodeGenTypes &CGT)
7409     : DefaultTargetCodeGenInfo(CGT) {}
7410 
7411   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7412                            CodeGen::CodeGenModule &M) const override;
7413 };
7414 
7415 void TCETargetCodeGenInfo::setTargetAttributes(
7416     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
7417   if (GV->isDeclaration())
7418     return;
7419   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
7420   if (!FD) return;
7421 
7422   llvm::Function *F = cast<llvm::Function>(GV);
7423 
7424   if (M.getLangOpts().OpenCL) {
7425     if (FD->hasAttr<OpenCLKernelAttr>()) {
7426       // OpenCL C Kernel functions are not subject to inlining
7427       F->addFnAttr(llvm::Attribute::NoInline);
7428       const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>();
7429       if (Attr) {
7430         // Convert the reqd_work_group_size() attributes to metadata.
7431         llvm::LLVMContext &Context = F->getContext();
7432         llvm::NamedMDNode *OpenCLMetadata =
7433             M.getModule().getOrInsertNamedMetadata(
7434                 "opencl.kernel_wg_size_info");
7435 
7436         SmallVector<llvm::Metadata *, 5> Operands;
7437         Operands.push_back(llvm::ConstantAsMetadata::get(F));
7438 
7439         Operands.push_back(
7440             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7441                 M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
7442         Operands.push_back(
7443             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7444                 M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
7445         Operands.push_back(
7446             llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
7447                 M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
7448 
7449         // Add a boolean constant operand for "required" (true) or "hint"
7450         // (false) for implementing the work_group_size_hint attr later.
7451         // Currently always true as the hint is not yet implemented.
7452         Operands.push_back(
7453             llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context)));
7454         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
7455       }
7456     }
7457   }
7458 }
7459 
7460 }
7461 
7462 //===----------------------------------------------------------------------===//
7463 // Hexagon ABI Implementation
7464 //===----------------------------------------------------------------------===//
7465 
7466 namespace {
7467 
7468 class HexagonABIInfo : public ABIInfo {
7469 
7470 
7471 public:
7472   HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
7473 
7474 private:
7475 
7476   ABIArgInfo classifyReturnType(QualType RetTy) const;
7477   ABIArgInfo classifyArgumentType(QualType RetTy) const;
7478 
7479   void computeInfo(CGFunctionInfo &FI) const override;
7480 
7481   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7482                     QualType Ty) const override;
7483 };
7484 
7485 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
7486 public:
7487   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
7488     :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
7489 
7490   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
7491     return 29;
7492   }
7493 };
7494 
7495 }
7496 
7497 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
7498   if (!getCXXABI().classifyReturnType(FI))
7499     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7500   for (auto &I : FI.arguments())
7501     I.info = classifyArgumentType(I.type);
7502 }
7503 
7504 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
7505   if (!isAggregateTypeForABI(Ty)) {
7506     // Treat an enum type as its underlying type.
7507     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
7508       Ty = EnumTy->getDecl()->getIntegerType();
7509 
7510     return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty)
7511                                           : ABIArgInfo::getDirect());
7512   }
7513 
7514   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
7515     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7516 
7517   // Ignore empty records.
7518   if (isEmptyRecord(getContext(), Ty, true))
7519     return ABIArgInfo::getIgnore();
7520 
7521   uint64_t Size = getContext().getTypeSize(Ty);
7522   if (Size > 64)
7523     return getNaturalAlignIndirect(Ty, /*ByVal=*/true);
7524     // Pass in the smallest viable integer type.
7525   else if (Size > 32)
7526       return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7527   else if (Size > 16)
7528       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7529   else if (Size > 8)
7530       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7531   else
7532       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7533 }
7534 
7535 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
7536   if (RetTy->isVoidType())
7537     return ABIArgInfo::getIgnore();
7538 
7539   // Large vector types should be returned via memory.
7540   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
7541     return getNaturalAlignIndirect(RetTy);
7542 
7543   if (!isAggregateTypeForABI(RetTy)) {
7544     // Treat an enum type as its underlying type.
7545     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
7546       RetTy = EnumTy->getDecl()->getIntegerType();
7547 
7548     return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy)
7549                                              : ABIArgInfo::getDirect());
7550   }
7551 
7552   if (isEmptyRecord(getContext(), RetTy, true))
7553     return ABIArgInfo::getIgnore();
7554 
7555   // Aggregates <= 8 bytes are returned in r0; other aggregates
7556   // are returned indirectly.
7557   uint64_t Size = getContext().getTypeSize(RetTy);
7558   if (Size <= 64) {
7559     // Return in the smallest viable integer type.
7560     if (Size <= 8)
7561       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
7562     if (Size <= 16)
7563       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7564     if (Size <= 32)
7565       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7566     return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
7567   }
7568 
7569   return getNaturalAlignIndirect(RetTy, /*ByVal=*/true);
7570 }
7571 
7572 Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7573                                   QualType Ty) const {
7574   // FIXME: Someone needs to audit that this handle alignment correctly.
7575   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
7576                           getContext().getTypeInfoInChars(Ty),
7577                           CharUnits::fromQuantity(4),
7578                           /*AllowHigherAlign*/ true);
7579 }
7580 
7581 //===----------------------------------------------------------------------===//
7582 // Lanai ABI Implementation
7583 //===----------------------------------------------------------------------===//
7584 
7585 namespace {
7586 class LanaiABIInfo : public DefaultABIInfo {
7587 public:
7588   LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
7589 
7590   bool shouldUseInReg(QualType Ty, CCState &State) const;
7591 
7592   void computeInfo(CGFunctionInfo &FI) const override {
7593     CCState State(FI.getCallingConvention());
7594     // Lanai uses 4 registers to pass arguments unless the function has the
7595     // regparm attribute set.
7596     if (FI.getHasRegParm()) {
7597       State.FreeRegs = FI.getRegParm();
7598     } else {
7599       State.FreeRegs = 4;
7600     }
7601 
7602     if (!getCXXABI().classifyReturnType(FI))
7603       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7604     for (auto &I : FI.arguments())
7605       I.info = classifyArgumentType(I.type, State);
7606   }
7607 
7608   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const;
7609   ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const;
7610 };
7611 } // end anonymous namespace
7612 
7613 bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const {
7614   unsigned Size = getContext().getTypeSize(Ty);
7615   unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U;
7616 
7617   if (SizeInRegs == 0)
7618     return false;
7619 
7620   if (SizeInRegs > State.FreeRegs) {
7621     State.FreeRegs = 0;
7622     return false;
7623   }
7624 
7625   State.FreeRegs -= SizeInRegs;
7626 
7627   return true;
7628 }
7629 
7630 ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal,
7631                                            CCState &State) const {
7632   if (!ByVal) {
7633     if (State.FreeRegs) {
7634       --State.FreeRegs; // Non-byval indirects just use one pointer.
7635       return getNaturalAlignIndirectInReg(Ty);
7636     }
7637     return getNaturalAlignIndirect(Ty, false);
7638   }
7639 
7640   // Compute the byval alignment.
7641   const unsigned MinABIStackAlignInBytes = 4;
7642   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
7643   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
7644                                  /*Realign=*/TypeAlign >
7645                                      MinABIStackAlignInBytes);
7646 }
7647 
7648 ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty,
7649                                               CCState &State) const {
7650   // Check with the C++ ABI first.
7651   const RecordType *RT = Ty->getAs<RecordType>();
7652   if (RT) {
7653     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
7654     if (RAA == CGCXXABI::RAA_Indirect) {
7655       return getIndirectResult(Ty, /*ByVal=*/false, State);
7656     } else if (RAA == CGCXXABI::RAA_DirectInMemory) {
7657       return getNaturalAlignIndirect(Ty, /*ByRef=*/true);
7658     }
7659   }
7660 
7661   if (isAggregateTypeForABI(Ty)) {
7662     // Structures with flexible arrays are always indirect.
7663     if (RT && RT->getDecl()->hasFlexibleArrayMember())
7664       return getIndirectResult(Ty, /*ByVal=*/true, State);
7665 
7666     // Ignore empty structs/unions.
7667     if (isEmptyRecord(getContext(), Ty, true))
7668       return ABIArgInfo::getIgnore();
7669 
7670     llvm::LLVMContext &LLVMContext = getVMContext();
7671     unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
7672     if (SizeInRegs <= State.FreeRegs) {
7673       llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
7674       SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
7675       llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
7676       State.FreeRegs -= SizeInRegs;
7677       return ABIArgInfo::getDirectInReg(Result);
7678     } else {
7679       State.FreeRegs = 0;
7680     }
7681     return getIndirectResult(Ty, true, State);
7682   }
7683 
7684   // Treat an enum type as its underlying type.
7685   if (const auto *EnumTy = Ty->getAs<EnumType>())
7686     Ty = EnumTy->getDecl()->getIntegerType();
7687 
7688   bool InReg = shouldUseInReg(Ty, State);
7689   if (Ty->isPromotableIntegerType()) {
7690     if (InReg)
7691       return ABIArgInfo::getDirectInReg();
7692     return ABIArgInfo::getExtend(Ty);
7693   }
7694   if (InReg)
7695     return ABIArgInfo::getDirectInReg();
7696   return ABIArgInfo::getDirect();
7697 }
7698 
7699 namespace {
7700 class LanaiTargetCodeGenInfo : public TargetCodeGenInfo {
7701 public:
7702   LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
7703       : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {}
7704 };
7705 }
7706 
7707 //===----------------------------------------------------------------------===//
7708 // AMDGPU ABI Implementation
7709 //===----------------------------------------------------------------------===//
7710 
7711 namespace {
7712 
7713 class AMDGPUABIInfo final : public DefaultABIInfo {
7714 private:
7715   static const unsigned MaxNumRegsForArgsRet = 16;
7716 
7717   unsigned numRegsForType(QualType Ty) const;
7718 
7719   bool isHomogeneousAggregateBaseType(QualType Ty) const override;
7720   bool isHomogeneousAggregateSmallEnough(const Type *Base,
7721                                          uint64_t Members) const override;
7722 
7723   // Coerce HIP pointer arguments from generic pointers to global ones.
7724   llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
7725                                        unsigned ToAS) const {
7726     // Structure types.
7727     if (auto STy = dyn_cast<llvm::StructType>(Ty)) {
7728       SmallVector<llvm::Type *, 8> EltTys;
7729       bool Changed = false;
7730       for (auto T : STy->elements()) {
7731         auto NT = coerceKernelArgumentType(T, FromAS, ToAS);
7732         EltTys.push_back(NT);
7733         Changed |= (NT != T);
7734       }
7735       // Skip if there is no change in element types.
7736       if (!Changed)
7737         return STy;
7738       if (STy->hasName())
7739         return llvm::StructType::create(
7740             EltTys, (STy->getName() + ".coerce").str(), STy->isPacked());
7741       return llvm::StructType::get(getVMContext(), EltTys, STy->isPacked());
7742     }
7743     // Arrary types.
7744     if (auto ATy = dyn_cast<llvm::ArrayType>(Ty)) {
7745       auto T = ATy->getElementType();
7746       auto NT = coerceKernelArgumentType(T, FromAS, ToAS);
7747       // Skip if there is no change in that element type.
7748       if (NT == T)
7749         return ATy;
7750       return llvm::ArrayType::get(NT, ATy->getNumElements());
7751     }
7752     // Single value types.
7753     if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS)
7754       return llvm::PointerType::get(
7755           cast<llvm::PointerType>(Ty)->getElementType(), ToAS);
7756     return Ty;
7757   }
7758 
7759 public:
7760   explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
7761     DefaultABIInfo(CGT) {}
7762 
7763   ABIArgInfo classifyReturnType(QualType RetTy) const;
7764   ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
7765   ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const;
7766 
7767   void computeInfo(CGFunctionInfo &FI) const override;
7768   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7769                     QualType Ty) const override;
7770 };
7771 
7772 bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
7773   return true;
7774 }
7775 
7776 bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
7777   const Type *Base, uint64_t Members) const {
7778   uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
7779 
7780   // Homogeneous Aggregates may occupy at most 16 registers.
7781   return Members * NumRegs <= MaxNumRegsForArgsRet;
7782 }
7783 
7784 /// Estimate number of registers the type will use when passed in registers.
7785 unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
7786   unsigned NumRegs = 0;
7787 
7788   if (const VectorType *VT = Ty->getAs<VectorType>()) {
7789     // Compute from the number of elements. The reported size is based on the
7790     // in-memory size, which includes the padding 4th element for 3-vectors.
7791     QualType EltTy = VT->getElementType();
7792     unsigned EltSize = getContext().getTypeSize(EltTy);
7793 
7794     // 16-bit element vectors should be passed as packed.
7795     if (EltSize == 16)
7796       return (VT->getNumElements() + 1) / 2;
7797 
7798     unsigned EltNumRegs = (EltSize + 31) / 32;
7799     return EltNumRegs * VT->getNumElements();
7800   }
7801 
7802   if (const RecordType *RT = Ty->getAs<RecordType>()) {
7803     const RecordDecl *RD = RT->getDecl();
7804     assert(!RD->hasFlexibleArrayMember());
7805 
7806     for (const FieldDecl *Field : RD->fields()) {
7807       QualType FieldTy = Field->getType();
7808       NumRegs += numRegsForType(FieldTy);
7809     }
7810 
7811     return NumRegs;
7812   }
7813 
7814   return (getContext().getTypeSize(Ty) + 31) / 32;
7815 }
7816 
7817 void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
7818   llvm::CallingConv::ID CC = FI.getCallingConvention();
7819 
7820   if (!getCXXABI().classifyReturnType(FI))
7821     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
7822 
7823   unsigned NumRegsLeft = MaxNumRegsForArgsRet;
7824   for (auto &Arg : FI.arguments()) {
7825     if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
7826       Arg.info = classifyKernelArgumentType(Arg.type);
7827     } else {
7828       Arg.info = classifyArgumentType(Arg.type, NumRegsLeft);
7829     }
7830   }
7831 }
7832 
7833 Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
7834                                  QualType Ty) const {
7835   llvm_unreachable("AMDGPU does not support varargs");
7836 }
7837 
7838 ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
7839   if (isAggregateTypeForABI(RetTy)) {
7840     // Records with non-trivial destructors/copy-constructors should not be
7841     // returned by value.
7842     if (!getRecordArgABI(RetTy, getCXXABI())) {
7843       // Ignore empty structs/unions.
7844       if (isEmptyRecord(getContext(), RetTy, true))
7845         return ABIArgInfo::getIgnore();
7846 
7847       // Lower single-element structs to just return a regular value.
7848       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
7849         return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7850 
7851       if (const RecordType *RT = RetTy->getAs<RecordType>()) {
7852         const RecordDecl *RD = RT->getDecl();
7853         if (RD->hasFlexibleArrayMember())
7854           return DefaultABIInfo::classifyReturnType(RetTy);
7855       }
7856 
7857       // Pack aggregates <= 4 bytes into single VGPR or pair.
7858       uint64_t Size = getContext().getTypeSize(RetTy);
7859       if (Size <= 16)
7860         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7861 
7862       if (Size <= 32)
7863         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7864 
7865       if (Size <= 64) {
7866         llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7867         return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7868       }
7869 
7870       if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
7871         return ABIArgInfo::getDirect();
7872     }
7873   }
7874 
7875   // Otherwise just do the default thing.
7876   return DefaultABIInfo::classifyReturnType(RetTy);
7877 }
7878 
7879 /// For kernels all parameters are really passed in a special buffer. It doesn't
7880 /// make sense to pass anything byval, so everything must be direct.
7881 ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
7882   Ty = useFirstFieldIfTransparentUnion(Ty);
7883 
7884   // TODO: Can we omit empty structs?
7885 
7886   llvm::Type *LTy = nullptr;
7887   if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7888     LTy = CGT.ConvertType(QualType(SeltTy, 0));
7889 
7890   if (getContext().getLangOpts().HIP) {
7891     if (!LTy)
7892       LTy = CGT.ConvertType(Ty);
7893     LTy = coerceKernelArgumentType(
7894         LTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
7895         /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
7896   }
7897 
7898   // If we set CanBeFlattened to true, CodeGen will expand the struct to its
7899   // individual elements, which confuses the Clover OpenCL backend; therefore we
7900   // have to set it to false here. Other args of getDirect() are just defaults.
7901   return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
7902 }
7903 
7904 ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty,
7905                                                unsigned &NumRegsLeft) const {
7906   assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
7907 
7908   Ty = useFirstFieldIfTransparentUnion(Ty);
7909 
7910   if (isAggregateTypeForABI(Ty)) {
7911     // Records with non-trivial destructors/copy-constructors should not be
7912     // passed by value.
7913     if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
7914       return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
7915 
7916     // Ignore empty structs/unions.
7917     if (isEmptyRecord(getContext(), Ty, true))
7918       return ABIArgInfo::getIgnore();
7919 
7920     // Lower single-element structs to just pass a regular value. TODO: We
7921     // could do reasonable-size multiple-element structs too, using getExpand(),
7922     // though watch out for things like bitfields.
7923     if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
7924       return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
7925 
7926     if (const RecordType *RT = Ty->getAs<RecordType>()) {
7927       const RecordDecl *RD = RT->getDecl();
7928       if (RD->hasFlexibleArrayMember())
7929         return DefaultABIInfo::classifyArgumentType(Ty);
7930     }
7931 
7932     // Pack aggregates <= 8 bytes into single VGPR or pair.
7933     uint64_t Size = getContext().getTypeSize(Ty);
7934     if (Size <= 64) {
7935       unsigned NumRegs = (Size + 31) / 32;
7936       NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
7937 
7938       if (Size <= 16)
7939         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
7940 
7941       if (Size <= 32)
7942         return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
7943 
7944       // XXX: Should this be i64 instead, and should the limit increase?
7945       llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
7946       return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
7947     }
7948 
7949     if (NumRegsLeft > 0) {
7950       unsigned NumRegs = numRegsForType(Ty);
7951       if (NumRegsLeft >= NumRegs) {
7952         NumRegsLeft -= NumRegs;
7953         return ABIArgInfo::getDirect();
7954       }
7955     }
7956   }
7957 
7958   // Otherwise just do the default thing.
7959   ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
7960   if (!ArgInfo.isIndirect()) {
7961     unsigned NumRegs = numRegsForType(Ty);
7962     NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
7963   }
7964 
7965   return ArgInfo;
7966 }
7967 
7968 class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
7969 public:
7970   AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
7971     : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {}
7972   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
7973                            CodeGen::CodeGenModule &M) const override;
7974   unsigned getOpenCLKernelCallingConv() const override;
7975 
7976   llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
7977       llvm::PointerType *T, QualType QT) const override;
7978 
7979   LangAS getASTAllocaAddressSpace() const override {
7980     return getLangASFromTargetAS(
7981         getABIInfo().getDataLayout().getAllocaAddrSpace());
7982   }
7983   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
7984                                   const VarDecl *D) const override;
7985   llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
7986                                          SyncScope Scope,
7987                                          llvm::AtomicOrdering Ordering,
7988                                          llvm::LLVMContext &Ctx) const override;
7989   llvm::Function *
7990   createEnqueuedBlockKernel(CodeGenFunction &CGF,
7991                             llvm::Function *BlockInvokeFunc,
7992                             llvm::Value *BlockLiteral) const override;
7993   bool shouldEmitStaticExternCAliases() const override;
7994   void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
7995 };
7996 }
7997 
7998 static bool requiresAMDGPUProtectedVisibility(const Decl *D,
7999                                               llvm::GlobalValue *GV) {
8000   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
8001     return false;
8002 
8003   return D->hasAttr<OpenCLKernelAttr>() ||
8004          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
8005          (isa<VarDecl>(D) &&
8006           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
8007            D->hasAttr<HIPPinnedShadowAttr>()));
8008 }
8009 
8010 static bool requiresAMDGPUDefaultVisibility(const Decl *D,
8011                                             llvm::GlobalValue *GV) {
8012   if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
8013     return false;
8014 
8015   return isa<VarDecl>(D) && D->hasAttr<HIPPinnedShadowAttr>();
8016 }
8017 
8018 void AMDGPUTargetCodeGenInfo::setTargetAttributes(
8019     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
8020   if (requiresAMDGPUDefaultVisibility(D, GV)) {
8021     GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
8022     GV->setDSOLocal(false);
8023   } else if (requiresAMDGPUProtectedVisibility(D, GV)) {
8024     GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
8025     GV->setDSOLocal(true);
8026   }
8027 
8028   if (GV->isDeclaration())
8029     return;
8030   const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
8031   if (!FD)
8032     return;
8033 
8034   llvm::Function *F = cast<llvm::Function>(GV);
8035 
8036   const auto *ReqdWGS = M.getLangOpts().OpenCL ?
8037     FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
8038 
8039 
8040   const bool IsOpenCLKernel = M.getLangOpts().OpenCL &&
8041                               FD->hasAttr<OpenCLKernelAttr>();
8042   const bool IsHIPKernel = M.getLangOpts().HIP &&
8043                            FD->hasAttr<CUDAGlobalAttr>();
8044   if ((IsOpenCLKernel || IsHIPKernel) &&
8045       (M.getTriple().getOS() == llvm::Triple::AMDHSA))
8046     F->addFnAttr("amdgpu-implicitarg-num-bytes", "56");
8047 
8048   const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
8049   if (ReqdWGS || FlatWGS) {
8050     unsigned Min = 0;
8051     unsigned Max = 0;
8052     if (FlatWGS) {
8053       Min = FlatWGS->getMin()
8054                 ->EvaluateKnownConstInt(M.getContext())
8055                 .getExtValue();
8056       Max = FlatWGS->getMax()
8057                 ->EvaluateKnownConstInt(M.getContext())
8058                 .getExtValue();
8059     }
8060     if (ReqdWGS && Min == 0 && Max == 0)
8061       Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
8062 
8063     if (Min != 0) {
8064       assert(Min <= Max && "Min must be less than or equal Max");
8065 
8066       std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
8067       F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
8068     } else
8069       assert(Max == 0 && "Max must be zero");
8070   } else if (IsOpenCLKernel || IsHIPKernel) {
8071     // By default, restrict the maximum size to 256.
8072     F->addFnAttr("amdgpu-flat-work-group-size", "1,256");
8073   }
8074 
8075   if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
8076     unsigned Min =
8077         Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue();
8078     unsigned Max = Attr->getMax() ? Attr->getMax()
8079                                         ->EvaluateKnownConstInt(M.getContext())
8080                                         .getExtValue()
8081                                   : 0;
8082 
8083     if (Min != 0) {
8084       assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
8085 
8086       std::string AttrVal = llvm::utostr(Min);
8087       if (Max != 0)
8088         AttrVal = AttrVal + "," + llvm::utostr(Max);
8089       F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
8090     } else
8091       assert(Max == 0 && "Max must be zero");
8092   }
8093 
8094   if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
8095     unsigned NumSGPR = Attr->getNumSGPR();
8096 
8097     if (NumSGPR != 0)
8098       F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
8099   }
8100 
8101   if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
8102     uint32_t NumVGPR = Attr->getNumVGPR();
8103 
8104     if (NumVGPR != 0)
8105       F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
8106   }
8107 }
8108 
8109 unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
8110   return llvm::CallingConv::AMDGPU_KERNEL;
8111 }
8112 
8113 // Currently LLVM assumes null pointers always have value 0,
8114 // which results in incorrectly transformed IR. Therefore, instead of
8115 // emitting null pointers in private and local address spaces, a null
8116 // pointer in generic address space is emitted which is casted to a
8117 // pointer in local or private address space.
8118 llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
8119     const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
8120     QualType QT) const {
8121   if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
8122     return llvm::ConstantPointerNull::get(PT);
8123 
8124   auto &Ctx = CGM.getContext();
8125   auto NPT = llvm::PointerType::get(PT->getElementType(),
8126       Ctx.getTargetAddressSpace(LangAS::opencl_generic));
8127   return llvm::ConstantExpr::getAddrSpaceCast(
8128       llvm::ConstantPointerNull::get(NPT), PT);
8129 }
8130 
8131 LangAS
8132 AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
8133                                                   const VarDecl *D) const {
8134   assert(!CGM.getLangOpts().OpenCL &&
8135          !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
8136          "Address space agnostic languages only");
8137   LangAS DefaultGlobalAS = getLangASFromTargetAS(
8138       CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
8139   if (!D)
8140     return DefaultGlobalAS;
8141 
8142   LangAS AddrSpace = D->getType().getAddressSpace();
8143   assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace));
8144   if (AddrSpace != LangAS::Default)
8145     return AddrSpace;
8146 
8147   if (CGM.isTypeConstant(D->getType(), false)) {
8148     if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
8149       return ConstAS.getValue();
8150   }
8151   return DefaultGlobalAS;
8152 }
8153 
8154 llvm::SyncScope::ID
8155 AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
8156                                             SyncScope Scope,
8157                                             llvm::AtomicOrdering Ordering,
8158                                             llvm::LLVMContext &Ctx) const {
8159   std::string Name;
8160   switch (Scope) {
8161   case SyncScope::OpenCLWorkGroup:
8162     Name = "workgroup";
8163     break;
8164   case SyncScope::OpenCLDevice:
8165     Name = "agent";
8166     break;
8167   case SyncScope::OpenCLAllSVMDevices:
8168     Name = "";
8169     break;
8170   case SyncScope::OpenCLSubGroup:
8171     Name = "wavefront";
8172   }
8173 
8174   if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) {
8175     if (!Name.empty())
8176       Name = Twine(Twine(Name) + Twine("-")).str();
8177 
8178     Name = Twine(Twine(Name) + Twine("one-as")).str();
8179   }
8180 
8181   return Ctx.getOrInsertSyncScopeID(Name);
8182 }
8183 
8184 bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
8185   return false;
8186 }
8187 
8188 void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
8189     const FunctionType *&FT) const {
8190   FT = getABIInfo().getContext().adjustFunctionType(
8191       FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel));
8192 }
8193 
8194 //===----------------------------------------------------------------------===//
8195 // SPARC v8 ABI Implementation.
8196 // Based on the SPARC Compliance Definition version 2.4.1.
8197 //
8198 // Ensures that complex values are passed in registers.
8199 //
8200 namespace {
8201 class SparcV8ABIInfo : public DefaultABIInfo {
8202 public:
8203   SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8204 
8205 private:
8206   ABIArgInfo classifyReturnType(QualType RetTy) const;
8207   void computeInfo(CGFunctionInfo &FI) const override;
8208 };
8209 } // end anonymous namespace
8210 
8211 
8212 ABIArgInfo
8213 SparcV8ABIInfo::classifyReturnType(QualType Ty) const {
8214   if (Ty->isAnyComplexType()) {
8215     return ABIArgInfo::getDirect();
8216   }
8217   else {
8218     return DefaultABIInfo::classifyReturnType(Ty);
8219   }
8220 }
8221 
8222 void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const {
8223 
8224   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8225   for (auto &Arg : FI.arguments())
8226     Arg.info = classifyArgumentType(Arg.type);
8227 }
8228 
8229 namespace {
8230 class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo {
8231 public:
8232   SparcV8TargetCodeGenInfo(CodeGenTypes &CGT)
8233     : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {}
8234 };
8235 } // end anonymous namespace
8236 
8237 //===----------------------------------------------------------------------===//
8238 // SPARC v9 ABI Implementation.
8239 // Based on the SPARC Compliance Definition version 2.4.1.
8240 //
8241 // Function arguments a mapped to a nominal "parameter array" and promoted to
8242 // registers depending on their type. Each argument occupies 8 or 16 bytes in
8243 // the array, structs larger than 16 bytes are passed indirectly.
8244 //
8245 // One case requires special care:
8246 //
8247 //   struct mixed {
8248 //     int i;
8249 //     float f;
8250 //   };
8251 //
8252 // When a struct mixed is passed by value, it only occupies 8 bytes in the
8253 // parameter array, but the int is passed in an integer register, and the float
8254 // is passed in a floating point register. This is represented as two arguments
8255 // with the LLVM IR inreg attribute:
8256 //
8257 //   declare void f(i32 inreg %i, float inreg %f)
8258 //
8259 // The code generator will only allocate 4 bytes from the parameter array for
8260 // the inreg arguments. All other arguments are allocated a multiple of 8
8261 // bytes.
8262 //
8263 namespace {
8264 class SparcV9ABIInfo : public ABIInfo {
8265 public:
8266   SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
8267 
8268 private:
8269   ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const;
8270   void computeInfo(CGFunctionInfo &FI) const override;
8271   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8272                     QualType Ty) const override;
8273 
8274   // Coercion type builder for structs passed in registers. The coercion type
8275   // serves two purposes:
8276   //
8277   // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned'
8278   //    in registers.
8279   // 2. Expose aligned floating point elements as first-level elements, so the
8280   //    code generator knows to pass them in floating point registers.
8281   //
8282   // We also compute the InReg flag which indicates that the struct contains
8283   // aligned 32-bit floats.
8284   //
8285   struct CoerceBuilder {
8286     llvm::LLVMContext &Context;
8287     const llvm::DataLayout &DL;
8288     SmallVector<llvm::Type*, 8> Elems;
8289     uint64_t Size;
8290     bool InReg;
8291 
8292     CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl)
8293       : Context(c), DL(dl), Size(0), InReg(false) {}
8294 
8295     // Pad Elems with integers until Size is ToSize.
8296     void pad(uint64_t ToSize) {
8297       assert(ToSize >= Size && "Cannot remove elements");
8298       if (ToSize == Size)
8299         return;
8300 
8301       // Finish the current 64-bit word.
8302       uint64_t Aligned = llvm::alignTo(Size, 64);
8303       if (Aligned > Size && Aligned <= ToSize) {
8304         Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size));
8305         Size = Aligned;
8306       }
8307 
8308       // Add whole 64-bit words.
8309       while (Size + 64 <= ToSize) {
8310         Elems.push_back(llvm::Type::getInt64Ty(Context));
8311         Size += 64;
8312       }
8313 
8314       // Final in-word padding.
8315       if (Size < ToSize) {
8316         Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size));
8317         Size = ToSize;
8318       }
8319     }
8320 
8321     // Add a floating point element at Offset.
8322     void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) {
8323       // Unaligned floats are treated as integers.
8324       if (Offset % Bits)
8325         return;
8326       // The InReg flag is only required if there are any floats < 64 bits.
8327       if (Bits < 64)
8328         InReg = true;
8329       pad(Offset);
8330       Elems.push_back(Ty);
8331       Size = Offset + Bits;
8332     }
8333 
8334     // Add a struct type to the coercion type, starting at Offset (in bits).
8335     void addStruct(uint64_t Offset, llvm::StructType *StrTy) {
8336       const llvm::StructLayout *Layout = DL.getStructLayout(StrTy);
8337       for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) {
8338         llvm::Type *ElemTy = StrTy->getElementType(i);
8339         uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i);
8340         switch (ElemTy->getTypeID()) {
8341         case llvm::Type::StructTyID:
8342           addStruct(ElemOffset, cast<llvm::StructType>(ElemTy));
8343           break;
8344         case llvm::Type::FloatTyID:
8345           addFloat(ElemOffset, ElemTy, 32);
8346           break;
8347         case llvm::Type::DoubleTyID:
8348           addFloat(ElemOffset, ElemTy, 64);
8349           break;
8350         case llvm::Type::FP128TyID:
8351           addFloat(ElemOffset, ElemTy, 128);
8352           break;
8353         case llvm::Type::PointerTyID:
8354           if (ElemOffset % 64 == 0) {
8355             pad(ElemOffset);
8356             Elems.push_back(ElemTy);
8357             Size += 64;
8358           }
8359           break;
8360         default:
8361           break;
8362         }
8363       }
8364     }
8365 
8366     // Check if Ty is a usable substitute for the coercion type.
8367     bool isUsableType(llvm::StructType *Ty) const {
8368       return llvm::makeArrayRef(Elems) == Ty->elements();
8369     }
8370 
8371     // Get the coercion type as a literal struct type.
8372     llvm::Type *getType() const {
8373       if (Elems.size() == 1)
8374         return Elems.front();
8375       else
8376         return llvm::StructType::get(Context, Elems);
8377     }
8378   };
8379 };
8380 } // end anonymous namespace
8381 
8382 ABIArgInfo
8383 SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const {
8384   if (Ty->isVoidType())
8385     return ABIArgInfo::getIgnore();
8386 
8387   uint64_t Size = getContext().getTypeSize(Ty);
8388 
8389   // Anything too big to fit in registers is passed with an explicit indirect
8390   // pointer / sret pointer.
8391   if (Size > SizeLimit)
8392     return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
8393 
8394   // Treat an enum type as its underlying type.
8395   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8396     Ty = EnumTy->getDecl()->getIntegerType();
8397 
8398   // Integer types smaller than a register are extended.
8399   if (Size < 64 && Ty->isIntegerType())
8400     return ABIArgInfo::getExtend(Ty);
8401 
8402   // Other non-aggregates go in registers.
8403   if (!isAggregateTypeForABI(Ty))
8404     return ABIArgInfo::getDirect();
8405 
8406   // If a C++ object has either a non-trivial copy constructor or a non-trivial
8407   // destructor, it is passed with an explicit indirect pointer / sret pointer.
8408   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI()))
8409     return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
8410 
8411   // This is a small aggregate type that should be passed in registers.
8412   // Build a coercion type from the LLVM struct type.
8413   llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty));
8414   if (!StrTy)
8415     return ABIArgInfo::getDirect();
8416 
8417   CoerceBuilder CB(getVMContext(), getDataLayout());
8418   CB.addStruct(0, StrTy);
8419   CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
8420 
8421   // Try to use the original type for coercion.
8422   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
8423 
8424   if (CB.InReg)
8425     return ABIArgInfo::getDirectInReg(CoerceTy);
8426   else
8427     return ABIArgInfo::getDirect(CoerceTy);
8428 }
8429 
8430 Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8431                                   QualType Ty) const {
8432   ABIArgInfo AI = classifyType(Ty, 16 * 8);
8433   llvm::Type *ArgTy = CGT.ConvertType(Ty);
8434   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
8435     AI.setCoerceToType(ArgTy);
8436 
8437   CharUnits SlotSize = CharUnits::fromQuantity(8);
8438 
8439   CGBuilderTy &Builder = CGF.Builder;
8440   Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize);
8441   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
8442 
8443   auto TypeInfo = getContext().getTypeInfoInChars(Ty);
8444 
8445   Address ArgAddr = Address::invalid();
8446   CharUnits Stride;
8447   switch (AI.getKind()) {
8448   case ABIArgInfo::Expand:
8449   case ABIArgInfo::CoerceAndExpand:
8450   case ABIArgInfo::InAlloca:
8451     llvm_unreachable("Unsupported ABI kind for va_arg");
8452 
8453   case ABIArgInfo::Extend: {
8454     Stride = SlotSize;
8455     CharUnits Offset = SlotSize - TypeInfo.first;
8456     ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend");
8457     break;
8458   }
8459 
8460   case ABIArgInfo::Direct: {
8461     auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType());
8462     Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize);
8463     ArgAddr = Addr;
8464     break;
8465   }
8466 
8467   case ABIArgInfo::Indirect:
8468     Stride = SlotSize;
8469     ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect");
8470     ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"),
8471                       TypeInfo.second);
8472     break;
8473 
8474   case ABIArgInfo::Ignore:
8475     return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second);
8476   }
8477 
8478   // Update VAList.
8479   Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next");
8480   Builder.CreateStore(NextPtr.getPointer(), VAListAddr);
8481 
8482   return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr");
8483 }
8484 
8485 void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const {
8486   FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8);
8487   for (auto &I : FI.arguments())
8488     I.info = classifyType(I.type, 16 * 8);
8489 }
8490 
8491 namespace {
8492 class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo {
8493 public:
8494   SparcV9TargetCodeGenInfo(CodeGenTypes &CGT)
8495     : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {}
8496 
8497   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override {
8498     return 14;
8499   }
8500 
8501   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8502                                llvm::Value *Address) const override;
8503 };
8504 } // end anonymous namespace
8505 
8506 bool
8507 SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
8508                                                 llvm::Value *Address) const {
8509   // This is calculated from the LLVM and GCC tables and verified
8510   // against gcc output.  AFAIK all ABIs use the same encoding.
8511 
8512   CodeGen::CGBuilderTy &Builder = CGF.Builder;
8513 
8514   llvm::IntegerType *i8 = CGF.Int8Ty;
8515   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
8516   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
8517 
8518   // 0-31: the 8-byte general-purpose registers
8519   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
8520 
8521   // 32-63: f0-31, the 4-byte floating-point registers
8522   AssignToArrayRange(Builder, Address, Four8, 32, 63);
8523 
8524   //   Y   = 64
8525   //   PSR = 65
8526   //   WIM = 66
8527   //   TBR = 67
8528   //   PC  = 68
8529   //   NPC = 69
8530   //   FSR = 70
8531   //   CSR = 71
8532   AssignToArrayRange(Builder, Address, Eight8, 64, 71);
8533 
8534   // 72-87: d0-15, the 8-byte floating-point registers
8535   AssignToArrayRange(Builder, Address, Eight8, 72, 87);
8536 
8537   return false;
8538 }
8539 
8540 // ARC ABI implementation.
8541 namespace {
8542 
8543 class ARCABIInfo : public DefaultABIInfo {
8544 public:
8545   using DefaultABIInfo::DefaultABIInfo;
8546 
8547 private:
8548   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8549                     QualType Ty) const override;
8550 
8551   void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const {
8552     if (!State.FreeRegs)
8553       return;
8554     if (Info.isIndirect() && Info.getInReg())
8555       State.FreeRegs--;
8556     else if (Info.isDirect() && Info.getInReg()) {
8557       unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32;
8558       if (sz < State.FreeRegs)
8559         State.FreeRegs -= sz;
8560       else
8561         State.FreeRegs = 0;
8562     }
8563   }
8564 
8565   void computeInfo(CGFunctionInfo &FI) const override {
8566     CCState State(FI.getCallingConvention());
8567     // ARC uses 8 registers to pass arguments.
8568     State.FreeRegs = 8;
8569 
8570     if (!getCXXABI().classifyReturnType(FI))
8571       FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
8572     updateState(FI.getReturnInfo(), FI.getReturnType(), State);
8573     for (auto &I : FI.arguments()) {
8574       I.info = classifyArgumentType(I.type, State.FreeRegs);
8575       updateState(I.info, I.type, State);
8576     }
8577   }
8578 
8579   ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const;
8580   ABIArgInfo getIndirectByValue(QualType Ty) const;
8581   ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const;
8582   ABIArgInfo classifyReturnType(QualType RetTy) const;
8583 };
8584 
8585 class ARCTargetCodeGenInfo : public TargetCodeGenInfo {
8586 public:
8587   ARCTargetCodeGenInfo(CodeGenTypes &CGT)
8588       : TargetCodeGenInfo(new ARCABIInfo(CGT)) {}
8589 };
8590 
8591 
8592 ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const {
8593   return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) :
8594                        getNaturalAlignIndirect(Ty, false);
8595 }
8596 
8597 ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const {
8598   // Compute the byval alignment.
8599   const unsigned MinABIStackAlignInBytes = 4;
8600   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
8601   return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true,
8602                                  TypeAlign > MinABIStackAlignInBytes);
8603 }
8604 
8605 Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8606                               QualType Ty) const {
8607   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false,
8608                           getContext().getTypeInfoInChars(Ty),
8609                           CharUnits::fromQuantity(4), true);
8610 }
8611 
8612 ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty,
8613                                             uint8_t FreeRegs) const {
8614   // Handle the generic C++ ABI.
8615   const RecordType *RT = Ty->getAs<RecordType>();
8616   if (RT) {
8617     CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI());
8618     if (RAA == CGCXXABI::RAA_Indirect)
8619       return getIndirectByRef(Ty, FreeRegs > 0);
8620 
8621     if (RAA == CGCXXABI::RAA_DirectInMemory)
8622       return getIndirectByValue(Ty);
8623   }
8624 
8625   // Treat an enum type as its underlying type.
8626   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
8627     Ty = EnumTy->getDecl()->getIntegerType();
8628 
8629   auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32;
8630 
8631   if (isAggregateTypeForABI(Ty)) {
8632     // Structures with flexible arrays are always indirect.
8633     if (RT && RT->getDecl()->hasFlexibleArrayMember())
8634       return getIndirectByValue(Ty);
8635 
8636     // Ignore empty structs/unions.
8637     if (isEmptyRecord(getContext(), Ty, true))
8638       return ABIArgInfo::getIgnore();
8639 
8640     llvm::LLVMContext &LLVMContext = getVMContext();
8641 
8642     llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext);
8643     SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32);
8644     llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
8645 
8646     return FreeRegs >= SizeInRegs ?
8647         ABIArgInfo::getDirectInReg(Result) :
8648         ABIArgInfo::getDirect(Result, 0, nullptr, false);
8649   }
8650 
8651   return Ty->isPromotableIntegerType() ?
8652       (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) :
8653                                 ABIArgInfo::getExtend(Ty)) :
8654       (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() :
8655                                 ABIArgInfo::getDirect());
8656 }
8657 
8658 ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const {
8659   if (RetTy->isAnyComplexType())
8660     return ABIArgInfo::getDirectInReg();
8661 
8662   // Arguments of size > 4 registers are indirect.
8663   auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32;
8664   if (RetSize > 4)
8665     return getIndirectByRef(RetTy, /*HasFreeRegs*/ true);
8666 
8667   return DefaultABIInfo::classifyReturnType(RetTy);
8668 }
8669 
8670 } // End anonymous namespace.
8671 
8672 //===----------------------------------------------------------------------===//
8673 // XCore ABI Implementation
8674 //===----------------------------------------------------------------------===//
8675 
8676 namespace {
8677 
8678 /// A SmallStringEnc instance is used to build up the TypeString by passing
8679 /// it by reference between functions that append to it.
8680 typedef llvm::SmallString<128> SmallStringEnc;
8681 
8682 /// TypeStringCache caches the meta encodings of Types.
8683 ///
8684 /// The reason for caching TypeStrings is two fold:
8685 ///   1. To cache a type's encoding for later uses;
8686 ///   2. As a means to break recursive member type inclusion.
8687 ///
8688 /// A cache Entry can have a Status of:
8689 ///   NonRecursive:   The type encoding is not recursive;
8690 ///   Recursive:      The type encoding is recursive;
8691 ///   Incomplete:     An incomplete TypeString;
8692 ///   IncompleteUsed: An incomplete TypeString that has been used in a
8693 ///                   Recursive type encoding.
8694 ///
8695 /// A NonRecursive entry will have all of its sub-members expanded as fully
8696 /// as possible. Whilst it may contain types which are recursive, the type
8697 /// itself is not recursive and thus its encoding may be safely used whenever
8698 /// the type is encountered.
8699 ///
8700 /// A Recursive entry will have all of its sub-members expanded as fully as
8701 /// possible. The type itself is recursive and it may contain other types which
8702 /// are recursive. The Recursive encoding must not be used during the expansion
8703 /// of a recursive type's recursive branch. For simplicity the code uses
8704 /// IncompleteCount to reject all usage of Recursive encodings for member types.
8705 ///
8706 /// An Incomplete entry is always a RecordType and only encodes its
8707 /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and
8708 /// are placed into the cache during type expansion as a means to identify and
8709 /// handle recursive inclusion of types as sub-members. If there is recursion
8710 /// the entry becomes IncompleteUsed.
8711 ///
8712 /// During the expansion of a RecordType's members:
8713 ///
8714 ///   If the cache contains a NonRecursive encoding for the member type, the
8715 ///   cached encoding is used;
8716 ///
8717 ///   If the cache contains a Recursive encoding for the member type, the
8718 ///   cached encoding is 'Swapped' out, as it may be incorrect, and...
8719 ///
8720 ///   If the member is a RecordType, an Incomplete encoding is placed into the
8721 ///   cache to break potential recursive inclusion of itself as a sub-member;
8722 ///
8723 ///   Once a member RecordType has been expanded, its temporary incomplete
8724 ///   entry is removed from the cache. If a Recursive encoding was swapped out
8725 ///   it is swapped back in;
8726 ///
8727 ///   If an incomplete entry is used to expand a sub-member, the incomplete
8728 ///   entry is marked as IncompleteUsed. The cache keeps count of how many
8729 ///   IncompleteUsed entries it currently contains in IncompleteUsedCount;
8730 ///
8731 ///   If a member's encoding is found to be a NonRecursive or Recursive viz:
8732 ///   IncompleteUsedCount==0, the member's encoding is added to the cache.
8733 ///   Else the member is part of a recursive type and thus the recursion has
8734 ///   been exited too soon for the encoding to be correct for the member.
8735 ///
8736 class TypeStringCache {
8737   enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed};
8738   struct Entry {
8739     std::string Str;     // The encoded TypeString for the type.
8740     enum Status State;   // Information about the encoding in 'Str'.
8741     std::string Swapped; // A temporary place holder for a Recursive encoding
8742                          // during the expansion of RecordType's members.
8743   };
8744   std::map<const IdentifierInfo *, struct Entry> Map;
8745   unsigned IncompleteCount;     // Number of Incomplete entries in the Map.
8746   unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map.
8747 public:
8748   TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}
8749   void addIncomplete(const IdentifierInfo *ID, std::string StubEnc);
8750   bool removeIncomplete(const IdentifierInfo *ID);
8751   void addIfComplete(const IdentifierInfo *ID, StringRef Str,
8752                      bool IsRecursive);
8753   StringRef lookupStr(const IdentifierInfo *ID);
8754 };
8755 
8756 /// TypeString encodings for enum & union fields must be order.
8757 /// FieldEncoding is a helper for this ordering process.
8758 class FieldEncoding {
8759   bool HasName;
8760   std::string Enc;
8761 public:
8762   FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}
8763   StringRef str() { return Enc; }
8764   bool operator<(const FieldEncoding &rhs) const {
8765     if (HasName != rhs.HasName) return HasName;
8766     return Enc < rhs.Enc;
8767   }
8768 };
8769 
8770 class XCoreABIInfo : public DefaultABIInfo {
8771 public:
8772   XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
8773   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8774                     QualType Ty) const override;
8775 };
8776 
8777 class XCoreTargetCodeGenInfo : public TargetCodeGenInfo {
8778   mutable TypeStringCache TSC;
8779 public:
8780   XCoreTargetCodeGenInfo(CodeGenTypes &CGT)
8781     :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {}
8782   void emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8783                     CodeGen::CodeGenModule &M) const override;
8784 };
8785 
8786 } // End anonymous namespace.
8787 
8788 // TODO: this implementation is likely now redundant with the default
8789 // EmitVAArg.
8790 Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
8791                                 QualType Ty) const {
8792   CGBuilderTy &Builder = CGF.Builder;
8793 
8794   // Get the VAList.
8795   CharUnits SlotSize = CharUnits::fromQuantity(4);
8796   Address AP(Builder.CreateLoad(VAListAddr), SlotSize);
8797 
8798   // Handle the argument.
8799   ABIArgInfo AI = classifyArgumentType(Ty);
8800   CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty);
8801   llvm::Type *ArgTy = CGT.ConvertType(Ty);
8802   if (AI.canHaveCoerceToType() && !AI.getCoerceToType())
8803     AI.setCoerceToType(ArgTy);
8804   llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy);
8805 
8806   Address Val = Address::invalid();
8807   CharUnits ArgSize = CharUnits::Zero();
8808   switch (AI.getKind()) {
8809   case ABIArgInfo::Expand:
8810   case ABIArgInfo::CoerceAndExpand:
8811   case ABIArgInfo::InAlloca:
8812     llvm_unreachable("Unsupported ABI kind for va_arg");
8813   case ABIArgInfo::Ignore:
8814     Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign);
8815     ArgSize = CharUnits::Zero();
8816     break;
8817   case ABIArgInfo::Extend:
8818   case ABIArgInfo::Direct:
8819     Val = Builder.CreateBitCast(AP, ArgPtrTy);
8820     ArgSize = CharUnits::fromQuantity(
8821                        getDataLayout().getTypeAllocSize(AI.getCoerceToType()));
8822     ArgSize = ArgSize.alignTo(SlotSize);
8823     break;
8824   case ABIArgInfo::Indirect:
8825     Val = Builder.CreateElementBitCast(AP, ArgPtrTy);
8826     Val = Address(Builder.CreateLoad(Val), TypeAlign);
8827     ArgSize = SlotSize;
8828     break;
8829   }
8830 
8831   // Increment the VAList.
8832   if (!ArgSize.isZero()) {
8833     Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize);
8834     Builder.CreateStore(APN.getPointer(), VAListAddr);
8835   }
8836 
8837   return Val;
8838 }
8839 
8840 /// During the expansion of a RecordType, an incomplete TypeString is placed
8841 /// into the cache as a means to identify and break recursion.
8842 /// If there is a Recursive encoding in the cache, it is swapped out and will
8843 /// be reinserted by removeIncomplete().
8844 /// All other types of encoding should have been used rather than arriving here.
8845 void TypeStringCache::addIncomplete(const IdentifierInfo *ID,
8846                                     std::string StubEnc) {
8847   if (!ID)
8848     return;
8849   Entry &E = Map[ID];
8850   assert( (E.Str.empty() || E.State == Recursive) &&
8851          "Incorrectly use of addIncomplete");
8852   assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()");
8853   E.Swapped.swap(E.Str); // swap out the Recursive
8854   E.Str.swap(StubEnc);
8855   E.State = Incomplete;
8856   ++IncompleteCount;
8857 }
8858 
8859 /// Once the RecordType has been expanded, the temporary incomplete TypeString
8860 /// must be removed from the cache.
8861 /// If a Recursive was swapped out by addIncomplete(), it will be replaced.
8862 /// Returns true if the RecordType was defined recursively.
8863 bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) {
8864   if (!ID)
8865     return false;
8866   auto I = Map.find(ID);
8867   assert(I != Map.end() && "Entry not present");
8868   Entry &E = I->second;
8869   assert( (E.State == Incomplete ||
8870            E.State == IncompleteUsed) &&
8871          "Entry must be an incomplete type");
8872   bool IsRecursive = false;
8873   if (E.State == IncompleteUsed) {
8874     // We made use of our Incomplete encoding, thus we are recursive.
8875     IsRecursive = true;
8876     --IncompleteUsedCount;
8877   }
8878   if (E.Swapped.empty())
8879     Map.erase(I);
8880   else {
8881     // Swap the Recursive back.
8882     E.Swapped.swap(E.Str);
8883     E.Swapped.clear();
8884     E.State = Recursive;
8885   }
8886   --IncompleteCount;
8887   return IsRecursive;
8888 }
8889 
8890 /// Add the encoded TypeString to the cache only if it is NonRecursive or
8891 /// Recursive (viz: all sub-members were expanded as fully as possible).
8892 void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str,
8893                                     bool IsRecursive) {
8894   if (!ID || IncompleteUsedCount)
8895     return; // No key or it is is an incomplete sub-type so don't add.
8896   Entry &E = Map[ID];
8897   if (IsRecursive && !E.Str.empty()) {
8898     assert(E.State==Recursive && E.Str.size() == Str.size() &&
8899            "This is not the same Recursive entry");
8900     // The parent container was not recursive after all, so we could have used
8901     // this Recursive sub-member entry after all, but we assumed the worse when
8902     // we started viz: IncompleteCount!=0.
8903     return;
8904   }
8905   assert(E.Str.empty() && "Entry already present");
8906   E.Str = Str.str();
8907   E.State = IsRecursive? Recursive : NonRecursive;
8908 }
8909 
8910 /// Return a cached TypeString encoding for the ID. If there isn't one, or we
8911 /// are recursively expanding a type (IncompleteCount != 0) and the cached
8912 /// encoding is Recursive, return an empty StringRef.
8913 StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) {
8914   if (!ID)
8915     return StringRef();   // We have no key.
8916   auto I = Map.find(ID);
8917   if (I == Map.end())
8918     return StringRef();   // We have no encoding.
8919   Entry &E = I->second;
8920   if (E.State == Recursive && IncompleteCount)
8921     return StringRef();   // We don't use Recursive encodings for member types.
8922 
8923   if (E.State == Incomplete) {
8924     // The incomplete type is being used to break out of recursion.
8925     E.State = IncompleteUsed;
8926     ++IncompleteUsedCount;
8927   }
8928   return E.Str;
8929 }
8930 
8931 /// The XCore ABI includes a type information section that communicates symbol
8932 /// type information to the linker. The linker uses this information to verify
8933 /// safety/correctness of things such as array bound and pointers et al.
8934 /// The ABI only requires C (and XC) language modules to emit TypeStrings.
8935 /// This type information (TypeString) is emitted into meta data for all global
8936 /// symbols: definitions, declarations, functions & variables.
8937 ///
8938 /// The TypeString carries type, qualifier, name, size & value details.
8939 /// Please see 'Tools Development Guide' section 2.16.2 for format details:
8940 /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf
8941 /// The output is tested by test/CodeGen/xcore-stringtype.c.
8942 ///
8943 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
8944                           CodeGen::CodeGenModule &CGM, TypeStringCache &TSC);
8945 
8946 /// XCore uses emitTargetMD to emit TypeString metadata for global symbols.
8947 void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV,
8948                                           CodeGen::CodeGenModule &CGM) const {
8949   SmallStringEnc Enc;
8950   if (getTypeString(Enc, D, CGM, TSC)) {
8951     llvm::LLVMContext &Ctx = CGM.getModule().getContext();
8952     llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV),
8953                                 llvm::MDString::get(Ctx, Enc.str())};
8954     llvm::NamedMDNode *MD =
8955       CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings");
8956     MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
8957   }
8958 }
8959 
8960 //===----------------------------------------------------------------------===//
8961 // SPIR ABI Implementation
8962 //===----------------------------------------------------------------------===//
8963 
8964 namespace {
8965 class SPIRTargetCodeGenInfo : public TargetCodeGenInfo {
8966 public:
8967   SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
8968     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
8969   unsigned getOpenCLKernelCallingConv() const override;
8970 };
8971 
8972 } // End anonymous namespace.
8973 
8974 namespace clang {
8975 namespace CodeGen {
8976 void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) {
8977   DefaultABIInfo SPIRABI(CGM.getTypes());
8978   SPIRABI.computeInfo(FI);
8979 }
8980 }
8981 }
8982 
8983 unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
8984   return llvm::CallingConv::SPIR_KERNEL;
8985 }
8986 
8987 static bool appendType(SmallStringEnc &Enc, QualType QType,
8988                        const CodeGen::CodeGenModule &CGM,
8989                        TypeStringCache &TSC);
8990 
8991 /// Helper function for appendRecordType().
8992 /// Builds a SmallVector containing the encoded field types in declaration
8993 /// order.
8994 static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE,
8995                              const RecordDecl *RD,
8996                              const CodeGen::CodeGenModule &CGM,
8997                              TypeStringCache &TSC) {
8998   for (const auto *Field : RD->fields()) {
8999     SmallStringEnc Enc;
9000     Enc += "m(";
9001     Enc += Field->getName();
9002     Enc += "){";
9003     if (Field->isBitField()) {
9004       Enc += "b(";
9005       llvm::raw_svector_ostream OS(Enc);
9006       OS << Field->getBitWidthValue(CGM.getContext());
9007       Enc += ':';
9008     }
9009     if (!appendType(Enc, Field->getType(), CGM, TSC))
9010       return false;
9011     if (Field->isBitField())
9012       Enc += ')';
9013     Enc += '}';
9014     FE.emplace_back(!Field->getName().empty(), Enc);
9015   }
9016   return true;
9017 }
9018 
9019 /// Appends structure and union types to Enc and adds encoding to cache.
9020 /// Recursively calls appendType (via extractFieldType) for each field.
9021 /// Union types have their fields ordered according to the ABI.
9022 static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT,
9023                              const CodeGen::CodeGenModule &CGM,
9024                              TypeStringCache &TSC, const IdentifierInfo *ID) {
9025   // Append the cached TypeString if we have one.
9026   StringRef TypeString = TSC.lookupStr(ID);
9027   if (!TypeString.empty()) {
9028     Enc += TypeString;
9029     return true;
9030   }
9031 
9032   // Start to emit an incomplete TypeString.
9033   size_t Start = Enc.size();
9034   Enc += (RT->isUnionType()? 'u' : 's');
9035   Enc += '(';
9036   if (ID)
9037     Enc += ID->getName();
9038   Enc += "){";
9039 
9040   // We collect all encoded fields and order as necessary.
9041   bool IsRecursive = false;
9042   const RecordDecl *RD = RT->getDecl()->getDefinition();
9043   if (RD && !RD->field_empty()) {
9044     // An incomplete TypeString stub is placed in the cache for this RecordType
9045     // so that recursive calls to this RecordType will use it whilst building a
9046     // complete TypeString for this RecordType.
9047     SmallVector<FieldEncoding, 16> FE;
9048     std::string StubEnc(Enc.substr(Start).str());
9049     StubEnc += '}';  // StubEnc now holds a valid incomplete TypeString.
9050     TSC.addIncomplete(ID, std::move(StubEnc));
9051     if (!extractFieldType(FE, RD, CGM, TSC)) {
9052       (void) TSC.removeIncomplete(ID);
9053       return false;
9054     }
9055     IsRecursive = TSC.removeIncomplete(ID);
9056     // The ABI requires unions to be sorted but not structures.
9057     // See FieldEncoding::operator< for sort algorithm.
9058     if (RT->isUnionType())
9059       llvm::sort(FE);
9060     // We can now complete the TypeString.
9061     unsigned E = FE.size();
9062     for (unsigned I = 0; I != E; ++I) {
9063       if (I)
9064         Enc += ',';
9065       Enc += FE[I].str();
9066     }
9067   }
9068   Enc += '}';
9069   TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive);
9070   return true;
9071 }
9072 
9073 /// Appends enum types to Enc and adds the encoding to the cache.
9074 static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET,
9075                            TypeStringCache &TSC,
9076                            const IdentifierInfo *ID) {
9077   // Append the cached TypeString if we have one.
9078   StringRef TypeString = TSC.lookupStr(ID);
9079   if (!TypeString.empty()) {
9080     Enc += TypeString;
9081     return true;
9082   }
9083 
9084   size_t Start = Enc.size();
9085   Enc += "e(";
9086   if (ID)
9087     Enc += ID->getName();
9088   Enc += "){";
9089 
9090   // We collect all encoded enumerations and order them alphanumerically.
9091   if (const EnumDecl *ED = ET->getDecl()->getDefinition()) {
9092     SmallVector<FieldEncoding, 16> FE;
9093     for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E;
9094          ++I) {
9095       SmallStringEnc EnumEnc;
9096       EnumEnc += "m(";
9097       EnumEnc += I->getName();
9098       EnumEnc += "){";
9099       I->getInitVal().toString(EnumEnc);
9100       EnumEnc += '}';
9101       FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc));
9102     }
9103     llvm::sort(FE);
9104     unsigned E = FE.size();
9105     for (unsigned I = 0; I != E; ++I) {
9106       if (I)
9107         Enc += ',';
9108       Enc += FE[I].str();
9109     }
9110   }
9111   Enc += '}';
9112   TSC.addIfComplete(ID, Enc.substr(Start), false);
9113   return true;
9114 }
9115 
9116 /// Appends type's qualifier to Enc.
9117 /// This is done prior to appending the type's encoding.
9118 static void appendQualifier(SmallStringEnc &Enc, QualType QT) {
9119   // Qualifiers are emitted in alphabetical order.
9120   static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"};
9121   int Lookup = 0;
9122   if (QT.isConstQualified())
9123     Lookup += 1<<0;
9124   if (QT.isRestrictQualified())
9125     Lookup += 1<<1;
9126   if (QT.isVolatileQualified())
9127     Lookup += 1<<2;
9128   Enc += Table[Lookup];
9129 }
9130 
9131 /// Appends built-in types to Enc.
9132 static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) {
9133   const char *EncType;
9134   switch (BT->getKind()) {
9135     case BuiltinType::Void:
9136       EncType = "0";
9137       break;
9138     case BuiltinType::Bool:
9139       EncType = "b";
9140       break;
9141     case BuiltinType::Char_U:
9142       EncType = "uc";
9143       break;
9144     case BuiltinType::UChar:
9145       EncType = "uc";
9146       break;
9147     case BuiltinType::SChar:
9148       EncType = "sc";
9149       break;
9150     case BuiltinType::UShort:
9151       EncType = "us";
9152       break;
9153     case BuiltinType::Short:
9154       EncType = "ss";
9155       break;
9156     case BuiltinType::UInt:
9157       EncType = "ui";
9158       break;
9159     case BuiltinType::Int:
9160       EncType = "si";
9161       break;
9162     case BuiltinType::ULong:
9163       EncType = "ul";
9164       break;
9165     case BuiltinType::Long:
9166       EncType = "sl";
9167       break;
9168     case BuiltinType::ULongLong:
9169       EncType = "ull";
9170       break;
9171     case BuiltinType::LongLong:
9172       EncType = "sll";
9173       break;
9174     case BuiltinType::Float:
9175       EncType = "ft";
9176       break;
9177     case BuiltinType::Double:
9178       EncType = "d";
9179       break;
9180     case BuiltinType::LongDouble:
9181       EncType = "ld";
9182       break;
9183     default:
9184       return false;
9185   }
9186   Enc += EncType;
9187   return true;
9188 }
9189 
9190 /// Appends a pointer encoding to Enc before calling appendType for the pointee.
9191 static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT,
9192                               const CodeGen::CodeGenModule &CGM,
9193                               TypeStringCache &TSC) {
9194   Enc += "p(";
9195   if (!appendType(Enc, PT->getPointeeType(), CGM, TSC))
9196     return false;
9197   Enc += ')';
9198   return true;
9199 }
9200 
9201 /// Appends array encoding to Enc before calling appendType for the element.
9202 static bool appendArrayType(SmallStringEnc &Enc, QualType QT,
9203                             const ArrayType *AT,
9204                             const CodeGen::CodeGenModule &CGM,
9205                             TypeStringCache &TSC, StringRef NoSizeEnc) {
9206   if (AT->getSizeModifier() != ArrayType::Normal)
9207     return false;
9208   Enc += "a(";
9209   if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
9210     CAT->getSize().toStringUnsigned(Enc);
9211   else
9212     Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "".
9213   Enc += ':';
9214   // The Qualifiers should be attached to the type rather than the array.
9215   appendQualifier(Enc, QT);
9216   if (!appendType(Enc, AT->getElementType(), CGM, TSC))
9217     return false;
9218   Enc += ')';
9219   return true;
9220 }
9221 
9222 /// Appends a function encoding to Enc, calling appendType for the return type
9223 /// and the arguments.
9224 static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT,
9225                              const CodeGen::CodeGenModule &CGM,
9226                              TypeStringCache &TSC) {
9227   Enc += "f{";
9228   if (!appendType(Enc, FT->getReturnType(), CGM, TSC))
9229     return false;
9230   Enc += "}(";
9231   if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) {
9232     // N.B. we are only interested in the adjusted param types.
9233     auto I = FPT->param_type_begin();
9234     auto E = FPT->param_type_end();
9235     if (I != E) {
9236       do {
9237         if (!appendType(Enc, *I, CGM, TSC))
9238           return false;
9239         ++I;
9240         if (I != E)
9241           Enc += ',';
9242       } while (I != E);
9243       if (FPT->isVariadic())
9244         Enc += ",va";
9245     } else {
9246       if (FPT->isVariadic())
9247         Enc += "va";
9248       else
9249         Enc += '0';
9250     }
9251   }
9252   Enc += ')';
9253   return true;
9254 }
9255 
9256 /// Handles the type's qualifier before dispatching a call to handle specific
9257 /// type encodings.
9258 static bool appendType(SmallStringEnc &Enc, QualType QType,
9259                        const CodeGen::CodeGenModule &CGM,
9260                        TypeStringCache &TSC) {
9261 
9262   QualType QT = QType.getCanonicalType();
9263 
9264   if (const ArrayType *AT = QT->getAsArrayTypeUnsafe())
9265     // The Qualifiers should be attached to the type rather than the array.
9266     // Thus we don't call appendQualifier() here.
9267     return appendArrayType(Enc, QT, AT, CGM, TSC, "");
9268 
9269   appendQualifier(Enc, QT);
9270 
9271   if (const BuiltinType *BT = QT->getAs<BuiltinType>())
9272     return appendBuiltinType(Enc, BT);
9273 
9274   if (const PointerType *PT = QT->getAs<PointerType>())
9275     return appendPointerType(Enc, PT, CGM, TSC);
9276 
9277   if (const EnumType *ET = QT->getAs<EnumType>())
9278     return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier());
9279 
9280   if (const RecordType *RT = QT->getAsStructureType())
9281     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
9282 
9283   if (const RecordType *RT = QT->getAsUnionType())
9284     return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier());
9285 
9286   if (const FunctionType *FT = QT->getAs<FunctionType>())
9287     return appendFunctionType(Enc, FT, CGM, TSC);
9288 
9289   return false;
9290 }
9291 
9292 static bool getTypeString(SmallStringEnc &Enc, const Decl *D,
9293                           CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) {
9294   if (!D)
9295     return false;
9296 
9297   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
9298     if (FD->getLanguageLinkage() != CLanguageLinkage)
9299       return false;
9300     return appendType(Enc, FD->getType(), CGM, TSC);
9301   }
9302 
9303   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
9304     if (VD->getLanguageLinkage() != CLanguageLinkage)
9305       return false;
9306     QualType QT = VD->getType().getCanonicalType();
9307     if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) {
9308       // Global ArrayTypes are given a size of '*' if the size is unknown.
9309       // The Qualifiers should be attached to the type rather than the array.
9310       // Thus we don't call appendQualifier() here.
9311       return appendArrayType(Enc, QT, AT, CGM, TSC, "*");
9312     }
9313     return appendType(Enc, QT, CGM, TSC);
9314   }
9315   return false;
9316 }
9317 
9318 //===----------------------------------------------------------------------===//
9319 // RISCV ABI Implementation
9320 //===----------------------------------------------------------------------===//
9321 
9322 namespace {
9323 class RISCVABIInfo : public DefaultABIInfo {
9324 private:
9325   // Size of the integer ('x') registers in bits.
9326   unsigned XLen;
9327   // Size of the floating point ('f') registers in bits. Note that the target
9328   // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target
9329   // with soft float ABI has FLen==0).
9330   unsigned FLen;
9331   static const int NumArgGPRs = 8;
9332   static const int NumArgFPRs = 8;
9333   bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
9334                                       llvm::Type *&Field1Ty,
9335                                       CharUnits &Field1Off,
9336                                       llvm::Type *&Field2Ty,
9337                                       CharUnits &Field2Off) const;
9338 
9339 public:
9340   RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen)
9341       : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {}
9342 
9343   // DefaultABIInfo's classifyReturnType and classifyArgumentType are
9344   // non-virtual, but computeInfo is virtual, so we overload it.
9345   void computeInfo(CGFunctionInfo &FI) const override;
9346 
9347   ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft,
9348                                   int &ArgFPRsLeft) const;
9349   ABIArgInfo classifyReturnType(QualType RetTy) const;
9350 
9351   Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9352                     QualType Ty) const override;
9353 
9354   ABIArgInfo extendType(QualType Ty) const;
9355 
9356   bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
9357                                 CharUnits &Field1Off, llvm::Type *&Field2Ty,
9358                                 CharUnits &Field2Off, int &NeededArgGPRs,
9359                                 int &NeededArgFPRs) const;
9360   ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty,
9361                                                CharUnits Field1Off,
9362                                                llvm::Type *Field2Ty,
9363                                                CharUnits Field2Off) const;
9364 };
9365 } // end anonymous namespace
9366 
9367 void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const {
9368   QualType RetTy = FI.getReturnType();
9369   if (!getCXXABI().classifyReturnType(FI))
9370     FI.getReturnInfo() = classifyReturnType(RetTy);
9371 
9372   // IsRetIndirect is true if classifyArgumentType indicated the value should
9373   // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128
9374   // is passed direct in LLVM IR, relying on the backend lowering code to
9375   // rewrite the argument list and pass indirectly on RV32.
9376   bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect ||
9377                        getContext().getTypeSize(RetTy) > (2 * XLen);
9378 
9379   // We must track the number of GPRs used in order to conform to the RISC-V
9380   // ABI, as integer scalars passed in registers should have signext/zeroext
9381   // when promoted, but are anyext if passed on the stack. As GPR usage is
9382   // different for variadic arguments, we must also track whether we are
9383   // examining a vararg or not.
9384   int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs;
9385   int ArgFPRsLeft = FLen ? NumArgFPRs : 0;
9386   int NumFixedArgs = FI.getNumRequiredArgs();
9387 
9388   int ArgNum = 0;
9389   for (auto &ArgInfo : FI.arguments()) {
9390     bool IsFixed = ArgNum < NumFixedArgs;
9391     ArgInfo.info =
9392         classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft);
9393     ArgNum++;
9394   }
9395 }
9396 
9397 // Returns true if the struct is a potential candidate for the floating point
9398 // calling convention. If this function returns true, the caller is
9399 // responsible for checking that if there is only a single field then that
9400 // field is a float.
9401 bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff,
9402                                                   llvm::Type *&Field1Ty,
9403                                                   CharUnits &Field1Off,
9404                                                   llvm::Type *&Field2Ty,
9405                                                   CharUnits &Field2Off) const {
9406   bool IsInt = Ty->isIntegralOrEnumerationType();
9407   bool IsFloat = Ty->isRealFloatingType();
9408 
9409   if (IsInt || IsFloat) {
9410     uint64_t Size = getContext().getTypeSize(Ty);
9411     if (IsInt && Size > XLen)
9412       return false;
9413     // Can't be eligible if larger than the FP registers. Half precision isn't
9414     // currently supported on RISC-V and the ABI hasn't been confirmed, so
9415     // default to the integer ABI in that case.
9416     if (IsFloat && (Size > FLen || Size < 32))
9417       return false;
9418     // Can't be eligible if an integer type was already found (int+int pairs
9419     // are not eligible).
9420     if (IsInt && Field1Ty && Field1Ty->isIntegerTy())
9421       return false;
9422     if (!Field1Ty) {
9423       Field1Ty = CGT.ConvertType(Ty);
9424       Field1Off = CurOff;
9425       return true;
9426     }
9427     if (!Field2Ty) {
9428       Field2Ty = CGT.ConvertType(Ty);
9429       Field2Off = CurOff;
9430       return true;
9431     }
9432     return false;
9433   }
9434 
9435   if (auto CTy = Ty->getAs<ComplexType>()) {
9436     if (Field1Ty)
9437       return false;
9438     QualType EltTy = CTy->getElementType();
9439     if (getContext().getTypeSize(EltTy) > FLen)
9440       return false;
9441     Field1Ty = CGT.ConvertType(EltTy);
9442     Field1Off = CurOff;
9443     assert(CurOff.isZero() && "Unexpected offset for first field");
9444     Field2Ty = Field1Ty;
9445     Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy);
9446     return true;
9447   }
9448 
9449   if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) {
9450     uint64_t ArraySize = ATy->getSize().getZExtValue();
9451     QualType EltTy = ATy->getElementType();
9452     CharUnits EltSize = getContext().getTypeSizeInChars(EltTy);
9453     for (uint64_t i = 0; i < ArraySize; ++i) {
9454       bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty,
9455                                                 Field1Off, Field2Ty, Field2Off);
9456       if (!Ret)
9457         return false;
9458       CurOff += EltSize;
9459     }
9460     return true;
9461   }
9462 
9463   if (const auto *RTy = Ty->getAs<RecordType>()) {
9464     // Structures with either a non-trivial destructor or a non-trivial
9465     // copy constructor are not eligible for the FP calling convention.
9466     if (getRecordArgABI(Ty, CGT.getCXXABI()))
9467       return false;
9468     if (isEmptyRecord(getContext(), Ty, true))
9469       return true;
9470     const RecordDecl *RD = RTy->getDecl();
9471     // Unions aren't eligible unless they're empty (which is caught above).
9472     if (RD->isUnion())
9473       return false;
9474     int ZeroWidthBitFieldCount = 0;
9475     for (const FieldDecl *FD : RD->fields()) {
9476       const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
9477       uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex());
9478       QualType QTy = FD->getType();
9479       if (FD->isBitField()) {
9480         unsigned BitWidth = FD->getBitWidthValue(getContext());
9481         // Allow a bitfield with a type greater than XLen as long as the
9482         // bitwidth is XLen or less.
9483         if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen)
9484           QTy = getContext().getIntTypeForBitwidth(XLen, false);
9485         if (BitWidth == 0) {
9486           ZeroWidthBitFieldCount++;
9487           continue;
9488         }
9489       }
9490 
9491       bool Ret = detectFPCCEligibleStructHelper(
9492           QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits),
9493           Field1Ty, Field1Off, Field2Ty, Field2Off);
9494       if (!Ret)
9495         return false;
9496 
9497       // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp
9498       // or int+fp structs, but are ignored for a struct with an fp field and
9499       // any number of zero-width bitfields.
9500       if (Field2Ty && ZeroWidthBitFieldCount > 0)
9501         return false;
9502     }
9503     return Field1Ty != nullptr;
9504   }
9505 
9506   return false;
9507 }
9508 
9509 // Determine if a struct is eligible for passing according to the floating
9510 // point calling convention (i.e., when flattened it contains a single fp
9511 // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and
9512 // NeededArgGPRs are incremented appropriately.
9513 bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty,
9514                                             CharUnits &Field1Off,
9515                                             llvm::Type *&Field2Ty,
9516                                             CharUnits &Field2Off,
9517                                             int &NeededArgGPRs,
9518                                             int &NeededArgFPRs) const {
9519   Field1Ty = nullptr;
9520   Field2Ty = nullptr;
9521   NeededArgGPRs = 0;
9522   NeededArgFPRs = 0;
9523   bool IsCandidate = detectFPCCEligibleStructHelper(
9524       Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off);
9525   // Not really a candidate if we have a single int but no float.
9526   if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy())
9527     return false;
9528   if (!IsCandidate)
9529     return false;
9530   if (Field1Ty && Field1Ty->isFloatingPointTy())
9531     NeededArgFPRs++;
9532   else if (Field1Ty)
9533     NeededArgGPRs++;
9534   if (Field2Ty && Field2Ty->isFloatingPointTy())
9535     NeededArgFPRs++;
9536   else if (Field2Ty)
9537     NeededArgGPRs++;
9538   return IsCandidate;
9539 }
9540 
9541 // Call getCoerceAndExpand for the two-element flattened struct described by
9542 // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an
9543 // appropriate coerceToType and unpaddedCoerceToType.
9544 ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct(
9545     llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty,
9546     CharUnits Field2Off) const {
9547   SmallVector<llvm::Type *, 3> CoerceElts;
9548   SmallVector<llvm::Type *, 2> UnpaddedCoerceElts;
9549   if (!Field1Off.isZero())
9550     CoerceElts.push_back(llvm::ArrayType::get(
9551         llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity()));
9552 
9553   CoerceElts.push_back(Field1Ty);
9554   UnpaddedCoerceElts.push_back(Field1Ty);
9555 
9556   if (!Field2Ty) {
9557     return ABIArgInfo::getCoerceAndExpand(
9558         llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()),
9559         UnpaddedCoerceElts[0]);
9560   }
9561 
9562   CharUnits Field2Align =
9563       CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty));
9564   CharUnits Field1Size =
9565       CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty));
9566   CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align);
9567 
9568   CharUnits Padding = CharUnits::Zero();
9569   if (Field2Off > Field2OffNoPadNoPack)
9570     Padding = Field2Off - Field2OffNoPadNoPack;
9571   else if (Field2Off != Field2Align && Field2Off > Field1Size)
9572     Padding = Field2Off - Field1Size;
9573 
9574   bool IsPacked = !Field2Off.isMultipleOf(Field2Align);
9575 
9576   if (!Padding.isZero())
9577     CoerceElts.push_back(llvm::ArrayType::get(
9578         llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity()));
9579 
9580   CoerceElts.push_back(Field2Ty);
9581   UnpaddedCoerceElts.push_back(Field2Ty);
9582 
9583   auto CoerceToType =
9584       llvm::StructType::get(getVMContext(), CoerceElts, IsPacked);
9585   auto UnpaddedCoerceToType =
9586       llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked);
9587 
9588   return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType);
9589 }
9590 
9591 ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed,
9592                                               int &ArgGPRsLeft,
9593                                               int &ArgFPRsLeft) const {
9594   assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow");
9595   Ty = useFirstFieldIfTransparentUnion(Ty);
9596 
9597   // Structures with either a non-trivial destructor or a non-trivial
9598   // copy constructor are always passed indirectly.
9599   if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) {
9600     if (ArgGPRsLeft)
9601       ArgGPRsLeft -= 1;
9602     return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA ==
9603                                            CGCXXABI::RAA_DirectInMemory);
9604   }
9605 
9606   // Ignore empty structs/unions.
9607   if (isEmptyRecord(getContext(), Ty, true))
9608     return ABIArgInfo::getIgnore();
9609 
9610   uint64_t Size = getContext().getTypeSize(Ty);
9611 
9612   // Pass floating point values via FPRs if possible.
9613   if (IsFixed && Ty->isFloatingType() && FLen >= Size && ArgFPRsLeft) {
9614     ArgFPRsLeft--;
9615     return ABIArgInfo::getDirect();
9616   }
9617 
9618   // Complex types for the hard float ABI must be passed direct rather than
9619   // using CoerceAndExpand.
9620   if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) {
9621     QualType EltTy = Ty->castAs<ComplexType>()->getElementType();
9622     if (getContext().getTypeSize(EltTy) <= FLen) {
9623       ArgFPRsLeft -= 2;
9624       return ABIArgInfo::getDirect();
9625     }
9626   }
9627 
9628   if (IsFixed && FLen && Ty->isStructureOrClassType()) {
9629     llvm::Type *Field1Ty = nullptr;
9630     llvm::Type *Field2Ty = nullptr;
9631     CharUnits Field1Off = CharUnits::Zero();
9632     CharUnits Field2Off = CharUnits::Zero();
9633     int NeededArgGPRs;
9634     int NeededArgFPRs;
9635     bool IsCandidate =
9636         detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off,
9637                                  NeededArgGPRs, NeededArgFPRs);
9638     if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft &&
9639         NeededArgFPRs <= ArgFPRsLeft) {
9640       ArgGPRsLeft -= NeededArgGPRs;
9641       ArgFPRsLeft -= NeededArgFPRs;
9642       return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty,
9643                                                Field2Off);
9644     }
9645   }
9646 
9647   uint64_t NeededAlign = getContext().getTypeAlign(Ty);
9648   bool MustUseStack = false;
9649   // Determine the number of GPRs needed to pass the current argument
9650   // according to the ABI. 2*XLen-aligned varargs are passed in "aligned"
9651   // register pairs, so may consume 3 registers.
9652   int NeededArgGPRs = 1;
9653   if (!IsFixed && NeededAlign == 2 * XLen)
9654     NeededArgGPRs = 2 + (ArgGPRsLeft % 2);
9655   else if (Size > XLen && Size <= 2 * XLen)
9656     NeededArgGPRs = 2;
9657 
9658   if (NeededArgGPRs > ArgGPRsLeft) {
9659     MustUseStack = true;
9660     NeededArgGPRs = ArgGPRsLeft;
9661   }
9662 
9663   ArgGPRsLeft -= NeededArgGPRs;
9664 
9665   if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) {
9666     // Treat an enum type as its underlying type.
9667     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
9668       Ty = EnumTy->getDecl()->getIntegerType();
9669 
9670     // All integral types are promoted to XLen width, unless passed on the
9671     // stack.
9672     if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) {
9673       return extendType(Ty);
9674     }
9675 
9676     return ABIArgInfo::getDirect();
9677   }
9678 
9679   // Aggregates which are <= 2*XLen will be passed in registers if possible,
9680   // so coerce to integers.
9681   if (Size <= 2 * XLen) {
9682     unsigned Alignment = getContext().getTypeAlign(Ty);
9683 
9684     // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is
9685     // required, and a 2-element XLen array if only XLen alignment is required.
9686     if (Size <= XLen) {
9687       return ABIArgInfo::getDirect(
9688           llvm::IntegerType::get(getVMContext(), XLen));
9689     } else if (Alignment == 2 * XLen) {
9690       return ABIArgInfo::getDirect(
9691           llvm::IntegerType::get(getVMContext(), 2 * XLen));
9692     } else {
9693       return ABIArgInfo::getDirect(llvm::ArrayType::get(
9694           llvm::IntegerType::get(getVMContext(), XLen), 2));
9695     }
9696   }
9697   return getNaturalAlignIndirect(Ty, /*ByVal=*/false);
9698 }
9699 
9700 ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const {
9701   if (RetTy->isVoidType())
9702     return ABIArgInfo::getIgnore();
9703 
9704   int ArgGPRsLeft = 2;
9705   int ArgFPRsLeft = FLen ? 2 : 0;
9706 
9707   // The rules for return and argument types are the same, so defer to
9708   // classifyArgumentType.
9709   return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft,
9710                               ArgFPRsLeft);
9711 }
9712 
9713 Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
9714                                 QualType Ty) const {
9715   CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8);
9716 
9717   // Empty records are ignored for parameter passing purposes.
9718   if (isEmptyRecord(getContext(), Ty, true)) {
9719     Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize);
9720     Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty));
9721     return Addr;
9722   }
9723 
9724   std::pair<CharUnits, CharUnits> SizeAndAlign =
9725       getContext().getTypeInfoInChars(Ty);
9726 
9727   // Arguments bigger than 2*Xlen bytes are passed indirectly.
9728   bool IsIndirect = SizeAndAlign.first > 2 * SlotSize;
9729 
9730   return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign,
9731                           SlotSize, /*AllowHigherAlign=*/true);
9732 }
9733 
9734 ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const {
9735   int TySize = getContext().getTypeSize(Ty);
9736   // RV64 ABI requires unsigned 32 bit integers to be sign extended.
9737   if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32)
9738     return ABIArgInfo::getSignExtend(Ty);
9739   return ABIArgInfo::getExtend(Ty);
9740 }
9741 
9742 namespace {
9743 class RISCVTargetCodeGenInfo : public TargetCodeGenInfo {
9744 public:
9745   RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen,
9746                          unsigned FLen)
9747       : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen, FLen)) {}
9748 
9749   void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
9750                            CodeGen::CodeGenModule &CGM) const override {
9751     const auto *FD = dyn_cast_or_null<FunctionDecl>(D);
9752     if (!FD) return;
9753 
9754     const auto *Attr = FD->getAttr<RISCVInterruptAttr>();
9755     if (!Attr)
9756       return;
9757 
9758     const char *Kind;
9759     switch (Attr->getInterrupt()) {
9760     case RISCVInterruptAttr::user: Kind = "user"; break;
9761     case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break;
9762     case RISCVInterruptAttr::machine: Kind = "machine"; break;
9763     }
9764 
9765     auto *Fn = cast<llvm::Function>(GV);
9766 
9767     Fn->addFnAttr("interrupt", Kind);
9768   }
9769 };
9770 } // namespace
9771 
9772 //===----------------------------------------------------------------------===//
9773 // Driver code
9774 //===----------------------------------------------------------------------===//
9775 
9776 bool CodeGenModule::supportsCOMDAT() const {
9777   return getTriple().supportsCOMDAT();
9778 }
9779 
9780 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
9781   if (TheTargetCodeGenInfo)
9782     return *TheTargetCodeGenInfo;
9783 
9784   // Helper to set the unique_ptr while still keeping the return value.
9785   auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & {
9786     this->TheTargetCodeGenInfo.reset(P);
9787     return *P;
9788   };
9789 
9790   const llvm::Triple &Triple = getTarget().getTriple();
9791   switch (Triple.getArch()) {
9792   default:
9793     return SetCGInfo(new DefaultTargetCodeGenInfo(Types));
9794 
9795   case llvm::Triple::le32:
9796     return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
9797   case llvm::Triple::mips:
9798   case llvm::Triple::mipsel:
9799     if (Triple.getOS() == llvm::Triple::NaCl)
9800       return SetCGInfo(new PNaClTargetCodeGenInfo(Types));
9801     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true));
9802 
9803   case llvm::Triple::mips64:
9804   case llvm::Triple::mips64el:
9805     return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false));
9806 
9807   case llvm::Triple::avr:
9808     return SetCGInfo(new AVRTargetCodeGenInfo(Types));
9809 
9810   case llvm::Triple::aarch64:
9811   case llvm::Triple::aarch64_32:
9812   case llvm::Triple::aarch64_be: {
9813     AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS;
9814     if (getTarget().getABI() == "darwinpcs")
9815       Kind = AArch64ABIInfo::DarwinPCS;
9816     else if (Triple.isOSWindows())
9817       return SetCGInfo(
9818           new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64));
9819 
9820     return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind));
9821   }
9822 
9823   case llvm::Triple::wasm32:
9824   case llvm::Triple::wasm64:
9825     return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types));
9826 
9827   case llvm::Triple::arm:
9828   case llvm::Triple::armeb:
9829   case llvm::Triple::thumb:
9830   case llvm::Triple::thumbeb: {
9831     if (Triple.getOS() == llvm::Triple::Win32) {
9832       return SetCGInfo(
9833           new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP));
9834     }
9835 
9836     ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
9837     StringRef ABIStr = getTarget().getABI();
9838     if (ABIStr == "apcs-gnu")
9839       Kind = ARMABIInfo::APCS;
9840     else if (ABIStr == "aapcs16")
9841       Kind = ARMABIInfo::AAPCS16_VFP;
9842     else if (CodeGenOpts.FloatABI == "hard" ||
9843              (CodeGenOpts.FloatABI != "soft" &&
9844               (Triple.getEnvironment() == llvm::Triple::GNUEABIHF ||
9845                Triple.getEnvironment() == llvm::Triple::MuslEABIHF ||
9846                Triple.getEnvironment() == llvm::Triple::EABIHF)))
9847       Kind = ARMABIInfo::AAPCS_VFP;
9848 
9849     return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind));
9850   }
9851 
9852   case llvm::Triple::ppc:
9853     return SetCGInfo(
9854         new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft" ||
9855                                    getTarget().hasFeature("spe")));
9856   case llvm::Triple::ppc64:
9857     if (Triple.isOSBinFormatELF()) {
9858       PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1;
9859       if (getTarget().getABI() == "elfv2")
9860         Kind = PPC64_SVR4_ABIInfo::ELFv2;
9861       bool HasQPX = getTarget().getABI() == "elfv1-qpx";
9862       bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
9863 
9864       return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
9865                                                         IsSoftFloat));
9866     } else
9867       return SetCGInfo(new PPC64TargetCodeGenInfo(Types));
9868   case llvm::Triple::ppc64le: {
9869     assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!");
9870     PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2;
9871     if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx")
9872       Kind = PPC64_SVR4_ABIInfo::ELFv1;
9873     bool HasQPX = getTarget().getABI() == "elfv1-qpx";
9874     bool IsSoftFloat = CodeGenOpts.FloatABI == "soft";
9875 
9876     return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX,
9877                                                       IsSoftFloat));
9878   }
9879 
9880   case llvm::Triple::nvptx:
9881   case llvm::Triple::nvptx64:
9882     return SetCGInfo(new NVPTXTargetCodeGenInfo(Types));
9883 
9884   case llvm::Triple::msp430:
9885     return SetCGInfo(new MSP430TargetCodeGenInfo(Types));
9886 
9887   case llvm::Triple::riscv32:
9888   case llvm::Triple::riscv64: {
9889     StringRef ABIStr = getTarget().getABI();
9890     unsigned XLen = getTarget().getPointerWidth(0);
9891     unsigned ABIFLen = 0;
9892     if (ABIStr.endswith("f"))
9893       ABIFLen = 32;
9894     else if (ABIStr.endswith("d"))
9895       ABIFLen = 64;
9896     return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen));
9897   }
9898 
9899   case llvm::Triple::systemz: {
9900     bool HasVector = getTarget().getABI() == "vector";
9901     return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector));
9902   }
9903 
9904   case llvm::Triple::tce:
9905   case llvm::Triple::tcele:
9906     return SetCGInfo(new TCETargetCodeGenInfo(Types));
9907 
9908   case llvm::Triple::x86: {
9909     bool IsDarwinVectorABI = Triple.isOSDarwin();
9910     bool RetSmallStructInRegABI =
9911         X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts);
9912     bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing();
9913 
9914     if (Triple.getOS() == llvm::Triple::Win32) {
9915       return SetCGInfo(new WinX86_32TargetCodeGenInfo(
9916           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
9917           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters));
9918     } else {
9919       return SetCGInfo(new X86_32TargetCodeGenInfo(
9920           Types, IsDarwinVectorABI, RetSmallStructInRegABI,
9921           IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters,
9922           CodeGenOpts.FloatABI == "soft"));
9923     }
9924   }
9925 
9926   case llvm::Triple::x86_64: {
9927     StringRef ABI = getTarget().getABI();
9928     X86AVXABILevel AVXLevel =
9929         (ABI == "avx512"
9930              ? X86AVXABILevel::AVX512
9931              : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None);
9932 
9933     switch (Triple.getOS()) {
9934     case llvm::Triple::Win32:
9935       return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel));
9936     default:
9937       return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel));
9938     }
9939   }
9940   case llvm::Triple::hexagon:
9941     return SetCGInfo(new HexagonTargetCodeGenInfo(Types));
9942   case llvm::Triple::lanai:
9943     return SetCGInfo(new LanaiTargetCodeGenInfo(Types));
9944   case llvm::Triple::r600:
9945     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
9946   case llvm::Triple::amdgcn:
9947     return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types));
9948   case llvm::Triple::sparc:
9949     return SetCGInfo(new SparcV8TargetCodeGenInfo(Types));
9950   case llvm::Triple::sparcv9:
9951     return SetCGInfo(new SparcV9TargetCodeGenInfo(Types));
9952   case llvm::Triple::xcore:
9953     return SetCGInfo(new XCoreTargetCodeGenInfo(Types));
9954   case llvm::Triple::arc:
9955     return SetCGInfo(new ARCTargetCodeGenInfo(Types));
9956   case llvm::Triple::spir:
9957   case llvm::Triple::spir64:
9958     return SetCGInfo(new SPIRTargetCodeGenInfo(Types));
9959   }
9960 }
9961 
9962 /// Create an OpenCL kernel for an enqueued block.
9963 ///
9964 /// The kernel has the same function type as the block invoke function. Its
9965 /// name is the name of the block invoke function postfixed with "_kernel".
9966 /// It simply calls the block invoke function then returns.
9967 llvm::Function *
9968 TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF,
9969                                              llvm::Function *Invoke,
9970                                              llvm::Value *BlockLiteral) const {
9971   auto *InvokeFT = Invoke->getFunctionType();
9972   llvm::SmallVector<llvm::Type *, 2> ArgTys;
9973   for (auto &P : InvokeFT->params())
9974     ArgTys.push_back(P);
9975   auto &C = CGF.getLLVMContext();
9976   std::string Name = Invoke->getName().str() + "_kernel";
9977   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
9978   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
9979                                    &CGF.CGM.getModule());
9980   auto IP = CGF.Builder.saveIP();
9981   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
9982   auto &Builder = CGF.Builder;
9983   Builder.SetInsertPoint(BB);
9984   llvm::SmallVector<llvm::Value *, 2> Args;
9985   for (auto &A : F->args())
9986     Args.push_back(&A);
9987   Builder.CreateCall(Invoke, Args);
9988   Builder.CreateRetVoid();
9989   Builder.restoreIP(IP);
9990   return F;
9991 }
9992 
9993 /// Create an OpenCL kernel for an enqueued block.
9994 ///
9995 /// The type of the first argument (the block literal) is the struct type
9996 /// of the block literal instead of a pointer type. The first argument
9997 /// (block literal) is passed directly by value to the kernel. The kernel
9998 /// allocates the same type of struct on stack and stores the block literal
9999 /// to it and passes its pointer to the block invoke function. The kernel
10000 /// has "enqueued-block" function attribute and kernel argument metadata.
10001 llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
10002     CodeGenFunction &CGF, llvm::Function *Invoke,
10003     llvm::Value *BlockLiteral) const {
10004   auto &Builder = CGF.Builder;
10005   auto &C = CGF.getLLVMContext();
10006 
10007   auto *BlockTy = BlockLiteral->getType()->getPointerElementType();
10008   auto *InvokeFT = Invoke->getFunctionType();
10009   llvm::SmallVector<llvm::Type *, 2> ArgTys;
10010   llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
10011   llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
10012   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
10013   llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
10014   llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
10015   llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
10016 
10017   ArgTys.push_back(BlockTy);
10018   ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
10019   AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
10020   ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
10021   ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
10022   AccessQuals.push_back(llvm::MDString::get(C, "none"));
10023   ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
10024   for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
10025     ArgTys.push_back(InvokeFT->getParamType(I));
10026     ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
10027     AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
10028     AccessQuals.push_back(llvm::MDString::get(C, "none"));
10029     ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
10030     ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
10031     ArgNames.push_back(
10032         llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
10033   }
10034   std::string Name = Invoke->getName().str() + "_kernel";
10035   auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
10036   auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
10037                                    &CGF.CGM.getModule());
10038   F->addFnAttr("enqueued-block");
10039   auto IP = CGF.Builder.saveIP();
10040   auto *BB = llvm::BasicBlock::Create(C, "entry", F);
10041   Builder.SetInsertPoint(BB);
10042   unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy);
10043   auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
10044   BlockPtr->setAlignment(llvm::MaybeAlign(BlockAlign));
10045   Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
10046   auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
10047   llvm::SmallVector<llvm::Value *, 2> Args;
10048   Args.push_back(Cast);
10049   for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I)
10050     Args.push_back(I);
10051   Builder.CreateCall(Invoke, Args);
10052   Builder.CreateRetVoid();
10053   Builder.restoreIP(IP);
10054 
10055   F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
10056   F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
10057   F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
10058   F->setMetadata("kernel_arg_base_type",
10059                  llvm::MDNode::get(C, ArgBaseTypeNames));
10060   F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
10061   if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
10062     F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
10063 
10064   return F;
10065 }
10066