182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov//===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===//
2c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
3c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//                     The LLVM Compiler Infrastructure
4c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
5c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// This file is distributed under the University of Illinois Open Source
6c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// License. See LICENSE.TXT for details.
7c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
8c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//===----------------------------------------------------------------------===//
9c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
10c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// These classes wrap the information about a call or function
11c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov// definition used to handle ABI compliancy.
12c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//
13c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov//===----------------------------------------------------------------------===//
14c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov#include "TargetInfo.h"
16c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "ABIInfo.h"
17c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "CodeGenFunction.h"
1819cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
1934c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel#include "clang/Frontend/CodeGenOptions.h"
20c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov#include "llvm/Type.h"
219c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner#include "llvm/Target/TargetData.h"
222c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar#include "llvm/ADT/Triple.h"
2328df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar#include "llvm/Support/raw_ostream.h"
24c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovusing namespace clang;
25c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovusing namespace CodeGen;
26c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
27aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallstatic void AssignToArrayRange(CodeGen::CGBuilderTy &Builder,
28aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               llvm::Value *Array,
29aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               llvm::Value *Value,
30aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               unsigned FirstIndex,
31aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                               unsigned LastIndex) {
32aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // Alternatively, we could emit this as a loop in the source.
33aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  for (unsigned I = FirstIndex; I <= LastIndex; ++I) {
34aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I);
35aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    Builder.CreateStore(Value, Cell);
36aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
37aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
38aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
39d608cdb7c044365cf4e8764ade1e11e99c176078John McCallstatic bool isAggregateTypeForABI(QualType T) {
40d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  return CodeGenFunction::hasAggregateLLVMType(T) ||
41d608cdb7c044365cf4e8764ade1e11e99c176078John McCall         T->isMemberFunctionPointerType();
42d608cdb7c044365cf4e8764ade1e11e99c176078John McCall}
43d608cdb7c044365cf4e8764ade1e11e99c176078John McCall
44c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton KorobeynikovABIInfo::~ABIInfo() {}
45c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
46ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris LattnerASTContext &ABIInfo::getContext() const {
47ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getContext();
48ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
49ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
50ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerllvm::LLVMContext &ABIInfo::getVMContext() const {
51ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getLLVMContext();
52ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
53ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
54ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerconst llvm::TargetData &ABIInfo::getTargetData() const {
55ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  return CGT.getTargetData();
56ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner}
57ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
58ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner
59c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovvoid ABIArgInfo::dump() const {
605f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  raw_ostream &OS = llvm::errs();
6128df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar  OS << "(ABIArgInfo Kind=";
62c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (TheKind) {
63c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Direct:
64800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    OS << "Direct Type=";
652acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    if (llvm::Type *Ty = getCoerceToType())
66800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      Ty->print(OS);
67800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    else
68800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      OS << "null";
69c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
70cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov  case Extend:
7128df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Extend";
72cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    break;
73c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Ignore:
7428df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Ignore";
75c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
76c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Indirect:
77dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar    OS << "Indirect Align=" << getIndirectAlign()
78e9b5d77b7bfd3e8bba05df9914a6e8c336d68ff3Joerg Sonnenberger       << " ByVal=" << getIndirectByVal()
79cf3b6f2504596812db1fcef0df8ce5b3449c4aacDaniel Dunbar       << " Realign=" << getIndirectRealign();
80c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
81c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Expand:
8228df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar    OS << "Expand";
83c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
84c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
8528df7a5813d94ff32904c31195d7f6fd74db8c53Daniel Dunbar  OS << ")\n";
86c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
87c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
8882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton KorobeynikovTargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; }
8982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
9049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall// If someone can figure out a general rule for this, that would be great.
9149e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall// It's probably just doomed to be platform-dependent, though.
9249e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCallunsigned TargetCodeGenInfo::getSizeOfUnwindException() const {
9349e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  // Verified for:
9449e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   x86-64     FreeBSD, Linux, Darwin
9549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   x86-32     FreeBSD, Linux, Darwin
9649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   PowerPC    Linux, Darwin
9749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  //   ARM        Darwin (*not* EABI)
9849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  return 32;
9949e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall}
10049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
101de5d3c717684f3821b8db58037bc7140acf134aaJohn McCallbool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args,
102de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                                     const FunctionNoProtoType *fnType) const {
10301f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  // The following conventions are known to require this to be false:
10401f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  //   x86_stdcall
10501f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  //   MIPS
10601f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  // For everything else, we just prefer false unless we opt out.
10701f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  return false;
10801f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall}
10901f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
11098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays);
111c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
112c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// isEmptyField - Return true iff a the field is "empty", that is it
113c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// is an unnamed bit-field or an (array of) empty record(s).
11498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyField(ASTContext &Context, const FieldDecl *FD,
11598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar                         bool AllowArrays) {
116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (FD->isUnnamedBitfield())
117c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  QualType FT = FD->getType();
120c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1217e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman  // Constant arrays of empty records count as empty, strip them off.
1227e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman  // Constant arrays of zero length always count as empty.
12398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (AllowArrays)
1247e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
1257e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman      if (AT->getSize() == 0)
1267e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman        return true;
12798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      FT = AT->getElementType();
1287e7ad3f8fa150de6144be332ae4bfe5d1acb5c6dEli Friedman    }
12998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
1305ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  const RecordType *RT = FT->getAs<RecordType>();
1315ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (!RT)
1325ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar    return false;
1335ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
1345ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // C++ record fields are never empty, at least in the Itanium ABI.
1355ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  //
1365ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // FIXME: We should use a predicate for whether this behavior is true in the
1375ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  // current ABI.
1385ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (isa<CXXRecordDecl>(RT->getDecl()))
1395ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar    return false;
1405ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
14198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return isEmptyRecord(Context, FT, AllowArrays);
142c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
143c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
144c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// isEmptyRecord - Return true iff a structure contains only empty
145c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// fields. Note that a structure with a flexible array member is not
146c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// considered empty.
14798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbarstatic bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) {
1486217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *RT = T->getAs<RecordType>();
149c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT)
150c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
151c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordDecl *RD = RT->getDecl();
152c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (RD->hasFlexibleArrayMember())
153c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
1545ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
155c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis  // If this is a C++ record, check the bases first.
1565ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
157c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
158c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis           e = CXXRD->bases_end(); i != e; ++i)
159c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis      if (!isEmptyRecord(Context, i->getType(), true))
160c5f18f3e8c3f1e9cb25534f9a9676f112bedc2a7Argyrios Kyrtzidis        return false;
1615ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar
16217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
16317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i)
164581deb3da481053c4993c7600f97acf7768caac5David Blaikie    if (!isEmptyField(Context, *i, AllowArrays))
165c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
166c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
167c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
168c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1690a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either
1700a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// a non-trivial destructor or a non-trivial copy constructor.
1710a8f847e97f40cce51dc69051b964732333dc028Anders Carlssonstatic bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) {
1720a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
1730a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  if (!RD)
1740a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    return false;
1758bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1760a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor();
1770a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson}
1780a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1790a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is
1800a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// a record type with either a non-trivial destructor or a non-trivial copy
1810a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson/// constructor.
1820a8f847e97f40cce51dc69051b964732333dc028Anders Carlssonstatic bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) {
1830a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  const RecordType *RT = T->getAs<RecordType>();
1840a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  if (!RT)
1850a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    return false;
1860a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1870a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson  return hasNonTrivialDestructorOrCopyConstructor(RT);
1880a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson}
1890a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
190c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// isSingleElementStruct - Determine if a structure is a "single
191c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// element struct", i.e. it has exactly one non-empty field or
192c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// exactly one field which is itself a single element
193c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// struct. Structures with flexible array members are never
194c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// considered single element structs.
195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov///
196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// \return The field declaration for the single non-empty field, if
197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// it exists.
198c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic const Type *isSingleElementStruct(QualType T, ASTContext &Context) {
199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordType *RT = T->getAsStructureType();
200c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT)
201c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
202c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
203c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const RecordDecl *RD = RT->getDecl();
204c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (RD->hasFlexibleArrayMember())
205c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return 0;
206c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
207c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  const Type *Found = 0;
2088bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2099430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  // If this is a C++ record, check the bases first.
2109430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
2119430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
2129430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar           e = CXXRD->bases_end(); i != e; ++i) {
2139430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // Ignore empty records.
2145ea68614bfe0e78b5d66339b781529038f86501fDaniel Dunbar      if (isEmptyRecord(Context, i->getType(), true))
2159430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        continue;
2169430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2179430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // If we already found an element then this isn't a single-element struct.
2189430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      if (Found)
2199430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        return 0;
2209430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2219430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // If this is non-empty and not a single element struct, the composite
2229430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      // cannot be a single element struct.
2239430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      Found = isSingleElementStruct(i->getType(), Context);
2249430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar      if (!Found)
2259430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar        return 0;
2269430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar    }
2279430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  }
2289430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar
2299430d5a61598c47d827e1cd05f7cf3f110eeec9eDaniel Dunbar  // Check for single element.
23017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
23117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
232581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
233c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    QualType FT = FD->getType();
234c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
235c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Ignore empty fields.
23698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (isEmptyField(Context, FD, true))
237c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      continue;
238c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
239c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // If we already found an element then this isn't a single-element
240c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // struct.
241c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Found)
242c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return 0;
243c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
244c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Treat single element arrays as the element.
245c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) {
246c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (AT->getSize().getZExtValue() != 1)
247c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
248c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      FT = AT->getElementType();
249c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
250c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
251d608cdb7c044365cf4e8764ade1e11e99c176078John McCall    if (!isAggregateTypeForABI(FT)) {
252c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Found = FT.getTypePtr();
253c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else {
254c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Found = isSingleElementStruct(FT, Context);
255c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (!Found)
256c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return 0;
257c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
258c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
259c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
260bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  // We don't consider a struct a single-element struct if it has
261bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  // padding beyond the element type.
262bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman  if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T))
263bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman    return 0;
264bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
265c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Found;
266c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) {
269a1842d32a1964712e42078e9b389dce9258c6a8cDaniel Dunbar  if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() &&
27055e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      !Ty->isAnyComplexType() && !Ty->isEnumeralType() &&
27155e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      !Ty->isBlockPointerType())
272c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Size = Context.getTypeSize(Ty);
275c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Size == 32 || Size == 64;
276c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
27853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// canExpandIndirectArgument - Test whether an argument type which is to be
27953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// passed indirectly (on the stack) would have the equivalent layout if it was
28053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// expanded into separate arguments. If so, we prefer to do the latter to avoid
28153012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar/// inhibiting optimizations.
28253012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar///
28353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// FIXME: This predicate is missing many cases, currently it just follows
28453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We
28553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// should probably make this smarter, or better yet make the LLVM backend
28653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar// capable of handling it.
28753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbarstatic bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) {
28853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // We can only expand structure types.
28953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
29053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  if (!RT)
29153012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    return false;
29253012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar
29353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // We can only expand (C) structures.
29453012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  //
29553012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  // FIXME: This needs to be generalized to handle classes as well.
29653012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
29753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar  if (!RD->isStruct() || isa<CXXRecordDecl>(RD))
29853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    return false;
29953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar
300506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  uint64_t Size = 0;
301506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
30217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
30317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         i != e; ++i) {
304581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
305c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
306c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (!is32Or64BitBasicType(FD->getType(), Context))
307c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
308c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
309c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: Reject bit-fields wholesale; there are two problems, we don't know
310c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // how to expand them yet, and the predicate for telling if a bitfield still
311c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // counts as "basic" is more complicated than what we were doing previously.
312c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (FD->isBitField())
313c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
314506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
315506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman    Size += Context.getTypeSize(FD->getType());
316c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
317c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
318506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  // Make sure there are not any holes in the struct.
319506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman  if (Size != Context.getTypeSize(Ty))
320506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman    return false;
321506d4e375a6a36a49eb70578983dc4acaf2f15aeEli Friedman
322c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
324c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
325c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovnamespace {
326c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// DefaultABIInfo - The default implementation for ABI specific
327c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// details. This implementation provides information which results in
328c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// self-consistent and sensible LLVM IR generation, but does not
329c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// conform to any particular ABI.
330c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass DefaultABIInfo : public ABIInfo {
331ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerpublic:
332ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
3338bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
334a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
335a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
337ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const {
338a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
339c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
340c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         it != ie; ++it)
341a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      it->info = classifyArgumentType(it->type);
342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
343c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
344c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
345c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
346c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
347c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
34882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
34982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
350ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
351ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
35282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
35382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
35482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovllvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
35582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                       CodeGenFunction &CGF) const {
35682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  return 0;
35782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
35882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
359a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const {
36090306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung  if (isAggregateTypeForABI(Ty)) {
36190306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    // Records with non trivial destructors/constructors should not be passed
36290306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    // by value.
36390306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
36490306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
36590306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung
36682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return ABIArgInfo::getIndirect(0);
36790306934bccaadaf2b538b3c90c3dd478aa1e7d8Jan Wen Voung  }
368dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
369a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  // Treat an enum type as its underlying type.
370a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
371a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner    Ty = EnumTy->getDecl()->getIntegerType();
372aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
373a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  return (Ty->isPromotableIntegerType() ?
374a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
37582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
37682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
3770024f940dd15987b8ffbe6e787dcf860a9ea1effBob WilsonABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const {
3780024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (RetTy->isVoidType())
3790024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    return ABIArgInfo::getIgnore();
3800024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
3810024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (isAggregateTypeForABI(RetTy))
3820024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    return ABIArgInfo::getIndirect(0);
3830024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
3840024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  // Treat an enum type as its underlying type.
3850024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3860024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson    RetTy = EnumTy->getDecl()->getIntegerType();
3870024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
3880024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson  return (RetTy->isPromotableIntegerType() ?
3890024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3900024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson}
3910024f940dd15987b8ffbe6e787dcf860a9ea1effBob Wilson
3929ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff//===----------------------------------------------------------------------===//
3939ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff// le32/PNaCl bitcode ABI Implementation
3949ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff//===----------------------------------------------------------------------===//
3959ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
3969ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffclass PNaClABIInfo : public ABIInfo {
3979ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff public:
3989ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
3999ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4009ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  ABIArgInfo classifyReturnType(QualType RetTy) const;
4019ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const;
4029ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4039ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  virtual void computeInfo(CGFunctionInfo &FI) const;
4049ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4059ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff                                 CodeGenFunction &CGF) const;
4069ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff};
4079ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4089ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffclass PNaClTargetCodeGenInfo : public TargetCodeGenInfo {
4099ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff public:
4109ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
4119ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {}
4129ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff};
4139ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4149ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffvoid PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const {
4159ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
4169ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4179ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0;
4189ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4199ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
4209ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff         it != ie; ++it)
4219ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff      it->info = classifyArgumentType(it->type, FreeRegs);
4229ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  }
4239ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4249ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuffllvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
4259ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff                                       CodeGenFunction &CGF) const {
4269ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  return 0;
4279ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff}
4289ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4299ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek SchuffABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty,
4309ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff                                              unsigned &FreeRegs) const {
4319ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (isAggregateTypeForABI(Ty)) {
4329ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    // Records with non trivial destructors/constructors should not be passed
4339ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    // by value.
4349ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    FreeRegs = 0;
4359ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
4369ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
4379ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4389ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return ABIArgInfo::getIndirect(0);
4399ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  }
4409ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4419ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  // Treat an enum type as its underlying type.
4429ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
4439ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    Ty = EnumTy->getDecl()->getIntegerType();
4449ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4459ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ?
4469ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4479ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4489ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  // Regparm regs hold 32 bits.
4499ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
4509ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (SizeInRegs == 0) return BaseInfo;
4519ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (SizeInRegs > FreeRegs) {
4529ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    FreeRegs = 0;
4539ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return BaseInfo;
4549ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  }
4559ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  FreeRegs -= SizeInRegs;
4569ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  return BaseInfo.isDirect() ?
4579ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff      ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) :
4589ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff      ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType());
4599ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff}
4609ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4619ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek SchuffABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const {
4629ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (RetTy->isVoidType())
4639ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return ABIArgInfo::getIgnore();
4649ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4659ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (isAggregateTypeForABI(RetTy))
4669ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return ABIArgInfo::getIndirect(0);
4679ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4689ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  // Treat an enum type as its underlying type.
4699ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
4709ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    RetTy = EnumTy->getDecl()->getIntegerType();
4719ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
4729ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  return (RetTy->isPromotableIntegerType() ?
4739ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
4749ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff}
4759ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff
47655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman/// UseX86_MMXType - Return true if this is an MMX type that should use the
47755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman/// special x86_mmx type.
4782acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerbool UseX86_MMXType(llvm::Type *IRType) {
479bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the
480bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  // special x86_mmx type.
481bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 &&
482bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() &&
483bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    IRType->getScalarSizeInBits() != 64;
484bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling}
485bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling
486ef6de3da8572607f786303c07150daa6e140ab19Jay Foadstatic llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
4875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                          StringRef Constraint,
488ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                          llvm::Type* Ty) {
4890507be662df482b5c67b7905ed7ca368cb5c6b69Bill Wendling  if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy())
4904b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return llvm::Type::getX86_MMXTy(CGF.getLLVMContext());
4914b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  return Ty;
4924b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne}
4934b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
494dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
495dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner// X86-32 ABI Implementation
496dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
4978bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
498c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// X86_32ABIInfo - The X86-32 ABI information.
499c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass X86_32ABIInfo : public ABIInfo {
500b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  enum Class {
501b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Integer,
502b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Float
503b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  };
504b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
505fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  static const unsigned MinABIStackAlignInBytes = 4;
506fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
5071e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsDarwinVectorABI;
5081e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall  bool IsSmallStructInRegABI;
509c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  bool IsMMXDisabled;
51055fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  bool IsWin32FloatStructABI;
511b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  unsigned DefaultNumRegisterParameters;
512c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
513c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  static bool isRegisterSize(unsigned Size) {
514c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return (Size == 8 || Size == 16 || Size == 32 || Size == 64);
515c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
516c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5176c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context,
5186c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                          unsigned callingConvention);
519c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
520dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
521dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar  /// such that the argument will be passed in memory.
522a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const;
523dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
524fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  /// \brief Return the alignment to use for the given type on the stack.
525e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const;
526fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
527b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  Class classify(QualType Ty) const;
528b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindola  ABIArgInfo classifyReturnType(QualType RetTy,
5296c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                unsigned callingConvention) const;
530b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  ABIArgInfo classifyArgumentTypeWithReg(QualType RetTy,
531b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                         unsigned &FreeRegs) const;
532a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
533c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
534b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindolapublic:
535b33a3c448ec669a7ef530ef8094cdfc9346468cfRafael Espindola
536aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  virtual void computeInfo(CGFunctionInfo &FI) const;
537c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
538c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
539c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
540b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m, bool w,
541b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                unsigned r)
542c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman    : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p),
543b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      IsMMXDisabled(m), IsWin32FloatStructABI(w),
544b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      DefaultNumRegisterParameters(r) {}
545c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
546c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
54782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_32TargetCodeGenInfo : public TargetCodeGenInfo {
54882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
54955fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman  X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT,
550b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      bool d, bool p, bool m, bool w, unsigned r)
551b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m, w, r)) {}
55274f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
55374f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
55474f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                           CodeGen::CodeGenModule &CGM) const;
5556374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
5566374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
5576374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // Darwin uses different dwarf register numbers for EH.
5586374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    if (CGM.isTargetDarwin()) return 5;
5596374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
5606374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 4;
5616374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
5626374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
5636374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
5646374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const;
5654b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
566ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
5675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
568ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
5694b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
5704b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
5714b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
57282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
57382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
57482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov}
575c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
576c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// shouldReturnTypeInRegister - Determine if the given type should be
577c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// passed in a register (for the Darwin ABI).
578c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovbool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty,
5796c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               ASTContext &Context,
5806c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                               unsigned callingConvention) {
581c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Size = Context.getTypeSize(Ty);
582c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
583c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Type must be register sized.
584c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!isRegisterSize(Size))
585c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return false;
586c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
587c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Ty->isVectorType()) {
588c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // 64- and 128- bit vectors inside structures are not returned in
589c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // registers.
590c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 64 || Size == 128)
591c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
592c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
593c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
594c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
595c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
5967711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // If this is a builtin, pointer, enum, complex type, member pointer, or
5977711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar  // member function pointer it is ok.
598a1842d32a1964712e42078e9b389dce9258c6a8cDaniel Dunbar  if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() ||
59955e59e139d9ebcaae16d710472e28edbcafac98aDaniel Dunbar      Ty->isAnyComplexType() || Ty->isEnumeralType() ||
6007711523d948bbe635f690f5795ef7ea9a3289eb2Daniel Dunbar      Ty->isBlockPointerType() || Ty->isMemberPointerType())
601c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return true;
602c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
603c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Arrays are treated like records.
604c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty))
6056c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return shouldReturnTypeInRegister(AT->getElementType(), Context,
6066c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                      callingConvention);
607c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
608c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Otherwise, it must be a record type.
6096217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *RT = Ty->getAs<RecordType>();
610c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!RT) return false;
611c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
612a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson  // FIXME: Traverse bases here too.
613a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson
6146c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // For thiscall conventions, structures will never be returned in
6156c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  // a register.  This is for compatibility with the MSVC ABI
6166c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  if (callingConvention == llvm::CallingConv::X86_ThisCall &&
6176c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman      RT->isStructureType()) {
6186c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    return false;
6196c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman  }
6206c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman
621c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Structure types are passed in register if all fields would be
622c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // passed in a register.
62317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(),
62417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         e = RT->getDecl()->field_end(); i != e; ++i) {
625581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
626c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
627c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Empty fields are ignored.
62898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (isEmptyField(Context, FD, true))
629c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      continue;
630c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
631c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Check fields recursively.
6326c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (!shouldReturnTypeInRegister(FD->getType(), Context,
6336c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                    callingConvention))
634c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return false;
635c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
636c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return true;
637c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
638c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
6396c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron BallmanABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy,
6406c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                            unsigned callingConvention) const {
641a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (RetTy->isVoidType())
642c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
6438bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
644a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const VectorType *VT = RetTy->getAs<VectorType>()) {
645c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // On Darwin, some vectors are returned in registers.
6461e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (IsDarwinVectorABI) {
647a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
648c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
649c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // 128-bit vectors are a special case; they are returned in
650c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // registers and we need to make sure to pick a type the LLVM
651c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // backend will like.
652c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size == 128)
653800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::VectorType::get(
654a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                  llvm::Type::getInt64Ty(getVMContext()), 2));
655c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
656c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Always return in register if it fits in a general purpose
657c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // register, or if it is 64 bits and has a single element.
658c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if ((Size == 8 || Size == 16 || Size == 32) ||
659c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          (Size == 64 && VT->getNumElements() == 1))
660800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
661a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                                            Size));
662c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
663c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
664c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
665c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
666c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getDirect();
667a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  }
6688bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
669d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(RetTy)) {
670a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = RetTy->getAs<RecordType>()) {
67140092972b591646b47037d2b46b695a4014df413Anders Carlsson      // Structures with either a non-trivial destructor or a non-trivial
67240092972b591646b47037d2b46b695a4014df413Anders Carlsson      // copy constructor are always indirect.
67340092972b591646b47037d2b46b695a4014df413Anders Carlsson      if (hasNonTrivialDestructorOrCopyConstructor(RT))
67440092972b591646b47037d2b46b695a4014df413Anders Carlsson        return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
6758bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
67640092972b591646b47037d2b46b695a4014df413Anders Carlsson      // Structures with flexible arrays are always indirect.
677c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
678c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return ABIArgInfo::getIndirect(0);
67940092972b591646b47037d2b46b695a4014df413Anders Carlsson    }
6808bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
6811e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    // If specified, structs and unions are always indirect.
6821e4249c10606f706aac181e6f5e8435ea99d9603David Chisnall    if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType())
683c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIndirect(0);
684c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
685c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Small structures which are register sized are generally returned
686c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // in a register.
6876c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman    if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(),
6886c60c8d7466b3191602dbb8e4a81f4ee7d9a09a6Aaron Ballman                                                  callingConvention)) {
689a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
690bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
691bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // As a special-case, if the struct is a "single-element" struct, and
692bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // the field is of type "float" or "double", return it in a
69355fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // floating-point register. (MSVC does not apply this special case.)
69455fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // We apply a similar transformation for pointer types to improve the
69555fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      // quality of the generated IR.
696bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
69755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman        if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType())
69855fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman            || SeltTy->hasPointerRepresentation())
699bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman          return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
700bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman
701bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // FIXME: We should be able to narrow this integer in cases with dead
702bd4d3bcd2cd64d1bba29b2a52705b97d68ebccd5Eli Friedman      // padding.
703800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size));
704c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
705c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
706c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIndirect(0);
707c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
7088bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
709a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  // Treat an enum type as its underlying type.
710a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
711a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    RetTy = EnumTy->getDecl()->getIntegerType();
712a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner
713a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  return (RetTy->isPromotableIntegerType() ?
714a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
715c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
716c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
717f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedmanstatic bool isSSEVectorType(ASTContext &Context, QualType Ty) {
718f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman  return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128;
719f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman}
720f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman
72193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbarstatic bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) {
72293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
72393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (!RT)
72493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 0;
72593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  const RecordDecl *RD = RT->getDecl();
72693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
72793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // If this is a C++ record, check the bases first.
72893ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD))
72993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
73093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar           e = CXXRD->bases_end(); i != e; ++i)
73193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      if (!isRecordWithSSEVectorType(Context, i->getType()))
73293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar        return false;
73393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
73493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
73593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar       i != e; ++i) {
73693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    QualType FT = i->getType();
73793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
738f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman    if (isSSEVectorType(Context, FT))
73993ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
74093ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
74193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    if (isRecordWithSSEVectorType(Context, FT))
74293ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar      return true;
74393ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  }
74493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
74593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return false;
74693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar}
74793ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
748e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbarunsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty,
749e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                                 unsigned Align) const {
750e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Otherwise, if the alignment is less than or equal to the minimum ABI
751e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // alignment, just use the default; the backend will handle this.
752fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar  if (Align <= MinABIStackAlignInBytes)
753e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return 0; // Use default alignment.
754e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
755e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // On non-Darwin, the stack type alignment is always 4.
756e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (!IsDarwinVectorABI) {
757e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    // Set explicit alignment, since we may need to realign the top.
758fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar    return MinABIStackAlignInBytes;
759e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  }
760fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
76193ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  // Otherwise, if the type contains an SSE vector type, the alignment is 16.
762f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman  if (Align >= 16 && (isSSEVectorType(getContext(), Ty) ||
763f4bd4d8fe029ca314c2c61edb1d2a65bc18cdbf2Eli Friedman                      isRecordWithSSEVectorType(getContext(), Ty)))
76493ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar    return 16;
76593ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar
76693ae947df36133c7a26a0c7d325c0679916ed2edDaniel Dunbar  return MinABIStackAlignInBytes;
767fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar}
768fb67d6c3814524fdd43bd2fb159f7c594eae581cDaniel Dunbar
769a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const {
77046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  if (!ByVal)
77146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return ABIArgInfo::getIndirect(0, false);
77246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
773e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // Compute the byval alignment.
774e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8;
775e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign);
776e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign == 0)
777de92d739ba0ef42a5a7dcfd6e170329549d0716bChris Lattner    return ABIArgInfo::getIndirect(4);
778e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
779e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // If the stack alignment is less than the type alignment, realign the
780e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  // argument.
781e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  if (StackAlign < TypeAlign)
782e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar    return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true,
783e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar                                   /*Realign=*/true);
784e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar
785e59d8585bb40a8bae6b847ad258536a2c01f20eaDaniel Dunbar  return ABIArgInfo::getIndirect(StackAlign);
786dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar}
787dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
788b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael EspindolaX86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const {
789b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  const Type *T = isSingleElementStruct(Ty, getContext());
790b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (!T)
791b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    T = Ty.getTypePtr();
792b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
793b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
794b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    BuiltinType::Kind K = BT->getKind();
795b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    if (K == BuiltinType::Float || K == BuiltinType::Double)
796b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola      return Float;
797b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
798b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  return Integer;
799b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola}
800b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
801b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael EspindolaABIArgInfo
802b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael EspindolaX86_32ABIInfo::classifyArgumentTypeWithReg(QualType Ty,
803b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           unsigned &FreeRegs) const {
804b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // Common case first.
805b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (FreeRegs == 0)
806b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
807b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
808b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  Class C = classify(Ty);
809b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (C == Float)
810b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
811b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
812b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32;
813b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (SizeInRegs == 0)
814b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
815b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
816b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (SizeInRegs > FreeRegs) {
817b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    FreeRegs = 0;
818b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return classifyArgumentType(Ty);
819b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
820b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  assert(SizeInRegs >= 1 && SizeInRegs <= 3);
821b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  FreeRegs -= SizeInRegs;
822b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
823b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // If it is a simple scalar, keep the type so that we produce a cleaner IR.
824b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  ABIArgInfo Foo = classifyArgumentType(Ty);
825b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (Foo.isDirect() && !Foo.getDirectOffset() && !Foo.getPaddingType())
826b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return ABIArgInfo::getDirectInReg(Foo.getCoerceToType());
827b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (Foo.isExtend())
828b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    return ABIArgInfo::getExtendInReg(Foo.getCoerceToType());
829b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
830b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  llvm::LLVMContext &LLVMContext = getVMContext();
831b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  llvm::Type *Int32 = llvm::Type::getInt32Ty(LLVMContext);
832b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  SmallVector<llvm::Type*, 3> Elements;
833b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  for (unsigned I = 0; I < SizeInRegs; ++I)
834b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Elements.push_back(Int32);
835b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements);
836b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  return ABIArgInfo::getDirectInReg(Result);
837b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola}
838b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
839a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const {
840c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Set alignment on indirect arguments.
841d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (isAggregateTypeForABI(Ty)) {
842c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Structures with flexible arrays are always indirect.
843a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    if (const RecordType *RT = Ty->getAs<RecordType>()) {
844a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      // Structures with either a non-trivial destructor or a non-trivial
845a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      // copy constructor are always indirect.
846a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson      if (hasNonTrivialDestructorOrCopyConstructor(RT))
847a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        return getIndirectResult(Ty, /*ByVal=*/false);
848dc6d574155072bfb35a7a29b94ef3afa0d40fb5aDaniel Dunbar
849c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (RT->getDecl()->hasFlexibleArrayMember())
850a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        return getIndirectResult(Ty);
851a887423cf580e19b2d03e3a0499c065730c96b28Anders Carlsson    }
852c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
8535a4d35247f55dae6dd0d5ad349ecadbbea0b4572Eli Friedman    // Ignore empty structs/unions.
8545a1ac89b244940a0337ea7ae7dc371e2a9bf7c50Eli Friedman    if (isEmptyRecord(getContext(), Ty, true))
855c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return ABIArgInfo::getIgnore();
856c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
85753012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // Expand small (<= 128-bit) record types when we know that the stack layout
85853012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // of those arguments will match the struct. This is important because the
85953012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // LLVM backend isn't smart enough to remove byval, which inhibits many
86053012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar    // optimizations.
861a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (getContext().getTypeSize(Ty) <= 4*32 &&
862a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner        canExpandIndirectArgument(Ty, getContext()))
86353012f447145bfd5e3a759f069a2bdf2b6705708Daniel Dunbar      return ABIArgInfo::getExpand();
864c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
865a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    return getIndirectResult(Ty);
8668bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  }
8678bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
868bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
8697b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // On Darwin, some vectors are passed in memory, we handle this by passing
8707b733505defd34f1bb7e74d9526be0bc41e76693Chris Lattner    // it as an i8/i16/i32/i64.
871bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    if (IsDarwinVectorABI) {
872bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      uint64_t Size = getContext().getTypeSize(Ty);
873bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner      if ((Size == 8 || Size == 16 || Size == 32) ||
874bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner          (Size == 64 && VT->getNumElements() == 1))
875bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
876bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner                                                            Size));
877bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    }
878bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling
8799cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *IRType = CGT.ConvertType(Ty);
880bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    if (UseX86_MMXType(IRType)) {
881c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman      if (IsMMXDisabled)
882c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman        return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
883c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman                                                            64));
884bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      ABIArgInfo AAI = ABIArgInfo::getDirect(IRType);
885bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext()));
886bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling      return AAI;
887bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling    }
8889cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
889bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner    return ABIArgInfo::getDirect();
890bbae8b40cd37d5b2815f8450cb588a41da89d7e5Chris Lattner  }
8919cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
8929cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
893a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
894a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    Ty = EnumTy->getDecl()->getIntegerType();
895aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
896a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  return (Ty->isPromotableIntegerType() ?
897a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
898c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
899c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
900aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindolavoid X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const {
901aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  FI.getReturnInfo() = classifyReturnType(FI.getReturnType(),
902aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola                                          FI.getCallingConvention());
903b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
904b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() :
905b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    DefaultNumRegisterParameters;
906b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
907b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // If the return value is indirect, then the hidden argument is consuming one
908b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  // integer register.
909b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  if (FI.getReturnInfo().isIndirect() && FreeRegs) {
910b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    --FreeRegs;
911b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    ABIArgInfo &Old = FI.getReturnInfo();
912b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(),
913b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                       Old.getIndirectByVal(),
914b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                       Old.getIndirectRealign());
915b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola  }
916b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola
917aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
918aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola       it != ie; ++it)
919b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola    it->info = classifyArgumentTypeWithReg(it->type, FreeRegs);
920aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola}
921aa9cf8d3abd8760d78b20e9194df169bbd8b0f01Rafael Espindola
922c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
923c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
9248b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
925c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
926c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
927c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
928c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                       "ap");
929c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
9307b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
9317b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  // Compute if the address needs to be aligned
9327b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity();
9337b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = getTypeStackAlignInBytes(Ty, Align);
9347b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  Align = std::max(Align, 4U);
9357b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  if (Align > 4) {
9367b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    // addr = (addr + align - 1) & -align;
9377b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Offset =
9387b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman      llvm::ConstantInt::get(CGF.Int32Ty, Align - 1);
9397b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateGEP(Addr, Offset);
9407b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr,
9417b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                                    CGF.Int32Ty);
9427b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align);
9437b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
9447b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      Addr->getType(),
9457b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman                                      "ap.cur.aligned");
9467b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman  }
9477b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman
948c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Type *PTy =
94996e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
950c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
951c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
952c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
9537b1fb81a512def2a20e2834b4598a7b3a740dc7fEli Friedman    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align);
954c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
95577b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
956c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
957c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
958c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
959c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
960c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
961c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
96274f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davisvoid X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
96374f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                                  llvm::GlobalValue *GV,
96474f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis                                            CodeGen::CodeGenModule &CGM) const {
96574f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
96674f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) {
96774f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Get the LLVM function.
96874f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      llvm::Function *Fn = cast<llvm::Function>(GV);
96974f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
97074f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      // Now add the 'alignstack' attribute with a value of 16.
97174f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis      Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16));
97274f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis    }
97374f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis  }
97474f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis}
97574f7293eb30bf77355c20a3c2cad7b67d8ce7388Charles Davis
9766374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCallbool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable(
9776374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               CodeGen::CodeGenFunction &CGF,
9786374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                                               llvm::Value *Address) const {
9796374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
9806374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
9818b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
9828bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
9836374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 0-7 are the eight integer registers;  the order is different
9846374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  //   on Darwin (for EH), but the range is the same.
9856374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  // 8 is %eip.
986aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 8);
9876374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
9886374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  if (CGF.CGM.isTargetDarwin()) {
9896374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 12-16 are st(0..4).  Not sure why we stop at 4.
9906374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 16, which is sizeof(long double) on
9916374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 8-byte alignment for that type.
9928b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16);
993aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Sixteen8, 12, 16);
9948bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
9956374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  } else {
9966374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 9 is %eflags, which doesn't get a size on Darwin for some
9976374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // reason.
9986374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9));
9996374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
10006374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // 11-16 are st(0..5).  Not sure why we stop at 5.
10016374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // These have size 12, which is sizeof(long double) on
10026374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    // platforms with 4-byte alignment for that type.
10038b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12);
1004aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    AssignToArrayRange(Builder, Address, Twelve8, 11, 16);
1005aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
10066374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
10076374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  return false;
10086374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall}
10096374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
1010dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
1011dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner// X86-64 ABI Implementation
1012dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
1013dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
1014dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
1015c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovnamespace {
1016c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov/// X86_64ABIInfo - The X86_64 ABI information.
1017c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass X86_64ABIInfo : public ABIInfo {
1018c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  enum Class {
1019c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Integer = 0,
1020c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSE,
1021c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    SSEUp,
1022c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87,
1023c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    X87Up,
1024c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ComplexX87,
1025c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    NoClass,
1026c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Memory
1027c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  };
1028c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1029c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// merge - Implement the X86_64 ABI merging algorithm.
1030c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1031c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// Merge an accumulating classification \arg Accum with a field
1032c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classification \arg Field.
1033c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1034c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Accum - The accumulating classification. This should
1035c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// always be either NoClass or the result of a previous merge
1036c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// call. In addition, this should never be Memory (the caller
1037c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// should just return Memory for the aggregate).
10381090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  static Class merge(Class Accum, Class Field);
1039c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
10404943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// postMerge - Implement the X86_64 ABI post merging algorithm.
10414943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
10424943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// Post merger cleanup, reduces a malformed Hi and Lo pair to
10434943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// final MEMORY or SSE classes when necessary.
10444943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
10454943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param AggregateSize - The size of the current aggregate in
10464943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// the classification process.
10474943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
10484943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Lo - The classification for the parts of the type
10494943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the low word of the containing object.
10504943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
10514943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// \param Hi - The classification for the parts of the type
10524943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  /// residing in the higher words of the containing object.
10534943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  ///
10544943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const;
10554943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
1056c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// classify - Determine the x86_64 register classes in which the
1057c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// given type T should be passed.
1058c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1059c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Lo - The classification for the parts of the type
1060c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the low word of the containing object.
1061c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1062c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param Hi - The classification for the parts of the type
1063c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// residing in the high word of the containing object.
1064c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1065c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// \param OffsetBase - The bit offset of this type in the
1066c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// containing object.  Some parameters are classified different
1067c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// depending on whether they straddle an eightbyte boundary.
1068c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1069c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If a word is unused its result will be NoClass; if a type should
1070c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// be passed in Memory then at least the classification of \arg Lo
1071c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// will be Memory.
1072c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1073c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// The \arg Lo class will be NoClass iff the argument is ignored.
1074c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ///
1075c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// If the \arg Lo class is ComplexX87, then the \arg Hi class will
1076c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// also be ComplexX87.
10779c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const;
1078c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
10794943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  llvm::Type *GetByteVectorType(QualType Ty) const;
10809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType,
10819cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned IROffset, QualType SourceTy,
10829cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                 unsigned SourceOffset) const;
10839cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType,
10849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned IROffset, QualType SourceTy,
10859cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner                                     unsigned SourceOffset) const;
10868bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1087c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
108846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// such that the argument will be returned in memory.
10899c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  ABIArgInfo getIndirectReturnResult(QualType Ty) const;
109046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
109146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  /// getIndirectResult - Give a source type \arg Ty, return a suitable result
1092c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  /// such that the argument will be passed in memory.
1093edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ///
1094edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// \param freeIntRegs - The number of free integer registers remaining
1095edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  /// available.
1096edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const;
1097c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1098a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
1099c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1100bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  ABIArgInfo classifyArgumentType(QualType Ty,
1101edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                  unsigned freeIntRegs,
1102bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling                                  unsigned &neededInt,
110399aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling                                  unsigned &neededSSE) const;
1104c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1105ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool IsIllegalVectorType(QualType Ty) const;
1106ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
110767a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// The 0.98 ABI revision clarified a lot of ambiguities,
110867a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// unfortunately in ways that were not always consistent with
110967a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// certain previous compilers.  In particular, platforms which
111067a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// required strict binary compatibility with older versions of GCC
111167a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  /// may need to exempt themselves.
111267a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  bool honorsRevision0_98() const {
1113bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return !getContext().getTargetInfo().getTriple().isOSDarwin();
111467a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall  }
111567a5773ba529aebcad03fa5e7cc95555d133e93dJohn McCall
1116ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  bool HasAVX;
1117ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovpublic:
1119ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) :
1120ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      ABIInfo(CGT), HasAVX(hasavx) {}
11219c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
1122de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isPassedUsingAVXType(QualType type) const {
1123de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    unsigned neededInt, neededSSE;
1124edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // The freeIntRegs argument doesn't matter here.
1125edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE);
1126de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (info.isDirect()) {
1127de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      llvm::Type *ty = info.getCoerceToType();
1128de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty))
1129de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        return (vectorTy->getBitWidth() > 128);
1130de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    }
1131de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return false;
1132de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
1133de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
1134ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
1135c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1136c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1137c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
1138c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
113982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1140f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner/// WinX86_64ABIInfo - The Windows X86_64 ABI information.
1141a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumiclass WinX86_64ABIInfo : public ABIInfo {
1142a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1143a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  ABIArgInfo classify(QualType Ty) const;
1144a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1145f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
1146a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {}
1147a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
1148a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  virtual void computeInfo(CGFunctionInfo &FI) const;
1149f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1150f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
1151f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                 CodeGenFunction &CGF) const;
1152f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
1153f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
115482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass X86_64TargetCodeGenInfo : public TargetCodeGenInfo {
115582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
1156ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX)
1157ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {}
11586374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
1159de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  const X86_64ABIInfo &getABIInfo() const {
1160de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo());
1161de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  }
1162de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
11636374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
11646374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 7;
11656374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
11666374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
11676374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
11686374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall                               llvm::Value *Address) const {
11698b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
11708bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1171aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 0-15 are the 16 integer registers.
1172aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    // 16 is %rip.
11738b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
11746374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return false;
11756374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
11764b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1177ef6de3da8572607f786303c07150daa6e140ab19Jay Foad  llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF,
11785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  StringRef Constraint,
1179ef6de3da8572607f786303c07150daa6e140ab19Jay Foad                                  llvm::Type* Ty) const {
11804b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne    return X86AdjustInlineAsmType(CGF, Constraint, Ty);
11814b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne  }
11824b93d660c6326ec79b5e369317d1051cf826c2f3Peter Collingbourne
1183de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall  bool isNoProtoCallVariadic(const CallArgList &args,
1184de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall                             const FunctionNoProtoType *fnType) const {
118501f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // The default CC on x86-64 sets %al to the number of SSA
118601f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall    // registers used, and GCC sets this when calling an unprototyped
11873ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    // function, so we override the default behavior.  However, don't do
118868805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // that when AVX types are involved: the ABI explicitly states it is
118968805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // undefined, and it doesn't work in practice because of how the ABI
119068805fef77978e69a14584148a3c6a4239e34171Eli Friedman    // defines varargs anyway.
1191de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) {
11923ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      bool HasAVXType = false;
1193de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall      for (CallArgList::const_iterator
1194de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall             it = args.begin(), ie = args.end(); it != ie; ++it) {
1195de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall        if (getABIInfo().isPassedUsingAVXType(it->Ty)) {
1196de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          HasAVXType = true;
1197de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall          break;
11983ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        }
11993ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      }
1200de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall
12013ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman      if (!HasAVXType)
12023ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman        return true;
12033ed7903d27f0e7e0cd3a61c165d39eca70f3cff5Eli Friedman    }
120401f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
1205de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall    return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType);
120601f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall  }
120701f151e0ffba72bcad770bea5f563a9b68ca050eJohn McCall
120882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
120982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
1210f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerclass WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo {
1211f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerpublic:
1212f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
1213f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {}
1214f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1215f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
1216f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return 7;
1217f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
1218f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1219f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
1220f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                               llvm::Value *Address) const {
12218b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8);
12229cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1223f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 0-15 are the 16 integer registers.
1224f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    // 16 is %rip.
12258b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16);
1226f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    return false;
1227f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  }
1228f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner};
1229f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
1230c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1231c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
12324943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesvoid X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo,
12334943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes                              Class &Hi) const {
12344943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done:
12354943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
12364943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (a) If one of the classes is Memory, the whole argument is passed in
12374943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
12384943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
12394943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (b) If X87UP is not preceded by X87, the whole argument is passed in
12404943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     memory.
12414943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
12424943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (c) If the size of the aggregate exceeds two eightbytes and the first
12434943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole
12444943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     argument is passed in memory. NOTE: This is necessary to keep the
12454943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //     ABI working for processors that don't support the __m256 type.
12464943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
12474943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE.
12484943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
12494943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Some of these are enforced by the merging logic.  Others can arise
12504943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // only with unions; for example:
12514943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //   union { _Complex double; unsigned; }
12524943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
12534943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  // Note that clauses (b) and (c) were added in 0.98.
12544943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  //
12554943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == Memory)
12564943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
12574943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == X87Up && Lo != X87 && honorsRevision0_98())
12584943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
12594943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp))
12604943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Lo = Memory;
12614943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes  if (Hi == SSEUp && Lo != SSE)
12624943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    Hi = SSE;
12634943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes}
12644943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes
12651090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerX86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) {
1266c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is
1267c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classified recursively so that always two fields are
1268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // considered. The resulting class is calculated according to
1269c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the classes of the fields in the eightbyte:
1270c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1271c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (a) If both classes are equal, this is the resulting class.
1272c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (b) If one of the classes is NO_CLASS, the resulting class is
1274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the other class.
1275c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1276c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (c) If one of the classes is MEMORY, the result is the MEMORY
1277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // class.
1278c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1279c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (d) If one of the classes is INTEGER, the result is the
1280c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // INTEGER.
1281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1282c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class,
1283c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // MEMORY is used as class.
1284c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
1285c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // (f) Otherwise class SSE is used.
1286c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1287c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Accum should never be memory (we should have returned) or
1288c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // ComplexX87 (because this cannot be passed in a structure).
1289c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Accum != Memory && Accum != ComplexX87) &&
1290c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov         "Invalid accumulated classification during merge.");
1291c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Accum == Field || Field == NoClass)
1292c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Accum;
12931090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == Memory)
1294c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
12951090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == NoClass)
1296c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Field;
12971090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Accum == Integer || Field == Integer)
1298c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Integer;
12991090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Field == X87 || Field == X87Up || Field == ComplexX87 ||
13001090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      Accum == X87 || Accum == X87Up)
1301c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return Memory;
13021090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  return SSE;
1303c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1304c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1305bcaedaed309ce453a992fdeef4a4c908cc7d9dfbChris Lattnervoid X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase,
1306c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                             Class &Lo, Class &Hi) const {
1307c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This code can be simplified by introducing a simple value class for
1308c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Class pairs with appropriate constructor methods for the various
1309c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // situations.
1310c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1311c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Some of the split computations are wrong; unaligned vectors
1312c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // shouldn't be passed in registers for example, so there is no chance they
1313c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // can straddle an eightbyte. Verify & simplify.
1314c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1315c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Lo = Hi = NoClass;
1316c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1317c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Class &Current = OffsetBase < 64 ? Lo : Hi;
1318c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Current = Memory;
1319c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1320183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
1321c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    BuiltinType::Kind k = BT->getKind();
1322c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (k == BuiltinType::Void) {
1324c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = NoClass;
1325c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) {
1326c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Integer;
1327c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Integer;
1328c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) {
1329c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
1330c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::Float || k == BuiltinType::Double) {
1331c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
1332c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (k == BuiltinType::LongDouble) {
1333c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = X87;
1334c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = X87Up;
1335c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _Decimal32 and _Decimal64 are SSE.
1337c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: _float128 and _Decimal128 are (SSE, SSEUp).
13381090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13391090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13408bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13411090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const EnumType *ET = Ty->getAs<EnumType>()) {
1342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Classify the underlying integer type.
13439c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi);
13441090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13451090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13468bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13471090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->hasPointerRepresentation()) {
1348c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = Integer;
13491090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13501090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13518bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13521090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (Ty->isMemberPointerType()) {
135367d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar    if (Ty->isMemberFunctionPointerType())
135467d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Lo = Hi = Integer;
135567d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar    else
135667d438d39a1cc37c372a2684dc354f58d0169bb1Daniel Dunbar      Current = Integer;
13571090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
13581090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
13598bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
13601090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const VectorType *VT = Ty->getAs<VectorType>()) {
1361ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(VT);
1362c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Size == 32) {
1363c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x
1364c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // float> as integer.
1365c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = Integer;
1366c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1367c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1368c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1369c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Real = (OffsetBase) / 64;
1370c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t EB_Imag = (OffsetBase + Size - 1) / 64;
1371c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (EB_Real != EB_Imag)
1372c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1373c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else if (Size == 64) {
1374c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x double> in memory. :(
1375c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double))
1376c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1377c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1378c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // gcc passes <1 x long long> as INTEGER.
1379473f8e723be93d84bd5fd15b094f4184802d4676Chris Lattner      if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) ||
13800fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) ||
13810fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) ||
13820fefa4175b0c9101564946f6a975ee9946c16d4bChris Lattner          VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong))
1383c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1384c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else
1385c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = SSE;
1386c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1387c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // If this type crosses an eightbyte boundary, it should be
1388c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // split.
1389c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (OffsetBase && OffsetBase != 64)
1390c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Hi = Lo;
1391ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    } else if (Size == 128 || (HasAVX && Size == 256)) {
13924943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // Arguments of 256-bits are split into four eightbyte chunks. The
13934943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // least significant one belongs to class SSE and all the others to class
13944943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // SSEUP. The original Lo and Hi design considers that types can't be
13954943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense.
13964943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // This design isn't correct for 256-bits, but since there're no cases
13974943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // where the upper parts would need to be inspected, avoid adding
13984943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes      // complexity and just consider Hi to match the 64-256 part.
1399c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = SSE;
1400c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = SSEUp;
1401c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
14021090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14031090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14048bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14051090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
1406ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    QualType ET = getContext().getCanonicalType(CT->getElementType());
1407c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1408ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
14092ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor    if (ET->isIntegralOrEnumerationType()) {
1410c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Size <= 64)
1411c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Current = Integer;
1412c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      else if (Size <= 128)
1413c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Hi = Integer;
1414ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    } else if (ET == getContext().FloatTy)
1415c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = SSE;
1416ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    else if (ET == getContext().DoubleTy)
1417c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = Hi = SSE;
1418ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    else if (ET == getContext().LongDoubleTy)
1419c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Current = ComplexX87;
1420c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1421c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // If this complex type crosses an eightbyte boundary then it
1422c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // should be split.
1423c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t EB_Real = (OffsetBase) / 64;
1424ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64;
1425c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (Hi == NoClass && EB_Real != EB_Imag)
1426c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = Lo;
14278bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14281090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14291090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14308bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1431ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) {
1432c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Arrays are treated like structures.
1433c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1434ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1435c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1436c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
14374943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
14384943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1439c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1440c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1441c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned
1442c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // fields, it has class MEMORY.
1443c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
1444c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Only need to check alignment of array base.
1445ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    if (OffsetBase % getContext().getTypeAlign(AT->getElementType()))
1446c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1447c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1448c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Otherwise implement simplified merge. We could be smarter about
1449c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // this, but it isn't worth it and would be harder to verify.
1450c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1451ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t EltSize = getContext().getTypeSize(AT->getElementType());
1452c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    uint64_t ArraySize = AT->getSize().getZExtValue();
1453089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1454089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // The only case a 256-bit wide vector could be used is when the array
1455089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1456089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    // to work for sizes wider than 128, early check and fallback to memory.
1457089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes    if (Size > 128 && EltSize != 256)
1458089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes      return;
1459089d8927abe73fe6a806987937d9b54b1a7a8659Bruno Cardoso Lopes
1460c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) {
1461c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
14629c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner      classify(AT->getElementType(), Offset, FieldLo, FieldHi);
1463c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1464c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1465c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1466c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1467c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1468c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
14694943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1470c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification.");
14711090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    return;
14721090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  }
14738bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
14741090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1475ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    uint64_t Size = getContext().getTypeSize(Ty);
1476c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1477c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger
14784943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // than four eightbytes, ..., it has class MEMORY.
14794943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    if (Size > 256)
1480c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1481c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
14820a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial
14830a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // copy constructor or a non-trivial destructor, it is passed by invisible
14840a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    // reference.
14850a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson    if (hasNonTrivialDestructorOrCopyConstructor(RT))
14860a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson      return;
1487ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1488c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    const RecordDecl *RD = RT->getDecl();
1489c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1490c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Assume variable sized types are passed in memory.
1491c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    if (RD->hasFlexibleArrayMember())
1492c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      return;
1493c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1494ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
1495c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1496c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Reset Lo class, this will be recomputed.
1497c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    Current = NoClass;
1498ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1499ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // If this is a C++ record, classify the bases first.
1500ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1501ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1502ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar             e = CXXRD->bases_end(); i != e; ++i) {
1503ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1504ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar               "Unexpected base class!");
1505ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        const CXXRecordDecl *Base =
1506ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1507ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1508ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // Classify this field.
1509ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        //
1510ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a
1511ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // single eightbyte, each is classified separately. Each eightbyte gets
1512ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        // initialized to class NO_CLASS.
1513ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Class FieldLo, FieldHi;
1514d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        uint64_t Offset =
1515d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer          OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base));
15169c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1517ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Lo = merge(Lo, FieldLo);
1518ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        Hi = merge(Hi, FieldHi);
1519ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar        if (Lo == Memory || Hi == Memory)
1520ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar          break;
1521ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar      }
1522ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    }
1523ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar
1524ce9f423d2ce4b8699d9f6c7623053f645ac4dc6dDaniel Dunbar    // Classify the fields one at a time, merging the results.
1525c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    unsigned idx = 0;
1526548e478b8bd02b0295bc4efd0c282337f00646fdBruno Cardoso Lopes    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
152717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis           i != e; ++i, ++idx) {
1528c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1529c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      bool BitField = i->isBitField();
1530c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1531b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than
1532b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // four eightbytes, or it contains unaligned fields, it has class MEMORY.
1533c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1534b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // The only case a 256-bit wide vector could be used is when the struct
1535b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // contains a single 256-bit element. Since Lo and Hi logic isn't extended
1536b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      // to work for sizes wider than 128, early check and fallback to memory.
1537b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      //
1538b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) {
1539b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        Lo = Memory;
1540b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes        return;
1541b8981df0ed2886dfa221f2fad6d86872c39d3549Bruno Cardoso Lopes      }
1542c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Note, skip this test for bit-fields, see below.
1543ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner      if (!BitField && Offset % getContext().getTypeAlign(i->getType())) {
1544c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        Lo = Memory;
1545c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        return;
1546c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      }
1547c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1548c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Classify this field.
1549c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      //
1550c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate
1551c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // exceeds a single eightbyte, each is classified
1552c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // separately. Each eightbyte gets initialized to class
1553c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // NO_CLASS.
1554c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Class FieldLo, FieldHi;
1555c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1556c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // Bit-fields require special handling, they do not force the
1557c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // structure to be passed in memory even if unaligned, and
1558c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      // therefore they can straddle an eightbyte.
1559c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (BitField) {
1560c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        // Ignore padding bit-fields.
1561c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (i->isUnnamedBitfield())
1562c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          continue;
1563c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1564c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx);
1565a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        uint64_t Size = i->getBitWidthValue(getContext());
1566c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1567c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Lo = Offset / 64;
1568c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        uint64_t EB_Hi = (Offset + Size - 1) / 64;
1569c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        FieldLo = FieldHi = NoClass;
1570c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        if (EB_Lo) {
1571c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes.");
1572c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = NoClass;
1573c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = Integer;
1574c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        } else {
1575c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldLo = Integer;
1576c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov          FieldHi = EB_Hi ? Integer : NoClass;
1577c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        }
1578c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      } else
15799c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner        classify(i->getType(), Offset, FieldLo, FieldHi);
1580c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Lo = merge(Lo, FieldLo);
1581c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      Hi = merge(Hi, FieldHi);
1582c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      if (Lo == Memory || Hi == Memory)
1583c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov        break;
1584c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
1585c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15864943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    postMerge(Size, Lo, Hi);
1587c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
1588c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1589c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
15909c254f0415bef9a0bafe5b5026ddb54b727597b1Chris LattnerABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const {
159146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // If this is a scalar LLVM value then assume LLVM will pass it in the right
159246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  // place naturally.
1593d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
159446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    // Treat an enum type as its underlying type.
159546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
159646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar      Ty = EnumTy->getDecl()->getIntegerType();
159746c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
159846c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return (Ty->isPromotableIntegerType() ?
159946c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
160046c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  }
160146c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
160246c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  return ABIArgInfo::getIndirect(0);
160346c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar}
160446c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar
1605ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedmanbool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const {
1606ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (const VectorType *VecTy = Ty->getAs<VectorType>()) {
1607ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    uint64_t Size = getContext().getTypeSize(VecTy);
1608ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    unsigned LargestVector = HasAVX ? 256 : 128;
1609ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    if (Size <= 64 || Size > LargestVector)
1610ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return true;
1611ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
1612ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1613ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  return false;
1614ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman}
1615ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
1616edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty,
1617edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                            unsigned freeIntRegs) const {
1618c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If this is a scalar LLVM value then assume LLVM will pass it in the right
1619c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // place naturally.
1620edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1621edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // This assumption is optimistic, as there could be free registers available
1622edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // when we need to pass this argument in memory, and LLVM could try to pass
1623edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // the argument in the free register. This does not seem to happen currently,
1624edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // but this code would be much safer if we could mark the argument with
1625edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // 'onstack'. See PR12193.
1626ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) {
1627aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
1628aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
1629aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
1630aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
1631cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
1632cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
1633aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
1634c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
163546c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
163646c54fb8ec45765a475b7b709b9aee7f94c490c2Daniel Dunbar    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
16370a8f847e97f40cce51dc69051b964732333dc028Anders Carlsson
1638855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // Compute the byval alignment. We specify the alignment of the byval in all
1639855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  // cases so that the mid-level optimizer knows the alignment of the byval.
1640855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U);
1641edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1642edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // Attempt to avoid passing indirect results using byval when possible. This
1643edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // is important for good codegen.
1644edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1645edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We do this by coercing the value into a scalar type which the backend can
1646edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // handle naturally (i.e., without using byval).
1647edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1648edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // For simplicity, we currently only do this when we have exhausted all of the
1649edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // free integer registers. Doing this when there are free integer registers
1650edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // would require more care, as we would have to ensure that the coerced value
1651edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // did not claim the unused register. That would require either reording the
1652edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to the function (so that any subsequent inreg values came first),
1653edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // or only doing this optimization when there were no following arguments that
1654edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // might be inreg.
1655edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1656edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We currently expect it to be rare (particularly in well written code) for
1657edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // arguments to be passed on the stack when there are still free integer
1658edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // registers available (this would typically imply large structs being passed
1659edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // by value), so this seems like a fair tradeoff for now.
1660edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  //
1661edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // We can revisit this if the backend grows support for 'onstack' parameter
1662edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  // attributes. See PR12193.
1663edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  if (freeIntRegs == 0) {
1664edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    uint64_t Size = getContext().getTypeSize(Ty);
1665edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1666edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // If this type fits in an eightbyte, coerce it into the matching integral
1667edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    // type, which will end up on the stack (with alignment 8).
1668edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    if (Align == 8 && Size <= 64)
1669edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
1670edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                                          Size));
1671edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  }
1672edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar
1673855d227967f8332237f1f1cf8eb63a1e22d8be05Chris Lattner  return ABIArgInfo::getIndirect(Align);
1674c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
1675c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
16764943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// GetByteVectorType - The ABI specifies that a value should be passed in an
16774943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes/// full vector XMM/YMM register.  Pick an LLVM IR type that will be passed as a
16780f408f5242522cbede304472e17931357c1b573dChris Lattner/// vector register.
16794943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopesllvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const {
16809cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *IRType = CGT.ConvertType(Ty);
16818bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
168215842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // Wrapper structs that just contain vectors are passed just like vectors,
168315842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  // strip them off if present.
16849cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType);
168515842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  while (STy && STy->getNumElements() == 1) {
168615842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    IRType = STy->getElementType(0);
168715842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner    STy = dyn_cast<llvm::StructType>(IRType);
168815842bd05bd6d3b7450385ac8f73aaee5f807e19Chris Lattner  }
16898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1690528a8c7b4c39ae1c551760fd087a508a71ee9541Bruno Cardoso Lopes  // If the preferred type is a 16-byte vector, prefer to pass it.
16919cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){
16929cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = VT->getElementType();
16934943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    unsigned BitWidth = VT->getBitWidth();
1694ce275675d33142c235d7027db16abe43da616ee4Tanya Lattner    if ((BitWidth >= 128 && BitWidth <= 256) &&
16950f408f5242522cbede304472e17931357c1b573dChris Lattner        (EltTy->isFloatTy() || EltTy->isDoubleTy() ||
16960f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) ||
16970f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) ||
16980f408f5242522cbede304472e17931357c1b573dChris Lattner         EltTy->isIntegerTy(128)))
16990f408f5242522cbede304472e17931357c1b573dChris Lattner      return VT;
17000f408f5242522cbede304472e17931357c1b573dChris Lattner  }
17018bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
17020f408f5242522cbede304472e17931357c1b573dChris Lattner  return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2);
17030f408f5242522cbede304472e17931357c1b573dChris Lattner}
17040f408f5242522cbede304472e17931357c1b573dChris Lattner
1705e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// BitsContainNoUserData - Return true if the specified [start,end) bit range
1706e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// is known to either be off the end of the specified type or being in
1707e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// alignment padding.  The user type specified is known to be at most 128 bits
1708e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// in size, and have passed through X86_64ABIInfo::classify with a successful
1709e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// classification that put one of the two halves in the INTEGER class.
1710e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner///
1711e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner/// It is conservatively correct to return false.
1712e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattnerstatic bool BitsContainNoUserData(QualType Ty, unsigned StartBit,
1713e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                  unsigned EndBit, ASTContext &Context) {
1714e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If the bytes being queried are off the end of the type, there is no user
1715e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // data hiding here.  This handles analysis of builtins, vectors and other
1716e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // types that don't contain interesting padding.
1717e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  unsigned TySize = (unsigned)Context.getTypeSize(Ty);
1718e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (TySize <= StartBit)
1719e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1720e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1721021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
1722021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType());
1723021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned NumElts = (unsigned)AT->getSize().getZExtValue();
1724021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner
1725021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // Check each element to see if the element overlaps with the queried range.
1726021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    for (unsigned i = 0; i != NumElts; ++i) {
1727021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      // If the element is after the span we care about, then we're done..
1728021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltOffset = i*EltSize;
1729021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (EltOffset >= EndBit) break;
17308bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1731021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0;
1732021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner      if (!BitsContainNoUserData(AT->getElementType(), EltStart,
1733021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner                                 EndBit-EltOffset, Context))
1734021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner        return false;
1735021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    }
1736021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    // If it overlaps no elements, then it is safe to process as padding.
1737021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    return true;
1738021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
17398bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1740e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (const RecordType *RT = Ty->getAs<RecordType>()) {
1741e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const RecordDecl *RD = RT->getDecl();
1742e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
17438bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1744e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If this is a C++ record, check the bases first.
1745e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
1746e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(),
1747e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner           e = CXXRD->bases_end(); i != e; ++i) {
1748e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        assert(!i->isVirtual() && !i->getType()->isDependentType() &&
1749e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner               "Unexpected base class!");
1750e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        const CXXRecordDecl *Base =
1751e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
17528bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1753e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        // If the base is after the span we care about, ignore it.
1754d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base));
1755e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (BaseOffset >= EndBit) continue;
17568bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1757e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0;
1758e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        if (!BitsContainNoUserData(i->getType(), BaseStart,
1759e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                   EndBit-BaseOffset, Context))
1760e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner          return false;
1761e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      }
1762e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
17638bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1764e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Verify that no field has data that overlaps the region of interest.  Yes
1765e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // this could be sped up a lot by being smarter about queried fields,
1766e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // however we're only looking at structs up to 16 bytes, so we don't care
1767e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // much.
1768e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    unsigned idx = 0;
1769e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
1770e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner         i != e; ++i, ++idx) {
1771e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx);
17728bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1773e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      // If we found a field after the region we care about, then we're done.
1774e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (FieldOffset >= EndBit) break;
1775e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1776e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0;
1777e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset,
1778e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                 Context))
1779e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return false;
1780e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
17818bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1782e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If nothing in this record overlapped the area of interest, then we're
1783e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // clean.
1784e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    return true;
1785e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
17868bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1787e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  return false;
1788e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner}
1789e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
17900b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a
17910b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float member at the specified offset.  For example, {int,{float}} has a
17920b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// float at offset 4.  It is conservatively correct for this routine to return
17930b3620066bfbb33004bed1816c851a923b9301afChris Lattner/// false.
17942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattnerstatic bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset,
17950b3620066bfbb33004bed1816c851a923b9301afChris Lattner                                  const llvm::TargetData &TD) {
17960b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // Base case if we find a float.
17970b3620066bfbb33004bed1816c851a923b9301afChris Lattner  if (IROffset == 0 && IRType->isFloatTy())
17980b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return true;
17998bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18000b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is a struct, recurse into the field at the specified offset.
18012acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
18020b3620066bfbb33004bed1816c851a923b9301afChris Lattner    const llvm::StructLayout *SL = TD.getStructLayout(STy);
18030b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned Elt = SL->getElementContainingOffset(IROffset);
18040b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= SL->getElementOffset(Elt);
18050b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD);
18060b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
18078bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18080b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // If this is an array, recurse into the field at the specified offset.
18092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
18102acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *EltTy = ATy->getElementType();
18110b3620066bfbb33004bed1816c851a923b9301afChris Lattner    unsigned EltSize = TD.getTypeAllocSize(EltTy);
18120b3620066bfbb33004bed1816c851a923b9301afChris Lattner    IROffset -= IROffset/EltSize*EltSize;
18130b3620066bfbb33004bed1816c851a923b9301afChris Lattner    return ContainsFloatAtOffset(EltTy, IROffset, TD);
18140b3620066bfbb33004bed1816c851a923b9301afChris Lattner  }
18150b3620066bfbb33004bed1816c851a923b9301afChris Lattner
18160b3620066bfbb33004bed1816c851a923b9301afChris Lattner  return false;
18170b3620066bfbb33004bed1816c851a923b9301afChris Lattner}
18180b3620066bfbb33004bed1816c851a923b9301afChris Lattner
1819f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1820f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// GetSSETypeAtOffset - Return a type that will be passed by the backend in the
1821f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner/// low 8 bytes of an XMM register, corresponding to the SSE class.
18229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
18239cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset,
1824f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                   QualType SourceTy, unsigned SourceOffset) const {
1825cba8d310163f84630fd140fbfa9b6fdad9d26587Chris Lattner  // The only three choices we have are either double, <2 x float>, or float. We
1826f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // pass as float if the last 4 bytes is just padding.  This happens for
1827f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  // structs that contain 3 floats.
1828f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  if (BitsContainNoUserData(SourceTy, SourceOffset*8+32,
1829f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner                            SourceOffset*8+64, getContext()))
1830f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner    return llvm::Type::getFloatTy(getVMContext());
18318bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18320b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // We want to pass as <2 x float> if the LLVM IR type contains a float at
18330b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // offset+0 and offset+4.  Walk the LLVM IR type to find out if this is the
18340b3620066bfbb33004bed1816c851a923b9301afChris Lattner  // case.
18350b3620066bfbb33004bed1816c851a923b9301afChris Lattner  if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) &&
183622fd4baf2eba2103e2b41e463f1a5f6486c398fbChris Lattner      ContainsFloatAtOffset(IRType, IROffset+4, getTargetData()))
183722fd4baf2eba2103e2b41e463f1a5f6486c398fbChris Lattner    return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2);
18388bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1839f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner  return llvm::Type::getDoubleTy(getVMContext());
1840f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner}
1841f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
1842f47c944b5710a545d564b4d4b641a2f8bac96af3Chris Lattner
18430d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in
18440d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// an 8-byte GPR.  This means that we either have a scalar or we are talking
18450d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// about the high or low part of an up-to-16-byte struct.  This routine picks
18460d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner/// the best LLVM IR type to represent this, which may be i64 or may be anything
1847519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// else that the backend will pass in a GPR that works better (e.g. i8, %foo*,
1848519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// etc).
1849519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1850519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// PrefType is an LLVM IR type that corresponds to (part of) the IR type for
1851519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the source type.  IROffset is an offset in bytes into the LLVM IR type that
1852519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// the 8-byte value references.  PrefType may be null.
1853519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
1854519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// SourceTy is the source level type for the entire argument.  SourceOffset is
1855519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner/// an offset into this that we're processing (which is always either 0 or 8).
1856519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner///
18579cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerllvm::Type *X86_64ABIInfo::
18589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris LattnerGetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset,
18590d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                       QualType SourceTy, unsigned SourceOffset) const {
1860e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // If we're dealing with an un-offset LLVM IR type, then it means that we're
1861e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  // returning an 8-byte unit starting with it.  See if we can safely use it.
1862e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  if (IROffset == 0) {
1863e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // Pointers and int64's always fill the 8-byte unit.
1864e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64))
1865e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      return IRType;
1866e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner
1867e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // If we have a 1/2/4-byte integer, we can use it only if the rest of the
1868e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // goodness in the source type is just tail padding.  This is allowed to
1869e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // kick in for struct {double,int} on the int, but not on
1870e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // struct{double,int,int} because we wouldn't return the second int.  We
1871e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // have to do this analysis on the source type because we can't depend on
1872e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    // unions being lowered a specific way etc.
1873e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) ||
1874e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        IRType->isIntegerTy(32)) {
1875e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth();
18768bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1877e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner      if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth,
1878e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner                                SourceOffset*8+64, getContext()))
1879e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner        return IRType;
1880e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner    }
1881e2962be11e8894329d37985eccaa4f4a12dea402Chris Lattner  }
1882519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
18832acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) {
1884519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    // If this is a struct, recurse into the field at the specified offset.
188544f0fd2804e9952a8dbf85bb60ee3501aa9f5ee7Chris Lattner    const llvm::StructLayout *SL = getTargetData().getStructLayout(STy);
1886519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    if (IROffset < SL->getSizeInBytes()) {
1887519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      unsigned FieldIdx = SL->getElementContainingOffset(IROffset);
1888519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner      IROffset -= SL->getElementOffset(FieldIdx);
18898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18900d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner      return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset,
18910d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                    SourceTy, SourceOffset);
18928bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    }
1893519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  }
18948bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
18952acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) {
18969cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *EltTy = ATy->getElementType();
1897021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltSize = getTargetData().getTypeAllocSize(EltTy);
1898021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner    unsigned EltOffset = IROffset/EltSize*EltSize;
18990d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner    return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy,
19000d2656d77053cc2ed6f3a3acdf12d67807c7f3a2Chris Lattner                                  SourceOffset);
1901021c3a349d4f55cc2c7970268758bcf37b924493Chris Lattner  }
19028bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1903519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // Okay, we don't have any better idea of what to pass, so we pass this in an
1904519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // integer register that isn't too big to fit the rest of the struct.
19059e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  unsigned TySizeInBytes =
19069e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner    (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity();
1907519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
19089e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  assert(TySizeInBytes != SourceOffset && "Empty field?");
19098bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1910519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // It is always safe to classify this as an integer type up to i64 that
1911519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner  // isn't larger than the structure.
19129e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner  return llvm::IntegerType::get(getVMContext(),
19139e45a3de3f462785a86bba77dee168ab354d9704Chris Lattner                                std::min(TySizeInBytes-SourceOffset, 8U)*8);
1914519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner}
1915519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner
191666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
191766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// GetX86_64ByValArgumentPair - Given a high and low type that can ideally
191866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// be used as elements of a two register pair to pass or return, return a
191966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// first class aggregate to represent them.  For example, if the low part of
192066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// a by-value argument should be passed as i32* and the high part as float,
192166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner/// return {i32*, float}.
19229cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattnerstatic llvm::Type *
1923ef6de3da8572607f786303c07150daa6e140ab19Jay FoadGetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi,
192466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner                           const llvm::TargetData &TD) {
192566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // In order to correctly satisfy the ABI, we need to the high part to start
192666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // at offset 8.  If the high and low parts we inferred are both 4-byte types
192766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have
192866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // the second element at offset 8.  Check for this:
192966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo);
193066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned HiAlign = TD.getABITypeAlignment(Hi);
193166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign);
193266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!");
19339cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
193466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // To handle this, we have to increase the size of the low part so that the
193566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // second element will start at an 8 byte offset.  We can't increase the size
193666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // of the second element because it might make us access off the end of the
193766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // struct.
193866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HiStart != 8) {
193966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // There are only two sorts of types the ABI generation code can produce for
194066e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32.
194166e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    // Promote these to a larger type.
194266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    if (Lo->isFloatTy())
194366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getDoubleTy(Lo->getContext());
194466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    else {
194566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      assert(Lo->isIntegerTy() && "Invalid/unknown lo type");
194666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner      Lo = llvm::Type::getInt64Ty(Lo->getContext());
194766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    }
194866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  }
19499cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
19509cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL);
19519cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
19529cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
195366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  // Verify that the second element is at an 8-byte offset.
195466e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 &&
195566e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner         "Invalid x86-64 argument pair!");
195666e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  return Result;
195766e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner}
195866e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner
19591090a9ba0902380dbd97d0a500daa4c373712df9Chris LattnerABIArgInfo X86_64ABIInfo::
1960a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerclassifyReturnType(QualType RetTy) const {
1961c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the
1962c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // classification algorithm.
1963c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
19649c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(RetTy, 0, Lo, Hi);
1965c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1966c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
1967c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
1968c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
1969c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
19709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
1971c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
1972c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
1973117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
1974117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
1975117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
1976117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
1977117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
1978117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
1979117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
1980c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1981c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
1982c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
1983b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
1984c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1985c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via
1986c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // hidden argument.
1987c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
19889c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    return getIndirectReturnResult(RetTy);
1989c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
1990c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next
1991c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rax, %rdx is used.
1992c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
19939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
19948bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
1995eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
1996eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
1997eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
1998eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
1999eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2000eb518b4b89e4134b21975530809697142f69b779Chris Lattner        RetTy = EnumTy->getDecl()->getIntegerType();
20018bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2002eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (RetTy->isIntegralOrEnumerationType() &&
2003eb518b4b89e4134b21975530809697142f69b779Chris Lattner          RetTy->isPromotableIntegerType())
2004eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
2005eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
2006519f68cd26777c755763a644a7f7ed7ac389beb9Chris Lattner    break;
2007c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2008c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next
2009c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register of the sequence %xmm0, %xmm1 is used.
2010c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSE:
20119cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0);
20120b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
2013c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2014c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is
2015c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned on the X87 stack in %st0 as 80-bit x87 number.
2016c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2017ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    ResType = llvm::Type::getX86_FP80Ty(getVMContext());
20180b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
2019c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2020c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real
2021c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // part of the value is returned in %st0 and the imaginary part in
2022c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // %st1.
2023c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2024c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification.");
20257650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()),
2026ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner                                    llvm::Type::getX86_FP80Ty(getVMContext()),
2027c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                    NULL);
2028c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2029c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2030c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
20319cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
2032c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
2033c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously and X87 should
2034c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // never occur as a hi class.
2035c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2036c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2037b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
2038c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2039c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87: // Previously handled.
20400b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner  case NoClass:
20410b30c67132f00c667512a65cfe1fe81ae54c2383Chris Lattner    break;
2042c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
20433db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case Integer:
20449cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
20453db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
20463db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2047c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
20483db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  case SSE:
20499cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
20503db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner    if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
20513db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2052c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2053c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2054c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte
20554943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // is passed in the next available eightbyte chunk if the last used
20564943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    // vector register.
2057c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    //
2058fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // SSEUP should always be preceded by SSE, just widen.
2059c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2060c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(Lo == SSE && "Unexpected SSEUp classification.");
20614943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(RetTy);
2062c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2063c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2064c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is
2065c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // returned together with the previous X87 value in %st0.
2066c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2067fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // If X87Up is preceded by X87, we don't need to do
2068c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // anything. However, in some cases with unions it may not be
2069fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // preceded by X87. In such situations we follow gcc and pass the
2070c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // extra bits in an SSE reg.
2071603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    if (Lo != X87) {
20729cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner      HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8);
20733db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner      if (Lo == NoClass)  // Return HighPart at offset 8 in memory.
20743db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner        return ABIArgInfo::getDirect(HighPart, 8);
2075603519d269d48dca99927f0ad65e92099bd76161Chris Lattner    }
2076c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2077c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
20789cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
20793db4dde12de84269c8f803f9dfca37a2d14f9898Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
2080645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
2081645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
208266e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner  if (HighPart)
208366e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
2084c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2085eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
20869c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner}
20879c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner
2088edfac0302490d84419eb958c812c533b8df29785Daniel DunbarABIArgInfo X86_64ABIInfo::classifyArgumentType(
2089edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE)
2090edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  const
2091edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar{
2092c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  X86_64ABIInfo::Class Lo, Hi;
20939c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner  classify(Ty, 0, Lo, Hi);
20948bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2095c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Check some invariants.
2096c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: Enforce these by construction.
2097c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != Memory || Lo == Memory) && "Invalid memory classification.");
2098c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification.");
2099c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2100c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededInt = 0;
2101c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  neededSSE = 0;
21029cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ResType = 0;
2103c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Lo) {
2104c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass:
2105117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    if (Hi == NoClass)
2106117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner      return ABIArgInfo::getIgnore();
2107117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // If the low part is just padding, it takes no register, leave ResType
2108117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    // null.
2109117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    assert((Hi == SSE || Hi == Integer || Hi == X87Up) &&
2110117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner           "Unknown missing lo part");
2111117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner    break;
21128bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2113c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument
2114c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // on the stack.
2115c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2116c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2117c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or
2118c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // COMPLEX_X87, it is passed in memory.
2119c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2120c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2121ded137fcab19f0aace08a28b5c91574e6b23debcEli Friedman    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
2122ded137fcab19f0aace08a28b5c91574e6b23debcEli Friedman      ++neededInt;
2123edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    return getIndirectResult(Ty, freeIntRegs);
2124c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2125c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2126c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2127b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for lo word.");
2128c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2129c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next
2130c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8
2131c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // and %r9 is used.
2132c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Integer:
21339c254f0415bef9a0bafe5b5026ddb54b727597b1Chris Lattner    ++neededInt;
21348bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
213549382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
21369cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0);
2137eb518b4b89e4134b21975530809697142f69b779Chris Lattner
2138eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // If we have a sign or zero extended integer, make sure to return Extend
2139eb518b4b89e4134b21975530809697142f69b779Chris Lattner    // so that the parameter gets the right LLVM IR attributes.
2140eb518b4b89e4134b21975530809697142f69b779Chris Lattner    if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) {
2141eb518b4b89e4134b21975530809697142f69b779Chris Lattner      // Treat an enum type as its underlying type.
2142eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2143eb518b4b89e4134b21975530809697142f69b779Chris Lattner        Ty = EnumTy->getDecl()->getIntegerType();
21448bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2145eb518b4b89e4134b21975530809697142f69b779Chris Lattner      if (Ty->isIntegralOrEnumerationType() &&
2146eb518b4b89e4134b21975530809697142f69b779Chris Lattner          Ty->isPromotableIntegerType())
2147eb518b4b89e4134b21975530809697142f69b779Chris Lattner        return ABIArgInfo::getExtend();
2148eb518b4b89e4134b21975530809697142f69b779Chris Lattner    }
21498bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2150c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2151c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2152c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next
2153c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // available SSE register is used, the registers are taken in the
2154c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // order from %xmm0 to %xmm7.
2155bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  case SSE: {
21569cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    llvm::Type *IRType = CGT.ConvertType(Ty);
215714508ff0bffee0fdfe5d336946c6db0e709099c8Eli Friedman    ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0);
215899aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    ++neededSSE;
2159c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2160c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2161bb465d7d0489a6605bb1eb82dea87350066ac5e2Bill Wendling  }
2162c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
21639cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *HighPart = 0;
2164c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  switch (Hi) {
2165c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // Memory was handled previously, ComplexX87 and X87 should
2166fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // never occur as hi classes, and X87Up must be preceded by X87,
2167c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // which is passed in memory.
2168c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case Memory:
2169c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87:
2170c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case ComplexX87:
2171b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Invalid classification for hi word.");
2172c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2173c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case NoClass: break;
21748bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2175645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case Integer:
2176c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededInt;
217749382de42c2a411bfd772408e987cb399071241dChris Lattner    // Pick an 8-byte type based on the preferred type.
21789cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
2179117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2180645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2181645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2182c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2183c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2184c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // X87Up generally doesn't occur here (long double is passed in
2185c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // memory), except in situations involving unions.
2186c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case X87Up:
2187645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  case SSE:
21889cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner    HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8);
21898bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2190645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner    if (Lo == NoClass)  // Pass HighPart at offset 8 in memory.
2191645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner      return ABIArgInfo::getDirect(HighPart, 8);
2192117e3f4cd4d6ea41c3202da8729f94168c5c8239Chris Lattner
2193c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    ++neededSSE;
2194c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2195c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2196c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the
2197c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte is passed in the upper half of the last used SSE
21988bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer    // register.  This only happens when 128-bit vectors are passed.
2199c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  case SSEUp:
2200ab5722e67794b3954c874a369086fc5f41ac46a5Chris Lattner    assert(Lo == SSE && "Unexpected SSEUp classification");
22014943c15df59fdec444656a48c16e72a2077ab61fBruno Cardoso Lopes    ResType = GetByteVectorType(Ty);
2202c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    break;
2203c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2204c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2205645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // If a high part was specified, merge it together with the low part.  It is
2206645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // known to pass in the high eightbyte of the result.  We do this by forming a
2207645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  // first class struct aggregate with the high and low part: {low, high}
2208645406a3d3405ad0f4b5a0e46a34ae92d9d23bd3Chris Lattner  if (HighPart)
220966e7b68b0016aeebe349e21ace93ff0178665d69Chris Lattner    ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData());
22109cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2211eb518b4b89e4134b21975530809697142f69b779Chris Lattner  return ABIArgInfo::getDirect(ResType);
2212c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2213c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2214ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
22158bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2216a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2217c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2218c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Keep track of the number of assigned registers.
221999aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned freeIntRegs = 6, freeSSERegs = 8;
2220c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2221c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // If the return value is indirect, then the hidden argument is consuming one
2222c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // integer register.
2223c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (FI.getReturnInfo().isIndirect())
2224c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    --freeIntRegs;
2225c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2226c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers
2227c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // get assigned (in left-to-right order) for passing as follows...
2228c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2229c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov       it != ie; ++it) {
223099aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    unsigned neededInt, neededSSE;
2231edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar    it->info = classifyArgumentType(it->type, freeIntRegs, neededInt,
2232edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar                                    neededSSE);
2233c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2234c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // AMD64-ABI 3.2.3p3: If there are no registers available for any
2235c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbyte of an argument, the whole argument is passed on the
2236c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // stack. If registers have already been assigned for some
2237c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // eightbytes of such an argument, the assignments get reverted.
223899aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling    if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) {
2239c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeIntRegs -= neededInt;
2240c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      freeSSERegs -= neededSSE;
2241c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    } else {
2242edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar      it->info = getIndirectResult(it->type, freeIntRegs);
2243c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
2244c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2245c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2246c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2247c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovstatic llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr,
2248c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        QualType Ty,
2249c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                        CodeGenFunction &CGF) {
2250c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area_p =
2251c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p");
2252c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *overflow_arg_area =
2253c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area");
2254c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2255c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16
2256c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // byte boundary if alignment needed by type exceeds 8 byte boundary.
22578d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // It isn't stated explicitly in the standard, but in practice we use
22588d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman  // alignment greater than 16 where necessary.
2259c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8;
2260c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (Align > 8) {
22618d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    // overflow_arg_area = (overflow_arg_area + align - 1) & -align;
22620032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson    llvm::Value *Offset =
22638d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman      llvm::ConstantInt::get(CGF.Int64Ty, Align - 1);
2264c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset);
2265c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area,
226677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                                    CGF.Int64Ty);
22678d2fe42417fcc861b3324d585dc29ac4da59bee0Eli Friedman    llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align);
2268c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    overflow_arg_area =
2269c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask),
2270c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 overflow_arg_area->getType(),
2271c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 "overflow_arg_area.align");
2272c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2273c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2274c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area.
22752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2276c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Res =
2277c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateBitCast(overflow_arg_area,
227896e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                              llvm::PointerType::getUnqual(LTy));
2279c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2280c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to:
2281c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->overflow_arg_area + sizeof(type).
2282c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to
2283c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an 8 byte boundary.
2284c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2285c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8;
22860032b2781b4deb131f8c9b7968f2030bf2489cddOwen Anderson  llvm::Value *Offset =
228777b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7)  & ~7);
2288c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset,
2289c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                            "overflow_arg_area.next");
2290c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p);
2291c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2292c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type.
2293c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return Res;
2294c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2295c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2296c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2297c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                      CodeGenFunction &CGF) const {
2298c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Assume that va_list type is correct; should be pointer to LLVM type:
2299c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // struct {
2300c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 gp_offset;
2301c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i32 fp_offset;
2302c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* overflow_arg_area;
2303c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //   i8* reg_save_area;
2304c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // };
230599aaae87ae972ac2dd4cccd8b4886537aabaff43Bill Wendling  unsigned neededInt, neededSSE;
23068bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2307a14db75641f377ef8b033c67653cd95ac4c36fe3Chris Lattner  Ty = CGF.getContext().getCanonicalType(Ty);
2308edfac0302490d84419eb958c812c533b8df29785Daniel Dunbar  ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE);
2309c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2310c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed
2311c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in the registers. If not go to step 7.
2312c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (!neededInt && !neededSSE)
2313c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2314c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2315c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of
2316c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // general purpose registers needed to pass type and num_fp to hold
2317c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // the number of floating point registers needed.
2318c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2319c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into
2320c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // registers. In the case: l->gp_offset > 48 - num_gp * 8 or
2321c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset > 304 - num_fp * 16 go to step 7.
2322c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of
2324c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // register save space).
2325c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2326c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *InRegs = 0;
2327c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *gp_offset_p = 0, *gp_offset = 0;
2328c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *fp_offset_p = 0, *fp_offset = 0;
2329c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
2330c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p");
2331c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset");
23321090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8);
23331090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp");
2334c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2335c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2336c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
2337c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p");
2338c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset");
2339c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FitsInFP =
23401090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner      llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16);
23411090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp");
2342c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP;
2343c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2344c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2345c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg");
2346c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem");
2347c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end");
2348c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock);
2349c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2350c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in registers.
2351c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2352c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InRegBlock);
2353c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2354c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with
2355c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // an offset of l->gp_offset and/or l->fp_offset. This may require
2356c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // copying to a temporary location in case the parameter is passed
2357c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // in different register classes or requires an alignment greater
2358c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // than 8 for general purpose registers and 16 for XMM registers.
2359c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  //
2360c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // FIXME: This really results in shameful code when we end up needing to
2361c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // collect arguments from different places; often what should result in a
2362c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // simple assembling of a structure from scattered addresses has many more
2363c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // loads than necessary. Can we clean this up?
23642acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type *LTy = CGF.ConvertTypeForMem(Ty);
2365c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *RegAddr =
2366c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3),
2367c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                           "reg_save_area");
2368c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt && neededSSE) {
2369c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    // FIXME: Cleanup.
2370800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    assert(AI.isDirect() && "Unexpected ABI info for mixed regs");
23712acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType());
2372c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *Tmp = CGF.CreateTempAlloca(ST);
2373c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs");
23742acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyLo = ST->getElementType(0);
23752acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *TyHi = ST->getElementType(1);
2376a8b7a7d3eaa51dd200cba1e5541f2542d24d7a6eChris Lattner    assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) &&
2377c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov           "Unexpected ABI info for mixed regs");
23782acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo);
23792acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi);
2380c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2381c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2382f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr;
2383f177d9d6c27fbbcee8c00fd90b8306985c03c54aDuncan Sands    llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr;
2384c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::Value *V =
2385c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov      CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo));
2386c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2387c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi));
2388c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2389c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2390a1cf15f4680e5cf39e72e28c5ea854fcba792e84Owen Anderson    RegAddr = CGF.Builder.CreateBitCast(Tmp,
239196e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2392c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else if (neededInt) {
2393c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset);
2394c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
239596e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson                                        llvm::PointerType::getUnqual(LTy));
2396dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner  } else if (neededSSE == 1) {
2397dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset);
2398dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(RegAddr,
2399dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2400c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  } else {
2401dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    assert(neededSSE == 2 && "Invalid number of needed registers!");
2402dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // SSE registers are spaced 16 bytes apart in the register save
2403dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    // area, we need to collect the two eightbytes together.
2404dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset);
24051090a9ba0902380dbd97d0a500daa4c373712df9Chris Lattner    llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16);
24068b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Type *DoubleTy = CGF.DoubleTy;
24072acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::Type *DblPtrTy =
2408dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner      llvm::PointerType::getUnqual(DoubleTy);
24092acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner    llvm::StructType *ST = llvm::StructType::get(DoubleTy,
2410dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                       DoubleTy, NULL);
2411dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST);
2412dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo,
2413dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2414dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0));
2415dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi,
2416dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                                         DblPtrTy));
2417dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1));
2418dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner    RegAddr = CGF.Builder.CreateBitCast(Tmp,
2419dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner                                        llvm::PointerType::getUnqual(LTy));
2420c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2421c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2422c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // AMD64-ABI 3.5.7p5: Step 5. Set:
2423c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->gp_offset = l->gp_offset + num_gp * 8
2424c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // l->fp_offset = l->fp_offset + num_fp * 16.
2425c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededInt) {
242677b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8);
2427c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset),
2428c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            gp_offset_p);
2429c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2430c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  if (neededSSE) {
243177b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16);
2432c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset),
2433c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                            fp_offset_p);
2434c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2435c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBranch(ContBlock);
2436c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2437c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Emit code to load the value if it was passed in memory.
2438c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2439c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(InMemBlock);
2440c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF);
2441c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2442c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  // Return the appropriate result.
2443c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2444c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGF.EmitBlock(ContBlock);
2445bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad  llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2,
2446c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                                 "vaarg.addr");
2447c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(RegAddr, InRegBlock);
2448c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  ResAddr->addIncoming(MemAddr, InMemBlock);
2449c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return ResAddr;
2450c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2451c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2452a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA TakumiABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const {
2453a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2454a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isVoidType())
2455a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIgnore();
2456a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2457a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2458a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    Ty = EnumTy->getDecl()->getIntegerType();
2459a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2460a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  uint64_t Size = getContext().getTypeSize(Ty);
2461a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2462a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (const RecordType *RT = Ty->getAs<RecordType>()) {
2463ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi    if (hasNonTrivialDestructorOrCopyConstructor(RT) ||
2464ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi        RT->getDecl()->hasFlexibleArrayMember())
2465a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2466a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
24676f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // FIXME: mingw-w64-gcc emits 128-bit struct as i128
24686f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    if (Size == 128 &&
246955fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman        getContext().getTargetInfo().getTriple().getOS()
247055fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman          == llvm::Triple::MinGW32)
24716f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
24726f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi                                                          Size));
24736f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi
24746f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is
24756f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    // not 1, 2, 4, or 8 bytes, must be passed by reference."
24766f17433b2d50262856ab09f52af96c6132b01012NAKAMURA Takumi    if (Size <= 64 &&
2477ff8be0e08e409af53130d12ce36019b35288fb78NAKAMURA Takumi        (Size & (Size - 1)) == 0)
2478a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2479a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi                                                          Size));
2480a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2481a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
2482a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  }
2483a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2484a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  if (Ty->isPromotableIntegerType())
2485a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    return ABIArgInfo::getExtend();
2486a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2487a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  return ABIArgInfo::getDirect();
2488a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2489a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2490a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumivoid WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const {
2491a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2492a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  QualType RetTy = FI.getReturnType();
2493a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  FI.getReturnInfo() = classify(RetTy);
2494a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2495a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2496a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi       it != ie; ++it)
2497a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi    it->info = classify(it->type);
2498a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi}
2499a75732201b19059a0e56a88b0eb5a0e5dd3c6ca3NAKAMURA Takumi
2500f13721dd91dda7675e499331a2770308ad20ca61Chris Lattnerllvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2501f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                      CodeGenFunction &CGF) const {
25028b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
2503f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2504f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  CGBuilderTy &Builder = CGF.Builder;
2505f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
2506f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                                                       "ap");
2507f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
2508f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Type *PTy =
2509f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
2510f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
2511f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner
2512f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  uint64_t Offset =
2513f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8);
2514f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  llvm::Value *NextAddr =
2515f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
2516f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner                      "ap.next");
2517f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
2518dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2519f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner  return AddrTyped;
2520f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner}
2521dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
2522ec853ba1087f606e9685cb1e800616565ba35093John McCall// PowerPC-32
2523ec853ba1087f606e9685cb1e800616565ba35093John McCall
2524ec853ba1087f606e9685cb1e800616565ba35093John McCallnamespace {
2525ec853ba1087f606e9685cb1e800616565ba35093John McCallclass PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
2526ec853ba1087f606e9685cb1e800616565ba35093John McCallpublic:
2527ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
25288bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2529ec853ba1087f606e9685cb1e800616565ba35093John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
2530ec853ba1087f606e9685cb1e800616565ba35093John McCall    // This is recovered from gcc output.
2531ec853ba1087f606e9685cb1e800616565ba35093John McCall    return 1; // r1 is the dedicated stack pointer
2532ec853ba1087f606e9685cb1e800616565ba35093John McCall  }
2533ec853ba1087f606e9685cb1e800616565ba35093John McCall
2534ec853ba1087f606e9685cb1e800616565ba35093John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
25358bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
2536ec853ba1087f606e9685cb1e800616565ba35093John McCall};
2537ec853ba1087f606e9685cb1e800616565ba35093John McCall
2538ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2539ec853ba1087f606e9685cb1e800616565ba35093John McCall
2540ec853ba1087f606e9685cb1e800616565ba35093John McCallbool
2541ec853ba1087f606e9685cb1e800616565ba35093John McCallPPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
2542ec853ba1087f606e9685cb1e800616565ba35093John McCall                                                llvm::Value *Address) const {
2543ec853ba1087f606e9685cb1e800616565ba35093John McCall  // This is calculated from the LLVM and GCC tables and verified
2544ec853ba1087f606e9685cb1e800616565ba35093John McCall  // against gcc output.  AFAIK all ABIs use the same encoding.
2545ec853ba1087f606e9685cb1e800616565ba35093John McCall
2546ec853ba1087f606e9685cb1e800616565ba35093John McCall  CodeGen::CGBuilderTy &Builder = CGF.Builder;
2547ec853ba1087f606e9685cb1e800616565ba35093John McCall
25488b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::IntegerType *i8 = CGF.Int8Ty;
2549ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
2550ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
2551ec853ba1087f606e9685cb1e800616565ba35093John McCall  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
2552ec853ba1087f606e9685cb1e800616565ba35093John McCall
2553ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 0-31: r0-31, the 4-byte general-purpose registers
2554aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 0, 31);
2555ec853ba1087f606e9685cb1e800616565ba35093John McCall
2556ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 32-63: fp0-31, the 8-byte floating-point registers
2557aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
2558ec853ba1087f606e9685cb1e800616565ba35093John McCall
2559ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64-76 are various 4-byte special-purpose registers:
2560ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 64: mq
2561ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 65: lr
2562ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 66: ctr
2563ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 67: ap
2564ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 68-75 cr0-7
2565ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 76: xer
2566aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 64, 76);
2567ec853ba1087f606e9685cb1e800616565ba35093John McCall
2568ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 77-108: v0-31, the 16-byte vector registers
2569aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
2570ec853ba1087f606e9685cb1e800616565ba35093John McCall
2571ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 109: vrsave
2572ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 110: vscr
2573ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 111: spe_acc
2574ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 112: spefscr
2575ec853ba1087f606e9685cb1e800616565ba35093John McCall  // 113: sfp
2576aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  AssignToArrayRange(Builder, Address, Four8, 109, 113);
2577ec853ba1087f606e9685cb1e800616565ba35093John McCall
25788bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer  return false;
2579ec853ba1087f606e9685cb1e800616565ba35093John McCall}
2580ec853ba1087f606e9685cb1e800616565ba35093John McCall
25810fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky// PowerPC-64
25820fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25830fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackynamespace {
25840fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackyclass PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo {
25850fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackypublic:
25860fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {}
25870fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25880fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
25890fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    // This is recovered from gcc output.
25900fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    return 1; // r1 is the dedicated stack pointer
25910fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  }
25920fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25930fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
25940fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky                               llvm::Value *Address) const;
25950fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky};
25960fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25970fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky}
25980fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
25990fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divackybool
26000fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman DivackyPPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
26010fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky                                                llvm::Value *Address) const {
26020fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // This is calculated from the LLVM and GCC tables and verified
26030fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // against gcc output.  AFAIK all ABIs use the same encoding.
26040fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26050fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  CodeGen::CGBuilderTy &Builder = CGF.Builder;
26060fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26070fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::IntegerType *i8 = CGF.Int8Ty;
26080fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4);
26090fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8);
26100fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16);
26110fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26120fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 0-31: r0-31, the 8-byte general-purpose registers
26130fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Eight8, 0, 31);
26140fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26150fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 32-63: fp0-31, the 8-byte floating-point registers
26160fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Eight8, 32, 63);
26170fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26180fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 64-76 are various 4-byte special-purpose registers:
26190fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 64: mq
26200fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 65: lr
26210fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 66: ctr
26220fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 67: ap
26230fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 68-75 cr0-7
26240fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 76: xer
26250fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Four8, 64, 76);
26260fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26270fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 77-108: v0-31, the 16-byte vector registers
26280fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Sixteen8, 77, 108);
26290fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26300fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 109: vrsave
26310fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 110: vscr
26320fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 111: spe_acc
26330fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 112: spefscr
26340fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  // 113: sfp
26350fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  AssignToArrayRange(Builder, Address, Four8, 109, 113);
26360fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky
26370fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  return false;
26380fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky}
2639ec853ba1087f606e9685cb1e800616565ba35093John McCall
2640dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
264134d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar// ARM ABI Implementation
2642dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
264334d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
264434d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbarnamespace {
264534d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
2646c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovclass ARMABIInfo : public ABIInfo {
26475e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
26485e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  enum ABIKind {
26495e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    APCS = 0,
26505e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS = 1,
26515e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    AAPCS_VFP
26525e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  };
26535e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
26545e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
26555e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind Kind;
26565e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
26575e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarpublic:
2658ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {}
26595e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
266049e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  bool isEABI() const {
266155fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    StringRef Env =
266255fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      getContext().getTargetInfo().getTriple().getEnvironmentName();
266394a7142f74bb4a9daa53c22087b19d4568073109Logan Chien    return (Env == "gnueabi" || Env == "eabi" ||
266494a7142f74bb4a9daa53c22087b19d4568073109Logan Chien            Env == "android" || Env == "androideabi");
266549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
266649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
26675e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbarprivate:
26685e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  ABIKind getABIKind() const { return Kind; }
26695e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
2670a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyReturnType(QualType RetTy) const;
2671a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  ABIArgInfo classifyArgumentType(QualType RetTy) const;
2672c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2673ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattner  virtual void computeInfo(CGFunctionInfo &FI) const;
2674c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2675c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
2676c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                                 CodeGenFunction &CGF) const;
2677c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov};
2678c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
267982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass ARMTargetCodeGenInfo : public TargetCodeGenInfo {
268082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
2681ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K)
2682ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {}
26836374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall
268449e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  const ARMABIInfo &getABIInfo() const {
268549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo());
268649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
268749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
26886374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
26896374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall    return 13;
26906374c3307e2d73348f7b8cc73eeeb0998ad0ac94John McCall  }
269109345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
26925f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef getARCRetainAutoreleasedReturnValueMarker() const {
2693f85e193739c953358c865005855253af4f68a497John McCall    return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue";
2694f85e193739c953358c865005855253af4f68a497John McCall  }
2695f85e193739c953358c865005855253af4f68a497John McCall
269609345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
269709345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky                               llvm::Value *Address) const {
26988b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
269909345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky
270009345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    // 0-15 are the 16 integer registers.
27018b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner    AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15);
270209345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky    return false;
270309345d1c2adf95ea90f06911dbb4f12372b7f24cRoman Divacky  }
270449e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
270549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
270649e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    if (getABIInfo().isEABI()) return 88;
270749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall    return TargetCodeGenInfo::getSizeOfUnwindException();
270849e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
270982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
271082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
271134d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar}
271234d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
2713ee5dcd064a811edc90f6c1fb31a837b6c961fed7Chris Lattnervoid ARMABIInfo::computeInfo(CGFunctionInfo &FI) const {
2714a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
2715c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
2716a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner       it != ie; ++it)
2717a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    it->info = classifyArgumentType(it->type);
27185e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
2719414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // Always honor user-specified calling convention.
2720414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  if (FI.getCallingConvention() != llvm::CallingConv::C)
2721414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    return;
2722414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov
2723414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // Calling convention as default by an ABI.
272425117ab35c1a033846073183314c68ef07d1701aRafael Espindola  llvm::CallingConv::ID DefaultCC;
272549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  if (isEABI())
272625117ab35c1a033846073183314c68ef07d1701aRafael Espindola    DefaultCC = llvm::CallingConv::ARM_AAPCS;
27271ed1a594e9befc91ebf00d81b41a2fdfab862657Rafael Espindola  else
27281ed1a594e9befc91ebf00d81b41a2fdfab862657Rafael Espindola    DefaultCC = llvm::CallingConv::ARM_APCS;
272925117ab35c1a033846073183314c68ef07d1701aRafael Espindola
2730414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // If user did not ask for specific calling convention explicitly (e.g. via
2731414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // pcs attribute), set effective calling convention if it's different than ABI
2732414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov  // default.
27335e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  switch (getABIKind()) {
27345e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case APCS:
273525117ab35c1a033846073183314c68ef07d1701aRafael Espindola    if (DefaultCC != llvm::CallingConv::ARM_APCS)
273625117ab35c1a033846073183314c68ef07d1701aRafael Espindola      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS);
27375e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
27385e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case AAPCS:
273925117ab35c1a033846073183314c68ef07d1701aRafael Espindola    if (DefaultCC != llvm::CallingConv::ARM_AAPCS)
274025117ab35c1a033846073183314c68ef07d1701aRafael Espindola      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS);
27415e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
27425e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  case AAPCS_VFP:
2743414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov    if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP)
2744414d8967e1d760ea1e19a4aca96b13777a8cf8c5Anton Korobeynikov      FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP);
27455e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar    break;
27465e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar  }
2747c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2748c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2749194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous
2750194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// aggregate.  If HAMembers is non-null, the number of base elements
2751194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// contained in the type is returned through it; this is used for the
2752194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson/// recursive calls that check aggregate component types.
2753194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilsonstatic bool isHomogeneousAggregate(QualType Ty, const Type *&Base,
2754194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   ASTContext &Context,
2755194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson                                   uint64_t *HAMembers = 0) {
2756eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  uint64_t Members = 0;
2757194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) {
2758194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members))
2759194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2760194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members *= AT->getSize().getZExtValue();
2761194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else if (const RecordType *RT = Ty->getAs<RecordType>()) {
2762194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const RecordDecl *RD = RT->getDecl();
2763eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (RD->hasFlexibleArrayMember())
2764194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2765eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2766194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 0;
2767194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
2768194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         i != e; ++i) {
2769581deb3da481053c4993c7600f97acf7768caac5David Blaikie      const FieldDecl *FD = *i;
2770194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      uint64_t FldMembers;
2771194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers))
2772194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2773eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2774eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      Members = (RD->isUnion() ?
2775eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov                 std::max(Members, FldMembers) : Members + FldMembers);
2776194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2777194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  } else {
2778194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    Members = 1;
2779194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const ComplexType *CT = Ty->getAs<ComplexType>()) {
2780194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Members = 2;
2781194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Ty = CT->getElementType();
2782194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2783194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2784194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // Homogeneous aggregates for AAPCS-VFP must have base types of float,
2785194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // double, or 64-bit or 128-bit vectors.
2786194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) {
2787194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (BT->getKind() != BuiltinType::Float &&
2788adfa45ffd67d1959cb1ff8cec88ad2ff3ffb7798Tim Northover          BT->getKind() != BuiltinType::Double &&
2789adfa45ffd67d1959cb1ff8cec88ad2ff3ffb7798Tim Northover          BT->getKind() != BuiltinType::LongDouble)
2790194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2791194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else if (const VectorType *VT = Ty->getAs<VectorType>()) {
2792194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      unsigned VecSize = Context.getTypeSize(VT);
2793194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      if (VecSize != 64 && VecSize != 128)
2794194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        return false;
2795194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    } else {
2796194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2797194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    }
2798194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2799194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // The base type must be the same for all members.  Vector types of the
2800194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // same total size are treated as being equivalent here.
2801194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *TyPtr = Ty.getTypePtr();
2802194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (!Base)
2803194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      Base = TyPtr;
2804194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    if (Base != TyPtr &&
2805194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson        (!Base->isVectorType() || !TyPtr->isVectorType() ||
2806194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson         Context.getTypeSize(Base) != Context.getTypeSize(TyPtr)))
2807194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return false;
2808194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
2809194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2810194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  // Homogeneous Aggregates can have at most 4 members of the base type.
2811194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (HAMembers)
2812194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    *HAMembers = Members;
2813eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov
2814eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov  return (Members > 0 && Members <= 4);
2815194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson}
2816194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2817a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const {
2818d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(Ty)) {
2819aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
2820aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
2821aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      Ty = EnumTy->getDecl()->getIntegerType();
2822aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
2823cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (Ty->isPromotableIntegerType() ?
2824cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2825aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
282698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2827420255710694e958fa04bed1d80d96508949879eDaniel Dunbar  // Ignore empty records.
2828a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), Ty, true))
2829420255710694e958fa04bed1d80d96508949879eDaniel Dunbar    return ABIArgInfo::getIgnore();
2830420255710694e958fa04bed1d80d96508949879eDaniel Dunbar
28310eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // Structures with either a non-trivial destructor or a non-trivial
28320eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // copy constructor are always indirect.
28330eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
28340eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
28350eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
2836194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  if (getABIKind() == ARMABIInfo::AAPCS_VFP) {
2837194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    // Homogeneous Aggregates need to be expanded.
2838194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson    const Type *Base = 0;
2839eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (isHomogeneousAggregate(Ty, Base, getContext())) {
2840eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      assert(Base && "Base class should be set for homogeneous aggregate");
2841194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson      return ABIArgInfo::getExpand();
2842eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    }
2843194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson  }
2844194f06a476d299a7a70e5ff1d152f5895dc0a76cBob Wilson
2845634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren  // Support byval for ARM.
2846634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren  if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64) ||
2847634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren      getContext().getTypeAlign(Ty) > 64) {
2848634b3d26969f139a25b223074567ba5ab7ba7dd9Manman Ren    return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
284979f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  }
285079f30981fcd25c6ff88807372a2744af02a7690eEli Friedman
28518aa87c71d9bfec14e135c683b0d7b9de999dbcb0Daniel Dunbar  // Otherwise, pass by coercing to a structure of the appropriate size.
28522acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner  llvm::Type* ElemTy;
2853c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  unsigned SizeRegs;
285479f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  // FIXME: Try to match the types of the arguments more accurately where
285579f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  // we can.
285679f30981fcd25c6ff88807372a2744af02a7690eEli Friedman  if (getContext().getTypeAlign(Ty) <= 32) {
285753fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    ElemTy = llvm::Type::getInt32Ty(getVMContext());
285853fc1a6151ec31350309f479c0d2252366e4815cBob Wilson    SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32;
285978eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren  } else {
286078eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren    ElemTy = llvm::Type::getInt64Ty(getVMContext());
286178eb76e2eefdc381dd4a97bc8ee628ae975aff35Manman Ren    SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64;
2862c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
2863b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings
28649cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *STy =
28657650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner    llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL);
2866b7f62d01369c2a6e4af5dd2a76052ae65892161dStuart Hastings  return ABIArgInfo::getDirect(STy);
2867c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
2868c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
2869a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattnerstatic bool isIntegerLikeType(QualType Ty, ASTContext &Context,
287098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar                              llvm::LLVMContext &VMContext) {
287198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure
287298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // is called integer-like if its size is less than or equal to one word, and
287398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // the offset of each of its addressable sub-fields is zero.
287498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
287598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  uint64_t Size = Context.getTypeSize(Ty);
287698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
287798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that the type fits in a word.
287898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Size > 32)
287998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
288098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
288198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // FIXME: Handle vector types!
288298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (Ty->isVectorType())
288398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
288498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2885b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  // Float types are never treated as "integer like".
2886b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar  if (Ty->isRealFloatingType())
2887b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar    return false;
2888b0d58196808aba4b3d1a7488bd5566f3c0a83e89Daniel Dunbar
288998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // If this is a builtin or pointer type then it is ok.
2890183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (Ty->getAs<BuiltinType>() || Ty->isPointerType())
289198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return true;
289298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
28934581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  // Small complex integer types are "integer like".
28944581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar  if (const ComplexType *CT = Ty->getAs<ComplexType>())
28954581581881d3f7349bf5a4b39d761bce688f9164Daniel Dunbar    return isIntegerLikeType(CT->getElementType(), Context, VMContext);
289698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
289798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Single element and zero sized arrays should be allowed, by the definition
289898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // above, but they are not.
289998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
290098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise, it must be a record type.
290198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordType *RT = Ty->getAs<RecordType>();
290298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (!RT) return false;
290398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
290498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Ignore records with flexible arrays.
290598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const RecordDecl *RD = RT->getDecl();
290698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RD->hasFlexibleArrayMember())
290798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return false;
290898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
290998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Check that all sub-fields are at offset 0, and are themselves "integer
291098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // like".
291198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
291298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
291398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  bool HadField = false;
291498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  unsigned idx = 0;
291598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
291698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar       i != e; ++i, ++idx) {
2917581deb3da481053c4993c7600f97acf7768caac5David Blaikie    const FieldDecl *FD = *i;
291898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2919679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Bit-fields are not addressable, we only need to verify they are "integer
2920679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // like". We still have to disallow a subsequent non-bitfield, for example:
2921679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    //   struct { int : 0; int x }
2922679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // is non-integer like according to gcc.
2923679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (FD->isBitField()) {
2924679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!RD->isUnion())
2925679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        HadField = true;
2926679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
2927679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      if (!isIntegerLikeType(FD->getType(), Context, VMContext))
2928679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar        return false;
292998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2930679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      continue;
293198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
293298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2933679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Check if this field is at offset 0.
2934679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    if (Layout.getFieldOffset(idx) != 0)
2935679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar      return false;
2936679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar
293798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!isIntegerLikeType(FD->getType(), Context, VMContext))
293898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return false;
29398bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer
2940679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // Only allow at most one field in a structure. This doesn't match the
2941679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // wording above, but follows gcc in situations with a field following an
2942679855a6e14fbc6c6838c566aa74c32f52f4f946Daniel Dunbar    // empty structure.
294398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    if (!RD->isUnion()) {
294498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (HadField)
294598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar        return false;
294698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
294798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      HadField = true;
294898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
294998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  }
295098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
295198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return true;
295298303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar}
295398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2954a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris LattnerABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const {
295598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (RetTy->isVoidType())
2956c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    return ABIArgInfo::getIgnore();
295798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
2958f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  // Large vector types should be returned via memory.
2959f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128)
2960f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar    return ABIArgInfo::getIndirect(0);
2961f554b1cc3083d9ed1fb9b52a305025f744e90d08Daniel Dunbar
2962d608cdb7c044365cf4e8764ade1e11e99c176078John McCall  if (!isAggregateTypeForABI(RetTy)) {
2963aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    // Treat an enum type as its underlying type.
2964aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
2965aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor      RetTy = EnumTy->getDecl()->getIntegerType();
2966aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor
2967cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov    return (RetTy->isPromotableIntegerType() ?
2968cc6fa88666ca2f287df4a600eb31a4087bab9c13Anton Korobeynikov            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
2969aa74a1e49f7c4b89539830290f76fe2c3e97187fDouglas Gregor  }
297098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
29710eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // Structures with either a non-trivial destructor or a non-trivial
29720eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  // copy constructor are always indirect.
29730eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola  if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
29740eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
29750eb1d9733801764cd8b692c67e117e4feeecf013Rafael Espindola
297698303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Are we following APCS?
297798303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  if (getABIKind() == APCS) {
2978a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isEmptyRecord(getContext(), RetTy, false))
297998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      return ABIArgInfo::getIgnore();
298098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
29814cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // Complex types are all returned as packed integers.
29824cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    //
29834cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // FIXME: Consider using 2 x vector types if the back end handles them
29844cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    // correctly.
29854cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar    if (RetTy->isAnyComplexType())
2986800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),
2987a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner                                              getContext().getTypeSize(RetTy)));
29884cc753f4503931763cfb762a95928b44fcbe64e9Daniel Dunbar
298998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Integer like structures are returned in r0.
2990a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner    if (isIntegerLikeType(RetTy, getContext(), getVMContext())) {
299198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      // Return in the smallest viable integer type.
2992a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner      uint64_t Size = getContext().getTypeSize(RetTy);
299398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 8)
2994800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
299598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar      if (Size <= 16)
2996800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner        return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
2997800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
299898303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    }
299998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
300098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    // Otherwise return in memory.
300198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar    return ABIArgInfo::getIndirect(0);
3002c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
300398303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
300498303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Otherwise this is an AAPCS variant.
300598303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar
3006a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  if (isEmptyRecord(getContext(), RetTy, true))
300716a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    return ABIArgInfo::getIgnore();
300816a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
30093b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  // Check for homogeneous aggregates with AAPCS-VFP.
30103b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  if (getABIKind() == AAPCS_VFP) {
30113b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson    const Type *Base = 0;
3012eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    if (isHomogeneousAggregate(RetTy, Base, getContext())) {
3013eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov      assert(Base && "Base class should be set for homogeneous aggregate");
30143b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      // Homogeneous Aggregates are returned directly.
30153b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson      return ABIArgInfo::getDirect();
3016eaf856db5d1a272dc7188937206ef4572836f82aAnton Korobeynikov    }
30173b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson  }
30183b694fab31d3a7a8379996cbe7ef8d53f7d677bcBob Wilson
301998303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // Aggregates <= 4 bytes are returned in r0; other aggregates
302098303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  // are returned indirectly.
3021a3c109bbf6e198f463fbe204da4d25b40dab65c4Chris Lattner  uint64_t Size = getContext().getTypeSize(RetTy);
302216a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  if (Size <= 32) {
302316a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    // Return in the smallest viable integer type.
302416a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 8)
3025800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
302616a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar    if (Size <= 16)
3027800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
3028800588fd230d2c37ddce8fbf4a3881352715d700Chris Lattner    return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
302916a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar  }
303016a0808b7992db2c2ba78b387e1732bbb0fb371bDaniel Dunbar
303198303b93ae335bbb4731f6f1f8164d3c70648346Daniel Dunbar  return ABIArgInfo::getIndirect(0);
3032c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3033c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3034c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikovllvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
303577b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner                                   CodeGenFunction &CGF) const {
30368b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
30378b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
3038c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3039c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  CGBuilderTy &Builder = CGF.Builder;
30408b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3041c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
3042e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  // Handle address alignment for type alignment > 32 bits
3043e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8;
3044e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  if (TyAlign > 4) {
3045e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    assert((TyAlign & (TyAlign - 1)) == 0 &&
3046e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola           "Alignment is not power of 2!");
3047e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty);
3048e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1));
3049e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1)));
3050e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola    Addr = Builder.CreateIntToPtr(AddrAsInt, BP);
3051e164c180527354acc16c1b9b2c5a5ed4a1e484d4Rafael Espindola  }
3052c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Type *PTy =
305396e0fc726c6fe7538522c60743705d5e696b40afOwen Anderson    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3054c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
3055c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3056c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  uint64_t Offset =
3057c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
3058c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  llvm::Value *NextAddr =
305977b89b87c3b9220fea1bc80f6d6598d2003cc8a8Chris Lattner    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
3060c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov                      "ap.next");
3061c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3062c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3063c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  return AddrTyped;
3064c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3065c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3066dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
30672c585b991596859f39860b6094247ba027a03530Justin Holewinski// NVPTX ABI Implementation
30680259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
30690259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30700259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskinamespace {
30710259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30722c585b991596859f39860b6094247ba027a03530Justin Holewinskiclass NVPTXABIInfo : public ABIInfo {
30730259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
30742c585b991596859f39860b6094247ba027a03530Justin Holewinski  NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
30750259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30760259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyReturnType(QualType RetTy) const;
30770259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  ABIArgInfo classifyArgumentType(QualType Ty) const;
30780259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30790259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual void computeInfo(CGFunctionInfo &FI) const;
30800259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
30810259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski                                 CodeGenFunction &CFG) const;
30820259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
30830259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30842c585b991596859f39860b6094247ba027a03530Justin Holewinskiclass NVPTXTargetCodeGenInfo : public TargetCodeGenInfo {
30850259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinskipublic:
30862c585b991596859f39860b6094247ba027a03530Justin Holewinski  NVPTXTargetCodeGenInfo(CodeGenTypes &CGT)
30872c585b991596859f39860b6094247ba027a03530Justin Holewinski    : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {}
3088818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
30892f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
30902f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
30910259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski};
30920259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
30932c585b991596859f39860b6094247ba027a03530Justin HolewinskiABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const {
30940259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (RetTy->isVoidType())
30950259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIgnore();
30960259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(RetTy))
30970259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
30980259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
30990259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
31000259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31012c585b991596859f39860b6094247ba027a03530Justin HolewinskiABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const {
31020259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (isAggregateTypeForABI(Ty))
31030259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return ABIArgInfo::getIndirect(0);
31040259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31050259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  return ABIArgInfo::getDirect();
31060259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
31070259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31082c585b991596859f39860b6094247ba027a03530Justin Holewinskivoid NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const {
31090259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
31100259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
31110259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski       it != ie; ++it)
31120259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    it->info = classifyArgumentType(it->type);
31130259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31140259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  // Always honor user-specified calling convention.
31150259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  if (FI.getCallingConvention() != llvm::CallingConv::C)
31160259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    return;
31170259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31180259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  // Calling convention as default by an ABI.
31192c585b991596859f39860b6094247ba027a03530Justin Holewinski  // We're still using the PTX_Kernel/PTX_Device calling conventions here,
31202c585b991596859f39860b6094247ba027a03530Justin Holewinski  // but we should switch to NVVM metadata later on.
31210259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  llvm::CallingConv::ID DefaultCC;
31224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const LangOptions &LangOpts = getContext().getLangOpts();
3123744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  if (LangOpts.OpenCL || LangOpts.CUDA) {
3124744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // If we are in OpenCL or CUDA mode, then default to device functions
31250259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski    DefaultCC = llvm::CallingConv::PTX_Device;
3126818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  } else {
3127818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // If we are in standard C/C++ mode, use the triple to decide on the default
3128818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    StringRef Env =
3129818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      getContext().getTargetInfo().getTriple().getEnvironmentName();
3130818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    if (Env == "device")
3131818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      DefaultCC = llvm::CallingConv::PTX_Device;
3132818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    else
3133818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      DefaultCC = llvm::CallingConv::PTX_Kernel;
3134818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  }
31350259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski  FI.setEffectiveCallingConvention(DefaultCC);
3136818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
31370259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
31380259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31392c585b991596859f39860b6094247ba027a03530Justin Holewinskillvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
31402c585b991596859f39860b6094247ba027a03530Justin Holewinski                                     CodeGenFunction &CFG) const {
31412c585b991596859f39860b6094247ba027a03530Justin Holewinski  llvm_unreachable("NVPTX does not support varargs");
31420259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
31430259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31442c585b991596859f39860b6094247ba027a03530Justin Holewinskivoid NVPTXTargetCodeGenInfo::
31452c585b991596859f39860b6094247ba027a03530Justin HolewinskiSetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
31462c585b991596859f39860b6094247ba027a03530Justin Holewinski                    CodeGen::CodeGenModule &M) const{
3147818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3148818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  if (!FD) return;
3149818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
3150818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  llvm::Function *F = cast<llvm::Function>(GV);
3151818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
3152818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  // Perform special handling in OpenCL mode
31534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().OpenCL) {
3154818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // Use OpenCL function attributes to set proper calling conventions
3155818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    // By default, all functions are device functions
3156818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    if (FD->hasAttr<OpenCLKernelAttr>()) {
3157818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      // OpenCL __kernel functions get a kernel calling convention
3158744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne      F->setCallingConv(llvm::CallingConv::PTX_Kernel);
3159818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      // And kernel functions are not subject to inlining
3160818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski      F->addFnAttr(llvm::Attribute::NoInline);
3161818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski    }
3162744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  }
3163818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
3164744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne  // Perform special handling in CUDA mode.
31654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().CUDA) {
3166744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // CUDA __global__ functions get a kernel calling convention.  Since
3167744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // __global__ functions cannot be called from the device, we do not
3168744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    // need to set the noinline attribute.
3169744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne    if (FD->getAttr<CUDAGlobalAttr>())
3170744d90bfe2a43847764a707b1bee7ef1e30ad5f2Peter Collingbourne      F->setCallingConv(llvm::CallingConv::PTX_Kernel);
3171818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski  }
3172818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski}
3173818eafb6ac56c87b80b34be29ca115cd309026d2Justin Holewinski
31740259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski}
31750259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
31760259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski//===----------------------------------------------------------------------===//
3177276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck// MBlaze ABI Implementation
3178276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
3179276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3180276fdf408050d205f3a7f34c1e788224a67d2098Wesley Pecknamespace {
3181276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3182276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeABIInfo : public ABIInfo {
3183276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
3184276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
3185276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3186276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  bool isPromotableIntegerType(QualType Ty) const;
3187276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3188276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyReturnType(QualType RetTy) const;
3189276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  ABIArgInfo classifyArgumentType(QualType RetTy) const;
3190276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3191276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual void computeInfo(CGFunctionInfo &FI) const {
3192276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
3193276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3194276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck         it != ie; ++it)
3195276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      it->info = classifyArgumentType(it->type);
3196276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
3197276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3198276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3199276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                 CodeGenFunction &CGF) const;
3200276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
3201276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3202276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckclass MBlazeTargetCodeGenInfo : public TargetCodeGenInfo {
3203276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckpublic:
3204276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  MBlazeTargetCodeGenInfo(CodeGenTypes &CGT)
3205276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {}
3206276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
3207276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                           CodeGen::CodeGenModule &M) const;
3208276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck};
3209276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3210276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3211276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3212276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckbool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const {
3213276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // MBlaze ABI requires all 8 and 16 bit quantities to be extended.
3214276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (const BuiltinType *BT = Ty->getAs<BuiltinType>())
3215276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    switch (BT->getKind()) {
3216276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Bool:
3217276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_S:
3218276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Char_U:
3219276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::SChar:
3220276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UChar:
3221276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::Short:
3222276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    case BuiltinType::UShort:
3223276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return true;
3224276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    default:
3225276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      return false;
3226276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    }
3227276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return false;
3228276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3229276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3230276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckllvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3231276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                      CodeGenFunction &CGF) const {
3232276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // FIXME: Implement
3233276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return 0;
3234276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3235276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3236276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3237276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const {
3238276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (RetTy->isVoidType())
3239276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIgnore();
3240276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(RetTy))
3241276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
3242276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3243276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(RetTy) ?
3244276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3245276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3246276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3247276fdf408050d205f3a7f34c1e788224a67d2098Wesley PeckABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const {
3248276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (isAggregateTypeForABI(Ty))
3249276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return ABIArgInfo::getIndirect(0);
3250276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3251276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  return (isPromotableIntegerType(Ty) ?
3252276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3253276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3254276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3255276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peckvoid MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D,
3256276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  llvm::GlobalValue *GV,
3257276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  CodeGen::CodeGenModule &M)
3258276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                                                  const {
3259276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
3260276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (!FD) return;
3261125b4cb35536e45201f8f2cb19ee620e3ad67c49NAKAMURA Takumi
3262276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  llvm::CallingConv::ID CC = llvm::CallingConv::C;
3263276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (FD->hasAttr<MBlazeInterruptHandlerAttr>())
3264276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_INTR;
3265276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  else if (FD->hasAttr<MBlazeSaveVolatilesAttr>())
3266276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    CC = llvm::CallingConv::MBLAZE_SVOL;
3267276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3268276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC != llvm::CallingConv::C) {
3269276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Handle 'interrupt_handler' attribute:
3270276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      llvm::Function *F = cast<llvm::Function>(GV);
3271276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3272276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 1: Set ISR calling convention.
3273276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      F->setCallingConv(CC);
3274276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3275276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      // Step 2: Add attributes goodness.
3276276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck      F->addFnAttr(llvm::Attribute::NoInline);
3277276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  }
3278276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3279276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  // Step 3: Emit _interrupt_handler alias.
3280276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  if (CC == llvm::CallingConv::MBLAZE_INTR)
3281276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
3282276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck                          "_interrupt_handler", GV, &M.getModule());
3283276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck}
3284276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3285276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
3286276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck//===----------------------------------------------------------------------===//
328782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov// MSP430 ABI Implementation
3288dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
328982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
329082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovnamespace {
329182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
329282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovclass MSP430TargetCodeGenInfo : public TargetCodeGenInfo {
329382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovpublic:
3294ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner  MSP430TargetCodeGenInfo(CodeGenTypes &CGT)
3295ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {}
329682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
329782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                           CodeGen::CodeGenModule &M) const;
329882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov};
329982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
3300c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3301c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
330282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikovvoid MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D,
330382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                                  llvm::GlobalValue *GV,
330482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                                             CodeGen::CodeGenModule &M) const {
330582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
330682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) {
330782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Handle 'interrupt' attribute:
330882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      llvm::Function *F = cast<llvm::Function>(GV);
330982d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
331082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 1: Set ISR calling convention.
331182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      F->setCallingConv(llvm::CallingConv::MSP430_INTR);
331282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
331382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 2: Add attributes goodness.
331482d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      F->addFnAttr(llvm::Attribute::NoInline);
331582d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov
331682d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      // Step 3: Emit ISR vector alias.
331782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      unsigned Num = attr->getNumber() + 0xffe0;
331882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage,
33195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            "vector_" + Twine::utohexstr(Num),
332082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov                            GV, &M.getModule());
332182d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    }
332282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  }
3323c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3324c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3325dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
3326aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// MIPS ABI Implementation.  This works for both little-endian and
3327aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall// big-endian variants.
3328dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner//===----------------------------------------------------------------------===//
3329dce5ad0cf70ba74e1ecdbb5e81f1a81d97821636Chris Lattner
3330aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallnamespace {
3331619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakaclass MipsABIInfo : public ABIInfo {
3332c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  bool IsO32;
3333c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  unsigned MinABIStackAlignInBytes, StackAlignInBytes;
3334c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  void CoerceToIntArgs(uint64_t TySize,
3335c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                       SmallVector<llvm::Type*, 8> &ArgList) const;
333691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const;
3337c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const;
3338a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const;
3339619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakapublic:
3340b551dd31f6b15aa959127ee906084fcf5bf0154eAkira Hatanaka  MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) :
3341c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8),
3342c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    StackAlignInBytes(IsO32 ? 8 : 16) {}
3343619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3344619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  ABIArgInfo classifyReturnType(QualType RetTy) const;
3345f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka  ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const;
3346619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual void computeInfo(CGFunctionInfo &FI) const;
3347619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3348619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                 CodeGenFunction &CGF) const;
3349619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka};
3350619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3351aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallclass MIPSTargetCodeGenInfo : public TargetCodeGenInfo {
3352e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka  unsigned SizeOfUnwindException;
3353aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallpublic:
3354c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka  MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32)
3355c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)),
3356c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka      SizeOfUnwindException(IsO32 ? 24 : 32) {}
3357aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3358aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const {
3359aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall    return 29;
3360aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  }
3361aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3362aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
33638bea82f6699e4384ef823cdc8800ad5db271177cMichael J. Spencer                               llvm::Value *Address) const;
336449e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall
336549e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  unsigned getSizeOfUnwindException() const {
3366e624fa02b2c2c614b3a27a25516885fc64e07001Akira Hatanaka    return SizeOfUnwindException;
336749e34be6ae0c25b9843610cdd2fd6fea9cd8b870John McCall  }
3368aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall};
3369aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
3370aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3371c359f2029d19016560a422551704ccc2419be0b1Akira Hatanakavoid MipsABIInfo::CoerceToIntArgs(uint64_t TySize,
3372c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                                  SmallVector<llvm::Type*, 8> &ArgList) const {
3373c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  llvm::IntegerType *IntTy =
3374c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
337591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
337691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  // Add (TySize / MinABIStackAlignInBytes) args of IntTy.
337791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N)
337891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    ArgList.push_back(IntTy);
337991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
338091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  // If necessary, add one more integer type to ArgList.
338191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  unsigned R = TySize % (MinABIStackAlignInBytes * 8);
338291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
338391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  if (R)
338491338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    ArgList.push_back(llvm::IntegerType::get(getVMContext(), R));
338591338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka}
338691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
3387d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// In N32/64, an aligned double precision floating point field is passed in
3388d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka// a register.
338991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanakallvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const {
3390c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  SmallVector<llvm::Type*, 8> ArgList, IntArgList;
3391c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka
3392c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (IsO32) {
3393c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    CoerceToIntArgs(TySize, ArgList);
3394c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    return llvm::StructType::get(getVMContext(), ArgList);
3395c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  }
3396d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
33972afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka  if (Ty->isComplexType())
33982afd23da0e33a8cd44c1c46b1651c677fdd27151Akira Hatanaka    return CGT.ConvertType(Ty);
33996d1080fd1851f18bd40bb46fa074aa1252b13e8eAkira Hatanaka
3400a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  const RecordType *RT = Ty->getAs<RecordType>();
3401d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3402c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  // Unions/vectors are passed in integer registers.
3403c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (!RT || !RT->isStructureOrClassType()) {
3404c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    CoerceToIntArgs(TySize, ArgList);
3405c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka    return llvm::StructType::get(getVMContext(), ArgList);
3406c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  }
3407d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3408d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const RecordDecl *RD = RT->getDecl();
3409d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
341091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  assert(!(TySize % 8) && "Size of structure must be multiple of 8.");
3411d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3412d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  uint64_t LastOffset = 0;
3413d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  unsigned idx = 0;
3414d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64);
3415d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3416a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // Iterate over fields in the struct/class and check if there are any aligned
3417a34e92116581531f7325527d952a9ffcc819d905Akira Hatanaka  // double fields.
3418d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
3419d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka       i != e; ++i, ++idx) {
3420262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie    const QualType Ty = i->getType();
3421d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    const BuiltinType *BT = Ty->getAs<BuiltinType>();
3422d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3423d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (!BT || BT->getKind() != BuiltinType::Double)
3424d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
3425d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3426d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    uint64_t Offset = Layout.getFieldOffset(idx);
3427d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    if (Offset % 64) // Ignore doubles that are not aligned.
3428d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      continue;
3429d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3430d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add ((Offset - LastOffset) / 64) args of type i64.
3431d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j)
3432d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka      ArgList.push_back(I64);
3433d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3434d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    // Add double type.
3435d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    ArgList.push_back(llvm::Type::getDoubleTy(getVMContext()));
3436d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka    LastOffset = Offset + 64;
3437d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  }
3438d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3439c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  CoerceToIntArgs(TySize - LastOffset, IntArgList);
3440c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  ArgList.append(IntArgList.begin(), IntArgList.end());
3441d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3442d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka  return llvm::StructType::get(getVMContext(), ArgList);
3443d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka}
3444d5a257f39b6f78fb66bb0227486b65592476c572Akira Hatanaka
3445a33fd393d5255716e904fed021f87260095ed00aAkira Hatanakallvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const {
344691338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  assert((Offset % MinABIStackAlignInBytes) == 0);
344791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
344891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  if ((Align - 1) & Offset)
344991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8);
3450a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
345191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  return 0;
3452a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka}
34539659d59ec368933050684af573b6d32ab5714332Akira Hatanaka
3454f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaABIArgInfo
3455f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira HatanakaMipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const {
3456a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t OrigOffset = Offset;
345791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  uint64_t TySize = getContext().getTypeSize(Ty);
3458a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  uint64_t Align = getContext().getTypeAlign(Ty) / 8;
345991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka
3460c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes),
3461c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka                   (uint64_t)StackAlignInBytes);
346291338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  Offset = llvm::RoundUpToAlignment(Offset, Align);
346391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
3464a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3465c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
3466619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    // Ignore empty aggregates.
3467f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    if (TySize == 0)
3468619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka      return ABIArgInfo::getIgnore();
3469619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3470511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka    // Records with non trivial destructors/constructors should not be passed
3471511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka    // by value.
3472f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) {
347391338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka      Offset = OrigOffset + MinABIStackAlignInBytes;
3474511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka      return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
3475f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    }
3476511949bf7ea721556ea3eb2777fc1e36e6c3e243Akira Hatanaka
347791338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // If we have reached here, aggregates are passed directly by coercing to
347891338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // another structure type. Padding is inserted if the offset of the
347991338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    // aggregate is unaligned.
348091338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka    return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0,
348191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka                                 getPaddingType(Align, OrigOffset));
3482619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
3483619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3484619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
3485619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = Ty->getAs<EnumType>())
3486619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    Ty = EnumTy->getDecl()->getIntegerType();
3487619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3488a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  if (Ty->isPromotableIntegerType())
3489a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka    return ABIArgInfo::getExtend();
3490a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka
3491a33fd393d5255716e904fed021f87260095ed00aAkira Hatanaka  return ABIArgInfo::getDirect(0, 0, getPaddingType(Align, OrigOffset));
3492619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3493619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3494c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanakallvm::Type*
3495c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira HatanakaMipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const {
3496da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  const RecordType *RT = RetTy->getAs<RecordType>();
3497c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  SmallVector<llvm::Type*, 8> RTList;
3498c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3499da54ff306270e179f64d046369419724356d30d7Akira Hatanaka  if (RT && RT->isStructureOrClassType()) {
3500c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    const RecordDecl *RD = RT->getDecl();
3501da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
3502da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    unsigned FieldCnt = Layout.getFieldCount();
3503da54ff306270e179f64d046369419724356d30d7Akira Hatanaka
3504da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // N32/64 returns struct/classes in floating point registers if the
3505da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // following conditions are met:
3506da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 1. The size of the struct/class is no larger than 128-bit.
3507da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 2. The struct/class has one or two fields all of which are floating
3508da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //    point types.
3509da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // 3. The offset of the first field is zero (this follows what gcc does).
3510da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
3511da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    // Any other composite results are returned in integer registers.
3512da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    //
3513da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) {
3514da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end();
3515da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      for (; b != e; ++b) {
3516262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        const BuiltinType *BT = b->getType()->getAs<BuiltinType>();
3517c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3518da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        if (!BT || !BT->isFloatingPoint())
3519da54ff306270e179f64d046369419724356d30d7Akira Hatanaka          break;
3520c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3521262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie        RTList.push_back(CGT.ConvertType(b->getType()));
3522da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      }
3523c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3524da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      if (b == e)
3525da54ff306270e179f64d046369419724356d30d7Akira Hatanaka        return llvm::StructType::get(getVMContext(), RTList,
3526da54ff306270e179f64d046369419724356d30d7Akira Hatanaka                                     RD->hasAttr<PackedAttr>());
3527c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3528da54ff306270e179f64d046369419724356d30d7Akira Hatanaka      RTList.clear();
3529da54ff306270e179f64d046369419724356d30d7Akira Hatanaka    }
3530c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  }
3531c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3532c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka  CoerceToIntArgs(Size, RTList);
3533c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka  return llvm::StructType::get(getVMContext(), RTList);
3534c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka}
3535c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3536619e8875d29cc019c7360595f66b9f91b3439494Akira HatanakaABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const {
3537a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  uint64_t Size = getContext().getTypeSize(RetTy);
3538a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka
3539a8536c086fbac59bd7f9a6493bc99b4a92d585e4Akira Hatanaka  if (RetTy->isVoidType() || Size == 0)
3540619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIgnore();
3541619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
35428aeb1471ef62a4befba00721925a3717914f21d8Akira Hatanaka  if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) {
3543c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    if (Size <= 128) {
3544c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka      if (RetTy->isAnyComplexType())
3545c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect();
3546c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka
3547c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka      // O32 returns integer vectors in registers.
3548c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka      if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())
3549c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3550c359f2029d19016560a422551704ccc2419be0b1Akira Hatanaka
3551526cdfb2bf51c8c6612504f1d06c02c736d3d126Akira Hatanaka      if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
3552c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka        return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size));
3553c7ecc2e3691e484cffcfec7fcefef18b2bd23e5fAkira Hatanaka    }
3554619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3555619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    return ABIArgInfo::getIndirect(0);
3556619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  }
3557619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3558619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  // Treat an enum type as its underlying type.
3559619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
3560619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka    RetTy = EnumTy->getDecl()->getIntegerType();
3561619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3562619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  return (RetTy->isPromotableIntegerType() ?
3563619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka          ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
3564619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3565619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3566619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakavoid MipsABIInfo::computeInfo(CGFunctionInfo &FI) const {
3567cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  ABIArgInfo &RetInfo = FI.getReturnInfo();
3568cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  RetInfo = classifyReturnType(FI.getReturnType());
3569cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
3570cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka  // Check if a pointer to an aggregate is passed as a hidden argument.
357191338cf4129cfdb85af7e9ef396ab09621da10ecAkira Hatanaka  uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0;
3572cc66254946ec86a2ec94ff9c8db96b05a364a94fAkira Hatanaka
3573619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
3574619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka       it != ie; ++it)
3575f0cc2087b18c48b17c2f647c88a3e7eef19285fdAkira Hatanaka    it->info = classifyArgumentType(it->type, Offset);
3576619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3577619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3578619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanakallvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
3579619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka                                    CodeGenFunction &CGF) const {
35808b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BP = CGF.Int8PtrTy;
35818b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
3582c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3583c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  CGBuilderTy &Builder = CGF.Builder;
3584c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap");
3585c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
35868f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8;
3587c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
3588c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AddrTyped;
35898f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0);
35908f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty;
3591c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3592c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  if (TypeAlign > MinABIStackAlignInBytes) {
35938f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy);
35948f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1);
35958f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign);
35968f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc);
3597c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask);
3598c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy);
3599c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  }
3600c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  else
3601c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    AddrTyped = Builder.CreateBitCast(Addr, PTy);
3602c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3603c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP);
36048f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka  TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes);
3605c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  uint64_t Offset =
3606c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign);
3607c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  llvm::Value *NextAddr =
36088f675e4b18fb9b8972847e9f681044184da5586cAkira Hatanaka    Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset),
3609c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka                      "ap.next");
3610c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
3611c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka
3612c35e69d758e43563ec3785cdd4472d9f2386cf9aAkira Hatanaka  return AddrTyped;
3613619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka}
3614619e8875d29cc019c7360595f66b9f91b3439494Akira Hatanaka
3615aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallbool
3616aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCallMIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF,
3617aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall                                               llvm::Value *Address) const {
3618aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // This information comes from gcc's implementation, which seems to
3619aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // as canonical as it gets.
3620aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3621aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // Everything on MIPS is 4 bytes.  Double-precision FP registers
3622aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // are aliased to pairs of single-precision FP registers.
36238b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4);
3624aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3625aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 0-31 are the general purpose registers, $0 - $31.
3626aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 32-63 are the floating-point registers, $f0 - $f31.
3627aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 64 and 65 are the multiply/divide registers, $hi and $lo.
3628aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 66 is the (notional, I think) register for signal-handler return.
36298b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65);
3630aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3631aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 67-74 are the floating-point status registers, $fcc0 - $fcc7.
3632aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // They are one bit wide and ignored here.
3633aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
3634aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31.
3635aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // (coprocessor 1 is the FP unit)
3636aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31.
3637aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31.
3638aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  // 176-181 are the DSP accumulator registers.
36398b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181);
3640aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  return false;
3641aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall}
3642aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
36432f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
36442f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults.
36452f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// Currently subclassed only to implement custom OpenCL C function attribute
36462f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne// handling.
36472f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne//===----------------------------------------------------------------------===//
36482f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36492f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournenamespace {
36502f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36512f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourneclass TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo {
36522f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournepublic:
36532f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  TCETargetCodeGenInfo(CodeGenTypes &CGT)
36542f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    : DefaultTargetCodeGenInfo(CGT) {}
36552f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36562f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
36572f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                   CodeGen::CodeGenModule &M) const;
36582f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne};
36592f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36602f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbournevoid TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D,
36612f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               llvm::GlobalValue *GV,
36622f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                                               CodeGen::CodeGenModule &M) const {
36632f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
36642f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  if (!FD) return;
36652f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36662f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  llvm::Function *F = cast<llvm::Function>(GV);
36672f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (M.getLangOpts().OpenCL) {
36692f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    if (FD->hasAttr<OpenCLKernelAttr>()) {
36702f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      // OpenCL C Kernel functions are not subject to inlining
36712f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      F->addFnAttr(llvm::Attribute::NoInline);
36722f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36732f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) {
36742f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36752f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Convert the reqd_work_group_size() attributes to metadata.
36762f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::LLVMContext &Context = F->getContext();
36772f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        llvm::NamedMDNode *OpenCLMetadata =
36782f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne            M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info");
36792f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36802f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        SmallVector<llvm::Value*, 5> Operands;
36812f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        Operands.push_back(F);
36822f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36838b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
36848b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
36858b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim())));
36868b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
36878b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
36882f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim())));
36898b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty,
36908b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                             llvm::APInt(32,
36912f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne                               FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim())));
36922f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
36932f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // Add a boolean constant operand for "required" (true) or "hint" (false)
36942f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // for implementing the work_group_size_hint attr later. Currently
36952f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        // always true as the hint is not yet implemented.
36968b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner        Operands.push_back(llvm::ConstantInt::getTrue(Context));
36972f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne        OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands));
36982f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne      }
36992f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    }
37002f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  }
37012f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
37022f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
37032f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne}
3704aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
37059631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
37069631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum// Hexagon ABI Implementation
37079631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum//===----------------------------------------------------------------------===//
37089631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37099631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumnamespace {
37109631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37119631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonABIInfo : public ABIInfo {
37129631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37139631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37149631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
37159631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {}
37169631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37179631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumprivate:
37189631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37199631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyReturnType(QualType RetTy) const;
37209631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  ABIArgInfo classifyArgumentType(QualType RetTy) const;
37219631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37229631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual void computeInfo(CGFunctionInfo &FI) const;
37239631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37249631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
37259631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                 CodeGenFunction &CGF) const;
37269631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
37279631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37289631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumclass HexagonTargetCodeGenInfo : public TargetCodeGenInfo {
37299631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumpublic:
37309631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  HexagonTargetCodeGenInfo(CodeGenTypes &CGT)
37319631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {}
37329631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37339631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const {
37349631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return 29;
37359631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
37369631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum};
37379631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37389631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
37399631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37409631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumvoid HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const {
37419631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
37429631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end();
37439631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum       it != ie; ++it)
37449631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    it->info = classifyArgumentType(it->type);
37459631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
37469631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37479631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const {
37489631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(Ty)) {
37499631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
37509631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = Ty->getAs<EnumType>())
37519631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      Ty = EnumTy->getDecl()->getIntegerType();
37529631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37539631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (Ty->isPromotableIntegerType() ?
37549631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
37559631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
37569631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37579631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Ignore empty records.
37589631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), Ty, true))
37599631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
37609631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37619631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Structures with either a non-trivial destructor or a non-trivial
37629631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // copy constructor are always indirect.
37639631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty))
37649631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
37659631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37669631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(Ty);
37679631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size > 64)
37689631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
37699631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Pass in the smallest viable integer type.
37709631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 32)
37719631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
37729631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 16)
37739631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
37749631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else if (Size > 8)
37759631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
37769631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  else
37779631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
37789631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
37799631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37809631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony LinthicumABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const {
37819631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVoidType())
37829631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
37839631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37849631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Large vector types should be returned via memory.
37859631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64)
37869631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0);
37879631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37889631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (!isAggregateTypeForABI(RetTy)) {
37899631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Treat an enum type as its underlying type.
37909631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (const EnumType *EnumTy = RetTy->getAs<EnumType>())
37919631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      RetTy = EnumTy->getDecl()->getIntegerType();
37929631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37939631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return (RetTy->isPromotableIntegerType() ?
37949631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum            ABIArgInfo::getExtend() : ABIArgInfo::getDirect());
37959631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
37969631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
37979631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Structures with either a non-trivial destructor or a non-trivial
37989631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // copy constructor are always indirect.
37999631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy))
38009631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIndirect(0, /*ByVal=*/false);
38019631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38029631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (isEmptyRecord(getContext(), RetTy, true))
38039631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getIgnore();
38049631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38059631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // Aggregates <= 8 bytes are returned in r0; other aggregates
38069631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // are returned indirectly.
38079631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Size = getContext().getTypeSize(RetTy);
38089631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  if (Size <= 64) {
38099631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    // Return in the smallest viable integer type.
38109631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 8)
38119631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()));
38129631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 16)
38139631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
38149631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    if (Size <= 32)
38159631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum      return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
38169631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext()));
38179631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  }
38189631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38199631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return ABIArgInfo::getIndirect(0, /*ByVal=*/true);
38209631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
38219631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38229631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicumllvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty,
38238b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner                                       CodeGenFunction &CGF) const {
38249631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  // FIXME: Need to handle alignment
38258b418685e9e4f02f4eb2a76e1ec063e07552b68dChris Lattner  llvm::Type *BPP = CGF.Int8PtrPtrTy;
38269631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38279631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  CGBuilderTy &Builder = CGF.Builder;
38289631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP,
38299631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                                                       "ap");
38309631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur");
38319631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Type *PTy =
38329631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::PointerType::getUnqual(CGF.ConvertType(Ty));
38339631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy);
38349631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38359631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  uint64_t Offset =
38369631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4);
38379631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  llvm::Value *NextAddr =
38389631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset),
38399631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum                      "ap.next");
38409631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  Builder.CreateStore(NextAddr, VAListAddrAsBPP);
38419631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38429631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  return AddrTyped;
38439631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum}
38449631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
38459631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum
3846ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattnerconst TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
384782d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  if (TheTargetCodeGenInfo)
384882d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov    return *TheTargetCodeGenInfo;
3849c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov
3850bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  const llvm::Triple &Triple = getContext().getTargetInfo().getTriple();
38511752ee4849f4c37f5e03193e658be92650b0e65aDaniel Dunbar  switch (Triple.getArch()) {
38522c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar  default:
3853ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types));
38542c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
38559ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff  case llvm::Triple::le32:
38569ed63f8b87b1f7d074d21cc1210fd28d93291beaDerek Schuff    return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types));
3857aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mips:
3858aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall  case llvm::Triple::mipsel:
3859c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true));
3860aeeb7011a875d3dd439e9fa07dc3ac54d97785b9John McCall
38618c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64:
38628c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka  case llvm::Triple::mips64el:
3863c0e3b665344a39bd733e0d9f55bf0f1937922289Akira Hatanaka    return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false));
38648c6dfbe044155277b06e4345f1b98910692390b6Akira Hatanaka
386534d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::arm:
386634d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar  case llvm::Triple::thumb:
386734c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    {
386834c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS;
38695e7bacef79f7725f4abc45e2a5eccedae40dfcd3Daniel Dunbar
3870bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0)
387134c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::APCS;
387234c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      else if (CodeGenOpts.FloatABI == "hard")
387334c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel        Kind = ARMABIInfo::AAPCS_VFP;
387434c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel
387534c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel      return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind));
387634c1af83e159cfe0f43e7a855e84783f301fc1f1Sandeep Patel    }
387734d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
3878ec853ba1087f606e9685cb1e800616565ba35093John McCall  case llvm::Triple::ppc:
3879ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types));
38800fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky  case llvm::Triple::ppc64:
38810fbc4b97bd763850ecd72ad79b22b1ad85c5d965Roman Divacky    return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types));
3882ec853ba1087f606e9685cb1e800616565ba35093John McCall
3883edb66f38dbdc501342aa1f17c8a15a34ed73584dPeter Collingbourne  case llvm::Triple::nvptx:
3884edb66f38dbdc501342aa1f17c8a15a34ed73584dPeter Collingbourne  case llvm::Triple::nvptx64:
38852c585b991596859f39860b6094247ba027a03530Justin Holewinski    return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types));
38860259c3a3df3c2f3b9de7e3845df1eea3ac04e1aaJustin Holewinski
3887276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck  case llvm::Triple::mblaze:
3888276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck    return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types));
3889276fdf408050d205f3a7f34c1e788224a67d2098Wesley Peck
389082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov  case llvm::Triple::msp430:
3891ea0443212e7ec6ff82e2f174e8e948a6eb0e0876Chris Lattner    return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types));
389234d91fddd0252d64456cdcea0bd22073f006f4e2Daniel Dunbar
38932f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne  case llvm::Triple::tce:
38942f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne    return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types));
38952f7aa998c0d6494301c12c4fceb6134a1bc248abPeter Collingbourne
3896c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  case llvm::Triple::x86: {
3897bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0;
3898c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman
3899db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    if (Triple.isOSDarwin())
390082d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
3901b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX, false,
3902b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
3903db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar
3904db57a4cdb0a6abf3239f3a794a900ce312c5887bDaniel Dunbar    switch (Triple.getOS()) {
39052c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::Cygwin:
39062c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::MinGW32:
3907727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::AuroraUX:
3908727e268bd2974a7b16af65a5cfdfe47da9ebeb6cEdward O'Callaghan    case llvm::Triple::DragonFly:
390975c135a511c855d94bbfa7f00dd27a165f61e953David Chisnall    case llvm::Triple::FreeBSD:
39102c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    case llvm::Triple::OpenBSD:
391142f74f21ece01dc8573d5377859d327fbb23b26cEli Friedman    case llvm::Triple::Bitrig:
391282d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
3913b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX,
3914b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           false,
3915b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
391655fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman
391755fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman    case llvm::Triple::Win32:
391855fc7e2b8005ba87a81664d065e9b9e2fff1b1afEli Friedman      return *(TheTargetCodeGenInfo =
3919b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX, true,
3920b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
39212c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
39222c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar    default:
392382d0a418c8699fc6f4a9417457ffe93d43bba1c1Anton Korobeynikov      return *(TheTargetCodeGenInfo =
3924b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola               new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX,
3925b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           false,
3926b48280ba1790122cd3fa6e17c88ecd6a4571a4ebRafael Espindola                                           CodeGenOpts.NumRegisterParameters));
3927c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov    }
3928c3e0fb406fb6fe83566dc6d8b05362e0a2c1e191Eli Friedman  }
39292c0843f166a82f251b20370fadab57878969e7aaDaniel Dunbar
3930ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  case llvm::Triple::x86_64: {
3931ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman    bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0;
3932ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman
3933f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    switch (Triple.getOS()) {
3934f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Win32:
39350aa205765aec0aa5eed672f8e3cade543372edcdNAKAMURA Takumi    case llvm::Triple::MinGW32:
3936f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    case llvm::Triple::Cygwin:
3937f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner      return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types));
3938f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    default:
3939ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman      return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types,
3940ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman                                                                  HasAVX));
3941f13721dd91dda7675e499331a2770308ad20ca61Chris Lattner    }
3942c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov  }
39439631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum  case llvm::Triple::hexagon:
39449631939f82c0eaa6fb3936a0ce58a41adfbc9011Tony Linthicum    return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types));
3945ee1ad99f1ced9ffee436466ef674d4541c37864eEli Friedman  }
3946c4a59eb306efeb4bffa3cefecd1e6392fc5c4144Anton Korobeynikov}
3947