TargetInfo.cpp revision edfac0302490d84419eb958c812c533b8df29785
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)
16498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    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) {
232c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    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) {
304c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    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 {
416fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  static const unsigned MinABIStackAlignInBytes = 4;
417fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
4181e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsDarwinVectorABI;
4191e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsSmallStructInRegABI;
420c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  bool IsMMXDisabled;
42155fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  bool IsWin32FloatStructABI;
422c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
423c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  static bool isRegisterSize(unsigned Size) {
424c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
425c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
426c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
4276c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
4286c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                          unsigned callingConvention);
429c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
430dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
431dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// such that the argument will be passed in memory.
432a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
433dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
434fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  /// \brief Return the alignment to use for the given type on the stack.
435e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
436fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
437c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovpublic:
438c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
4396c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  ABIArgInfo classifyReturnType(QualType RetTy,
4406c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                unsigned callingConvention) const;
441a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
442c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
443ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const {
4446c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
4456c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                            FI.getCallingConvention());
446c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
447c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         it != ie; ++it)
448a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      it->info = classifyArgumentType(it->type);
449c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
450c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
451c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
452c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
453c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
45455fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w)
455c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman    : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
45655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      IsMMXDisabled(m), IsWin32FloatStructABI(w) {}
457c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
458c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
45982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
46082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
46155fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
46255fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      bool d, bool p, bool m, bool w)
46355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w)) {}
46474f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
46574f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
46674f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                           CodeGen::CodeGenModule &CGM) const;
4676374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
4686374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
4696374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // Darwin uses different dwarf register numbers for EH.
4706374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    if (CGM.isTargetDarwin()) return 5;
4716374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
4726374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 4;
4736374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
4746374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
4756374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
4766374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const;
4774b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
478ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
4795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
480ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
4814b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
4824b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
4834b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
48482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
48582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
48682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
487c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
488c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// shouldReturnTypeInRegister - Determine if the given type should be
489c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// passed in a register (for the Darwin ABI).
490c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovbool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
4916c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               ASTContext &Context,
4926c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               unsigned callingConvention) {
493c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Size = Context.getTypeSize(Ty);
494c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
495c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Type must be register sized.
496c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!isRegisterSize(Size))
497c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
498c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
499c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Ty->isVectorType()) {
500c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // 64- and 128- bit vectors inside structures are not returned in
501c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // registers.
502c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 64 || Size == 128)
503c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
504c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
505c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
506c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
507c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5087711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // If this is a builtin, pointer, enum, complex type, member pointer, or
5097711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // member function pointer it is ok.
510a1842d32a1964712e42078e9b389dce9258c6a8cDaniel Dunbar  if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
51155e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      Ty->isAnyComplexType() || Ty->isEnumeralType() ||
5127711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar      Ty->isBlockPointerType() || Ty->isMemberPointerType())
513c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
514c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
515c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Arrays are treated like records.
516c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
5176c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return shouldReturnTypeInRegister(AT->getElementType(), Context,
5186c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                      callingConvention);
519c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
520c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Otherwise, it must be a record type.
5216217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *RT = Ty->getAs<RecordType>();
522c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT) return false;
523c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
524a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson  // FIXME: Traverse bases here too.
525a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson
5266c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // For thiscall conventions, structures will never be returned in
5276c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // a register.  This is for compatibility with the MSVC ABI
5286c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  if (callingConvention == llvm::CallingConv::X86_ThisCall &&
5296c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman      RT->isStructureType()) {
5306c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return false;
5316c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  }
5326c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman
533c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Structure types are passed in register if all fields would be
534c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // passed in a register.
53517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
53617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         e = RT->getDecl()->field_end(); i != e; ++i) {
537c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    const FieldDecl *FD = *i;
538c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
539c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Empty fields are ignored.
54098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (isEmptyField(Context, FD, true))
541c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      continue;
542c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
543c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Check fields recursively.
5446c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (!shouldReturnTypeInRegister(FD->getType(), Context,
5456c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                    callingConvention))
546c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
547c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
548c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
549c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
550c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5516c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron BallmanABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
5526c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                            unsigned callingConvention) const {
553a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (RetTy->isVoidType())
554c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
5558bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
556a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const VectorType *VT = RetTy->getAs<VectorType>()) {
557c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // On Darwin, some vectors are returned in registers.
5581e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (IsDarwinVectorABI) {
559a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
560c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
561c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // 128-bit vectors are a special case; they are returned in
562c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // registers and we need to make sure to pick a type the LLVM
563c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // backend will like.
564c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size == 128)
565800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::VectorType::get(
566a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                  llvm::Type::getInt64Ty(getVMContext()), 2));
567c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
568c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Always return in register if it fits in a general purpose
569c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // register, or if it is 64 bits and has a single element.
570c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if ((Size == 8 || Size == 16 || Size == 32) ||
571c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          (Size == 64 && VT->getNumElements() == 1))
572800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
573a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                                            Size));
574c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
575c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
576c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
577c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
578c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getDirect();
579a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  }
5808bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
581d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(RetTy)) {
582a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = RetTy->getAs<RecordType>()) {
58340092972b591646b47037d2b46b695a4014df413Anders Carlsson      // Structures with either a non-trivial destructor or a non-trivial
58440092972b591646b47037d2b46b695a4014df413Anders Carlsson      // copy constructor are always indirect.
58540092972b591646b47037d2b46b695a4014df413Anders Carlsson      if (hasNonTrivialDestructorOrCopyConstructor(RT))
58640092972b591646b47037d2b46b695a4014df413Anders Carlsson        return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
5878bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
58840092972b591646b47037d2b46b695a4014df413Anders Carlsson      // Structures with flexible arrays are always indirect.
589c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
590c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return ABIArgInfo::getIndirect(0);
59140092972b591646b47037d2b46b695a4014df413Anders Carlsson    }
5928bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
5931e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    // If specified, structs and unions are always indirect.
5941e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
595c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
596c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
597c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Small structures which are register sized are generally returned
598c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // in a register.
5996c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
6006c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                                  callingConvention)) {
601a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
602bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
603bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // As a special-case, if the struct is a "single-element" struct, and
604bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // the field is of type "float" or "double", return it in a
60555fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // floating-point register. (MSVC does not apply this special case.)
60655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // We apply a similar transformation for pointer types to improve the
60755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // quality of the generated IR.
608bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
60955fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman        if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
61055fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman            || SeltTy->hasPointerRepresentation())
611bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman          return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
612bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
613bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // FIXME: We should be able to narrow this integer in cases with dead
614bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // padding.
615800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
616c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
617c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
618c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIndirect(0);
619c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
6208bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
621a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  // Treat an enum type as its underlying type.
622a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
623a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    RetTy = EnumTy->getDecl()->getIntegerType();
624a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner
625a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  return (RetTy->isPromotableIntegerType() ?
626a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
627c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
628c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
62993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbarstatic bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
63093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
63193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (!RT)
63293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 0;
63393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
63493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
63593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // If this is a C++ record, check the bases first.
63693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
63793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
63893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar           e = CXXRD->bases_end(); i != e; ++i)
63993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      if (!isRecordWithSSEVectorType(Context, i->getType()))
64093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar        return false;
64193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
64293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
64393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar       i != e; ++i) {
64493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    QualType FT = i->getType();
64593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
6467b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    if (FT->getAs<VectorType>() && Context.getTypeSize(FT) == 128)
64793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
64893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
64993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    if (isRecordWithSSEVectorType(Context, FT))
65093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
65193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  }
65293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
65393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return false;
65493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar}
65593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
656e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbarunsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
657e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                                 unsigned Align) const {
658e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Otherwise, if the alignment is less than or equal to the minimum ABI
659e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // alignment, just use the default; the backend will handle this.
660fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  if (Align <= MinABIStackAlignInBytes)
661e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return 0; // Use default alignment.
662e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
663e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // On non-Darwin, the stack type alignment is always 4.
664e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (!IsDarwinVectorABI) {
665e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    // Set explicit alignment, since we may need to realign the top.
666fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar    return MinABIStackAlignInBytes;
667e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  }
668fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
66993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // Otherwise, if the type contains an SSE vector type, the alignment is 16.
6707b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  if (Align >= 16 && isRecordWithSSEVectorType(getContext(), Ty))
67193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 16;
67293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
67393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return MinABIStackAlignInBytes;
674fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar}
675fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
676a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
67746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  if (!ByVal)
67846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return ABIArgInfo::getIndirect(0, false);
67946c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
680e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Compute the byval alignment.
681e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
682e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
683e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign == 0)
684de92d739ba0ef42a5a7dcfd6e170329549d0716bChris Lattner    return ABIArgInfo::getIndirect(4);
685e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
686e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // If the stack alignment is less than the type alignment, realign the
687e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // argument.
688e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign < TypeAlign)
689e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
690e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                   /*Realign=*/true);
691e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
692e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  return ABIArgInfo::getIndirect(StackAlign);
693dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar}
694dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
695a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
696c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Set alignment on indirect arguments.
697d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(Ty)) {
698c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Structures with flexible arrays are always indirect.
699a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
700a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      // Structures with either a non-trivial destructor or a non-trivial
701a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      // copy constructor are always indirect.
702a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      if (hasNonTrivialDestructorOrCopyConstructor(RT))
703a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        return getIndirectResult(Ty, /*ByVal=*/false);
704dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
705c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
706a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        return getIndirectResult(Ty);
707a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    }
708c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
7095a4d35247f55dae6dd0d5ad349ecadbbea0b4572Eli Friedman    // Ignore empty structs/unions.
7105a1ac89b244940a0337ea7ae7dc371e2a9bf7c50Eli Friedman    if (isEmptyRecord(getContext(), Ty, true))
711c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIgnore();
712c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
71353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // Expand small (<= 128-bit) record types when we know that the stack layout
71453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // of those arguments will match the struct. This is important because the
71553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // LLVM backend isn't smart enough to remove byval, which inhibits many
71653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // optimizations.
717a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (getContext().getTypeSize(Ty) <= 4*32 &&
718a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        canExpandIndirectArgument(Ty, getContext()))
71953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar      return ABIArgInfo::getExpand();
720c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
721a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    return getIndirectResult(Ty);
7228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  }
7238bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
724bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
7257b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // On Darwin, some vectors are passed in memory, we handle this by passing
7267b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // it as an i8/i16/i32/i64.
727bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    if (IsDarwinVectorABI) {
728bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      uint64_t Size = getContext().getTypeSize(Ty);
729bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      if ((Size == 8 || Size == 16 || Size == 32) ||
730bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner          (Size == 64 && VT->getNumElements() == 1))
731bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
732bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner                                                            Size));
733bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    }
734bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling
7359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *IRType = CGT.ConvertType(Ty);
736bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    if (UseX86_MMXType(IRType)) {
737c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman      if (IsMMXDisabled)
738c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
739c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman                                                            64));
740bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
741bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
742bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      return AAI;
743bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    }
7449cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
745bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    return ABIArgInfo::getDirect();
746bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  }
7479cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
7489cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
749a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
750a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    Ty = EnumTy->getDecl()->getIntegerType();
751aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
752a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  return (Ty->isPromotableIntegerType() ?
753a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
754c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
755c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
756c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
757c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
7588b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
759c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
760c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
761c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
762c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                       "ap");
763c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
7647b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
7657b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  // Compute if the address needs to be aligned
7667b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
7677b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = getTypeStackAlignInBytes(Ty, Align);
7687b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = std::max(Align, 4U);
7697b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  if (Align > 4) {
7707b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    // addr = (addr + align - 1) & -align;
7717b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Offset =
7727b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman      llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
7737b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateGEP(Addr, Offset);
7747b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
7757b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                                    CGF.Int32Ty);
7767b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
7777b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
7787b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      Addr->getType(),
7797b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      "ap.cur.aligned");
7807b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  }
7817b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
782c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Type *PTy =
78396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
784c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
785c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
786c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
7877b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
788c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
78977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
790c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
791c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
792c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
793c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
794c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
795c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
79674f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davisvoid X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
79774f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                                  llvm::GlobalValue *GV,
79874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                            CodeGen::CodeGenModule &CGM) const {
79974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
80074f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
80174f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Get the LLVM function.
80274f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      llvm::Function *Fn = cast<llvm::Function>(GV);
80374f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
80474f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Now add the 'alignstack' attribute with a value of 16.
80574f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
80674f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    }
80774f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  }
80874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis}
80974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
8106374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCallbool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
8116374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               CodeGen::CodeGenFunction &CGF,
8126374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               llvm::Value *Address) const {
8136374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
8146374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
8158b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
8168bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
8176374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 0-7 are the eight integer registers;  the order is different
8186374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  //   on Darwin (for EH), but the range is the same.
8196374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 8 is %eip.
820aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 8);
8216374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
8226374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  if (CGF.CGM.isTargetDarwin()) {
8236374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 12-16 are st(0..4).  Not sure why we stop at 4.
8246374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 16, which is sizeof(long double) on
8256374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 8-byte alignment for that type.
8268b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
827aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
8288bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
8296374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  } else {
8306374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 9 is %eflags, which doesn't get a size on Darwin for some
8316374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // reason.
8326374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
8336374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
8346374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 11-16 are st(0..5).  Not sure why we stop at 5.
8356374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 12, which is sizeof(long double) on
8366374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 4-byte alignment for that type.
8378b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
838aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
839aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
8406374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
8416374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  return false;
8426374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall}
8436374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
844dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
845dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner// X86-64 ABI Implementation
846dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
847dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
848dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
849c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovnamespace {
850c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// X86_64ABIInfo - The X86_64 ABI information.
851c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass X86_64ABIInfo : public ABIInfo {
852c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  enum Class {
853c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Integer = 0,
854c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSE,
855c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSEUp,
856c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87,
857c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87Up,
858c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ComplexX87,
859c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    NoClass,
860c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Memory
861c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  };
862c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
863c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// merge - Implement the X86_64 ABI merging algorithm.
864c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
865c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// Merge an accumulating classification \arg Accum with a field
866c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classification \arg Field.
867c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
868c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Accum - The accumulating classification. This should
869c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// always be either NoClass or the result of a previous merge
870c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// call. In addition, this should never be Memory (the caller
871c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// should just return Memory for the aggregate).
8721090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  static Class merge(Class Accum, Class Field);
873c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
8744943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// postMerge - Implement the X86_64 ABI post merging algorithm.
8754943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
8764943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// Post merger cleanup, reduces a malformed Hi and Lo pair to
8774943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// final MEMORY or SSE classes when necessary.
8784943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
8794943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param AggregateSize - The size of the current aggregate in
8804943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// the classification process.
8814943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
8824943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Lo - The classification for the parts of the type
8834943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the low word of the containing object.
8844943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
8854943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Hi - The classification for the parts of the type
8864943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the higher words of the containing object.
8874943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
8884943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
8894943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
890c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classify - Determine the x86_64 register classes in which the
891c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// given type T should be passed.
892c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
893c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Lo - The classification for the parts of the type
894c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the low word of the containing object.
895c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
896c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Hi - The classification for the parts of the type
897c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the high word of the containing object.
898c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
899c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param OffsetBase - The bit offset of this type in the
900c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// containing object.  Some parameters are classified different
901c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// depending on whether they straddle an eightbyte boundary.
902c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
903c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If a word is unused its result will be NoClass; if a type should
904c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// be passed in Memory then at least the classification of \arg Lo
905c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// will be Memory.
906c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
907c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// The \arg Lo class will be NoClass iff the argument is ignored.
908c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
909c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
910c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// also be ComplexX87.
9119c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
912c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
9134943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  llvm::Type *GetByteVectorType(QualType Ty) const;
9149cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
9159cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned IROffset, QualType SourceTy,
9169cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned SourceOffset) const;
9179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
9189cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned IROffset, QualType SourceTy,
9199cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned SourceOffset) const;
9208bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
921c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
92246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// such that the argument will be returned in memory.
9239c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  ABIArgInfo getIndirectReturnResult(QualType Ty) const;
92446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
92546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
926c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// such that the argument will be passed in memory.
927edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ///
928edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// \param freeIntRegs - The number of free integer registers remaining
929edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// available.
930edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
931c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
932a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
933c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
934bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  ABIArgInfo classifyArgumentType(QualType Ty,
935edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                  unsigned freeIntRegs,
936bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling                                  unsigned &neededInt,
93799aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling                                  unsigned &neededSSE) const;
938c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
939ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool IsIllegalVectorType(QualType Ty) const;
940ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
94167a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// The 0.98 ABI revision clarified a lot of ambiguities,
94267a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// unfortunately in ways that were not always consistent with
94367a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// certain previous compilers.  In particular, platforms which
94467a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// required strict binary compatibility with older versions of GCC
94567a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// may need to exempt themselves.
94667a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  bool honorsRevision0_98() const {
947bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return !getContext().getTargetInfo().getTriple().isOSDarwin();
94867a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  }
94967a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall
950ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool HasAVX;
951ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
952c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovpublic:
953ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
954ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      ABIInfo(CGT), HasAVX(hasavx) {}
9559c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
956de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isPassedUsingAVXType(QualType type) const {
957de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    unsigned neededInt, neededSSE;
958edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // The freeIntRegs argument doesn't matter here.
959edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
960de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (info.isDirect()) {
961de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      llvm::Type *ty = info.getCoerceToType();
962de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
963de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        return (vectorTy->getBitWidth() > 128);
964de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    }
965de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return false;
966de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
967de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
968ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
969c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
970c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
971c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
972c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
97382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
974f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
975a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumiclass WinX86_64ABIInfo : public ABIInfo {
976a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
977a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  ABIArgInfo classify(QualType Ty) const;
978a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
979f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
980a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
981a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
982a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  virtual void computeInfo(CGFunctionInfo &FI) const;
983f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
984f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
985f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                 CodeGenFunction &CGF) const;
986f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
987f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
98882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
98982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
990ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
991ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
9926374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
993de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  const X86_64ABIInfo &getABIInfo() const {
994de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
995de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
996de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
9976374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
9986374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 7;
9996374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
10006374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
10016374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
10026374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const {
10038b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
10048bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1005aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 0-15 are the 16 integer registers.
1006aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 16 is %rip.
10078b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
10086374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return false;
10096374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
10104b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1011ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
10125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
1013ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
10144b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
10154b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
10164b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1017de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isNoProtoCallVariadic(const CallArgList &args,
1018de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             const FunctionNoProtoType *fnType) const {
101901f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // The default CC on x86-64 sets %al to the number of SSA
102001f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // registers used, and GCC sets this when calling an unprototyped
10213ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    // function, so we override the default behavior.  However, don't do
102268805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // that when AVX types are involved: the ABI explicitly states it is
102368805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // undefined, and it doesn't work in practice because of how the ABI
102468805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // defines varargs anyway.
1025de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
10263ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      bool HasAVXType = false;
1027de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      for (CallArgList::const_iterator
1028de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall             it = args.begin(), ie = args.end(); it != ie; ++it) {
1029de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1030de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          HasAVXType = true;
1031de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          break;
10323ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        }
10333ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      }
1034de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
10353ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      if (!HasAVXType)
10363ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        return true;
10373ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    }
103801f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
1039de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
104001f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  }
104101f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
104282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
104382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1044f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerclass WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1045f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
1046f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1047f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1048f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1049f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1050f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return 7;
1051f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
1052f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1053f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1054f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                               llvm::Value *Address) const {
10558b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
10569cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1057f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 0-15 are the 16 integer registers.
1058f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 16 is %rip.
10598b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1060f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return false;
1061f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
1062f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
1063f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1064c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1065c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
10664943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesvoid X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
10674943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes                              Class &Hi) const {
10684943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
10694943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
10704943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (a) If one of the classes is Memory, the whole argument is passed in
10714943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
10724943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
10734943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (b) If X87UP is not preceded by X87, the whole argument is passed in
10744943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
10754943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
10764943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (c) If the size of the aggregate exceeds two eightbytes and the first
10774943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
10784943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     argument is passed in memory. NOTE: This is necessary to keep the
10794943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     ABI working for processors that don't support the __m256 type.
10804943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
10814943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
10824943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
10834943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Some of these are enforced by the merging logic.  Others can arise
10844943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // only with unions; for example:
10854943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //   union { _Complex double; unsigned; }
10864943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
10874943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Note that clauses (b) and (c) were added in 0.98.
10884943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
10894943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == Memory)
10904943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
10914943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
10924943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
10934943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
10944943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
10954943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == SSEUp && Lo != SSE)
10964943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Hi = SSE;
10974943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes}
10984943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
10991090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerX86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
1100c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1101c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classified recursively so that always two fields are
1102c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // considered. The resulting class is calculated according to
1103c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the classes of the fields in the eightbyte:
1104c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1105c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (a) If both classes are equal, this is the resulting class.
1106c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1107c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (b) If one of the classes is NO_CLASS, the resulting class is
1108c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the other class.
1109c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1110c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (c) If one of the classes is MEMORY, the result is the MEMORY
1111c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // class.
1112c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1113c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (d) If one of the classes is INTEGER, the result is the
1114c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // INTEGER.
1115c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1117c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // MEMORY is used as class.
1118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (f) Otherwise class SSE is used.
1120c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1121c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Accum should never be memory (we should have returned) or
1122c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // ComplexX87 (because this cannot be passed in a structure).
1123c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Accum != Memory && Accum != ComplexX87) &&
1124c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         "Invalid accumulated classification during merge.");
1125c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Accum == Field || Field == NoClass)
1126c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Accum;
11271090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == Memory)
1128c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
11291090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == NoClass)
1130c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Field;
11311090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == Integer || Field == Integer)
1132c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Integer;
11331090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
11341090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      Accum == X87 || Accum == X87Up)
1135c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
11361090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  return SSE;
1137c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1138c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1139bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattnervoid X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
1140c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                             Class &Lo, Class &Hi) const {
1141c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This code can be simplified by introducing a simple value class for
1142c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Class pairs with appropriate constructor methods for the various
1143c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // situations.
1144c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1145c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Some of the split computations are wrong; unaligned vectors
1146c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // shouldn't be passed in registers for example, so there is no chance they
1147c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // can straddle an eightbyte. Verify & simplify.
1148c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1149c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Lo = Hi = NoClass;
1150c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1151c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Class &Current = OffsetBase < 64 ? Lo : Hi;
1152c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Current = Memory;
1153c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1154183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1155c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    BuiltinType::Kind k = BT->getKind();
1156c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1157c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (k == BuiltinType::Void) {
1158c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = NoClass;
1159c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1160c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Integer;
1161c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Integer;
1162c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1163c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
1164c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1165c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
1166c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::LongDouble) {
1167c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = X87;
1168c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = X87Up;
1169c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1170c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _Decimal32 and _Decimal64 are SSE.
1171c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
11721090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
11731090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
11748bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
11751090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const EnumType *ET = Ty->getAs<EnumType>()) {
1176c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Classify the underlying integer type.
11779c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
11781090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
11791090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
11808bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
11811090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->hasPointerRepresentation()) {
1182c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = Integer;
11831090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
11841090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
11858bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
11861090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->isMemberPointerType()) {
118767d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar    if (Ty->isMemberFunctionPointerType())
118867d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Lo = Hi = Integer;
118967d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar    else
119067d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Current = Integer;
11911090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
11921090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
11938bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
11941090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
1195ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(VT);
1196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 32) {
1197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // float> as integer.
1199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
1200c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1201c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1202c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1203c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Real = (OffsetBase) / 64;
1204c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1205c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (EB_Real != EB_Imag)
1206c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1207c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (Size == 64) {
1208c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x double> in memory. :(
1209c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1210c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1211c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1212c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x long long> as INTEGER.
1213473f8e723be93d84bd5fd15b094f4184802d4676Chris Lattner      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
12140fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
12150fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
12160fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
1217c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1218c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else
1219c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = SSE;
1220c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1221c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1222c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1223c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (OffsetBase && OffsetBase != 64)
1224c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1225ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    } else if (Size == 128 || (HasAVX && Size == 256)) {
12264943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // Arguments of 256-bits are split into four eightbyte chunks. The
12274943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // least significant one belongs to class SSE and all the others to class
12284943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // SSEUP. The original Lo and Hi design considers that types can't be
12294943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
12304943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // This design isn't correct for 256-bits, but since there're no cases
12314943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // where the upper parts would need to be inspected, avoid adding
12324943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // complexity and just consider Hi to match the 64-256 part.
1233c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = SSE;
1234c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = SSEUp;
1235c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
12361090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
12371090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
12388bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
12391090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
1240ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    QualType ET = getContext().getCanonicalType(CT->getElementType());
1241c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1242ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
12432ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (ET->isIntegralOrEnumerationType()) {
1244c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size <= 64)
1245c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1246c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else if (Size <= 128)
1247c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Hi = Integer;
1248ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    } else if (ET == getContext().FloatTy)
1249c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
1250ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    else if (ET == getContext().DoubleTy)
1251c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Hi = SSE;
1252ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    else if (ET == getContext().LongDoubleTy)
1253c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = ComplexX87;
1254c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1255c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // If this complex type crosses an eightbyte boundary then it
1256c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // should be split.
1257c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t EB_Real = (OffsetBase) / 64;
1258ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
1259c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Hi == NoClass && EB_Real != EB_Imag)
1260c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Lo;
12618bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
12621090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
12631090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
12648bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1265ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
1266c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Arrays are treated like structures.
1267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1268ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1269c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1270c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
12714943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
12724943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1275c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1276c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // fields, it has class MEMORY.
1277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
1278c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Only need to check alignment of array base.
1279ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
1280c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1282c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Otherwise implement simplified merge. We could be smarter about
1283c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // this, but it isn't worth it and would be harder to verify.
1284c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1285ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
1286c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t ArraySize = AT->getSize().getZExtValue();
1287089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1288089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // The only case a 256-bit wide vector could be used is when the array
1289089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1290089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // to work for sizes wider than 128, early check and fallback to memory.
1291089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    if (Size > 128 && EltSize != 256)
1292089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes      return;
1293089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1294c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1295c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
12969c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner      classify(AT->getElementType(), Offset, FieldLo, FieldHi);
1297c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1298c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1299c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1300c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1301c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1302c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
13034943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1304c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
13051090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13061090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13078bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13081090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1309ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1310c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1311c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
13124943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
13134943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1314c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1315c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
13160a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
13170a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // copy constructor or a non-trivial destructor, it is passed by invisible
13180a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // reference.
13190a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    if (hasNonTrivialDestructorOrCopyConstructor(RT))
13200a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson      return;
1321ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1322c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    const RecordDecl *RD = RT->getDecl();
1323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1324c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Assume variable sized types are passed in memory.
1325c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (RD->hasFlexibleArrayMember())
1326c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1327c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1328ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1329c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1330c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Reset Lo class, this will be recomputed.
1331c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1332ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1333ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // If this is a C++ record, classify the bases first.
1334ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1335ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1336ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar             e = CXXRD->bases_end(); i != e; ++i) {
1337ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1338ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar               "Unexpected base class!");
1339ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        const CXXRecordDecl *Base =
1340ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1341ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1342ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // Classify this field.
1343ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        //
1344ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1345ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // single eightbyte, each is classified separately. Each eightbyte gets
1346ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // initialized to class NO_CLASS.
1347ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Class FieldLo, FieldHi;
1348a14f5979572aa25c03d24750ee4724d2031d4edeAnders Carlsson        uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base);
13499c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1350ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Lo = merge(Lo, FieldLo);
1351ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Hi = merge(Hi, FieldHi);
1352ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        if (Lo == Memory || Hi == Memory)
1353ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          break;
1354ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      }
1355ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    }
1356ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1357ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // Classify the fields one at a time, merging the results.
1358c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    unsigned idx = 0;
1359548e478b8bd02b0295bc4efd0c282337f00646fdBruno Cardoso Lopes    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
136017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis           i != e; ++i, ++idx) {
1361c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1362c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      bool BitField = i->isBitField();
1363c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1364b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1365b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // four eightbytes, or it contains unaligned fields, it has class MEMORY.
1366c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1367b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // The only case a 256-bit wide vector could be used is when the struct
1368b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1369b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // to work for sizes wider than 128, early check and fallback to memory.
1370b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      //
1371b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1372b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        Lo = Memory;
1373b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        return;
1374b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      }
1375c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Note, skip this test for bit-fields, see below.
1376ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner      if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
1377c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Memory;
1378c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1379c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      }
1380c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1381c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Classify this field.
1382c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1383c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1384c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // exceeds a single eightbyte, each is classified
1385c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // separately. Each eightbyte gets initialized to class
1386c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // NO_CLASS.
1387c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
1388c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1389c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Bit-fields require special handling, they do not force the
1390c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // structure to be passed in memory even if unaligned, and
1391c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // therefore they can straddle an eightbyte.
1392c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (BitField) {
1393c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        // Ignore padding bit-fields.
1394c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (i->isUnnamedBitfield())
1395c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          continue;
1396c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1397c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1398a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        uint64_t Size = i->getBitWidthValue(getContext());
1399c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1400c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Lo = Offset / 64;
1401c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Hi = (Offset + Size - 1) / 64;
1402c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        FieldLo = FieldHi = NoClass;
1403c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (EB_Lo) {
1404c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1405c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = NoClass;
1406c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = Integer;
1407c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        } else {
1408c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = Integer;
1409c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = EB_Hi ? Integer : NoClass;
1410c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        }
1411c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      } else
14129c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1413c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1414c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1415c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1416c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1417c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1418c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
14194943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1420c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
1421c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1422c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
14239c254f0415bef9a0bafe5b5026ddb54b727597b1Chris LattnerABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
142446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // If this is a scalar LLVM value then assume LLVM will pass it in the right
142546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // place naturally.
1426d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
142746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    // Treat an enum type as its underlying type.
142846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
142946c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar      Ty = EnumTy->getDecl()->getIntegerType();
143046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
143146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return (Ty->isPromotableIntegerType() ?
143246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
143346c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  }
143446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
143546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  return ABIArgInfo::getIndirect(0);
143646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar}
143746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
1438ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedmanbool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1439ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1440ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    uint64_t Size = getContext().getTypeSize(VecTy);
1441ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    unsigned LargestVector = HasAVX ? 256 : 128;
1442ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    if (Size <= 64 || Size > LargestVector)
1443ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return true;
1444ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
1445ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1446ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  return false;
1447ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman}
1448ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1449edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1450edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                            unsigned freeIntRegs) const {
1451c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1452c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // place naturally.
1453edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1454edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // This assumption is optimistic, as there could be free registers available
1455edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // when we need to pass this argument in memory, and LLVM could try to pass
1456edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // the argument in the free register. This does not seem to happen currently,
1457edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // but this code would be much safer if we could mark the argument with
1458edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // 'onstack'. See PR12193.
1459ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
1460aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
1461aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1462aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
1463aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
1464cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
1465cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1466aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
1467c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
146846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
146946c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
14700a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1471855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // Compute the byval alignment. We specify the alignment of the byval in all
1472855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // cases so that the mid-level optimizer knows the alignment of the byval.
1473855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1474edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1475edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // Attempt to avoid passing indirect results using byval when possible. This
1476edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // is important for good codegen.
1477edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1478edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We do this by coercing the value into a scalar type which the backend can
1479edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // handle naturally (i.e., without using byval).
1480edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1481edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // For simplicity, we currently only do this when we have exhausted all of the
1482edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // free integer registers. Doing this when there are free integer registers
1483edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // would require more care, as we would have to ensure that the coerced value
1484edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // did not claim the unused register. That would require either reording the
1485edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to the function (so that any subsequent inreg values came first),
1486edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // or only doing this optimization when there were no following arguments that
1487edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // might be inreg.
1488edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1489edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We currently expect it to be rare (particularly in well written code) for
1490edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to be passed on the stack when there are still free integer
1491edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // registers available (this would typically imply large structs being passed
1492edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // by value), so this seems like a fair tradeoff for now.
1493edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1494edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We can revisit this if the backend grows support for 'onstack' parameter
1495edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // attributes. See PR12193.
1496edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  if (freeIntRegs == 0) {
1497edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    uint64_t Size = getContext().getTypeSize(Ty);
1498edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1499edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // If this type fits in an eightbyte, coerce it into the matching integral
1500edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // type, which will end up on the stack (with alignment 8).
1501edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    if (Align == 8 && Size <= 64)
1502edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1503edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                                          Size));
1504edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  }
1505edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1506855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  return ABIArgInfo::getIndirect(Align);
1507c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1508c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15094943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// GetByteVectorType - The ABI specifies that a value should be passed in an
15104943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// full vector XMM/YMM register.  Pick an LLVM IR type that will be passed as a
15110f408f5242522cbede304472e17931357c1b573dChris Lattner/// vector register.
15124943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesllvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
15139cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *IRType = CGT.ConvertType(Ty);
15148bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
151515842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // Wrapper structs that just contain vectors are passed just like vectors,
151615842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // strip them off if present.
15179cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
151815842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  while (STy && STy->getNumElements() == 1) {
151915842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    IRType = STy->getElementType(0);
152015842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    STy = dyn_cast<llvm::StructType>(IRType);
152115842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  }
15228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1523528a8c7b4c39ae1c551760fd087a508a71ee9541Bruno Cardoso Lopes  // If the preferred type is a 16-byte vector, prefer to pass it.
15249cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
15259cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = VT->getElementType();
15264943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    unsigned BitWidth = VT->getBitWidth();
1527ce275675d33142c235d7027db16abe43da616ee4Tanya Lattner    if ((BitWidth >= 128 && BitWidth <= 256) &&
15280f408f5242522cbede304472e17931357c1b573dChris Lattner        (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
15290f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
15300f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
15310f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(128)))
15320f408f5242522cbede304472e17931357c1b573dChris Lattner      return VT;
15330f408f5242522cbede304472e17931357c1b573dChris Lattner  }
15348bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
15350f408f5242522cbede304472e17931357c1b573dChris Lattner  return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
15360f408f5242522cbede304472e17931357c1b573dChris Lattner}
15370f408f5242522cbede304472e17931357c1b573dChris Lattner
1538e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1539e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// is known to either be off the end of the specified type or being in
1540e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// alignment padding.  The user type specified is known to be at most 128 bits
1541e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// in size, and have passed through X86_64ABIInfo::classify with a successful
1542e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// classification that put one of the two halves in the INTEGER class.
1543e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner///
1544e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// It is conservatively correct to return false.
1545e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattnerstatic bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1546e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                  unsigned EndBit, ASTContext &Context) {
1547e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If the bytes being queried are off the end of the type, there is no user
1548e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // data hiding here.  This handles analysis of builtins, vectors and other
1549e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // types that don't contain interesting padding.
1550e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1551e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (TySize <= StartBit)
1552e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1553e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1554021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1555021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1556021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1557021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner
1558021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // Check each element to see if the element overlaps with the queried range.
1559021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    for (unsigned i = 0; i != NumElts; ++i) {
1560021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      // If the element is after the span we care about, then we're done..
1561021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltOffset = i*EltSize;
1562021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (EltOffset >= EndBit) break;
15638bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1564021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1565021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1566021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner                                 EndBit-EltOffset, Context))
1567021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner        return false;
1568021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    }
1569021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // If it overlaps no elements, then it is safe to process as padding.
1570021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    return true;
1571021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
15728bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1573e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1574e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const RecordDecl *RD = RT->getDecl();
1575e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
15768bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1577e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If this is a C++ record, check the bases first.
1578e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1579e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1580e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner           e = CXXRD->bases_end(); i != e; ++i) {
1581e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1582e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner               "Unexpected base class!");
1583e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        const CXXRecordDecl *Base =
1584e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
15858bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1586e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        // If the base is after the span we care about, ignore it.
1587a14f5979572aa25c03d24750ee4724d2031d4edeAnders Carlsson        unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base);
1588e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (BaseOffset >= EndBit) continue;
15898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1590e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1591e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (!BitsContainNoUserData(i->getType(), BaseStart,
1592e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                   EndBit-BaseOffset, Context))
1593e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          return false;
1594e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      }
1595e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
15968bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1597e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Verify that no field has data that overlaps the region of interest.  Yes
1598e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // this could be sped up a lot by being smarter about queried fields,
1599e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // however we're only looking at structs up to 16 bytes, so we don't care
1600e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // much.
1601e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    unsigned idx = 0;
1602e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1603e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner         i != e; ++i, ++idx) {
1604e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
16058bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1606e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      // If we found a field after the region we care about, then we're done.
1607e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (FieldOffset >= EndBit) break;
1608e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1609e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1610e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1611e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                 Context))
1612e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return false;
1613e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
16148bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1615e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If nothing in this record overlapped the area of interest, then we're
1616e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // clean.
1617e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1618e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
16198bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1620e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  return false;
1621e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner}
1622e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
16230b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
16240b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float member at the specified offset.  For example, {int,{float}} has a
16250b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float at offset 4.  It is conservatively correct for this routine to return
16260b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// false.
16272acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerstatic bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
16280b3620066bfbb33004bed1816c851a923b9301afChris Lattner                                  const llvm::TargetData &TD) {
16290b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // Base case if we find a float.
16300b3620066bfbb33004bed1816c851a923b9301afChris Lattner  if (IROffset == 0 && IRType->isFloatTy())
16310b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return true;
16328bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
16330b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is a struct, recurse into the field at the specified offset.
16342acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
16350b3620066bfbb33004bed1816c851a923b9301afChris Lattner    const llvm::StructLayout *SL = TD.getStructLayout(STy);
16360b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned Elt = SL->getElementContainingOffset(IROffset);
16370b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= SL->getElementOffset(Elt);
16380b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
16390b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
16408bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
16410b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is an array, recurse into the field at the specified offset.
16422acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
16432acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ATy->getElementType();
16440b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned EltSize = TD.getTypeAllocSize(EltTy);
16450b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= IROffset/EltSize*EltSize;
16460b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(EltTy, IROffset, TD);
16470b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
16480b3620066bfbb33004bed1816c851a923b9301afChris Lattner
16490b3620066bfbb33004bed1816c851a923b9301afChris Lattner  return false;
16500b3620066bfbb33004bed1816c851a923b9301afChris Lattner}
16510b3620066bfbb33004bed1816c851a923b9301afChris Lattner
1652f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1653f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1654f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// low 8 bytes of an XMM register, corresponding to the SSE class.
16559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
16569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
1657f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                   QualType SourceTy, unsigned SourceOffset) const {
1658cba8d310163f84630fd140fbfa9b6fdad9d26587Chris Lattner  // The only three choices we have are either double, <2 x float>, or float. We
1659f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // pass as float if the last 4 bytes is just padding.  This happens for
1660f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // structs that contain 3 floats.
1661f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1662f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                            SourceOffset*8+64, getContext()))
1663f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner    return llvm::Type::getFloatTy(getVMContext());
16648bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
16650b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // We want to pass as <2 x float> if the LLVM IR type contains a float at
16660b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
16670b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // case.
16680b3620066bfbb33004bed1816c851a923b9301afChris Lattner  if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
166922fd4baf2eba2103e2b41e463f1a5f6486c398fbChris Lattner      ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
167022fd4baf2eba2103e2b41e463f1a5f6486c398fbChris Lattner    return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
16718bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1672f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  return llvm::Type::getDoubleTy(getVMContext());
1673f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner}
1674f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1675f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
16760d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
16770d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// an 8-byte GPR.  This means that we either have a scalar or we are talking
16780d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// about the high or low part of an up-to-16-byte struct.  This routine picks
16790d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// the best LLVM IR type to represent this, which may be i64 or may be anything
1680519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1681519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// etc).
1682519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1683519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1684519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the source type.  IROffset is an offset in bytes into the LLVM IR type that
1685519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the 8-byte value references.  PrefType may be null.
1686519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1687519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// SourceTy is the source level type for the entire argument.  SourceOffset is
1688519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// an offset into this that we're processing (which is always either 0 or 8).
1689519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
16909cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
16919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
16920d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                       QualType SourceTy, unsigned SourceOffset) const {
1693e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If we're dealing with an un-offset LLVM IR type, then it means that we're
1694e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // returning an 8-byte unit starting with it.  See if we can safely use it.
1695e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (IROffset == 0) {
1696e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Pointers and int64's always fill the 8-byte unit.
1697e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1698e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      return IRType;
1699e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1700e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1701e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // goodness in the source type is just tail padding.  This is allowed to
1702e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // kick in for struct {double,int} on the int, but not on
1703e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // struct{double,int,int} because we wouldn't return the second int.  We
1704e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // have to do this analysis on the source type because we can't depend on
1705e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // unions being lowered a specific way etc.
1706e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1707e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        IRType->isIntegerTy(32)) {
1708e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
17098bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1710e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1711e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                SourceOffset*8+64, getContext()))
1712e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return IRType;
1713e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
1714e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
1715519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
17162acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1717519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    // If this is a struct, recurse into the field at the specified offset.
171844f0fd2804e9952a8dbf85bb60ee3501aa9f5ee7Chris Lattner    const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
1719519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    if (IROffset < SL->getSizeInBytes()) {
1720519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1721519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      IROffset -= SL->getElementOffset(FieldIdx);
17228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
17230d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner      return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
17240d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                    SourceTy, SourceOffset);
17258bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    }
1726519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  }
17278bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
17282acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
17299cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = ATy->getElementType();
1730021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1731021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltOffset = IROffset/EltSize*EltSize;
17320d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner    return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
17330d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                  SourceOffset);
1734021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
17358bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1736519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // Okay, we don't have any better idea of what to pass, so we pass this in an
1737519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // integer register that isn't too big to fit the rest of the struct.
17389e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  unsigned TySizeInBytes =
17399e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner    (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
1740519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
17419e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  assert(TySizeInBytes != SourceOffset && "Empty field?");
17428bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1743519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // It is always safe to classify this as an integer type up to i64 that
1744519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // isn't larger than the structure.
17459e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  return llvm::IntegerType::get(getVMContext(),
17469e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner                                std::min(TySizeInBytes-SourceOffset, 8U)*8);
1747519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner}
1748519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
174966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
175066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
175166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// be used as elements of a two register pair to pass or return, return a
175266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// first class aggregate to represent them.  For example, if the low part of
175366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// a by-value argument should be passed as i32* and the high part as float,
175466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// return {i32*, float}.
17559cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *
1756ef6de3da8572607f786303c07150daa6e140ab19Jay FoadGetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
175766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner                           const llvm::TargetData &TD) {
175866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // In order to correctly satisfy the ABI, we need to the high part to start
175966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // at offset 8.  If the high and low parts we inferred are both 4-byte types
176066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
176166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // the second element at offset 8.  Check for this:
176266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
176366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned HiAlign = TD.getABITypeAlignment(Hi);
176466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
176566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
17669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
176766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // To handle this, we have to increase the size of the low part so that the
176866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // second element will start at an 8 byte offset.  We can't increase the size
176966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // of the second element because it might make us access off the end of the
177066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // struct.
177166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HiStart != 8) {
177266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // There are only two sorts of types the ABI generation code can produce for
177366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
177466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // Promote these to a larger type.
177566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    if (Lo->isFloatTy())
177666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getDoubleTy(Lo->getContext());
177766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    else {
177866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
177966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getInt64Ty(Lo->getContext());
178066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    }
178166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  }
17829cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
17839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
17849cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
17859cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
178666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // Verify that the second element is at an 8-byte offset.
178766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
178866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner         "Invalid x86-64 argument pair!");
178966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  return Result;
179066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner}
179166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
17921090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerABIArgInfo X86_64ABIInfo::
1793a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerclassifyReturnType(QualType RetTy) const {
1794c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1795c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classification algorithm.
1796c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
17979c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(RetTy, 0, Lo, Hi);
1798c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1799c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
1800c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1801c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1802c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
18039cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
1804c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
1805c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
1806117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
1807117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
1808117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
1809117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
1810117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1811117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
1812117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
1813c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1814c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
1815c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
1816b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
1817c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1818c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1819c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // hidden argument.
1820c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
18219c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    return getIndirectReturnResult(RetTy);
1822c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1823c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1824c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rax, %rdx is used.
1825c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
18269cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
18278bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1828eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
1829eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
1830eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1831eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
1832eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
1833eb518b4b89e4134b21975530809697142f69b779Chris Lattner        RetTy = EnumTy->getDecl()->getIntegerType();
18348bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1835eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (RetTy->isIntegralOrEnumerationType() &&
1836eb518b4b89e4134b21975530809697142f69b779Chris Lattner          RetTy->isPromotableIntegerType())
1837eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
1838eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
1839519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    break;
1840c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1841c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
1842c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register of the sequence %xmm0, %xmm1 is used.
1843c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSE:
18449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
18450b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
1846c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1847c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
1848c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned on the X87 stack in %st0 as 80-bit x87 number.
1849c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
1850ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    ResType = llvm::Type::getX86_FP80Ty(getVMContext());
18510b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
1852c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1853c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
1854c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // part of the value is returned in %st0 and the imaginary part in
1855c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // %st1.
1856c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
1857c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
18587650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
1859ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner                                    llvm::Type::getX86_FP80Ty(getVMContext()),
1860c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                    NULL);
1861c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1862c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
1863c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
18649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
1865c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
1866c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously and X87 should
1867c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // never occur as a hi class.
1868c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
1869c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
1870b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
1871c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1872c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87: // Previously handled.
18730b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner  case NoClass:
18740b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
1875c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
18763db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case Integer:
18779cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
18783db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
18793db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
1880c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
18813db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case SSE:
18829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
18833db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
18843db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
1885c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1886c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1887c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
18884943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // is passed in the next available eightbyte chunk if the last used
18894943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // vector register.
1890c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
1891fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // SSEUP should always be preceded by SSE, just widen.
1892c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
1893c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Lo == SSE && "Unexpected SSEUp classification.");
18944943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(RetTy);
1895c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1896c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1897c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
1898c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned together with the previous X87 value in %st0.
1899c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
1900fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // If X87Up is preceded by X87, we don't need to do
1901c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // anything. However, in some cases with unions it may not be
1902fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // preceded by X87. In such situations we follow gcc and pass the
1903c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // extra bits in an SSE reg.
1904603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    if (Lo != X87) {
19059cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
19063db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
19073db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner        return ABIArgInfo::getDirect(HighPart, 8);
1908603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    }
1909c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1910c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
19119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
19123db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
1913645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
1914645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
191566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HighPart)
191666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
1917c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1918eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
19199c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner}
19209c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
1921edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::classifyArgumentType(
1922edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
1923edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  const
1924edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar{
1925c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
19269c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(Ty, 0, Lo, Hi);
19278bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1928c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
1929c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Enforce these by construction.
1930c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1931c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1932c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1933c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededInt = 0;
1934c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededSSE = 0;
19359cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
1936c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
1937c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
1938117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
1939117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
1940117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
1941117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
1942117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1943117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
1944117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
19458bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1946c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
1947c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // on the stack.
1948c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
1949c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1950c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
1951c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // COMPLEX_X87, it is passed in memory.
1952c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
1953c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
1954ded137fcab19f0aace08a28b5c91574e6b23debcEli Friedman    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
1955ded137fcab19f0aace08a28b5c91574e6b23debcEli Friedman      ++neededInt;
1956edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    return getIndirectResult(Ty, freeIntRegs);
1957c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1958c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
1959c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
1960b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
1961c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1962c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
1963c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
1964c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // and %r9 is used.
1965c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
19669c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    ++neededInt;
19678bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
196849382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
19699cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
1970eb518b4b89e4134b21975530809697142f69b779Chris Lattner
1971eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
1972eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
1973eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1974eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
1975eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1976eb518b4b89e4134b21975530809697142f69b779Chris Lattner        Ty = EnumTy->getDecl()->getIntegerType();
19778bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1978eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (Ty->isIntegralOrEnumerationType() &&
1979eb518b4b89e4134b21975530809697142f69b779Chris Lattner          Ty->isPromotableIntegerType())
1980eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
1981eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
19828bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1983c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1984c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1985c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
1986c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register is used, the registers are taken in the
1987c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // order from %xmm0 to %xmm7.
1988bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  case SSE: {
19899cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *IRType = CGT.ConvertType(Ty);
199014508ff0bffee0fdfe5d336946c6db0e709099c8Eli Friedman    ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
199199aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    ++neededSSE;
1992c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
1993c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
1994bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  }
1995c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
19969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
1997c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
1998c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously, ComplexX87 and X87 should
1999fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // never occur as hi classes, and X87Up must be preceded by X87,
2000c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // which is passed in memory.
2001c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2002c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2003c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2004b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
2005c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2006c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass: break;
20078bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2008645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case Integer:
2009c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededInt;
201049382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
20119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2012117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2013645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2014645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2015c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2016c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2017c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // X87Up generally doesn't occur here (long double is passed in
2018c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // memory), except in situations involving unions.
2019c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2020645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case SSE:
20219cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
20228bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2023645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2024645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2025117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2026c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededSSE;
2027c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2028c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2029c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2030c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte is passed in the upper half of the last used SSE
20318bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    // register.  This only happens when 128-bit vectors are passed.
2032c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2033ab5722e67794b3954c874a369086fc5f41ac46a5Chris Lattner    assert(Lo == SSE && "Unexpected SSEUp classification");
20344943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(Ty);
2035c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2036c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2037c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2038645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
2039645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
2040645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
2041645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  if (HighPart)
204266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
20439cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2044eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
2045c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2046c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2047ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
20488bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2049a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2050c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2051c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Keep track of the number of assigned registers.
205299aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned freeIntRegs = 6, freeSSERegs = 8;
2053c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2054c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If the return value is indirect, then the hidden argument is consuming one
2055c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // integer register.
2056c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (FI.getReturnInfo().isIndirect())
2057c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    --freeIntRegs;
2058c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2059c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2060c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // get assigned (in left-to-right order) for passing as follows...
2061c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2062c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov       it != ie; ++it) {
206399aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    unsigned neededInt, neededSSE;
2064edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2065edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                    neededSSE);
2066c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2067c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: If there are no registers available for any
2068c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte of an argument, the whole argument is passed on the
2069c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // stack. If registers have already been assigned for some
2070c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbytes of such an argument, the assignments get reverted.
207199aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
2072c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeIntRegs -= neededInt;
2073c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeSSERegs -= neededSSE;
2074c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else {
2075edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      it->info = getIndirectResult(it->type, freeIntRegs);
2076c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
2077c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2078c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2079c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2080c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2081c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        QualType Ty,
2082c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        CodeGenFunction &CGF) {
2083c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area_p =
2084c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2085c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area =
2086c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2087c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2088c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2089c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // byte boundary if alignment needed by type exceeds 8 byte boundary.
20908d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // It isn't stated explicitly in the standard, but in practice we use
20918d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // alignment greater than 16 where necessary.
2092c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2093c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Align > 8) {
20948d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
20950032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    llvm::Value *Offset =
20968d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman      llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
2097c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2098c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
209977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                                    CGF.Int64Ty);
21008d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
2101c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area =
2102c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2103c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 overflow_arg_area->getType(),
2104c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 "overflow_arg_area.align");
2105c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2106c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2107c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
21082acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2109c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Res =
2110c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateBitCast(overflow_arg_area,
211196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                              llvm::PointerType::getUnqual(LTy));
2112c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2113c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2114c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->overflow_arg_area + sizeof(type).
2115c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an 8 byte boundary.
2117c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
21190032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Offset =
212077b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
2121c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2122c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                            "overflow_arg_area.next");
2123c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2124c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2125c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2126c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Res;
2127c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2128c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2129c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2130c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
2131c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Assume that va_list type is correct; should be pointer to LLVM type:
2132c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // struct {
2133c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 gp_offset;
2134c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 fp_offset;
2135c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* overflow_arg_area;
2136c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* reg_save_area;
2137c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // };
213899aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned neededInt, neededSSE;
21398bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2140a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  Ty = CGF.getContext().getCanonicalType(Ty);
2141edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
2142c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2143c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2144c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in the registers. If not go to step 7.
2145c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!neededInt && !neededSSE)
2146c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2147c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2148c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2149c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // general purpose registers needed to pass type and num_fp to hold
2150c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the number of floating point registers needed.
2151c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2152c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2153c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2154c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset > 304 - num_fp * 16 go to step 7.
2155c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2156c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2157c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // register save space).
2158c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2159c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *InRegs = 0;
2160c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2161c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2162c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
2163c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2164c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
21651090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
21661090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
2167c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2168c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2169c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
2170c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2171c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2172c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FitsInFP =
21731090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
21741090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
2175c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2176c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2177c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2178c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2179c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2180c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2181c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2182c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2183c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in registers.
2184c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2185c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InRegBlock);
2186c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2187c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2188c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an offset of l->gp_offset and/or l->fp_offset. This may require
2189c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // copying to a temporary location in case the parameter is passed
2190c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in different register classes or requires an alignment greater
2191c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // than 8 for general purpose registers and 16 for XMM registers.
2192c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2193c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This really results in shameful code when we end up needing to
2194c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // collect arguments from different places; often what should result in a
2195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // simple assembling of a structure from scattered addresses has many more
2196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // loads than necessary. Can we clean this up?
21972acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *RegAddr =
2199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2200c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                           "reg_save_area");
2201c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt && neededSSE) {
2202c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: Cleanup.
2203800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
22042acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2205c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2206c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
22072acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyLo = ST->getElementType(0);
22082acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyHi = ST->getElementType(1);
2209a8b7a7d3eaa51dd200cba1e5541f2542d24d7a6eChris Lattner    assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
2210c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov           "Unexpected ABI info for mixed regs");
22112acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
22122acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
2213c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2214c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2215f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2216f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
2217c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *V =
2218c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2219c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2220c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2221c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2222c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2223a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson    RegAddr = CGF.Builder.CreateBitCast(Tmp,
222496e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2225c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else if (neededInt) {
2226c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2227c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
222896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2229dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner  } else if (neededSSE == 1) {
2230dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2231dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2232dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2233c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else {
2234dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    assert(neededSSE == 2 && "Invalid number of needed registers!");
2235dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // SSE registers are spaced 16 bytes apart in the register save
2236dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // area, we need to collect the two eightbytes together.
2237dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
22381090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
22398b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Type *DoubleTy = CGF.DoubleTy;
22402acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *DblPtrTy =
2241dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner      llvm::PointerType::getUnqual(DoubleTy);
22422acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = llvm::StructType::get(DoubleTy,
2243dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                       DoubleTy, NULL);
2244dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2245dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2246dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2247dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2248dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2249dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2250dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2251dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(Tmp,
2252dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2253c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2254c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2255c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 5. Set:
2256c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->gp_offset = l->gp_offset + num_gp * 8
2257c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset = l->fp_offset + num_fp * 16.
2258c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
225977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
2260c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2261c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            gp_offset_p);
2262c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2263c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
226477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
2265c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2266c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            fp_offset_p);
2267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBranch(ContBlock);
2269c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2270c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in memory.
2271c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2272c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InMemBlock);
2273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2275c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Return the appropriate result.
2276c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(ContBlock);
2278bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
2279c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                 "vaarg.addr");
2280c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(RegAddr, InRegBlock);
2281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(MemAddr, InMemBlock);
2282c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return ResAddr;
2283c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2284c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2285a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA TakumiABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2286a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2287a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isVoidType())
2288a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIgnore();
2289a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2290a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2291a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    Ty = EnumTy->getDecl()->getIntegerType();
2292a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2293a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  uint64_t Size = getContext().getTypeSize(Ty);
2294a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2295a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const RecordType *RT = Ty->getAs<RecordType>()) {
2296ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi    if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2297ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi        RT->getDecl()->hasFlexibleArrayMember())
2298a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2299a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
23006f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // FIXME: mingw-w64-gcc emits 128-bit struct as i128
23016f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    if (Size == 128 &&
230255fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman        getContext().getTargetInfo().getTriple().getOS()
230355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman          == llvm::Triple::MinGW32)
23046f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
23056f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi                                                          Size));
23066f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi
23076f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
23086f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // not 1, 2, 4, or 8 bytes, must be passed by reference."
23096f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    if (Size <= 64 &&
2310ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi        (Size & (Size - 1)) == 0)
2311a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2312a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi                                                          Size));
2313a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2314a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2315a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  }
2316a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2317a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isPromotableIntegerType())
2318a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getExtend();
2319a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2320a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  return ABIArgInfo::getDirect();
2321a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2322a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2323a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumivoid WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2324a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2325a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  QualType RetTy = FI.getReturnType();
2326a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  FI.getReturnInfo() = classify(RetTy);
2327a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2328a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2329a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi       it != ie; ++it)
2330a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    it->info = classify(it->type);
2331a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2332a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2333f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerllvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2334f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                      CodeGenFunction &CGF) const {
23358b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2336f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2337f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  CGBuilderTy &Builder = CGF.Builder;
2338f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2339f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                                       "ap");
2340f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2341f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Type *PTy =
2342f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2343f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2344f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2345f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  uint64_t Offset =
2346f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2347f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *NextAddr =
2348f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2349f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                      "ap.next");
2350f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2351dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2352f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  return AddrTyped;
2353f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner}
2354dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2355ec853ba1087f606e9685cb1e800616565ba35093John McCall// PowerPC-32
2356ec853ba1087f606e9685cb1e800616565ba35093John McCall
2357ec853ba1087f606e9685cb1e800616565ba35093John McCallnamespace {
2358ec853ba1087f606e9685cb1e800616565ba35093John McCallclass PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2359ec853ba1087f606e9685cb1e800616565ba35093John McCallpublic:
2360ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
23618bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2362ec853ba1087f606e9685cb1e800616565ba35093John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2363ec853ba1087f606e9685cb1e800616565ba35093John McCall    // This is recovered from gcc output.
2364ec853ba1087f606e9685cb1e800616565ba35093John McCall    return 1; // r1 is the dedicated stack pointer
2365ec853ba1087f606e9685cb1e800616565ba35093John McCall  }
2366ec853ba1087f606e9685cb1e800616565ba35093John McCall
2367ec853ba1087f606e9685cb1e800616565ba35093John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
23688bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
2369ec853ba1087f606e9685cb1e800616565ba35093John McCall};
2370ec853ba1087f606e9685cb1e800616565ba35093John McCall
2371ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2372ec853ba1087f606e9685cb1e800616565ba35093John McCall
2373ec853ba1087f606e9685cb1e800616565ba35093John McCallbool
2374ec853ba1087f606e9685cb1e800616565ba35093John McCallPPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2375ec853ba1087f606e9685cb1e800616565ba35093John McCall                                                llvm::Value *Address) const {
2376ec853ba1087f606e9685cb1e800616565ba35093John McCall  // This is calculated from the LLVM and GCC tables and verified
2377ec853ba1087f606e9685cb1e800616565ba35093John McCall  // against gcc output.  AFAIK all ABIs use the same encoding.
2378ec853ba1087f606e9685cb1e800616565ba35093John McCall
2379ec853ba1087f606e9685cb1e800616565ba35093John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
2380ec853ba1087f606e9685cb1e800616565ba35093John McCall
23818b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::IntegerType *i8 = CGF.Int8Ty;
2382ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2383ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2384ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2385ec853ba1087f606e9685cb1e800616565ba35093John McCall
2386ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 0-31: r0-31, the 4-byte general-purpose registers
2387aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 31);
2388ec853ba1087f606e9685cb1e800616565ba35093John McCall
2389ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 32-63: fp0-31, the 8-byte floating-point registers
2390aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2391ec853ba1087f606e9685cb1e800616565ba35093John McCall
2392ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64-76 are various 4-byte special-purpose registers:
2393ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64: mq
2394ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 65: lr
2395ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 66: ctr
2396ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 67: ap
2397ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 68-75 cr0-7
2398ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 76: xer
2399aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 64, 76);
2400ec853ba1087f606e9685cb1e800616565ba35093John McCall
2401ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 77-108: v0-31, the 16-byte vector registers
2402aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2403ec853ba1087f606e9685cb1e800616565ba35093John McCall
2404ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 109: vrsave
2405ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 110: vscr
2406ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 111: spe_acc
2407ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 112: spefscr
2408ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 113: sfp
2409aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 109, 113);
2410ec853ba1087f606e9685cb1e800616565ba35093John McCall
24118bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  return false;
2412ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2413ec853ba1087f606e9685cb1e800616565ba35093John McCall
2414ec853ba1087f606e9685cb1e800616565ba35093John McCall
2415dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
241634d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar// ARM ABI Implementation
2417dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
241834d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
241934d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbarnamespace {
242034d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
2421c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass ARMABIInfo : public ABIInfo {
24225e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
24235e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  enum ABIKind {
24245e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    APCS = 0,
24255e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS = 1,
24265e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS_VFP
24275e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  };
24285e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
24295e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
24305e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind Kind;
24315e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
24325e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
2433ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
24345e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
243549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  bool isEABI() const {
243655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    StringRef Env =
243755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      getContext().getTargetInfo().getTriple().getEnvironmentName();
2438b43550bf1bd944a16cdae9703cb1c2049b04e6bdChandler Carruth    return (Env == "gnueabi" || Env == "eabi" || Env == "androideabi");
243949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
244049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
24415e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
24425e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind getABIKind() const { return Kind; }
24435e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
2444a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
2445a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
2446c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2447ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
2448c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2449c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2450c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
2451c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
2452c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
245382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass ARMTargetCodeGenInfo : public TargetCodeGenInfo {
245482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
2455ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2456ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
24576374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
245849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  const ARMABIInfo &getABIInfo() const {
245949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
246049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
246149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
24626374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
24636374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 13;
24646374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
246509345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
24665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef getARCRetainAutoreleasedReturnValueMarker() const {
2467f85e193739c953358c865005855253af4f68a497John McCall    return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2468f85e193739c953358c865005855253af4f68a497John McCall  }
2469f85e193739c953358c865005855253af4f68a497John McCall
247009345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
247109345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky                               llvm::Value *Address) const {
24728b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
247309345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
247409345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    // 0-15 are the 16 integer registers.
24758b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
247609345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    return false;
247709345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  }
247849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
247949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
248049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    if (getABIInfo().isEABI()) return 88;
248149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return TargetCodeGenInfo::getSizeOfUnwindException();
248249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
248382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
248482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
248534d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar}
248634d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
2487ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
2488a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2489c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2490a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner       it != ie; ++it)
2491a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    it->info = classifyArgumentType(it->type);
24925e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
2493414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // Always honor user-specified calling convention.
2494414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  if (FI.getCallingConvention() != llvm::CallingConv::C)
2495414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return;
2496414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov
2497414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // Calling convention as default by an ABI.
249825117ab35c1a033846073183314c68ef07d1701aRafael Espindola  llvm::CallingConv::ID DefaultCC;
249949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  if (isEABI())
250025117ab35c1a033846073183314c68ef07d1701aRafael Espindola    DefaultCC = llvm::CallingConv::ARM_AAPCS;
25011ed1a594e9befc91ebf00d81b41a2fdfab862657Rafael Espindola  else
25021ed1a594e9befc91ebf00d81b41a2fdfab862657Rafael Espindola    DefaultCC = llvm::CallingConv::ARM_APCS;
250325117ab35c1a033846073183314c68ef07d1701aRafael Espindola
2504414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // If user did not ask for specific calling convention explicitly (e.g. via
2505414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // pcs attribute), set effective calling convention if it's different than ABI
2506414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // default.
25075e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  switch (getABIKind()) {
25085e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case APCS:
250925117ab35c1a033846073183314c68ef07d1701aRafael Espindola    if (DefaultCC != llvm::CallingConv::ARM_APCS)
251025117ab35c1a033846073183314c68ef07d1701aRafael Espindola      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
25115e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
25125e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case AAPCS:
251325117ab35c1a033846073183314c68ef07d1701aRafael Espindola    if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
251425117ab35c1a033846073183314c68ef07d1701aRafael Espindola      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
25155e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
25165e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case AAPCS_VFP:
2517414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2518414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
25195e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
25205e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  }
2521c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2522c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2523194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2524194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// aggregate.  If HAMembers is non-null, the number of base elements
2525194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// contained in the type is returned through it; this is used for the
2526194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// recursive calls that check aggregate component types.
2527194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilsonstatic bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2528194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   ASTContext &Context,
2529194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   uint64_t *HAMembers = 0) {
2530194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  uint64_t Members;
2531194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2532194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2533194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2534194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members *= AT->getSize().getZExtValue();
2535194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2536194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const RecordDecl *RD = RT->getDecl();
2537194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (RD->isUnion() || RD->hasFlexibleArrayMember())
2538194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2539194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2540194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (!CXXRD->isAggregate())
2541194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2542194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2543194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 0;
2544194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2545194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         i != e; ++i) {
2546194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      const FieldDecl *FD = *i;
2547194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      uint64_t FldMembers;
2548194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2549194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2550194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Members += FldMembers;
2551194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2552194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
2553194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 1;
2554194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2555194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Members = 2;
2556194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Ty = CT->getElementType();
2557194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2558194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2559194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2560194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // double, or 64-bit or 128-bit vectors.
2561194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2562194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (BT->getKind() != BuiltinType::Float &&
2563194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson          BT->getKind() != BuiltinType::Double)
2564194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2565194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2566194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      unsigned VecSize = Context.getTypeSize(VT);
2567194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (VecSize != 64 && VecSize != 128)
2568194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2569194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else {
2570194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2571194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2572194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2573194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // The base type must be the same for all members.  Vector types of the
2574194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // same total size are treated as being equivalent here.
2575194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *TyPtr = Ty.getTypePtr();
2576194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!Base)
2577194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Base = TyPtr;
2578194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (Base != TyPtr &&
2579194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        (!Base->isVectorType() || !TyPtr->isVectorType() ||
2580194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2581194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2582194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
2583194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2584194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  // Homogeneous Aggregates can have at most 4 members of the base type.
2585194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (HAMembers)
2586194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    *HAMembers = Members;
2587194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  return (Members <= 4);
2588194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson}
2589194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2590a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
2591d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
2592aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
2593aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2594aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
2595aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
2596cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
2597cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2598aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
259998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2600420255710694e958fa04bed1d80d96508949879eDaniel Dunbar  // Ignore empty records.
2601a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), Ty, true))
2602420255710694e958fa04bed1d80d96508949879eDaniel Dunbar    return ABIArgInfo::getIgnore();
2603420255710694e958fa04bed1d80d96508949879eDaniel Dunbar
26040eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // Structures with either a non-trivial destructor or a non-trivial
26050eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // copy constructor are always indirect.
26060eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
26070eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
26080eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
2609194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2610194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // Homogeneous Aggregates need to be expanded.
2611194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *Base = 0;
2612194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (isHomogeneousAggregate(Ty, Base, getContext()))
2613194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return ABIArgInfo::getExpand();
2614194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
2615194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
26168aa87c71d9bfec14e135c683b0d7b9de999dbcb0Daniel Dunbar  // Otherwise, pass by coercing to a structure of the appropriate size.
26178aa87c71d9bfec14e135c683b0d7b9de999dbcb0Daniel Dunbar  //
261853fc1a6151ec31350309f479c0d2252366e4815cBob Wilson  // FIXME: This is kind of nasty... but there isn't much choice because the ARM
261953fc1a6151ec31350309f479c0d2252366e4815cBob Wilson  // backend doesn't support byval.
2620c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This doesn't handle alignment > 64 bits.
26212acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type* ElemTy;
2622c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  unsigned SizeRegs;
262353fc1a6151ec31350309f479c0d2252366e4815cBob Wilson  if (getContext().getTypeAlign(Ty) > 32) {
262467d097e1232b7d66f58989c16a45b8a11721f76eStuart Hastings    ElemTy = llvm::Type::getInt64Ty(getVMContext());
262567d097e1232b7d66f58989c16a45b8a11721f76eStuart Hastings    SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
262653fc1a6151ec31350309f479c0d2252366e4815cBob Wilson  } else {
262753fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    ElemTy = llvm::Type::getInt32Ty(getVMContext());
262853fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
2629c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2630b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings
26319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *STy =
26327650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
2633b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings  return ABIArgInfo::getDirect(STy);
2634c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2635c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2636a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattnerstatic bool isIntegerLikeType(QualType Ty, ASTContext &Context,
263798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar                              llvm::LLVMContext &VMContext) {
263898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
263998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // is called integer-like if its size is less than or equal to one word, and
264098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // the offset of each of its addressable sub-fields is zero.
264198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
264298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  uint64_t Size = Context.getTypeSize(Ty);
264398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
264498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that the type fits in a word.
264598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Size > 32)
264698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
264798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
264898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // FIXME: Handle vector types!
264998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Ty->isVectorType())
265098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
265198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2652b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  // Float types are never treated as "integer like".
2653b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  if (Ty->isRealFloatingType())
2654b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar    return false;
2655b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar
265698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // If this is a builtin or pointer type then it is ok.
2657183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
265898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return true;
265998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
26604581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  // Small complex integer types are "integer like".
26614581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  if (const ComplexType *CT = Ty->getAs<ComplexType>())
26624581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar    return isIntegerLikeType(CT->getElementType(), Context, VMContext);
266398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
266498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Single element and zero sized arrays should be allowed, by the definition
266598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // above, but they are not.
266698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
266798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise, it must be a record type.
266898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
266998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (!RT) return false;
267098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
267198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Ignore records with flexible arrays.
267298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
267398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RD->hasFlexibleArrayMember())
267498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
267598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
267698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that all sub-fields are at offset 0, and are themselves "integer
267798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // like".
267898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
267998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
268098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  bool HadField = false;
268198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  unsigned idx = 0;
268298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
268398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar       i != e; ++i, ++idx) {
268498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    const FieldDecl *FD = *i;
268598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2686679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Bit-fields are not addressable, we only need to verify they are "integer
2687679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // like". We still have to disallow a subsequent non-bitfield, for example:
2688679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    //   struct { int : 0; int x }
2689679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // is non-integer like according to gcc.
2690679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (FD->isBitField()) {
2691679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!RD->isUnion())
2692679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        HadField = true;
2693679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
2694679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2695679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        return false;
269698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2697679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      continue;
269898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
269998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2700679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Check if this field is at offset 0.
2701679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (Layout.getFieldOffset(idx) != 0)
2702679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      return false;
2703679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
270498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!isIntegerLikeType(FD->getType(), Context, VMContext))
270598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return false;
27068bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2707679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Only allow at most one field in a structure. This doesn't match the
2708679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // wording above, but follows gcc in situations with a field following an
2709679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // empty structure.
271098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!RD->isUnion()) {
271198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (HadField)
271298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar        return false;
271398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
271498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      HadField = true;
271598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
271698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  }
271798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
271898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return true;
271998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar}
272098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2721a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
272298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RetTy->isVoidType())
2723c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
272498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2725f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  // Large vector types should be returned via memory.
2726f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2727f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar    return ABIArgInfo::getIndirect(0);
2728f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar
2729d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(RetTy)) {
2730aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
2731aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2732aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      RetTy = EnumTy->getDecl()->getIntegerType();
2733aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
2734cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (RetTy->isPromotableIntegerType() ?
2735cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2736aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
273798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
27380eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // Structures with either a non-trivial destructor or a non-trivial
27390eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // copy constructor are always indirect.
27400eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
27410eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
27420eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
274398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Are we following APCS?
274498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (getABIKind() == APCS) {
2745a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isEmptyRecord(getContext(), RetTy, false))
274698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return ABIArgInfo::getIgnore();
274798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
27484cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // Complex types are all returned as packed integers.
27494cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    //
27504cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // FIXME: Consider using 2 x vector types if the back end handles them
27514cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // correctly.
27524cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    if (RetTy->isAnyComplexType())
2753800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2754a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                              getContext().getTypeSize(RetTy)));
27554cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar
275698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Integer like structures are returned in r0.
2757a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
275898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      // Return in the smallest viable integer type.
2759a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
276098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 8)
2761800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
276298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 16)
2763800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2764800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
276598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
276698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
276798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Otherwise return in memory.
276898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return ABIArgInfo::getIndirect(0);
2769c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
277098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
277198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise this is an AAPCS variant.
277298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2773a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), RetTy, true))
277416a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    return ABIArgInfo::getIgnore();
277516a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
27763b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  // Check for homogeneous aggregates with AAPCS-VFP.
27773b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  if (getABIKind() == AAPCS_VFP) {
27783b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson    const Type *Base = 0;
27793b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson    if (isHomogeneousAggregate(RetTy, Base, getContext()))
27803b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      // Homogeneous Aggregates are returned directly.
27813b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      return ABIArgInfo::getDirect();
27823b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  }
27833b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson
278498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Aggregates <= 4 bytes are returned in r0; other aggregates
278598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // are returned indirectly.
2786a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  uint64_t Size = getContext().getTypeSize(RetTy);
278716a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  if (Size <= 32) {
278816a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    // Return in the smallest viable integer type.
278916a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 8)
2790800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
279116a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 16)
2792800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2793800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
279416a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  }
279516a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
279698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return ABIArgInfo::getIndirect(0);
2797c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2798c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2799c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
280077b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                   CodeGenFunction &CGF) const {
28018b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
28028b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2803c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2804c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
28058b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
2806c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2807e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  // Handle address alignment for type alignment > 32 bits
2808e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
2809e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  if (TyAlign > 4) {
2810e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    assert((TyAlign & (TyAlign - 1)) == 0 &&
2811e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola           "Alignment is not power of 2!");
2812e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
2813e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
2814e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
2815e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
2816e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  }
2817c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Type *PTy =
281896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2819c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2820c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2821c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
2822c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
2823c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
282477b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2825c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
2826c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2827c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2828c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
2829c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2830c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2831dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
28320259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski// PTX ABI Implementation
28330259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
28340259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28350259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskinamespace {
28360259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28370259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskiclass PTXABIInfo : public ABIInfo {
28380259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
28390259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
28400259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28410259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyReturnType(QualType RetTy) const;
28420259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyArgumentType(QualType Ty) const;
28430259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28440259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual void computeInfo(CGFunctionInfo &FI) const;
28450259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
28460259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski                                 CodeGenFunction &CFG) const;
28470259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
28480259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28490259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskiclass PTXTargetCodeGenInfo : public TargetCodeGenInfo {
28500259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
28510259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  PTXTargetCodeGenInfo(CodeGenTypes &CGT)
28520259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    : TargetCodeGenInfo(new PTXABIInfo(CGT)) {}
2853818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
28542f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
28552f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
28560259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
28570259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28580259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin HolewinskiABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const {
28590259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (RetTy->isVoidType())
28600259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIgnore();
28610259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(RetTy))
28620259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
28630259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
28640259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
28650259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28660259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin HolewinskiABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const {
28670259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(Ty))
28680259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
28690259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28700259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
28710259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
28720259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28730259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskivoid PTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
28740259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
28750259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
28760259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski       it != ie; ++it)
28770259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    it->info = classifyArgumentType(it->type);
28780259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28790259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  // Always honor user-specified calling convention.
28800259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (FI.getCallingConvention() != llvm::CallingConv::C)
28810259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return;
28820259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
28830259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  // Calling convention as default by an ABI.
28840259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  llvm::CallingConv::ID DefaultCC;
2885744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  const LangOptions &LangOpts = getContext().getLangOptions();
2886744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  if (LangOpts.OpenCL || LangOpts.CUDA) {
2887744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // If we are in OpenCL or CUDA mode, then default to device functions
28880259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    DefaultCC = llvm::CallingConv::PTX_Device;
2889818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  } else {
2890818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // If we are in standard C/C++ mode, use the triple to decide on the default
2891818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    StringRef Env =
2892818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      getContext().getTargetInfo().getTriple().getEnvironmentName();
2893818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    if (Env == "device")
2894818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      DefaultCC = llvm::CallingConv::PTX_Device;
2895818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    else
2896818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      DefaultCC = llvm::CallingConv::PTX_Kernel;
2897818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  }
28980259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  FI.setEffectiveCallingConvention(DefaultCC);
2899818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
29000259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
29010259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
29020259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskillvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
29030259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski                                   CodeGenFunction &CFG) const {
29040259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  llvm_unreachable("PTX does not support varargs");
29050259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
29060259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
2907818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinskivoid PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
2908818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski                                               llvm::GlobalValue *GV,
2909818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski                                               CodeGen::CodeGenModule &M) const{
2910818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
2911818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  if (!FD) return;
2912818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
2913818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  llvm::Function *F = cast<llvm::Function>(GV);
2914818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
2915818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  // Perform special handling in OpenCL mode
2916744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  if (M.getLangOptions().OpenCL) {
2917818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // Use OpenCL function attributes to set proper calling conventions
2918818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // By default, all functions are device functions
2919818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    if (FD->hasAttr<OpenCLKernelAttr>()) {
2920818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      // OpenCL __kernel functions get a kernel calling convention
2921744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne      F->setCallingConv(llvm::CallingConv::PTX_Kernel);
2922818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      // And kernel functions are not subject to inlining
2923818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      F->addFnAttr(llvm::Attribute::NoInline);
2924818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    }
2925744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  }
2926818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
2927744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  // Perform special handling in CUDA mode.
2928744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  if (M.getLangOptions().CUDA) {
2929744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // CUDA __global__ functions get a kernel calling convention.  Since
2930744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // __global__ functions cannot be called from the device, we do not
2931744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // need to set the noinline attribute.
2932744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    if (FD->getAttr<CUDAGlobalAttr>())
2933744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne      F->setCallingConv(llvm::CallingConv::PTX_Kernel);
2934818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  }
2935818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski}
2936818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
29370259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
29380259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
29390259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
2940276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck// MBlaze ABI Implementation
2941276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
2942276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2943276fdf408050d205f3a7f34c1e788224a67d2098Wesley Pecknamespace {
2944276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2945276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeABIInfo : public ABIInfo {
2946276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
2947276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
2948276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2949276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  bool isPromotableIntegerType(QualType Ty) const;
2950276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2951276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyReturnType(QualType RetTy) const;
2952276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyArgumentType(QualType RetTy) const;
2953276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2954276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual void computeInfo(CGFunctionInfo &FI) const {
2955276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2956276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2957276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck         it != ie; ++it)
2958276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      it->info = classifyArgumentType(it->type);
2959276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
2960276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2961276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2962276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                 CodeGenFunction &CGF) const;
2963276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
2964276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2965276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
2966276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
2967276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
2968276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
2969276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
2970276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                           CodeGen::CodeGenModule &M) const;
2971276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
2972276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2973276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
2974276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2975276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckbool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
2976276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
2977276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
2978276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    switch (BT->getKind()) {
2979276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Bool:
2980276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_S:
2981276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_U:
2982276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::SChar:
2983276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UChar:
2984276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Short:
2985276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UShort:
2986276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return true;
2987276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    default:
2988276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return false;
2989276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    }
2990276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return false;
2991276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
2992276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2993276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckllvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2994276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                      CodeGenFunction &CGF) const {
2995276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // FIXME: Implement
2996276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return 0;
2997276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
2998276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
2999276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3000276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3001276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (RetTy->isVoidType())
3002276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIgnore();
3003276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(RetTy))
3004276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
3005276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3006276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(RetTy) ?
3007276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3008276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3009276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3010276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3011276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(Ty))
3012276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
3013276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3014276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(Ty) ?
3015276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3016276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3017276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3018276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckvoid MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3019276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  llvm::GlobalValue *GV,
3020276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  CodeGen::CodeGenModule &M)
3021276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  const {
3022276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3023276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (!FD) return;
3024125b4cb35536e45201f8f2cb19ee620e3ad67c49NAKAMURA Takumi
3025276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  llvm::CallingConv::ID CC = llvm::CallingConv::C;
3026276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3027276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_INTR;
3028276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3029276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_SVOL;
3030276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3031276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC != llvm::CallingConv::C) {
3032276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Handle 'interrupt_handler' attribute:
3033276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      llvm::Function *F = cast<llvm::Function>(GV);
3034276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3035276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 1: Set ISR calling convention.
3036276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      F->setCallingConv(CC);
3037276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3038276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 2: Add attributes goodness.
3039276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      F->addFnAttr(llvm::Attribute::NoInline);
3040276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
3041276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3042276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // Step 3: Emit _interrupt_handler alias.
3043276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC == llvm::CallingConv::MBLAZE_INTR)
3044276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3045276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                          "_interrupt_handler", GV, &M.getModule());
3046276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3047276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3048276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3049276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
305082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov// MSP430 ABI Implementation
3051dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
305282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
305382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovnamespace {
305482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
305582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
305682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
3057ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3058ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
305982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
306082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                           CodeGen::CodeGenModule &M) const;
306182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
306282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
3063c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3064c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
306582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovvoid MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
306682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                                  llvm::GlobalValue *GV,
306782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                             CodeGen::CodeGenModule &M) const {
306882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
306982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
307082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Handle 'interrupt' attribute:
307182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      llvm::Function *F = cast<llvm::Function>(GV);
307282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
307382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 1: Set ISR calling convention.
307482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      F->setCallingConv(llvm::CallingConv::MSP430_INTR);
307582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
307682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 2: Add attributes goodness.
307782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      F->addFnAttr(llvm::Attribute::NoInline);
307882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
307982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 3: Emit ISR vector alias.
308082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      unsigned Num = attr->getNumber() + 0xffe0;
308182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
30825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            "vector_" + Twine::utohexstr(Num),
308382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                            GV, &M.getModule());
308482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    }
308582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  }
3086c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3087c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3088dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
3089aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// MIPS ABI Implementation.  This works for both little-endian and
3090aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// big-endian variants.
3091dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
3092dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
3093aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallnamespace {
3094619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakaclass MipsABIInfo : public ABIInfo {
3095c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  bool IsO32;
3096b551dd31f6b15aa959127ee906084fcf5bf0154eAkira Hatanaka  unsigned MinABIStackAlignInBytes;
30976d1080fd1851f18bd40bb46fa074aa1252b13e8eAkira Hatanaka  llvm::Type* HandleAggregates(QualType Ty) const;
3098c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
3099a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
3100619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakapublic:
3101b551dd31f6b15aa959127ee906084fcf5bf0154eAkira Hatanaka  MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3102b551dd31f6b15aa959127ee906084fcf5bf0154eAkira Hatanaka    ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8) {}
3103619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3104619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  ABIArgInfo classifyReturnType(QualType RetTy) const;
3105f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka  ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
3106619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual void computeInfo(CGFunctionInfo &FI) const;
3107619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3108619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                 CodeGenFunction &CGF) const;
3109619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka};
3110619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3111aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallclass MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
3112e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka  unsigned SizeOfUnwindException;
3113aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallpublic:
3114c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3115c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3116c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka      SizeOfUnwindException(IsO32 ? 24 : 32) {}
3117aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3118aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3119aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    return 29;
3120aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
3121aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3122aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
31238bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
312449e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
312549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
3126e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka    return SizeOfUnwindException;
312749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
3128aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall};
3129aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
3130aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3131d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// In N32/64, an aligned double precision floating point field is passed in
3132d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// a register.
31336d1080fd1851f18bd40bb46fa074aa1252b13e8eAkira Hatanakallvm::Type* MipsABIInfo::HandleAggregates(QualType Ty) const {
3134d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  if (IsO32)
3135d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    return 0;
3136d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
31372afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka  if (Ty->isComplexType())
31382afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka    return CGT.ConvertType(Ty);
31396d1080fd1851f18bd40bb46fa074aa1252b13e8eAkira Hatanaka
3140a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  const RecordType *RT = Ty->getAs<RecordType>();
3141d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3142a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // Unions are passed in integer registers.
3143a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  if (!RT || !RT->isStructureOrClassType())
3144d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    return 0;
3145d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3146d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const RecordDecl *RD = RT->getDecl();
3147d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3148d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  uint64_t StructSize = getContext().getTypeSize(Ty);
3149d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  assert(!(StructSize % 8) && "Size of structure must be multiple of 8.");
3150d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3151d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  uint64_t LastOffset = 0;
3152d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  unsigned idx = 0;
3153d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
31542afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka  SmallVector<llvm::Type*, 8> ArgList;
3155d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3156a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // Iterate over fields in the struct/class and check if there are any aligned
3157a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // double fields.
3158d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3159d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka       i != e; ++i, ++idx) {
3160d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    const QualType Ty = (*i)->getType();
3161d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    const BuiltinType *BT = Ty->getAs<BuiltinType>();
3162d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3163d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (!BT || BT->getKind() != BuiltinType::Double)
3164d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
3165d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3166d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    uint64_t Offset = Layout.getFieldOffset(idx);
3167d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (Offset % 64) // Ignore doubles that are not aligned.
3168d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
3169d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3170d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add ((Offset - LastOffset) / 64) args of type i64.
3171d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3172d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      ArgList.push_back(I64);
3173d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3174d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add double type.
3175d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3176d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    LastOffset = Offset + 64;
3177d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  }
3178d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3179a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // This struct/class doesn't have an aligned double field.
3180d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  if (!LastOffset)
3181d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    return 0;
3182d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3183d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  // Add ((StructSize - LastOffset) / 64) args of type i64.
3184d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  for (unsigned N = (StructSize - LastOffset) / 64; N; --N)
3185d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    ArgList.push_back(I64);
3186d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3187b49d5a6606ec9b912f74279c935934c4ecb39dfaAkira Hatanaka  // If the size of the remainder is not zero, add one more integer type to
3188b49d5a6606ec9b912f74279c935934c4ecb39dfaAkira Hatanaka  // ArgList.
3189d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  unsigned R = (StructSize - LastOffset) % 64;
3190b49d5a6606ec9b912f74279c935934c4ecb39dfaAkira Hatanaka  if (R)
3191b49d5a6606ec9b912f74279c935934c4ecb39dfaAkira Hatanaka    ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
3192d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3193d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  return llvm::StructType::get(getVMContext(), ArgList);
3194d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka}
3195d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3196a33fd393d5255716e904fed021f87260095ed00aAkira Hatanakallvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
3197a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  // Padding is inserted only for N32/64.
3198a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  if (IsO32)
3199a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    return 0;
3200a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3201a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  assert(Align <= 16 && "Alignment larger than 16 not handled.");
3202a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  return (Align == 16 && Offset & 0xf) ?
3203a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    llvm::IntegerType::get(getVMContext(), 64) : 0;
3204a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka}
32059659d59ec368933050684af573b6d32ab5714332Akira Hatanaka
3206f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaABIArgInfo
3207f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaMipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
3208a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t OrigOffset = Offset;
3209a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t TySize =
3210a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    llvm::RoundUpToAlignment(getContext().getTypeSize(Ty), 64) / 8;
3211a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t Align = getContext().getTypeAlign(Ty) / 8;
3212a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  Offset = llvm::RoundUpToAlignment(Offset, std::max(Align, (uint64_t)8));
3213a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  Offset += TySize;
3214a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3215619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (isAggregateTypeForABI(Ty)) {
3216619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    // Ignore empty aggregates.
3217f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    if (TySize == 0)
3218619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka      return ABIArgInfo::getIgnore();
3219619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3220511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka    // Records with non trivial destructors/constructors should not be passed
3221511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka    // by value.
3222f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
3223a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka      Offset = OrigOffset + 8;
3224511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3225f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    }
3226511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka
3227f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    // If we have reached here, aggregates are passed either indirectly via a
3228f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    // byval pointer or directly by coercing to another structure type. In the
3229f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    // latter case, padding is inserted if the offset of the aggregate is
3230f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    // unaligned.
32316d1080fd1851f18bd40bb46fa074aa1252b13e8eAkira Hatanaka    llvm::Type *ResType = HandleAggregates(Ty);
32329659d59ec368933050684af573b6d32ab5714332Akira Hatanaka
3233a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    if (!ResType)
3234a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka      return ABIArgInfo::getIndirect(0);
3235a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3236a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    return ABIArgInfo::getDirect(ResType, 0, getPaddingType(Align, OrigOffset));
3237619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
3238619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3239619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
3240619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3241619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    Ty = EnumTy->getDecl()->getIntegerType();
3242619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3243a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  if (Ty->isPromotableIntegerType())
3244a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    return ABIArgInfo::getExtend();
3245a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3246a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
3247619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3248619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3249c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanakallvm::Type*
3250c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira HatanakaMipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
3251da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  const RecordType *RT = RetTy->getAs<RecordType>();
3252c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  SmallVector<llvm::Type*, 2> RTList;
3253c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3254da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  if (RT && RT->isStructureOrClassType()) {
3255c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    const RecordDecl *RD = RT->getDecl();
3256da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3257da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    unsigned FieldCnt = Layout.getFieldCount();
3258da54ff306270e179f64d046369419724356d30d7Akira Hatanaka
3259da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // N32/64 returns struct/classes in floating point registers if the
3260da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // following conditions are met:
3261da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 1. The size of the struct/class is no larger than 128-bit.
3262da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 2. The struct/class has one or two fields all of which are floating
3263da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //    point types.
3264da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 3. The offset of the first field is zero (this follows what gcc does).
3265da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
3266da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // Any other composite results are returned in integer registers.
3267da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
3268da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3269da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3270da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      for (; b != e; ++b) {
3271da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        const BuiltinType *BT = (*b)->getType()->getAs<BuiltinType>();
3272c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3273da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        if (!BT || !BT->isFloatingPoint())
3274da54ff306270e179f64d046369419724356d30d7Akira Hatanaka          break;
3275c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3276da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        RTList.push_back(CGT.ConvertType((*b)->getType()));
3277da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      }
3278c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3279da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      if (b == e)
3280da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        return llvm::StructType::get(getVMContext(), RTList,
3281da54ff306270e179f64d046369419724356d30d7Akira Hatanaka                                     RD->hasAttr<PackedAttr>());
3282c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3283da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RTList.clear();
3284da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    }
3285c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  }
3286c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3287c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  RTList.push_back(llvm::IntegerType::get(getVMContext(),
3288c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka                                          std::min(Size, (uint64_t)64)));
3289c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  if (Size > 64)
3290c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    RTList.push_back(llvm::IntegerType::get(getVMContext(), Size - 64));
3291c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3292c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  return llvm::StructType::get(getVMContext(), RTList);
3293c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka}
3294c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3295619e8875d29cc019c7360595f66b9f91b3439494Akira HatanakaABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3296a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  uint64_t Size = getContext().getTypeSize(RetTy);
3297a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka
3298a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  if (RetTy->isVoidType() || Size == 0)
3299619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIgnore();
3300619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3301619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (isAggregateTypeForABI(RetTy)) {
3302c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    if (Size <= 128) {
3303c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka      if (RetTy->isAnyComplexType())
3304c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect();
3305c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3306526cdfb2bf51c8c6612504f1d06c02c736d3d126Akira Hatanaka      if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3307c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3308c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    }
3309619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3310619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIndirect(0);
3311619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
3312619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3313619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
3314619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3315619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    RetTy = EnumTy->getDecl()->getIntegerType();
3316619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3317619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  return (RetTy->isPromotableIntegerType() ?
3318619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3319619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3320619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3321619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakavoid MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3322cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  ABIArgInfo &RetInfo = FI.getReturnInfo();
3323cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  RetInfo = classifyReturnType(FI.getReturnType());
3324cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
3325cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  // Check if a pointer to an aggregate is passed as a hidden argument.
3326cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  uint64_t Offset = RetInfo.isIndirect() ? 8 : 0;
3327cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
3328619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3329619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka       it != ie; ++it)
3330f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    it->info = classifyArgumentType(it->type, Offset);
3331619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3332619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3333619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakallvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3334619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                    CodeGenFunction &CGF) const {
33358b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
33368b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
3337c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3338c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  CGBuilderTy &Builder = CGF.Builder;
3339c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3340c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
33418f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
3342c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3343c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AddrTyped;
33448f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
33458f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
3346c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3347c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  if (TypeAlign > MinABIStackAlignInBytes) {
33488f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
33498f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
33508f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
33518f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
3352c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3353c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3354c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  }
3355c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  else
3356c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = Builder.CreateBitCast(Addr, PTy);
3357c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3358c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
33598f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
3360c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  uint64_t Offset =
3361c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3362c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *NextAddr =
33638f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
3364c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka                      "ap.next");
3365c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3366c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3367c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  return AddrTyped;
3368619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3369619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3370aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallbool
3371aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallMIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3372aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                                               llvm::Value *Address) const {
3373aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // This information comes from gcc's implementation, which seems to
3374aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // as canonical as it gets.
3375aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3376aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // Everything on MIPS is 4 bytes.  Double-precision FP registers
3377aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // are aliased to pairs of single-precision FP registers.
33788b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
3379aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3380aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 0-31 are the general purpose registers, $0 - $31.
3381aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 32-63 are the floating-point registers, $f0 - $f31.
3382aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 64 and 65 are the multiply/divide registers, $hi and $lo.
3383aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 66 is the (notional, I think) register for signal-handler return.
33848b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
3385aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3386aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3387aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // They are one bit wide and ignored here.
3388aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3389aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3390aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // (coprocessor 1 is the FP unit)
3391aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3392aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3393aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 176-181 are the DSP accumulator registers.
33948b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
3395aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  return false;
3396aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
3397aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
33982f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
33992f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
34002f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// Currently subclassed only to implement custom OpenCL C function attribute
34012f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// handling.
34022f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
34032f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34042f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournenamespace {
34052f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34062f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourneclass TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
34072f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournepublic:
34082f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  TCETargetCodeGenInfo(CodeGenTypes &CGT)
34092f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    : DefaultTargetCodeGenInfo(CGT) {}
34102f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34112f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
34122f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
34132f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne};
34142f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34152f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournevoid TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
34162f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               llvm::GlobalValue *GV,
34172f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               CodeGen::CodeGenModule &M) const {
34182f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
34192f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  if (!FD) return;
34202f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34212f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  llvm::Function *F = cast<llvm::Function>(GV);
34222f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34232f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  if (M.getLangOptions().OpenCL) {
34242f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    if (FD->hasAttr<OpenCLKernelAttr>()) {
34252f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      // OpenCL C Kernel functions are not subject to inlining
34262f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      F->addFnAttr(llvm::Attribute::NoInline);
34272f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34282f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
34292f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34302f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Convert the reqd_work_group_size() attributes to metadata.
34312f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::LLVMContext &Context = F->getContext();
34322f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::NamedMDNode *OpenCLMetadata =
34332f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne            M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
34342f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34352f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        SmallVector<llvm::Value*, 5> Operands;
34362f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        Operands.push_back(F);
34372f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34388b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
34398b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
34408b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
34418b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
34428b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
34432f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
34448b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
34458b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
34462f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
34472f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34482f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Add a boolean constant operand for "required" (true) or "hint" (false)
34492f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // for implementing the work_group_size_hint attr later. Currently
34502f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // always true as the hint is not yet implemented.
34518b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::ConstantInt::getTrue(Context));
34522f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
34532f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      }
34542f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    }
34552f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  }
34562f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
34572f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
34582f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
3459aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
34609631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
34619631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum// Hexagon ABI Implementation
34629631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
34639631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34649631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumnamespace {
34659631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34669631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonABIInfo : public ABIInfo {
34679631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34689631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34699631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
34709631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
34719631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34729631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumprivate:
34739631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34749631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyReturnType(QualType RetTy) const;
34759631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyArgumentType(QualType RetTy) const;
34769631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34779631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual void computeInfo(CGFunctionInfo &FI) const;
34789631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34799631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
34809631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                 CodeGenFunction &CGF) const;
34819631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
34829631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34839631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
34849631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
34859631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
34869631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
34879631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34889631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
34899631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return 29;
34909631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
34919631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
34929631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34939631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
34949631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
34959631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumvoid HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
34969631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
34979631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
34989631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum       it != ie; ++it)
34999631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    it->info = classifyArgumentType(it->type);
35009631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
35019631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35029631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
35039631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(Ty)) {
35049631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
35059631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
35069631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      Ty = EnumTy->getDecl()->getIntegerType();
35079631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35089631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (Ty->isPromotableIntegerType() ?
35099631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
35109631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
35119631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35129631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Ignore empty records.
35139631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), Ty, true))
35149631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
35159631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35169631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Structures with either a non-trivial destructor or a non-trivial
35179631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // copy constructor are always indirect.
35189631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
35199631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
35209631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35219631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(Ty);
35229631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size > 64)
35239631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
35249631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Pass in the smallest viable integer type.
35259631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 32)
35269631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
35279631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 16)
35289631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
35299631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 8)
35309631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
35319631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else
35329631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
35339631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
35349631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35359631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
35369631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVoidType())
35379631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
35389631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35399631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Large vector types should be returned via memory.
35409631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
35419631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0);
35429631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35439631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(RetTy)) {
35449631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
35459631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
35469631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      RetTy = EnumTy->getDecl()->getIntegerType();
35479631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35489631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (RetTy->isPromotableIntegerType() ?
35499631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
35509631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
35519631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35529631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Structures with either a non-trivial destructor or a non-trivial
35539631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // copy constructor are always indirect.
35549631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
35559631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
35569631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35579631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), RetTy, true))
35589631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
35599631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35609631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Aggregates <= 8 bytes are returned in r0; other aggregates
35619631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // are returned indirectly.
35629631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(RetTy);
35639631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size <= 64) {
35649631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Return in the smallest viable integer type.
35659631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 8)
35669631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
35679631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 16)
35689631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
35699631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 32)
35709631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
35719631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
35729631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
35739631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35749631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
35759631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
35769631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35779631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumllvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
35788b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                                       CodeGenFunction &CGF) const {
35799631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // FIXME: Need to handle alignment
35808b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
35819631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35829631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  CGBuilderTy &Builder = CGF.Builder;
35839631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
35849631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                                       "ap");
35859631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
35869631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Type *PTy =
35879631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
35889631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
35899631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35909631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Offset =
35919631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
35929631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *NextAddr =
35939631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
35949631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                      "ap.next");
35959631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
35969631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
35979631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return AddrTyped;
35989631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
35999631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
36009631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
3601ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerconst TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
360282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (TheTargetCodeGenInfo)
360382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return *TheTargetCodeGenInfo;
3604c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3605bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
36061752ee4849f4c37f5e03193e658be92650b0e65aDaniel Dunbar  switch (Triple.getArch()) {
36072c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar  default:
3608ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
36092c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
3610aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mips:
3611aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mipsel:
3612c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
3613aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
36148c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64:
36158c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64el:
3616c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
36178c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka
361834d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::arm:
361934d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::thumb:
362034c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    {
362134c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
36225e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
3623bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
362434c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::APCS;
362534c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      else if (CodeGenOpts.FloatABI == "hard")
362634c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::AAPCS_VFP;
362734c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel
362834c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
362934c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    }
363034d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
3631ec853ba1087f606e9685cb1e800616565ba35093John McCall  case llvm::Triple::ppc:
3632ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
3633ec853ba1087f606e9685cb1e800616565ba35093John McCall
36340259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  case llvm::Triple::ptx32:
36350259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  case llvm::Triple::ptx64:
36360259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types));
36370259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
3638276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  case llvm::Triple::mblaze:
3639276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3640276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
364182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  case llvm::Triple::msp430:
3642ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
364334d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
36442f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  case llvm::Triple::tce:
36452f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
36462f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
3647c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  case llvm::Triple::x86: {
3648bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
3649c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman
3650db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    if (Triple.isOSDarwin())
365182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
365255fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman               new X86_32TargetCodeGenInfo(
365355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman                 Types, true, true, DisableMMX, false));
3654db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar
3655db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    switch (Triple.getOS()) {
36562c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::Cygwin:
36572c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::MinGW32:
3658727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::AuroraUX:
3659727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::DragonFly:
366075c135a511c855d94bbfa7f00dd27a165f61e953David Chisnall    case llvm::Triple::FreeBSD:
36612c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::OpenBSD:
366282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
366355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman               new X86_32TargetCodeGenInfo(
366455fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman                 Types, false, true, DisableMMX, false));
366555fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman
366655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    case llvm::Triple::Win32:
366755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      return *(TheTargetCodeGenInfo =
366855fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman               new X86_32TargetCodeGenInfo(
366955fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman                 Types, false, true, DisableMMX, true));
36702c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
36712c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    default:
367282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
367355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman               new X86_32TargetCodeGenInfo(
367455fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman                 Types, false, false, DisableMMX, false));
3675c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
3676c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  }
36772c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
3678ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  case llvm::Triple::x86_64: {
3679ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3680ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
3681f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    switch (Triple.getOS()) {
3682f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Win32:
36830aa205765aec0aa5eed672f8e3cade543372edcdNAKAMURA Takumi    case llvm::Triple::MinGW32:
3684f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Cygwin:
3685f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner      return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3686f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    default:
3687ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3688ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman                                                                  HasAVX));
3689f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    }
3690c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
36919631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  case llvm::Triple::hexagon:
36929631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
3693ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
3694c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3695