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