Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // These classes wrap the information about a call or function |
| 11 | // definition used to handle ABI compliancy. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
| 17 | #include "CodeGenFunction.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/RecordLayout.h" |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/CodeGenOptions.h" |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
| 22 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 27 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 28 | llvm::Value *Array, |
| 29 | llvm::Value *Value, |
| 30 | unsigned FirstIndex, |
| 31 | unsigned LastIndex) { |
| 32 | // Alternatively, we could emit this as a loop in the source. |
| 33 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| 34 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 35 | Builder.CreateStore(Value, Cell); |
| 36 | } |
| 37 | } |
| 38 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 39 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 40 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 41 | T->isMemberFunctionPointerType(); |
| 42 | } |
| 43 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 44 | ABIInfo::~ABIInfo() {} |
| 45 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 46 | ASTContext &ABIInfo::getContext() const { |
| 47 | return CGT.getContext(); |
| 48 | } |
| 49 | |
| 50 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 51 | return CGT.getLLVMContext(); |
| 52 | } |
| 53 | |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 54 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 55 | return CGT.getDataLayout(); |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 59 | void ABIArgInfo::dump() const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 60 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 61 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 62 | switch (TheKind) { |
| 63 | case Direct: |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 64 | OS << "Direct Type="; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 65 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 66 | Ty->print(OS); |
| 67 | else |
| 68 | OS << "null"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 69 | break; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 70 | case Extend: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 71 | OS << "Extend"; |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 72 | break; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 73 | case Ignore: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 74 | OS << "Ignore"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 75 | break; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 76 | case Indirect: |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 77 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | e9b5d77 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 78 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | cf3b6f2 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 79 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 80 | break; |
| 81 | case Expand: |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 82 | OS << "Expand"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 83 | break; |
| 84 | } |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 85 | OS << ")\n"; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 88 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 89 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 90 | // If someone can figure out a general rule for this, that would be great. |
| 91 | // It's probably just doomed to be platform-dependent, though. |
| 92 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 93 | // Verified for: |
| 94 | // x86-64 FreeBSD, Linux, Darwin |
| 95 | // x86-32 FreeBSD, Linux, Darwin |
| 96 | // PowerPC Linux, Darwin |
| 97 | // ARM Darwin (*not* EABI) |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 98 | // AArch64 Linux |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 99 | return 32; |
| 100 | } |
| 101 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 102 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 103 | const FunctionNoProtoType *fnType) const { |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 104 | // The following conventions are known to require this to be false: |
| 105 | // x86_stdcall |
| 106 | // MIPS |
| 107 | // For everything else, we just prefer false unless we opt out. |
| 108 | return false; |
| 109 | } |
| 110 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 111 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 112 | |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 113 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 114 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 115 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 116 | bool AllowArrays) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 117 | if (FD->isUnnamedBitfield()) |
| 118 | return true; |
| 119 | |
| 120 | QualType FT = FD->getType(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 121 | |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 122 | // Constant arrays of empty records count as empty, strip them off. |
| 123 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 124 | if (AllowArrays) |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 125 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 126 | if (AT->getSize() == 0) |
| 127 | return true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 128 | FT = AT->getElementType(); |
Eli Friedman | 7e7ad3f | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 129 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 131 | const RecordType *RT = FT->getAs<RecordType>(); |
| 132 | if (!RT) |
| 133 | return false; |
| 134 | |
| 135 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 136 | // |
| 137 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 138 | // current ABI. |
| 139 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 140 | return false; |
| 141 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 142 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 145 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 146 | /// fields. Note that a structure with a flexible array member is not |
| 147 | /// considered empty. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 148 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 149 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 150 | if (!RT) |
| 151 | return 0; |
| 152 | const RecordDecl *RD = RT->getDecl(); |
| 153 | if (RD->hasFlexibleArrayMember()) |
| 154 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 155 | |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 156 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 157 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 158 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 159 | e = CXXRD->bases_end(); i != e; ++i) |
| 160 | if (!isEmptyRecord(Context, i->getType(), true)) |
| 161 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 162 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 163 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 164 | i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 165 | if (!isEmptyField(Context, *i, AllowArrays)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 166 | return false; |
| 167 | return true; |
| 168 | } |
| 169 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 170 | /// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either |
| 171 | /// a non-trivial destructor or a non-trivial copy constructor. |
| 172 | static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) { |
| 173 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 174 | if (!RD) |
| 175 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 176 | |
Richard Smith | 426391c | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 177 | return !RD->hasTrivialDestructor() || RD->hasNonTrivialCopyConstructor(); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is |
| 181 | /// a record type with either a non-trivial destructor or a non-trivial copy |
| 182 | /// constructor. |
| 183 | static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) { |
| 184 | const RecordType *RT = T->getAs<RecordType>(); |
| 185 | if (!RT) |
| 186 | return false; |
| 187 | |
| 188 | return hasNonTrivialDestructorOrCopyConstructor(RT); |
| 189 | } |
| 190 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 191 | /// isSingleElementStruct - Determine if a structure is a "single |
| 192 | /// element struct", i.e. it has exactly one non-empty field or |
| 193 | /// exactly one field which is itself a single element |
| 194 | /// struct. Structures with flexible array members are never |
| 195 | /// considered single element structs. |
| 196 | /// |
| 197 | /// \return The field declaration for the single non-empty field, if |
| 198 | /// it exists. |
| 199 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 200 | const RecordType *RT = T->getAsStructureType(); |
| 201 | if (!RT) |
| 202 | return 0; |
| 203 | |
| 204 | const RecordDecl *RD = RT->getDecl(); |
| 205 | if (RD->hasFlexibleArrayMember()) |
| 206 | return 0; |
| 207 | |
| 208 | const Type *Found = 0; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 209 | |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 210 | // If this is a C++ record, check the bases first. |
| 211 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 212 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 213 | e = CXXRD->bases_end(); i != e; ++i) { |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 214 | // Ignore empty records. |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 215 | if (isEmptyRecord(Context, i->getType(), true)) |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 216 | continue; |
| 217 | |
| 218 | // If we already found an element then this isn't a single-element struct. |
| 219 | if (Found) |
| 220 | return 0; |
| 221 | |
| 222 | // If this is non-empty and not a single element struct, the composite |
| 223 | // cannot be a single element struct. |
| 224 | Found = isSingleElementStruct(i->getType(), Context); |
| 225 | if (!Found) |
| 226 | return 0; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // Check for single element. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 231 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 232 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 233 | const FieldDecl *FD = *i; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 234 | QualType FT = FD->getType(); |
| 235 | |
| 236 | // Ignore empty fields. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 237 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 238 | continue; |
| 239 | |
| 240 | // If we already found an element then this isn't a single-element |
| 241 | // struct. |
| 242 | if (Found) |
| 243 | return 0; |
| 244 | |
| 245 | // Treat single element arrays as the element. |
| 246 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 247 | if (AT->getSize().getZExtValue() != 1) |
| 248 | break; |
| 249 | FT = AT->getElementType(); |
| 250 | } |
| 251 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 252 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 253 | Found = FT.getTypePtr(); |
| 254 | } else { |
| 255 | Found = isSingleElementStruct(FT, Context); |
| 256 | if (!Found) |
| 257 | return 0; |
| 258 | } |
| 259 | } |
| 260 | |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 261 | // We don't consider a struct a single-element struct if it has |
| 262 | // padding beyond the element type. |
| 263 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
| 264 | return 0; |
| 265 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 266 | return Found; |
| 267 | } |
| 268 | |
| 269 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | db748a3 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 270 | // Treat complex types as the element type. |
| 271 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 272 | Ty = CTy->getElementType(); |
| 273 | |
| 274 | // Check for a type which we know has a simple scalar argument-passing |
| 275 | // convention without any padding. (We're specifically looking for 32 |
| 276 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 277 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | db748a3 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 278 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 279 | return false; |
| 280 | |
| 281 | uint64_t Size = Context.getTypeSize(Ty); |
| 282 | return Size == 32 || Size == 64; |
| 283 | } |
| 284 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 285 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 286 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 287 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 288 | /// inhibiting optimizations. |
| 289 | /// |
| 290 | // FIXME: This predicate is missing many cases, currently it just follows |
| 291 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 292 | // should probably make this smarter, or better yet make the LLVM backend |
| 293 | // capable of handling it. |
| 294 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 295 | // We can only expand structure types. |
| 296 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 297 | if (!RT) |
| 298 | return false; |
| 299 | |
| 300 | // We can only expand (C) structures. |
| 301 | // |
| 302 | // FIXME: This needs to be generalized to handle classes as well. |
| 303 | const RecordDecl *RD = RT->getDecl(); |
| 304 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 305 | return false; |
| 306 | |
Eli Friedman | 506d4e3 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 307 | uint64_t Size = 0; |
| 308 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 309 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 310 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 311 | const FieldDecl *FD = *i; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 312 | |
| 313 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 314 | return false; |
| 315 | |
| 316 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 317 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 318 | // counts as "basic" is more complicated than what we were doing previously. |
| 319 | if (FD->isBitField()) |
| 320 | return false; |
Eli Friedman | 506d4e3 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 321 | |
| 322 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Eli Friedman | 506d4e3 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 325 | // Make sure there are not any holes in the struct. |
| 326 | if (Size != Context.getTypeSize(Ty)) |
| 327 | return false; |
| 328 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
| 332 | namespace { |
| 333 | /// DefaultABIInfo - The default implementation for ABI specific |
| 334 | /// details. This implementation provides information which results in |
| 335 | /// self-consistent and sensible LLVM IR generation, but does not |
| 336 | /// conform to any particular ABI. |
| 337 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 338 | public: |
| 339 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 340 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 341 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 342 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 343 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 344 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 345 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 346 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 347 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 348 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 352 | CodeGenFunction &CGF) const; |
| 353 | }; |
| 354 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 355 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 356 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 357 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 358 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 362 | CodeGenFunction &CGF) const { |
| 363 | return 0; |
| 364 | } |
| 365 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 366 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Jan Wen Voung | 9030693 | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 367 | if (isAggregateTypeForABI(Ty)) { |
| 368 | // Records with non trivial destructors/constructors should not be passed |
| 369 | // by value. |
| 370 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 371 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 372 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 373 | return ABIArgInfo::getIndirect(0); |
Jan Wen Voung | 9030693 | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 374 | } |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 375 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 376 | // Treat an enum type as its underlying type. |
| 377 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 378 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 379 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 380 | return (Ty->isPromotableIntegerType() ? |
| 381 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Bob Wilson | 0024f94 | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 384 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 385 | if (RetTy->isVoidType()) |
| 386 | return ABIArgInfo::getIgnore(); |
| 387 | |
| 388 | if (isAggregateTypeForABI(RetTy)) |
| 389 | return ABIArgInfo::getIndirect(0); |
| 390 | |
| 391 | // Treat an enum type as its underlying type. |
| 392 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 393 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 394 | |
| 395 | return (RetTy->isPromotableIntegerType() ? |
| 396 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 397 | } |
| 398 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | // le32/PNaCl bitcode ABI Implementation |
| 401 | //===----------------------------------------------------------------------===// |
| 402 | |
| 403 | class PNaClABIInfo : public ABIInfo { |
| 404 | public: |
| 405 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 406 | |
| 407 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 408 | ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs) const; |
| 409 | |
| 410 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 411 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 412 | CodeGenFunction &CGF) const; |
| 413 | }; |
| 414 | |
| 415 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 416 | public: |
| 417 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 418 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 419 | }; |
| 420 | |
| 421 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 422 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 423 | |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame^] | 424 | // Obtain the initial number of registers available for passing integers |
| 425 | // from the function's regparm attribute. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 426 | unsigned FreeRegs = FI.getHasRegParm() ? FI.getRegParm() : 0; |
| 427 | |
| 428 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 429 | it != ie; ++it) |
| 430 | it->info = classifyArgumentType(it->type, FreeRegs); |
| 431 | } |
| 432 | |
| 433 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 434 | CodeGenFunction &CGF) const { |
| 435 | return 0; |
| 436 | } |
| 437 | |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame^] | 438 | // \brief Classify argument of given type \p Ty. \p FreeRegs is the number of |
| 439 | // registers available for passing arguments - it can be updated by this |
| 440 | // method. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 441 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty, |
| 442 | unsigned &FreeRegs) const { |
| 443 | if (isAggregateTypeForABI(Ty)) { |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame^] | 444 | // In the PNaCl ABI we always pass records/structures on the stack. The |
| 445 | // byval attribute can be used if the record doesn't have non-trivial |
| 446 | // constructors/destructors. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 447 | FreeRegs = 0; |
| 448 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 449 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 450 | return ABIArgInfo::getIndirect(0); |
| 451 | } |
| 452 | |
| 453 | // Treat an enum type as its underlying type. |
| 454 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 455 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 456 | |
| 457 | ABIArgInfo BaseInfo = (Ty->isPromotableIntegerType() ? |
| 458 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 459 | |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame^] | 460 | // Figure out how many of the free registers can be occupied by this type. |
| 461 | // regparm registers are 32-bit. |
| 462 | unsigned NumRegsRequired = (getContext().getTypeSize(Ty) + 31) / 32; |
| 463 | if (NumRegsRequired == 0) return BaseInfo; |
| 464 | if (NumRegsRequired > FreeRegs) { |
| 465 | // If this type needs more registers than we have available, no more |
| 466 | // passing in-registers can happen. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 467 | FreeRegs = 0; |
| 468 | return BaseInfo; |
| 469 | } |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame^] | 470 | FreeRegs -= NumRegsRequired; |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 471 | return BaseInfo.isDirect() ? |
| 472 | ABIArgInfo::getDirectInReg(BaseInfo.getCoerceToType()) : |
| 473 | ABIArgInfo::getExtendInReg(BaseInfo.getCoerceToType()); |
| 474 | } |
| 475 | |
| 476 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 477 | if (RetTy->isVoidType()) |
| 478 | return ABIArgInfo::getIgnore(); |
| 479 | |
Eli Bendersky | e45dfd1 | 2013-04-04 22:49:35 +0000 | [diff] [blame^] | 480 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 481 | if (isAggregateTypeForABI(RetTy)) |
| 482 | return ABIArgInfo::getIndirect(0); |
| 483 | |
| 484 | // Treat an enum type as its underlying type. |
| 485 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 486 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 487 | |
| 488 | return (RetTy->isPromotableIntegerType() ? |
| 489 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 490 | } |
| 491 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 492 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 493 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 494 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 495 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 496 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 497 | IRType->getScalarSizeInBits() != 64; |
| 498 | } |
| 499 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 500 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 501 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 502 | llvm::Type* Ty) { |
Bill Wendling | 0507be6 | 2011-03-07 22:47:14 +0000 | [diff] [blame] | 503 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 504 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
| 505 | return Ty; |
| 506 | } |
| 507 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 508 | //===----------------------------------------------------------------------===// |
| 509 | // X86-32 ABI Implementation |
| 510 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 511 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 512 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 513 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 514 | enum Class { |
| 515 | Integer, |
| 516 | Float |
| 517 | }; |
| 518 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 519 | static const unsigned MinABIStackAlignInBytes = 4; |
| 520 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 521 | bool IsDarwinVectorABI; |
| 522 | bool IsSmallStructInRegABI; |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 523 | bool IsWin32FloatStructABI; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 524 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 525 | |
| 526 | static bool isRegisterSize(unsigned Size) { |
| 527 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 528 | } |
| 529 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 530 | static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 531 | unsigned callingConvention); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 532 | |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 533 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 534 | /// such that the argument will be passed in memory. |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 535 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, |
| 536 | unsigned &FreeRegs) const; |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 537 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 538 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 539 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 540 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 541 | Class classify(QualType Ty) const; |
Rafael Espindola | b33a3c4 | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 542 | ABIArgInfo classifyReturnType(QualType RetTy, |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 543 | unsigned callingConvention) const; |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 544 | ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &FreeRegs, |
| 545 | bool IsFastCall) const; |
| 546 | bool shouldUseInReg(QualType Ty, unsigned &FreeRegs, |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 547 | bool IsFastCall, bool &NeedsPadding) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 548 | |
Rafael Espindola | b33a3c4 | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 549 | public: |
| 550 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 551 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 552 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 553 | CodeGenFunction &CGF) const; |
| 554 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 555 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 556 | unsigned r) |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 557 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 558 | IsWin32FloatStructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 559 | }; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 560 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 561 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 562 | public: |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 563 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 564 | bool d, bool p, bool w, unsigned r) |
| 565 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 566 | |
| 567 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 568 | CodeGen::CodeGenModule &CGM) const; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 569 | |
| 570 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 571 | // Darwin uses different dwarf register numbers for EH. |
| 572 | if (CGM.isTargetDarwin()) return 5; |
| 573 | |
| 574 | return 4; |
| 575 | } |
| 576 | |
| 577 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 578 | llvm::Value *Address) const; |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 579 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 580 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 581 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 582 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 583 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 584 | } |
| 585 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 586 | }; |
| 587 | |
| 588 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 589 | |
| 590 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 591 | /// passed in a register (for the Darwin ABI). |
| 592 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 593 | ASTContext &Context, |
| 594 | unsigned callingConvention) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 595 | uint64_t Size = Context.getTypeSize(Ty); |
| 596 | |
| 597 | // Type must be register sized. |
| 598 | if (!isRegisterSize(Size)) |
| 599 | return false; |
| 600 | |
| 601 | if (Ty->isVectorType()) { |
| 602 | // 64- and 128- bit vectors inside structures are not returned in |
| 603 | // registers. |
| 604 | if (Size == 64 || Size == 128) |
| 605 | return false; |
| 606 | |
| 607 | return true; |
| 608 | } |
| 609 | |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 610 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 611 | // member function pointer it is ok. |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 612 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 613 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 614 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 615 | return true; |
| 616 | |
| 617 | // Arrays are treated like records. |
| 618 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 619 | return shouldReturnTypeInRegister(AT->getElementType(), Context, |
| 620 | callingConvention); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 621 | |
| 622 | // Otherwise, it must be a record type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 623 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 624 | if (!RT) return false; |
| 625 | |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 626 | // FIXME: Traverse bases here too. |
| 627 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 628 | // For thiscall conventions, structures will never be returned in |
| 629 | // a register. This is for compatibility with the MSVC ABI |
| 630 | if (callingConvention == llvm::CallingConv::X86_ThisCall && |
| 631 | RT->isStructureType()) { |
| 632 | return false; |
| 633 | } |
| 634 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 635 | // Structure types are passed in register if all fields would be |
| 636 | // passed in a register. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 637 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), |
| 638 | e = RT->getDecl()->field_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 639 | const FieldDecl *FD = *i; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 640 | |
| 641 | // Empty fields are ignored. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 642 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 643 | continue; |
| 644 | |
| 645 | // Check fields recursively. |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 646 | if (!shouldReturnTypeInRegister(FD->getType(), Context, |
| 647 | callingConvention)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 648 | return false; |
| 649 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 650 | return true; |
| 651 | } |
| 652 | |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 653 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 654 | unsigned callingConvention) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 655 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 656 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 657 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 658 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 659 | // On Darwin, some vectors are returned in registers. |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 660 | if (IsDarwinVectorABI) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 661 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 662 | |
| 663 | // 128-bit vectors are a special case; they are returned in |
| 664 | // registers and we need to make sure to pick a type the LLVM |
| 665 | // backend will like. |
| 666 | if (Size == 128) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 667 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 668 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 669 | |
| 670 | // Always return in register if it fits in a general purpose |
| 671 | // register, or if it is 64 bits and has a single element. |
| 672 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 673 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 674 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 675 | Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 676 | |
| 677 | return ABIArgInfo::getIndirect(0); |
| 678 | } |
| 679 | |
| 680 | return ABIArgInfo::getDirect(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 681 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 682 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 683 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 684 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 685 | // Structures with either a non-trivial destructor or a non-trivial |
| 686 | // copy constructor are always indirect. |
| 687 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 688 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 689 | |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 690 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 691 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 692 | return ABIArgInfo::getIndirect(0); |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 693 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 694 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 695 | // If specified, structs and unions are always indirect. |
| 696 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 697 | return ABIArgInfo::getIndirect(0); |
| 698 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 699 | // Small structures which are register sized are generally returned |
| 700 | // in a register. |
Aaron Ballman | 6c60c8d | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 701 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext(), |
| 702 | callingConvention)) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 703 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 704 | |
| 705 | // As a special-case, if the struct is a "single-element" struct, and |
| 706 | // the field is of type "float" or "double", return it in a |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 707 | // floating-point register. (MSVC does not apply this special case.) |
| 708 | // We apply a similar transformation for pointer types to improve the |
| 709 | // quality of the generated IR. |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 710 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 711 | if ((!IsWin32FloatStructABI && SeltTy->isRealFloatingType()) |
| 712 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | bd4d3bc | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 713 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 714 | |
| 715 | // FIXME: We should be able to narrow this integer in cases with dead |
| 716 | // padding. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 717 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 721 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 722 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 723 | // Treat an enum type as its underlying type. |
| 724 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 725 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 726 | |
| 727 | return (RetTy->isPromotableIntegerType() ? |
| 728 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 731 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 732 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 733 | } |
| 734 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 735 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 736 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 737 | if (!RT) |
| 738 | return 0; |
| 739 | const RecordDecl *RD = RT->getDecl(); |
| 740 | |
| 741 | // If this is a C++ record, check the bases first. |
| 742 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 743 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 744 | e = CXXRD->bases_end(); i != e; ++i) |
| 745 | if (!isRecordWithSSEVectorType(Context, i->getType())) |
| 746 | return false; |
| 747 | |
| 748 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 749 | i != e; ++i) { |
| 750 | QualType FT = i->getType(); |
| 751 | |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 752 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 753 | return true; |
| 754 | |
| 755 | if (isRecordWithSSEVectorType(Context, FT)) |
| 756 | return true; |
| 757 | } |
| 758 | |
| 759 | return false; |
| 760 | } |
| 761 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 762 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 763 | unsigned Align) const { |
| 764 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 765 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 766 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 767 | return 0; // Use default alignment. |
| 768 | |
| 769 | // On non-Darwin, the stack type alignment is always 4. |
| 770 | if (!IsDarwinVectorABI) { |
| 771 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 772 | return MinABIStackAlignInBytes; |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 773 | } |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 774 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 775 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
Eli Friedman | f4bd4d8 | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 776 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 777 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 778 | return 16; |
| 779 | |
| 780 | return MinABIStackAlignInBytes; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 783 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 784 | unsigned &FreeRegs) const { |
| 785 | if (!ByVal) { |
| 786 | if (FreeRegs) { |
| 787 | --FreeRegs; // Non byval indirects just use one pointer. |
| 788 | return ABIArgInfo::getIndirectInReg(0, false); |
| 789 | } |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 790 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 791 | } |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 792 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 793 | // Compute the byval alignment. |
| 794 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 795 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 796 | if (StackAlign == 0) |
Chris Lattner | de92d73 | 2011-05-22 23:35:00 +0000 | [diff] [blame] | 797 | return ABIArgInfo::getIndirect(4); |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 798 | |
| 799 | // If the stack alignment is less than the type alignment, realign the |
| 800 | // argument. |
| 801 | if (StackAlign < TypeAlign) |
| 802 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, |
| 803 | /*Realign=*/true); |
| 804 | |
| 805 | return ABIArgInfo::getIndirect(StackAlign); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 808 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 809 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 810 | if (!T) |
| 811 | T = Ty.getTypePtr(); |
| 812 | |
| 813 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 814 | BuiltinType::Kind K = BT->getKind(); |
| 815 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 816 | return Float; |
| 817 | } |
| 818 | return Integer; |
| 819 | } |
| 820 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 821 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, unsigned &FreeRegs, |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 822 | bool IsFastCall, bool &NeedsPadding) const { |
| 823 | NeedsPadding = false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 824 | Class C = classify(Ty); |
| 825 | if (C == Float) |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 826 | return false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 827 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 828 | unsigned Size = getContext().getTypeSize(Ty); |
| 829 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | 5f14fcb | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 830 | |
| 831 | if (SizeInRegs == 0) |
| 832 | return false; |
| 833 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 834 | if (SizeInRegs > FreeRegs) { |
| 835 | FreeRegs = 0; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 836 | return false; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 837 | } |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 838 | |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 839 | FreeRegs -= SizeInRegs; |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 840 | |
| 841 | if (IsFastCall) { |
| 842 | if (Size > 32) |
| 843 | return false; |
| 844 | |
| 845 | if (Ty->isIntegralOrEnumerationType()) |
| 846 | return true; |
| 847 | |
| 848 | if (Ty->isPointerType()) |
| 849 | return true; |
| 850 | |
| 851 | if (Ty->isReferenceType()) |
| 852 | return true; |
| 853 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 854 | if (FreeRegs) |
| 855 | NeedsPadding = true; |
| 856 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 857 | return false; |
| 858 | } |
| 859 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 860 | return true; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 863 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 864 | unsigned &FreeRegs, |
| 865 | bool IsFastCall) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 866 | // FIXME: Set alignment on indirect arguments. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 867 | if (isAggregateTypeForABI(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 868 | // Structures with flexible arrays are always indirect. |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 869 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 870 | // Structures with either a non-trivial destructor or a non-trivial |
| 871 | // copy constructor are always indirect. |
| 872 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 873 | return getIndirectResult(Ty, false, FreeRegs); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 874 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 875 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 876 | return getIndirectResult(Ty, true, FreeRegs); |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 877 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 878 | |
Eli Friedman | 5a4d352 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 879 | // Ignore empty structs/unions. |
Eli Friedman | 5a1ac89 | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 880 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 881 | return ABIArgInfo::getIgnore(); |
| 882 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 883 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 884 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 885 | bool NeedsPadding; |
| 886 | if (shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding)) { |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 887 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 888 | SmallVector<llvm::Type*, 3> Elements; |
| 889 | for (unsigned I = 0; I < SizeInRegs; ++I) |
| 890 | Elements.push_back(Int32); |
| 891 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 892 | return ABIArgInfo::getDirectInReg(Result); |
| 893 | } |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 894 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0; |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 895 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 896 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 897 | // of those arguments will match the struct. This is important because the |
| 898 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 899 | // optimizations. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 900 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 901 | canExpandIndirectArgument(Ty, getContext())) |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 902 | return ABIArgInfo::getExpandWithPadding(IsFastCall, PaddingType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 903 | |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 904 | return getIndirectResult(Ty, true, FreeRegs); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 907 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 7b73350 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 908 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 909 | // it as an i8/i16/i32/i64. |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 910 | if (IsDarwinVectorABI) { |
| 911 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 912 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 913 | (Size == 64 && VT->getNumElements() == 1)) |
| 914 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 915 | Size)); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 916 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 917 | |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 918 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 919 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 920 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 921 | return ABIArgInfo::getDirect(); |
| 922 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 923 | |
| 924 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 925 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 926 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 927 | |
Rafael Espindola | e4aeeaa | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 928 | bool NeedsPadding; |
| 929 | bool InReg = shouldUseInReg(Ty, FreeRegs, IsFastCall, NeedsPadding); |
Rafael Espindola | 0b4cc95 | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 930 | |
| 931 | if (Ty->isPromotableIntegerType()) { |
| 932 | if (InReg) |
| 933 | return ABIArgInfo::getExtendInReg(); |
| 934 | return ABIArgInfo::getExtend(); |
| 935 | } |
| 936 | if (InReg) |
| 937 | return ABIArgInfo::getDirectInReg(); |
| 938 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 941 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 942 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), |
| 943 | FI.getCallingConvention()); |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 944 | |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 945 | unsigned CC = FI.getCallingConvention(); |
| 946 | bool IsFastCall = CC == llvm::CallingConv::X86_FastCall; |
| 947 | unsigned FreeRegs; |
| 948 | if (IsFastCall) |
| 949 | FreeRegs = 2; |
| 950 | else if (FI.getHasRegParm()) |
| 951 | FreeRegs = FI.getRegParm(); |
| 952 | else |
| 953 | FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 954 | |
| 955 | // If the return value is indirect, then the hidden argument is consuming one |
| 956 | // integer register. |
| 957 | if (FI.getReturnInfo().isIndirect() && FreeRegs) { |
| 958 | --FreeRegs; |
| 959 | ABIArgInfo &Old = FI.getReturnInfo(); |
| 960 | Old = ABIArgInfo::getIndirectInReg(Old.getIndirectAlign(), |
| 961 | Old.getIndirectByVal(), |
| 962 | Old.getIndirectRealign()); |
| 963 | } |
| 964 | |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 965 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 966 | it != ie; ++it) |
Rafael Espindola | b693269 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 967 | it->info = classifyArgumentType(it->type, FreeRegs, IsFastCall); |
Rafael Espindola | aa9cf8d | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 970 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 971 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 972 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 973 | |
| 974 | CGBuilderTy &Builder = CGF.Builder; |
| 975 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 976 | "ap"); |
| 977 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 978 | |
| 979 | // Compute if the address needs to be aligned |
| 980 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 981 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 982 | Align = std::max(Align, 4U); |
| 983 | if (Align > 4) { |
| 984 | // addr = (addr + align - 1) & -align; |
| 985 | llvm::Value *Offset = |
| 986 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 987 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 988 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 989 | CGF.Int32Ty); |
| 990 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 991 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 992 | Addr->getType(), |
| 993 | "ap.cur.aligned"); |
| 994 | } |
| 995 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 996 | llvm::Type *PTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 997 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 998 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 999 | |
| 1000 | uint64_t Offset = |
Eli Friedman | 7b1fb81 | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1001 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1002 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1003 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1004 | "ap.next"); |
| 1005 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1006 | |
| 1007 | return AddrTyped; |
| 1008 | } |
| 1009 | |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1010 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1011 | llvm::GlobalValue *GV, |
| 1012 | CodeGen::CodeGenModule &CGM) const { |
| 1013 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1014 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1015 | // Get the LLVM function. |
| 1016 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1017 | |
| 1018 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | 0d58339 | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1019 | llvm::AttrBuilder B; |
Bill Wendling | e91e9ec | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1020 | B.addStackAlignmentAttr(16); |
Bill Wendling | 909b6de | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1021 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1022 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1023 | llvm::AttributeSet::FunctionIndex, |
| 1024 | B)); |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1029 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1030 | CodeGen::CodeGenFunction &CGF, |
| 1031 | llvm::Value *Address) const { |
| 1032 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1033 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1034 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1035 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1036 | // 0-7 are the eight integer registers; the order is different |
| 1037 | // on Darwin (for EH), but the range is the same. |
| 1038 | // 8 is %eip. |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1039 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1040 | |
| 1041 | if (CGF.CGM.isTargetDarwin()) { |
| 1042 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1043 | // These have size 16, which is sizeof(long double) on |
| 1044 | // platforms with 8-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1045 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1046 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1047 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1048 | } else { |
| 1049 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1050 | // reason. |
| 1051 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1052 | |
| 1053 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1054 | // These have size 12, which is sizeof(long double) on |
| 1055 | // platforms with 4-byte alignment for that type. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1056 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1057 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1058 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1059 | |
| 1060 | return false; |
| 1061 | } |
| 1062 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1063 | //===----------------------------------------------------------------------===// |
| 1064 | // X86-64 ABI Implementation |
| 1065 | //===----------------------------------------------------------------------===// |
| 1066 | |
| 1067 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1068 | namespace { |
| 1069 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1070 | class X86_64ABIInfo : public ABIInfo { |
| 1071 | enum Class { |
| 1072 | Integer = 0, |
| 1073 | SSE, |
| 1074 | SSEUp, |
| 1075 | X87, |
| 1076 | X87Up, |
| 1077 | ComplexX87, |
| 1078 | NoClass, |
| 1079 | Memory |
| 1080 | }; |
| 1081 | |
| 1082 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1083 | /// |
| 1084 | /// Merge an accumulating classification \arg Accum with a field |
| 1085 | /// classification \arg Field. |
| 1086 | /// |
| 1087 | /// \param Accum - The accumulating classification. This should |
| 1088 | /// always be either NoClass or the result of a previous merge |
| 1089 | /// call. In addition, this should never be Memory (the caller |
| 1090 | /// should just return Memory for the aggregate). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1091 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1092 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1093 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1094 | /// |
| 1095 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1096 | /// final MEMORY or SSE classes when necessary. |
| 1097 | /// |
| 1098 | /// \param AggregateSize - The size of the current aggregate in |
| 1099 | /// the classification process. |
| 1100 | /// |
| 1101 | /// \param Lo - The classification for the parts of the type |
| 1102 | /// residing in the low word of the containing object. |
| 1103 | /// |
| 1104 | /// \param Hi - The classification for the parts of the type |
| 1105 | /// residing in the higher words of the containing object. |
| 1106 | /// |
| 1107 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1108 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1109 | /// classify - Determine the x86_64 register classes in which the |
| 1110 | /// given type T should be passed. |
| 1111 | /// |
| 1112 | /// \param Lo - The classification for the parts of the type |
| 1113 | /// residing in the low word of the containing object. |
| 1114 | /// |
| 1115 | /// \param Hi - The classification for the parts of the type |
| 1116 | /// residing in the high word of the containing object. |
| 1117 | /// |
| 1118 | /// \param OffsetBase - The bit offset of this type in the |
| 1119 | /// containing object. Some parameters are classified different |
| 1120 | /// depending on whether they straddle an eightbyte boundary. |
| 1121 | /// |
| 1122 | /// If a word is unused its result will be NoClass; if a type should |
| 1123 | /// be passed in Memory then at least the classification of \arg Lo |
| 1124 | /// will be Memory. |
| 1125 | /// |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1126 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1127 | /// |
| 1128 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1129 | /// also be ComplexX87. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1130 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1131 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1132 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1133 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1134 | unsigned IROffset, QualType SourceTy, |
| 1135 | unsigned SourceOffset) const; |
| 1136 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1137 | unsigned IROffset, QualType SourceTy, |
| 1138 | unsigned SourceOffset) const; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1139 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1140 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1141 | /// such that the argument will be returned in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1142 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1143 | |
| 1144 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1145 | /// such that the argument will be passed in memory. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1146 | /// |
| 1147 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1148 | /// available. |
| 1149 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1151 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1152 | |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1153 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1154 | unsigned freeIntRegs, |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1155 | unsigned &neededInt, |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1156 | unsigned &neededSSE) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1157 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1158 | bool IsIllegalVectorType(QualType Ty) const; |
| 1159 | |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1160 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1161 | /// unfortunately in ways that were not always consistent with |
| 1162 | /// certain previous compilers. In particular, platforms which |
| 1163 | /// required strict binary compatibility with older versions of GCC |
| 1164 | /// may need to exempt themselves. |
| 1165 | bool honorsRevision0_98() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1166 | return !getContext().getTargetInfo().getTriple().isOSDarwin(); |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1169 | bool HasAVX; |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1170 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1171 | // 64-bit hardware. |
| 1172 | bool Has64BitPointers; |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1173 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1174 | public: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1175 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1176 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 90da80c | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1177 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1178 | } |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1179 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1180 | bool isPassedUsingAVXType(QualType type) const { |
| 1181 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1182 | // The freeIntRegs argument doesn't matter here. |
| 1183 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1184 | if (info.isDirect()) { |
| 1185 | llvm::Type *ty = info.getCoerceToType(); |
| 1186 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1187 | return (vectorTy->getBitWidth() > 128); |
| 1188 | } |
| 1189 | return false; |
| 1190 | } |
| 1191 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 1192 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1193 | |
| 1194 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1195 | CodeGenFunction &CGF) const; |
| 1196 | }; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1198 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1199 | class WinX86_64ABIInfo : public ABIInfo { |
| 1200 | |
| 1201 | ABIArgInfo classify(QualType Ty) const; |
| 1202 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1203 | public: |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1204 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1205 | |
| 1206 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1207 | |
| 1208 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1209 | CodeGenFunction &CGF) const; |
| 1210 | }; |
| 1211 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1212 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1213 | public: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1214 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1215 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1216 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1217 | const X86_64ABIInfo &getABIInfo() const { |
| 1218 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1219 | } |
| 1220 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1221 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 1222 | return 7; |
| 1223 | } |
| 1224 | |
| 1225 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 1226 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1227 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1228 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1229 | // 0-15 are the 16 integer registers. |
| 1230 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1231 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1232 | return false; |
| 1233 | } |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1234 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1235 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1236 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1237 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1238 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1239 | } |
| 1240 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1241 | bool isNoProtoCallVariadic(const CallArgList &args, |
| 1242 | const FunctionNoProtoType *fnType) const { |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1243 | // The default CC on x86-64 sets %al to the number of SSA |
| 1244 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1245 | // function, so we override the default behavior. However, don't do |
Eli Friedman | 68805fe | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1246 | // that when AVX types are involved: the ABI explicitly states it is |
| 1247 | // undefined, and it doesn't work in practice because of how the ABI |
| 1248 | // defines varargs anyway. |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1249 | if (fnType->getCallConv() == CC_Default || fnType->getCallConv() == CC_C) { |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1250 | bool HasAVXType = false; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1251 | for (CallArgList::const_iterator |
| 1252 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1253 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1254 | HasAVXType = true; |
| 1255 | break; |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1256 | } |
| 1257 | } |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1258 | |
Eli Friedman | 3ed7903 | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1259 | if (!HasAVXType) |
| 1260 | return true; |
| 1261 | } |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1262 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1263 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1266 | }; |
| 1267 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1268 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1269 | public: |
| 1270 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1271 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 1272 | |
| 1273 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 1274 | return 7; |
| 1275 | } |
| 1276 | |
| 1277 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 1278 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1279 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1280 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1281 | // 0-15 are the 16 integer registers. |
| 1282 | // 16 is %rip. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1283 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1284 | return false; |
| 1285 | } |
| 1286 | }; |
| 1287 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1290 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1291 | Class &Hi) const { |
| 1292 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1293 | // |
| 1294 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1295 | // memory. |
| 1296 | // |
| 1297 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1298 | // memory. |
| 1299 | // |
| 1300 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1301 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1302 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1303 | // ABI working for processors that don't support the __m256 type. |
| 1304 | // |
| 1305 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1306 | // |
| 1307 | // Some of these are enforced by the merging logic. Others can arise |
| 1308 | // only with unions; for example: |
| 1309 | // union { _Complex double; unsigned; } |
| 1310 | // |
| 1311 | // Note that clauses (b) and (c) were added in 0.98. |
| 1312 | // |
| 1313 | if (Hi == Memory) |
| 1314 | Lo = Memory; |
| 1315 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1316 | Lo = Memory; |
| 1317 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1318 | Lo = Memory; |
| 1319 | if (Hi == SSEUp && Lo != SSE) |
| 1320 | Hi = SSE; |
| 1321 | } |
| 1322 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1323 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1324 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1325 | // classified recursively so that always two fields are |
| 1326 | // considered. The resulting class is calculated according to |
| 1327 | // the classes of the fields in the eightbyte: |
| 1328 | // |
| 1329 | // (a) If both classes are equal, this is the resulting class. |
| 1330 | // |
| 1331 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1332 | // the other class. |
| 1333 | // |
| 1334 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1335 | // class. |
| 1336 | // |
| 1337 | // (d) If one of the classes is INTEGER, the result is the |
| 1338 | // INTEGER. |
| 1339 | // |
| 1340 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1341 | // MEMORY is used as class. |
| 1342 | // |
| 1343 | // (f) Otherwise class SSE is used. |
| 1344 | |
| 1345 | // Accum should never be memory (we should have returned) or |
| 1346 | // ComplexX87 (because this cannot be passed in a structure). |
| 1347 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1348 | "Invalid accumulated classification during merge."); |
| 1349 | if (Accum == Field || Field == NoClass) |
| 1350 | return Accum; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1351 | if (Field == Memory) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1352 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1353 | if (Accum == NoClass) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1354 | return Field; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1355 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1356 | return Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1357 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1358 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1359 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1360 | return SSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1363 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1364 | Class &Lo, Class &Hi) const { |
| 1365 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1366 | // Class pairs with appropriate constructor methods for the various |
| 1367 | // situations. |
| 1368 | |
| 1369 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1370 | // shouldn't be passed in registers for example, so there is no chance they |
| 1371 | // can straddle an eightbyte. Verify & simplify. |
| 1372 | |
| 1373 | Lo = Hi = NoClass; |
| 1374 | |
| 1375 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1376 | Current = Memory; |
| 1377 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1378 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1379 | BuiltinType::Kind k = BT->getKind(); |
| 1380 | |
| 1381 | if (k == BuiltinType::Void) { |
| 1382 | Current = NoClass; |
| 1383 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1384 | Lo = Integer; |
| 1385 | Hi = Integer; |
| 1386 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1387 | Current = Integer; |
Derek Schuff | 7da46f9 | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1388 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1389 | (k == BuiltinType::LongDouble && |
| 1390 | getContext().getTargetInfo().getTriple().getOS() == |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 1391 | llvm::Triple::NaCl)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1392 | Current = SSE; |
| 1393 | } else if (k == BuiltinType::LongDouble) { |
| 1394 | Lo = X87; |
| 1395 | Hi = X87Up; |
| 1396 | } |
| 1397 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1398 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1401 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1402 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1403 | // Classify the underlying integer type. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1404 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1405 | return; |
| 1406 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1407 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1408 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1409 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1410 | return; |
| 1411 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1412 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1413 | if (Ty->isMemberPointerType()) { |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1414 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 67d438d | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1415 | Lo = Hi = Integer; |
| 1416 | else |
| 1417 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1418 | return; |
| 1419 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1420 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1421 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1422 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1423 | if (Size == 32) { |
| 1424 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1425 | // float> as integer. |
| 1426 | Current = Integer; |
| 1427 | |
| 1428 | // If this type crosses an eightbyte boundary, it should be |
| 1429 | // split. |
| 1430 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1431 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1432 | if (EB_Real != EB_Imag) |
| 1433 | Hi = Lo; |
| 1434 | } else if (Size == 64) { |
| 1435 | // gcc passes <1 x double> in memory. :( |
| 1436 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1437 | return; |
| 1438 | |
| 1439 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 473f8e7 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1440 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 0fefa41 | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1441 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1442 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1443 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1444 | Current = Integer; |
| 1445 | else |
| 1446 | Current = SSE; |
| 1447 | |
| 1448 | // If this type crosses an eightbyte boundary, it should be |
| 1449 | // split. |
| 1450 | if (OffsetBase && OffsetBase != 64) |
| 1451 | Hi = Lo; |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1452 | } else if (Size == 128 || (HasAVX && Size == 256)) { |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1453 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1454 | // least significant one belongs to class SSE and all the others to class |
| 1455 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1456 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1457 | // This design isn't correct for 256-bits, but since there're no cases |
| 1458 | // where the upper parts would need to be inspected, avoid adding |
| 1459 | // complexity and just consider Hi to match the 64-256 part. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1460 | Lo = SSE; |
| 1461 | Hi = SSEUp; |
| 1462 | } |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1463 | return; |
| 1464 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1466 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1467 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1468 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1469 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1470 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1471 | if (Size <= 64) |
| 1472 | Current = Integer; |
| 1473 | else if (Size <= 128) |
| 1474 | Lo = Hi = Integer; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1475 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1476 | Current = SSE; |
Derek Schuff | 7da46f9 | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1477 | else if (ET == getContext().DoubleTy || |
| 1478 | (ET == getContext().LongDoubleTy && |
| 1479 | getContext().getTargetInfo().getTriple().getOS() == |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 1480 | llvm::Triple::NaCl)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1481 | Lo = Hi = SSE; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1482 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1483 | Current = ComplexX87; |
| 1484 | |
| 1485 | // If this complex type crosses an eightbyte boundary then it |
| 1486 | // should be split. |
| 1487 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1488 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1489 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1490 | Hi = Lo; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1495 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1496 | // Arrays are treated like structures. |
| 1497 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1498 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1499 | |
| 1500 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1501 | // than four eightbytes, ..., it has class MEMORY. |
| 1502 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1503 | return; |
| 1504 | |
| 1505 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1506 | // fields, it has class MEMORY. |
| 1507 | // |
| 1508 | // Only need to check alignment of array base. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1509 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | |
| 1512 | // Otherwise implement simplified merge. We could be smarter about |
| 1513 | // this, but it isn't worth it and would be harder to verify. |
| 1514 | Current = NoClass; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1515 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1516 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 089d892 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1517 | |
| 1518 | // The only case a 256-bit wide vector could be used is when the array |
| 1519 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1520 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1521 | if (Size > 128 && EltSize != 256) |
| 1522 | return; |
| 1523 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1524 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1525 | Class FieldLo, FieldHi; |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1526 | classify(AT->getElementType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1527 | Lo = merge(Lo, FieldLo); |
| 1528 | Hi = merge(Hi, FieldHi); |
| 1529 | if (Lo == Memory || Hi == Memory) |
| 1530 | break; |
| 1531 | } |
| 1532 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1533 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1534 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1535 | return; |
| 1536 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1538 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1539 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1540 | |
| 1541 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1542 | // than four eightbytes, ..., it has class MEMORY. |
| 1543 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1544 | return; |
| 1545 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1546 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1547 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1548 | // reference. |
| 1549 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 1550 | return; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1551 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1552 | const RecordDecl *RD = RT->getDecl(); |
| 1553 | |
| 1554 | // Assume variable sized types are passed in memory. |
| 1555 | if (RD->hasFlexibleArrayMember()) |
| 1556 | return; |
| 1557 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1558 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Reset Lo class, this will be recomputed. |
| 1561 | Current = NoClass; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1562 | |
| 1563 | // If this is a C++ record, classify the bases first. |
| 1564 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1565 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1566 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1567 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1568 | "Unexpected base class!"); |
| 1569 | const CXXRecordDecl *Base = |
| 1570 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1571 | |
| 1572 | // Classify this field. |
| 1573 | // |
| 1574 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1575 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1576 | // initialized to class NO_CLASS. |
| 1577 | Class FieldLo, FieldHi; |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1578 | uint64_t Offset = |
| 1579 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1580 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1581 | Lo = merge(Lo, FieldLo); |
| 1582 | Hi = merge(Hi, FieldHi); |
| 1583 | if (Lo == Memory || Hi == Memory) |
| 1584 | break; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1589 | unsigned idx = 0; |
Bruno Cardoso Lopes | 548e478 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1590 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1591 | i != e; ++i, ++idx) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1592 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1593 | bool BitField = i->isBitField(); |
| 1594 | |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1595 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1596 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1597 | // |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1598 | // The only case a 256-bit wide vector could be used is when the struct |
| 1599 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1600 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1601 | // |
| 1602 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1603 | Lo = Memory; |
| 1604 | return; |
| 1605 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1606 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1607 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1608 | Lo = Memory; |
| 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | // Classify this field. |
| 1613 | // |
| 1614 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1615 | // exceeds a single eightbyte, each is classified |
| 1616 | // separately. Each eightbyte gets initialized to class |
| 1617 | // NO_CLASS. |
| 1618 | Class FieldLo, FieldHi; |
| 1619 | |
| 1620 | // Bit-fields require special handling, they do not force the |
| 1621 | // structure to be passed in memory even if unaligned, and |
| 1622 | // therefore they can straddle an eightbyte. |
| 1623 | if (BitField) { |
| 1624 | // Ignore padding bit-fields. |
| 1625 | if (i->isUnnamedBitfield()) |
| 1626 | continue; |
| 1627 | |
| 1628 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1629 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1630 | |
| 1631 | uint64_t EB_Lo = Offset / 64; |
| 1632 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 1633 | FieldLo = FieldHi = NoClass; |
| 1634 | if (EB_Lo) { |
| 1635 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1636 | FieldLo = NoClass; |
| 1637 | FieldHi = Integer; |
| 1638 | } else { |
| 1639 | FieldLo = Integer; |
| 1640 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1641 | } |
| 1642 | } else |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1643 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1644 | Lo = merge(Lo, FieldLo); |
| 1645 | Hi = merge(Hi, FieldHi); |
| 1646 | if (Lo == Memory || Hi == Memory) |
| 1647 | break; |
| 1648 | } |
| 1649 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1650 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1651 | } |
| 1652 | } |
| 1653 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1654 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1655 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1656 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1657 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1658 | // Treat an enum type as its underlying type. |
| 1659 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1660 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1661 | |
| 1662 | return (Ty->isPromotableIntegerType() ? |
| 1663 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1664 | } |
| 1665 | |
| 1666 | return ABIArgInfo::getIndirect(0); |
| 1667 | } |
| 1668 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1669 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1670 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1671 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1672 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1673 | if (Size <= 64 || Size > LargestVector) |
| 1674 | return true; |
| 1675 | } |
| 1676 | |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1680 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1681 | unsigned freeIntRegs) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1682 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1683 | // place naturally. |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1684 | // |
| 1685 | // This assumption is optimistic, as there could be free registers available |
| 1686 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1687 | // the argument in the free register. This does not seem to happen currently, |
| 1688 | // but this code would be much safer if we could mark the argument with |
| 1689 | // 'onstack'. See PR12193. |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1690 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1691 | // Treat an enum type as its underlying type. |
| 1692 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1693 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1694 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1695 | return (Ty->isPromotableIntegerType() ? |
| 1696 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1697 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1698 | |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1699 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 1700 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1702 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1703 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1704 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1705 | |
| 1706 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1707 | // is important for good codegen. |
| 1708 | // |
| 1709 | // We do this by coercing the value into a scalar type which the backend can |
| 1710 | // handle naturally (i.e., without using byval). |
| 1711 | // |
| 1712 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1713 | // free integer registers. Doing this when there are free integer registers |
| 1714 | // would require more care, as we would have to ensure that the coerced value |
| 1715 | // did not claim the unused register. That would require either reording the |
| 1716 | // arguments to the function (so that any subsequent inreg values came first), |
| 1717 | // or only doing this optimization when there were no following arguments that |
| 1718 | // might be inreg. |
| 1719 | // |
| 1720 | // We currently expect it to be rare (particularly in well written code) for |
| 1721 | // arguments to be passed on the stack when there are still free integer |
| 1722 | // registers available (this would typically imply large structs being passed |
| 1723 | // by value), so this seems like a fair tradeoff for now. |
| 1724 | // |
| 1725 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 1726 | // attributes. See PR12193. |
| 1727 | if (freeIntRegs == 0) { |
| 1728 | uint64_t Size = getContext().getTypeSize(Ty); |
| 1729 | |
| 1730 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 1731 | // type, which will end up on the stack (with alignment 8). |
| 1732 | if (Align == 8 && Size <= 64) |
| 1733 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1734 | Size)); |
| 1735 | } |
| 1736 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1737 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1740 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1741 | /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1742 | /// vector register. |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1743 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1744 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1745 | |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1746 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1747 | // strip them off if present. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1748 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1749 | while (STy && STy->getNumElements() == 1) { |
| 1750 | IRType = STy->getElementType(0); |
| 1751 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1752 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1753 | |
Bruno Cardoso Lopes | 528a8c7 | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1754 | // If the preferred type is a 16-byte vector, prefer to pass it. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1755 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1756 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1757 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | ce27567 | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 1758 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1759 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1760 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1761 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1762 | EltTy->isIntegerTy(128))) |
| 1763 | return VT; |
| 1764 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1766 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1767 | } |
| 1768 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1769 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1770 | /// is known to either be off the end of the specified type or being in |
| 1771 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1772 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1773 | /// classification that put one of the two halves in the INTEGER class. |
| 1774 | /// |
| 1775 | /// It is conservatively correct to return false. |
| 1776 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1777 | unsigned EndBit, ASTContext &Context) { |
| 1778 | // If the bytes being queried are off the end of the type, there is no user |
| 1779 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1780 | // types that don't contain interesting padding. |
| 1781 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1782 | if (TySize <= StartBit) |
| 1783 | return true; |
| 1784 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1785 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1786 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1787 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1788 | |
| 1789 | // Check each element to see if the element overlaps with the queried range. |
| 1790 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1791 | // If the element is after the span we care about, then we're done.. |
| 1792 | unsigned EltOffset = i*EltSize; |
| 1793 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1794 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1795 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1796 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1797 | EndBit-EltOffset, Context)) |
| 1798 | return false; |
| 1799 | } |
| 1800 | // If it overlaps no elements, then it is safe to process as padding. |
| 1801 | return true; |
| 1802 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1803 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1804 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1805 | const RecordDecl *RD = RT->getDecl(); |
| 1806 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1807 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1808 | // If this is a C++ record, check the bases first. |
| 1809 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1810 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1811 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1812 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1813 | "Unexpected base class!"); |
| 1814 | const CXXRecordDecl *Base = |
| 1815 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1816 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1817 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | d4f5198 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1818 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1819 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1820 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1821 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
| 1822 | if (!BitsContainNoUserData(i->getType(), BaseStart, |
| 1823 | EndBit-BaseOffset, Context)) |
| 1824 | return false; |
| 1825 | } |
| 1826 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1827 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1828 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1829 | // this could be sped up a lot by being smarter about queried fields, |
| 1830 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 1831 | // much. |
| 1832 | unsigned idx = 0; |
| 1833 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 1834 | i != e; ++i, ++idx) { |
| 1835 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1836 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1837 | // If we found a field after the region we care about, then we're done. |
| 1838 | if (FieldOffset >= EndBit) break; |
| 1839 | |
| 1840 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 1841 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 1842 | Context)) |
| 1843 | return false; |
| 1844 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1845 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1846 | // If nothing in this record overlapped the area of interest, then we're |
| 1847 | // clean. |
| 1848 | return true; |
| 1849 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1850 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1851 | return false; |
| 1852 | } |
| 1853 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1854 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 1855 | /// float member at the specified offset. For example, {int,{float}} has a |
| 1856 | /// float at offset 4. It is conservatively correct for this routine to return |
| 1857 | /// false. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1858 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1859 | const llvm::DataLayout &TD) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1860 | // Base case if we find a float. |
| 1861 | if (IROffset == 0 && IRType->isFloatTy()) |
| 1862 | return true; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1863 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1864 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1865 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1866 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 1867 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 1868 | IROffset -= SL->getElementOffset(Elt); |
| 1869 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 1870 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1871 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1872 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1873 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 1874 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1875 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 1876 | IROffset -= IROffset/EltSize*EltSize; |
| 1877 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 1878 | } |
| 1879 | |
| 1880 | return false; |
| 1881 | } |
| 1882 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1883 | |
| 1884 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 1885 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1886 | llvm::Type *X86_64ABIInfo:: |
| 1887 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1888 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | cba8d31 | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 1889 | // The only three choices we have are either double, <2 x float>, or float. We |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1890 | // pass as float if the last 4 bytes is just padding. This happens for |
| 1891 | // structs that contain 3 floats. |
| 1892 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 1893 | SourceOffset*8+64, getContext())) |
| 1894 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1895 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1896 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 1897 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 1898 | // case. |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1899 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 1900 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 22fd4ba | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 1901 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1902 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1903 | return llvm::Type::getDoubleTy(getVMContext()); |
| 1904 | } |
| 1905 | |
| 1906 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1907 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 1908 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 1909 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 1910 | /// the best LLVM IR type to represent this, which may be i64 or may be anything |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1911 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 1912 | /// etc). |
| 1913 | /// |
| 1914 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 1915 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 1916 | /// the 8-byte value references. PrefType may be null. |
| 1917 | /// |
| 1918 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 1919 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 1920 | /// |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1921 | llvm::Type *X86_64ABIInfo:: |
| 1922 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1923 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1924 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 1925 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 1926 | if (IROffset == 0) { |
| 1927 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1928 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 1929 | IRType->isIntegerTy(64)) |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1930 | return IRType; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1932 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 1933 | // goodness in the source type is just tail padding. This is allowed to |
| 1934 | // kick in for struct {double,int} on the int, but not on |
| 1935 | // struct{double,int,int} because we wouldn't return the second int. We |
| 1936 | // have to do this analysis on the source type because we can't depend on |
| 1937 | // unions being lowered a specific way etc. |
| 1938 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | babaf31 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1939 | IRType->isIntegerTy(32) || |
| 1940 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 1941 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 1942 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1943 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1944 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 1945 | SourceOffset*8+64, getContext())) |
| 1946 | return IRType; |
| 1947 | } |
| 1948 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1949 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1950 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1951 | // If this is a struct, recurse into the field at the specified offset. |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1952 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1953 | if (IROffset < SL->getSizeInBytes()) { |
| 1954 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 1955 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1956 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1957 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 1958 | SourceTy, SourceOffset); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1959 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1960 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1961 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1962 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1963 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1964 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1965 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1966 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 1967 | SourceOffset); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1968 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1969 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1970 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 1971 | // integer register that isn't too big to fit the rest of the struct. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1972 | unsigned TySizeInBytes = |
| 1973 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1975 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1976 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1977 | // It is always safe to classify this as an integer type up to i64 that |
| 1978 | // isn't larger than the structure. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1979 | return llvm::IntegerType::get(getVMContext(), |
| 1980 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1983 | |
| 1984 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 1985 | /// be used as elements of a two register pair to pass or return, return a |
| 1986 | /// first class aggregate to represent them. For example, if the low part of |
| 1987 | /// a by-value argument should be passed as i32* and the high part as float, |
| 1988 | /// return {i32*, float}. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1989 | static llvm::Type * |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1990 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1991 | const llvm::DataLayout &TD) { |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1992 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 1993 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 1994 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 1995 | // the second element at offset 8. Check for this: |
| 1996 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 1997 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1998 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1999 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2000 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2001 | // To handle this, we have to increase the size of the low part so that the |
| 2002 | // second element will start at an 8 byte offset. We can't increase the size |
| 2003 | // of the second element because it might make us access off the end of the |
| 2004 | // struct. |
| 2005 | if (HiStart != 8) { |
| 2006 | // There are only two sorts of types the ABI generation code can produce for |
| 2007 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2008 | // Promote these to a larger type. |
| 2009 | if (Lo->isFloatTy()) |
| 2010 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2011 | else { |
| 2012 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2013 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2014 | } |
| 2015 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2016 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2017 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2018 | |
| 2019 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2020 | // Verify that the second element is at an 8-byte offset. |
| 2021 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2022 | "Invalid x86-64 argument pair!"); |
| 2023 | return Result; |
| 2024 | } |
| 2025 | |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2026 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2027 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2028 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2029 | // classification algorithm. |
| 2030 | X86_64ABIInfo::Class Lo, Hi; |
| 2031 | classify(RetTy, 0, Lo, Hi); |
| 2032 | |
| 2033 | // Check some invariants. |
| 2034 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2035 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2036 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2037 | llvm::Type *ResType = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2038 | switch (Lo) { |
| 2039 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2040 | if (Hi == NoClass) |
| 2041 | return ABIArgInfo::getIgnore(); |
| 2042 | // If the low part is just padding, it takes no register, leave ResType |
| 2043 | // null. |
| 2044 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2045 | "Unknown missing lo part"); |
| 2046 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2047 | |
| 2048 | case SSEUp: |
| 2049 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2050 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2051 | |
| 2052 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2053 | // hidden argument. |
| 2054 | case Memory: |
| 2055 | return getIndirectReturnResult(RetTy); |
| 2056 | |
| 2057 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2058 | // available register of the sequence %rax, %rdx is used. |
| 2059 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2060 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2061 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2062 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2063 | // so that the parameter gets the right LLVM IR attributes. |
| 2064 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2065 | // Treat an enum type as its underlying type. |
| 2066 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2067 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2068 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2069 | if (RetTy->isIntegralOrEnumerationType() && |
| 2070 | RetTy->isPromotableIntegerType()) |
| 2071 | return ABIArgInfo::getExtend(); |
| 2072 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2073 | break; |
| 2074 | |
| 2075 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2076 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2077 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2078 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2079 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2080 | |
| 2081 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2082 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2083 | case X87: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2084 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2085 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2086 | |
| 2087 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2088 | // part of the value is returned in %st0 and the imaginary part in |
| 2089 | // %st1. |
| 2090 | case ComplexX87: |
| 2091 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2092 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2093 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2094 | NULL); |
| 2095 | break; |
| 2096 | } |
| 2097 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2098 | llvm::Type *HighPart = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2099 | switch (Hi) { |
| 2100 | // Memory was handled previously and X87 should |
| 2101 | // never occur as a hi class. |
| 2102 | case Memory: |
| 2103 | case X87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2104 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2105 | |
| 2106 | case ComplexX87: // Previously handled. |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2107 | case NoClass: |
| 2108 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2109 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2110 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2111 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2112 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2113 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2114 | break; |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2115 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2116 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2117 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2118 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2119 | break; |
| 2120 | |
| 2121 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2122 | // is passed in the next available eightbyte chunk if the last used |
| 2123 | // vector register. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2124 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2125 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2126 | case SSEUp: |
| 2127 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2128 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2129 | break; |
| 2130 | |
| 2131 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2132 | // returned together with the previous X87 value in %st0. |
| 2133 | case X87Up: |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2134 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2135 | // anything. However, in some cases with unions it may not be |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2136 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2137 | // extra bits in an SSE reg. |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2138 | if (Lo != X87) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2139 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2140 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2141 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2142 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2143 | break; |
| 2144 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2145 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2146 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2147 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2148 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2149 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2150 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2151 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2152 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2155 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
| 2156 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE) |
| 2157 | const |
| 2158 | { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2159 | X86_64ABIInfo::Class Lo, Hi; |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2160 | classify(Ty, 0, Lo, Hi); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2161 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2162 | // Check some invariants. |
| 2163 | // FIXME: Enforce these by construction. |
| 2164 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2165 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2166 | |
| 2167 | neededInt = 0; |
| 2168 | neededSSE = 0; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2169 | llvm::Type *ResType = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2170 | switch (Lo) { |
| 2171 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2172 | if (Hi == NoClass) |
| 2173 | return ABIArgInfo::getIgnore(); |
| 2174 | // If the low part is just padding, it takes no register, leave ResType |
| 2175 | // null. |
| 2176 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2177 | "Unknown missing lo part"); |
| 2178 | break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2179 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2180 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2181 | // on the stack. |
| 2182 | case Memory: |
| 2183 | |
| 2184 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2185 | // COMPLEX_X87, it is passed in memory. |
| 2186 | case X87: |
| 2187 | case ComplexX87: |
Eli Friedman | ded137f | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2188 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 2189 | ++neededInt; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2190 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2191 | |
| 2192 | case SSEUp: |
| 2193 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2194 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2195 | |
| 2196 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2197 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2198 | // and %r9 is used. |
| 2199 | case Integer: |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2200 | ++neededInt; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2201 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2202 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2203 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2204 | |
| 2205 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2206 | // so that the parameter gets the right LLVM IR attributes. |
| 2207 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2208 | // Treat an enum type as its underlying type. |
| 2209 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2210 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2211 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2212 | if (Ty->isIntegralOrEnumerationType() && |
| 2213 | Ty->isPromotableIntegerType()) |
| 2214 | return ABIArgInfo::getExtend(); |
| 2215 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2216 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2217 | break; |
| 2218 | |
| 2219 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2220 | // available SSE register is used, the registers are taken in the |
| 2221 | // order from %xmm0 to %xmm7. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2222 | case SSE: { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2223 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 14508ff | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2224 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2225 | ++neededSSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2226 | break; |
| 2227 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2228 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2230 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2231 | switch (Hi) { |
| 2232 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2233 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2234 | // which is passed in memory. |
| 2235 | case Memory: |
| 2236 | case X87: |
| 2237 | case ComplexX87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2238 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2239 | |
| 2240 | case NoClass: break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2241 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2242 | case Integer: |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2243 | ++neededInt; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2244 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2245 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2246 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2247 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2248 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2249 | break; |
| 2250 | |
| 2251 | // X87Up generally doesn't occur here (long double is passed in |
| 2252 | // memory), except in situations involving unions. |
| 2253 | case X87Up: |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2254 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2255 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2256 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2257 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2258 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2259 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2260 | ++neededSSE; |
| 2261 | break; |
| 2262 | |
| 2263 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2264 | // eightbyte is passed in the upper half of the last used SSE |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2265 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2266 | case SSEUp: |
Chris Lattner | ab5722e | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2267 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2268 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2269 | break; |
| 2270 | } |
| 2271 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2272 | // If a high part was specified, merge it together with the low part. It is |
| 2273 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2274 | // first class struct aggregate with the high and low part: {low, high} |
| 2275 | if (HighPart) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2276 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2277 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2278 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2281 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2282 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2283 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2284 | |
| 2285 | // Keep track of the number of assigned registers. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2286 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2287 | |
| 2288 | // If the return value is indirect, then the hidden argument is consuming one |
| 2289 | // integer register. |
| 2290 | if (FI.getReturnInfo().isIndirect()) |
| 2291 | --freeIntRegs; |
| 2292 | |
| 2293 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2294 | // get assigned (in left-to-right order) for passing as follows... |
| 2295 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2296 | it != ie; ++it) { |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2297 | unsigned neededInt, neededSSE; |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2298 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
| 2299 | neededSSE); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2300 | |
| 2301 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2302 | // eightbyte of an argument, the whole argument is passed on the |
| 2303 | // stack. If registers have already been assigned for some |
| 2304 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2305 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2306 | freeIntRegs -= neededInt; |
| 2307 | freeSSERegs -= neededSSE; |
| 2308 | } else { |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2309 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2315 | QualType Ty, |
| 2316 | CodeGenFunction &CGF) { |
| 2317 | llvm::Value *overflow_arg_area_p = |
| 2318 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2319 | llvm::Value *overflow_arg_area = |
| 2320 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2321 | |
| 2322 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2323 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2324 | // It isn't stated explicitly in the standard, but in practice we use |
| 2325 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2326 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2327 | if (Align > 8) { |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2328 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2329 | llvm::Value *Offset = |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2330 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2331 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2332 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2333 | CGF.Int64Ty); |
Eli Friedman | 8d2fe42 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2334 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2335 | overflow_arg_area = |
| 2336 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2337 | overflow_arg_area->getType(), |
| 2338 | "overflow_arg_area.align"); |
| 2339 | } |
| 2340 | |
| 2341 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2342 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2343 | llvm::Value *Res = |
| 2344 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2345 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2346 | |
| 2347 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2348 | // l->overflow_arg_area + sizeof(type). |
| 2349 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2350 | // an 8 byte boundary. |
| 2351 | |
| 2352 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2353 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2354 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2355 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2356 | "overflow_arg_area.next"); |
| 2357 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2358 | |
| 2359 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2360 | return Res; |
| 2361 | } |
| 2362 | |
| 2363 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2364 | CodeGenFunction &CGF) const { |
| 2365 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2366 | // struct { |
| 2367 | // i32 gp_offset; |
| 2368 | // i32 fp_offset; |
| 2369 | // i8* overflow_arg_area; |
| 2370 | // i8* reg_save_area; |
| 2371 | // }; |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2372 | unsigned neededInt, neededSSE; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2373 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2374 | Ty = CGF.getContext().getCanonicalType(Ty); |
Daniel Dunbar | edfac03 | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2375 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2376 | |
| 2377 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2378 | // in the registers. If not go to step 7. |
| 2379 | if (!neededInt && !neededSSE) |
| 2380 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2381 | |
| 2382 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2383 | // general purpose registers needed to pass type and num_fp to hold |
| 2384 | // the number of floating point registers needed. |
| 2385 | |
| 2386 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2387 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2388 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2389 | // |
| 2390 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2391 | // register save space). |
| 2392 | |
| 2393 | llvm::Value *InRegs = 0; |
| 2394 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2395 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2396 | if (neededInt) { |
| 2397 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2398 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2399 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2400 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | if (neededSSE) { |
| 2404 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2405 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2406 | llvm::Value *FitsInFP = |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2407 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2408 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2409 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2410 | } |
| 2411 | |
| 2412 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2413 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2414 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2415 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2416 | |
| 2417 | // Emit code to load the value if it was passed in registers. |
| 2418 | |
| 2419 | CGF.EmitBlock(InRegBlock); |
| 2420 | |
| 2421 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2422 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2423 | // copying to a temporary location in case the parameter is passed |
| 2424 | // in different register classes or requires an alignment greater |
| 2425 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2426 | // |
| 2427 | // FIXME: This really results in shameful code when we end up needing to |
| 2428 | // collect arguments from different places; often what should result in a |
| 2429 | // simple assembling of a structure from scattered addresses has many more |
| 2430 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2431 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2432 | llvm::Value *RegAddr = |
| 2433 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2434 | "reg_save_area"); |
| 2435 | if (neededInt && neededSSE) { |
| 2436 | // FIXME: Cleanup. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2437 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2438 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2439 | llvm::Value *Tmp = CGF.CreateTempAlloca(ST); |
| 2440 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2441 | llvm::Type *TyLo = ST->getElementType(0); |
| 2442 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | a8b7a7d | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2443 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2444 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2445 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2446 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2447 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2448 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2449 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2450 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2451 | llvm::Value *V = |
| 2452 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2453 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2454 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2455 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2456 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2457 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2458 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2459 | } else if (neededInt) { |
| 2460 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2461 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2462 | llvm::PointerType::getUnqual(LTy)); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2463 | } else if (neededSSE == 1) { |
| 2464 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2465 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2466 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2467 | } else { |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2468 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2469 | // SSE registers are spaced 16 bytes apart in the register save |
| 2470 | // area, we need to collect the two eightbytes together. |
| 2471 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2472 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2473 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2474 | llvm::Type *DblPtrTy = |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2475 | llvm::PointerType::getUnqual(DoubleTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2476 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2477 | DoubleTy, NULL); |
| 2478 | llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); |
| 2479 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2480 | DblPtrTy)); |
| 2481 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2482 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2483 | DblPtrTy)); |
| 2484 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2485 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2486 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2490 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2491 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2492 | if (neededInt) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2493 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2494 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2495 | gp_offset_p); |
| 2496 | } |
| 2497 | if (neededSSE) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2498 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2499 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2500 | fp_offset_p); |
| 2501 | } |
| 2502 | CGF.EmitBranch(ContBlock); |
| 2503 | |
| 2504 | // Emit code to load the value if it was passed in memory. |
| 2505 | |
| 2506 | CGF.EmitBlock(InMemBlock); |
| 2507 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2508 | |
| 2509 | // Return the appropriate result. |
| 2510 | |
| 2511 | CGF.EmitBlock(ContBlock); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2512 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2513 | "vaarg.addr"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2514 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2515 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2516 | return ResAddr; |
| 2517 | } |
| 2518 | |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2519 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const { |
| 2520 | |
| 2521 | if (Ty->isVoidType()) |
| 2522 | return ABIArgInfo::getIgnore(); |
| 2523 | |
| 2524 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2525 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2526 | |
| 2527 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2528 | |
| 2529 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
NAKAMURA Takumi | ff8be0e | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2530 | if (hasNonTrivialDestructorOrCopyConstructor(RT) || |
| 2531 | RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2532 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2533 | |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2534 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
| 2535 | if (Size == 128 && |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 2536 | getContext().getTargetInfo().getTriple().getOS() |
| 2537 | == llvm::Triple::MinGW32) |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2538 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2539 | Size)); |
| 2540 | |
| 2541 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2542 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 2543 | if (Size <= 64 && |
NAKAMURA Takumi | ff8be0e | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2544 | (Size & (Size - 1)) == 0) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2545 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2546 | Size)); |
| 2547 | |
| 2548 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2549 | } |
| 2550 | |
| 2551 | if (Ty->isPromotableIntegerType()) |
| 2552 | return ABIArgInfo::getExtend(); |
| 2553 | |
| 2554 | return ABIArgInfo::getDirect(); |
| 2555 | } |
| 2556 | |
| 2557 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2558 | |
| 2559 | QualType RetTy = FI.getReturnType(); |
| 2560 | FI.getReturnInfo() = classify(RetTy); |
| 2561 | |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2562 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2563 | it != ie; ++it) |
| 2564 | it->info = classify(it->type); |
| 2565 | } |
| 2566 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2567 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2568 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2569 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2570 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2571 | CGBuilderTy &Builder = CGF.Builder; |
| 2572 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2573 | "ap"); |
| 2574 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2575 | llvm::Type *PTy = |
| 2576 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2577 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2578 | |
| 2579 | uint64_t Offset = |
| 2580 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2581 | llvm::Value *NextAddr = |
| 2582 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2583 | "ap.next"); |
| 2584 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2585 | |
| 2586 | return AddrTyped; |
| 2587 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2588 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2589 | namespace { |
| 2590 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2591 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2592 | public: |
| 2593 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2594 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
| 2595 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 2596 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2597 | CodeGenFunction &CGF) const; |
| 2598 | private: |
| 2599 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2600 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2601 | }; |
| 2602 | |
| 2603 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2604 | public: |
| 2605 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2606 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} |
| 2607 | }; |
| 2608 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2611 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2612 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2613 | PInfo.computeInfo(FI); |
| 2614 | else |
| 2615 | NInfo.computeInfo(FI); |
| 2616 | } |
| 2617 | |
| 2618 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2619 | CodeGenFunction &CGF) const { |
| 2620 | // Always use the native convention; calling pnacl-style varargs functions |
| 2621 | // is unuspported. |
| 2622 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2623 | } |
| 2624 | |
| 2625 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2626 | // PowerPC-32 |
| 2627 | |
| 2628 | namespace { |
| 2629 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2630 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2631 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2632 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2633 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2634 | // This is recovered from gcc output. |
| 2635 | return 1; // r1 is the dedicated stack pointer |
| 2636 | } |
| 2637 | |
| 2638 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2639 | llvm::Value *Address) const; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2640 | }; |
| 2641 | |
| 2642 | } |
| 2643 | |
| 2644 | bool |
| 2645 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2646 | llvm::Value *Address) const { |
| 2647 | // This is calculated from the LLVM and GCC tables and verified |
| 2648 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2649 | |
| 2650 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2651 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2652 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2653 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2654 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2655 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2656 | |
| 2657 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2658 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2659 | |
| 2660 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2661 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2662 | |
| 2663 | // 64-76 are various 4-byte special-purpose registers: |
| 2664 | // 64: mq |
| 2665 | // 65: lr |
| 2666 | // 66: ctr |
| 2667 | // 67: ap |
| 2668 | // 68-75 cr0-7 |
| 2669 | // 76: xer |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2670 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2671 | |
| 2672 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2673 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2674 | |
| 2675 | // 109: vrsave |
| 2676 | // 110: vscr |
| 2677 | // 111: spe_acc |
| 2678 | // 112: spefscr |
| 2679 | // 113: sfp |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2680 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2681 | |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2682 | return false; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2685 | // PowerPC-64 |
| 2686 | |
| 2687 | namespace { |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2688 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 2689 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
| 2690 | |
| 2691 | public: |
| 2692 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 2693 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2694 | bool isPromotableTypeForABI(QualType Ty) const; |
| 2695 | |
| 2696 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2697 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2698 | |
Bill Schmidt | b1f5fe0 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2699 | // TODO: We can add more logic to computeInfo to improve performance. |
| 2700 | // Example: For aggregate arguments that fit in a register, we could |
| 2701 | // use getDirectInReg (as is done below for structs containing a single |
| 2702 | // floating-point value) to avoid pushing them to memory on function |
| 2703 | // entry. This would require changing the logic in PPCISelLowering |
| 2704 | // when lowering the parameters in the caller and args in the callee. |
| 2705 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 2706 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2707 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2708 | it != ie; ++it) { |
| 2709 | // We rely on the default argument classification for the most part. |
| 2710 | // One exception: An aggregate containing a single floating-point |
| 2711 | // item must be passed in a register if one is available. |
| 2712 | const Type *T = isSingleElementStruct(it->type, getContext()); |
| 2713 | if (T) { |
| 2714 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
| 2715 | if (BT && BT->isFloatingPoint()) { |
| 2716 | QualType QT(T, 0); |
| 2717 | it->info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
| 2718 | continue; |
| 2719 | } |
| 2720 | } |
| 2721 | it->info = classifyArgumentType(it->type); |
| 2722 | } |
| 2723 | } |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2724 | |
| 2725 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, |
| 2726 | QualType Ty, |
| 2727 | CodeGenFunction &CGF) const; |
| 2728 | }; |
| 2729 | |
| 2730 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2731 | public: |
| 2732 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2733 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {} |
| 2734 | |
| 2735 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2736 | // This is recovered from gcc output. |
| 2737 | return 1; // r1 is the dedicated stack pointer |
| 2738 | } |
| 2739 | |
| 2740 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2741 | llvm::Value *Address) const; |
| 2742 | }; |
| 2743 | |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2744 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2745 | public: |
| 2746 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 2747 | |
| 2748 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2749 | // This is recovered from gcc output. |
| 2750 | return 1; // r1 is the dedicated stack pointer |
| 2751 | } |
| 2752 | |
| 2753 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2754 | llvm::Value *Address) const; |
| 2755 | }; |
| 2756 | |
| 2757 | } |
| 2758 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2759 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 2760 | // extended to 64 bits. |
| 2761 | bool |
| 2762 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 2763 | // Treat an enum type as its underlying type. |
| 2764 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2765 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2766 | |
| 2767 | // Promotable integer types are required to be promoted by the ABI. |
| 2768 | if (Ty->isPromotableIntegerType()) |
| 2769 | return true; |
| 2770 | |
| 2771 | // In addition to the usual promotable integer types, we also need to |
| 2772 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 2773 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2774 | switch (BT->getKind()) { |
| 2775 | case BuiltinType::Int: |
| 2776 | case BuiltinType::UInt: |
| 2777 | return true; |
| 2778 | default: |
| 2779 | break; |
| 2780 | } |
| 2781 | |
| 2782 | return false; |
| 2783 | } |
| 2784 | |
| 2785 | ABIArgInfo |
| 2786 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | c9715fc | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 2787 | if (Ty->isAnyComplexType()) |
| 2788 | return ABIArgInfo::getDirect(); |
| 2789 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2790 | if (isAggregateTypeForABI(Ty)) { |
| 2791 | // Records with non trivial destructors/constructors should not be passed |
| 2792 | // by value. |
| 2793 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 2794 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2795 | |
| 2796 | return ABIArgInfo::getIndirect(0); |
| 2797 | } |
| 2798 | |
| 2799 | return (isPromotableTypeForABI(Ty) ? |
| 2800 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2801 | } |
| 2802 | |
| 2803 | ABIArgInfo |
| 2804 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 2805 | if (RetTy->isVoidType()) |
| 2806 | return ABIArgInfo::getIgnore(); |
| 2807 | |
Bill Schmidt | 9e6111a | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 2808 | if (RetTy->isAnyComplexType()) |
| 2809 | return ABIArgInfo::getDirect(); |
| 2810 | |
Ulrich Weigand | 71c0dcc | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2811 | if (isAggregateTypeForABI(RetTy)) |
| 2812 | return ABIArgInfo::getIndirect(0); |
| 2813 | |
| 2814 | return (isPromotableTypeForABI(RetTy) ? |
| 2815 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2816 | } |
| 2817 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2818 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 2819 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 2820 | QualType Ty, |
| 2821 | CodeGenFunction &CGF) const { |
| 2822 | llvm::Type *BP = CGF.Int8PtrTy; |
| 2823 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 2824 | |
| 2825 | CGBuilderTy &Builder = CGF.Builder; |
| 2826 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 2827 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2828 | |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2829 | // Update the va_list pointer. The pointer should be bumped by the |
| 2830 | // size of the object. We can trust getTypeSize() except for a complex |
| 2831 | // type whose base type is smaller than a doubleword. For these, the |
| 2832 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2833 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2834 | QualType BaseTy; |
| 2835 | unsigned CplxBaseSize = 0; |
| 2836 | |
| 2837 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 2838 | BaseTy = CTy->getElementType(); |
| 2839 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 2840 | if (CplxBaseSize < 8) |
| 2841 | SizeInBytes = 16; |
| 2842 | } |
| 2843 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2844 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 2845 | llvm::Value *NextAddr = |
| 2846 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 2847 | "ap.next"); |
| 2848 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2849 | |
Bill Schmidt | 19f8e85 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 2850 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 2851 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 2852 | // in separate doublewords. However, Clang expects us to produce a |
| 2853 | // pointer to a structure with the two parts packed tightly. So generate |
| 2854 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 2855 | // and store them to a temporary structure. |
| 2856 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 2857 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 2858 | llvm::Value *ImagAddr = RealAddr; |
| 2859 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 2860 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 2861 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 2862 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 2863 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 2864 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 2865 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 2866 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 2867 | "vacplx"); |
| 2868 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 2869 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 2870 | Builder.CreateStore(Real, RealPtr, false); |
| 2871 | Builder.CreateStore(Imag, ImagPtr, false); |
| 2872 | return Ptr; |
| 2873 | } |
| 2874 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2875 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 2876 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 2877 | // correct offset. |
| 2878 | if (SizeInBytes < 8) { |
| 2879 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 2880 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 2881 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 2882 | } |
| 2883 | |
| 2884 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2885 | return Builder.CreateBitCast(Addr, PTy); |
| 2886 | } |
| 2887 | |
| 2888 | static bool |
| 2889 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2890 | llvm::Value *Address) { |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2891 | // This is calculated from the LLVM and GCC tables and verified |
| 2892 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2893 | |
| 2894 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2895 | |
| 2896 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 2897 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2898 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2899 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2900 | |
| 2901 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 2902 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 2903 | |
| 2904 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 2905 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 2906 | |
| 2907 | // 64-76 are various 4-byte special-purpose registers: |
| 2908 | // 64: mq |
| 2909 | // 65: lr |
| 2910 | // 66: ctr |
| 2911 | // 67: ap |
| 2912 | // 68-75 cr0-7 |
| 2913 | // 76: xer |
| 2914 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 2915 | |
| 2916 | // 77-108: v0-31, the 16-byte vector registers |
| 2917 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 2918 | |
| 2919 | // 109: vrsave |
| 2920 | // 110: vscr |
| 2921 | // 111: spe_acc |
| 2922 | // 112: spefscr |
| 2923 | // 113: sfp |
| 2924 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 2925 | |
| 2926 | return false; |
| 2927 | } |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2928 | |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2929 | bool |
| 2930 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 2931 | CodeGen::CodeGenFunction &CGF, |
| 2932 | llvm::Value *Address) const { |
| 2933 | |
| 2934 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 2935 | } |
| 2936 | |
| 2937 | bool |
| 2938 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2939 | llvm::Value *Address) const { |
| 2940 | |
| 2941 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 2942 | } |
| 2943 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2944 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2945 | // ARM ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2946 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2947 | |
| 2948 | namespace { |
| 2949 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2950 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2951 | public: |
| 2952 | enum ABIKind { |
| 2953 | APCS = 0, |
| 2954 | AAPCS = 1, |
| 2955 | AAPCS_VFP |
| 2956 | }; |
| 2957 | |
| 2958 | private: |
| 2959 | ABIKind Kind; |
| 2960 | |
| 2961 | public: |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2962 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
| 2963 | setRuntimeCC(); |
| 2964 | } |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2965 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2966 | bool isEABI() const { |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 2967 | StringRef Env = |
| 2968 | getContext().getTargetInfo().getTriple().getEnvironmentName(); |
Logan Chien | 94a7142 | 2012-09-02 09:30:11 +0000 | [diff] [blame] | 2969 | return (Env == "gnueabi" || Env == "eabi" || |
| 2970 | Env == "android" || Env == "androideabi"); |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2973 | private: |
| 2974 | ABIKind getABIKind() const { return Kind; } |
| 2975 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2976 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 2977 | ABIArgInfo classifyArgumentType(QualType RetTy, int *VFPRegs, |
| 2978 | unsigned &AllocatedVFP, |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 2979 | bool &IsHA) const; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 2980 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2981 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2982 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2983 | |
| 2984 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2985 | CodeGenFunction &CGF) const; |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2986 | |
| 2987 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 2988 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 2989 | void setRuntimeCC(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2990 | }; |
| 2991 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2992 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2993 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2994 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 2995 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2996 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2997 | const ARMABIInfo &getABIInfo() const { |
| 2998 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2999 | } |
| 3000 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3001 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 3002 | return 13; |
| 3003 | } |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3004 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3005 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3006 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 3007 | } |
| 3008 | |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3009 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3010 | llvm::Value *Address) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3011 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3012 | |
| 3013 | // 0-15 are the 16 integer registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3014 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3015 | return false; |
| 3016 | } |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3017 | |
| 3018 | unsigned getSizeOfUnwindException() const { |
| 3019 | if (getABIInfo().isEABI()) return 88; |
| 3020 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 3021 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3022 | }; |
| 3023 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3026 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3027 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3028 | // VFP registers allocated so far. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3029 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3030 | // VFP registers of the appropriate type unallocated then the argument is |
| 3031 | // allocated to the lowest-numbered sequence of such registers. |
| 3032 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3033 | // unallocated are marked as unavailable. |
| 3034 | unsigned AllocatedVFP = 0; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3035 | int VFPRegs[16] = { 0 }; |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3036 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3037 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3038 | it != ie; ++it) { |
| 3039 | unsigned PreAllocation = AllocatedVFP; |
| 3040 | bool IsHA = false; |
| 3041 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 3042 | // s0-s15 (d0-d7) for passing arguments. |
| 3043 | const unsigned NumVFPs = 16; |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3044 | it->info = classifyArgumentType(it->type, VFPRegs, AllocatedVFP, IsHA); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3045 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3046 | // that are unallocated are marked as unavailable. To achieve this, we add |
| 3047 | // padding of (NumVFPs - PreAllocation) floats. |
| 3048 | if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { |
| 3049 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3050 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); |
| 3051 | it->info = ABIArgInfo::getExpandWithPadding(false, PaddingTy); |
| 3052 | } |
| 3053 | } |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3054 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3055 | // Always honor user-specified calling convention. |
| 3056 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 3057 | return; |
| 3058 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3059 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 3060 | if (cc != llvm::CallingConv::C) |
| 3061 | FI.setEffectiveCallingConvention(cc); |
| 3062 | } |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 3063 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3064 | /// Return the default calling convention that LLVM will use. |
| 3065 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 3066 | // The default calling convention that LLVM will infer. |
| 3067 | if (getContext().getTargetInfo().getTriple().getEnvironmentName()=="gnueabihf") |
| 3068 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 3069 | else if (isEABI()) |
| 3070 | return llvm::CallingConv::ARM_AAPCS; |
| 3071 | else |
| 3072 | return llvm::CallingConv::ARM_APCS; |
| 3073 | } |
| 3074 | |
| 3075 | /// Return the calling convention that our ABI would like us to use |
| 3076 | /// as the C calling convention. |
| 3077 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3078 | switch (getABIKind()) { |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3079 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 3080 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 3081 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3082 | } |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3083 | llvm_unreachable("bad ABI kind"); |
| 3084 | } |
| 3085 | |
| 3086 | void ARMABIInfo::setRuntimeCC() { |
| 3087 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 3088 | |
| 3089 | // Don't muddy up the IR with a ton of explicit annotations if |
| 3090 | // they'd just match what LLVM will infer from the triple. |
| 3091 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 3092 | if (abiCC != getLLVMDefaultCC()) |
| 3093 | RuntimeCC = abiCC; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3096 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 3097 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 3098 | /// contained in the type is returned through it; this is used for the |
| 3099 | /// recursive calls that check aggregate component types. |
| 3100 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3101 | ASTContext &Context, |
| 3102 | uint64_t *HAMembers = 0) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3103 | uint64_t Members = 0; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3104 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3105 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 3106 | return false; |
| 3107 | Members *= AT->getSize().getZExtValue(); |
| 3108 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3109 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3110 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3111 | return false; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3112 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3113 | Members = 0; |
| 3114 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3115 | i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3116 | const FieldDecl *FD = *i; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3117 | uint64_t FldMembers; |
| 3118 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 3119 | return false; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3120 | |
| 3121 | Members = (RD->isUnion() ? |
| 3122 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3123 | } |
| 3124 | } else { |
| 3125 | Members = 1; |
| 3126 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3127 | Members = 2; |
| 3128 | Ty = CT->getElementType(); |
| 3129 | } |
| 3130 | |
| 3131 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 3132 | // double, or 64-bit or 128-bit vectors. |
| 3133 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3134 | if (BT->getKind() != BuiltinType::Float && |
Tim Northover | adfa45f | 2012-07-20 22:29:29 +0000 | [diff] [blame] | 3135 | BT->getKind() != BuiltinType::Double && |
| 3136 | BT->getKind() != BuiltinType::LongDouble) |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3137 | return false; |
| 3138 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3139 | unsigned VecSize = Context.getTypeSize(VT); |
| 3140 | if (VecSize != 64 && VecSize != 128) |
| 3141 | return false; |
| 3142 | } else { |
| 3143 | return false; |
| 3144 | } |
| 3145 | |
| 3146 | // The base type must be the same for all members. Vector types of the |
| 3147 | // same total size are treated as being equivalent here. |
| 3148 | const Type *TyPtr = Ty.getTypePtr(); |
| 3149 | if (!Base) |
| 3150 | Base = TyPtr; |
| 3151 | if (Base != TyPtr && |
| 3152 | (!Base->isVectorType() || !TyPtr->isVectorType() || |
| 3153 | Context.getTypeSize(Base) != Context.getTypeSize(TyPtr))) |
| 3154 | return false; |
| 3155 | } |
| 3156 | |
| 3157 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 3158 | if (HAMembers) |
| 3159 | *HAMembers = Members; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3160 | |
| 3161 | return (Members > 0 && Members <= 4); |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3164 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 3165 | /// number of VFP registers (unit is S register) requested. |
| 3166 | static void markAllocatedVFPs(int *VFPRegs, unsigned &AllocatedVFP, |
| 3167 | unsigned Alignment, |
| 3168 | unsigned NumRequired) { |
| 3169 | // Early Exit. |
| 3170 | if (AllocatedVFP >= 16) |
| 3171 | return; |
| 3172 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3173 | // VFP registers of the appropriate type unallocated then the argument is |
| 3174 | // allocated to the lowest-numbered sequence of such registers. |
| 3175 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 3176 | bool FoundSlot = true; |
| 3177 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3178 | if (J >= 16 || VFPRegs[J]) { |
| 3179 | FoundSlot = false; |
| 3180 | break; |
| 3181 | } |
| 3182 | if (FoundSlot) { |
| 3183 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 3184 | VFPRegs[J] = 1; |
| 3185 | AllocatedVFP += NumRequired; |
| 3186 | return; |
| 3187 | } |
| 3188 | } |
| 3189 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3190 | // unallocated are marked as unavailable. |
| 3191 | for (unsigned I = 0; I < 16; I++) |
| 3192 | VFPRegs[I] = 1; |
| 3193 | AllocatedVFP = 17; // We do not have enough VFP registers. |
| 3194 | } |
| 3195 | |
| 3196 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, int *VFPRegs, |
| 3197 | unsigned &AllocatedVFP, |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3198 | bool &IsHA) const { |
| 3199 | // We update number of allocated VFPs according to |
| 3200 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 3201 | // A single-precision floating-point type (including promoted |
| 3202 | // half-precision types); A double-precision floating-point type; |
| 3203 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 3204 | // with a Base Type of a single- or double-precision floating-point type, |
| 3205 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 3206 | // to four Elements. |
| 3207 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3208 | // Handle illegal vector types here. |
| 3209 | if (isIllegalVectorType(Ty)) { |
| 3210 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3211 | if (Size <= 32) { |
| 3212 | llvm::Type *ResType = |
| 3213 | llvm::Type::getInt32Ty(getVMContext()); |
| 3214 | return ABIArgInfo::getDirect(ResType); |
| 3215 | } |
| 3216 | if (Size == 64) { |
| 3217 | llvm::Type *ResType = llvm::VectorType::get( |
| 3218 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3219 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3220 | return ABIArgInfo::getDirect(ResType); |
| 3221 | } |
| 3222 | if (Size == 128) { |
| 3223 | llvm::Type *ResType = llvm::VectorType::get( |
| 3224 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3225 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 4, 4); |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3226 | return ABIArgInfo::getDirect(ResType); |
| 3227 | } |
| 3228 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3229 | } |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3230 | // Update VFPRegs for legal vector types. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3231 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3232 | uint64_t Size = getContext().getTypeSize(VT); |
| 3233 | // Size of a legal vector should be power of 2 and above 64. |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3234 | markAllocatedVFPs(VFPRegs, AllocatedVFP, Size >= 128 ? 4 : 2, Size / 32); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3235 | } |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3236 | // Update VFPRegs for floating point types. |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3237 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3238 | if (BT->getKind() == BuiltinType::Half || |
| 3239 | BT->getKind() == BuiltinType::Float) |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3240 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, 1); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3241 | if (BT->getKind() == BuiltinType::Double || |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3242 | BT->getKind() == BuiltinType::LongDouble) |
| 3243 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, 2); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3244 | } |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3245 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3246 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3247 | // Treat an enum type as its underlying type. |
| 3248 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3249 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3250 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3251 | return (Ty->isPromotableIntegerType() ? |
| 3252 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3253 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3254 | |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3255 | // Ignore empty records. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3256 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 3257 | return ABIArgInfo::getIgnore(); |
| 3258 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3259 | // Structures with either a non-trivial destructor or a non-trivial |
| 3260 | // copy constructor are always indirect. |
| 3261 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 3262 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3263 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3264 | if (getABIKind() == ARMABIInfo::AAPCS_VFP) { |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3265 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 3266 | // into VFP registers. |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3267 | const Type *Base = 0; |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3268 | uint64_t Members = 0; |
| 3269 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3270 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3271 | // Base can be a floating-point or a vector. |
| 3272 | if (Base->isVectorType()) { |
| 3273 | // ElementSize is in number of floats. |
| 3274 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3275 | markAllocatedVFPs(VFPRegs, AllocatedVFP, ElementSize, |
| 3276 | Members * ElementSize); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3277 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3278 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 1, Members); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3279 | else { |
| 3280 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 3281 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Manman Ren | 710c517 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3282 | markAllocatedVFPs(VFPRegs, AllocatedVFP, 2, Members * 2); |
Manman Ren | b3fa55f | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3283 | } |
| 3284 | IsHA = true; |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3285 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3286 | } |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Manman Ren | 634b3d2 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 3289 | // Support byval for ARM. |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3290 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 3291 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 3292 | // than ABI alignment. |
Manman Ren | fd1ba91 | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 3293 | uint64_t ABIAlign = 4; |
| 3294 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 3295 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3296 | getABIKind() == ARMABIInfo::AAPCS) |
| 3297 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 885ad69 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 3298 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
| 3299 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true, |
Manman Ren | cb489dd | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 3300 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Daniel Dunbar | 8aa87c7 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 3303 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3304 | llvm::Type* ElemTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3305 | unsigned SizeRegs; |
Eli Friedman | 79f3098 | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 3306 | // FIXME: Try to match the types of the arguments more accurately where |
| 3307 | // we can. |
| 3308 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 53fc1a6 | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 3309 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 3310 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3311 | } else { |
Manman Ren | 78eb76e | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 3312 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3313 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | 67d097e | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 3314 | } |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3315 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3316 | llvm::Type *STy = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3317 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 3318 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3321 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3322 | llvm::LLVMContext &VMContext) { |
| 3323 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 3324 | // is called integer-like if its size is less than or equal to one word, and |
| 3325 | // the offset of each of its addressable sub-fields is zero. |
| 3326 | |
| 3327 | uint64_t Size = Context.getTypeSize(Ty); |
| 3328 | |
| 3329 | // Check that the type fits in a word. |
| 3330 | if (Size > 32) |
| 3331 | return false; |
| 3332 | |
| 3333 | // FIXME: Handle vector types! |
| 3334 | if (Ty->isVectorType()) |
| 3335 | return false; |
| 3336 | |
Daniel Dunbar | b0d5819 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 3337 | // Float types are never treated as "integer like". |
| 3338 | if (Ty->isRealFloatingType()) |
| 3339 | return false; |
| 3340 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3341 | // If this is a builtin or pointer type then it is ok. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3342 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3343 | return true; |
| 3344 | |
Daniel Dunbar | 4581581 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 3345 | // Small complex integer types are "integer like". |
| 3346 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 3347 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3348 | |
| 3349 | // Single element and zero sized arrays should be allowed, by the definition |
| 3350 | // above, but they are not. |
| 3351 | |
| 3352 | // Otherwise, it must be a record type. |
| 3353 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3354 | if (!RT) return false; |
| 3355 | |
| 3356 | // Ignore records with flexible arrays. |
| 3357 | const RecordDecl *RD = RT->getDecl(); |
| 3358 | if (RD->hasFlexibleArrayMember()) |
| 3359 | return false; |
| 3360 | |
| 3361 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 3362 | // like". |
| 3363 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 3364 | |
| 3365 | bool HadField = false; |
| 3366 | unsigned idx = 0; |
| 3367 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3368 | i != e; ++i, ++idx) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3369 | const FieldDecl *FD = *i; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3370 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3371 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 3372 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 3373 | // struct { int : 0; int x } |
| 3374 | // is non-integer like according to gcc. |
| 3375 | if (FD->isBitField()) { |
| 3376 | if (!RD->isUnion()) |
| 3377 | HadField = true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3378 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3379 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3380 | return false; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3381 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3382 | continue; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3385 | // Check if this field is at offset 0. |
| 3386 | if (Layout.getFieldOffset(idx) != 0) |
| 3387 | return false; |
| 3388 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3389 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 3390 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3391 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 3392 | // Only allow at most one field in a structure. This doesn't match the |
| 3393 | // wording above, but follows gcc in situations with a field following an |
| 3394 | // empty structure. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3395 | if (!RD->isUnion()) { |
| 3396 | if (HadField) |
| 3397 | return false; |
| 3398 | |
| 3399 | HadField = true; |
| 3400 | } |
| 3401 | } |
| 3402 | |
| 3403 | return true; |
| 3404 | } |
| 3405 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3406 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3407 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3408 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3409 | |
Daniel Dunbar | f554b1c | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 3410 | // Large vector types should be returned via memory. |
| 3411 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 3412 | return ABIArgInfo::getIndirect(0); |
| 3413 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 3414 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3415 | // Treat an enum type as its underlying type. |
| 3416 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3417 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3418 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 3419 | return (RetTy->isPromotableIntegerType() ? |
| 3420 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 3421 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3422 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 3423 | // Structures with either a non-trivial destructor or a non-trivial |
| 3424 | // copy constructor are always indirect. |
| 3425 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) |
| 3426 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3427 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3428 | // Are we following APCS? |
| 3429 | if (getABIKind() == APCS) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3430 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3431 | return ABIArgInfo::getIgnore(); |
| 3432 | |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3433 | // Complex types are all returned as packed integers. |
| 3434 | // |
| 3435 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 3436 | // correctly. |
| 3437 | if (RetTy->isAnyComplexType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3438 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3439 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 3440 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3441 | // Integer like structures are returned in r0. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3442 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3443 | // Return in the smallest viable integer type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3444 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3445 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3446 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3447 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3448 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3449 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | // Otherwise return in memory. |
| 3453 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3454 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3455 | |
| 3456 | // Otherwise this is an AAPCS variant. |
| 3457 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3458 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3459 | return ABIArgInfo::getIgnore(); |
| 3460 | |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3461 | // Check for homogeneous aggregates with AAPCS-VFP. |
| 3462 | if (getABIKind() == AAPCS_VFP) { |
| 3463 | const Type *Base = 0; |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3464 | if (isHomogeneousAggregate(RetTy, Base, getContext())) { |
| 3465 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3466 | // Homogeneous Aggregates are returned directly. |
| 3467 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | eaf856d | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3468 | } |
Bob Wilson | 3b694fa | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3471 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 3472 | // are returned indirectly. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3473 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3474 | if (Size <= 32) { |
| 3475 | // Return in the smallest viable integer type. |
| 3476 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3477 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3478 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3479 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 3480 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 3483 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3486 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 3487 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 3488 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3489 | // Check whether VT is legal. |
| 3490 | unsigned NumElements = VT->getNumElements(); |
| 3491 | uint64_t Size = getContext().getTypeSize(VT); |
| 3492 | // NumElements should be power of 2. |
| 3493 | if ((NumElements & (NumElements - 1)) != 0) |
| 3494 | return true; |
| 3495 | // Size should be greater than 32 bits. |
| 3496 | return Size <= 32; |
| 3497 | } |
| 3498 | return false; |
| 3499 | } |
| 3500 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3501 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3502 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3503 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3504 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3505 | |
| 3506 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3507 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3508 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3509 | |
| 3510 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3511 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3512 | bool IsIndirect = false; |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3513 | |
| 3514 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 3515 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
Manman Ren | 9337102 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 3516 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 3517 | getABIKind() == ARMABIInfo::AAPCS) |
| 3518 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 3519 | else |
| 3520 | TyAlign = 4; |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3521 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 3522 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 3523 | IsIndirect = true; |
| 3524 | Size = 4; |
| 3525 | TyAlign = 4; |
| 3526 | } |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3527 | |
| 3528 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3529 | if (TyAlign > 4) { |
| 3530 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 3531 | "Alignment is not power of 2!"); |
| 3532 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 3533 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 3534 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3535 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 3536 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3537 | |
| 3538 | uint64_t Offset = |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3539 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3540 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3541 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3542 | "ap.next"); |
| 3543 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3544 | |
Manman Ren | 97f8157 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3545 | if (IsIndirect) |
| 3546 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 9337102 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 3547 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | d105e73 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 3548 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 3549 | // may not be correctly aligned for the vector type. We create an aligned |
| 3550 | // temporary space and copy the content over from ap.cur to the temporary |
| 3551 | // space. This is necessary if the natural alignment of the type is greater |
| 3552 | // than the ABI alignment. |
| 3553 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 3554 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 3555 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 3556 | "var.align"); |
| 3557 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 3558 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 3559 | Builder.CreateMemCpy(Dst, Src, |
| 3560 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 3561 | TyAlign, false); |
| 3562 | Addr = AlignedTemp; //The content is in aligned location. |
| 3563 | } |
| 3564 | llvm::Type *PTy = |
| 3565 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3566 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3567 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3568 | return AddrTyped; |
| 3569 | } |
| 3570 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3571 | namespace { |
| 3572 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3573 | class NaClARMABIInfo : public ABIInfo { |
| 3574 | public: |
| 3575 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3576 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
| 3577 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 3578 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3579 | CodeGenFunction &CGF) const; |
| 3580 | private: |
| 3581 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 3582 | ARMABIInfo NInfo; // Used for everything else. |
| 3583 | }; |
| 3584 | |
| 3585 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3586 | public: |
| 3587 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 3588 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 3589 | }; |
| 3590 | |
Benjamin Kramer | c6f84cf | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3593 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3594 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 3595 | PInfo.computeInfo(FI); |
| 3596 | else |
| 3597 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 3598 | } |
| 3599 | |
| 3600 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3601 | CodeGenFunction &CGF) const { |
| 3602 | // Always use the native convention; calling pnacl-style varargs functions |
| 3603 | // is unsupported. |
| 3604 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 3605 | } |
| 3606 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3607 | //===----------------------------------------------------------------------===// |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 3608 | // AArch64 ABI Implementation |
| 3609 | //===----------------------------------------------------------------------===// |
| 3610 | |
| 3611 | namespace { |
| 3612 | |
| 3613 | class AArch64ABIInfo : public ABIInfo { |
| 3614 | public: |
| 3615 | AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 3616 | |
| 3617 | private: |
| 3618 | // The AArch64 PCS is explicit about return types and argument types being |
| 3619 | // handled identically, so we don't need to draw a distinction between |
| 3620 | // Argument and Return classification. |
| 3621 | ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs, |
| 3622 | int &FreeVFPRegs) const; |
| 3623 | |
| 3624 | ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt, |
| 3625 | llvm::Type *DirectTy = 0) const; |
| 3626 | |
| 3627 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 3628 | |
| 3629 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3630 | CodeGenFunction &CGF) const; |
| 3631 | }; |
| 3632 | |
| 3633 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3634 | public: |
| 3635 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3636 | :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {} |
| 3637 | |
| 3638 | const AArch64ABIInfo &getABIInfo() const { |
| 3639 | return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3640 | } |
| 3641 | |
| 3642 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 3643 | return 31; |
| 3644 | } |
| 3645 | |
| 3646 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3647 | llvm::Value *Address) const { |
| 3648 | // 0-31 are x0-x30 and sp: 8 bytes each |
| 3649 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| 3650 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31); |
| 3651 | |
| 3652 | // 64-95 are v0-v31: 16 bytes each |
| 3653 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
| 3654 | AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95); |
| 3655 | |
| 3656 | return false; |
| 3657 | } |
| 3658 | |
| 3659 | }; |
| 3660 | |
| 3661 | } |
| 3662 | |
| 3663 | void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3664 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 3665 | |
| 3666 | FI.getReturnInfo() = classifyGenericType(FI.getReturnType(), |
| 3667 | FreeIntRegs, FreeVFPRegs); |
| 3668 | |
| 3669 | FreeIntRegs = FreeVFPRegs = 8; |
| 3670 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3671 | it != ie; ++it) { |
| 3672 | it->info = classifyGenericType(it->type, FreeIntRegs, FreeVFPRegs); |
| 3673 | |
| 3674 | } |
| 3675 | } |
| 3676 | |
| 3677 | ABIArgInfo |
| 3678 | AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, |
| 3679 | bool IsInt, llvm::Type *DirectTy) const { |
| 3680 | if (FreeRegs >= RegsNeeded) { |
| 3681 | FreeRegs -= RegsNeeded; |
| 3682 | return ABIArgInfo::getDirect(DirectTy); |
| 3683 | } |
| 3684 | |
| 3685 | llvm::Type *Padding = 0; |
| 3686 | |
| 3687 | // We need padding so that later arguments don't get filled in anyway. That |
| 3688 | // wouldn't happen if only ByVal arguments followed in the same category, but |
| 3689 | // a large structure will simply seem to be a pointer as far as LLVM is |
| 3690 | // concerned. |
| 3691 | if (FreeRegs > 0) { |
| 3692 | if (IsInt) |
| 3693 | Padding = llvm::Type::getInt64Ty(getVMContext()); |
| 3694 | else |
| 3695 | Padding = llvm::Type::getFloatTy(getVMContext()); |
| 3696 | |
| 3697 | // Either [N x i64] or [N x float]. |
| 3698 | Padding = llvm::ArrayType::get(Padding, FreeRegs); |
| 3699 | FreeRegs = 0; |
| 3700 | } |
| 3701 | |
| 3702 | return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8, |
| 3703 | /*IsByVal=*/ true, /*Realign=*/ false, |
| 3704 | Padding); |
| 3705 | } |
| 3706 | |
| 3707 | |
| 3708 | ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty, |
| 3709 | int &FreeIntRegs, |
| 3710 | int &FreeVFPRegs) const { |
| 3711 | // Can only occurs for return, but harmless otherwise. |
| 3712 | if (Ty->isVoidType()) |
| 3713 | return ABIArgInfo::getIgnore(); |
| 3714 | |
| 3715 | // Large vector types should be returned via memory. There's no such concept |
| 3716 | // in the ABI, but they'd be over 16 bytes anyway so no matter how they're |
| 3717 | // classified they'd go into memory (see B.3). |
| 3718 | if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) { |
| 3719 | if (FreeIntRegs > 0) |
| 3720 | --FreeIntRegs; |
| 3721 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3722 | } |
| 3723 | |
| 3724 | // All non-aggregate LLVM types have a concrete ABI representation so they can |
| 3725 | // be passed directly. After this block we're guaranteed to be in a |
| 3726 | // complicated case. |
| 3727 | if (!isAggregateTypeForABI(Ty)) { |
| 3728 | // Treat an enum type as its underlying type. |
| 3729 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3730 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3731 | |
| 3732 | if (Ty->isFloatingType() || Ty->isVectorType()) |
| 3733 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false); |
| 3734 | |
| 3735 | assert(getContext().getTypeSize(Ty) <= 128 && |
| 3736 | "unexpectedly large scalar type"); |
| 3737 | |
| 3738 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 3739 | |
| 3740 | // If the type may need padding registers to ensure "alignment", we must be |
| 3741 | // careful when this is accounted for. Increasing the effective size covers |
| 3742 | // all cases. |
| 3743 | if (getContext().getTypeAlign(Ty) == 128) |
| 3744 | RegsNeeded += FreeIntRegs % 2 != 0; |
| 3745 | |
| 3746 | return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true); |
| 3747 | } |
| 3748 | |
| 3749 | // Structures with either a non-trivial destructor or a non-trivial |
| 3750 | // copy constructor are always indirect. |
| 3751 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) { |
| 3752 | if (FreeIntRegs > 0) |
| 3753 | --FreeIntRegs; |
| 3754 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3755 | } |
| 3756 | |
| 3757 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3758 | if (!getContext().getLangOpts().CPlusPlus) { |
| 3759 | // Empty structs outside C++ mode are a GNU extension, so no ABI can |
| 3760 | // possibly tell us what to do. It turns out (I believe) that GCC ignores |
| 3761 | // the object for parameter-passsing purposes. |
| 3762 | return ABIArgInfo::getIgnore(); |
| 3763 | } |
| 3764 | |
| 3765 | // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode |
| 3766 | // description of va_arg in the PCS require that an empty struct does |
| 3767 | // actually occupy space for parameter-passing. I'm hoping for a |
| 3768 | // clarification giving an explicit paragraph to point to in future. |
| 3769 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true, |
| 3770 | llvm::Type::getInt8Ty(getVMContext())); |
| 3771 | } |
| 3772 | |
| 3773 | // Homogeneous vector aggregates get passed in registers or on the stack. |
| 3774 | const Type *Base = 0; |
| 3775 | uint64_t NumMembers = 0; |
| 3776 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) { |
| 3777 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 3778 | // Homogeneous aggregates are passed and returned directly. |
| 3779 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers, |
| 3780 | /*IsInt=*/ false); |
| 3781 | } |
| 3782 | |
| 3783 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3784 | if (Size <= 128) { |
| 3785 | // Small structs can use the same direct type whether they're in registers |
| 3786 | // or on the stack. |
| 3787 | llvm::Type *BaseTy; |
| 3788 | unsigned NumBases; |
| 3789 | int SizeInRegs = (Size + 63) / 64; |
| 3790 | |
| 3791 | if (getContext().getTypeAlign(Ty) == 128) { |
| 3792 | BaseTy = llvm::Type::getIntNTy(getVMContext(), 128); |
| 3793 | NumBases = 1; |
| 3794 | |
| 3795 | // If the type may need padding registers to ensure "alignment", we must |
| 3796 | // be careful when this is accounted for. Increasing the effective size |
| 3797 | // covers all cases. |
| 3798 | SizeInRegs += FreeIntRegs % 2 != 0; |
| 3799 | } else { |
| 3800 | BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3801 | NumBases = SizeInRegs; |
| 3802 | } |
| 3803 | llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases); |
| 3804 | |
| 3805 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs, |
| 3806 | /*IsInt=*/ true, DirectTy); |
| 3807 | } |
| 3808 | |
| 3809 | // If the aggregate is > 16 bytes, it's passed and returned indirectly. In |
| 3810 | // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere. |
| 3811 | --FreeIntRegs; |
| 3812 | return ABIArgInfo::getIndirect(0, /* byVal = */ false); |
| 3813 | } |
| 3814 | |
| 3815 | llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3816 | CodeGenFunction &CGF) const { |
| 3817 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 3818 | // Standard, section B.4: |
| 3819 | // |
| 3820 | // struct { |
| 3821 | // void *__stack; |
| 3822 | // void *__gr_top; |
| 3823 | // void *__vr_top; |
| 3824 | // int __gr_offs; |
| 3825 | // int __vr_offs; |
| 3826 | // }; |
| 3827 | |
| 3828 | assert(!CGF.CGM.getDataLayout().isBigEndian() |
| 3829 | && "va_arg not implemented for big-endian AArch64"); |
| 3830 | |
| 3831 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 3832 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 3833 | ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs); |
| 3834 | |
| 3835 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 3836 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3837 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 3838 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3839 | |
| 3840 | llvm::Value *reg_offs_p = 0, *reg_offs = 0; |
| 3841 | int reg_top_index; |
| 3842 | int RegSize; |
| 3843 | if (FreeIntRegs < 8) { |
| 3844 | assert(FreeVFPRegs == 8 && "Arguments never split between int & VFP regs"); |
| 3845 | // 3 is the field number of __gr_offs |
| 3846 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 3847 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 3848 | reg_top_index = 1; // field number for __gr_top |
| 3849 | RegSize = 8 * (8 - FreeIntRegs); |
| 3850 | } else { |
| 3851 | assert(FreeVFPRegs < 8 && "Argument must go in VFP or int regs"); |
| 3852 | // 4 is the field number of __vr_offs. |
| 3853 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 3854 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 3855 | reg_top_index = 2; // field number for __vr_top |
| 3856 | RegSize = 16 * (8 - FreeVFPRegs); |
| 3857 | } |
| 3858 | |
| 3859 | //======================================= |
| 3860 | // Find out where argument was passed |
| 3861 | //======================================= |
| 3862 | |
| 3863 | // If reg_offs >= 0 we're already using the stack for this type of |
| 3864 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 3865 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 3866 | // whatever they get). |
| 3867 | llvm::Value *UsingStack = 0; |
| 3868 | UsingStack = CGF.Builder.CreateICmpSGE(reg_offs, |
| 3869 | llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 3870 | |
| 3871 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 3872 | |
| 3873 | // Otherwise, at least some kind of argument could go in these registers, the |
| 3874 | // quesiton is whether this particular type is too big. |
| 3875 | CGF.EmitBlock(MaybeRegBlock); |
| 3876 | |
| 3877 | // Integer arguments may need to correct register alignment (for example a |
| 3878 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 3879 | // align __gr_offs to calculate the potential address. |
| 3880 | if (FreeIntRegs < 8 && AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 3881 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 3882 | |
| 3883 | reg_offs = CGF.Builder.CreateAdd(reg_offs, |
| 3884 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 3885 | "align_regoffs"); |
| 3886 | reg_offs = CGF.Builder.CreateAnd(reg_offs, |
| 3887 | llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 3888 | "aligned_regoffs"); |
| 3889 | } |
| 3890 | |
| 3891 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
| 3892 | llvm::Value *NewOffset = 0; |
| 3893 | NewOffset = CGF.Builder.CreateAdd(reg_offs, |
| 3894 | llvm::ConstantInt::get(CGF.Int32Ty, RegSize), |
| 3895 | "new_reg_offs"); |
| 3896 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 3897 | |
| 3898 | // Now we're in a position to decide whether this argument really was in |
| 3899 | // registers or not. |
| 3900 | llvm::Value *InRegs = 0; |
| 3901 | InRegs = CGF.Builder.CreateICmpSLE(NewOffset, |
| 3902 | llvm::ConstantInt::get(CGF.Int32Ty, 0), |
| 3903 | "inreg"); |
| 3904 | |
| 3905 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 3906 | |
| 3907 | //======================================= |
| 3908 | // Argument was in registers |
| 3909 | //======================================= |
| 3910 | |
| 3911 | // Now we emit the code for if the argument was originally passed in |
| 3912 | // registers. First start the appropriate block: |
| 3913 | CGF.EmitBlock(InRegBlock); |
| 3914 | |
| 3915 | llvm::Value *reg_top_p = 0, *reg_top = 0; |
| 3916 | reg_top_p = CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 3917 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 3918 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
| 3919 | llvm::Value *RegAddr = 0; |
| 3920 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 3921 | |
| 3922 | if (!AI.isDirect()) { |
| 3923 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 3924 | // stored registers or on the stack will actually be a struct **. |
| 3925 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 3926 | } |
| 3927 | |
| 3928 | const Type *Base = 0; |
| 3929 | uint64_t NumMembers; |
| 3930 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers) |
| 3931 | && NumMembers > 1) { |
| 3932 | // Homogeneous aggregates passed in registers will have their elements split |
| 3933 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 3934 | // qN+1, ...). We reload and store into a temporary local variable |
| 3935 | // contiguously. |
| 3936 | assert(AI.isDirect() && "Homogeneous aggregates should be passed directly"); |
| 3937 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 3938 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 3939 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 3940 | |
| 3941 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 3942 | llvm::Value *BaseOffset = llvm::ConstantInt::get(CGF.Int32Ty, 16 * i); |
| 3943 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 3944 | LoadAddr = CGF.Builder.CreateBitCast(LoadAddr, |
| 3945 | llvm::PointerType::getUnqual(BaseTy)); |
| 3946 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 3947 | |
| 3948 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 3949 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 3950 | } |
| 3951 | |
| 3952 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 3953 | } else { |
| 3954 | // Otherwise the object is contiguous in memory |
| 3955 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 3956 | } |
| 3957 | |
| 3958 | CGF.EmitBranch(ContBlock); |
| 3959 | |
| 3960 | //======================================= |
| 3961 | // Argument was on the stack |
| 3962 | //======================================= |
| 3963 | CGF.EmitBlock(OnStackBlock); |
| 3964 | |
| 3965 | llvm::Value *stack_p = 0, *OnStackAddr = 0; |
| 3966 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 3967 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 3968 | |
| 3969 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 3970 | // floating-point ones might be affected. |
| 3971 | if (AI.isDirect() && getContext().getTypeAlign(Ty) > 64) { |
| 3972 | int Align = getContext().getTypeAlign(Ty) / 8; |
| 3973 | |
| 3974 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3975 | |
| 3976 | OnStackAddr = CGF.Builder.CreateAdd(OnStackAddr, |
| 3977 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 3978 | "align_stack"); |
| 3979 | OnStackAddr = CGF.Builder.CreateAnd(OnStackAddr, |
| 3980 | llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 3981 | "align_stack"); |
| 3982 | |
| 3983 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3984 | } |
| 3985 | |
| 3986 | uint64_t StackSize; |
| 3987 | if (AI.isDirect()) |
| 3988 | StackSize = getContext().getTypeSize(Ty) / 8; |
| 3989 | else |
| 3990 | StackSize = 8; |
| 3991 | |
| 3992 | // All stack slots are 8 bytes |
| 3993 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 3994 | |
| 3995 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 3996 | llvm::Value *NewStack = CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, |
| 3997 | "new_stack"); |
| 3998 | |
| 3999 | // Write the new value of __stack for the next call to va_arg |
| 4000 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4001 | |
| 4002 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4003 | |
| 4004 | CGF.EmitBranch(ContBlock); |
| 4005 | |
| 4006 | //======================================= |
| 4007 | // Tidy up |
| 4008 | //======================================= |
| 4009 | CGF.EmitBlock(ContBlock); |
| 4010 | |
| 4011 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4012 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4013 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4014 | |
| 4015 | if (AI.isDirect()) |
| 4016 | return ResAddr; |
| 4017 | |
| 4018 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4019 | } |
| 4020 | |
| 4021 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4022 | // NVPTX ABI Implementation |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4023 | //===----------------------------------------------------------------------===// |
| 4024 | |
| 4025 | namespace { |
| 4026 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4027 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4028 | public: |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4029 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4030 | |
| 4031 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4032 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4033 | |
| 4034 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 4035 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4036 | CodeGenFunction &CFG) const; |
| 4037 | }; |
| 4038 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4039 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4040 | public: |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4041 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4042 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4043 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4044 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4045 | CodeGen::CodeGenModule &M) const; |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4046 | private: |
| 4047 | static void addKernelMetadata(llvm::Function *F); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4048 | }; |
| 4049 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4050 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4051 | if (RetTy->isVoidType()) |
| 4052 | return ABIArgInfo::getIgnore(); |
| 4053 | if (isAggregateTypeForABI(RetTy)) |
| 4054 | return ABIArgInfo::getIndirect(0); |
| 4055 | return ABIArgInfo::getDirect(); |
| 4056 | } |
| 4057 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4058 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4059 | if (isAggregateTypeForABI(Ty)) |
| 4060 | return ABIArgInfo::getIndirect(0); |
| 4061 | |
| 4062 | return ABIArgInfo::getDirect(); |
| 4063 | } |
| 4064 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4065 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4066 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4067 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4068 | it != ie; ++it) |
| 4069 | it->info = classifyArgumentType(it->type); |
| 4070 | |
| 4071 | // Always honor user-specified calling convention. |
| 4072 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4073 | return; |
| 4074 | |
John McCall | bd7370a | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4075 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 4076 | } |
| 4077 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4078 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4079 | CodeGenFunction &CFG) const { |
| 4080 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4083 | void NVPTXTargetCodeGenInfo:: |
| 4084 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4085 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4086 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4087 | if (!FD) return; |
| 4088 | |
| 4089 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4090 | |
| 4091 | // Perform special handling in OpenCL mode |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4092 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4093 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4094 | // By default, all functions are device functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4095 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4096 | // OpenCL __kernel functions get kernel metadata |
| 4097 | addKernelMetadata(F); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4098 | // And kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4099 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4100 | } |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4101 | } |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4102 | |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4103 | // Perform special handling in CUDA mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4104 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4105 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4106 | // __global__ functions cannot be called from the device, we do not |
| 4107 | // need to set the noinline attribute. |
| 4108 | if (FD->getAttr<CUDAGlobalAttr>()) |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4109 | addKernelMetadata(F); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4110 | } |
| 4111 | } |
| 4112 | |
Justin Holewinski | dca8f33 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4113 | void NVPTXTargetCodeGenInfo::addKernelMetadata(llvm::Function *F) { |
| 4114 | llvm::Module *M = F->getParent(); |
| 4115 | llvm::LLVMContext &Ctx = M->getContext(); |
| 4116 | |
| 4117 | // Get "nvvm.annotations" metadata node |
| 4118 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 4119 | |
| 4120 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4121 | llvm::SmallVector<llvm::Value *, 3> MDVals; |
| 4122 | MDVals.push_back(F); |
| 4123 | MDVals.push_back(llvm::MDString::get(Ctx, "kernel")); |
| 4124 | MDVals.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1)); |
| 4125 | |
| 4126 | // Append metadata to nvvm.annotations |
| 4127 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 4128 | } |
| 4129 | |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | //===----------------------------------------------------------------------===// |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 4133 | // MBlaze ABI Implementation |
| 4134 | //===----------------------------------------------------------------------===// |
| 4135 | |
| 4136 | namespace { |
| 4137 | |
| 4138 | class MBlazeABIInfo : public ABIInfo { |
| 4139 | public: |
| 4140 | MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4141 | |
| 4142 | bool isPromotableIntegerType(QualType Ty) const; |
| 4143 | |
| 4144 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4145 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 4146 | |
| 4147 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 4148 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4149 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4150 | it != ie; ++it) |
| 4151 | it->info = classifyArgumentType(it->type); |
| 4152 | } |
| 4153 | |
| 4154 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4155 | CodeGenFunction &CGF) const; |
| 4156 | }; |
| 4157 | |
| 4158 | class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4159 | public: |
| 4160 | MBlazeTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4161 | : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {} |
| 4162 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4163 | CodeGen::CodeGenModule &M) const; |
| 4164 | }; |
| 4165 | |
| 4166 | } |
| 4167 | |
| 4168 | bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 4169 | // MBlaze ABI requires all 8 and 16 bit quantities to be extended. |
| 4170 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4171 | switch (BT->getKind()) { |
| 4172 | case BuiltinType::Bool: |
| 4173 | case BuiltinType::Char_S: |
| 4174 | case BuiltinType::Char_U: |
| 4175 | case BuiltinType::SChar: |
| 4176 | case BuiltinType::UChar: |
| 4177 | case BuiltinType::Short: |
| 4178 | case BuiltinType::UShort: |
| 4179 | return true; |
| 4180 | default: |
| 4181 | return false; |
| 4182 | } |
| 4183 | return false; |
| 4184 | } |
| 4185 | |
| 4186 | llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4187 | CodeGenFunction &CGF) const { |
| 4188 | // FIXME: Implement |
| 4189 | return 0; |
| 4190 | } |
| 4191 | |
| 4192 | |
| 4193 | ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const { |
| 4194 | if (RetTy->isVoidType()) |
| 4195 | return ABIArgInfo::getIgnore(); |
| 4196 | if (isAggregateTypeForABI(RetTy)) |
| 4197 | return ABIArgInfo::getIndirect(0); |
| 4198 | |
| 4199 | return (isPromotableIntegerType(RetTy) ? |
| 4200 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4201 | } |
| 4202 | |
| 4203 | ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const { |
| 4204 | if (isAggregateTypeForABI(Ty)) |
| 4205 | return ABIArgInfo::getIndirect(0); |
| 4206 | |
| 4207 | return (isPromotableIntegerType(Ty) ? |
| 4208 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4209 | } |
| 4210 | |
| 4211 | void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4212 | llvm::GlobalValue *GV, |
| 4213 | CodeGen::CodeGenModule &M) |
| 4214 | const { |
| 4215 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4216 | if (!FD) return; |
NAKAMURA Takumi | 125b4cb | 2011-02-17 08:50:50 +0000 | [diff] [blame] | 4217 | |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 4218 | llvm::CallingConv::ID CC = llvm::CallingConv::C; |
| 4219 | if (FD->hasAttr<MBlazeInterruptHandlerAttr>()) |
| 4220 | CC = llvm::CallingConv::MBLAZE_INTR; |
| 4221 | else if (FD->hasAttr<MBlazeSaveVolatilesAttr>()) |
| 4222 | CC = llvm::CallingConv::MBLAZE_SVOL; |
| 4223 | |
| 4224 | if (CC != llvm::CallingConv::C) { |
| 4225 | // Handle 'interrupt_handler' attribute: |
| 4226 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4227 | |
| 4228 | // Step 1: Set ISR calling convention. |
| 4229 | F->setCallingConv(CC); |
| 4230 | |
| 4231 | // Step 2: Add attributes goodness. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4232 | F->addFnAttr(llvm::Attribute::NoInline); |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | // Step 3: Emit _interrupt_handler alias. |
| 4236 | if (CC == llvm::CallingConv::MBLAZE_INTR) |
| 4237 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
| 4238 | "_interrupt_handler", GV, &M.getModule()); |
| 4239 | } |
| 4240 | |
| 4241 | |
| 4242 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4243 | // MSP430 ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4244 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4245 | |
| 4246 | namespace { |
| 4247 | |
| 4248 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4249 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4250 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 4251 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4252 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4253 | CodeGen::CodeGenModule &M) const; |
| 4254 | }; |
| 4255 | |
| 4256 | } |
| 4257 | |
| 4258 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4259 | llvm::GlobalValue *GV, |
| 4260 | CodeGen::CodeGenModule &M) const { |
| 4261 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4262 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 4263 | // Handle 'interrupt' attribute: |
| 4264 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4265 | |
| 4266 | // Step 1: Set ISR calling convention. |
| 4267 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 4268 | |
| 4269 | // Step 2: Add attributes goodness. |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4270 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4271 | |
| 4272 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | f419a85 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4273 | unsigned Num = attr->getNumber() / 2; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4274 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Anton Korobeynikov | f419a85 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 4275 | "__isr_" + Twine(Num), |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4276 | GV, &M.getModule()); |
| 4277 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4281 | //===----------------------------------------------------------------------===// |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4282 | // MIPS ABI Implementation. This works for both little-endian and |
| 4283 | // big-endian variants. |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4284 | //===----------------------------------------------------------------------===// |
| 4285 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4286 | namespace { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4287 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4288 | bool IsO32; |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4289 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 4290 | void CoerceToIntArgs(uint64_t TySize, |
| 4291 | SmallVector<llvm::Type*, 8> &ArgList) const; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4292 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4293 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4294 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4295 | public: |
Akira Hatanaka | b551dd3 | 2011-11-03 00:05:50 +0000 | [diff] [blame] | 4296 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4297 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
| 4298 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4299 | |
| 4300 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4301 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4302 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 4303 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4304 | CodeGenFunction &CGF) const; |
| 4305 | }; |
| 4306 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4307 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4308 | unsigned SizeOfUnwindException; |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4309 | public: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4310 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 4311 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
| 4312 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4313 | |
| 4314 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 4315 | return 29; |
| 4316 | } |
| 4317 | |
Reed Kotler | 7dfd182 | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4318 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4319 | CodeGen::CodeGenModule &CGM) const { |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4320 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4321 | if (!FD) return; |
Rafael Espindola | d8e6d6d | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 4322 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4323 | if (FD->hasAttr<Mips16Attr>()) { |
| 4324 | Fn->addFnAttr("mips16"); |
| 4325 | } |
| 4326 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 4327 | Fn->addFnAttr("nomips16"); |
| 4328 | } |
Reed Kotler | 7dfd182 | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 4329 | } |
Reed Kotler | ad4b8b4 | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 4330 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4331 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4332 | llvm::Value *Address) const; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4333 | |
| 4334 | unsigned getSizeOfUnwindException() const { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 4335 | return SizeOfUnwindException; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4336 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4337 | }; |
| 4338 | } |
| 4339 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4340 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
| 4341 | SmallVector<llvm::Type*, 8> &ArgList) const { |
| 4342 | llvm::IntegerType *IntTy = |
| 4343 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4344 | |
| 4345 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 4346 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 4347 | ArgList.push_back(IntTy); |
| 4348 | |
| 4349 | // If necessary, add one more integer type to ArgList. |
| 4350 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 4351 | |
| 4352 | if (R) |
| 4353 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4356 | // In N32/64, an aligned double precision floating point field is passed in |
| 4357 | // a register. |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4358 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4359 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 4360 | |
| 4361 | if (IsO32) { |
| 4362 | CoerceToIntArgs(TySize, ArgList); |
| 4363 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4364 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4365 | |
Akira Hatanaka | 2afd23d | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 4366 | if (Ty->isComplexType()) |
| 4367 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 6d1080f | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 4368 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4369 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4370 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4371 | // Unions/vectors are passed in integer registers. |
| 4372 | if (!RT || !RT->isStructureOrClassType()) { |
| 4373 | CoerceToIntArgs(TySize, ArgList); |
| 4374 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4375 | } |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4376 | |
| 4377 | const RecordDecl *RD = RT->getDecl(); |
| 4378 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4379 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4380 | |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4381 | uint64_t LastOffset = 0; |
| 4382 | unsigned idx = 0; |
| 4383 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 4384 | |
Akira Hatanaka | a34e921 | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 4385 | // Iterate over fields in the struct/class and check if there are any aligned |
| 4386 | // double fields. |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4387 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4388 | i != e; ++i, ++idx) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4389 | const QualType Ty = i->getType(); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4390 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 4391 | |
| 4392 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 4393 | continue; |
| 4394 | |
| 4395 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 4396 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 4397 | continue; |
| 4398 | |
| 4399 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 4400 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 4401 | ArgList.push_back(I64); |
| 4402 | |
| 4403 | // Add double type. |
| 4404 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 4405 | LastOffset = Offset + 64; |
| 4406 | } |
| 4407 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4408 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 4409 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | d5a257f | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 4410 | |
| 4411 | return llvm::StructType::get(getVMContext(), ArgList); |
| 4412 | } |
| 4413 | |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4414 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t Align, uint64_t Offset) const { |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4415 | assert((Offset % MinABIStackAlignInBytes) == 0); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4416 | |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4417 | if ((Align - 1) & Offset) |
| 4418 | return llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
| 4419 | |
| 4420 | return 0; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4421 | } |
Akira Hatanaka | 9659d59 | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 4422 | |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4423 | ABIArgInfo |
| 4424 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4425 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4426 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4427 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4428 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4429 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 4430 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4431 | Offset = llvm::RoundUpToAlignment(Offset, Align); |
| 4432 | Offset += llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4433 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4434 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4435 | // Ignore empty aggregates. |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4436 | if (TySize == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4437 | return ABIArgInfo::getIgnore(); |
| 4438 | |
Akira Hatanaka | 511949b | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 4439 | // Records with non trivial destructors/constructors should not be passed |
| 4440 | // by value. |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4441 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) { |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4442 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Akira Hatanaka | 511949b | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 4443 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4444 | } |
Akira Hatanaka | 511949b | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 4445 | |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4446 | // If we have reached here, aggregates are passed directly by coercing to |
| 4447 | // another structure type. Padding is inserted if the offset of the |
| 4448 | // aggregate is unaligned. |
| 4449 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 4450 | getPaddingType(Align, OrigOffset)); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | // Treat an enum type as its underlying type. |
| 4454 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4455 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4456 | |
Akira Hatanaka | a33fd39 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 4457 | if (Ty->isPromotableIntegerType()) |
| 4458 | return ABIArgInfo::getExtend(); |
| 4459 | |
Akira Hatanaka | 4055cfc | 2013-01-24 21:47:33 +0000 | [diff] [blame] | 4460 | return ABIArgInfo::getDirect(0, 0, |
| 4461 | IsO32 ? 0 : getPaddingType(Align, OrigOffset)); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4462 | } |
| 4463 | |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4464 | llvm::Type* |
| 4465 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4466 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4467 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4468 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4469 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4470 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4471 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 4472 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4473 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4474 | // N32/64 returns struct/classes in floating point registers if the |
| 4475 | // following conditions are met: |
| 4476 | // 1. The size of the struct/class is no larger than 128-bit. |
| 4477 | // 2. The struct/class has one or two fields all of which are floating |
| 4478 | // point types. |
| 4479 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 4480 | // |
| 4481 | // Any other composite results are returned in integer registers. |
| 4482 | // |
| 4483 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 4484 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 4485 | for (; b != e; ++b) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4486 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4487 | |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4488 | if (!BT || !BT->isFloatingPoint()) |
| 4489 | break; |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4490 | |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4491 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | da54ff3 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | if (b == e) |
| 4495 | return llvm::StructType::get(getVMContext(), RTList, |
| 4496 | RD->hasAttr<PackedAttr>()); |
| 4497 | |
| 4498 | RTList.clear(); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4499 | } |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4502 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4503 | return llvm::StructType::get(getVMContext(), RTList); |
| 4504 | } |
| 4505 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4506 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | a8536c0 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 4507 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4508 | |
| 4509 | if (RetTy->isVoidType() || Size == 0) |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4510 | return ABIArgInfo::getIgnore(); |
| 4511 | |
Akira Hatanaka | 8aeb147 | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 4512 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4513 | if (Size <= 128) { |
| 4514 | if (RetTy->isAnyComplexType()) |
| 4515 | return ABIArgInfo::getDirect(); |
| 4516 | |
Akira Hatanaka | c359f20 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 4517 | // O32 returns integer vectors in registers. |
| 4518 | if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) |
| 4519 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 4520 | |
Akira Hatanaka | 526cdfb | 2012-02-08 01:31:22 +0000 | [diff] [blame] | 4521 | if (!IsO32 && !isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) |
Akira Hatanaka | c7ecc2e | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 4522 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 4523 | } |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4524 | |
| 4525 | return ABIArgInfo::getIndirect(0); |
| 4526 | } |
| 4527 | |
| 4528 | // Treat an enum type as its underlying type. |
| 4529 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4530 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4531 | |
| 4532 | return (RetTy->isPromotableIntegerType() ? |
| 4533 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4534 | } |
| 4535 | |
| 4536 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 4537 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
| 4538 | RetInfo = classifyReturnType(FI.getReturnType()); |
| 4539 | |
| 4540 | // Check if a pointer to an aggregate is passed as a hidden argument. |
Akira Hatanaka | 91338cf | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 4541 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | cc66254 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 4542 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4543 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4544 | it != ie; ++it) |
Akira Hatanaka | f0cc208 | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 4545 | it->info = classifyArgumentType(it->type, Offset); |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4549 | CodeGenFunction &CGF) const { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4550 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4551 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4552 | |
| 4553 | CGBuilderTy &Builder = CGF.Builder; |
| 4554 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4555 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4556 | int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4557 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4558 | llvm::Value *AddrTyped; |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4559 | unsigned PtrWidth = getContext().getTargetInfo().getPointerWidth(0); |
| 4560 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4561 | |
| 4562 | if (TypeAlign > MinABIStackAlignInBytes) { |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4563 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 4564 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 4565 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 4566 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4567 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 4568 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 4569 | } |
| 4570 | else |
| 4571 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4572 | |
| 4573 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4574 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4575 | uint64_t Offset = |
| 4576 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 4577 | llvm::Value *NextAddr = |
Akira Hatanaka | 8f675e4 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 4578 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 4579 | "ap.next"); |
| 4580 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4581 | |
| 4582 | return AddrTyped; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4585 | bool |
| 4586 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4587 | llvm::Value *Address) const { |
| 4588 | // This information comes from gcc's implementation, which seems to |
| 4589 | // as canonical as it gets. |
| 4590 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4591 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 4592 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4593 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4594 | |
| 4595 | // 0-31 are the general purpose registers, $0 - $31. |
| 4596 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 4597 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 4598 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4599 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4600 | |
| 4601 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 4602 | // They are one bit wide and ignored here. |
| 4603 | |
| 4604 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 4605 | // (coprocessor 1 is the FP unit) |
| 4606 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 4607 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 4608 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4609 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4610 | return false; |
| 4611 | } |
| 4612 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4613 | //===----------------------------------------------------------------------===// |
| 4614 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 4615 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 4616 | // handling. |
| 4617 | //===----------------------------------------------------------------------===// |
| 4618 | |
| 4619 | namespace { |
| 4620 | |
| 4621 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4622 | public: |
| 4623 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 4624 | : DefaultTargetCodeGenInfo(CGT) {} |
| 4625 | |
| 4626 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4627 | CodeGen::CodeGenModule &M) const; |
| 4628 | }; |
| 4629 | |
| 4630 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 4631 | llvm::GlobalValue *GV, |
| 4632 | CodeGen::CodeGenModule &M) const { |
| 4633 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4634 | if (!FD) return; |
| 4635 | |
| 4636 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4637 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4638 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4639 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 4640 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4641 | F->addFnAttr(llvm::Attribute::NoInline); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4642 | |
| 4643 | if (FD->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 4644 | |
| 4645 | // Convert the reqd_work_group_size() attributes to metadata. |
| 4646 | llvm::LLVMContext &Context = F->getContext(); |
| 4647 | llvm::NamedMDNode *OpenCLMetadata = |
| 4648 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 4649 | |
| 4650 | SmallVector<llvm::Value*, 5> Operands; |
| 4651 | Operands.push_back(F); |
| 4652 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4653 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 4654 | llvm::APInt(32, |
| 4655 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getXDim()))); |
| 4656 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 4657 | llvm::APInt(32, |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4658 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getYDim()))); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4659 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
| 4660 | llvm::APInt(32, |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4661 | FD->getAttr<ReqdWorkGroupSizeAttr>()->getZDim()))); |
| 4662 | |
| 4663 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 4664 | // for implementing the work_group_size_hint attr later. Currently |
| 4665 | // always true as the hint is not yet implemented. |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4666 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4667 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 4668 | } |
| 4669 | } |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4674 | |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 4675 | //===----------------------------------------------------------------------===// |
| 4676 | // Hexagon ABI Implementation |
| 4677 | //===----------------------------------------------------------------------===// |
| 4678 | |
| 4679 | namespace { |
| 4680 | |
| 4681 | class HexagonABIInfo : public ABIInfo { |
| 4682 | |
| 4683 | |
| 4684 | public: |
| 4685 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4686 | |
| 4687 | private: |
| 4688 | |
| 4689 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4690 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 4691 | |
| 4692 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 4693 | |
| 4694 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4695 | CodeGenFunction &CGF) const; |
| 4696 | }; |
| 4697 | |
| 4698 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4699 | public: |
| 4700 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4701 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 4702 | |
| 4703 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 4704 | return 29; |
| 4705 | } |
| 4706 | }; |
| 4707 | |
| 4708 | } |
| 4709 | |
| 4710 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4711 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 4712 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 4713 | it != ie; ++it) |
| 4714 | it->info = classifyArgumentType(it->type); |
| 4715 | } |
| 4716 | |
| 4717 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 4718 | if (!isAggregateTypeForABI(Ty)) { |
| 4719 | // Treat an enum type as its underlying type. |
| 4720 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4721 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4722 | |
| 4723 | return (Ty->isPromotableIntegerType() ? |
| 4724 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4725 | } |
| 4726 | |
| 4727 | // Ignore empty records. |
| 4728 | if (isEmptyRecord(getContext(), Ty, true)) |
| 4729 | return ABIArgInfo::getIgnore(); |
| 4730 | |
| 4731 | // Structures with either a non-trivial destructor or a non-trivial |
| 4732 | // copy constructor are always indirect. |
| 4733 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 4734 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4735 | |
| 4736 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4737 | if (Size > 64) |
| 4738 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 4739 | // Pass in the smallest viable integer type. |
| 4740 | else if (Size > 32) |
| 4741 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 4742 | else if (Size > 16) |
| 4743 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 4744 | else if (Size > 8) |
| 4745 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4746 | else |
| 4747 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4748 | } |
| 4749 | |
| 4750 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 4751 | if (RetTy->isVoidType()) |
| 4752 | return ABIArgInfo::getIgnore(); |
| 4753 | |
| 4754 | // Large vector types should be returned via memory. |
| 4755 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 4756 | return ABIArgInfo::getIndirect(0); |
| 4757 | |
| 4758 | if (!isAggregateTypeForABI(RetTy)) { |
| 4759 | // Treat an enum type as its underlying type. |
| 4760 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4761 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4762 | |
| 4763 | return (RetTy->isPromotableIntegerType() ? |
| 4764 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 4765 | } |
| 4766 | |
| 4767 | // Structures with either a non-trivial destructor or a non-trivial |
| 4768 | // copy constructor are always indirect. |
| 4769 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) |
| 4770 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4771 | |
| 4772 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4773 | return ABIArgInfo::getIgnore(); |
| 4774 | |
| 4775 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 4776 | // are returned indirectly. |
| 4777 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4778 | if (Size <= 64) { |
| 4779 | // Return in the smallest viable integer type. |
| 4780 | if (Size <= 8) |
| 4781 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4782 | if (Size <= 16) |
| 4783 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4784 | if (Size <= 32) |
| 4785 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 4786 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 4787 | } |
| 4788 | |
| 4789 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 4790 | } |
| 4791 | |
| 4792 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4793 | CodeGenFunction &CGF) const { |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 4794 | // FIXME: Need to handle alignment |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4795 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 4796 | |
| 4797 | CGBuilderTy &Builder = CGF.Builder; |
| 4798 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 4799 | "ap"); |
| 4800 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4801 | llvm::Type *PTy = |
| 4802 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4803 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4804 | |
| 4805 | uint64_t Offset = |
| 4806 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 4807 | llvm::Value *NextAddr = |
| 4808 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 4809 | "ap.next"); |
| 4810 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4811 | |
| 4812 | return AddrTyped; |
| 4813 | } |
| 4814 | |
| 4815 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4816 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4817 | if (TheTargetCodeGenInfo) |
| 4818 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4820 | const llvm::Triple &Triple = getContext().getTargetInfo().getTriple(); |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 4821 | switch (Triple.getArch()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 4822 | default: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4823 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 4824 | |
Derek Schuff | 9ed63f8 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 4825 | case llvm::Triple::le32: |
| 4826 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4827 | case llvm::Triple::mips: |
| 4828 | case llvm::Triple::mipsel: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4829 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4830 | |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 4831 | case llvm::Triple::mips64: |
| 4832 | case llvm::Triple::mips64el: |
Akira Hatanaka | c0e3b66 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 4833 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 4834 | |
Tim Northover | c264e16 | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4835 | case llvm::Triple::aarch64: |
| 4836 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types)); |
| 4837 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4838 | case llvm::Triple::arm: |
| 4839 | case llvm::Triple::thumb: |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 4840 | { |
| 4841 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4842 | if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 4843 | Kind = ARMABIInfo::APCS; |
David Tweed | b16abb1 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 4844 | else if (CodeGenOpts.FloatABI == "hard" || |
| 4845 | (CodeGenOpts.FloatABI != "soft" && Triple.getEnvironment()==llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 4846 | Kind = ARMABIInfo::AAPCS_VFP; |
| 4847 | |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4848 | switch (Triple.getOS()) { |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 4849 | case llvm::Triple::NaCl: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4850 | return *(TheTargetCodeGenInfo = |
| 4851 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 4852 | default: |
| 4853 | return *(TheTargetCodeGenInfo = |
| 4854 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 4855 | } |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 4856 | } |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4857 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4858 | case llvm::Triple::ppc: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4859 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | 0fbc4b9 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4860 | case llvm::Triple::ppc64: |
Bill Schmidt | 2fc107f | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4861 | if (Triple.isOSBinFormatELF()) |
| 4862 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
| 4863 | else |
| 4864 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4865 | |
Peter Collingbourne | edb66f3 | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 4866 | case llvm::Triple::nvptx: |
| 4867 | case llvm::Triple::nvptx64: |
Justin Holewinski | 2c585b9 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4868 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4869 | |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 4870 | case llvm::Triple::mblaze: |
| 4871 | return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types)); |
| 4872 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4873 | case llvm::Triple::msp430: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4874 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4875 | |
Peter Collingbourne | 2f7aa99 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 4876 | case llvm::Triple::tce: |
| 4877 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 4878 | |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 4879 | case llvm::Triple::x86: { |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 4880 | if (Triple.isOSDarwin()) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4881 | return *(TheTargetCodeGenInfo = |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 4882 | new X86_32TargetCodeGenInfo(Types, true, true, false, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 4883 | CodeGenOpts.NumRegisterParameters)); |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 4884 | |
| 4885 | switch (Triple.getOS()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 4886 | case llvm::Triple::Cygwin: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 4887 | case llvm::Triple::MinGW32: |
Edward O'Callaghan | 727e268 | 2009-10-21 11:58:24 +0000 | [diff] [blame] | 4888 | case llvm::Triple::AuroraUX: |
| 4889 | case llvm::Triple::DragonFly: |
David Chisnall | 75c135a | 2009-09-03 01:48:05 +0000 | [diff] [blame] | 4890 | case llvm::Triple::FreeBSD: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 4891 | case llvm::Triple::OpenBSD: |
Eli Friedman | 42f74f2 | 2012-08-08 23:57:20 +0000 | [diff] [blame] | 4892 | case llvm::Triple::Bitrig: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4893 | return *(TheTargetCodeGenInfo = |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 4894 | new X86_32TargetCodeGenInfo(Types, false, true, false, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 4895 | CodeGenOpts.NumRegisterParameters)); |
Eli Friedman | 55fc7e2 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 4896 | |
| 4897 | case llvm::Triple::Win32: |
| 4898 | return *(TheTargetCodeGenInfo = |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 4899 | new X86_32TargetCodeGenInfo(Types, false, true, true, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 4900 | CodeGenOpts.NumRegisterParameters)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 4901 | |
| 4902 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4903 | return *(TheTargetCodeGenInfo = |
Chad Rosier | 1f1df1f | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 4904 | new X86_32TargetCodeGenInfo(Types, false, false, false, |
Rafael Espindola | b48280b | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 4905 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4906 | } |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 4907 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4908 | |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 4909 | case llvm::Triple::x86_64: { |
| 4910 | bool HasAVX = strcmp(getContext().getTargetInfo().getABI(), "avx") == 0; |
| 4911 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4912 | switch (Triple.getOS()) { |
| 4913 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 0aa2057 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 4914 | case llvm::Triple::MinGW32: |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4915 | case llvm::Triple::Cygwin: |
| 4916 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Eli Bendersky | 441d9f7 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 4917 | case llvm::Triple::NaCl: |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4918 | return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, HasAVX)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4919 | default: |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 4920 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, |
| 4921 | HasAVX)); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4922 | } |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 4923 | } |
Tony Linthicum | 9631939 | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 4924 | case llvm::Triple::hexagon: |
| 4925 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Eli Friedman | ee1ad99 | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 4926 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4927 | } |