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