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