TargetInfo.cpp revision 634b3d26969f139a25b223074567ba5ab7ba7dd9
182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
2c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
3c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//                     The LLVM Compiler Infrastructure
4c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
5c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// This file is distributed under the University of Illinois Open Source
6c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// License. See LICENSE.TXT for details.
7c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
8c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//===----------------------------------------------------------------------===//
9c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
10c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// These classes wrap the information about a call or function
11c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// definition used to handle ABI compliancy.
12c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
13c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//===----------------------------------------------------------------------===//
14c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetInfo.h"
16c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "ABIInfo.h"
17c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "CodeGenFunction.h"
1819cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
1934c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel#include "clang/Frontend/CodeGenOptions.h"
20c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "llvm/Type.h"
219c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner#include "llvm/Target/TargetData.h"
222c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar#include "llvm/ADT/Triple.h"
2328df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar#include "llvm/Support/raw_ostream.h"
24c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovusing namespace clang;
25c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovusing namespace CodeGen;
26c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
27aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallstatic void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               llvm::Value *Array,
29aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               llvm::Value *Value,
30aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               unsigned FirstIndex,
31aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               unsigned LastIndex) {
32aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // Alternatively, we could emit this as a loop in the source.
33aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    Builder.CreateStore(Value, Cell);
36aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
37aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
38aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
39d608cdb7c044365cf4e8764ade1e11e99c176078John McCallstatic bool isAggregateTypeForABI(QualType T) {
40d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  return CodeGenFunction::hasAggregateLLVMType(T) ||
41d608cdb7c044365cf4e8764ade1e11e99c176078John McCall         T->isMemberFunctionPointerType();
42d608cdb7c044365cf4e8764ade1e11e99c176078John McCall}
43d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
44c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton KorobeynikovABIInfo::~ABIInfo() {}
45c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
46ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris LattnerASTContext &ABIInfo::getContext() const {
47ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getContext();
48ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
49ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
50ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerllvm::LLVMContext &ABIInfo::getVMContext() const {
51ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getLLVMContext();
52ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
53ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
54ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerconst llvm::TargetData &ABIInfo::getTargetData() const {
55ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getTargetData();
56ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
57ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
58ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
59c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovvoid ABIArgInfo::dump() const {
605f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  raw_ostream &OS = llvm::errs();
6128df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar  OS << "(ABIArgInfo Kind=";
62c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (TheKind) {
63c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Direct:
64800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    OS << "Direct Type=";
652acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    if (llvm::Type *Ty = getCoerceToType())
66800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      Ty->print(OS);
67800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    else
68800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      OS << "null";
69c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
70cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case Extend:
7128df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Extend";
72cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    break;
73c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Ignore:
7428df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Ignore";
75c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
76c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Indirect:
77dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar    OS << "Indirect Align=" << getIndirectAlign()
78e9b5d77b7bfd3e8bba05df9914a6e8c336d68ff3Joerg Sonnenberger       << " ByVal=" << getIndirectByVal()
79cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar       << " Realign=" << getIndirectRealign();
80c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
81c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Expand:
8228df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Expand";
83c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
84c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
8528df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar  OS << ")\n";
86c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
87c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
8882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovTargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
8982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
9049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall// If someone can figure out a general rule for this, that would be great.
9149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall// It's probably just doomed to be platform-dependent, though.
9249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCallunsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
9349e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  // Verified for:
9449e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   x86-64     FreeBSD, Linux, Darwin
9549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   x86-32     FreeBSD, Linux, Darwin
9649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   PowerPC    Linux, Darwin
9749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   ARM        Darwin (*not* EABI)
9849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  return 32;
9949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall}
10049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
101de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallbool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                     const FunctionNoProtoType *fnType) const {
10301f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  // The following conventions are known to require this to be false:
10401f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  //   x86_stdcall
10501f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  //   MIPS
10601f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  // For everything else, we just prefer false unless we opt out.
10701f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  return false;
10801f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall}
10901f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
11098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
111c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
112c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// isEmptyField - Return true iff a the field is "empty", that is it
113c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// is an unnamed bit-field or an (array of) empty record(s).
11498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
11598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar                         bool AllowArrays) {
116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (FD->isUnnamedBitfield())
117c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  QualType FT = FD->getType();
120c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1217e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman  // Constant arrays of empty records count as empty, strip them off.
1227e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman  // Constant arrays of zero length always count as empty.
12398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (AllowArrays)
1247e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
1257e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman      if (AT->getSize() == 0)
1267e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman        return true;
12798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      FT = AT->getElementType();
1287e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman    }
12998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
1305ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  const RecordType *RT = FT->getAs<RecordType>();
1315ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (!RT)
1325ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar    return false;
1335ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
1345ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // C++ record fields are never empty, at least in the Itanium ABI.
1355ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  //
1365ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // FIXME: We should use a predicate for whether this behavior is true in the
1375ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // current ABI.
1385ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (isa<CXXRecordDecl>(RT->getDecl()))
1395ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar    return false;
1405ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
14198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return isEmptyRecord(Context, FT, AllowArrays);
142c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
143c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
144c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// isEmptyRecord - Return true iff a structure contains only empty
145c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// fields. Note that a structure with a flexible array member is not
146c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// considered empty.
14798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
1486217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *RT = T->getAs<RecordType>();
149c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT)
150c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
151c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordDecl *RD = RT->getDecl();
152c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (RD->hasFlexibleArrayMember())
153c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
1545ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
155c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis  // If this is a C++ record, check the bases first.
1565ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
157c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
158c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis           e = CXXRD->bases_end(); i != e; ++i)
159c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis      if (!isEmptyRecord(Context, i->getType(), true))
160c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis        return false;
1615ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
16217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
16317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i)
164581deb3da481053c4993c7600f97acf7768caac5David Blaikie    if (!isEmptyField(Context, *i, AllowArrays))
165c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
166c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
167c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
168c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1690a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
1700a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// a non-trivial destructor or a non-trivial copy constructor.
1710a8f847e97f40cce51dc69051b964732333dc028Anders Carlssonstatic bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
1720a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
1730a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  if (!RD)
1740a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    return false;
1758bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1760a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
1770a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson}
1780a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1790a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
1800a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// a record type with either a non-trivial destructor or a non-trivial copy
1810a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// constructor.
1820a8f847e97f40cce51dc69051b964732333dc028Anders Carlssonstatic bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
1830a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  const RecordType *RT = T->getAs<RecordType>();
1840a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  if (!RT)
1850a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    return false;
1860a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1870a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  return hasNonTrivialDestructorOrCopyConstructor(RT);
1880a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson}
1890a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
190c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// isSingleElementStruct - Determine if a structure is a "single
191c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// element struct", i.e. it has exactly one non-empty field or
192c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// exactly one field which is itself a single element
193c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// struct. Structures with flexible array members are never
194c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// considered single element structs.
195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov///
196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// \return The field declaration for the single non-empty field, if
197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// it exists.
198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordType *RT = T->getAsStructureType();
200c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT)
201c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
202c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
203c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordDecl *RD = RT->getDecl();
204c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (RD->hasFlexibleArrayMember())
205c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
206c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
207c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const Type *Found = 0;
2088bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2099430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  // If this is a C++ record, check the bases first.
2109430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2119430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
2129430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar           e = CXXRD->bases_end(); i != e; ++i) {
2139430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // Ignore empty records.
2145ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar      if (isEmptyRecord(Context, i->getType(), true))
2159430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        continue;
2169430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2179430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // If we already found an element then this isn't a single-element struct.
2189430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      if (Found)
2199430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        return 0;
2209430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2219430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // If this is non-empty and not a single element struct, the composite
2229430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // cannot be a single element struct.
2239430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      Found = isSingleElementStruct(i->getType(), Context);
2249430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      if (!Found)
2259430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        return 0;
2269430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar    }
2279430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  }
2289430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2299430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  // Check for single element.
23017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
23117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
232581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
233c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    QualType FT = FD->getType();
234c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
235c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Ignore empty fields.
23698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (isEmptyField(Context, FD, true))
237c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      continue;
238c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
239c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // If we already found an element then this isn't a single-element
240c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // struct.
241c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Found)
242c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return 0;
243c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
244c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Treat single element arrays as the element.
245c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
246c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (AT->getSize().getZExtValue() != 1)
247c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
248c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      FT = AT->getElementType();
249c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
250c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
251d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    if (!isAggregateTypeForABI(FT)) {
252c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Found = FT.getTypePtr();
253c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else {
254c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Found = isSingleElementStruct(FT, Context);
255c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (!Found)
256c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return 0;
257c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
258c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
259c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
260bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  // We don't consider a struct a single-element struct if it has
261bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  // padding beyond the element type.
262bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
263bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman    return 0;
264bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
265c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Found;
266c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
269a1842d32a1964712e42078e9b389dce9258c6a8cDaniel Dunbar  if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
27055e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
27155e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      !Ty->isBlockPointerType())
272c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Size = Context.getTypeSize(Ty);
275c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Size == 32 || Size == 64;
276c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
27853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// canExpandIndirectArgument - Test whether an argument type which is to be
27953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// passed indirectly (on the stack) would have the equivalent layout if it was
28053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// expanded into separate arguments. If so, we prefer to do the latter to avoid
28153012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// inhibiting optimizations.
28253012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar///
28353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// FIXME: This predicate is missing many cases, currently it just follows
28453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
28553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// should probably make this smarter, or better yet make the LLVM backend
28653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// capable of handling it.
28753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbarstatic bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
28853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // We can only expand structure types.
28953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
29053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  if (!RT)
29153012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    return false;
29253012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar
29353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // We can only expand (C) structures.
29453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  //
29553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // FIXME: This needs to be generalized to handle classes as well.
29653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
29753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
29853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    return false;
29953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar
300506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  uint64_t Size = 0;
301506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
30217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
30317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
304581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
305c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
306c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (!is32Or64BitBasicType(FD->getType(), Context))
307c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
308c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
309c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
310c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // how to expand them yet, and the predicate for telling if a bitfield still
311c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // counts as "basic" is more complicated than what we were doing previously.
312c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (FD->isBitField())
313c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
314506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
315506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman    Size += Context.getTypeSize(FD->getType());
316c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
317c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
318506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  // Make sure there are not any holes in the struct.
319506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  if (Size != Context.getTypeSize(Ty))
320506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman    return false;
321506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
322c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
324c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
325c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovnamespace {
326c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// DefaultABIInfo - The default implementation for ABI specific
327c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// details. This implementation provides information which results in
328c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// self-consistent and sensible LLVM IR generation, but does not
329c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// conform to any particular ABI.
330c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass DefaultABIInfo : public ABIInfo {
331ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerpublic:
332ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
3338bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
334a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
335a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
337ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const {
338a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
339c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
340c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         it != ie; ++it)
341a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      it->info = classifyArgumentType(it->type);
342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
343c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
344c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
345c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
346c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
347c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
34882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
34982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
350ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
351ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
35282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
35382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
35482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovllvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
35582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                       CodeGenFunction &CGF) const {
35682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  return 0;
35782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
35882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
359a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
36090306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung  if (isAggregateTypeForABI(Ty)) {
36190306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    // Records with non trivial destructors/constructors should not be passed
36290306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    // by value.
36390306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
36490306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
36590306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung
36682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return ABIArgInfo::getIndirect(0);
36790306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung  }
368dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
369a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  // Treat an enum type as its underlying type.
370a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
371a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner    Ty = EnumTy->getDecl()->getIntegerType();
372aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
373a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  return (Ty->isPromotableIntegerType() ?
374a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
37582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
37682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
3770024f940dd15987b8ffbe6e787dcf860a9ea1effBob WilsonABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
3780024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (RetTy->isVoidType())
3790024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    return ABIArgInfo::getIgnore();
3800024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
3810024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (isAggregateTypeForABI(RetTy))
3820024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    return ABIArgInfo::getIndirect(0);
3830024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
3840024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  // Treat an enum type as its underlying type.
3850024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3860024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    RetTy = EnumTy->getDecl()->getIntegerType();
3870024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
3880024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  return (RetTy->isPromotableIntegerType() ?
3890024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3900024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson}
3910024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
39255fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman/// UseX86_MMXType - Return true if this is an MMX type that should use the
39355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman/// special x86_mmx type.
3942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerbool UseX86_MMXType(llvm::Type *IRType) {
395bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
396bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  // special x86_mmx type.
397bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
398bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
399bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    IRType->getScalarSizeInBits() != 64;
400bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling}
401bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling
402ef6de3da8572607f786303c07150daa6e140ab19Jay Foadstatic llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
4035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef Constraint,
404ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                          llvm::Type* Ty) {
4050507be662df482b5c67b7905ed7ca368cb5c6b69Bill Wendling  if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
4064b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
4074b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  return Ty;
4084b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne}
4094b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
410dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
411dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner// X86-32 ABI Implementation
412dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
4138bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
414c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// X86_32ABIInfo - The X86-32 ABI information.
415c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass X86_32ABIInfo : public ABIInfo {
416b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  enum Class {
417b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Integer,
418b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Float
419b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  };
420b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
421fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  static const unsigned MinABIStackAlignInBytes = 4;
422fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
4231e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsDarwinVectorABI;
4241e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsSmallStructInRegABI;
425c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  bool IsMMXDisabled;
42655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  bool IsWin32FloatStructABI;
427b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  unsigned DefaultNumRegisterParameters;
428c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
429c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  static bool isRegisterSize(unsigned Size) {
430c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
431c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
432c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
4336c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
4346c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                          unsigned callingConvention);
435c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
436dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
437dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// such that the argument will be passed in memory.
438a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
439dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
440fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  /// \brief Return the alignment to use for the given type on the stack.
441e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
442fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
443b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  Class classify(QualType Ty) const;
444b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindola  ABIArgInfo classifyReturnType(QualType RetTy,
4456c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                unsigned callingConvention) const;
446b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  ABIArgInfo classifyArgumentTypeWithReg(QualType RetTy,
447b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                         unsigned &FreeRegs) const;
448a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
449c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
450b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindolapublic:
451b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindola
452aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  virtual void computeInfo(CGFunctionInfo &FI) const;
453c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
454c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
455c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
456b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
457b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                unsigned r)
458c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman    : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
459b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      IsMMXDisabled(m), IsWin32FloatStructABI(w),
460b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      DefaultNumRegisterParameters(r) {}
461c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
462c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
46382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
46482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
46555fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
466b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      bool d, bool p, bool m, bool w, unsigned r)
467b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
46874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
46974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
47074f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                           CodeGen::CodeGenModule &CGM) const;
4716374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
4726374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4736374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // Darwin uses different dwarf register numbers for EH.
4746374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    if (CGM.isTargetDarwin()) return 5;
4756374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
4766374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 4;
4776374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
4786374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
4796374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4806374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const;
4814b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
482ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
4835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
484ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
4854b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
4864b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
4874b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
48882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
48982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
49082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
491c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
492c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// shouldReturnTypeInRegister - Determine if the given type should be
493c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// passed in a register (for the Darwin ABI).
494c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovbool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
4956c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               ASTContext &Context,
4966c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               unsigned callingConvention) {
497c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Size = Context.getTypeSize(Ty);
498c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
499c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Type must be register sized.
500c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!isRegisterSize(Size))
501c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
502c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
503c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Ty->isVectorType()) {
504c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // 64- and 128- bit vectors inside structures are not returned in
505c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // registers.
506c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 64 || Size == 128)
507c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
508c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
509c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
510c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
511c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5127711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // If this is a builtin, pointer, enum, complex type, member pointer, or
5137711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // member function pointer it is ok.
514a1842d32a1964712e42078e9b389dce9258c6a8cDaniel Dunbar  if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
51555e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      Ty->isAnyComplexType() || Ty->isEnumeralType() ||
5167711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar      Ty->isBlockPointerType() || Ty->isMemberPointerType())
517c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
518c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
519c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Arrays are treated like records.
520c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
5216c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return shouldReturnTypeInRegister(AT->getElementType(), Context,
5226c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                      callingConvention);
523c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
524c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Otherwise, it must be a record type.
5256217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *RT = Ty->getAs<RecordType>();
526c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT) return false;
527c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
528a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson  // FIXME: Traverse bases here too.
529a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson
5306c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // For thiscall conventions, structures will never be returned in
5316c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // a register.  This is for compatibility with the MSVC ABI
5326c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  if (callingConvention == llvm::CallingConv::X86_ThisCall &&
5336c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman      RT->isStructureType()) {
5346c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return false;
5356c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  }
5366c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman
537c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Structure types are passed in register if all fields would be
538c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // passed in a register.
53917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
54017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         e = RT->getDecl()->field_end(); i != e; ++i) {
541581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
542c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
543c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Empty fields are ignored.
54498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (isEmptyField(Context, FD, true))
545c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      continue;
546c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
547c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Check fields recursively.
5486c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (!shouldReturnTypeInRegister(FD->getType(), Context,
5496c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                    callingConvention))
550c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
551c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
552c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
553c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
554c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5556c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron BallmanABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
5566c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                            unsigned callingConvention) const {
557a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (RetTy->isVoidType())
558c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
5598bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
560a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const VectorType *VT = RetTy->getAs<VectorType>()) {
561c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // On Darwin, some vectors are returned in registers.
5621e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (IsDarwinVectorABI) {
563a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
564c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
565c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // 128-bit vectors are a special case; they are returned in
566c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // registers and we need to make sure to pick a type the LLVM
567c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // backend will like.
568c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size == 128)
569800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::VectorType::get(
570a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                  llvm::Type::getInt64Ty(getVMContext()), 2));
571c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
572c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Always return in register if it fits in a general purpose
573c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // register, or if it is 64 bits and has a single element.
574c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if ((Size == 8 || Size == 16 || Size == 32) ||
575c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          (Size == 64 && VT->getNumElements() == 1))
576800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
577a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                                            Size));
578c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
579c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
580c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
581c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
582c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getDirect();
583a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  }
5848bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
585d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(RetTy)) {
586a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = RetTy->getAs<RecordType>()) {
58740092972b591646b47037d2b46b695a4014df413Anders Carlsson      // Structures with either a non-trivial destructor or a non-trivial
58840092972b591646b47037d2b46b695a4014df413Anders Carlsson      // copy constructor are always indirect.
58940092972b591646b47037d2b46b695a4014df413Anders Carlsson      if (hasNonTrivialDestructorOrCopyConstructor(RT))
59040092972b591646b47037d2b46b695a4014df413Anders Carlsson        return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5918bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
59240092972b591646b47037d2b46b695a4014df413Anders Carlsson      // Structures with flexible arrays are always indirect.
593c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
594c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return ABIArgInfo::getIndirect(0);
59540092972b591646b47037d2b46b695a4014df413Anders Carlsson    }
5968bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
5971e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    // If specified, structs and unions are always indirect.
5981e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
599c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
600c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
601c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Small structures which are register sized are generally returned
602c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // in a register.
6036c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
6046c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                                  callingConvention)) {
605a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
606bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
607bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // As a special-case, if the struct is a "single-element" struct, and
608bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // the field is of type "float" or "double", return it in a
60955fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // floating-point register. (MSVC does not apply this special case.)
61055fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // We apply a similar transformation for pointer types to improve the
61155fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // quality of the generated IR.
612bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
61355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman        if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
61455fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman            || SeltTy->hasPointerRepresentation())
615bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman          return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
616bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
617bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // FIXME: We should be able to narrow this integer in cases with dead
618bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // padding.
619800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
620c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
621c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
622c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIndirect(0);
623c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
6248bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
625a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  // Treat an enum type as its underlying type.
626a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
627a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    RetTy = EnumTy->getDecl()->getIntegerType();
628a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner
629a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  return (RetTy->isPromotableIntegerType() ?
630a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
631c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
632c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
633f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedmanstatic bool isSSEVectorType(ASTContext &Context, QualType Ty) {
634f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman  return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
635f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman}
636f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman
63793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbarstatic bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
63893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
63993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (!RT)
64093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 0;
64193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
64293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
64393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // If this is a C++ record, check the bases first.
64493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
64593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
64693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar           e = CXXRD->bases_end(); i != e; ++i)
64793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      if (!isRecordWithSSEVectorType(Context, i->getType()))
64893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar        return false;
64993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
65093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
65193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar       i != e; ++i) {
65293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    QualType FT = i->getType();
65393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
654f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman    if (isSSEVectorType(Context, FT))
65593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
65693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
65793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    if (isRecordWithSSEVectorType(Context, FT))
65893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
65993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  }
66093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
66193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return false;
66293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar}
66393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
664e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbarunsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
665e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                                 unsigned Align) const {
666e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Otherwise, if the alignment is less than or equal to the minimum ABI
667e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // alignment, just use the default; the backend will handle this.
668fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  if (Align <= MinABIStackAlignInBytes)
669e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return 0; // Use default alignment.
670e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
671e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // On non-Darwin, the stack type alignment is always 4.
672e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (!IsDarwinVectorABI) {
673e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    // Set explicit alignment, since we may need to realign the top.
674fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar    return MinABIStackAlignInBytes;
675e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  }
676fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
67793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // Otherwise, if the type contains an SSE vector type, the alignment is 16.
678f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman  if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
679f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman                      isRecordWithSSEVectorType(getContext(), Ty)))
68093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 16;
68193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
68293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return MinABIStackAlignInBytes;
683fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar}
684fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
685a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
68646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  if (!ByVal)
68746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return ABIArgInfo::getIndirect(0, false);
68846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
689e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Compute the byval alignment.
690e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
691e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
692e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign == 0)
693de92d739ba0ef42a5a7dcfd6e170329549d0716bChris Lattner    return ABIArgInfo::getIndirect(4);
694e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
695e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // If the stack alignment is less than the type alignment, realign the
696e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // argument.
697e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign < TypeAlign)
698e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
699e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                   /*Realign=*/true);
700e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
701e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  return ABIArgInfo::getIndirect(StackAlign);
702dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar}
703dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
704b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael EspindolaX86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
705b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  const Type *T = isSingleElementStruct(Ty, getContext());
706b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (!T)
707b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    T = Ty.getTypePtr();
708b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
709b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
710b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    BuiltinType::Kind K = BT->getKind();
711b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    if (K == BuiltinType::Float || K == BuiltinType::Double)
712b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      return Float;
713b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
714b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  return Integer;
715b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola}
716b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
717b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael EspindolaABIArgInfo
718b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael EspindolaX86_32ABIInfo::classifyArgumentTypeWithReg(QualType Ty,
719b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           unsigned &FreeRegs) const {
720b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // Common case first.
721b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (FreeRegs == 0)
722b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
723b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
724b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  Class C = classify(Ty);
725b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (C == Float)
726b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
727b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
728b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
729b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (SizeInRegs == 0)
730b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
731b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
732b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (SizeInRegs > FreeRegs) {
733b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    FreeRegs = 0;
734b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
735b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
736b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  assert(SizeInRegs >= 1 && SizeInRegs <= 3);
737b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  FreeRegs -= SizeInRegs;
738b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
739b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // If it is a simple scalar, keep the type so that we produce a cleaner IR.
740b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  ABIArgInfo Foo = classifyArgumentType(Ty);
741b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (Foo.isDirect() && !Foo.getDirectOffset() && !Foo.getPaddingType())
742b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return ABIArgInfo::getDirectInReg(Foo.getCoerceToType());
743b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (Foo.isExtend())
744b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return ABIArgInfo::getExtendInReg(Foo.getCoerceToType());
745b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
746b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  llvm::LLVMContext &LLVMContext = getVMContext();
747b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext);
748b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  SmallVector<llvm::Type*, 3> Elements;
749b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  for (unsigned I = 0; I < SizeInRegs; ++I)
750b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Elements.push_back(Int32);
751b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
752b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  return ABIArgInfo::getDirectInReg(Result);
753b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola}
754b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
755a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
756c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Set alignment on indirect arguments.
757d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(Ty)) {
758c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Structures with flexible arrays are always indirect.
759a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
760a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      // Structures with either a non-trivial destructor or a non-trivial
761a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      // copy constructor are always indirect.
762a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      if (hasNonTrivialDestructorOrCopyConstructor(RT))
763a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        return getIndirectResult(Ty, /*ByVal=*/false);
764dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
765c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
766a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        return getIndirectResult(Ty);
767a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    }
768c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
7695a4d35247f55dae6dd0d5ad349ecadbbea0b4572Eli Friedman    // Ignore empty structs/unions.
7705a1ac89b244940a0337ea7ae7dc371e2a9bf7c50Eli Friedman    if (isEmptyRecord(getContext(), Ty, true))
771c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIgnore();
772c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
77353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // Expand small (<= 128-bit) record types when we know that the stack layout
77453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // of those arguments will match the struct. This is important because the
77553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // LLVM backend isn't smart enough to remove byval, which inhibits many
77653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // optimizations.
777a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (getContext().getTypeSize(Ty) <= 4*32 &&
778a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        canExpandIndirectArgument(Ty, getContext()))
77953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar      return ABIArgInfo::getExpand();
780c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
781a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    return getIndirectResult(Ty);
7828bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  }
7838bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
784bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
7857b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // On Darwin, some vectors are passed in memory, we handle this by passing
7867b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // it as an i8/i16/i32/i64.
787bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    if (IsDarwinVectorABI) {
788bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      uint64_t Size = getContext().getTypeSize(Ty);
789bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      if ((Size == 8 || Size == 16 || Size == 32) ||
790bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner          (Size == 64 && VT->getNumElements() == 1))
791bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
792bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner                                                            Size));
793bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    }
794bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling
7959cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *IRType = CGT.ConvertType(Ty);
796bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    if (UseX86_MMXType(IRType)) {
797c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman      if (IsMMXDisabled)
798c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
799c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman                                                            64));
800bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
801bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
802bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      return AAI;
803bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    }
8049cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
805bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    return ABIArgInfo::getDirect();
806bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  }
8079cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
8089cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
809a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
810a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    Ty = EnumTy->getDecl()->getIntegerType();
811aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
812a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  return (Ty->isPromotableIntegerType() ?
813a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
814c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
815c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
816aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindolavoid X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
817aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
818aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola                                          FI.getCallingConvention());
819b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
820b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() :
821b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    DefaultNumRegisterParameters;
822b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
823b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // If the return value is indirect, then the hidden argument is consuming one
824b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // integer register.
825b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (FI.getReturnInfo().isIndirect() && FreeRegs) {
826b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    --FreeRegs;
827b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    ABIArgInfo &Old = FI.getReturnInfo();
828b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
829b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                       Old.getIndirectByVal(),
830b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                       Old.getIndirectRealign());
831b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
832b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
833aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
834aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola       it != ie; ++it)
835b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    it->info = classifyArgumentTypeWithReg(it->type, FreeRegs);
836aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola}
837aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola
838c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
839c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
8408b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
841c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
842c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
843c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
844c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                       "ap");
845c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
8467b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
8477b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  // Compute if the address needs to be aligned
8487b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
8497b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = getTypeStackAlignInBytes(Ty, Align);
8507b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = std::max(Align, 4U);
8517b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  if (Align > 4) {
8527b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    // addr = (addr + align - 1) & -align;
8537b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Offset =
8547b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman      llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
8557b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateGEP(Addr, Offset);
8567b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
8577b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                                    CGF.Int32Ty);
8587b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
8597b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
8607b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      Addr->getType(),
8617b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      "ap.cur.aligned");
8627b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  }
8637b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
864c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Type *PTy =
86596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
866c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
867c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
868c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
8697b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
870c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
87177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
872c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
873c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
874c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
875c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
876c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
877c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
87874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davisvoid X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
87974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                                  llvm::GlobalValue *GV,
88074f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                            CodeGen::CodeGenModule &CGM) const {
88174f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
88274f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
88374f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Get the LLVM function.
88474f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      llvm::Function *Fn = cast<llvm::Function>(GV);
88574f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
88674f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Now add the 'alignstack' attribute with a value of 16.
88774f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
88874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    }
88974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  }
89074f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis}
89174f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
8926374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCallbool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
8936374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               CodeGen::CodeGenFunction &CGF,
8946374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               llvm::Value *Address) const {
8956374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
8966374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
8978b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
8988bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
8996374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 0-7 are the eight integer registers;  the order is different
9006374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  //   on Darwin (for EH), but the range is the same.
9016374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 8 is %eip.
902aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 8);
9036374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
9046374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  if (CGF.CGM.isTargetDarwin()) {
9056374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 12-16 are st(0..4).  Not sure why we stop at 4.
9066374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 16, which is sizeof(long double) on
9076374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 8-byte alignment for that type.
9088b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
909aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
9108bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
9116374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  } else {
9126374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 9 is %eflags, which doesn't get a size on Darwin for some
9136374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // reason.
9146374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
9156374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
9166374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 11-16 are st(0..5).  Not sure why we stop at 5.
9176374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 12, which is sizeof(long double) on
9186374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 4-byte alignment for that type.
9198b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
920aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
921aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
9226374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
9236374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  return false;
9246374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall}
9256374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
926dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
927dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner// X86-64 ABI Implementation
928dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
929dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
930dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
931c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovnamespace {
932c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// X86_64ABIInfo - The X86_64 ABI information.
933c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass X86_64ABIInfo : public ABIInfo {
934c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  enum Class {
935c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Integer = 0,
936c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSE,
937c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSEUp,
938c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87,
939c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87Up,
940c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ComplexX87,
941c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    NoClass,
942c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Memory
943c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  };
944c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
945c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// merge - Implement the X86_64 ABI merging algorithm.
946c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
947c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// Merge an accumulating classification \arg Accum with a field
948c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classification \arg Field.
949c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
950c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Accum - The accumulating classification. This should
951c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// always be either NoClass or the result of a previous merge
952c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// call. In addition, this should never be Memory (the caller
953c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// should just return Memory for the aggregate).
9541090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  static Class merge(Class Accum, Class Field);
955c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
9564943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// postMerge - Implement the X86_64 ABI post merging algorithm.
9574943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
9584943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// Post merger cleanup, reduces a malformed Hi and Lo pair to
9594943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// final MEMORY or SSE classes when necessary.
9604943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
9614943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param AggregateSize - The size of the current aggregate in
9624943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// the classification process.
9634943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
9644943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Lo - The classification for the parts of the type
9654943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the low word of the containing object.
9664943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
9674943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Hi - The classification for the parts of the type
9684943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the higher words of the containing object.
9694943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
9704943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
9714943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
972c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classify - Determine the x86_64 register classes in which the
973c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// given type T should be passed.
974c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
975c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Lo - The classification for the parts of the type
976c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the low word of the containing object.
977c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
978c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Hi - The classification for the parts of the type
979c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the high word of the containing object.
980c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
981c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param OffsetBase - The bit offset of this type in the
982c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// containing object.  Some parameters are classified different
983c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// depending on whether they straddle an eightbyte boundary.
984c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
985c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If a word is unused its result will be NoClass; if a type should
986c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// be passed in Memory then at least the classification of \arg Lo
987c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// will be Memory.
988c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
989c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// The \arg Lo class will be NoClass iff the argument is ignored.
990c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
991c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
992c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// also be ComplexX87.
9939c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
994c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
9954943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  llvm::Type *GetByteVectorType(QualType Ty) const;
9969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
9979cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned IROffset, QualType SourceTy,
9989cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned SourceOffset) const;
9999cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
10009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned IROffset, QualType SourceTy,
10019cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned SourceOffset) const;
10028bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1003c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
100446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// such that the argument will be returned in memory.
10059c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  ABIArgInfo getIndirectReturnResult(QualType Ty) const;
100646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
100746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1008c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// such that the argument will be passed in memory.
1009edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ///
1010edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// \param freeIntRegs - The number of free integer registers remaining
1011edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// available.
1012edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
1013c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1014a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
1015c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1016bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  ABIArgInfo classifyArgumentType(QualType Ty,
1017edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                  unsigned freeIntRegs,
1018bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling                                  unsigned &neededInt,
101999aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling                                  unsigned &neededSSE) const;
1020c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1021ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool IsIllegalVectorType(QualType Ty) const;
1022ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
102367a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// The 0.98 ABI revision clarified a lot of ambiguities,
102467a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// unfortunately in ways that were not always consistent with
102567a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// certain previous compilers.  In particular, platforms which
102667a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// required strict binary compatibility with older versions of GCC
102767a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// may need to exempt themselves.
102867a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  bool honorsRevision0_98() const {
1029bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return !getContext().getTargetInfo().getTriple().isOSDarwin();
103067a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  }
103167a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall
1032ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool HasAVX;
1033ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1034c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovpublic:
1035ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
1036ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      ABIInfo(CGT), HasAVX(hasavx) {}
10379c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
1038de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isPassedUsingAVXType(QualType type) const {
1039de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    unsigned neededInt, neededSSE;
1040edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // The freeIntRegs argument doesn't matter here.
1041edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
1042de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (info.isDirect()) {
1043de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      llvm::Type *ty = info.getCoerceToType();
1044de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1045de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        return (vectorTy->getBitWidth() > 128);
1046de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    }
1047de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return false;
1048de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
1049de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
1050ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
1051c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1052c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1053c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
1054c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
105582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1056f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
1057a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumiclass WinX86_64ABIInfo : public ABIInfo {
1058a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1059a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  ABIArgInfo classify(QualType Ty) const;
1060a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1061f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
1062a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1063a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1064a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  virtual void computeInfo(CGFunctionInfo &FI) const;
1065f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1066f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1067f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                 CodeGenFunction &CGF) const;
1068f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
1069f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
107082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
107182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
1072ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
1073ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
10746374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
1075de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  const X86_64ABIInfo &getABIInfo() const {
1076de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1077de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
1078de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
10796374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
10806374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 7;
10816374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
10826374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
10836374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
10846374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const {
10858b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
10868bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1087aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 0-15 are the 16 integer registers.
1088aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 16 is %rip.
10898b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
10906374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return false;
10916374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
10924b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1093ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
10945f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
1095ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
10964b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
10974b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
10984b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1099de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isNoProtoCallVariadic(const CallArgList &args,
1100de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             const FunctionNoProtoType *fnType) const {
110101f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // The default CC on x86-64 sets %al to the number of SSA
110201f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // registers used, and GCC sets this when calling an unprototyped
11033ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    // function, so we override the default behavior.  However, don't do
110468805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // that when AVX types are involved: the ABI explicitly states it is
110568805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // undefined, and it doesn't work in practice because of how the ABI
110668805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // defines varargs anyway.
1107de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
11083ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      bool HasAVXType = false;
1109de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      for (CallArgList::const_iterator
1110de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall             it = args.begin(), ie = args.end(); it != ie; ++it) {
1111de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1112de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          HasAVXType = true;
1113de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          break;
11143ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        }
11153ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      }
1116de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
11173ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      if (!HasAVXType)
11183ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        return true;
11193ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    }
112001f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
1121de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
112201f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  }
112301f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
112482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
112582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1126f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerclass WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1127f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
1128f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1129f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1130f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1131f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1132f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return 7;
1133f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
1134f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1135f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1136f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                               llvm::Value *Address) const {
11378b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
11389cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1139f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 0-15 are the 16 integer registers.
1140f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 16 is %rip.
11418b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1142f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return false;
1143f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
1144f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
1145f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1146c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1147c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
11484943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesvoid X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
11494943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes                              Class &Hi) const {
11504943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
11514943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
11524943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (a) If one of the classes is Memory, the whole argument is passed in
11534943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
11544943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
11554943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (b) If X87UP is not preceded by X87, the whole argument is passed in
11564943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
11574943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
11584943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (c) If the size of the aggregate exceeds two eightbytes and the first
11594943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
11604943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     argument is passed in memory. NOTE: This is necessary to keep the
11614943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     ABI working for processors that don't support the __m256 type.
11624943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
11634943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
11644943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
11654943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Some of these are enforced by the merging logic.  Others can arise
11664943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // only with unions; for example:
11674943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //   union { _Complex double; unsigned; }
11684943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
11694943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Note that clauses (b) and (c) were added in 0.98.
11704943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
11714943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == Memory)
11724943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
11734943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
11744943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
11754943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
11764943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
11774943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == SSEUp && Lo != SSE)
11784943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Hi = SSE;
11794943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes}
11804943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
11811090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerX86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
1182c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1183c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classified recursively so that always two fields are
1184c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // considered. The resulting class is calculated according to
1185c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the classes of the fields in the eightbyte:
1186c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1187c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (a) If both classes are equal, this is the resulting class.
1188c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1189c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (b) If one of the classes is NO_CLASS, the resulting class is
1190c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the other class.
1191c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1192c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (c) If one of the classes is MEMORY, the result is the MEMORY
1193c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // class.
1194c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (d) If one of the classes is INTEGER, the result is the
1196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // INTEGER.
1197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // MEMORY is used as class.
1200c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1201c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (f) Otherwise class SSE is used.
1202c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1203c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Accum should never be memory (we should have returned) or
1204c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // ComplexX87 (because this cannot be passed in a structure).
1205c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Accum != Memory && Accum != ComplexX87) &&
1206c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         "Invalid accumulated classification during merge.");
1207c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Accum == Field || Field == NoClass)
1208c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Accum;
12091090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == Memory)
1210c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
12111090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == NoClass)
1212c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Field;
12131090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == Integer || Field == Integer)
1214c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Integer;
12151090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
12161090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      Accum == X87 || Accum == X87Up)
1217c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
12181090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  return SSE;
1219c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1220c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1221bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattnervoid X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
1222c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                             Class &Lo, Class &Hi) const {
1223c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This code can be simplified by introducing a simple value class for
1224c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Class pairs with appropriate constructor methods for the various
1225c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // situations.
1226c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1227c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Some of the split computations are wrong; unaligned vectors
1228c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // shouldn't be passed in registers for example, so there is no chance they
1229c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // can straddle an eightbyte. Verify & simplify.
1230c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1231c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Lo = Hi = NoClass;
1232c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1233c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Class &Current = OffsetBase < 64 ? Lo : Hi;
1234c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Current = Memory;
1235c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1236183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1237c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    BuiltinType::Kind k = BT->getKind();
1238c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1239c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (k == BuiltinType::Void) {
1240c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = NoClass;
1241c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1242c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Integer;
1243c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Integer;
1244c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1245c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
1246c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1247c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
1248c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::LongDouble) {
1249c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = X87;
1250c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = X87Up;
1251c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1252c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _Decimal32 and _Decimal64 are SSE.
1253c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
12541090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
12551090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
12568bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
12571090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const EnumType *ET = Ty->getAs<EnumType>()) {
1258c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Classify the underlying integer type.
12599c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
12601090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
12611090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
12628bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
12631090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->hasPointerRepresentation()) {
1264c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = Integer;
12651090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
12661090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
12678bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
12681090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->isMemberPointerType()) {
126967d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar    if (Ty->isMemberFunctionPointerType())
127067d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Lo = Hi = Integer;
127167d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar    else
127267d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Current = Integer;
12731090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
12741090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
12758bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
12761090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
1277ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(VT);
1278c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 32) {
1279c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1280c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // float> as integer.
1281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
1282c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1283c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1284c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1285c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Real = (OffsetBase) / 64;
1286c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1287c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (EB_Real != EB_Imag)
1288c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1289c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (Size == 64) {
1290c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x double> in memory. :(
1291c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1292c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1293c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1294c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x long long> as INTEGER.
1295473f8e723be93d84bd5fd15b094f4184802d4676Chris Lattner      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
12960fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
12970fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
12980fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
1299c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1300c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else
1301c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = SSE;
1302c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1303c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1304c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1305c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (OffsetBase && OffsetBase != 64)
1306c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1307ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    } else if (Size == 128 || (HasAVX && Size == 256)) {
13084943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // Arguments of 256-bits are split into four eightbyte chunks. The
13094943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // least significant one belongs to class SSE and all the others to class
13104943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // SSEUP. The original Lo and Hi design considers that types can't be
13114943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
13124943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // This design isn't correct for 256-bits, but since there're no cases
13134943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // where the upper parts would need to be inspected, avoid adding
13144943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // complexity and just consider Hi to match the 64-256 part.
1315c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = SSE;
1316c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = SSEUp;
1317c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
13181090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13191090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13208bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13211090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
1322ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    QualType ET = getContext().getCanonicalType(CT->getElementType());
1323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1324ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
13252ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (ET->isIntegralOrEnumerationType()) {
1326c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size <= 64)
1327c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1328c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else if (Size <= 128)
1329c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Hi = Integer;
1330ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    } else if (ET == getContext().FloatTy)
1331c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
1332ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    else if (ET == getContext().DoubleTy)
1333c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Hi = SSE;
1334ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    else if (ET == getContext().LongDoubleTy)
1335c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = ComplexX87;
1336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1337c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // If this complex type crosses an eightbyte boundary then it
1338c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // should be split.
1339c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t EB_Real = (OffsetBase) / 64;
1340ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
1341c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Hi == NoClass && EB_Real != EB_Imag)
1342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Lo;
13438bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13441090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13451090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13468bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1347ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
1348c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Arrays are treated like structures.
1349c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1350ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1351c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1352c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
13534943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
13544943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1355c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1356c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1357c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1358c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // fields, it has class MEMORY.
1359c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
1360c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Only need to check alignment of array base.
1361ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
1362c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1363c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1364c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Otherwise implement simplified merge. We could be smarter about
1365c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // this, but it isn't worth it and would be harder to verify.
1366c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1367ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
1368c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t ArraySize = AT->getSize().getZExtValue();
1369089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1370089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // The only case a 256-bit wide vector could be used is when the array
1371089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1372089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // to work for sizes wider than 128, early check and fallback to memory.
1373089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    if (Size > 128 && EltSize != 256)
1374089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes      return;
1375089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1376c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1377c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
13789c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner      classify(AT->getElementType(), Offset, FieldLo, FieldHi);
1379c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1380c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1381c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1382c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1383c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1384c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
13854943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1386c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
13871090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13881090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13901090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1391ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1392c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1393c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
13944943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
13954943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1396c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1397c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
13980a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
13990a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // copy constructor or a non-trivial destructor, it is passed by invisible
14000a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // reference.
14010a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    if (hasNonTrivialDestructorOrCopyConstructor(RT))
14020a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson      return;
1403ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1404c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    const RecordDecl *RD = RT->getDecl();
1405c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1406c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Assume variable sized types are passed in memory.
1407c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (RD->hasFlexibleArrayMember())
1408c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1409c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1410ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1411c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1412c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Reset Lo class, this will be recomputed.
1413c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1414ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1415ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // If this is a C++ record, classify the bases first.
1416ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1417ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1418ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar             e = CXXRD->bases_end(); i != e; ++i) {
1419ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1420ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar               "Unexpected base class!");
1421ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        const CXXRecordDecl *Base =
1422ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1423ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1424ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // Classify this field.
1425ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        //
1426ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1427ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // single eightbyte, each is classified separately. Each eightbyte gets
1428ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // initialized to class NO_CLASS.
1429ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Class FieldLo, FieldHi;
1430d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        uint64_t Offset =
1431d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer          OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
14329c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1433ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Lo = merge(Lo, FieldLo);
1434ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Hi = merge(Hi, FieldHi);
1435ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        if (Lo == Memory || Hi == Memory)
1436ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          break;
1437ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      }
1438ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    }
1439ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1440ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // Classify the fields one at a time, merging the results.
1441c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    unsigned idx = 0;
1442548e478b8bd02b0295bc4efd0c282337f00646fdBruno Cardoso Lopes    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
144317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis           i != e; ++i, ++idx) {
1444c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1445c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      bool BitField = i->isBitField();
1446c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1447b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1448b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // four eightbytes, or it contains unaligned fields, it has class MEMORY.
1449c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1450b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // The only case a 256-bit wide vector could be used is when the struct
1451b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1452b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // to work for sizes wider than 128, early check and fallback to memory.
1453b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      //
1454b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1455b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        Lo = Memory;
1456b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        return;
1457b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      }
1458c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Note, skip this test for bit-fields, see below.
1459ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner      if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
1460c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Memory;
1461c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1462c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      }
1463c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1464c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Classify this field.
1465c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1466c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1467c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // exceeds a single eightbyte, each is classified
1468c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // separately. Each eightbyte gets initialized to class
1469c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // NO_CLASS.
1470c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
1471c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1472c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Bit-fields require special handling, they do not force the
1473c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // structure to be passed in memory even if unaligned, and
1474c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // therefore they can straddle an eightbyte.
1475c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (BitField) {
1476c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        // Ignore padding bit-fields.
1477c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (i->isUnnamedBitfield())
1478c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          continue;
1479c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1480c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1481a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        uint64_t Size = i->getBitWidthValue(getContext());
1482c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1483c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Lo = Offset / 64;
1484c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Hi = (Offset + Size - 1) / 64;
1485c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        FieldLo = FieldHi = NoClass;
1486c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (EB_Lo) {
1487c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1488c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = NoClass;
1489c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = Integer;
1490c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        } else {
1491c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = Integer;
1492c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = EB_Hi ? Integer : NoClass;
1493c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        }
1494c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      } else
14959c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1496c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1497c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1498c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1499c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1500c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1501c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15024943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1503c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
1504c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1505c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15069c254f0415bef9a0bafe5b5026ddb54b727597b1Chris LattnerABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
150746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // If this is a scalar LLVM value then assume LLVM will pass it in the right
150846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // place naturally.
1509d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
151046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    // Treat an enum type as its underlying type.
151146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
151246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar      Ty = EnumTy->getDecl()->getIntegerType();
151346c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
151446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return (Ty->isPromotableIntegerType() ?
151546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
151646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  }
151746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
151846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  return ABIArgInfo::getIndirect(0);
151946c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar}
152046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
1521ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedmanbool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1522ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1523ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    uint64_t Size = getContext().getTypeSize(VecTy);
1524ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    unsigned LargestVector = HasAVX ? 256 : 128;
1525ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    if (Size <= 64 || Size > LargestVector)
1526ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return true;
1527ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
1528ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1529ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  return false;
1530ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman}
1531ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1532edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1533edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                            unsigned freeIntRegs) const {
1534c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1535c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // place naturally.
1536edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1537edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // This assumption is optimistic, as there could be free registers available
1538edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // when we need to pass this argument in memory, and LLVM could try to pass
1539edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // the argument in the free register. This does not seem to happen currently,
1540edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // but this code would be much safer if we could mark the argument with
1541edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // 'onstack'. See PR12193.
1542ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
1543aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
1544aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1545aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
1546aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
1547cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
1548cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1549aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
1550c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
155146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
155246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
15530a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1554855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // Compute the byval alignment. We specify the alignment of the byval in all
1555855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // cases so that the mid-level optimizer knows the alignment of the byval.
1556855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1557edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1558edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // Attempt to avoid passing indirect results using byval when possible. This
1559edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // is important for good codegen.
1560edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1561edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We do this by coercing the value into a scalar type which the backend can
1562edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // handle naturally (i.e., without using byval).
1563edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1564edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // For simplicity, we currently only do this when we have exhausted all of the
1565edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // free integer registers. Doing this when there are free integer registers
1566edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // would require more care, as we would have to ensure that the coerced value
1567edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // did not claim the unused register. That would require either reording the
1568edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to the function (so that any subsequent inreg values came first),
1569edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // or only doing this optimization when there were no following arguments that
1570edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // might be inreg.
1571edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1572edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We currently expect it to be rare (particularly in well written code) for
1573edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to be passed on the stack when there are still free integer
1574edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // registers available (this would typically imply large structs being passed
1575edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // by value), so this seems like a fair tradeoff for now.
1576edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1577edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We can revisit this if the backend grows support for 'onstack' parameter
1578edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // attributes. See PR12193.
1579edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  if (freeIntRegs == 0) {
1580edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    uint64_t Size = getContext().getTypeSize(Ty);
1581edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1582edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // If this type fits in an eightbyte, coerce it into the matching integral
1583edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // type, which will end up on the stack (with alignment 8).
1584edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    if (Align == 8 && Size <= 64)
1585edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1586edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                                          Size));
1587edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  }
1588edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1589855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  return ABIArgInfo::getIndirect(Align);
1590c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1591c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15924943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// GetByteVectorType - The ABI specifies that a value should be passed in an
15934943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// full vector XMM/YMM register.  Pick an LLVM IR type that will be passed as a
15940f408f5242522cbede304472e17931357c1b573dChris Lattner/// vector register.
15954943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesllvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
15969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *IRType = CGT.ConvertType(Ty);
15978bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
159815842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // Wrapper structs that just contain vectors are passed just like vectors,
159915842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // strip them off if present.
16009cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
160115842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  while (STy && STy->getNumElements() == 1) {
160215842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    IRType = STy->getElementType(0);
160315842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    STy = dyn_cast<llvm::StructType>(IRType);
160415842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  }
16058bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1606528a8c7b4c39ae1c551760fd087a508a71ee9541Bruno Cardoso Lopes  // If the preferred type is a 16-byte vector, prefer to pass it.
16079cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
16089cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = VT->getElementType();
16094943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    unsigned BitWidth = VT->getBitWidth();
1610ce275675d33142c235d7027db16abe43da616ee4Tanya Lattner    if ((BitWidth >= 128 && BitWidth <= 256) &&
16110f408f5242522cbede304472e17931357c1b573dChris Lattner        (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
16120f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
16130f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
16140f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(128)))
16150f408f5242522cbede304472e17931357c1b573dChris Lattner      return VT;
16160f408f5242522cbede304472e17931357c1b573dChris Lattner  }
16178bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
16180f408f5242522cbede304472e17931357c1b573dChris Lattner  return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
16190f408f5242522cbede304472e17931357c1b573dChris Lattner}
16200f408f5242522cbede304472e17931357c1b573dChris Lattner
1621e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1622e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// is known to either be off the end of the specified type or being in
1623e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// alignment padding.  The user type specified is known to be at most 128 bits
1624e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// in size, and have passed through X86_64ABIInfo::classify with a successful
1625e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// classification that put one of the two halves in the INTEGER class.
1626e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner///
1627e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// It is conservatively correct to return false.
1628e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattnerstatic bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1629e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                  unsigned EndBit, ASTContext &Context) {
1630e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If the bytes being queried are off the end of the type, there is no user
1631e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // data hiding here.  This handles analysis of builtins, vectors and other
1632e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // types that don't contain interesting padding.
1633e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1634e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (TySize <= StartBit)
1635e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1636e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1637021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1638021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1639021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1640021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner
1641021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // Check each element to see if the element overlaps with the queried range.
1642021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    for (unsigned i = 0; i != NumElts; ++i) {
1643021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      // If the element is after the span we care about, then we're done..
1644021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltOffset = i*EltSize;
1645021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (EltOffset >= EndBit) break;
16468bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1647021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1648021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1649021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner                                 EndBit-EltOffset, Context))
1650021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner        return false;
1651021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    }
1652021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // If it overlaps no elements, then it is safe to process as padding.
1653021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    return true;
1654021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
16558bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1656e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1657e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const RecordDecl *RD = RT->getDecl();
1658e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
16598bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1660e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If this is a C++ record, check the bases first.
1661e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1662e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1663e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner           e = CXXRD->bases_end(); i != e; ++i) {
1664e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1665e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner               "Unexpected base class!");
1666e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        const CXXRecordDecl *Base =
1667e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
16688bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1669e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        // If the base is after the span we care about, ignore it.
1670d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
1671e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (BaseOffset >= EndBit) continue;
16728bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1673e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1674e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (!BitsContainNoUserData(i->getType(), BaseStart,
1675e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                   EndBit-BaseOffset, Context))
1676e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          return false;
1677e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      }
1678e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
16798bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1680e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Verify that no field has data that overlaps the region of interest.  Yes
1681e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // this could be sped up a lot by being smarter about queried fields,
1682e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // however we're only looking at structs up to 16 bytes, so we don't care
1683e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // much.
1684e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    unsigned idx = 0;
1685e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1686e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner         i != e; ++i, ++idx) {
1687e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
16888bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1689e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      // If we found a field after the region we care about, then we're done.
1690e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (FieldOffset >= EndBit) break;
1691e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1692e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1693e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1694e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                 Context))
1695e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return false;
1696e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
16978bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1698e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If nothing in this record overlapped the area of interest, then we're
1699e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // clean.
1700e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1701e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
17028bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1703e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  return false;
1704e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner}
1705e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
17060b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
17070b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float member at the specified offset.  For example, {int,{float}} has a
17080b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float at offset 4.  It is conservatively correct for this routine to return
17090b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// false.
17102acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerstatic bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
17110b3620066bfbb33004bed1816c851a923b9301afChris Lattner                                  const llvm::TargetData &TD) {
17120b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // Base case if we find a float.
17130b3620066bfbb33004bed1816c851a923b9301afChris Lattner  if (IROffset == 0 && IRType->isFloatTy())
17140b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return true;
17158bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
17160b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is a struct, recurse into the field at the specified offset.
17172acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
17180b3620066bfbb33004bed1816c851a923b9301afChris Lattner    const llvm::StructLayout *SL = TD.getStructLayout(STy);
17190b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned Elt = SL->getElementContainingOffset(IROffset);
17200b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= SL->getElementOffset(Elt);
17210b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
17220b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
17238bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
17240b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is an array, recurse into the field at the specified offset.
17252acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
17262acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ATy->getElementType();
17270b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned EltSize = TD.getTypeAllocSize(EltTy);
17280b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= IROffset/EltSize*EltSize;
17290b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(EltTy, IROffset, TD);
17300b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
17310b3620066bfbb33004bed1816c851a923b9301afChris Lattner
17320b3620066bfbb33004bed1816c851a923b9301afChris Lattner  return false;
17330b3620066bfbb33004bed1816c851a923b9301afChris Lattner}
17340b3620066bfbb33004bed1816c851a923b9301afChris Lattner
1735f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1736f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1737f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// low 8 bytes of an XMM register, corresponding to the SSE class.
17389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
17399cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
1740f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                   QualType SourceTy, unsigned SourceOffset) const {
1741cba8d310163f84630fd140fbfa9b6fdad9d26587Chris Lattner  // The only three choices we have are either double, <2 x float>, or float. We
1742f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // pass as float if the last 4 bytes is just padding.  This happens for
1743f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // structs that contain 3 floats.
1744f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1745f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                            SourceOffset*8+64, getContext()))
1746f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner    return llvm::Type::getFloatTy(getVMContext());
17478bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
17480b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // We want to pass as <2 x float> if the LLVM IR type contains a float at
17490b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
17500b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // case.
17510b3620066bfbb33004bed1816c851a923b9301afChris Lattner  if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
175222fd4baf2eba2103e2b41e463f1a5f6486c398fbChris Lattner      ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
175322fd4baf2eba2103e2b41e463f1a5f6486c398fbChris Lattner    return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
17548bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1755f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  return llvm::Type::getDoubleTy(getVMContext());
1756f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner}
1757f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1758f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
17590d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
17600d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// an 8-byte GPR.  This means that we either have a scalar or we are talking
17610d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// about the high or low part of an up-to-16-byte struct.  This routine picks
17620d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// the best LLVM IR type to represent this, which may be i64 or may be anything
1763519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1764519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// etc).
1765519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1766519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1767519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the source type.  IROffset is an offset in bytes into the LLVM IR type that
1768519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the 8-byte value references.  PrefType may be null.
1769519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1770519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// SourceTy is the source level type for the entire argument.  SourceOffset is
1771519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// an offset into this that we're processing (which is always either 0 or 8).
1772519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
17739cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
17749cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
17750d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                       QualType SourceTy, unsigned SourceOffset) const {
1776e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If we're dealing with an un-offset LLVM IR type, then it means that we're
1777e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // returning an 8-byte unit starting with it.  See if we can safely use it.
1778e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (IROffset == 0) {
1779e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Pointers and int64's always fill the 8-byte unit.
1780e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1781e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      return IRType;
1782e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1783e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1784e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // goodness in the source type is just tail padding.  This is allowed to
1785e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // kick in for struct {double,int} on the int, but not on
1786e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // struct{double,int,int} because we wouldn't return the second int.  We
1787e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // have to do this analysis on the source type because we can't depend on
1788e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // unions being lowered a specific way etc.
1789e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1790e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        IRType->isIntegerTy(32)) {
1791e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
17928bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1793e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1794e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                SourceOffset*8+64, getContext()))
1795e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return IRType;
1796e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
1797e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
1798519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
17992acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1800519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    // If this is a struct, recurse into the field at the specified offset.
180144f0fd2804e9952a8dbf85bb60ee3501aa9f5ee7Chris Lattner    const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
1802519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    if (IROffset < SL->getSizeInBytes()) {
1803519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1804519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      IROffset -= SL->getElementOffset(FieldIdx);
18058bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18060d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner      return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
18070d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                    SourceTy, SourceOffset);
18088bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    }
1809519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  }
18108bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18112acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
18129cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = ATy->getElementType();
1813021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1814021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltOffset = IROffset/EltSize*EltSize;
18150d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner    return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
18160d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                  SourceOffset);
1817021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
18188bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1819519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // Okay, we don't have any better idea of what to pass, so we pass this in an
1820519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // integer register that isn't too big to fit the rest of the struct.
18219e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  unsigned TySizeInBytes =
18229e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner    (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
1823519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
18249e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  assert(TySizeInBytes != SourceOffset && "Empty field?");
18258bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1826519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // It is always safe to classify this as an integer type up to i64 that
1827519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // isn't larger than the structure.
18289e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  return llvm::IntegerType::get(getVMContext(),
18299e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner                                std::min(TySizeInBytes-SourceOffset, 8U)*8);
1830519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner}
1831519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
183266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
183366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
183466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// be used as elements of a two register pair to pass or return, return a
183566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// first class aggregate to represent them.  For example, if the low part of
183666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// a by-value argument should be passed as i32* and the high part as float,
183766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// return {i32*, float}.
18389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *
1839ef6de3da8572607f786303c07150daa6e140ab19Jay FoadGetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
184066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner                           const llvm::TargetData &TD) {
184166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // In order to correctly satisfy the ABI, we need to the high part to start
184266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // at offset 8.  If the high and low parts we inferred are both 4-byte types
184366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
184466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // the second element at offset 8.  Check for this:
184566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
184666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned HiAlign = TD.getABITypeAlignment(Hi);
184766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
184866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
18499cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
185066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // To handle this, we have to increase the size of the low part so that the
185166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // second element will start at an 8 byte offset.  We can't increase the size
185266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // of the second element because it might make us access off the end of the
185366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // struct.
185466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HiStart != 8) {
185566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // There are only two sorts of types the ABI generation code can produce for
185666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
185766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // Promote these to a larger type.
185866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    if (Lo->isFloatTy())
185966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getDoubleTy(Lo->getContext());
186066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    else {
186166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
186266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getInt64Ty(Lo->getContext());
186366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    }
186466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  }
18659cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
18669cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
18679cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
18689cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
186966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // Verify that the second element is at an 8-byte offset.
187066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
187166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner         "Invalid x86-64 argument pair!");
187266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  return Result;
187366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner}
187466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
18751090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerABIArgInfo X86_64ABIInfo::
1876a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerclassifyReturnType(QualType RetTy) const {
1877c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1878c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classification algorithm.
1879c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
18809c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(RetTy, 0, Lo, Hi);
1881c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1882c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
1883c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1884c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1885c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
18869cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
1887c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
1888c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
1889117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
1890117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
1891117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
1892117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
1893117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1894117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
1895117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
1896c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1897c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
1898c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
1899b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
1900c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1901c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1902c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // hidden argument.
1903c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
19049c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    return getIndirectReturnResult(RetTy);
1905c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1906c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1907c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rax, %rdx is used.
1908c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
19099cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
19108bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1911eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
1912eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
1913eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1914eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
1915eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1916eb518b4b89e4134b21975530809697142f69b779Chris Lattner        RetTy = EnumTy->getDecl()->getIntegerType();
19178bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1918eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (RetTy->isIntegralOrEnumerationType() &&
1919eb518b4b89e4134b21975530809697142f69b779Chris Lattner          RetTy->isPromotableIntegerType())
1920eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
1921eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
1922519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    break;
1923c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1924c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1925c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register of the sequence %xmm0, %xmm1 is used.
1926c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSE:
19279cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
19280b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
1929c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1930c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1931c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned on the X87 stack in %st0 as 80-bit x87 number.
1932c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
1933ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    ResType = llvm::Type::getX86_FP80Ty(getVMContext());
19340b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
1935c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1936c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1937c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // part of the value is returned in %st0 and the imaginary part in
1938c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // %st1.
1939c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
1940c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
19417650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
1942ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner                                    llvm::Type::getX86_FP80Ty(getVMContext()),
1943c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                    NULL);
1944c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1945c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
1946c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
19479cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
1948c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
1949c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously and X87 should
1950c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // never occur as a hi class.
1951c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
1952c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
1953b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
1954c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1955c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87: // Previously handled.
19560b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner  case NoClass:
19570b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
1958c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
19593db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case Integer:
19609cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
19613db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
19623db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
1963c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
19643db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case SSE:
19659cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
19663db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
19673db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
1968c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1969c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1970c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
19714943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // is passed in the next available eightbyte chunk if the last used
19724943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // vector register.
1973c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
1974fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // SSEUP should always be preceded by SSE, just widen.
1975c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
1976c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Lo == SSE && "Unexpected SSEUp classification.");
19774943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(RetTy);
1978c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1979c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1980c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1981c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned together with the previous X87 value in %st0.
1982c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
1983fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // If X87Up is preceded by X87, we don't need to do
1984c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // anything. However, in some cases with unions it may not be
1985fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // preceded by X87. In such situations we follow gcc and pass the
1986c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // extra bits in an SSE reg.
1987603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    if (Lo != X87) {
19889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
19893db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
19903db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner        return ABIArgInfo::getDirect(HighPart, 8);
1991603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    }
1992c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1993c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
19949cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
19953db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
1996645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
1997645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
199866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HighPart)
199966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
2000c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2001eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
20029c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner}
20039c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
2004edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::classifyArgumentType(
2005edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2006edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  const
2007edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar{
2008c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
20099c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(Ty, 0, Lo, Hi);
20108bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2011c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
2012c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Enforce these by construction.
2013c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2014c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2015c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2016c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededInt = 0;
2017c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededSSE = 0;
20189cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
2019c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
2020c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
2021117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
2022117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
2023117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
2024117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
2025117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2026117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
2027117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
20288bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2029c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2030c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // on the stack.
2031c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2032c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2033c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2034c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // COMPLEX_X87, it is passed in memory.
2035c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2036c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2037ded137fcab19f0aace08a28b5c91574e6b23debcEli Friedman    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2038ded137fcab19f0aace08a28b5c91574e6b23debcEli Friedman      ++neededInt;
2039edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    return getIndirectResult(Ty, freeIntRegs);
2040c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2041c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2042c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2043b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
2044c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2045c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2046c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2047c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // and %r9 is used.
2048c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
20499c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    ++neededInt;
20508bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
205149382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
20529cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
2053eb518b4b89e4134b21975530809697142f69b779Chris Lattner
2054eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
2055eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
2056eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2057eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
2058eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2059eb518b4b89e4134b21975530809697142f69b779Chris Lattner        Ty = EnumTy->getDecl()->getIntegerType();
20608bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2061eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (Ty->isIntegralOrEnumerationType() &&
2062eb518b4b89e4134b21975530809697142f69b779Chris Lattner          Ty->isPromotableIntegerType())
2063eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
2064eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
20658bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2066c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2067c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2068c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2069c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register is used, the registers are taken in the
2070c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // order from %xmm0 to %xmm7.
2071bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  case SSE: {
20729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *IRType = CGT.ConvertType(Ty);
207314508ff0bffee0fdfe5d336946c6db0e709099c8Eli Friedman    ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
207499aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    ++neededSSE;
2075c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2076c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2077bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  }
2078c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
20799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
2080c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
2081c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously, ComplexX87 and X87 should
2082fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // never occur as hi classes, and X87Up must be preceded by X87,
2083c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // which is passed in memory.
2084c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2085c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2086c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2087b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
2088c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2089c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass: break;
20908bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2091645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case Integer:
2092c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededInt;
209349382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
20949cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2095117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2096645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2097645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2098c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2099c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2100c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // X87Up generally doesn't occur here (long double is passed in
2101c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // memory), except in situations involving unions.
2102c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2103645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case SSE:
21049cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
21058bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2106645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2107645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2108117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2109c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededSSE;
2110c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2111c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2112c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2113c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte is passed in the upper half of the last used SSE
21148bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    // register.  This only happens when 128-bit vectors are passed.
2115c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2116ab5722e67794b3954c874a369086fc5f41ac46a5Chris Lattner    assert(Lo == SSE && "Unexpected SSEUp classification");
21174943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(Ty);
2118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2120c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2121645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
2122645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
2123645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
2124645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  if (HighPart)
212566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
21269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2127eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
2128c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2129c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2130ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
21318bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2132a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2133c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2134c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Keep track of the number of assigned registers.
213599aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned freeIntRegs = 6, freeSSERegs = 8;
2136c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2137c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If the return value is indirect, then the hidden argument is consuming one
2138c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // integer register.
2139c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (FI.getReturnInfo().isIndirect())
2140c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    --freeIntRegs;
2141c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2142c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2143c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // get assigned (in left-to-right order) for passing as follows...
2144c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2145c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov       it != ie; ++it) {
214699aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    unsigned neededInt, neededSSE;
2147edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2148edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                    neededSSE);
2149c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2150c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: If there are no registers available for any
2151c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte of an argument, the whole argument is passed on the
2152c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // stack. If registers have already been assigned for some
2153c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbytes of such an argument, the assignments get reverted.
215499aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
2155c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeIntRegs -= neededInt;
2156c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeSSERegs -= neededSSE;
2157c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else {
2158edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      it->info = getIndirectResult(it->type, freeIntRegs);
2159c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
2160c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2161c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2162c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2163c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2164c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        QualType Ty,
2165c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        CodeGenFunction &CGF) {
2166c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area_p =
2167c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2168c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area =
2169c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2170c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2171c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2172c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // byte boundary if alignment needed by type exceeds 8 byte boundary.
21738d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // It isn't stated explicitly in the standard, but in practice we use
21748d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // alignment greater than 16 where necessary.
2175c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2176c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Align > 8) {
21778d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
21780032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    llvm::Value *Offset =
21798d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman      llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
2180c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2181c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
218277b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                                    CGF.Int64Ty);
21838d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
2184c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area =
2185c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2186c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 overflow_arg_area->getType(),
2187c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 "overflow_arg_area.align");
2188c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2189c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2190c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
21912acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2192c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Res =
2193c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateBitCast(overflow_arg_area,
219496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                              llvm::PointerType::getUnqual(LTy));
2195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->overflow_arg_area + sizeof(type).
2198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an 8 byte boundary.
2200c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2201c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
22020032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Offset =
220377b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
2204c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2205c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                            "overflow_arg_area.next");
2206c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2207c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2208c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2209c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Res;
2210c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2211c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2212c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2213c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
2214c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Assume that va_list type is correct; should be pointer to LLVM type:
2215c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // struct {
2216c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 gp_offset;
2217c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 fp_offset;
2218c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* overflow_arg_area;
2219c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* reg_save_area;
2220c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // };
222199aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned neededInt, neededSSE;
22228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2223a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  Ty = CGF.getContext().getCanonicalType(Ty);
2224edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
2225c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2226c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2227c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in the registers. If not go to step 7.
2228c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!neededInt && !neededSSE)
2229c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2230c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2231c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2232c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // general purpose registers needed to pass type and num_fp to hold
2233c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the number of floating point registers needed.
2234c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2235c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2236c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2237c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset > 304 - num_fp * 16 go to step 7.
2238c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2239c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2240c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // register save space).
2241c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2242c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *InRegs = 0;
2243c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2244c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2245c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
2246c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2247c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
22481090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
22491090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
2250c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2251c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2252c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
2253c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2254c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2255c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FitsInFP =
22561090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
22571090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
2258c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2259c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2260c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2261c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2262c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2263c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2264c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2265c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2266c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in registers.
2267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InRegBlock);
2269c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2270c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2271c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an offset of l->gp_offset and/or l->fp_offset. This may require
2272c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // copying to a temporary location in case the parameter is passed
2273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in different register classes or requires an alignment greater
2274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // than 8 for general purpose registers and 16 for XMM registers.
2275c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2276c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This really results in shameful code when we end up needing to
2277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // collect arguments from different places; often what should result in a
2278c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // simple assembling of a structure from scattered addresses has many more
2279c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // loads than necessary. Can we clean this up?
22802acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *RegAddr =
2282c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2283c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                           "reg_save_area");
2284c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt && neededSSE) {
2285c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: Cleanup.
2286800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
22872acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2288c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2289c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
22902acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyLo = ST->getElementType(0);
22912acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyHi = ST->getElementType(1);
2292a8b7a7d3eaa51dd200cba1e5541f2542d24d7a6eChris Lattner    assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
2293c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov           "Unexpected ABI info for mixed regs");
22942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
22952acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
2296c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2297c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2298f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2299f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
2300c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *V =
2301c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2302c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2303c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2304c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2305c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2306a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson    RegAddr = CGF.Builder.CreateBitCast(Tmp,
230796e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2308c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else if (neededInt) {
2309c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2310c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
231196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2312dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner  } else if (neededSSE == 1) {
2313dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2314dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2315dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2316c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else {
2317dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    assert(neededSSE == 2 && "Invalid number of needed registers!");
2318dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // SSE registers are spaced 16 bytes apart in the register save
2319dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // area, we need to collect the two eightbytes together.
2320dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
23211090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
23228b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Type *DoubleTy = CGF.DoubleTy;
23232acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *DblPtrTy =
2324dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner      llvm::PointerType::getUnqual(DoubleTy);
23252acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = llvm::StructType::get(DoubleTy,
2326dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                       DoubleTy, NULL);
2327dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2328dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2329dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2330dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2331dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2332dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2333dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2334dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(Tmp,
2335dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2337c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2338c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 5. Set:
2339c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->gp_offset = l->gp_offset + num_gp * 8
2340c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset = l->fp_offset + num_fp * 16.
2341c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
234277b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
2343c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2344c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            gp_offset_p);
2345c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2346c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
234777b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
2348c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2349c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            fp_offset_p);
2350c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2351c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBranch(ContBlock);
2352c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2353c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in memory.
2354c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2355c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InMemBlock);
2356c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2357c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2358c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Return the appropriate result.
2359c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2360c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(ContBlock);
2361bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
2362c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                 "vaarg.addr");
2363c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(RegAddr, InRegBlock);
2364c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(MemAddr, InMemBlock);
2365c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return ResAddr;
2366c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2367c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2368a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA TakumiABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2369a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2370a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isVoidType())
2371a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIgnore();
2372a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2373a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2374a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    Ty = EnumTy->getDecl()->getIntegerType();
2375a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2376a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  uint64_t Size = getContext().getTypeSize(Ty);
2377a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2378a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const RecordType *RT = Ty->getAs<RecordType>()) {
2379ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi    if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2380ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi        RT->getDecl()->hasFlexibleArrayMember())
2381a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2382a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
23836f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // FIXME: mingw-w64-gcc emits 128-bit struct as i128
23846f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    if (Size == 128 &&
238555fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman        getContext().getTargetInfo().getTriple().getOS()
238655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman          == llvm::Triple::MinGW32)
23876f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
23886f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi                                                          Size));
23896f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi
23906f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
23916f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // not 1, 2, 4, or 8 bytes, must be passed by reference."
23926f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    if (Size <= 64 &&
2393ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi        (Size & (Size - 1)) == 0)
2394a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2395a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi                                                          Size));
2396a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2397a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2398a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  }
2399a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2400a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isPromotableIntegerType())
2401a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getExtend();
2402a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2403a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  return ABIArgInfo::getDirect();
2404a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2405a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2406a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumivoid WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2407a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2408a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  QualType RetTy = FI.getReturnType();
2409a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  FI.getReturnInfo() = classify(RetTy);
2410a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2411a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2412a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi       it != ie; ++it)
2413a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    it->info = classify(it->type);
2414a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2415a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2416f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerllvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2417f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                      CodeGenFunction &CGF) const {
24188b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2419f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2420f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  CGBuilderTy &Builder = CGF.Builder;
2421f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2422f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                                       "ap");
2423f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2424f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Type *PTy =
2425f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2426f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2427f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2428f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  uint64_t Offset =
2429f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2430f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *NextAddr =
2431f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2432f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                      "ap.next");
2433f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2434dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2435f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  return AddrTyped;
2436f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner}
2437dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2438ec853ba1087f606e9685cb1e800616565ba35093John McCall// PowerPC-32
2439ec853ba1087f606e9685cb1e800616565ba35093John McCall
2440ec853ba1087f606e9685cb1e800616565ba35093John McCallnamespace {
2441ec853ba1087f606e9685cb1e800616565ba35093John McCallclass PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2442ec853ba1087f606e9685cb1e800616565ba35093John McCallpublic:
2443ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
24448bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2445ec853ba1087f606e9685cb1e800616565ba35093John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2446ec853ba1087f606e9685cb1e800616565ba35093John McCall    // This is recovered from gcc output.
2447ec853ba1087f606e9685cb1e800616565ba35093John McCall    return 1; // r1 is the dedicated stack pointer
2448ec853ba1087f606e9685cb1e800616565ba35093John McCall  }
2449ec853ba1087f606e9685cb1e800616565ba35093John McCall
2450ec853ba1087f606e9685cb1e800616565ba35093John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
24518bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
2452ec853ba1087f606e9685cb1e800616565ba35093John McCall};
2453ec853ba1087f606e9685cb1e800616565ba35093John McCall
2454ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2455ec853ba1087f606e9685cb1e800616565ba35093John McCall
2456ec853ba1087f606e9685cb1e800616565ba35093John McCallbool
2457ec853ba1087f606e9685cb1e800616565ba35093John McCallPPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2458ec853ba1087f606e9685cb1e800616565ba35093John McCall                                                llvm::Value *Address) const {
2459ec853ba1087f606e9685cb1e800616565ba35093John McCall  // This is calculated from the LLVM and GCC tables and verified
2460ec853ba1087f606e9685cb1e800616565ba35093John McCall  // against gcc output.  AFAIK all ABIs use the same encoding.
2461ec853ba1087f606e9685cb1e800616565ba35093John McCall
2462ec853ba1087f606e9685cb1e800616565ba35093John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
2463ec853ba1087f606e9685cb1e800616565ba35093John McCall
24648b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::IntegerType *i8 = CGF.Int8Ty;
2465ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2466ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2467ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2468ec853ba1087f606e9685cb1e800616565ba35093John McCall
2469ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 0-31: r0-31, the 4-byte general-purpose registers
2470aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 31);
2471ec853ba1087f606e9685cb1e800616565ba35093John McCall
2472ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 32-63: fp0-31, the 8-byte floating-point registers
2473aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2474ec853ba1087f606e9685cb1e800616565ba35093John McCall
2475ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64-76 are various 4-byte special-purpose registers:
2476ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64: mq
2477ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 65: lr
2478ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 66: ctr
2479ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 67: ap
2480ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 68-75 cr0-7
2481ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 76: xer
2482aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 64, 76);
2483ec853ba1087f606e9685cb1e800616565ba35093John McCall
2484ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 77-108: v0-31, the 16-byte vector registers
2485aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2486ec853ba1087f606e9685cb1e800616565ba35093John McCall
2487ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 109: vrsave
2488ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 110: vscr
2489ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 111: spe_acc
2490ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 112: spefscr
2491ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 113: sfp
2492aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 109, 113);
2493ec853ba1087f606e9685cb1e800616565ba35093John McCall
24948bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  return false;
2495ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2496ec853ba1087f606e9685cb1e800616565ba35093John McCall
24970fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky// PowerPC-64
24980fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
24990fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackynamespace {
25000fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackyclass PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
25010fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackypublic:
25020fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
25030fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25040fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
25050fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    // This is recovered from gcc output.
25060fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    return 1; // r1 is the dedicated stack pointer
25070fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  }
25080fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25090fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
25100fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky                               llvm::Value *Address) const;
25110fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky};
25120fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25130fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky}
25140fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25150fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackybool
25160fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman DivackyPPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
25170fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky                                                llvm::Value *Address) const {
25180fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // This is calculated from the LLVM and GCC tables and verified
25190fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // against gcc output.  AFAIK all ABIs use the same encoding.
25200fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25210fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  CodeGen::CGBuilderTy &Builder = CGF.Builder;
25220fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25230fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::IntegerType *i8 = CGF.Int8Ty;
25240fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
25250fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
25260fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
25270fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25280fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 0-31: r0-31, the 8-byte general-purpose registers
25290fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Eight8, 0, 31);
25300fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25310fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 32-63: fp0-31, the 8-byte floating-point registers
25320fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
25330fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25340fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 64-76 are various 4-byte special-purpose registers:
25350fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 64: mq
25360fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 65: lr
25370fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 66: ctr
25380fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 67: ap
25390fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 68-75 cr0-7
25400fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 76: xer
25410fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Four8, 64, 76);
25420fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25430fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 77-108: v0-31, the 16-byte vector registers
25440fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
25450fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25460fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 109: vrsave
25470fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 110: vscr
25480fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 111: spe_acc
25490fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 112: spefscr
25500fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 113: sfp
25510fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Four8, 109, 113);
25520fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25530fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  return false;
25540fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky}
2555ec853ba1087f606e9685cb1e800616565ba35093John McCall
2556dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
255734d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar// ARM ABI Implementation
2558dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
255934d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
256034d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbarnamespace {
256134d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
2562c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass ARMABIInfo : public ABIInfo {
25635e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
25645e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  enum ABIKind {
25655e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    APCS = 0,
25665e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS = 1,
25675e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS_VFP
25685e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  };
25695e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
25705e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
25715e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind Kind;
25725e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
25735e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
2574ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
25755e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
257649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  bool isEABI() const {
257755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    StringRef Env =
257855fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      getContext().getTargetInfo().getTriple().getEnvironmentName();
2579b43550bf1bd944a16cdae9703cb1c2049b04e6bdChandler Carruth    return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
258049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
258149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
25825e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
25835e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind getABIKind() const { return Kind; }
25845e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
2585a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
2586a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
2587c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2588ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
2589c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2590c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2591c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
2592c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
2593c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
259482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass ARMTargetCodeGenInfo : public TargetCodeGenInfo {
259582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
2596ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2597ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
25986374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
259949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  const ARMABIInfo &getABIInfo() const {
260049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
260149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
260249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
26036374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
26046374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 13;
26056374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
260609345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
26075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef getARCRetainAutoreleasedReturnValueMarker() const {
2608f85e193739c953358c865005855253af4f68a497John McCall    return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2609f85e193739c953358c865005855253af4f68a497John McCall  }
2610f85e193739c953358c865005855253af4f68a497John McCall
261109345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
261209345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky                               llvm::Value *Address) const {
26138b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
261409345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
261509345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    // 0-15 are the 16 integer registers.
26168b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
261709345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    return false;
261809345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  }
261949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
262049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
262149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    if (getABIInfo().isEABI()) return 88;
262249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return TargetCodeGenInfo::getSizeOfUnwindException();
262349e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
262482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
262582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
262634d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar}
262734d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
2628ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
2629a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2630c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2631a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner       it != ie; ++it)
2632a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    it->info = classifyArgumentType(it->type);
26335e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
2634414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // Always honor user-specified calling convention.
2635414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  if (FI.getCallingConvention() != llvm::CallingConv::C)
2636414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return;
2637414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov
2638414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // Calling convention as default by an ABI.
263925117ab35c1a033846073183314c68ef07d1701aRafael Espindola  llvm::CallingConv::ID DefaultCC;
264049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  if (isEABI())
264125117ab35c1a033846073183314c68ef07d1701aRafael Espindola    DefaultCC = llvm::CallingConv::ARM_AAPCS;
26421ed1a594e9befc91ebf00d81b41a2fdfab862657Rafael Espindola  else
26431ed1a594e9befc91ebf00d81b41a2fdfab862657Rafael Espindola    DefaultCC = llvm::CallingConv::ARM_APCS;
264425117ab35c1a033846073183314c68ef07d1701aRafael Espindola
2645414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // If user did not ask for specific calling convention explicitly (e.g. via
2646414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // pcs attribute), set effective calling convention if it's different than ABI
2647414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // default.
26485e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  switch (getABIKind()) {
26495e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case APCS:
265025117ab35c1a033846073183314c68ef07d1701aRafael Espindola    if (DefaultCC != llvm::CallingConv::ARM_APCS)
265125117ab35c1a033846073183314c68ef07d1701aRafael Espindola      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
26525e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
26535e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case AAPCS:
265425117ab35c1a033846073183314c68ef07d1701aRafael Espindola    if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
265525117ab35c1a033846073183314c68ef07d1701aRafael Espindola      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
26565e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
26575e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case AAPCS_VFP:
2658414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2659414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
26605e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
26615e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  }
2662c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2663c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2664194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2665194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// aggregate.  If HAMembers is non-null, the number of base elements
2666194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// contained in the type is returned through it; this is used for the
2667194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// recursive calls that check aggregate component types.
2668194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilsonstatic bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2669194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   ASTContext &Context,
2670194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   uint64_t *HAMembers = 0) {
2671eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  uint64_t Members = 0;
2672194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2673194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2674194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2675194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members *= AT->getSize().getZExtValue();
2676194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2677194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const RecordDecl *RD = RT->getDecl();
2678eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->hasFlexibleArrayMember())
2679194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2680eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2681194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 0;
2682194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2683194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         i != e; ++i) {
2684581deb3da481053c4993c7600f97acf7768caac5David Blaikie      const FieldDecl *FD = *i;
2685194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      uint64_t FldMembers;
2686194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2687194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2688eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2689eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      Members = (RD->isUnion() ?
2690eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov                 std::max(Members, FldMembers) : Members + FldMembers);
2691194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2692194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
2693194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 1;
2694194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2695194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Members = 2;
2696194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Ty = CT->getElementType();
2697194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2698194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2699194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2700194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // double, or 64-bit or 128-bit vectors.
2701194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2702194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (BT->getKind() != BuiltinType::Float &&
2703adfa45ffd67d1959cb1ff8cec88ad2ff3ffb7798Tim Northover          BT->getKind() != BuiltinType::Double &&
2704adfa45ffd67d1959cb1ff8cec88ad2ff3ffb7798Tim Northover          BT->getKind() != BuiltinType::LongDouble)
2705194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2706194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2707194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      unsigned VecSize = Context.getTypeSize(VT);
2708194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (VecSize != 64 && VecSize != 128)
2709194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2710194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else {
2711194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2712194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2713194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2714194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // The base type must be the same for all members.  Vector types of the
2715194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // same total size are treated as being equivalent here.
2716194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *TyPtr = Ty.getTypePtr();
2717194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!Base)
2718194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Base = TyPtr;
2719194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (Base != TyPtr &&
2720194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        (!Base->isVectorType() || !TyPtr->isVectorType() ||
2721194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2722194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2723194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
2724194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2725194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  // Homogeneous Aggregates can have at most 4 members of the base type.
2726194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (HAMembers)
2727194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    *HAMembers = Members;
2728eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2729eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  return (Members > 0 && Members <= 4);
2730194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson}
2731194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2732a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
2733d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
2734aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
2735aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2736aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
2737aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
2738cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
2739cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2740aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
274198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2742420255710694e958fa04bed1d80d96508949879eDaniel Dunbar  // Ignore empty records.
2743a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), Ty, true))
2744420255710694e958fa04bed1d80d96508949879eDaniel Dunbar    return ABIArgInfo::getIgnore();
2745420255710694e958fa04bed1d80d96508949879eDaniel Dunbar
27460eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // Structures with either a non-trivial destructor or a non-trivial
27470eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // copy constructor are always indirect.
27480eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
27490eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
27500eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
2751194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2752194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // Homogeneous Aggregates need to be expanded.
2753194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *Base = 0;
2754eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (isHomogeneousAggregate(Ty, Base, getContext())) {
2755eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      assert(Base && "Base class should be set for homogeneous aggregate");
2756194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return ABIArgInfo::getExpand();
2757eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    }
2758194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
2759194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2760634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren  // Support byval for ARM.
2761634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren  if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
2762634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren      getContext().getTypeAlign(Ty) > 64) {
2763634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren    return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
276479f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  }
276579f30981fcd25c6ff88807372a2744af02a7690eEli Friedman
27668aa87c71d9bfec14e135c683b0d7b9de999dbcb0Daniel Dunbar  // Otherwise, pass by coercing to a structure of the appropriate size.
27672acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type* ElemTy;
2768c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  unsigned SizeRegs;
276979f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  // FIXME: Try to match the types of the arguments more accurately where
277079f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  // we can.
277179f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  if (getContext().getTypeAlign(Ty) <= 32) {
277253fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    ElemTy = llvm::Type::getInt32Ty(getVMContext());
277353fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
277478eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren  } else {
277578eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren    ElemTy = llvm::Type::getInt64Ty(getVMContext());
277678eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren    SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
2777c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2778b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings
27799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *STy =
27807650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
2781b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings  return ABIArgInfo::getDirect(STy);
2782c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2783c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2784a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattnerstatic bool isIntegerLikeType(QualType Ty, ASTContext &Context,
278598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar                              llvm::LLVMContext &VMContext) {
278698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
278798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // is called integer-like if its size is less than or equal to one word, and
278898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // the offset of each of its addressable sub-fields is zero.
278998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
279098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  uint64_t Size = Context.getTypeSize(Ty);
279198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
279298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that the type fits in a word.
279398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Size > 32)
279498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
279598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
279698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // FIXME: Handle vector types!
279798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Ty->isVectorType())
279898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
279998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2800b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  // Float types are never treated as "integer like".
2801b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  if (Ty->isRealFloatingType())
2802b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar    return false;
2803b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar
280498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // If this is a builtin or pointer type then it is ok.
2805183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
280698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return true;
280798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
28084581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  // Small complex integer types are "integer like".
28094581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  if (const ComplexType *CT = Ty->getAs<ComplexType>())
28104581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar    return isIntegerLikeType(CT->getElementType(), Context, VMContext);
281198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
281298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Single element and zero sized arrays should be allowed, by the definition
281398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // above, but they are not.
281498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
281598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise, it must be a record type.
281698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
281798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (!RT) return false;
281898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
281998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Ignore records with flexible arrays.
282098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
282198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RD->hasFlexibleArrayMember())
282298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
282398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
282498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that all sub-fields are at offset 0, and are themselves "integer
282598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // like".
282698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
282798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
282898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  bool HadField = false;
282998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  unsigned idx = 0;
283098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
283198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar       i != e; ++i, ++idx) {
2832581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
283398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2834679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Bit-fields are not addressable, we only need to verify they are "integer
2835679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // like". We still have to disallow a subsequent non-bitfield, for example:
2836679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    //   struct { int : 0; int x }
2837679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // is non-integer like according to gcc.
2838679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (FD->isBitField()) {
2839679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!RD->isUnion())
2840679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        HadField = true;
2841679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
2842679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2843679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        return false;
284498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2845679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      continue;
284698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
284798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2848679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Check if this field is at offset 0.
2849679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (Layout.getFieldOffset(idx) != 0)
2850679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      return false;
2851679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
285298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!isIntegerLikeType(FD->getType(), Context, VMContext))
285398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return false;
28548bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2855679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Only allow at most one field in a structure. This doesn't match the
2856679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // wording above, but follows gcc in situations with a field following an
2857679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // empty structure.
285898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!RD->isUnion()) {
285998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (HadField)
286098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar        return false;
286198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
286298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      HadField = true;
286398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
286498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  }
286598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
286698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return true;
286798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar}
286898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2869a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
287098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RetTy->isVoidType())
2871c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
287298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2873f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  // Large vector types should be returned via memory.
2874f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2875f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar    return ABIArgInfo::getIndirect(0);
2876f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar
2877d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(RetTy)) {
2878aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
2879aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2880aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      RetTy = EnumTy->getDecl()->getIntegerType();
2881aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
2882cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (RetTy->isPromotableIntegerType() ?
2883cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2884aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
288598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
28860eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // Structures with either a non-trivial destructor or a non-trivial
28870eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // copy constructor are always indirect.
28880eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
28890eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
28900eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
289198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Are we following APCS?
289298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (getABIKind() == APCS) {
2893a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isEmptyRecord(getContext(), RetTy, false))
289498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return ABIArgInfo::getIgnore();
289598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
28964cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // Complex types are all returned as packed integers.
28974cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    //
28984cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // FIXME: Consider using 2 x vector types if the back end handles them
28994cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // correctly.
29004cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    if (RetTy->isAnyComplexType())
2901800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2902a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                              getContext().getTypeSize(RetTy)));
29034cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar
290498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Integer like structures are returned in r0.
2905a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
290698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      // Return in the smallest viable integer type.
2907a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
290898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 8)
2909800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
291098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 16)
2911800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2912800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
291398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
291498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
291598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Otherwise return in memory.
291698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return ABIArgInfo::getIndirect(0);
2917c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
291898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
291998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise this is an AAPCS variant.
292098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2921a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), RetTy, true))
292216a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    return ABIArgInfo::getIgnore();
292316a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
29243b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  // Check for homogeneous aggregates with AAPCS-VFP.
29253b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  if (getABIKind() == AAPCS_VFP) {
29263b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson    const Type *Base = 0;
2927eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (isHomogeneousAggregate(RetTy, Base, getContext())) {
2928eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      assert(Base && "Base class should be set for homogeneous aggregate");
29293b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      // Homogeneous Aggregates are returned directly.
29303b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      return ABIArgInfo::getDirect();
2931eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    }
29323b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  }
29333b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson
293498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Aggregates <= 4 bytes are returned in r0; other aggregates
293598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // are returned indirectly.
2936a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  uint64_t Size = getContext().getTypeSize(RetTy);
293716a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  if (Size <= 32) {
293816a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    // Return in the smallest viable integer type.
293916a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 8)
2940800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
294116a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 16)
2942800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2943800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
294416a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  }
294516a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
294698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return ABIArgInfo::getIndirect(0);
2947c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2948c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2949c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
295077b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                   CodeGenFunction &CGF) const {
29518b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
29528b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2953c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2954c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
29558b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2956c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2957e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  // Handle address alignment for type alignment > 32 bits
2958e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2959e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  if (TyAlign > 4) {
2960e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    assert((TyAlign & (TyAlign - 1)) == 0 &&
2961e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola           "Alignment is not power of 2!");
2962e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2963e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2964e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2965e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2966e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  }
2967c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Type *PTy =
296896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2969c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2970c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2971c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
2972c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2973c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
297477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2975c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
2976c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2977c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2978c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
2979c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2980c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2981dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
29822c585b991596859f39860b6094247ba027a03530Justin Holewinski// NVPTX ABI Implementation
29830259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
29840259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
29850259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskinamespace {
29860259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
29872c585b991596859f39860b6094247ba027a03530Justin Holewinskiclass NVPTXABIInfo : public ABIInfo {
29880259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
29892c585b991596859f39860b6094247ba027a03530Justin Holewinski  NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
29900259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
29910259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyReturnType(QualType RetTy) const;
29920259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyArgumentType(QualType Ty) const;
29930259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
29940259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual void computeInfo(CGFunctionInfo &FI) const;
29950259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
29960259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski                                 CodeGenFunction &CFG) const;
29970259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
29980259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
29992c585b991596859f39860b6094247ba027a03530Justin Holewinskiclass NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
30000259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
30012c585b991596859f39860b6094247ba027a03530Justin Holewinski  NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
30022c585b991596859f39860b6094247ba027a03530Justin Holewinski    : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
3003818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
30042f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
30052f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
30060259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
30070259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30082c585b991596859f39860b6094247ba027a03530Justin HolewinskiABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
30090259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (RetTy->isVoidType())
30100259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIgnore();
30110259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(RetTy))
30120259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
30130259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
30140259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
30150259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30162c585b991596859f39860b6094247ba027a03530Justin HolewinskiABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
30170259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(Ty))
30180259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
30190259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30200259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
30210259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
30220259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30232c585b991596859f39860b6094247ba027a03530Justin Holewinskivoid NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
30240259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
30250259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
30260259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski       it != ie; ++it)
30270259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    it->info = classifyArgumentType(it->type);
30280259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30290259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  // Always honor user-specified calling convention.
30300259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (FI.getCallingConvention() != llvm::CallingConv::C)
30310259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return;
30320259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30330259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  // Calling convention as default by an ABI.
30342c585b991596859f39860b6094247ba027a03530Justin Holewinski  // We're still using the PTX_Kernel/PTX_Device calling conventions here,
30352c585b991596859f39860b6094247ba027a03530Justin Holewinski  // but we should switch to NVVM metadata later on.
30360259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  llvm::CallingConv::ID DefaultCC;
30374e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = getContext().getLangOpts();
3038744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  if (LangOpts.OpenCL || LangOpts.CUDA) {
3039744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // If we are in OpenCL or CUDA mode, then default to device functions
30400259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    DefaultCC = llvm::CallingConv::PTX_Device;
3041818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  } else {
3042818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // If we are in standard C/C++ mode, use the triple to decide on the default
3043818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    StringRef Env =
3044818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      getContext().getTargetInfo().getTriple().getEnvironmentName();
3045818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    if (Env == "device")
3046818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      DefaultCC = llvm::CallingConv::PTX_Device;
3047818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    else
3048818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      DefaultCC = llvm::CallingConv::PTX_Kernel;
3049818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  }
30500259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  FI.setEffectiveCallingConvention(DefaultCC);
3051818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
30520259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
30530259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30542c585b991596859f39860b6094247ba027a03530Justin Holewinskillvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
30552c585b991596859f39860b6094247ba027a03530Justin Holewinski                                     CodeGenFunction &CFG) const {
30562c585b991596859f39860b6094247ba027a03530Justin Holewinski  llvm_unreachable("NVPTX does not support varargs");
30570259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
30580259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30592c585b991596859f39860b6094247ba027a03530Justin Holewinskivoid NVPTXTargetCodeGenInfo::
30602c585b991596859f39860b6094247ba027a03530Justin HolewinskiSetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
30612c585b991596859f39860b6094247ba027a03530Justin Holewinski                    CodeGen::CodeGenModule &M) const{
3062818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3063818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  if (!FD) return;
3064818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
3065818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  llvm::Function *F = cast<llvm::Function>(GV);
3066818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
3067818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  // Perform special handling in OpenCL mode
30684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().OpenCL) {
3069818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // Use OpenCL function attributes to set proper calling conventions
3070818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // By default, all functions are device functions
3071818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    if (FD->hasAttr<OpenCLKernelAttr>()) {
3072818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      // OpenCL __kernel functions get a kernel calling convention
3073744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne      F->setCallingConv(llvm::CallingConv::PTX_Kernel);
3074818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      // And kernel functions are not subject to inlining
3075818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      F->addFnAttr(llvm::Attribute::NoInline);
3076818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    }
3077744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  }
3078818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
3079744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  // Perform special handling in CUDA mode.
30804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().CUDA) {
3081744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // CUDA __global__ functions get a kernel calling convention.  Since
3082744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // __global__ functions cannot be called from the device, we do not
3083744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // need to set the noinline attribute.
3084744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    if (FD->getAttr<CUDAGlobalAttr>())
3085744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne      F->setCallingConv(llvm::CallingConv::PTX_Kernel);
3086818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  }
3087818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski}
3088818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
30890259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
30900259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30910259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
3092276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck// MBlaze ABI Implementation
3093276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
3094276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3095276fdf408050d205f3a7f34c1e788224a67d2098Wesley Pecknamespace {
3096276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3097276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeABIInfo : public ABIInfo {
3098276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
3099276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3100276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3101276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  bool isPromotableIntegerType(QualType Ty) const;
3102276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3103276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyReturnType(QualType RetTy) const;
3104276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyArgumentType(QualType RetTy) const;
3105276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3106276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual void computeInfo(CGFunctionInfo &FI) const {
3107276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3108276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3109276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck         it != ie; ++it)
3110276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      it->info = classifyArgumentType(it->type);
3111276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
3112276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3113276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3114276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                 CodeGenFunction &CGF) const;
3115276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
3116276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3117276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3118276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
3119276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3120276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3121276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3122276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                           CodeGen::CodeGenModule &M) const;
3123276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
3124276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3125276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3126276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3127276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckbool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3128276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3129276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3130276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    switch (BT->getKind()) {
3131276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Bool:
3132276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_S:
3133276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_U:
3134276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::SChar:
3135276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UChar:
3136276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Short:
3137276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UShort:
3138276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return true;
3139276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    default:
3140276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return false;
3141276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    }
3142276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return false;
3143276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3144276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3145276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckllvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3146276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                      CodeGenFunction &CGF) const {
3147276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // FIXME: Implement
3148276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return 0;
3149276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3150276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3151276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3152276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3153276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (RetTy->isVoidType())
3154276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIgnore();
3155276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(RetTy))
3156276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
3157276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3158276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(RetTy) ?
3159276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3160276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3161276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3162276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3163276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(Ty))
3164276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
3165276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3166276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(Ty) ?
3167276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3168276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3169276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3170276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckvoid MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3171276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  llvm::GlobalValue *GV,
3172276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  CodeGen::CodeGenModule &M)
3173276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  const {
3174276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3175276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (!FD) return;
3176125b4cb35536e45201f8f2cb19ee620e3ad67c49NAKAMURA Takumi
3177276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  llvm::CallingConv::ID CC = llvm::CallingConv::C;
3178276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3179276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_INTR;
3180276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3181276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_SVOL;
3182276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3183276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC != llvm::CallingConv::C) {
3184276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Handle 'interrupt_handler' attribute:
3185276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      llvm::Function *F = cast<llvm::Function>(GV);
3186276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3187276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 1: Set ISR calling convention.
3188276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      F->setCallingConv(CC);
3189276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3190276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 2: Add attributes goodness.
3191276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      F->addFnAttr(llvm::Attribute::NoInline);
3192276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
3193276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3194276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // Step 3: Emit _interrupt_handler alias.
3195276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC == llvm::CallingConv::MBLAZE_INTR)
3196276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3197276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                          "_interrupt_handler", GV, &M.getModule());
3198276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3199276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3200276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3201276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
320282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov// MSP430 ABI Implementation
3203dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
320482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
320582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovnamespace {
320682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
320782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
320882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
3209ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3210ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
321182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
321282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                           CodeGen::CodeGenModule &M) const;
321382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
321482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
3215c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3216c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
321782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovvoid MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
321882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                                  llvm::GlobalValue *GV,
321982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                             CodeGen::CodeGenModule &M) const {
322082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
322182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
322282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Handle 'interrupt' attribute:
322382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      llvm::Function *F = cast<llvm::Function>(GV);
322482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
322582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 1: Set ISR calling convention.
322682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      F->setCallingConv(llvm::CallingConv::MSP430_INTR);
322782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
322882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 2: Add attributes goodness.
322982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      F->addFnAttr(llvm::Attribute::NoInline);
323082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
323182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 3: Emit ISR vector alias.
323282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      unsigned Num = attr->getNumber() + 0xffe0;
323382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
32345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            "vector_" + Twine::utohexstr(Num),
323582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                            GV, &M.getModule());
323682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    }
323782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  }
3238c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3239c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3240dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
3241aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// MIPS ABI Implementation.  This works for both little-endian and
3242aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// big-endian variants.
3243dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
3244dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
3245aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallnamespace {
3246619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakaclass MipsABIInfo : public ABIInfo {
3247c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  bool IsO32;
3248c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3249c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  void CoerceToIntArgs(uint64_t TySize,
3250c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                       SmallVector<llvm::Type*, 8> &ArgList) const;
325191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
3252c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
3253a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
3254619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakapublic:
3255b551dd31f6b15aa959127ee906084fcf5bf0154eAkira Hatanaka  MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3256c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3257c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    StackAlignInBytes(IsO32 ? 8 : 16) {}
3258619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3259619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  ABIArgInfo classifyReturnType(QualType RetTy) const;
3260f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka  ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
3261619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual void computeInfo(CGFunctionInfo &FI) const;
3262619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3263619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                 CodeGenFunction &CGF) const;
3264619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka};
3265619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3266aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallclass MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
3267e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka  unsigned SizeOfUnwindException;
3268aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallpublic:
3269c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3270c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3271c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka      SizeOfUnwindException(IsO32 ? 24 : 32) {}
3272aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3273aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3274aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    return 29;
3275aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
3276aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3277aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
32788bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
327949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
328049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
3281e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka    return SizeOfUnwindException;
328249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
3283aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall};
3284aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
3285aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3286c359f2029d19016560a422551704ccc2419be0b1Akira Hatanakavoid MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3287c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                                  SmallVector<llvm::Type*, 8> &ArgList) const {
3288c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  llvm::IntegerType *IntTy =
3289c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
329091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
329191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
329291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
329391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    ArgList.push_back(IntTy);
329491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
329591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  // If necessary, add one more integer type to ArgList.
329691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  unsigned R = TySize % (MinABIStackAlignInBytes * 8);
329791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
329891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  if (R)
329991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
330091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka}
330191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
3302d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// In N32/64, an aligned double precision floating point field is passed in
3303d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// a register.
330491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanakallvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
3305c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3306c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka
3307c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (IsO32) {
3308c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    CoerceToIntArgs(TySize, ArgList);
3309c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    return llvm::StructType::get(getVMContext(), ArgList);
3310c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  }
3311d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
33122afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka  if (Ty->isComplexType())
33132afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka    return CGT.ConvertType(Ty);
33146d1080fd1851f18bd40bb46fa074aa1252b13e8eAkira Hatanaka
3315a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  const RecordType *RT = Ty->getAs<RecordType>();
3316d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3317c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  // Unions/vectors are passed in integer registers.
3318c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (!RT || !RT->isStructureOrClassType()) {
3319c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    CoerceToIntArgs(TySize, ArgList);
3320c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    return llvm::StructType::get(getVMContext(), ArgList);
3321c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  }
3322d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3323d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const RecordDecl *RD = RT->getDecl();
3324d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
332591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
3326d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3327d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  uint64_t LastOffset = 0;
3328d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  unsigned idx = 0;
3329d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3330d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3331a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // Iterate over fields in the struct/class and check if there are any aligned
3332a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // double fields.
3333d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3334d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka       i != e; ++i, ++idx) {
3335262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    const QualType Ty = i->getType();
3336d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    const BuiltinType *BT = Ty->getAs<BuiltinType>();
3337d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3338d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (!BT || BT->getKind() != BuiltinType::Double)
3339d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
3340d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3341d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    uint64_t Offset = Layout.getFieldOffset(idx);
3342d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (Offset % 64) // Ignore doubles that are not aligned.
3343d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
3344d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3345d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add ((Offset - LastOffset) / 64) args of type i64.
3346d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3347d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      ArgList.push_back(I64);
3348d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3349d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add double type.
3350d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3351d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    LastOffset = Offset + 64;
3352d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  }
3353d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3354c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  CoerceToIntArgs(TySize - LastOffset, IntArgList);
3355c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  ArgList.append(IntArgList.begin(), IntArgList.end());
3356d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3357d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  return llvm::StructType::get(getVMContext(), ArgList);
3358d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka}
3359d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3360a33fd393d5255716e904fed021f87260095ed00aAkira Hatanakallvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
336191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  assert((Offset % MinABIStackAlignInBytes) == 0);
336291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
336391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  if ((Align - 1) & Offset)
336491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3365a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
336691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  return 0;
3367a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka}
33689659d59ec368933050684af573b6d32ab5714332Akira Hatanaka
3369f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaABIArgInfo
3370f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaMipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
3371a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t OrigOffset = Offset;
337291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  uint64_t TySize = getContext().getTypeSize(Ty);
3373a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t Align = getContext().getTypeAlign(Ty) / 8;
337491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
3375c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3376c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                   (uint64_t)StackAlignInBytes);
337791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  Offset = llvm::RoundUpToAlignment(Offset, Align);
337891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
3379a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3380c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
3381619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    // Ignore empty aggregates.
3382f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    if (TySize == 0)
3383619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka      return ABIArgInfo::getIgnore();
3384619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3385511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka    // Records with non trivial destructors/constructors should not be passed
3386511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka    // by value.
3387f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
338891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka      Offset = OrigOffset + MinABIStackAlignInBytes;
3389511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3390f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    }
3391511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka
339291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // If we have reached here, aggregates are passed directly by coercing to
339391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // another structure type. Padding is inserted if the offset of the
339491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // aggregate is unaligned.
339591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
339691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka                                 getPaddingType(Align, OrigOffset));
3397619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
3398619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3399619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
3400619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3401619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    Ty = EnumTy->getDecl()->getIntegerType();
3402619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3403a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  if (Ty->isPromotableIntegerType())
3404a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    return ABIArgInfo::getExtend();
3405a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3406a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
3407619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3408619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3409c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanakallvm::Type*
3410c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira HatanakaMipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
3411da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  const RecordType *RT = RetTy->getAs<RecordType>();
3412c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  SmallVector<llvm::Type*, 8> RTList;
3413c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3414da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  if (RT && RT->isStructureOrClassType()) {
3415c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    const RecordDecl *RD = RT->getDecl();
3416da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3417da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    unsigned FieldCnt = Layout.getFieldCount();
3418da54ff306270e179f64d046369419724356d30d7Akira Hatanaka
3419da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // N32/64 returns struct/classes in floating point registers if the
3420da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // following conditions are met:
3421da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 1. The size of the struct/class is no larger than 128-bit.
3422da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 2. The struct/class has one or two fields all of which are floating
3423da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //    point types.
3424da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 3. The offset of the first field is zero (this follows what gcc does).
3425da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
3426da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // Any other composite results are returned in integer registers.
3427da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
3428da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3429da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3430da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      for (; b != e; ++b) {
3431262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
3432c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3433da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        if (!BT || !BT->isFloatingPoint())
3434da54ff306270e179f64d046369419724356d30d7Akira Hatanaka          break;
3435c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3436262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        RTList.push_back(CGT.ConvertType(b->getType()));
3437da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      }
3438c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3439da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      if (b == e)
3440da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        return llvm::StructType::get(getVMContext(), RTList,
3441da54ff306270e179f64d046369419724356d30d7Akira Hatanaka                                     RD->hasAttr<PackedAttr>());
3442c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3443da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RTList.clear();
3444da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    }
3445c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  }
3446c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3447c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  CoerceToIntArgs(Size, RTList);
3448c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  return llvm::StructType::get(getVMContext(), RTList);
3449c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka}
3450c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3451619e8875d29cc019c7360595f66b9f91b3439494Akira HatanakaABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3452a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  uint64_t Size = getContext().getTypeSize(RetTy);
3453a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka
3454a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  if (RetTy->isVoidType() || Size == 0)
3455619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIgnore();
3456619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
34578aeb1471ef62a4befba00721925a3717914f21d8Akira Hatanaka  if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
3458c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    if (Size <= 128) {
3459c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka      if (RetTy->isAnyComplexType())
3460c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect();
3461c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3462c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka      // O32 returns integer vectors in registers.
3463c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka      if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3464c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3465c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka
3466526cdfb2bf51c8c6612504f1d06c02c736d3d126Akira Hatanaka      if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3467c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3468c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    }
3469619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3470619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIndirect(0);
3471619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
3472619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3473619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
3474619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3475619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    RetTy = EnumTy->getDecl()->getIntegerType();
3476619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3477619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  return (RetTy->isPromotableIntegerType() ?
3478619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3479619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3480619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3481619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakavoid MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3482cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  ABIArgInfo &RetInfo = FI.getReturnInfo();
3483cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  RetInfo = classifyReturnType(FI.getReturnType());
3484cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
3485cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  // Check if a pointer to an aggregate is passed as a hidden argument.
348691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
3487cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
3488619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3489619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka       it != ie; ++it)
3490f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    it->info = classifyArgumentType(it->type, Offset);
3491619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3492619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3493619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakallvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3494619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                    CodeGenFunction &CGF) const {
34958b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
34968b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
3497c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3498c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  CGBuilderTy &Builder = CGF.Builder;
3499c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3500c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
35018f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
3502c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3503c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AddrTyped;
35048f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
35058f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
3506c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3507c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  if (TypeAlign > MinABIStackAlignInBytes) {
35088f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
35098f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
35108f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
35118f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
3512c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3513c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3514c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  }
3515c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  else
3516c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = Builder.CreateBitCast(Addr, PTy);
3517c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3518c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
35198f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
3520c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  uint64_t Offset =
3521c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3522c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *NextAddr =
35238f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
3524c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka                      "ap.next");
3525c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3526c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3527c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  return AddrTyped;
3528619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3529619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3530aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallbool
3531aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallMIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3532aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                                               llvm::Value *Address) const {
3533aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // This information comes from gcc's implementation, which seems to
3534aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // as canonical as it gets.
3535aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3536aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // Everything on MIPS is 4 bytes.  Double-precision FP registers
3537aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // are aliased to pairs of single-precision FP registers.
35388b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
3539aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3540aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 0-31 are the general purpose registers, $0 - $31.
3541aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 32-63 are the floating-point registers, $f0 - $f31.
3542aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 64 and 65 are the multiply/divide registers, $hi and $lo.
3543aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 66 is the (notional, I think) register for signal-handler return.
35448b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
3545aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3546aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3547aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // They are one bit wide and ignored here.
3548aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3549aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3550aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // (coprocessor 1 is the FP unit)
3551aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3552aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3553aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 176-181 are the DSP accumulator registers.
35548b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
3555aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  return false;
3556aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
3557aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
35582f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
35592f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
35602f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// Currently subclassed only to implement custom OpenCL C function attribute
35612f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// handling.
35622f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
35632f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35642f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournenamespace {
35652f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35662f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourneclass TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
35672f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournepublic:
35682f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  TCETargetCodeGenInfo(CodeGenTypes &CGT)
35692f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    : DefaultTargetCodeGenInfo(CGT) {}
35702f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35712f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
35722f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
35732f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne};
35742f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35752f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournevoid TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
35762f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               llvm::GlobalValue *GV,
35772f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               CodeGen::CodeGenModule &M) const {
35782f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
35792f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  if (!FD) return;
35802f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35812f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  llvm::Function *F = cast<llvm::Function>(GV);
35822f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35834e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().OpenCL) {
35842f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    if (FD->hasAttr<OpenCLKernelAttr>()) {
35852f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      // OpenCL C Kernel functions are not subject to inlining
35862f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      F->addFnAttr(llvm::Attribute::NoInline);
35872f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35882f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
35892f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35902f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Convert the reqd_work_group_size() attributes to metadata.
35912f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::LLVMContext &Context = F->getContext();
35922f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::NamedMDNode *OpenCLMetadata =
35932f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne            M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
35942f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35952f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        SmallVector<llvm::Value*, 5> Operands;
35962f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        Operands.push_back(F);
35972f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
35988b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
35998b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
36008b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
36018b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
36028b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
36032f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
36048b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
36058b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
36062f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
36072f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36082f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Add a boolean constant operand for "required" (true) or "hint" (false)
36092f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // for implementing the work_group_size_hint attr later. Currently
36102f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // always true as the hint is not yet implemented.
36118b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::ConstantInt::getTrue(Context));
36122f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
36132f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      }
36142f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    }
36152f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  }
36162f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
36172f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36182f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
3619aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
36209631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
36219631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum// Hexagon ABI Implementation
36229631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
36239631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36249631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumnamespace {
36259631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36269631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonABIInfo : public ABIInfo {
36279631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36289631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36299631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
36309631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
36319631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36329631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumprivate:
36339631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36349631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyReturnType(QualType RetTy) const;
36359631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyArgumentType(QualType RetTy) const;
36369631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36379631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual void computeInfo(CGFunctionInfo &FI) const;
36389631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36399631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
36409631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                 CodeGenFunction &CGF) const;
36419631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
36429631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36439631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
36449631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
36459631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
36469631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
36479631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36489631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
36499631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return 29;
36509631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
36519631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
36529631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36539631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
36549631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36559631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumvoid HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
36569631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
36579631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
36589631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum       it != ie; ++it)
36599631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    it->info = classifyArgumentType(it->type);
36609631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
36619631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36629631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
36639631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(Ty)) {
36649631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
36659631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
36669631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      Ty = EnumTy->getDecl()->getIntegerType();
36679631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36689631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (Ty->isPromotableIntegerType() ?
36699631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
36709631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
36719631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36729631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Ignore empty records.
36739631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), Ty, true))
36749631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
36759631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36769631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Structures with either a non-trivial destructor or a non-trivial
36779631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // copy constructor are always indirect.
36789631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
36799631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
36809631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36819631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(Ty);
36829631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size > 64)
36839631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
36849631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Pass in the smallest viable integer type.
36859631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 32)
36869631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
36879631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 16)
36889631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
36899631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 8)
36909631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
36919631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else
36929631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
36939631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
36949631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36959631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
36969631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVoidType())
36979631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
36989631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36999631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Large vector types should be returned via memory.
37009631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
37019631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0);
37029631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37039631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(RetTy)) {
37049631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
37059631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
37069631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      RetTy = EnumTy->getDecl()->getIntegerType();
37079631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37089631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (RetTy->isPromotableIntegerType() ?
37099631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
37109631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
37119631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37129631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Structures with either a non-trivial destructor or a non-trivial
37139631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // copy constructor are always indirect.
37149631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
37159631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
37169631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37179631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), RetTy, true))
37189631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
37199631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37209631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Aggregates <= 8 bytes are returned in r0; other aggregates
37219631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // are returned indirectly.
37229631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(RetTy);
37239631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size <= 64) {
37249631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Return in the smallest viable integer type.
37259631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 8)
37269631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
37279631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 16)
37289631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
37299631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 32)
37309631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
37319631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
37329631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
37339631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37349631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
37359631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
37369631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37379631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumllvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
37388b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                                       CodeGenFunction &CGF) const {
37399631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // FIXME: Need to handle alignment
37408b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
37419631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37429631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  CGBuilderTy &Builder = CGF.Builder;
37439631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
37449631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                                       "ap");
37459631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
37469631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Type *PTy =
37479631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
37489631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
37499631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37509631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Offset =
37519631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
37529631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *NextAddr =
37539631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
37549631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                      "ap.next");
37559631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
37569631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37579631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return AddrTyped;
37589631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
37599631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37609631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
3761ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerconst TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
376282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (TheTargetCodeGenInfo)
376382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return *TheTargetCodeGenInfo;
3764c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3765bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
37661752ee4849f4c37f5e03193e658be92650b0e65aDaniel Dunbar  switch (Triple.getArch()) {
37672c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar  default:
3768ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
37692c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
3770aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mips:
3771aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mipsel:
3772c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
3773aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
37748c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64:
37758c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64el:
3776c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
37778c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka
377834d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::arm:
377934d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::thumb:
378034c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    {
378134c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
37825e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
3783bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
378434c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::APCS;
378534c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      else if (CodeGenOpts.FloatABI == "hard")
378634c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::AAPCS_VFP;
378734c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel
378834c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
378934c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    }
379034d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
3791ec853ba1087f606e9685cb1e800616565ba35093John McCall  case llvm::Triple::ppc:
3792ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
37930fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  case llvm::Triple::ppc64:
37940fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
3795ec853ba1087f606e9685cb1e800616565ba35093John McCall
3796edb66f38dbdc501342aa1f17c8a15a34ed73584dPeter Collingbourne  case llvm::Triple::nvptx:
3797edb66f38dbdc501342aa1f17c8a15a34ed73584dPeter Collingbourne  case llvm::Triple::nvptx64:
37982c585b991596859f39860b6094247ba027a03530Justin Holewinski    return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
37990259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
3800276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  case llvm::Triple::mblaze:
3801276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3802276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
380382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  case llvm::Triple::msp430:
3804ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
380534d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
38062f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  case llvm::Triple::tce:
38072f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
38082f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
3809c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  case llvm::Triple::x86: {
3810bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
3811c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman
3812db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    if (Triple.isOSDarwin())
381382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
3814b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
3815b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
3816db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar
3817db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    switch (Triple.getOS()) {
38182c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::Cygwin:
38192c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::MinGW32:
3820727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::AuroraUX:
3821727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::DragonFly:
382275c135a511c855d94bbfa7f00dd27a165f61e953David Chisnall    case llvm::Triple::FreeBSD:
38232c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::OpenBSD:
382442f74f21ece01dc8573d5377859d327fbb23b26cEli Friedman    case llvm::Triple::Bitrig:
382582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
3826b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
3827b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           false,
3828b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
382955fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman
383055fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    case llvm::Triple::Win32:
383155fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      return *(TheTargetCodeGenInfo =
3832b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
3833b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
38342c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
38352c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    default:
383682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
3837b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
3838b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           false,
3839b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
3840c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
3841c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  }
38422c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
3843ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  case llvm::Triple::x86_64: {
3844ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3845ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
3846f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    switch (Triple.getOS()) {
3847f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Win32:
38480aa205765aec0aa5eed672f8e3cade543372edcdNAKAMURA Takumi    case llvm::Triple::MinGW32:
3849f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Cygwin:
3850f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner      return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3851f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    default:
3852ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3853ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman                                                                  HasAVX));
3854f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    }
3855c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
38569631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  case llvm::Triple::hexagon:
38579631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
3858ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
3859c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3860