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