clang API Documentation

CodeGen/TargetInfo.cpp
Go to the documentation of this file.
00001 //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // These classes wrap the information about a call or function
00011 // definition used to handle ABI compliancy.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "TargetInfo.h"
00016 #include "ABIInfo.h"
00017 #include "CodeGenFunction.h"
00018 #include "clang/AST/RecordLayout.h"
00019 #include "clang/Frontend/CodeGenOptions.h"
00020 #include "llvm/Type.h"
00021 #include "llvm/Target/TargetData.h"
00022 #include "llvm/ADT/Triple.h"
00023 #include "llvm/Support/raw_ostream.h"
00024 using namespace clang;
00025 using namespace CodeGen;
00026 
00027 static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
00028                                llvm::Value *Array,
00029                                llvm::Value *Value,
00030                                unsigned FirstIndex,
00031                                unsigned LastIndex) {
00032   // Alternatively, we could emit this as a loop in the source.
00033   for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
00034     llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
00035     Builder.CreateStore(Value, Cell);
00036   }
00037 }
00038 
00039 static bool isAggregateTypeForABI(QualType T) {
00040   return CodeGenFunction::hasAggregateLLVMType(T) ||
00041          T->isMemberFunctionPointerType();
00042 }
00043 
00044 ABIInfo::~ABIInfo() {}
00045 
00046 ASTContext &ABIInfo::getContext() const {
00047   return CGT.getContext();
00048 }
00049 
00050 llvm::LLVMContext &ABIInfo::getVMContext() const {
00051   return CGT.getLLVMContext();
00052 }
00053 
00054 const llvm::TargetData &ABIInfo::getTargetData() const {
00055   return CGT.getTargetData();
00056 }
00057 
00058 
00059 void ABIArgInfo::dump() const {
00060   raw_ostream &OS = llvm::errs();
00061   OS << "(ABIArgInfo Kind=";
00062   switch (TheKind) {
00063   case Direct:
00064     OS << "Direct Type=";
00065     if (llvm::Type *Ty = getCoerceToType())
00066       Ty->print(OS);
00067     else
00068       OS << "null";
00069     break;
00070   case Extend:
00071     OS << "Extend";
00072     break;
00073   case Ignore:
00074     OS << "Ignore";
00075     break;
00076   case Indirect:
00077     OS << "Indirect Align=" << getIndirectAlign()
00078        << " ByVal=" << getIndirectByVal()
00079        << " Realign=" << getIndirectRealign();
00080     break;
00081   case Expand:
00082     OS << "Expand";
00083     break;
00084   }
00085   OS << ")\n";
00086 }
00087 
00088 TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
00089 
00090 // If someone can figure out a general rule for this, that would be great.
00091 // It's probably just doomed to be platform-dependent, though.
00092 unsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
00093   // Verified for:
00094   //   x86-64     FreeBSD, Linux, Darwin
00095   //   x86-32     FreeBSD, Linux, Darwin
00096   //   PowerPC    Linux, Darwin
00097   //   ARM        Darwin (*not* EABI)
00098   return 32;
00099 }
00100 
00101 bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
00102                                      const FunctionNoProtoType *fnType) const {
00103   // The following conventions are known to require this to be false:
00104   //   x86_stdcall
00105   //   MIPS
00106   // For everything else, we just prefer false unless we opt out.
00107   return false;
00108 }
00109 
00110 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
00111 
00112 /// isEmptyField - Return true iff a the field is "empty", that is it
00113 /// is an unnamed bit-field or an (array of) empty record(s).
00114 static bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
00115                          bool AllowArrays) {
00116   if (FD->isUnnamedBitfield())
00117     return true;
00118 
00119   QualType FT = FD->getType();
00120 
00121   // Constant arrays of empty records count as empty, strip them off.
00122   // Constant arrays of zero length always count as empty.
00123   if (AllowArrays)
00124     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
00125       if (AT->getSize() == 0)
00126         return true;
00127       FT = AT->getElementType();
00128     }
00129 
00130   const RecordType *RT = FT->getAs<RecordType>();
00131   if (!RT)
00132     return false;
00133 
00134   // C++ record fields are never empty, at least in the Itanium ABI.
00135   //
00136   // FIXME: We should use a predicate for whether this behavior is true in the
00137   // current ABI.
00138   if (isa<CXXRecordDecl>(RT->getDecl()))
00139     return false;
00140 
00141   return isEmptyRecord(Context, FT, AllowArrays);
00142 }
00143 
00144 /// isEmptyRecord - Return true iff a structure contains only empty
00145 /// fields. Note that a structure with a flexible array member is not
00146 /// considered empty.
00147 static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
00148   const RecordType *RT = T->getAs<RecordType>();
00149   if (!RT)
00150     return 0;
00151   const RecordDecl *RD = RT->getDecl();
00152   if (RD->hasFlexibleArrayMember())
00153     return false;
00154 
00155   // If this is a C++ record, check the bases first.
00156   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
00157     for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
00158            e = CXXRD->bases_end(); i != e; ++i)
00159       if (!isEmptyRecord(Context, i->getType(), true))
00160         return false;
00161 
00162   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
00163          i != e; ++i)
00164     if (!isEmptyField(Context, &*i, AllowArrays))
00165       return false;
00166   return true;
00167 }
00168 
00169 /// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
00170 /// a non-trivial destructor or a non-trivial copy constructor.
00171 static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
00172   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
00173   if (!RD)
00174     return false;
00175 
00176   return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
00177 }
00178 
00179 /// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
00180 /// a record type with either a non-trivial destructor or a non-trivial copy
00181 /// constructor.
00182 static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
00183   const RecordType *RT = T->getAs<RecordType>();
00184   if (!RT)
00185     return false;
00186 
00187   return hasNonTrivialDestructorOrCopyConstructor(RT);
00188 }
00189 
00190 /// isSingleElementStruct - Determine if a structure is a "single
00191 /// element struct", i.e. it has exactly one non-empty field or
00192 /// exactly one field which is itself a single element
00193 /// struct. Structures with flexible array members are never
00194 /// considered single element structs.
00195 ///
00196 /// \return The field declaration for the single non-empty field, if
00197 /// it exists.
00198 static const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
00199   const RecordType *RT = T->getAsStructureType();
00200   if (!RT)
00201     return 0;
00202 
00203   const RecordDecl *RD = RT->getDecl();
00204   if (RD->hasFlexibleArrayMember())
00205     return 0;
00206 
00207   const Type *Found = 0;
00208 
00209   // If this is a C++ record, check the bases first.
00210   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
00211     for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
00212            e = CXXRD->bases_end(); i != e; ++i) {
00213       // Ignore empty records.
00214       if (isEmptyRecord(Context, i->getType(), true))
00215         continue;
00216 
00217       // If we already found an element then this isn't a single-element struct.
00218       if (Found)
00219         return 0;
00220 
00221       // If this is non-empty and not a single element struct, the composite
00222       // cannot be a single element struct.
00223       Found = isSingleElementStruct(i->getType(), Context);
00224       if (!Found)
00225         return 0;
00226     }
00227   }
00228 
00229   // Check for single element.
00230   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
00231          i != e; ++i) {
00232     const FieldDecl *FD = &*i;
00233     QualType FT = FD->getType();
00234 
00235     // Ignore empty fields.
00236     if (isEmptyField(Context, FD, true))
00237       continue;
00238 
00239     // If we already found an element then this isn't a single-element
00240     // struct.
00241     if (Found)
00242       return 0;
00243 
00244     // Treat single element arrays as the element.
00245     while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
00246       if (AT->getSize().getZExtValue() != 1)
00247         break;
00248       FT = AT->getElementType();
00249     }
00250 
00251     if (!isAggregateTypeForABI(FT)) {
00252       Found = FT.getTypePtr();
00253     } else {
00254       Found = isSingleElementStruct(FT, Context);
00255       if (!Found)
00256         return 0;
00257     }
00258   }
00259 
00260   // We don't consider a struct a single-element struct if it has
00261   // padding beyond the element type.
00262   if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
00263     return 0;
00264 
00265   return Found;
00266 }
00267 
00268 static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
00269   if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
00270       !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
00271       !Ty->isBlockPointerType())
00272     return false;
00273 
00274   uint64_t Size = Context.getTypeSize(Ty);
00275   return Size == 32 || Size == 64;
00276 }
00277 
00278 /// canExpandIndirectArgument - Test whether an argument type which is to be
00279 /// passed indirectly (on the stack) would have the equivalent layout if it was
00280 /// expanded into separate arguments. If so, we prefer to do the latter to avoid
00281 /// inhibiting optimizations.
00282 ///
00283 // FIXME: This predicate is missing many cases, currently it just follows
00284 // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
00285 // should probably make this smarter, or better yet make the LLVM backend
00286 // capable of handling it.
00287 static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
00288   // We can only expand structure types.
00289   const RecordType *RT = Ty->getAs<RecordType>();
00290   if (!RT)
00291     return false;
00292 
00293   // We can only expand (C) structures.
00294   //
00295   // FIXME: This needs to be generalized to handle classes as well.
00296   const RecordDecl *RD = RT->getDecl();
00297   if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
00298     return false;
00299 
00300   uint64_t Size = 0;
00301 
00302   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
00303          i != e; ++i) {
00304     const FieldDecl *FD = &*i;
00305 
00306     if (!is32Or64BitBasicType(FD->getType(), Context))
00307       return false;
00308 
00309     // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
00310     // how to expand them yet, and the predicate for telling if a bitfield still
00311     // counts as "basic" is more complicated than what we were doing previously.
00312     if (FD->isBitField())
00313       return false;
00314 
00315     Size += Context.getTypeSize(FD->getType());
00316   }
00317 
00318   // Make sure there are not any holes in the struct.
00319   if (Size != Context.getTypeSize(Ty))
00320     return false;
00321 
00322   return true;
00323 }
00324 
00325 namespace {
00326 /// DefaultABIInfo - The default implementation for ABI specific
00327 /// details. This implementation provides information which results in
00328 /// self-consistent and sensible LLVM IR generation, but does not
00329 /// conform to any particular ABI.
00330 class DefaultABIInfo : public ABIInfo {
00331 public:
00332   DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
00333 
00334   ABIArgInfo classifyReturnType(QualType RetTy) const;
00335   ABIArgInfo classifyArgumentType(QualType RetTy) const;
00336 
00337   virtual void computeInfo(CGFunctionInfo &FI) const {
00338     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
00339     for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
00340          it != ie; ++it)
00341       it->info = classifyArgumentType(it->type);
00342   }
00343 
00344   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
00345                                  CodeGenFunction &CGF) const;
00346 };
00347 
00348 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
00349 public:
00350   DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
00351     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
00352 };
00353 
00354 llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
00355                                        CodeGenFunction &CGF) const {
00356   return 0;
00357 }
00358 
00359 ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
00360   if (isAggregateTypeForABI(Ty)) {
00361     // Records with non trivial destructors/constructors should not be passed
00362     // by value.
00363     if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
00364       return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
00365 
00366     return ABIArgInfo::getIndirect(0);
00367   }
00368 
00369   // Treat an enum type as its underlying type.
00370   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
00371     Ty = EnumTy->getDecl()->getIntegerType();
00372 
00373   return (Ty->isPromotableIntegerType() ?
00374           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
00375 }
00376 
00377 ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
00378   if (RetTy->isVoidType())
00379     return ABIArgInfo::getIgnore();
00380 
00381   if (isAggregateTypeForABI(RetTy))
00382     return ABIArgInfo::getIndirect(0);
00383 
00384   // Treat an enum type as its underlying type.
00385   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
00386     RetTy = EnumTy->getDecl()->getIntegerType();
00387 
00388   return (RetTy->isPromotableIntegerType() ?
00389           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
00390 }
00391 
00392 /// UseX86_MMXType - Return true if this is an MMX type that should use the
00393 /// special x86_mmx type.
00394 bool UseX86_MMXType(llvm::Type *IRType) {
00395   // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
00396   // special x86_mmx type.
00397   return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
00398     cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
00399     IRType->getScalarSizeInBits() != 64;
00400 }
00401 
00402 static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
00403                                           StringRef Constraint,
00404                                           llvm::Type* Ty) {
00405   if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
00406     return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
00407   return Ty;
00408 }
00409 
00410 //===----------------------------------------------------------------------===//
00411 // X86-32 ABI Implementation
00412 //===----------------------------------------------------------------------===//
00413 
00414 /// X86_32ABIInfo - The X86-32 ABI information.
00415 class X86_32ABIInfo : public ABIInfo {
00416   static const unsigned MinABIStackAlignInBytes = 4;
00417 
00418   bool IsDarwinVectorABI;
00419   bool IsSmallStructInRegABI;
00420   bool IsMMXDisabled;
00421   bool IsWin32FloatStructABI;
00422 
00423   static bool isRegisterSize(unsigned Size) {
00424     return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
00425   }
00426 
00427   static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, 
00428                                           unsigned callingConvention);
00429 
00430   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
00431   /// such that the argument will be passed in memory.
00432   ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
00433 
00434   /// \brief Return the alignment to use for the given type on the stack.
00435   unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
00436 
00437 public:
00438 
00439   ABIArgInfo classifyReturnType(QualType RetTy, 
00440                                 unsigned callingConvention) const;
00441   ABIArgInfo classifyArgumentType(QualType RetTy) const;
00442 
00443   virtual void computeInfo(CGFunctionInfo &FI) const {
00444     FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), 
00445                                             FI.getCallingConvention());
00446     for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
00447          it != ie; ++it)
00448       it->info = classifyArgumentType(it->type);
00449   }
00450 
00451   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
00452                                  CodeGenFunction &CGF) const;
00453 
00454   X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w)
00455     : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
00456       IsMMXDisabled(m), IsWin32FloatStructABI(w) {}
00457 };
00458 
00459 class X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
00460 public:
00461   X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
00462       bool d, bool p, bool m, bool w)
00463     :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w)) {}
00464 
00465   void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
00466                            CodeGen::CodeGenModule &CGM) const;
00467 
00468   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
00469     // Darwin uses different dwarf register numbers for EH.
00470     if (CGM.isTargetDarwin()) return 5;
00471 
00472     return 4;
00473   }
00474 
00475   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
00476                                llvm::Value *Address) const;
00477 
00478   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
00479                                   StringRef Constraint,
00480                                   llvm::Type* Ty) const {
00481     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
00482   }
00483 
00484 };
00485 
00486 }
00487 
00488 /// shouldReturnTypeInRegister - Determine if the given type should be
00489 /// passed in a register (for the Darwin ABI).
00490 bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
00491                                                ASTContext &Context,
00492                                                unsigned callingConvention) {
00493   uint64_t Size = Context.getTypeSize(Ty);
00494 
00495   // Type must be register sized.
00496   if (!isRegisterSize(Size))
00497     return false;
00498 
00499   if (Ty->isVectorType()) {
00500     // 64- and 128- bit vectors inside structures are not returned in
00501     // registers.
00502     if (Size == 64 || Size == 128)
00503       return false;
00504 
00505     return true;
00506   }
00507 
00508   // If this is a builtin, pointer, enum, complex type, member pointer, or
00509   // member function pointer it is ok.
00510   if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
00511       Ty->isAnyComplexType() || Ty->isEnumeralType() ||
00512       Ty->isBlockPointerType() || Ty->isMemberPointerType())
00513     return true;
00514 
00515   // Arrays are treated like records.
00516   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
00517     return shouldReturnTypeInRegister(AT->getElementType(), Context,
00518                                       callingConvention);
00519 
00520   // Otherwise, it must be a record type.
00521   const RecordType *RT = Ty->getAs<RecordType>();
00522   if (!RT) return false;
00523 
00524   // FIXME: Traverse bases here too.
00525 
00526   // For thiscall conventions, structures will never be returned in
00527   // a register.  This is for compatibility with the MSVC ABI
00528   if (callingConvention == llvm::CallingConv::X86_ThisCall && 
00529       RT->isStructureType()) {
00530     return false;
00531   }
00532 
00533   // Structure types are passed in register if all fields would be
00534   // passed in a register.
00535   for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
00536          e = RT->getDecl()->field_end(); i != e; ++i) {
00537     const FieldDecl *FD = &*i;
00538 
00539     // Empty fields are ignored.
00540     if (isEmptyField(Context, FD, true))
00541       continue;
00542 
00543     // Check fields recursively.
00544     if (!shouldReturnTypeInRegister(FD->getType(), Context, 
00545                                     callingConvention))
00546       return false;
00547   }
00548   return true;
00549 }
00550 
00551 ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, 
00552                                             unsigned callingConvention) const {
00553   if (RetTy->isVoidType())
00554     return ABIArgInfo::getIgnore();
00555 
00556   if (const VectorType *VT = RetTy->getAs<VectorType>()) {
00557     // On Darwin, some vectors are returned in registers.
00558     if (IsDarwinVectorABI) {
00559       uint64_t Size = getContext().getTypeSize(RetTy);
00560 
00561       // 128-bit vectors are a special case; they are returned in
00562       // registers and we need to make sure to pick a type the LLVM
00563       // backend will like.
00564       if (Size == 128)
00565         return ABIArgInfo::getDirect(llvm::VectorType::get(
00566                   llvm::Type::getInt64Ty(getVMContext()), 2));
00567 
00568       // Always return in register if it fits in a general purpose
00569       // register, or if it is 64 bits and has a single element.
00570       if ((Size == 8 || Size == 16 || Size == 32) ||
00571           (Size == 64 && VT->getNumElements() == 1))
00572         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
00573                                                             Size));
00574 
00575       return ABIArgInfo::getIndirect(0);
00576     }
00577 
00578     return ABIArgInfo::getDirect();
00579   }
00580 
00581   if (isAggregateTypeForABI(RetTy)) {
00582     if (const RecordType *RT = RetTy->getAs<RecordType>()) {
00583       // Structures with either a non-trivial destructor or a non-trivial
00584       // copy constructor are always indirect.
00585       if (hasNonTrivialDestructorOrCopyConstructor(RT))
00586         return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
00587 
00588       // Structures with flexible arrays are always indirect.
00589       if (RT->getDecl()->hasFlexibleArrayMember())
00590         return ABIArgInfo::getIndirect(0);
00591     }
00592 
00593     // If specified, structs and unions are always indirect.
00594     if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
00595       return ABIArgInfo::getIndirect(0);
00596 
00597     // Small structures which are register sized are generally returned
00598     // in a register.
00599     if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(), 
00600                                                   callingConvention)) {
00601       uint64_t Size = getContext().getTypeSize(RetTy);
00602 
00603       // As a special-case, if the struct is a "single-element" struct, and
00604       // the field is of type "float" or "double", return it in a
00605       // floating-point register. (MSVC does not apply this special case.)
00606       // We apply a similar transformation for pointer types to improve the
00607       // quality of the generated IR.
00608       if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
00609         if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
00610             || SeltTy->hasPointerRepresentation())
00611           return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
00612 
00613       // FIXME: We should be able to narrow this integer in cases with dead
00614       // padding.
00615       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
00616     }
00617 
00618     return ABIArgInfo::getIndirect(0);
00619   }
00620 
00621   // Treat an enum type as its underlying type.
00622   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
00623     RetTy = EnumTy->getDecl()->getIntegerType();
00624 
00625   return (RetTy->isPromotableIntegerType() ?
00626           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
00627 }
00628 
00629 static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
00630   const RecordType *RT = Ty->getAs<RecordType>();
00631   if (!RT)
00632     return 0;
00633   const RecordDecl *RD = RT->getDecl();
00634 
00635   // If this is a C++ record, check the bases first.
00636   if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
00637     for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
00638            e = CXXRD->bases_end(); i != e; ++i)
00639       if (!isRecordWithSSEVectorType(Context, i->getType()))
00640         return false;
00641 
00642   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
00643        i != e; ++i) {
00644     QualType FT = i->getType();
00645 
00646     if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
00647       return true;
00648 
00649     if (isRecordWithSSEVectorType(Context, FT))
00650       return true;
00651   }
00652 
00653   return false;
00654 }
00655 
00656 unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
00657                                                  unsigned Align) const {
00658   // Otherwise, if the alignment is less than or equal to the minimum ABI
00659   // alignment, just use the default; the backend will handle this.
00660   if (Align <= MinABIStackAlignInBytes)
00661     return 0; // Use default alignment.
00662 
00663   // On non-Darwin, the stack type alignment is always 4.
00664   if (!IsDarwinVectorABI) {
00665     // Set explicit alignment, since we may need to realign the top.
00666     return MinABIStackAlignInBytes;
00667   }
00668 
00669   // Otherwise, if the type contains an SSE vector type, the alignment is 16.
00670   if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
00671     return 16;
00672 
00673   return MinABIStackAlignInBytes;
00674 }
00675 
00676 ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
00677   if (!ByVal)
00678     return ABIArgInfo::getIndirect(0, false);
00679 
00680   // Compute the byval alignment.
00681   unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
00682   unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
00683   if (StackAlign == 0)
00684     return ABIArgInfo::getIndirect(4);
00685 
00686   // If the stack alignment is less than the type alignment, realign the
00687   // argument.
00688   if (StackAlign < TypeAlign)
00689     return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
00690                                    /*Realign=*/true);
00691 
00692   return ABIArgInfo::getIndirect(StackAlign);
00693 }
00694 
00695 ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
00696   // FIXME: Set alignment on indirect arguments.
00697   if (isAggregateTypeForABI(Ty)) {
00698     // Structures with flexible arrays are always indirect.
00699     if (const RecordType *RT = Ty->getAs<RecordType>()) {
00700       // Structures with either a non-trivial destructor or a non-trivial
00701       // copy constructor are always indirect.
00702       if (hasNonTrivialDestructorOrCopyConstructor(RT))
00703         return getIndirectResult(Ty, /*ByVal=*/false);
00704 
00705       if (RT->getDecl()->hasFlexibleArrayMember())
00706         return getIndirectResult(Ty);
00707     }
00708 
00709     // Ignore empty structs/unions.
00710     if (isEmptyRecord(getContext(), Ty, true))
00711       return ABIArgInfo::getIgnore();
00712 
00713     // Expand small (<= 128-bit) record types when we know that the stack layout
00714     // of those arguments will match the struct. This is important because the
00715     // LLVM backend isn't smart enough to remove byval, which inhibits many
00716     // optimizations.
00717     if (getContext().getTypeSize(Ty) <= 4*32 &&
00718         canExpandIndirectArgument(Ty, getContext()))
00719       return ABIArgInfo::getExpand();
00720 
00721     return getIndirectResult(Ty);
00722   }
00723 
00724   if (const VectorType *VT = Ty->getAs<VectorType>()) {
00725     // On Darwin, some vectors are passed in memory, we handle this by passing
00726     // it as an i8/i16/i32/i64.
00727     if (IsDarwinVectorABI) {
00728       uint64_t Size = getContext().getTypeSize(Ty);
00729       if ((Size == 8 || Size == 16 || Size == 32) ||
00730           (Size == 64 && VT->getNumElements() == 1))
00731         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
00732                                                             Size));
00733     }
00734 
00735     llvm::Type *IRType = CGT.ConvertType(Ty);
00736     if (UseX86_MMXType(IRType)) {
00737       if (IsMMXDisabled)
00738         return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
00739                                                             64));
00740       ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
00741       AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
00742       return AAI;
00743     }
00744 
00745     return ABIArgInfo::getDirect();
00746   }
00747 
00748 
00749   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
00750     Ty = EnumTy->getDecl()->getIntegerType();
00751 
00752   return (Ty->isPromotableIntegerType() ?
00753           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
00754 }
00755 
00756 llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
00757                                       CodeGenFunction &CGF) const {
00758   llvm::Type *BPP = CGF.Int8PtrPtrTy;
00759 
00760   CGBuilderTy &Builder = CGF.Builder;
00761   llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
00762                                                        "ap");
00763   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
00764 
00765   // Compute if the address needs to be aligned
00766   unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
00767   Align = getTypeStackAlignInBytes(Ty, Align);
00768   Align = std::max(Align, 4U);
00769   if (Align > 4) {
00770     // addr = (addr + align - 1) & -align;
00771     llvm::Value *Offset =
00772       llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
00773     Addr = CGF.Builder.CreateGEP(Addr, Offset);
00774     llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
00775                                                     CGF.Int32Ty);
00776     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
00777     Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
00778                                       Addr->getType(),
00779                                       "ap.cur.aligned");
00780   }
00781 
00782   llvm::Type *PTy =
00783     llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
00784   llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
00785 
00786   uint64_t Offset =
00787     llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
00788   llvm::Value *NextAddr =
00789     Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
00790                       "ap.next");
00791   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
00792 
00793   return AddrTyped;
00794 }
00795 
00796 void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
00797                                                   llvm::GlobalValue *GV,
00798                                             CodeGen::CodeGenModule &CGM) const {
00799   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
00800     if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
00801       // Get the LLVM function.
00802       llvm::Function *Fn = cast<llvm::Function>(GV);
00803 
00804       // Now add the 'alignstack' attribute with a value of 16.
00805       Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
00806     }
00807   }
00808 }
00809 
00810 bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
00811                                                CodeGen::CodeGenFunction &CGF,
00812                                                llvm::Value *Address) const {
00813   CodeGen::CGBuilderTy &Builder = CGF.Builder;
00814 
00815   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
00816 
00817   // 0-7 are the eight integer registers;  the order is different
00818   //   on Darwin (for EH), but the range is the same.
00819   // 8 is %eip.
00820   AssignToArrayRange(Builder, Address, Four8, 0, 8);
00821 
00822   if (CGF.CGM.isTargetDarwin()) {
00823     // 12-16 are st(0..4).  Not sure why we stop at 4.
00824     // These have size 16, which is sizeof(long double) on
00825     // platforms with 8-byte alignment for that type.
00826     llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
00827     AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
00828 
00829   } else {
00830     // 9 is %eflags, which doesn't get a size on Darwin for some
00831     // reason.
00832     Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
00833 
00834     // 11-16 are st(0..5).  Not sure why we stop at 5.
00835     // These have size 12, which is sizeof(long double) on
00836     // platforms with 4-byte alignment for that type.
00837     llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
00838     AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
00839   }
00840 
00841   return false;
00842 }
00843 
00844 //===----------------------------------------------------------------------===//
00845 // X86-64 ABI Implementation
00846 //===----------------------------------------------------------------------===//
00847 
00848 
00849 namespace {
00850 /// X86_64ABIInfo - The X86_64 ABI information.
00851 class X86_64ABIInfo : public ABIInfo {
00852   enum Class {
00853     Integer = 0,
00854     SSE,
00855     SSEUp,
00856     X87,
00857     X87Up,
00858     ComplexX87,
00859     NoClass,
00860     Memory
00861   };
00862 
00863   /// merge - Implement the X86_64 ABI merging algorithm.
00864   ///
00865   /// Merge an accumulating classification \arg Accum with a field
00866   /// classification \arg Field.
00867   ///
00868   /// \param Accum - The accumulating classification. This should
00869   /// always be either NoClass or the result of a previous merge
00870   /// call. In addition, this should never be Memory (the caller
00871   /// should just return Memory for the aggregate).
00872   static Class merge(Class Accum, Class Field);
00873 
00874   /// postMerge - Implement the X86_64 ABI post merging algorithm.
00875   ///
00876   /// Post merger cleanup, reduces a malformed Hi and Lo pair to
00877   /// final MEMORY or SSE classes when necessary.
00878   ///
00879   /// \param AggregateSize - The size of the current aggregate in
00880   /// the classification process.
00881   ///
00882   /// \param Lo - The classification for the parts of the type
00883   /// residing in the low word of the containing object.
00884   ///
00885   /// \param Hi - The classification for the parts of the type
00886   /// residing in the higher words of the containing object.
00887   ///
00888   void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
00889 
00890   /// classify - Determine the x86_64 register classes in which the
00891   /// given type T should be passed.
00892   ///
00893   /// \param Lo - The classification for the parts of the type
00894   /// residing in the low word of the containing object.
00895   ///
00896   /// \param Hi - The classification for the parts of the type
00897   /// residing in the high word of the containing object.
00898   ///
00899   /// \param OffsetBase - The bit offset of this type in the
00900   /// containing object.  Some parameters are classified different
00901   /// depending on whether they straddle an eightbyte boundary.
00902   ///
00903   /// If a word is unused its result will be NoClass; if a type should
00904   /// be passed in Memory then at least the classification of \arg Lo
00905   /// will be Memory.
00906   ///
00907   /// The \arg Lo class will be NoClass iff the argument is ignored.
00908   ///
00909   /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
00910   /// also be ComplexX87.
00911   void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
00912 
00913   llvm::Type *GetByteVectorType(QualType Ty) const;
00914   llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
00915                                  unsigned IROffset, QualType SourceTy,
00916                                  unsigned SourceOffset) const;
00917   llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
00918                                      unsigned IROffset, QualType SourceTy,
00919                                      unsigned SourceOffset) const;
00920 
00921   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
00922   /// such that the argument will be returned in memory.
00923   ABIArgInfo getIndirectReturnResult(QualType Ty) const;
00924 
00925   /// getIndirectResult - Give a source type \arg Ty, return a suitable result
00926   /// such that the argument will be passed in memory.
00927   ///
00928   /// \param freeIntRegs - The number of free integer registers remaining
00929   /// available.
00930   ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
00931 
00932   ABIArgInfo classifyReturnType(QualType RetTy) const;
00933 
00934   ABIArgInfo classifyArgumentType(QualType Ty,
00935                                   unsigned freeIntRegs,
00936                                   unsigned &neededInt,
00937                                   unsigned &neededSSE) const;
00938 
00939   bool IsIllegalVectorType(QualType Ty) const;
00940 
00941   /// The 0.98 ABI revision clarified a lot of ambiguities,
00942   /// unfortunately in ways that were not always consistent with
00943   /// certain previous compilers.  In particular, platforms which
00944   /// required strict binary compatibility with older versions of GCC
00945   /// may need to exempt themselves.
00946   bool honorsRevision0_98() const {
00947     return !getContext().getTargetInfo().getTriple().isOSDarwin();
00948   }
00949 
00950   bool HasAVX;
00951 
00952 public:
00953   X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
00954       ABIInfo(CGT), HasAVX(hasavx) {}
00955 
00956   bool isPassedUsingAVXType(QualType type) const {
00957     unsigned neededInt, neededSSE;
00958     // The freeIntRegs argument doesn't matter here.
00959     ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
00960     if (info.isDirect()) {
00961       llvm::Type *ty = info.getCoerceToType();
00962       if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
00963         return (vectorTy->getBitWidth() > 128);
00964     }
00965     return false;
00966   }
00967 
00968   virtual void computeInfo(CGFunctionInfo &FI) const;
00969 
00970   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
00971                                  CodeGenFunction &CGF) const;
00972 };
00973 
00974 /// WinX86_64ABIInfo - The Windows X86_64 ABI information.
00975 class WinX86_64ABIInfo : public ABIInfo {
00976 
00977   ABIArgInfo classify(QualType Ty) const;
00978 
00979 public:
00980   WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
00981 
00982   virtual void computeInfo(CGFunctionInfo &FI) const;
00983 
00984   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
00985                                  CodeGenFunction &CGF) const;
00986 };
00987 
00988 class X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
00989 public:
00990   X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
00991     : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
00992 
00993   const X86_64ABIInfo &getABIInfo() const {
00994     return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
00995   }
00996 
00997   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
00998     return 7;
00999   }
01000 
01001   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
01002                                llvm::Value *Address) const {
01003     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
01004 
01005     // 0-15 are the 16 integer registers.
01006     // 16 is %rip.
01007     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
01008     return false;
01009   }
01010 
01011   llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
01012                                   StringRef Constraint,
01013                                   llvm::Type* Ty) const {
01014     return X86AdjustInlineAsmType(CGF, Constraint, Ty);
01015   }
01016 
01017   bool isNoProtoCallVariadic(const CallArgList &args,
01018                              const FunctionNoProtoType *fnType) const {
01019     // The default CC on x86-64 sets %al to the number of SSA
01020     // registers used, and GCC sets this when calling an unprototyped
01021     // function, so we override the default behavior.  However, don't do
01022     // that when AVX types are involved: the ABI explicitly states it is
01023     // undefined, and it doesn't work in practice because of how the ABI
01024     // defines varargs anyway.
01025     if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
01026       bool HasAVXType = false;
01027       for (CallArgList::const_iterator
01028              it = args.begin(), ie = args.end(); it != ie; ++it) {
01029         if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
01030           HasAVXType = true;
01031           break;
01032         }
01033       }
01034 
01035       if (!HasAVXType)
01036         return true;
01037     }
01038 
01039     return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
01040   }
01041 
01042 };
01043 
01044 class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
01045 public:
01046   WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
01047     : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
01048 
01049   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
01050     return 7;
01051   }
01052 
01053   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
01054                                llvm::Value *Address) const {
01055     llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
01056 
01057     // 0-15 are the 16 integer registers.
01058     // 16 is %rip.
01059     AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
01060     return false;
01061   }
01062 };
01063 
01064 }
01065 
01066 void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
01067                               Class &Hi) const {
01068   // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
01069   //
01070   // (a) If one of the classes is Memory, the whole argument is passed in
01071   //     memory.
01072   //
01073   // (b) If X87UP is not preceded by X87, the whole argument is passed in
01074   //     memory.
01075   //
01076   // (c) If the size of the aggregate exceeds two eightbytes and the first
01077   //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
01078   //     argument is passed in memory. NOTE: This is necessary to keep the
01079   //     ABI working for processors that don't support the __m256 type.
01080   //
01081   // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
01082   //
01083   // Some of these are enforced by the merging logic.  Others can arise
01084   // only with unions; for example:
01085   //   union { _Complex double; unsigned; }
01086   //
01087   // Note that clauses (b) and (c) were added in 0.98.
01088   //
01089   if (Hi == Memory)
01090     Lo = Memory;
01091   if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
01092     Lo = Memory;
01093   if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
01094     Lo = Memory;
01095   if (Hi == SSEUp && Lo != SSE)
01096     Hi = SSE;
01097 }
01098 
01099 X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
01100   // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
01101   // classified recursively so that always two fields are
01102   // considered. The resulting class is calculated according to
01103   // the classes of the fields in the eightbyte:
01104   //
01105   // (a) If both classes are equal, this is the resulting class.
01106   //
01107   // (b) If one of the classes is NO_CLASS, the resulting class is
01108   // the other class.
01109   //
01110   // (c) If one of the classes is MEMORY, the result is the MEMORY
01111   // class.
01112   //
01113   // (d) If one of the classes is INTEGER, the result is the
01114   // INTEGER.
01115   //
01116   // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
01117   // MEMORY is used as class.
01118   //
01119   // (f) Otherwise class SSE is used.
01120 
01121   // Accum should never be memory (we should have returned) or
01122   // ComplexX87 (because this cannot be passed in a structure).
01123   assert((Accum != Memory && Accum != ComplexX87) &&
01124          "Invalid accumulated classification during merge.");
01125   if (Accum == Field || Field == NoClass)
01126     return Accum;
01127   if (Field == Memory)
01128     return Memory;
01129   if (Accum == NoClass)
01130     return Field;
01131   if (Accum == Integer || Field == Integer)
01132     return Integer;
01133   if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
01134       Accum == X87 || Accum == X87Up)
01135     return Memory;
01136   return SSE;
01137 }
01138 
01139 void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
01140                              Class &Lo, Class &Hi) const {
01141   // FIXME: This code can be simplified by introducing a simple value class for
01142   // Class pairs with appropriate constructor methods for the various
01143   // situations.
01144 
01145   // FIXME: Some of the split computations are wrong; unaligned vectors
01146   // shouldn't be passed in registers for example, so there is no chance they
01147   // can straddle an eightbyte. Verify & simplify.
01148 
01149   Lo = Hi = NoClass;
01150 
01151   Class &Current = OffsetBase < 64 ? Lo : Hi;
01152   Current = Memory;
01153 
01154   if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
01155     BuiltinType::Kind k = BT->getKind();
01156 
01157     if (k == BuiltinType::Void) {
01158       Current = NoClass;
01159     } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
01160       Lo = Integer;
01161       Hi = Integer;
01162     } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
01163       Current = Integer;
01164     } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
01165       Current = SSE;
01166     } else if (k == BuiltinType::LongDouble) {
01167       Lo = X87;
01168       Hi = X87Up;
01169     }
01170     // FIXME: _Decimal32 and _Decimal64 are SSE.
01171     // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
01172     return;
01173   }
01174 
01175   if (const EnumType *ET = Ty->getAs<EnumType>()) {
01176     // Classify the underlying integer type.
01177     classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
01178     return;
01179   }
01180 
01181   if (Ty->hasPointerRepresentation()) {
01182     Current = Integer;
01183     return;
01184   }
01185 
01186   if (Ty->isMemberPointerType()) {
01187     if (Ty->isMemberFunctionPointerType())
01188       Lo = Hi = Integer;
01189     else
01190       Current = Integer;
01191     return;
01192   }
01193 
01194   if (const VectorType *VT = Ty->getAs<VectorType>()) {
01195     uint64_t Size = getContext().getTypeSize(VT);
01196     if (Size == 32) {
01197       // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
01198       // float> as integer.
01199       Current = Integer;
01200 
01201       // If this type crosses an eightbyte boundary, it should be
01202       // split.
01203       uint64_t EB_Real = (OffsetBase) / 64;
01204       uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
01205       if (EB_Real != EB_Imag)
01206         Hi = Lo;
01207     } else if (Size == 64) {
01208       // gcc passes <1 x double> in memory. :(
01209       if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
01210         return;
01211 
01212       // gcc passes <1 x long long> as INTEGER.
01213       if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
01214           VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
01215           VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
01216           VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
01217         Current = Integer;
01218       else
01219         Current = SSE;
01220 
01221       // If this type crosses an eightbyte boundary, it should be
01222       // split.
01223       if (OffsetBase && OffsetBase != 64)
01224         Hi = Lo;
01225     } else if (Size == 128 || (HasAVX && Size == 256)) {
01226       // Arguments of 256-bits are split into four eightbyte chunks. The
01227       // least significant one belongs to class SSE and all the others to class
01228       // SSEUP. The original Lo and Hi design considers that types can't be
01229       // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
01230       // This design isn't correct for 256-bits, but since there're no cases
01231       // where the upper parts would need to be inspected, avoid adding
01232       // complexity and just consider Hi to match the 64-256 part.
01233       Lo = SSE;
01234       Hi = SSEUp;
01235     }
01236     return;
01237   }
01238 
01239   if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
01240     QualType ET = getContext().getCanonicalType(CT->getElementType());
01241 
01242     uint64_t Size = getContext().getTypeSize(Ty);
01243     if (ET->isIntegralOrEnumerationType()) {
01244       if (Size <= 64)
01245         Current = Integer;
01246       else if (Size <= 128)
01247         Lo = Hi = Integer;
01248     } else if (ET == getContext().FloatTy)
01249       Current = SSE;
01250     else if (ET == getContext().DoubleTy)
01251       Lo = Hi = SSE;
01252     else if (ET == getContext().LongDoubleTy)
01253       Current = ComplexX87;
01254 
01255     // If this complex type crosses an eightbyte boundary then it
01256     // should be split.
01257     uint64_t EB_Real = (OffsetBase) / 64;
01258     uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
01259     if (Hi == NoClass && EB_Real != EB_Imag)
01260       Hi = Lo;
01261 
01262     return;
01263   }
01264 
01265   if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
01266     // Arrays are treated like structures.
01267 
01268     uint64_t Size = getContext().getTypeSize(Ty);
01269 
01270     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
01271     // than four eightbytes, ..., it has class MEMORY.
01272     if (Size > 256)
01273       return;
01274 
01275     // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
01276     // fields, it has class MEMORY.
01277     //
01278     // Only need to check alignment of array base.
01279     if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
01280       return;
01281 
01282     // Otherwise implement simplified merge. We could be smarter about
01283     // this, but it isn't worth it and would be harder to verify.
01284     Current = NoClass;
01285     uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
01286     uint64_t ArraySize = AT->getSize().getZExtValue();
01287 
01288     // The only case a 256-bit wide vector could be used is when the array
01289     // contains a single 256-bit element. Since Lo and Hi logic isn't extended
01290     // to work for sizes wider than 128, early check and fallback to memory.
01291     if (Size > 128 && EltSize != 256)
01292       return;
01293 
01294     for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
01295       Class FieldLo, FieldHi;
01296       classify(AT->getElementType(), Offset, FieldLo, FieldHi);
01297       Lo = merge(Lo, FieldLo);
01298       Hi = merge(Hi, FieldHi);
01299       if (Lo == Memory || Hi == Memory)
01300         break;
01301     }
01302 
01303     postMerge(Size, Lo, Hi);
01304     assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
01305     return;
01306   }
01307 
01308   if (const RecordType *RT = Ty->getAs<RecordType>()) {
01309     uint64_t Size = getContext().getTypeSize(Ty);
01310 
01311     // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
01312     // than four eightbytes, ..., it has class MEMORY.
01313     if (Size > 256)
01314       return;
01315 
01316     // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
01317     // copy constructor or a non-trivial destructor, it is passed by invisible
01318     // reference.
01319     if (hasNonTrivialDestructorOrCopyConstructor(RT))
01320       return;
01321 
01322     const RecordDecl *RD = RT->getDecl();
01323 
01324     // Assume variable sized types are passed in memory.
01325     if (RD->hasFlexibleArrayMember())
01326       return;
01327 
01328     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
01329 
01330     // Reset Lo class, this will be recomputed.
01331     Current = NoClass;
01332 
01333     // If this is a C++ record, classify the bases first.
01334     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
01335       for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
01336              e = CXXRD->bases_end(); i != e; ++i) {
01337         assert(!i->isVirtual() && !i->getType()->isDependentType() &&
01338                "Unexpected base class!");
01339         const CXXRecordDecl *Base =
01340           cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
01341 
01342         // Classify this field.
01343         //
01344         // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
01345         // single eightbyte, each is classified separately. Each eightbyte gets
01346         // initialized to class NO_CLASS.
01347         Class FieldLo, FieldHi;
01348         uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
01349         classify(i->getType(), Offset, FieldLo, FieldHi);
01350         Lo = merge(Lo, FieldLo);
01351         Hi = merge(Hi, FieldHi);
01352         if (Lo == Memory || Hi == Memory)
01353           break;
01354       }
01355     }
01356 
01357     // Classify the fields one at a time, merging the results.
01358     unsigned idx = 0;
01359     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
01360            i != e; ++i, ++idx) {
01361       uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
01362       bool BitField = i->isBitField();
01363 
01364       // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
01365       // four eightbytes, or it contains unaligned fields, it has class MEMORY.
01366       //
01367       // The only case a 256-bit wide vector could be used is when the struct
01368       // contains a single 256-bit element. Since Lo and Hi logic isn't extended
01369       // to work for sizes wider than 128, early check and fallback to memory.
01370       //
01371       if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
01372         Lo = Memory;
01373         return;
01374       }
01375       // Note, skip this test for bit-fields, see below.
01376       if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
01377         Lo = Memory;
01378         return;
01379       }
01380 
01381       // Classify this field.
01382       //
01383       // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
01384       // exceeds a single eightbyte, each is classified
01385       // separately. Each eightbyte gets initialized to class
01386       // NO_CLASS.
01387       Class FieldLo, FieldHi;
01388 
01389       // Bit-fields require special handling, they do not force the
01390       // structure to be passed in memory even if unaligned, and
01391       // therefore they can straddle an eightbyte.
01392       if (BitField) {
01393         // Ignore padding bit-fields.
01394         if (i->isUnnamedBitfield())
01395           continue;
01396 
01397         uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
01398         uint64_t Size = i->getBitWidthValue(getContext());
01399 
01400         uint64_t EB_Lo = Offset / 64;
01401         uint64_t EB_Hi = (Offset + Size - 1) / 64;
01402         FieldLo = FieldHi = NoClass;
01403         if (EB_Lo) {
01404           assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
01405           FieldLo = NoClass;
01406           FieldHi = Integer;
01407         } else {
01408           FieldLo = Integer;
01409           FieldHi = EB_Hi ? Integer : NoClass;
01410         }
01411       } else
01412         classify(i->getType(), Offset, FieldLo, FieldHi);
01413       Lo = merge(Lo, FieldLo);
01414       Hi = merge(Hi, FieldHi);
01415       if (Lo == Memory || Hi == Memory)
01416         break;
01417     }
01418 
01419     postMerge(Size, Lo, Hi);
01420   }
01421 }
01422 
01423 ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
01424   // If this is a scalar LLVM value then assume LLVM will pass it in the right
01425   // place naturally.
01426   if (!isAggregateTypeForABI(Ty)) {
01427     // Treat an enum type as its underlying type.
01428     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
01429       Ty = EnumTy->getDecl()->getIntegerType();
01430 
01431     return (Ty->isPromotableIntegerType() ?
01432             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
01433   }
01434 
01435   return ABIArgInfo::getIndirect(0);
01436 }
01437 
01438 bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
01439   if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
01440     uint64_t Size = getContext().getTypeSize(VecTy);
01441     unsigned LargestVector = HasAVX ? 256 : 128;
01442     if (Size <= 64 || Size > LargestVector)
01443       return true;
01444   }
01445 
01446   return false;
01447 }
01448 
01449 ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
01450                                             unsigned freeIntRegs) const {
01451   // If this is a scalar LLVM value then assume LLVM will pass it in the right
01452   // place naturally.
01453   //
01454   // This assumption is optimistic, as there could be free registers available
01455   // when we need to pass this argument in memory, and LLVM could try to pass
01456   // the argument in the free register. This does not seem to happen currently,
01457   // but this code would be much safer if we could mark the argument with
01458   // 'onstack'. See PR12193.
01459   if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
01460     // Treat an enum type as its underlying type.
01461     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
01462       Ty = EnumTy->getDecl()->getIntegerType();
01463 
01464     return (Ty->isPromotableIntegerType() ?
01465             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
01466   }
01467 
01468   if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
01469     return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
01470 
01471   // Compute the byval alignment. We specify the alignment of the byval in all
01472   // cases so that the mid-level optimizer knows the alignment of the byval.
01473   unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
01474 
01475   // Attempt to avoid passing indirect results using byval when possible. This
01476   // is important for good codegen.
01477   //
01478   // We do this by coercing the value into a scalar type which the backend can
01479   // handle naturally (i.e., without using byval).
01480   //
01481   // For simplicity, we currently only do this when we have exhausted all of the
01482   // free integer registers. Doing this when there are free integer registers
01483   // would require more care, as we would have to ensure that the coerced value
01484   // did not claim the unused register. That would require either reording the
01485   // arguments to the function (so that any subsequent inreg values came first),
01486   // or only doing this optimization when there were no following arguments that
01487   // might be inreg.
01488   //
01489   // We currently expect it to be rare (particularly in well written code) for
01490   // arguments to be passed on the stack when there are still free integer
01491   // registers available (this would typically imply large structs being passed
01492   // by value), so this seems like a fair tradeoff for now.
01493   //
01494   // We can revisit this if the backend grows support for 'onstack' parameter
01495   // attributes. See PR12193.
01496   if (freeIntRegs == 0) {
01497     uint64_t Size = getContext().getTypeSize(Ty);
01498 
01499     // If this type fits in an eightbyte, coerce it into the matching integral
01500     // type, which will end up on the stack (with alignment 8).
01501     if (Align == 8 && Size <= 64)
01502       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
01503                                                           Size));
01504   }
01505 
01506   return ABIArgInfo::getIndirect(Align);
01507 }
01508 
01509 /// GetByteVectorType - The ABI specifies that a value should be passed in an
01510 /// full vector XMM/YMM register.  Pick an LLVM IR type that will be passed as a
01511 /// vector register.
01512 llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
01513   llvm::Type *IRType = CGT.ConvertType(Ty);
01514 
01515   // Wrapper structs that just contain vectors are passed just like vectors,
01516   // strip them off if present.
01517   llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
01518   while (STy && STy->getNumElements() == 1) {
01519     IRType = STy->getElementType(0);
01520     STy = dyn_cast<llvm::StructType>(IRType);
01521   }
01522 
01523   // If the preferred type is a 16-byte vector, prefer to pass it.
01524   if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
01525     llvm::Type *EltTy = VT->getElementType();
01526     unsigned BitWidth = VT->getBitWidth();
01527     if ((BitWidth >= 128 && BitWidth <= 256) &&
01528         (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
01529          EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
01530          EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
01531          EltTy->isIntegerTy(128)))
01532       return VT;
01533   }
01534 
01535   return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
01536 }
01537 
01538 /// BitsContainNoUserData - Return true if the specified [start,end) bit range
01539 /// is known to either be off the end of the specified type or being in
01540 /// alignment padding.  The user type specified is known to be at most 128 bits
01541 /// in size, and have passed through X86_64ABIInfo::classify with a successful
01542 /// classification that put one of the two halves in the INTEGER class.
01543 ///
01544 /// It is conservatively correct to return false.
01545 static bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
01546                                   unsigned EndBit, ASTContext &Context) {
01547   // If the bytes being queried are off the end of the type, there is no user
01548   // data hiding here.  This handles analysis of builtins, vectors and other
01549   // types that don't contain interesting padding.
01550   unsigned TySize = (unsigned)Context.getTypeSize(Ty);
01551   if (TySize <= StartBit)
01552     return true;
01553 
01554   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
01555     unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
01556     unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
01557 
01558     // Check each element to see if the element overlaps with the queried range.
01559     for (unsigned i = 0; i != NumElts; ++i) {
01560       // If the element is after the span we care about, then we're done..
01561       unsigned EltOffset = i*EltSize;
01562       if (EltOffset >= EndBit) break;
01563 
01564       unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
01565       if (!BitsContainNoUserData(AT->getElementType(), EltStart,
01566                                  EndBit-EltOffset, Context))
01567         return false;
01568     }
01569     // If it overlaps no elements, then it is safe to process as padding.
01570     return true;
01571   }
01572 
01573   if (const RecordType *RT = Ty->getAs<RecordType>()) {
01574     const RecordDecl *RD = RT->getDecl();
01575     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
01576 
01577     // If this is a C++ record, check the bases first.
01578     if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
01579       for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
01580            e = CXXRD->bases_end(); i != e; ++i) {
01581         assert(!i->isVirtual() && !i->getType()->isDependentType() &&
01582                "Unexpected base class!");
01583         const CXXRecordDecl *Base =
01584           cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
01585 
01586         // If the base is after the span we care about, ignore it.
01587         unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
01588         if (BaseOffset >= EndBit) continue;
01589 
01590         unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
01591         if (!BitsContainNoUserData(i->getType(), BaseStart,
01592                                    EndBit-BaseOffset, Context))
01593           return false;
01594       }
01595     }
01596 
01597     // Verify that no field has data that overlaps the region of interest.  Yes
01598     // this could be sped up a lot by being smarter about queried fields,
01599     // however we're only looking at structs up to 16 bytes, so we don't care
01600     // much.
01601     unsigned idx = 0;
01602     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
01603          i != e; ++i, ++idx) {
01604       unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
01605 
01606       // If we found a field after the region we care about, then we're done.
01607       if (FieldOffset >= EndBit) break;
01608 
01609       unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
01610       if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
01611                                  Context))
01612         return false;
01613     }
01614 
01615     // If nothing in this record overlapped the area of interest, then we're
01616     // clean.
01617     return true;
01618   }
01619 
01620   return false;
01621 }
01622 
01623 /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
01624 /// float member at the specified offset.  For example, {int,{float}} has a
01625 /// float at offset 4.  It is conservatively correct for this routine to return
01626 /// false.
01627 static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
01628                                   const llvm::TargetData &TD) {
01629   // Base case if we find a float.
01630   if (IROffset == 0 && IRType->isFloatTy())
01631     return true;
01632 
01633   // If this is a struct, recurse into the field at the specified offset.
01634   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
01635     const llvm::StructLayout *SL = TD.getStructLayout(STy);
01636     unsigned Elt = SL->getElementContainingOffset(IROffset);
01637     IROffset -= SL->getElementOffset(Elt);
01638     return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
01639   }
01640 
01641   // If this is an array, recurse into the field at the specified offset.
01642   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
01643     llvm::Type *EltTy = ATy->getElementType();
01644     unsigned EltSize = TD.getTypeAllocSize(EltTy);
01645     IROffset -= IROffset/EltSize*EltSize;
01646     return ContainsFloatAtOffset(EltTy, IROffset, TD);
01647   }
01648 
01649   return false;
01650 }
01651 
01652 
01653 /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
01654 /// low 8 bytes of an XMM register, corresponding to the SSE class.
01655 llvm::Type *X86_64ABIInfo::
01656 GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
01657                    QualType SourceTy, unsigned SourceOffset) const {
01658   // The only three choices we have are either double, <2 x float>, or float. We
01659   // pass as float if the last 4 bytes is just padding.  This happens for
01660   // structs that contain 3 floats.
01661   if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
01662                             SourceOffset*8+64, getContext()))
01663     return llvm::Type::getFloatTy(getVMContext());
01664 
01665   // We want to pass as <2 x float> if the LLVM IR type contains a float at
01666   // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
01667   // case.
01668   if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
01669       ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
01670     return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
01671 
01672   return llvm::Type::getDoubleTy(getVMContext());
01673 }
01674 
01675 
01676 /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
01677 /// an 8-byte GPR.  This means that we either have a scalar or we are talking
01678 /// about the high or low part of an up-to-16-byte struct.  This routine picks
01679 /// the best LLVM IR type to represent this, which may be i64 or may be anything
01680 /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
01681 /// etc).
01682 ///
01683 /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
01684 /// the source type.  IROffset is an offset in bytes into the LLVM IR type that
01685 /// the 8-byte value references.  PrefType may be null.
01686 ///
01687 /// SourceTy is the source level type for the entire argument.  SourceOffset is
01688 /// an offset into this that we're processing (which is always either 0 or 8).
01689 ///
01690 llvm::Type *X86_64ABIInfo::
01691 GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
01692                        QualType SourceTy, unsigned SourceOffset) const {
01693   // If we're dealing with an un-offset LLVM IR type, then it means that we're
01694   // returning an 8-byte unit starting with it.  See if we can safely use it.
01695   if (IROffset == 0) {
01696     // Pointers and int64's always fill the 8-byte unit.
01697     if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
01698       return IRType;
01699 
01700     // If we have a 1/2/4-byte integer, we can use it only if the rest of the
01701     // goodness in the source type is just tail padding.  This is allowed to
01702     // kick in for struct {double,int} on the int, but not on
01703     // struct{double,int,int} because we wouldn't return the second int.  We
01704     // have to do this analysis on the source type because we can't depend on
01705     // unions being lowered a specific way etc.
01706     if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
01707         IRType->isIntegerTy(32)) {
01708       unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
01709 
01710       if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
01711                                 SourceOffset*8+64, getContext()))
01712         return IRType;
01713     }
01714   }
01715 
01716   if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
01717     // If this is a struct, recurse into the field at the specified offset.
01718     const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
01719     if (IROffset < SL->getSizeInBytes()) {
01720       unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
01721       IROffset -= SL->getElementOffset(FieldIdx);
01722 
01723       return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
01724                                     SourceTy, SourceOffset);
01725     }
01726   }
01727 
01728   if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
01729     llvm::Type *EltTy = ATy->getElementType();
01730     unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
01731     unsigned EltOffset = IROffset/EltSize*EltSize;
01732     return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
01733                                   SourceOffset);
01734   }
01735 
01736   // Okay, we don't have any better idea of what to pass, so we pass this in an
01737   // integer register that isn't too big to fit the rest of the struct.
01738   unsigned TySizeInBytes =
01739     (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
01740 
01741   assert(TySizeInBytes != SourceOffset && "Empty field?");
01742 
01743   // It is always safe to classify this as an integer type up to i64 that
01744   // isn't larger than the structure.
01745   return llvm::IntegerType::get(getVMContext(),
01746                                 std::min(TySizeInBytes-SourceOffset, 8U)*8);
01747 }
01748 
01749 
01750 /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
01751 /// be used as elements of a two register pair to pass or return, return a
01752 /// first class aggregate to represent them.  For example, if the low part of
01753 /// a by-value argument should be passed as i32* and the high part as float,
01754 /// return {i32*, float}.
01755 static llvm::Type *
01756 GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
01757                            const llvm::TargetData &TD) {
01758   // In order to correctly satisfy the ABI, we need to the high part to start
01759   // at offset 8.  If the high and low parts we inferred are both 4-byte types
01760   // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
01761   // the second element at offset 8.  Check for this:
01762   unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
01763   unsigned HiAlign = TD.getABITypeAlignment(Hi);
01764   unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
01765   assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
01766 
01767   // To handle this, we have to increase the size of the low part so that the
01768   // second element will start at an 8 byte offset.  We can't increase the size
01769   // of the second element because it might make us access off the end of the
01770   // struct.
01771   if (HiStart != 8) {
01772     // There are only two sorts of types the ABI generation code can produce for
01773     // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
01774     // Promote these to a larger type.
01775     if (Lo->isFloatTy())
01776       Lo = llvm::Type::getDoubleTy(Lo->getContext());
01777     else {
01778       assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
01779       Lo = llvm::Type::getInt64Ty(Lo->getContext());
01780     }
01781   }
01782 
01783   llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
01784 
01785 
01786   // Verify that the second element is at an 8-byte offset.
01787   assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
01788          "Invalid x86-64 argument pair!");
01789   return Result;
01790 }
01791 
01792 ABIArgInfo X86_64ABIInfo::
01793 classifyReturnType(QualType RetTy) const {
01794   // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
01795   // classification algorithm.
01796   X86_64ABIInfo::Class Lo, Hi;
01797   classify(RetTy, 0, Lo, Hi);
01798 
01799   // Check some invariants.
01800   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
01801   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
01802 
01803   llvm::Type *ResType = 0;
01804   switch (Lo) {
01805   case NoClass:
01806     if (Hi == NoClass)
01807       return ABIArgInfo::getIgnore();
01808     // If the low part is just padding, it takes no register, leave ResType
01809     // null.
01810     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
01811            "Unknown missing lo part");
01812     break;
01813 
01814   case SSEUp:
01815   case X87Up:
01816     llvm_unreachable("Invalid classification for lo word.");
01817 
01818     // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
01819     // hidden argument.
01820   case Memory:
01821     return getIndirectReturnResult(RetTy);
01822 
01823     // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
01824     // available register of the sequence %rax, %rdx is used.
01825   case Integer:
01826     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
01827 
01828     // If we have a sign or zero extended integer, make sure to return Extend
01829     // so that the parameter gets the right LLVM IR attributes.
01830     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
01831       // Treat an enum type as its underlying type.
01832       if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
01833         RetTy = EnumTy->getDecl()->getIntegerType();
01834 
01835       if (RetTy->isIntegralOrEnumerationType() &&
01836           RetTy->isPromotableIntegerType())
01837         return ABIArgInfo::getExtend();
01838     }
01839     break;
01840 
01841     // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
01842     // available SSE register of the sequence %xmm0, %xmm1 is used.
01843   case SSE:
01844     ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
01845     break;
01846 
01847     // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
01848     // returned on the X87 stack in %st0 as 80-bit x87 number.
01849   case X87:
01850     ResType = llvm::Type::getX86_FP80Ty(getVMContext());
01851     break;
01852 
01853     // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
01854     // part of the value is returned in %st0 and the imaginary part in
01855     // %st1.
01856   case ComplexX87:
01857     assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
01858     ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
01859                                     llvm::Type::getX86_FP80Ty(getVMContext()),
01860                                     NULL);
01861     break;
01862   }
01863 
01864   llvm::Type *HighPart = 0;
01865   switch (Hi) {
01866     // Memory was handled previously and X87 should
01867     // never occur as a hi class.
01868   case Memory:
01869   case X87:
01870     llvm_unreachable("Invalid classification for hi word.");
01871 
01872   case ComplexX87: // Previously handled.
01873   case NoClass:
01874     break;
01875 
01876   case Integer:
01877     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
01878     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
01879       return ABIArgInfo::getDirect(HighPart, 8);
01880     break;
01881   case SSE:
01882     HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
01883     if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
01884       return ABIArgInfo::getDirect(HighPart, 8);
01885     break;
01886 
01887     // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
01888     // is passed in the next available eightbyte chunk if the last used
01889     // vector register.
01890     //
01891     // SSEUP should always be preceded by SSE, just widen.
01892   case SSEUp:
01893     assert(Lo == SSE && "Unexpected SSEUp classification.");
01894     ResType = GetByteVectorType(RetTy);
01895     break;
01896 
01897     // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
01898     // returned together with the previous X87 value in %st0.
01899   case X87Up:
01900     // If X87Up is preceded by X87, we don't need to do
01901     // anything. However, in some cases with unions it may not be
01902     // preceded by X87. In such situations we follow gcc and pass the
01903     // extra bits in an SSE reg.
01904     if (Lo != X87) {
01905       HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
01906       if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
01907         return ABIArgInfo::getDirect(HighPart, 8);
01908     }
01909     break;
01910   }
01911 
01912   // If a high part was specified, merge it together with the low part.  It is
01913   // known to pass in the high eightbyte of the result.  We do this by forming a
01914   // first class struct aggregate with the high and low part: {low, high}
01915   if (HighPart)
01916     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
01917 
01918   return ABIArgInfo::getDirect(ResType);
01919 }
01920 
01921 ABIArgInfo X86_64ABIInfo::classifyArgumentType(
01922   QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
01923   const
01924 {
01925   X86_64ABIInfo::Class Lo, Hi;
01926   classify(Ty, 0, Lo, Hi);
01927 
01928   // Check some invariants.
01929   // FIXME: Enforce these by construction.
01930   assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
01931   assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
01932 
01933   neededInt = 0;
01934   neededSSE = 0;
01935   llvm::Type *ResType = 0;
01936   switch (Lo) {
01937   case NoClass:
01938     if (Hi == NoClass)
01939       return ABIArgInfo::getIgnore();
01940     // If the low part is just padding, it takes no register, leave ResType
01941     // null.
01942     assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
01943            "Unknown missing lo part");
01944     break;
01945 
01946     // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
01947     // on the stack.
01948   case Memory:
01949 
01950     // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
01951     // COMPLEX_X87, it is passed in memory.
01952   case X87:
01953   case ComplexX87:
01954     if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
01955       ++neededInt;
01956     return getIndirectResult(Ty, freeIntRegs);
01957 
01958   case SSEUp:
01959   case X87Up:
01960     llvm_unreachable("Invalid classification for lo word.");
01961 
01962     // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
01963     // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
01964     // and %r9 is used.
01965   case Integer:
01966     ++neededInt;
01967 
01968     // Pick an 8-byte type based on the preferred type.
01969     ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
01970 
01971     // If we have a sign or zero extended integer, make sure to return Extend
01972     // so that the parameter gets the right LLVM IR attributes.
01973     if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
01974       // Treat an enum type as its underlying type.
01975       if (const EnumType *EnumTy = Ty->getAs<EnumType>())
01976         Ty = EnumTy->getDecl()->getIntegerType();
01977 
01978       if (Ty->isIntegralOrEnumerationType() &&
01979           Ty->isPromotableIntegerType())
01980         return ABIArgInfo::getExtend();
01981     }
01982 
01983     break;
01984 
01985     // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
01986     // available SSE register is used, the registers are taken in the
01987     // order from %xmm0 to %xmm7.
01988   case SSE: {
01989     llvm::Type *IRType = CGT.ConvertType(Ty);
01990     ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
01991     ++neededSSE;
01992     break;
01993   }
01994   }
01995 
01996   llvm::Type *HighPart = 0;
01997   switch (Hi) {
01998     // Memory was handled previously, ComplexX87 and X87 should
01999     // never occur as hi classes, and X87Up must be preceded by X87,
02000     // which is passed in memory.
02001   case Memory:
02002   case X87:
02003   case ComplexX87:
02004     llvm_unreachable("Invalid classification for hi word.");
02005 
02006   case NoClass: break;
02007 
02008   case Integer:
02009     ++neededInt;
02010     // Pick an 8-byte type based on the preferred type.
02011     HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
02012 
02013     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
02014       return ABIArgInfo::getDirect(HighPart, 8);
02015     break;
02016 
02017     // X87Up generally doesn't occur here (long double is passed in
02018     // memory), except in situations involving unions.
02019   case X87Up:
02020   case SSE:
02021     HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
02022 
02023     if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
02024       return ABIArgInfo::getDirect(HighPart, 8);
02025 
02026     ++neededSSE;
02027     break;
02028 
02029     // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
02030     // eightbyte is passed in the upper half of the last used SSE
02031     // register.  This only happens when 128-bit vectors are passed.
02032   case SSEUp:
02033     assert(Lo == SSE && "Unexpected SSEUp classification");
02034     ResType = GetByteVectorType(Ty);
02035     break;
02036   }
02037 
02038   // If a high part was specified, merge it together with the low part.  It is
02039   // known to pass in the high eightbyte of the result.  We do this by forming a
02040   // first class struct aggregate with the high and low part: {low, high}
02041   if (HighPart)
02042     ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
02043 
02044   return ABIArgInfo::getDirect(ResType);
02045 }
02046 
02047 void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
02048 
02049   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
02050 
02051   // Keep track of the number of assigned registers.
02052   unsigned freeIntRegs = 6, freeSSERegs = 8;
02053 
02054   // If the return value is indirect, then the hidden argument is consuming one
02055   // integer register.
02056   if (FI.getReturnInfo().isIndirect())
02057     --freeIntRegs;
02058 
02059   // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
02060   // get assigned (in left-to-right order) for passing as follows...
02061   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
02062        it != ie; ++it) {
02063     unsigned neededInt, neededSSE;
02064     it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
02065                                     neededSSE);
02066 
02067     // AMD64-ABI 3.2.3p3: If there are no registers available for any
02068     // eightbyte of an argument, the whole argument is passed on the
02069     // stack. If registers have already been assigned for some
02070     // eightbytes of such an argument, the assignments get reverted.
02071     if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
02072       freeIntRegs -= neededInt;
02073       freeSSERegs -= neededSSE;
02074     } else {
02075       it->info = getIndirectResult(it->type, freeIntRegs);
02076     }
02077   }
02078 }
02079 
02080 static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
02081                                         QualType Ty,
02082                                         CodeGenFunction &CGF) {
02083   llvm::Value *overflow_arg_area_p =
02084     CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
02085   llvm::Value *overflow_arg_area =
02086     CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
02087 
02088   // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
02089   // byte boundary if alignment needed by type exceeds 8 byte boundary.
02090   // It isn't stated explicitly in the standard, but in practice we use
02091   // alignment greater than 16 where necessary.
02092   uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
02093   if (Align > 8) {
02094     // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
02095     llvm::Value *Offset =
02096       llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
02097     overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
02098     llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
02099                                                     CGF.Int64Ty);
02100     llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
02101     overflow_arg_area =
02102       CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
02103                                  overflow_arg_area->getType(),
02104                                  "overflow_arg_area.align");
02105   }
02106 
02107   // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
02108   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
02109   llvm::Value *Res =
02110     CGF.Builder.CreateBitCast(overflow_arg_area,
02111                               llvm::PointerType::getUnqual(LTy));
02112 
02113   // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
02114   // l->overflow_arg_area + sizeof(type).
02115   // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
02116   // an 8 byte boundary.
02117 
02118   uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
02119   llvm::Value *Offset =
02120       llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
02121   overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
02122                                             "overflow_arg_area.next");
02123   CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
02124 
02125   // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
02126   return Res;
02127 }
02128 
02129 llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
02130                                       CodeGenFunction &CGF) const {
02131   // Assume that va_list type is correct; should be pointer to LLVM type:
02132   // struct {
02133   //   i32 gp_offset;
02134   //   i32 fp_offset;
02135   //   i8* overflow_arg_area;
02136   //   i8* reg_save_area;
02137   // };
02138   unsigned neededInt, neededSSE;
02139 
02140   Ty = CGF.getContext().getCanonicalType(Ty);
02141   ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
02142 
02143   // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
02144   // in the registers. If not go to step 7.
02145   if (!neededInt && !neededSSE)
02146     return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
02147 
02148   // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
02149   // general purpose registers needed to pass type and num_fp to hold
02150   // the number of floating point registers needed.
02151 
02152   // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
02153   // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
02154   // l->fp_offset > 304 - num_fp * 16 go to step 7.
02155   //
02156   // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
02157   // register save space).
02158 
02159   llvm::Value *InRegs = 0;
02160   llvm::Value *gp_offset_p = 0, *gp_offset = 0;
02161   llvm::Value *fp_offset_p = 0, *fp_offset = 0;
02162   if (neededInt) {
02163     gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
02164     gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
02165     InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
02166     InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
02167   }
02168 
02169   if (neededSSE) {
02170     fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
02171     fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
02172     llvm::Value *FitsInFP =
02173       llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
02174     FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
02175     InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
02176   }
02177 
02178   llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
02179   llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
02180   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
02181   CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
02182 
02183   // Emit code to load the value if it was passed in registers.
02184 
02185   CGF.EmitBlock(InRegBlock);
02186 
02187   // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
02188   // an offset of l->gp_offset and/or l->fp_offset. This may require
02189   // copying to a temporary location in case the parameter is passed
02190   // in different register classes or requires an alignment greater
02191   // than 8 for general purpose registers and 16 for XMM registers.
02192   //
02193   // FIXME: This really results in shameful code when we end up needing to
02194   // collect arguments from different places; often what should result in a
02195   // simple assembling of a structure from scattered addresses has many more
02196   // loads than necessary. Can we clean this up?
02197   llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
02198   llvm::Value *RegAddr =
02199     CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
02200                            "reg_save_area");
02201   if (neededInt && neededSSE) {
02202     // FIXME: Cleanup.
02203     assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
02204     llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
02205     llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
02206     assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
02207     llvm::Type *TyLo = ST->getElementType(0);
02208     llvm::Type *TyHi = ST->getElementType(1);
02209     assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
02210            "Unexpected ABI info for mixed regs");
02211     llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
02212     llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
02213     llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
02214     llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
02215     llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
02216     llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
02217     llvm::Value *V =
02218       CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
02219     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
02220     V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
02221     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
02222 
02223     RegAddr = CGF.Builder.CreateBitCast(Tmp,
02224                                         llvm::PointerType::getUnqual(LTy));
02225   } else if (neededInt) {
02226     RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
02227     RegAddr = CGF.Builder.CreateBitCast(RegAddr,
02228                                         llvm::PointerType::getUnqual(LTy));
02229   } else if (neededSSE == 1) {
02230     RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
02231     RegAddr = CGF.Builder.CreateBitCast(RegAddr,
02232                                         llvm::PointerType::getUnqual(LTy));
02233   } else {
02234     assert(neededSSE == 2 && "Invalid number of needed registers!");
02235     // SSE registers are spaced 16 bytes apart in the register save
02236     // area, we need to collect the two eightbytes together.
02237     llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
02238     llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
02239     llvm::Type *DoubleTy = CGF.DoubleTy;
02240     llvm::Type *DblPtrTy =
02241       llvm::PointerType::getUnqual(DoubleTy);
02242     llvm::StructType *ST = llvm::StructType::get(DoubleTy,
02243                                                        DoubleTy, NULL);
02244     llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
02245     V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
02246                                                          DblPtrTy));
02247     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
02248     V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
02249                                                          DblPtrTy));
02250     CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
02251     RegAddr = CGF.Builder.CreateBitCast(Tmp,
02252                                         llvm::PointerType::getUnqual(LTy));
02253   }
02254 
02255   // AMD64-ABI 3.5.7p5: Step 5. Set:
02256   // l->gp_offset = l->gp_offset + num_gp * 8
02257   // l->fp_offset = l->fp_offset + num_fp * 16.
02258   if (neededInt) {
02259     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
02260     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
02261                             gp_offset_p);
02262   }
02263   if (neededSSE) {
02264     llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
02265     CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
02266                             fp_offset_p);
02267   }
02268   CGF.EmitBranch(ContBlock);
02269 
02270   // Emit code to load the value if it was passed in memory.
02271 
02272   CGF.EmitBlock(InMemBlock);
02273   llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
02274 
02275   // Return the appropriate result.
02276 
02277   CGF.EmitBlock(ContBlock);
02278   llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
02279                                                  "vaarg.addr");
02280   ResAddr->addIncoming(RegAddr, InRegBlock);
02281   ResAddr->addIncoming(MemAddr, InMemBlock);
02282   return ResAddr;
02283 }
02284 
02285 ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
02286 
02287   if (Ty->isVoidType())
02288     return ABIArgInfo::getIgnore();
02289 
02290   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
02291     Ty = EnumTy->getDecl()->getIntegerType();
02292 
02293   uint64_t Size = getContext().getTypeSize(Ty);
02294 
02295   if (const RecordType *RT = Ty->getAs<RecordType>()) {
02296     if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
02297         RT->getDecl()->hasFlexibleArrayMember())
02298       return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
02299 
02300     // FIXME: mingw-w64-gcc emits 128-bit struct as i128
02301     if (Size == 128 &&
02302         getContext().getTargetInfo().getTriple().getOS()
02303           == llvm::Triple::MinGW32)
02304       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
02305                                                           Size));
02306 
02307     // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
02308     // not 1, 2, 4, or 8 bytes, must be passed by reference."
02309     if (Size <= 64 &&
02310         (Size & (Size - 1)) == 0)
02311       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
02312                                                           Size));
02313 
02314     return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
02315   }
02316 
02317   if (Ty->isPromotableIntegerType())
02318     return ABIArgInfo::getExtend();
02319 
02320   return ABIArgInfo::getDirect();
02321 }
02322 
02323 void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
02324 
02325   QualType RetTy = FI.getReturnType();
02326   FI.getReturnInfo() = classify(RetTy);
02327 
02328   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
02329        it != ie; ++it)
02330     it->info = classify(it->type);
02331 }
02332 
02333 llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
02334                                       CodeGenFunction &CGF) const {
02335   llvm::Type *BPP = CGF.Int8PtrPtrTy;
02336 
02337   CGBuilderTy &Builder = CGF.Builder;
02338   llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
02339                                                        "ap");
02340   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
02341   llvm::Type *PTy =
02342     llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
02343   llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
02344 
02345   uint64_t Offset =
02346     llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
02347   llvm::Value *NextAddr =
02348     Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
02349                       "ap.next");
02350   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
02351 
02352   return AddrTyped;
02353 }
02354 
02355 // PowerPC-32
02356 
02357 namespace {
02358 class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
02359 public:
02360   PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
02361 
02362   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
02363     // This is recovered from gcc output.
02364     return 1; // r1 is the dedicated stack pointer
02365   }
02366 
02367   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
02368                                llvm::Value *Address) const;
02369 };
02370 
02371 }
02372 
02373 bool
02374 PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
02375                                                 llvm::Value *Address) const {
02376   // This is calculated from the LLVM and GCC tables and verified
02377   // against gcc output.  AFAIK all ABIs use the same encoding.
02378 
02379   CodeGen::CGBuilderTy &Builder = CGF.Builder;
02380 
02381   llvm::IntegerType *i8 = CGF.Int8Ty;
02382   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
02383   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
02384   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
02385 
02386   // 0-31: r0-31, the 4-byte general-purpose registers
02387   AssignToArrayRange(Builder, Address, Four8, 0, 31);
02388 
02389   // 32-63: fp0-31, the 8-byte floating-point registers
02390   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
02391 
02392   // 64-76 are various 4-byte special-purpose registers:
02393   // 64: mq
02394   // 65: lr
02395   // 66: ctr
02396   // 67: ap
02397   // 68-75 cr0-7
02398   // 76: xer
02399   AssignToArrayRange(Builder, Address, Four8, 64, 76);
02400 
02401   // 77-108: v0-31, the 16-byte vector registers
02402   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
02403 
02404   // 109: vrsave
02405   // 110: vscr
02406   // 111: spe_acc
02407   // 112: spefscr
02408   // 113: sfp
02409   AssignToArrayRange(Builder, Address, Four8, 109, 113);
02410 
02411   return false;
02412 }
02413 
02414 // PowerPC-64
02415 
02416 namespace {
02417 class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
02418 public:
02419   PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
02420 
02421   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
02422     // This is recovered from gcc output.
02423     return 1; // r1 is the dedicated stack pointer
02424   }
02425 
02426   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
02427                                llvm::Value *Address) const;
02428 };
02429 
02430 }
02431 
02432 bool
02433 PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
02434                                                 llvm::Value *Address) const {
02435   // This is calculated from the LLVM and GCC tables and verified
02436   // against gcc output.  AFAIK all ABIs use the same encoding.
02437 
02438   CodeGen::CGBuilderTy &Builder = CGF.Builder;
02439 
02440   llvm::IntegerType *i8 = CGF.Int8Ty;
02441   llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
02442   llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
02443   llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
02444 
02445   // 0-31: r0-31, the 8-byte general-purpose registers
02446   AssignToArrayRange(Builder, Address, Eight8, 0, 31);
02447 
02448   // 32-63: fp0-31, the 8-byte floating-point registers
02449   AssignToArrayRange(Builder, Address, Eight8, 32, 63);
02450 
02451   // 64-76 are various 4-byte special-purpose registers:
02452   // 64: mq
02453   // 65: lr
02454   // 66: ctr
02455   // 67: ap
02456   // 68-75 cr0-7
02457   // 76: xer
02458   AssignToArrayRange(Builder, Address, Four8, 64, 76);
02459 
02460   // 77-108: v0-31, the 16-byte vector registers
02461   AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
02462 
02463   // 109: vrsave
02464   // 110: vscr
02465   // 111: spe_acc
02466   // 112: spefscr
02467   // 113: sfp
02468   AssignToArrayRange(Builder, Address, Four8, 109, 113);
02469 
02470   return false;
02471 }
02472 
02473 //===----------------------------------------------------------------------===//
02474 // ARM ABI Implementation
02475 //===----------------------------------------------------------------------===//
02476 
02477 namespace {
02478 
02479 class ARMABIInfo : public ABIInfo {
02480 public:
02481   enum ABIKind {
02482     APCS = 0,
02483     AAPCS = 1,
02484     AAPCS_VFP
02485   };
02486 
02487 private:
02488   ABIKind Kind;
02489 
02490 public:
02491   ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
02492 
02493   bool isEABI() const {
02494     StringRef Env =
02495       getContext().getTargetInfo().getTriple().getEnvironmentName();
02496     return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
02497   }
02498 
02499 private:
02500   ABIKind getABIKind() const { return Kind; }
02501 
02502   ABIArgInfo classifyReturnType(QualType RetTy) const;
02503   ABIArgInfo classifyArgumentType(QualType RetTy) const;
02504 
02505   virtual void computeInfo(CGFunctionInfo &FI) const;
02506 
02507   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
02508                                  CodeGenFunction &CGF) const;
02509 };
02510 
02511 class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
02512 public:
02513   ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
02514     :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
02515 
02516   const ARMABIInfo &getABIInfo() const {
02517     return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
02518   }
02519 
02520   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
02521     return 13;
02522   }
02523 
02524   StringRef getARCRetainAutoreleasedReturnValueMarker() const {
02525     return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
02526   }
02527 
02528   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
02529                                llvm::Value *Address) const {
02530     llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
02531 
02532     // 0-15 are the 16 integer registers.
02533     AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
02534     return false;
02535   }
02536 
02537   unsigned getSizeOfUnwindException() const {
02538     if (getABIInfo().isEABI()) return 88;
02539     return TargetCodeGenInfo::getSizeOfUnwindException();
02540   }
02541 };
02542 
02543 }
02544 
02545 void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
02546   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
02547   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
02548        it != ie; ++it)
02549     it->info = classifyArgumentType(it->type);
02550 
02551   // Always honor user-specified calling convention.
02552   if (FI.getCallingConvention() != llvm::CallingConv::C)
02553     return;
02554 
02555   // Calling convention as default by an ABI.
02556   llvm::CallingConv::ID DefaultCC;
02557   if (isEABI())
02558     DefaultCC = llvm::CallingConv::ARM_AAPCS;
02559   else
02560     DefaultCC = llvm::CallingConv::ARM_APCS;
02561 
02562   // If user did not ask for specific calling convention explicitly (e.g. via
02563   // pcs attribute), set effective calling convention if it's different than ABI
02564   // default.
02565   switch (getABIKind()) {
02566   case APCS:
02567     if (DefaultCC != llvm::CallingConv::ARM_APCS)
02568       FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
02569     break;
02570   case AAPCS:
02571     if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
02572       FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
02573     break;
02574   case AAPCS_VFP:
02575     if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
02576       FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
02577     break;
02578   }
02579 }
02580 
02581 /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
02582 /// aggregate.  If HAMembers is non-null, the number of base elements
02583 /// contained in the type is returned through it; this is used for the
02584 /// recursive calls that check aggregate component types.
02585 static bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
02586                                    ASTContext &Context,
02587                                    uint64_t *HAMembers = 0) {
02588   uint64_t Members = 0;
02589   if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
02590     if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
02591       return false;
02592     Members *= AT->getSize().getZExtValue();
02593   } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
02594     const RecordDecl *RD = RT->getDecl();
02595     if (RD->hasFlexibleArrayMember())
02596       return false;
02597 
02598     Members = 0;
02599     for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
02600          i != e; ++i) {
02601       const FieldDecl *FD = &*i;
02602       uint64_t FldMembers;
02603       if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
02604         return false;
02605 
02606       Members = (RD->isUnion() ?
02607                  std::max(Members, FldMembers) : Members + FldMembers);
02608     }
02609   } else {
02610     Members = 1;
02611     if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
02612       Members = 2;
02613       Ty = CT->getElementType();
02614     }
02615 
02616     // Homogeneous aggregates for AAPCS-VFP must have base types of float,
02617     // double, or 64-bit or 128-bit vectors.
02618     if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
02619       if (BT->getKind() != BuiltinType::Float && 
02620           BT->getKind() != BuiltinType::Double)
02621         return false;
02622     } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
02623       unsigned VecSize = Context.getTypeSize(VT);
02624       if (VecSize != 64 && VecSize != 128)
02625         return false;
02626     } else {
02627       return false;
02628     }
02629 
02630     // The base type must be the same for all members.  Vector types of the
02631     // same total size are treated as being equivalent here.
02632     const Type *TyPtr = Ty.getTypePtr();
02633     if (!Base)
02634       Base = TyPtr;
02635     if (Base != TyPtr &&
02636         (!Base->isVectorType() || !TyPtr->isVectorType() ||
02637          Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
02638       return false;
02639   }
02640 
02641   // Homogeneous Aggregates can have at most 4 members of the base type.
02642   if (HAMembers)
02643     *HAMembers = Members;
02644 
02645   return (Members > 0 && Members <= 4);
02646 }
02647 
02648 ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
02649   if (!isAggregateTypeForABI(Ty)) {
02650     // Treat an enum type as its underlying type.
02651     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
02652       Ty = EnumTy->getDecl()->getIntegerType();
02653 
02654     return (Ty->isPromotableIntegerType() ?
02655             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
02656   }
02657 
02658   // Ignore empty records.
02659   if (isEmptyRecord(getContext(), Ty, true))
02660     return ABIArgInfo::getIgnore();
02661 
02662   // Structures with either a non-trivial destructor or a non-trivial
02663   // copy constructor are always indirect.
02664   if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
02665     return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
02666 
02667   if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
02668     // Homogeneous Aggregates need to be expanded.
02669     const Type *Base = 0;
02670     if (isHomogeneousAggregate(Ty, Base, getContext())) {
02671       assert(Base && "Base class should be set for homogeneous aggregate");
02672       return ABIArgInfo::getExpand();
02673     }
02674   }
02675 
02676   // Otherwise, pass by coercing to a structure of the appropriate size.
02677   //
02678   // FIXME: This is kind of nasty... but there isn't much choice because the ARM
02679   // backend doesn't support byval.
02680   // FIXME: This doesn't handle alignment > 64 bits.
02681   llvm::Type* ElemTy;
02682   unsigned SizeRegs;
02683   if (getContext().getTypeAlign(Ty) > 32) {
02684     ElemTy = llvm::Type::getInt64Ty(getVMContext());
02685     SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
02686   } else {
02687     ElemTy = llvm::Type::getInt32Ty(getVMContext());
02688     SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
02689   }
02690 
02691   llvm::Type *STy =
02692     llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
02693   return ABIArgInfo::getDirect(STy);
02694 }
02695 
02696 static bool isIntegerLikeType(QualType Ty, ASTContext &Context,
02697                               llvm::LLVMContext &VMContext) {
02698   // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
02699   // is called integer-like if its size is less than or equal to one word, and
02700   // the offset of each of its addressable sub-fields is zero.
02701 
02702   uint64_t Size = Context.getTypeSize(Ty);
02703 
02704   // Check that the type fits in a word.
02705   if (Size > 32)
02706     return false;
02707 
02708   // FIXME: Handle vector types!
02709   if (Ty->isVectorType())
02710     return false;
02711 
02712   // Float types are never treated as "integer like".
02713   if (Ty->isRealFloatingType())
02714     return false;
02715 
02716   // If this is a builtin or pointer type then it is ok.
02717   if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
02718     return true;
02719 
02720   // Small complex integer types are "integer like".
02721   if (const ComplexType *CT = Ty->getAs<ComplexType>())
02722     return isIntegerLikeType(CT->getElementType(), Context, VMContext);
02723 
02724   // Single element and zero sized arrays should be allowed, by the definition
02725   // above, but they are not.
02726 
02727   // Otherwise, it must be a record type.
02728   const RecordType *RT = Ty->getAs<RecordType>();
02729   if (!RT) return false;
02730 
02731   // Ignore records with flexible arrays.
02732   const RecordDecl *RD = RT->getDecl();
02733   if (RD->hasFlexibleArrayMember())
02734     return false;
02735 
02736   // Check that all sub-fields are at offset 0, and are themselves "integer
02737   // like".
02738   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
02739 
02740   bool HadField = false;
02741   unsigned idx = 0;
02742   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
02743        i != e; ++i, ++idx) {
02744     const FieldDecl *FD = &*i;
02745 
02746     // Bit-fields are not addressable, we only need to verify they are "integer
02747     // like". We still have to disallow a subsequent non-bitfield, for example:
02748     //   struct { int : 0; int x }
02749     // is non-integer like according to gcc.
02750     if (FD->isBitField()) {
02751       if (!RD->isUnion())
02752         HadField = true;
02753 
02754       if (!isIntegerLikeType(FD->getType(), Context, VMContext))
02755         return false;
02756 
02757       continue;
02758     }
02759 
02760     // Check if this field is at offset 0.
02761     if (Layout.getFieldOffset(idx) != 0)
02762       return false;
02763 
02764     if (!isIntegerLikeType(FD->getType(), Context, VMContext))
02765       return false;
02766 
02767     // Only allow at most one field in a structure. This doesn't match the
02768     // wording above, but follows gcc in situations with a field following an
02769     // empty structure.
02770     if (!RD->isUnion()) {
02771       if (HadField)
02772         return false;
02773 
02774       HadField = true;
02775     }
02776   }
02777 
02778   return true;
02779 }
02780 
02781 ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
02782   if (RetTy->isVoidType())
02783     return ABIArgInfo::getIgnore();
02784 
02785   // Large vector types should be returned via memory.
02786   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
02787     return ABIArgInfo::getIndirect(0);
02788 
02789   if (!isAggregateTypeForABI(RetTy)) {
02790     // Treat an enum type as its underlying type.
02791     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
02792       RetTy = EnumTy->getDecl()->getIntegerType();
02793 
02794     return (RetTy->isPromotableIntegerType() ?
02795             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
02796   }
02797 
02798   // Structures with either a non-trivial destructor or a non-trivial
02799   // copy constructor are always indirect.
02800   if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
02801     return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
02802 
02803   // Are we following APCS?
02804   if (getABIKind() == APCS) {
02805     if (isEmptyRecord(getContext(), RetTy, false))
02806       return ABIArgInfo::getIgnore();
02807 
02808     // Complex types are all returned as packed integers.
02809     //
02810     // FIXME: Consider using 2 x vector types if the back end handles them
02811     // correctly.
02812     if (RetTy->isAnyComplexType())
02813       return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
02814                                               getContext().getTypeSize(RetTy)));
02815 
02816     // Integer like structures are returned in r0.
02817     if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
02818       // Return in the smallest viable integer type.
02819       uint64_t Size = getContext().getTypeSize(RetTy);
02820       if (Size <= 8)
02821         return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
02822       if (Size <= 16)
02823         return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
02824       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
02825     }
02826 
02827     // Otherwise return in memory.
02828     return ABIArgInfo::getIndirect(0);
02829   }
02830 
02831   // Otherwise this is an AAPCS variant.
02832 
02833   if (isEmptyRecord(getContext(), RetTy, true))
02834     return ABIArgInfo::getIgnore();
02835 
02836   // Check for homogeneous aggregates with AAPCS-VFP.
02837   if (getABIKind() == AAPCS_VFP) {
02838     const Type *Base = 0;
02839     if (isHomogeneousAggregate(RetTy, Base, getContext())) {
02840       assert(Base && "Base class should be set for homogeneous aggregate");
02841       // Homogeneous Aggregates are returned directly.
02842       return ABIArgInfo::getDirect();
02843     }
02844   }
02845 
02846   // Aggregates <= 4 bytes are returned in r0; other aggregates
02847   // are returned indirectly.
02848   uint64_t Size = getContext().getTypeSize(RetTy);
02849   if (Size <= 32) {
02850     // Return in the smallest viable integer type.
02851     if (Size <= 8)
02852       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
02853     if (Size <= 16)
02854       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
02855     return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
02856   }
02857 
02858   return ABIArgInfo::getIndirect(0);
02859 }
02860 
02861 llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
02862                                    CodeGenFunction &CGF) const {
02863   llvm::Type *BP = CGF.Int8PtrTy;
02864   llvm::Type *BPP = CGF.Int8PtrPtrTy;
02865 
02866   CGBuilderTy &Builder = CGF.Builder;
02867   llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
02868   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
02869   // Handle address alignment for type alignment > 32 bits
02870   uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
02871   if (TyAlign > 4) {
02872     assert((TyAlign & (TyAlign - 1)) == 0 &&
02873            "Alignment is not power of 2!");
02874     llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
02875     AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
02876     AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
02877     Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
02878   }
02879   llvm::Type *PTy =
02880     llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
02881   llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
02882 
02883   uint64_t Offset =
02884     llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
02885   llvm::Value *NextAddr =
02886     Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
02887                       "ap.next");
02888   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
02889 
02890   return AddrTyped;
02891 }
02892 
02893 //===----------------------------------------------------------------------===//
02894 // PTX ABI Implementation
02895 //===----------------------------------------------------------------------===//
02896 
02897 namespace {
02898 
02899 class PTXABIInfo : public ABIInfo {
02900 public:
02901   PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
02902 
02903   ABIArgInfo classifyReturnType(QualType RetTy) const;
02904   ABIArgInfo classifyArgumentType(QualType Ty) const;
02905 
02906   virtual void computeInfo(CGFunctionInfo &FI) const;
02907   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
02908                                  CodeGenFunction &CFG) const;
02909 };
02910 
02911 class PTXTargetCodeGenInfo : public TargetCodeGenInfo {
02912 public:
02913   PTXTargetCodeGenInfo(CodeGenTypes &CGT)
02914     : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
02915     
02916   virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
02917                                    CodeGen::CodeGenModule &M) const;
02918 };
02919 
02920 ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
02921   if (RetTy->isVoidType())
02922     return ABIArgInfo::getIgnore();
02923   if (isAggregateTypeForABI(RetTy))
02924     return ABIArgInfo::getIndirect(0);
02925   return ABIArgInfo::getDirect();
02926 }
02927 
02928 ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
02929   if (isAggregateTypeForABI(Ty))
02930     return ABIArgInfo::getIndirect(0);
02931 
02932   return ABIArgInfo::getDirect();
02933 }
02934 
02935 void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
02936   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
02937   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
02938        it != ie; ++it)
02939     it->info = classifyArgumentType(it->type);
02940 
02941   // Always honor user-specified calling convention.
02942   if (FI.getCallingConvention() != llvm::CallingConv::C)
02943     return;
02944 
02945   // Calling convention as default by an ABI.
02946   llvm::CallingConv::ID DefaultCC;
02947   const LangOptions &LangOpts = getContext().getLangOpts();
02948   if (LangOpts.OpenCL || LangOpts.CUDA) {
02949     // If we are in OpenCL or CUDA mode, then default to device functions
02950     DefaultCC = llvm::CallingConv::PTX_Device;
02951   } else {
02952     // If we are in standard C/C++ mode, use the triple to decide on the default
02953     StringRef Env = 
02954       getContext().getTargetInfo().getTriple().getEnvironmentName();
02955     if (Env == "device")
02956       DefaultCC = llvm::CallingConv::PTX_Device;
02957     else
02958       DefaultCC = llvm::CallingConv::PTX_Kernel;
02959   }
02960   FI.setEffectiveCallingConvention(DefaultCC);
02961    
02962 }
02963 
02964 llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
02965                                    CodeGenFunction &CFG) const {
02966   llvm_unreachable("PTX does not support varargs");
02967 }
02968 
02969 void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
02970                                                llvm::GlobalValue *GV,
02971                                                CodeGen::CodeGenModule &M) const{
02972   const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
02973   if (!FD) return;
02974 
02975   llvm::Function *F = cast<llvm::Function>(GV);
02976 
02977   // Perform special handling in OpenCL mode
02978   if (M.getLangOpts().OpenCL) {
02979     // Use OpenCL function attributes to set proper calling conventions
02980     // By default, all functions are device functions
02981     if (FD->hasAttr<OpenCLKernelAttr>()) {
02982       // OpenCL __kernel functions get a kernel calling convention
02983       F->setCallingConv(llvm::CallingConv::PTX_Kernel);
02984       // And kernel functions are not subject to inlining
02985       F->addFnAttr(llvm::Attribute::NoInline);
02986     }
02987   }
02988 
02989   // Perform special handling in CUDA mode.
02990   if (M.getLangOpts().CUDA) {
02991     // CUDA __global__ functions get a kernel calling convention.  Since
02992     // __global__ functions cannot be called from the device, we do not
02993     // need to set the noinline attribute.
02994     if (FD->getAttr<CUDAGlobalAttr>())
02995       F->setCallingConv(llvm::CallingConv::PTX_Kernel);
02996   }
02997 }
02998 
02999 }
03000 
03001 //===----------------------------------------------------------------------===//
03002 // MBlaze ABI Implementation
03003 //===----------------------------------------------------------------------===//
03004 
03005 namespace {
03006 
03007 class MBlazeABIInfo : public ABIInfo {
03008 public:
03009   MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
03010 
03011   bool isPromotableIntegerType(QualType Ty) const;
03012 
03013   ABIArgInfo classifyReturnType(QualType RetTy) const;
03014   ABIArgInfo classifyArgumentType(QualType RetTy) const;
03015 
03016   virtual void computeInfo(CGFunctionInfo &FI) const {
03017     FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
03018     for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
03019          it != ie; ++it)
03020       it->info = classifyArgumentType(it->type);
03021   }
03022 
03023   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
03024                                  CodeGenFunction &CGF) const;
03025 };
03026 
03027 class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
03028 public:
03029   MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
03030     : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
03031   void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
03032                            CodeGen::CodeGenModule &M) const;
03033 };
03034 
03035 }
03036 
03037 bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
03038   // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
03039   if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
03040     switch (BT->getKind()) {
03041     case BuiltinType::Bool:
03042     case BuiltinType::Char_S:
03043     case BuiltinType::Char_U:
03044     case BuiltinType::SChar:
03045     case BuiltinType::UChar:
03046     case BuiltinType::Short:
03047     case BuiltinType::UShort:
03048       return true;
03049     default:
03050       return false;
03051     }
03052   return false;
03053 }
03054 
03055 llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
03056                                       CodeGenFunction &CGF) const {
03057   // FIXME: Implement
03058   return 0;
03059 }
03060 
03061 
03062 ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
03063   if (RetTy->isVoidType())
03064     return ABIArgInfo::getIgnore();
03065   if (isAggregateTypeForABI(RetTy))
03066     return ABIArgInfo::getIndirect(0);
03067 
03068   return (isPromotableIntegerType(RetTy) ?
03069           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
03070 }
03071 
03072 ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
03073   if (isAggregateTypeForABI(Ty))
03074     return ABIArgInfo::getIndirect(0);
03075 
03076   return (isPromotableIntegerType(Ty) ?
03077           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
03078 }
03079 
03080 void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
03081                                                   llvm::GlobalValue *GV,
03082                                                   CodeGen::CodeGenModule &M)
03083                                                   const {
03084   const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
03085   if (!FD) return;
03086 
03087   llvm::CallingConv::ID CC = llvm::CallingConv::C;
03088   if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
03089     CC = llvm::CallingConv::MBLAZE_INTR;
03090   else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
03091     CC = llvm::CallingConv::MBLAZE_SVOL;
03092 
03093   if (CC != llvm::CallingConv::C) {
03094       // Handle 'interrupt_handler' attribute:
03095       llvm::Function *F = cast<llvm::Function>(GV);
03096 
03097       // Step 1: Set ISR calling convention.
03098       F->setCallingConv(CC);
03099 
03100       // Step 2: Add attributes goodness.
03101       F->addFnAttr(llvm::Attribute::NoInline);
03102   }
03103 
03104   // Step 3: Emit _interrupt_handler alias.
03105   if (CC == llvm::CallingConv::MBLAZE_INTR)
03106     new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
03107                           "_interrupt_handler", GV, &M.getModule());
03108 }
03109 
03110 
03111 //===----------------------------------------------------------------------===//
03112 // MSP430 ABI Implementation
03113 //===----------------------------------------------------------------------===//
03114 
03115 namespace {
03116 
03117 class MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
03118 public:
03119   MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
03120     : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
03121   void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
03122                            CodeGen::CodeGenModule &M) const;
03123 };
03124 
03125 }
03126 
03127 void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
03128                                                   llvm::GlobalValue *GV,
03129                                              CodeGen::CodeGenModule &M) const {
03130   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
03131     if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
03132       // Handle 'interrupt' attribute:
03133       llvm::Function *F = cast<llvm::Function>(GV);
03134 
03135       // Step 1: Set ISR calling convention.
03136       F->setCallingConv(llvm::CallingConv::MSP430_INTR);
03137 
03138       // Step 2: Add attributes goodness.
03139       F->addFnAttr(llvm::Attribute::NoInline);
03140 
03141       // Step 3: Emit ISR vector alias.
03142       unsigned Num = attr->getNumber() + 0xffe0;
03143       new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
03144                             "vector_" + Twine::utohexstr(Num),
03145                             GV, &M.getModule());
03146     }
03147   }
03148 }
03149 
03150 //===----------------------------------------------------------------------===//
03151 // MIPS ABI Implementation.  This works for both little-endian and
03152 // big-endian variants.
03153 //===----------------------------------------------------------------------===//
03154 
03155 namespace {
03156 class MipsABIInfo : public ABIInfo {
03157   bool IsO32;
03158   unsigned MinABIStackAlignInBytes;
03159   llvm::Type* CoerceToIntArgs(uint64_t TySize) const;
03160   llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
03161   llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
03162   llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
03163 public:
03164   MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
03165     ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
03166 
03167   ABIArgInfo classifyReturnType(QualType RetTy) const;
03168   ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
03169   virtual void computeInfo(CGFunctionInfo &FI) const;
03170   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
03171                                  CodeGenFunction &CGF) const;
03172 };
03173 
03174 class MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
03175   unsigned SizeOfUnwindException;
03176 public:
03177   MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
03178     : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
03179       SizeOfUnwindException(IsO32 ? 24 : 32) {}
03180 
03181   int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
03182     return 29;
03183   }
03184 
03185   bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
03186                                llvm::Value *Address) const;
03187 
03188   unsigned getSizeOfUnwindException() const {
03189     return SizeOfUnwindException;
03190   }
03191 };
03192 }
03193 
03194 llvm::Type* MipsABIInfo::CoerceToIntArgs(uint64_t TySize) const {
03195   SmallVector<llvm::Type*, 8> ArgList;
03196   llvm::IntegerType *IntTy = llvm::IntegerType::get(getVMContext(),
03197                                                     MinABIStackAlignInBytes * 8);
03198 
03199   // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
03200   for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
03201     ArgList.push_back(IntTy);
03202 
03203   // If necessary, add one more integer type to ArgList.
03204   unsigned R = TySize % (MinABIStackAlignInBytes * 8);
03205 
03206   if (R)
03207     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
03208 
03209   return llvm::StructType::get(getVMContext(), ArgList);
03210 }
03211 
03212 // In N32/64, an aligned double precision floating point field is passed in
03213 // a register.
03214 llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
03215   if (IsO32)
03216     return CoerceToIntArgs(TySize);
03217 
03218   if (Ty->isComplexType())
03219     return CGT.ConvertType(Ty);
03220 
03221   const RecordType *RT = Ty->getAs<RecordType>();
03222 
03223   // Unions are passed in integer registers.
03224   if (!RT || !RT->isStructureOrClassType())
03225     return CoerceToIntArgs(TySize);
03226 
03227   const RecordDecl *RD = RT->getDecl();
03228   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
03229   assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
03230   
03231   uint64_t LastOffset = 0;
03232   unsigned idx = 0;
03233   llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
03234   SmallVector<llvm::Type*, 8> ArgList;
03235 
03236   // Iterate over fields in the struct/class and check if there are any aligned
03237   // double fields.
03238   for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
03239        i != e; ++i, ++idx) {
03240     const QualType Ty = i->getType();
03241     const BuiltinType *BT = Ty->getAs<BuiltinType>();
03242 
03243     if (!BT || BT->getKind() != BuiltinType::Double)
03244       continue;
03245 
03246     uint64_t Offset = Layout.getFieldOffset(idx);
03247     if (Offset % 64) // Ignore doubles that are not aligned.
03248       continue;
03249 
03250     // Add ((Offset - LastOffset) / 64) args of type i64.
03251     for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
03252       ArgList.push_back(I64);
03253 
03254     // Add double type.
03255     ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
03256     LastOffset = Offset + 64;
03257   }
03258 
03259   // Add ((TySize - LastOffset) / 64) args of type i64.
03260   for (unsigned N = (TySize - LastOffset) / 64; N; --N)
03261     ArgList.push_back(I64);
03262 
03263   // If the size of the remainder is not zero, add one more integer type to
03264   // ArgList.
03265   unsigned R = (TySize - LastOffset) % 64;
03266   if (R)
03267     ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
03268 
03269   return llvm::StructType::get(getVMContext(), ArgList);
03270 }
03271 
03272 llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
03273   assert((Offset % MinABIStackAlignInBytes) == 0);
03274 
03275   if ((Align - 1) & Offset)
03276     return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
03277 
03278   return 0;
03279 }
03280 
03281 ABIArgInfo
03282 MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
03283   uint64_t OrigOffset = Offset;
03284   uint64_t TySize = getContext().getTypeSize(Ty);
03285   uint64_t Align = getContext().getTypeAlign(Ty) / 8;
03286 
03287   Align = std::max(Align, (uint64_t)MinABIStackAlignInBytes);
03288   Offset = llvm::RoundUpToAlignment(Offset, Align);
03289   Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
03290 
03291   if (isAggregateTypeForABI(Ty)) {
03292     // Ignore empty aggregates.
03293     if (TySize == 0)
03294       return ABIArgInfo::getIgnore();
03295 
03296     // Records with non trivial destructors/constructors should not be passed
03297     // by value.
03298     if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
03299       Offset = OrigOffset + MinABIStackAlignInBytes;
03300       return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
03301     }
03302 
03303     // If we have reached here, aggregates are passed directly by coercing to
03304     // another structure type. Padding is inserted if the offset of the
03305     // aggregate is unaligned.
03306     return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
03307                                  getPaddingType(Align, OrigOffset));
03308   }
03309 
03310   // Treat an enum type as its underlying type.
03311   if (const EnumType *EnumTy = Ty->getAs<EnumType>())
03312     Ty = EnumTy->getDecl()->getIntegerType();
03313 
03314   if (Ty->isPromotableIntegerType())
03315     return ABIArgInfo::getExtend();
03316 
03317   return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
03318 }
03319 
03320 llvm::Type*
03321 MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
03322   const RecordType *RT = RetTy->getAs<RecordType>();
03323   SmallVector<llvm::Type*, 2> RTList;
03324 
03325   if (RT && RT->isStructureOrClassType()) {
03326     const RecordDecl *RD = RT->getDecl();
03327     const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
03328     unsigned FieldCnt = Layout.getFieldCount();
03329 
03330     // N32/64 returns struct/classes in floating point registers if the
03331     // following conditions are met:
03332     // 1. The size of the struct/class is no larger than 128-bit.
03333     // 2. The struct/class has one or two fields all of which are floating
03334     //    point types.
03335     // 3. The offset of the first field is zero (this follows what gcc does). 
03336     //
03337     // Any other composite results are returned in integer registers.
03338     //
03339     if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
03340       RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
03341       for (; b != e; ++b) {
03342         const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
03343 
03344         if (!BT || !BT->isFloatingPoint())
03345           break;
03346 
03347         RTList.push_back(CGT.ConvertType(b->getType()));
03348       }
03349 
03350       if (b == e)
03351         return llvm::StructType::get(getVMContext(), RTList,
03352                                      RD->hasAttr<PackedAttr>());
03353 
03354       RTList.clear();
03355     }
03356   }
03357 
03358   RTList.push_back(llvm::IntegerType::get(getVMContext(),
03359                                           std::min(Size, (uint64_t)64)));
03360   if (Size > 64)
03361     RTList.push_back(llvm::IntegerType::get(getVMContext(), Size - 64));
03362 
03363   return llvm::StructType::get(getVMContext(), RTList);
03364 }
03365 
03366 ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
03367   uint64_t Size = getContext().getTypeSize(RetTy);
03368 
03369   if (RetTy->isVoidType() || Size == 0)
03370     return ABIArgInfo::getIgnore();
03371 
03372   if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
03373     if (Size <= 128) {
03374       if (RetTy->isAnyComplexType())
03375         return ABIArgInfo::getDirect();
03376 
03377       if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
03378         return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
03379     }
03380 
03381     return ABIArgInfo::getIndirect(0);
03382   }
03383 
03384   // Treat an enum type as its underlying type.
03385   if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
03386     RetTy = EnumTy->getDecl()->getIntegerType();
03387 
03388   return (RetTy->isPromotableIntegerType() ?
03389           ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
03390 }
03391 
03392 void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
03393   ABIArgInfo &RetInfo = FI.getReturnInfo();
03394   RetInfo = classifyReturnType(FI.getReturnType());
03395 
03396   // Check if a pointer to an aggregate is passed as a hidden argument.  
03397   uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
03398 
03399   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
03400        it != ie; ++it)
03401     it->info = classifyArgumentType(it->type, Offset);
03402 }
03403 
03404 llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
03405                                     CodeGenFunction &CGF) const {
03406   llvm::Type *BP = CGF.Int8PtrTy;
03407   llvm::Type *BPP = CGF.Int8PtrPtrTy;
03408  
03409   CGBuilderTy &Builder = CGF.Builder;
03410   llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
03411   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
03412   int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
03413   llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
03414   llvm::Value *AddrTyped;
03415   unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
03416   llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
03417 
03418   if (TypeAlign > MinABIStackAlignInBytes) {
03419     llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
03420     llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
03421     llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
03422     llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
03423     llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
03424     AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
03425   }
03426   else
03427     AddrTyped = Builder.CreateBitCast(Addr, PTy);  
03428 
03429   llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
03430   TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
03431   uint64_t Offset =
03432     llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
03433   llvm::Value *NextAddr =
03434     Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
03435                       "ap.next");
03436   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
03437   
03438   return AddrTyped;
03439 }
03440 
03441 bool
03442 MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
03443                                                llvm::Value *Address) const {
03444   // This information comes from gcc's implementation, which seems to
03445   // as canonical as it gets.
03446 
03447   // Everything on MIPS is 4 bytes.  Double-precision FP registers
03448   // are aliased to pairs of single-precision FP registers.
03449   llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
03450 
03451   // 0-31 are the general purpose registers, $0 - $31.
03452   // 32-63 are the floating-point registers, $f0 - $f31.
03453   // 64 and 65 are the multiply/divide registers, $hi and $lo.
03454   // 66 is the (notional, I think) register for signal-handler return.
03455   AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
03456 
03457   // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
03458   // They are one bit wide and ignored here.
03459 
03460   // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
03461   // (coprocessor 1 is the FP unit)
03462   // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
03463   // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
03464   // 176-181 are the DSP accumulator registers.
03465   AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
03466   return false;
03467 }
03468 
03469 //===----------------------------------------------------------------------===//
03470 // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
03471 // Currently subclassed only to implement custom OpenCL C function attribute 
03472 // handling.
03473 //===----------------------------------------------------------------------===//
03474 
03475 namespace {
03476 
03477 class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
03478 public:
03479   TCETargetCodeGenInfo(CodeGenTypes &CGT)
03480     : DefaultTargetCodeGenInfo(CGT) {}
03481 
03482   virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
03483                                    CodeGen::CodeGenModule &M) const;
03484 };
03485 
03486 void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
03487                                                llvm::GlobalValue *GV,
03488                                                CodeGen::CodeGenModule &M) const {
03489   const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
03490   if (!FD) return;
03491 
03492   llvm::Function *F = cast<llvm::Function>(GV);
03493   
03494   if (M.getLangOpts().OpenCL) {
03495     if (FD->hasAttr<OpenCLKernelAttr>()) {
03496       // OpenCL C Kernel functions are not subject to inlining
03497       F->addFnAttr(llvm::Attribute::NoInline);
03498           
03499       if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
03500 
03501         // Convert the reqd_work_group_size() attributes to metadata.
03502         llvm::LLVMContext &Context = F->getContext();
03503         llvm::NamedMDNode *OpenCLMetadata = 
03504             M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
03505 
03506         SmallVector<llvm::Value*, 5> Operands;
03507         Operands.push_back(F);
03508 
03509         Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 
03510                              llvm::APInt(32, 
03511                              FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
03512         Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
03513                              llvm::APInt(32,
03514                                FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
03515         Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, 
03516                              llvm::APInt(32, 
03517                                FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
03518 
03519         // Add a boolean constant operand for "required" (true) or "hint" (false)
03520         // for implementing the work_group_size_hint attr later. Currently 
03521         // always true as the hint is not yet implemented.
03522         Operands.push_back(llvm::ConstantInt::getTrue(Context));
03523         OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
03524       }
03525     }
03526   }
03527 }
03528 
03529 }
03530 
03531 //===----------------------------------------------------------------------===//
03532 // Hexagon ABI Implementation
03533 //===----------------------------------------------------------------------===//
03534 
03535 namespace {
03536 
03537 class HexagonABIInfo : public ABIInfo {
03538 
03539 
03540 public:
03541   HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
03542 
03543 private:
03544 
03545   ABIArgInfo classifyReturnType(QualType RetTy) const;
03546   ABIArgInfo classifyArgumentType(QualType RetTy) const;
03547 
03548   virtual void computeInfo(CGFunctionInfo &FI) const;
03549 
03550   virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
03551                                  CodeGenFunction &CGF) const;
03552 };
03553 
03554 class HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
03555 public:
03556   HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
03557     :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
03558 
03559   int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
03560     return 29;
03561   }
03562 };
03563 
03564 }
03565 
03566 void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
03567   FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
03568   for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
03569        it != ie; ++it)
03570     it->info = classifyArgumentType(it->type);
03571 }
03572 
03573 ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
03574   if (!isAggregateTypeForABI(Ty)) {
03575     // Treat an enum type as its underlying type.
03576     if (const EnumType *EnumTy = Ty->getAs<EnumType>())
03577       Ty = EnumTy->getDecl()->getIntegerType();
03578 
03579     return (Ty->isPromotableIntegerType() ?
03580             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
03581   }
03582 
03583   // Ignore empty records.
03584   if (isEmptyRecord(getContext(), Ty, true))
03585     return ABIArgInfo::getIgnore();
03586 
03587   // Structures with either a non-trivial destructor or a non-trivial
03588   // copy constructor are always indirect.
03589   if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
03590     return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
03591 
03592   uint64_t Size = getContext().getTypeSize(Ty);
03593   if (Size > 64)
03594     return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
03595     // Pass in the smallest viable integer type.
03596   else if (Size > 32)
03597       return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
03598   else if (Size > 16)
03599       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
03600   else if (Size > 8)
03601       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
03602   else
03603       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
03604 }
03605 
03606 ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
03607   if (RetTy->isVoidType())
03608     return ABIArgInfo::getIgnore();
03609 
03610   // Large vector types should be returned via memory.
03611   if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
03612     return ABIArgInfo::getIndirect(0);
03613 
03614   if (!isAggregateTypeForABI(RetTy)) {
03615     // Treat an enum type as its underlying type.
03616     if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
03617       RetTy = EnumTy->getDecl()->getIntegerType();
03618 
03619     return (RetTy->isPromotableIntegerType() ?
03620             ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
03621   }
03622 
03623   // Structures with either a non-trivial destructor or a non-trivial
03624   // copy constructor are always indirect.
03625   if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
03626     return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
03627 
03628   if (isEmptyRecord(getContext(), RetTy, true))
03629     return ABIArgInfo::getIgnore();
03630 
03631   // Aggregates <= 8 bytes are returned in r0; other aggregates
03632   // are returned indirectly.
03633   uint64_t Size = getContext().getTypeSize(RetTy);
03634   if (Size <= 64) {
03635     // Return in the smallest viable integer type.
03636     if (Size <= 8)
03637       return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
03638     if (Size <= 16)
03639       return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
03640     if (Size <= 32)
03641       return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
03642     return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
03643   }
03644 
03645   return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
03646 }
03647 
03648 llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
03649                                        CodeGenFunction &CGF) const {
03650   // FIXME: Need to handle alignment
03651   llvm::Type *BPP = CGF.Int8PtrPtrTy;
03652 
03653   CGBuilderTy &Builder = CGF.Builder;
03654   llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
03655                                                        "ap");
03656   llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
03657   llvm::Type *PTy =
03658     llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
03659   llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
03660 
03661   uint64_t Offset =
03662     llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
03663   llvm::Value *NextAddr =
03664     Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
03665                       "ap.next");
03666   Builder.CreateStore(NextAddr, VAListAddrAsBPP);
03667 
03668   return AddrTyped;
03669 }
03670 
03671 
03672 const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
03673   if (TheTargetCodeGenInfo)
03674     return *TheTargetCodeGenInfo;
03675 
03676   const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
03677   switch (Triple.getArch()) {
03678   default:
03679     return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
03680 
03681   case llvm::Triple::mips:
03682   case llvm::Triple::mipsel:
03683     return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
03684 
03685   case llvm::Triple::mips64:
03686   case llvm::Triple::mips64el:
03687     return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
03688 
03689   case llvm::Triple::arm:
03690   case llvm::Triple::thumb:
03691     {
03692       ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
03693 
03694       if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
03695         Kind = ARMABIInfo::APCS;
03696       else if (CodeGenOpts.FloatABI == "hard")
03697         Kind = ARMABIInfo::AAPCS_VFP;
03698 
03699       return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
03700     }
03701 
03702   case llvm::Triple::ppc:
03703     return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
03704   case llvm::Triple::ppc64:
03705     return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
03706 
03707   case llvm::Triple::ptx32:
03708   case llvm::Triple::ptx64:
03709     return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
03710 
03711   case llvm::Triple::mblaze:
03712     return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
03713 
03714   case llvm::Triple::msp430:
03715     return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
03716 
03717   case llvm::Triple::tce:
03718     return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
03719 
03720   case llvm::Triple::x86: {
03721     bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
03722 
03723     if (Triple.isOSDarwin())
03724       return *(TheTargetCodeGenInfo =
03725                new X86_32TargetCodeGenInfo(
03726                  Types, true, true, DisableMMX, false));
03727 
03728     switch (Triple.getOS()) {
03729     case llvm::Triple::Cygwin:
03730     case llvm::Triple::MinGW32:
03731     case llvm::Triple::AuroraUX:
03732     case llvm::Triple::DragonFly:
03733     case llvm::Triple::FreeBSD:
03734     case llvm::Triple::OpenBSD:
03735       return *(TheTargetCodeGenInfo =
03736                new X86_32TargetCodeGenInfo(
03737                  Types, false, true, DisableMMX, false));
03738 
03739     case llvm::Triple::Win32:
03740       return *(TheTargetCodeGenInfo =
03741                new X86_32TargetCodeGenInfo(
03742                  Types, false, true, DisableMMX, true));
03743 
03744     default:
03745       return *(TheTargetCodeGenInfo =
03746                new X86_32TargetCodeGenInfo(
03747                  Types, false, false, DisableMMX, false));
03748     }
03749   }
03750 
03751   case llvm::Triple::x86_64: {
03752     bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
03753 
03754     switch (Triple.getOS()) {
03755     case llvm::Triple::Win32:
03756     case llvm::Triple::MinGW32:
03757     case llvm::Triple::Cygwin:
03758       return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
03759     default:
03760       return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
03761                                                                   HasAVX));
03762     }
03763   }
03764   case llvm::Triple::hexagon:
03765     return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
03766   }
03767 }