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" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 20 | #include "llvm/Type.h" |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Triple.h" |
Daniel Dunbar | 28df7a5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace CodeGen; |
| 26 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 27 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 28 | llvm::Value *Array, |
| 29 | llvm::Value *Value, |
| 30 | unsigned FirstIndex, |
| 31 | unsigned LastIndex) { |
| 32 | // Alternatively, we could emit this as a loop in the source. |
| 33 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| 34 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 35 | Builder.CreateStore(Value, Cell); |
| 36 | } |
| 37 | } |
| 38 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 39 | static bool isAggregateTypeForABI(QualType T) { |
| 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 | |
| 54 | const llvm::TargetData &ABIInfo::getTargetData() const { |
| 55 | return CGT.getTargetData(); |
| 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 | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 101 | bool TargetCodeGenInfo::isNoProtoCallVariadic(CallingConv CC) const { |
| 102 | // The following conventions are known to require this to be false: |
| 103 | // x86_stdcall |
| 104 | // MIPS |
| 105 | // For everything else, we just prefer false unless we opt out. |
| 106 | return false; |
| 107 | } |
| 108 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 109 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 110 | |
| 111 | /// isEmptyField - Return true iff a the field is "empty", that is it |
| 112 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 113 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 114 | bool AllowArrays) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 115 | if (FD->isUnnamedBitfield()) |
| 116 | return true; |
| 117 | |
| 118 | QualType FT = FD->getType(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 119 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 120 | // Constant arrays of empty records count as empty, strip them off. |
| 121 | if (AllowArrays) |
| 122 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) |
| 123 | FT = AT->getElementType(); |
| 124 | |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 125 | const RecordType *RT = FT->getAs<RecordType>(); |
| 126 | if (!RT) |
| 127 | return false; |
| 128 | |
| 129 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 130 | // |
| 131 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 132 | // current ABI. |
| 133 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 134 | return false; |
| 135 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 136 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /// isEmptyRecord - Return true iff a structure contains only empty |
| 140 | /// fields. Note that a structure with a flexible array member is not |
| 141 | /// considered empty. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 142 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 143 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 144 | if (!RT) |
| 145 | return 0; |
| 146 | const RecordDecl *RD = RT->getDecl(); |
| 147 | if (RD->hasFlexibleArrayMember()) |
| 148 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 149 | |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 150 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 151 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Argyrios Kyrtzidis | c5f18f3 | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 152 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 153 | e = CXXRD->bases_end(); i != e; ++i) |
| 154 | if (!isEmptyRecord(Context, i->getType(), true)) |
| 155 | return false; |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 156 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 157 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 158 | i != e; ++i) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 159 | if (!isEmptyField(Context, *i, AllowArrays)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 160 | return false; |
| 161 | return true; |
| 162 | } |
| 163 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 164 | /// hasNonTrivialDestructorOrCopyConstructor - Determine if a type has either |
| 165 | /// a non-trivial destructor or a non-trivial copy constructor. |
| 166 | static bool hasNonTrivialDestructorOrCopyConstructor(const RecordType *RT) { |
| 167 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 168 | if (!RD) |
| 169 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 170 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 171 | return !RD->hasTrivialDestructor() || !RD->hasTrivialCopyConstructor(); |
| 172 | } |
| 173 | |
| 174 | /// isRecordWithNonTrivialDestructorOrCopyConstructor - Determine if a type is |
| 175 | /// a record type with either a non-trivial destructor or a non-trivial copy |
| 176 | /// constructor. |
| 177 | static bool isRecordWithNonTrivialDestructorOrCopyConstructor(QualType T) { |
| 178 | const RecordType *RT = T->getAs<RecordType>(); |
| 179 | if (!RT) |
| 180 | return false; |
| 181 | |
| 182 | return hasNonTrivialDestructorOrCopyConstructor(RT); |
| 183 | } |
| 184 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 185 | /// isSingleElementStruct - Determine if a structure is a "single |
| 186 | /// element struct", i.e. it has exactly one non-empty field or |
| 187 | /// exactly one field which is itself a single element |
| 188 | /// struct. Structures with flexible array members are never |
| 189 | /// considered single element structs. |
| 190 | /// |
| 191 | /// \return The field declaration for the single non-empty field, if |
| 192 | /// it exists. |
| 193 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 194 | const RecordType *RT = T->getAsStructureType(); |
| 195 | if (!RT) |
| 196 | return 0; |
| 197 | |
| 198 | const RecordDecl *RD = RT->getDecl(); |
| 199 | if (RD->hasFlexibleArrayMember()) |
| 200 | return 0; |
| 201 | |
| 202 | const Type *Found = 0; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 204 | // If this is a C++ record, check the bases first. |
| 205 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 206 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 207 | e = CXXRD->bases_end(); i != e; ++i) { |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 208 | // Ignore empty records. |
Daniel Dunbar | 5ea6861 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 209 | if (isEmptyRecord(Context, i->getType(), true)) |
Daniel Dunbar | 9430d5a | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 210 | continue; |
| 211 | |
| 212 | // If we already found an element then this isn't a single-element struct. |
| 213 | if (Found) |
| 214 | return 0; |
| 215 | |
| 216 | // If this is non-empty and not a single element struct, the composite |
| 217 | // cannot be a single element struct. |
| 218 | Found = isSingleElementStruct(i->getType(), Context); |
| 219 | if (!Found) |
| 220 | return 0; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Check for single element. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 225 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 226 | i != e; ++i) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 227 | const FieldDecl *FD = *i; |
| 228 | QualType FT = FD->getType(); |
| 229 | |
| 230 | // Ignore empty fields. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 231 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 232 | continue; |
| 233 | |
| 234 | // If we already found an element then this isn't a single-element |
| 235 | // struct. |
| 236 | if (Found) |
| 237 | return 0; |
| 238 | |
| 239 | // Treat single element arrays as the element. |
| 240 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 241 | if (AT->getSize().getZExtValue() != 1) |
| 242 | break; |
| 243 | FT = AT->getElementType(); |
| 244 | } |
| 245 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 246 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 247 | Found = FT.getTypePtr(); |
| 248 | } else { |
| 249 | Found = isSingleElementStruct(FT, Context); |
| 250 | if (!Found) |
| 251 | return 0; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return Found; |
| 256 | } |
| 257 | |
| 258 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 259 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 260 | !Ty->isAnyComplexType() && !Ty->isEnumeralType() && |
| 261 | !Ty->isBlockPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 262 | return false; |
| 263 | |
| 264 | uint64_t Size = Context.getTypeSize(Ty); |
| 265 | return Size == 32 || Size == 64; |
| 266 | } |
| 267 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 268 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 269 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 270 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 271 | /// inhibiting optimizations. |
| 272 | /// |
| 273 | // FIXME: This predicate is missing many cases, currently it just follows |
| 274 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 275 | // should probably make this smarter, or better yet make the LLVM backend |
| 276 | // capable of handling it. |
| 277 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 278 | // We can only expand structure types. |
| 279 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 280 | if (!RT) |
| 281 | return false; |
| 282 | |
| 283 | // We can only expand (C) structures. |
| 284 | // |
| 285 | // FIXME: This needs to be generalized to handle classes as well. |
| 286 | const RecordDecl *RD = RT->getDecl(); |
| 287 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 288 | return false; |
| 289 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 290 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 291 | i != e; ++i) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 292 | const FieldDecl *FD = *i; |
| 293 | |
| 294 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 295 | return false; |
| 296 | |
| 297 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 298 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 299 | // counts as "basic" is more complicated than what we were doing previously. |
| 300 | if (FD->isBitField()) |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | namespace { |
| 308 | /// DefaultABIInfo - The default implementation for ABI specific |
| 309 | /// details. This implementation provides information which results in |
| 310 | /// self-consistent and sensible LLVM IR generation, but does not |
| 311 | /// conform to any particular ABI. |
| 312 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 313 | public: |
| 314 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 315 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 316 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 317 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 318 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 319 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 320 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 321 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 322 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 323 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 327 | CodeGenFunction &CGF) const; |
| 328 | }; |
| 329 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 330 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 331 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 332 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 333 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 337 | CodeGenFunction &CGF) const { |
| 338 | return 0; |
| 339 | } |
| 340 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 341 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 342 | if (isAggregateTypeForABI(Ty)) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 343 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 344 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 345 | // Treat an enum type as its underlying type. |
| 346 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 347 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 348 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 349 | return (Ty->isPromotableIntegerType() ? |
| 350 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Bob Wilson | 0024f94 | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 353 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 354 | if (RetTy->isVoidType()) |
| 355 | return ABIArgInfo::getIgnore(); |
| 356 | |
| 357 | if (isAggregateTypeForABI(RetTy)) |
| 358 | return ABIArgInfo::getIndirect(0); |
| 359 | |
| 360 | // Treat an enum type as its underlying type. |
| 361 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 362 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 363 | |
| 364 | return (RetTy->isPromotableIntegerType() ? |
| 365 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 366 | } |
| 367 | |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 368 | /// UseX86_MMXType - Return true if this is an MMX type that should use the special |
| 369 | /// x86_mmx type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 370 | bool UseX86_MMXType(llvm::Type *IRType) { |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 371 | // If the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>, use the |
| 372 | // special x86_mmx type. |
| 373 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 374 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 375 | IRType->getScalarSizeInBits() != 64; |
| 376 | } |
| 377 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 378 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 379 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 380 | llvm::Type* Ty) { |
Bill Wendling | 0507be6 | 2011-03-07 22:47:14 +0000 | [diff] [blame] | 381 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 382 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
| 383 | return Ty; |
| 384 | } |
| 385 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 386 | //===----------------------------------------------------------------------===// |
| 387 | // X86-32 ABI Implementation |
| 388 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 389 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 390 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 391 | class X86_32ABIInfo : public ABIInfo { |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 392 | static const unsigned MinABIStackAlignInBytes = 4; |
| 393 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 394 | bool IsDarwinVectorABI; |
| 395 | bool IsSmallStructInRegABI; |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 396 | bool IsMMXDisabled; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 397 | |
| 398 | static bool isRegisterSize(unsigned Size) { |
| 399 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 400 | } |
| 401 | |
| 402 | static bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context); |
| 403 | |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 404 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 405 | /// such that the argument will be passed in memory. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 406 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal = true) const; |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 408 | /// \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] | 409 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 410 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 411 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 412 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 413 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 414 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 415 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 416 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 417 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 418 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 419 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 420 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 424 | CodeGenFunction &CGF) const; |
| 425 | |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 426 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m) |
| 427 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
| 428 | IsMMXDisabled(m) {} |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 429 | }; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 430 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 431 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 432 | public: |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 433 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool m) |
| 434 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, m)) {} |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 435 | |
| 436 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 437 | CodeGen::CodeGenModule &CGM) const; |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 438 | |
| 439 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 440 | // Darwin uses different dwarf register numbers for EH. |
| 441 | if (CGM.isTargetDarwin()) return 5; |
| 442 | |
| 443 | return 4; |
| 444 | } |
| 445 | |
| 446 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 447 | llvm::Value *Address) const; |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 448 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 449 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 450 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 451 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 452 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 453 | } |
| 454 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 455 | }; |
| 456 | |
| 457 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 458 | |
| 459 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 460 | /// passed in a register (for the Darwin ABI). |
| 461 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 462 | ASTContext &Context) { |
| 463 | uint64_t Size = Context.getTypeSize(Ty); |
| 464 | |
| 465 | // Type must be register sized. |
| 466 | if (!isRegisterSize(Size)) |
| 467 | return false; |
| 468 | |
| 469 | if (Ty->isVectorType()) { |
| 470 | // 64- and 128- bit vectors inside structures are not returned in |
| 471 | // registers. |
| 472 | if (Size == 64 || Size == 128) |
| 473 | return false; |
| 474 | |
| 475 | return true; |
| 476 | } |
| 477 | |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 478 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 479 | // member function pointer it is ok. |
Daniel Dunbar | a1842d3 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 480 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | 55e59e1 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 481 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 7711523 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 482 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 483 | return true; |
| 484 | |
| 485 | // Arrays are treated like records. |
| 486 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
| 487 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
| 488 | |
| 489 | // Otherwise, it must be a record type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 490 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 491 | if (!RT) return false; |
| 492 | |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 493 | // FIXME: Traverse bases here too. |
| 494 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 495 | // Structure types are passed in register if all fields would be |
| 496 | // passed in a register. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 497 | for (RecordDecl::field_iterator i = RT->getDecl()->field_begin(), |
| 498 | e = RT->getDecl()->field_end(); i != e; ++i) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 499 | const FieldDecl *FD = *i; |
| 500 | |
| 501 | // Empty fields are ignored. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 502 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 503 | continue; |
| 504 | |
| 505 | // Check fields recursively. |
| 506 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | return true; |
| 511 | } |
| 512 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 513 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy) const { |
| 514 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 515 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 516 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 517 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 518 | // On Darwin, some vectors are returned in registers. |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 519 | if (IsDarwinVectorABI) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 520 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 521 | |
| 522 | // 128-bit vectors are a special case; they are returned in |
| 523 | // registers and we need to make sure to pick a type the LLVM |
| 524 | // backend will like. |
| 525 | if (Size == 128) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 526 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 527 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 528 | |
| 529 | // Always return in register if it fits in a general purpose |
| 530 | // register, or if it is 64 bits and has a single element. |
| 531 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 532 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 533 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 534 | Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 535 | |
| 536 | return ABIArgInfo::getIndirect(0); |
| 537 | } |
| 538 | |
| 539 | return ABIArgInfo::getDirect(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 540 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 541 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 542 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 543 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 544 | // Structures with either a non-trivial destructor or a non-trivial |
| 545 | // copy constructor are always indirect. |
| 546 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 547 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 548 | |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 549 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 550 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 551 | return ABIArgInfo::getIndirect(0); |
Anders Carlsson | 4009297 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 552 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 553 | |
David Chisnall | 1e4249c | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 554 | // If specified, structs and unions are always indirect. |
| 555 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 556 | return ABIArgInfo::getIndirect(0); |
| 557 | |
| 558 | // Classify "single element" structs as their element type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 559 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 560 | if (const BuiltinType *BT = SeltTy->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 561 | if (BT->isIntegerType()) { |
| 562 | // We need to use the size of the structure, padding |
| 563 | // bit-fields can adjust that to be larger than the single |
| 564 | // element type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 565 | uint64_t Size = getContext().getTypeSize(RetTy); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 566 | return ABIArgInfo::getDirect( |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 567 | llvm::IntegerType::get(getVMContext(), (unsigned)Size)); |
| 568 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 569 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 570 | if (BT->getKind() == BuiltinType::Float) { |
| 571 | assert(getContext().getTypeSize(RetTy) == |
| 572 | getContext().getTypeSize(SeltTy) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 573 | "Unexpect single element structure size!"); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 574 | return ABIArgInfo::getDirect(llvm::Type::getFloatTy(getVMContext())); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 575 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 576 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 577 | if (BT->getKind() == BuiltinType::Double) { |
| 578 | assert(getContext().getTypeSize(RetTy) == |
| 579 | getContext().getTypeSize(SeltTy) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 580 | "Unexpect single element structure size!"); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 581 | return ABIArgInfo::getDirect(llvm::Type::getDoubleTy(getVMContext())); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 582 | } |
| 583 | } else if (SeltTy->isPointerType()) { |
| 584 | // FIXME: It would be really nice if this could come out as the proper |
| 585 | // pointer type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 586 | llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(getVMContext()); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 587 | return ABIArgInfo::getDirect(PtrTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 588 | } else if (SeltTy->isVectorType()) { |
| 589 | // 64- and 128-bit vectors are never returned in a |
| 590 | // register when inside a structure. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 591 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 592 | if (Size == 64 || Size == 128) |
| 593 | return ABIArgInfo::getIndirect(0); |
| 594 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 595 | return classifyReturnType(QualType(SeltTy, 0)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| 599 | // Small structures which are register sized are generally returned |
| 600 | // in a register. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 601 | if (X86_32ABIInfo::shouldReturnTypeInRegister(RetTy, getContext())) { |
| 602 | uint64_t Size = getContext().getTypeSize(RetTy); |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 603 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 607 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 608 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 609 | // Treat an enum type as its underlying type. |
| 610 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 611 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 612 | |
| 613 | return (RetTy->isPromotableIntegerType() ? |
| 614 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 617 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 618 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 619 | if (!RT) |
| 620 | return 0; |
| 621 | const RecordDecl *RD = RT->getDecl(); |
| 622 | |
| 623 | // If this is a C++ record, check the bases first. |
| 624 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 625 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 626 | e = CXXRD->bases_end(); i != e; ++i) |
| 627 | if (!isRecordWithSSEVectorType(Context, i->getType())) |
| 628 | return false; |
| 629 | |
| 630 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 631 | i != e; ++i) { |
| 632 | QualType FT = i->getType(); |
| 633 | |
| 634 | if (FT->getAs<VectorType>() && Context.getTypeSize(Ty) == 128) |
| 635 | return true; |
| 636 | |
| 637 | if (isRecordWithSSEVectorType(Context, FT)) |
| 638 | return true; |
| 639 | } |
| 640 | |
| 641 | return false; |
| 642 | } |
| 643 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 644 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 645 | unsigned Align) const { |
| 646 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 647 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 648 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 649 | return 0; // Use default alignment. |
| 650 | |
| 651 | // On non-Darwin, the stack type alignment is always 4. |
| 652 | if (!IsDarwinVectorABI) { |
| 653 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 654 | return MinABIStackAlignInBytes; |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 655 | } |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 656 | |
Daniel Dunbar | 93ae947 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 657 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
| 658 | if (isRecordWithSSEVectorType(getContext(), Ty)) |
| 659 | return 16; |
| 660 | |
| 661 | return MinABIStackAlignInBytes; |
Daniel Dunbar | fb67d6c | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 664 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 665 | if (!ByVal) |
| 666 | return ABIArgInfo::getIndirect(0, false); |
| 667 | |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 668 | // Compute the byval alignment. |
| 669 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 670 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 671 | if (StackAlign == 0) |
Chris Lattner | de92d73 | 2011-05-22 23:35:00 +0000 | [diff] [blame] | 672 | return ABIArgInfo::getIndirect(4); |
Daniel Dunbar | e59d858 | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 673 | |
| 674 | // If the stack alignment is less than the type alignment, realign the |
| 675 | // argument. |
| 676 | if (StackAlign < TypeAlign) |
| 677 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, |
| 678 | /*Realign=*/true); |
| 679 | |
| 680 | return ABIArgInfo::getIndirect(StackAlign); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 683 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 684 | // FIXME: Set alignment on indirect arguments. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 685 | if (isAggregateTypeForABI(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 686 | // Structures with flexible arrays are always indirect. |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 687 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 688 | // Structures with either a non-trivial destructor or a non-trivial |
| 689 | // copy constructor are always indirect. |
| 690 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 691 | return getIndirectResult(Ty, /*ByVal=*/false); |
Daniel Dunbar | dc6d574 | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 692 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 693 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 694 | return getIndirectResult(Ty); |
Anders Carlsson | a887423 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 695 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 696 | |
| 697 | // Ignore empty structs. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 698 | if (Ty->isStructureType() && getContext().getTypeSize(Ty) == 0) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 699 | return ABIArgInfo::getIgnore(); |
| 700 | |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 701 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 702 | // of those arguments will match the struct. This is important because the |
| 703 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 704 | // optimizations. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 705 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 706 | canExpandIndirectArgument(Ty, getContext())) |
Daniel Dunbar | 53012f4 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 707 | return ABIArgInfo::getExpand(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 708 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 709 | return getIndirectResult(Ty); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 712 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 7b73350 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 713 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 714 | // it as an i8/i16/i32/i64. |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 715 | if (IsDarwinVectorABI) { |
| 716 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 717 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 718 | (Size == 64 && VT->getNumElements() == 1)) |
| 719 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 720 | Size)); |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 721 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 722 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 723 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 724 | if (UseX86_MMXType(IRType)) { |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 725 | if (IsMMXDisabled) |
| 726 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 727 | 64)); |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 728 | ABIArgInfo AAI = ABIArgInfo::getDirect(IRType); |
| 729 | AAI.setCoerceToType(llvm::Type::getX86_MMXTy(getVMContext())); |
| 730 | return AAI; |
| 731 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 732 | |
Chris Lattner | bbae8b4 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 733 | return ABIArgInfo::getDirect(); |
| 734 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 735 | |
| 736 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 737 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 738 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 739 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 740 | return (Ty->isPromotableIntegerType() ? |
| 741 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 745 | CodeGenFunction &CGF) const { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 746 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 747 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 748 | |
| 749 | CGBuilderTy &Builder = CGF.Builder; |
| 750 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 751 | "ap"); |
| 752 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 753 | llvm::Type *PTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 754 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 755 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 756 | |
| 757 | uint64_t Offset = |
| 758 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 759 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 760 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 761 | "ap.next"); |
| 762 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 763 | |
| 764 | return AddrTyped; |
| 765 | } |
| 766 | |
Charles Davis | 74f7293 | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 767 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 768 | llvm::GlobalValue *GV, |
| 769 | CodeGen::CodeGenModule &CGM) const { |
| 770 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 771 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 772 | // Get the LLVM function. |
| 773 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 774 | |
| 775 | // Now add the 'alignstack' attribute with a value of 16. |
| 776 | Fn->addFnAttr(llvm::Attribute::constructStackAlignmentFromInt(16)); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 781 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 782 | CodeGen::CodeGenFunction &CGF, |
| 783 | llvm::Value *Address) const { |
| 784 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 785 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 786 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 787 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 788 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 789 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 790 | // 0-7 are the eight integer registers; the order is different |
| 791 | // on Darwin (for EH), but the range is the same. |
| 792 | // 8 is %eip. |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 793 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 794 | |
| 795 | if (CGF.CGM.isTargetDarwin()) { |
| 796 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 797 | // These have size 16, which is sizeof(long double) on |
| 798 | // platforms with 8-byte alignment for that type. |
| 799 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 800 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 801 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 802 | } else { |
| 803 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 804 | // reason. |
| 805 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 806 | |
| 807 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 808 | // These have size 12, which is sizeof(long double) on |
| 809 | // platforms with 4-byte alignment for that type. |
| 810 | llvm::Value *Twelve8 = llvm::ConstantInt::get(i8, 12); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 811 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 812 | } |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 813 | |
| 814 | return false; |
| 815 | } |
| 816 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 817 | //===----------------------------------------------------------------------===// |
| 818 | // X86-64 ABI Implementation |
| 819 | //===----------------------------------------------------------------------===// |
| 820 | |
| 821 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 822 | namespace { |
| 823 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 824 | class X86_64ABIInfo : public ABIInfo { |
| 825 | enum Class { |
| 826 | Integer = 0, |
| 827 | SSE, |
| 828 | SSEUp, |
| 829 | X87, |
| 830 | X87Up, |
| 831 | ComplexX87, |
| 832 | NoClass, |
| 833 | Memory |
| 834 | }; |
| 835 | |
| 836 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 837 | /// |
| 838 | /// Merge an accumulating classification \arg Accum with a field |
| 839 | /// classification \arg Field. |
| 840 | /// |
| 841 | /// \param Accum - The accumulating classification. This should |
| 842 | /// always be either NoClass or the result of a previous merge |
| 843 | /// call. In addition, this should never be Memory (the caller |
| 844 | /// should just return Memory for the aggregate). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 845 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 846 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 847 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 848 | /// |
| 849 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 850 | /// final MEMORY or SSE classes when necessary. |
| 851 | /// |
| 852 | /// \param AggregateSize - The size of the current aggregate in |
| 853 | /// the classification process. |
| 854 | /// |
| 855 | /// \param Lo - The classification for the parts of the type |
| 856 | /// residing in the low word of the containing object. |
| 857 | /// |
| 858 | /// \param Hi - The classification for the parts of the type |
| 859 | /// residing in the higher words of the containing object. |
| 860 | /// |
| 861 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 862 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 863 | /// classify - Determine the x86_64 register classes in which the |
| 864 | /// given type T should be passed. |
| 865 | /// |
| 866 | /// \param Lo - The classification for the parts of the type |
| 867 | /// residing in the low word of the containing object. |
| 868 | /// |
| 869 | /// \param Hi - The classification for the parts of the type |
| 870 | /// residing in the high word of the containing object. |
| 871 | /// |
| 872 | /// \param OffsetBase - The bit offset of this type in the |
| 873 | /// containing object. Some parameters are classified different |
| 874 | /// depending on whether they straddle an eightbyte boundary. |
| 875 | /// |
| 876 | /// If a word is unused its result will be NoClass; if a type should |
| 877 | /// be passed in Memory then at least the classification of \arg Lo |
| 878 | /// will be Memory. |
| 879 | /// |
| 880 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
| 881 | /// |
| 882 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 883 | /// also be ComplexX87. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 884 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 885 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 886 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 887 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 888 | unsigned IROffset, QualType SourceTy, |
| 889 | unsigned SourceOffset) const; |
| 890 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 891 | unsigned IROffset, QualType SourceTy, |
| 892 | unsigned SourceOffset) const; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 893 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 894 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 895 | /// such that the argument will be returned in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 896 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 897 | |
| 898 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 899 | /// such that the argument will be passed in memory. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 900 | ABIArgInfo getIndirectResult(QualType Ty) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 901 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 902 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 903 | |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 904 | ABIArgInfo classifyArgumentType(QualType Ty, |
| 905 | unsigned &neededInt, |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 906 | unsigned &neededSSE) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 907 | |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 908 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 909 | /// unfortunately in ways that were not always consistent with |
| 910 | /// certain previous compilers. In particular, platforms which |
| 911 | /// required strict binary compatibility with older versions of GCC |
| 912 | /// may need to exempt themselves. |
| 913 | bool honorsRevision0_98() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 914 | return !getContext().getTargetInfo().getTriple().isOSDarwin(); |
John McCall | 67a5773 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 917 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 918 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 919 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 920 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 921 | |
| 922 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 923 | CodeGenFunction &CGF) const; |
| 924 | }; |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 925 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 926 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 927 | class WinX86_64ABIInfo : public ABIInfo { |
| 928 | |
| 929 | ABIArgInfo classify(QualType Ty) const; |
| 930 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 931 | public: |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 932 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 933 | |
| 934 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 935 | |
| 936 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 937 | CodeGenFunction &CGF) const; |
| 938 | }; |
| 939 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 940 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 941 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 942 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 943 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 944 | |
| 945 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 946 | return 7; |
| 947 | } |
| 948 | |
| 949 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 950 | llvm::Value *Address) const { |
| 951 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 952 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 953 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 954 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 955 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 956 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 957 | // 0-15 are the 16 integer registers. |
| 958 | // 16 is %rip. |
| 959 | AssignToArrayRange(Builder, Address, Eight8, 0, 16); |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 960 | |
| 961 | return false; |
| 962 | } |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 963 | |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 964 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 965 | StringRef Constraint, |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 966 | llvm::Type* Ty) const { |
Peter Collingbourne | 4b93d66 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 967 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 968 | } |
| 969 | |
John McCall | 01f151e | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 970 | bool isNoProtoCallVariadic(CallingConv CC) const { |
| 971 | // The default CC on x86-64 sets %al to the number of SSA |
| 972 | // registers used, and GCC sets this when calling an unprototyped |
| 973 | // function, so we override the default behavior. |
| 974 | if (CC == CC_Default || CC == CC_C) return true; |
| 975 | |
| 976 | return TargetCodeGenInfo::isNoProtoCallVariadic(CC); |
| 977 | } |
| 978 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 981 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 982 | public: |
| 983 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 984 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 985 | |
| 986 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 987 | return 7; |
| 988 | } |
| 989 | |
| 990 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 991 | llvm::Value *Address) const { |
| 992 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 993 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 994 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 995 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 996 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 997 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 998 | // 0-15 are the 16 integer registers. |
| 999 | // 16 is %rip. |
| 1000 | AssignToArrayRange(Builder, Address, Eight8, 0, 16); |
| 1001 | |
| 1002 | return false; |
| 1003 | } |
| 1004 | }; |
| 1005 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1008 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1009 | Class &Hi) const { |
| 1010 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1011 | // |
| 1012 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1013 | // memory. |
| 1014 | // |
| 1015 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1016 | // memory. |
| 1017 | // |
| 1018 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1019 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1020 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1021 | // ABI working for processors that don't support the __m256 type. |
| 1022 | // |
| 1023 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1024 | // |
| 1025 | // Some of these are enforced by the merging logic. Others can arise |
| 1026 | // only with unions; for example: |
| 1027 | // union { _Complex double; unsigned; } |
| 1028 | // |
| 1029 | // Note that clauses (b) and (c) were added in 0.98. |
| 1030 | // |
| 1031 | if (Hi == Memory) |
| 1032 | Lo = Memory; |
| 1033 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1034 | Lo = Memory; |
| 1035 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1036 | Lo = Memory; |
| 1037 | if (Hi == SSEUp && Lo != SSE) |
| 1038 | Hi = SSE; |
| 1039 | } |
| 1040 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1041 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1042 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1043 | // classified recursively so that always two fields are |
| 1044 | // considered. The resulting class is calculated according to |
| 1045 | // the classes of the fields in the eightbyte: |
| 1046 | // |
| 1047 | // (a) If both classes are equal, this is the resulting class. |
| 1048 | // |
| 1049 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1050 | // the other class. |
| 1051 | // |
| 1052 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1053 | // class. |
| 1054 | // |
| 1055 | // (d) If one of the classes is INTEGER, the result is the |
| 1056 | // INTEGER. |
| 1057 | // |
| 1058 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1059 | // MEMORY is used as class. |
| 1060 | // |
| 1061 | // (f) Otherwise class SSE is used. |
| 1062 | |
| 1063 | // Accum should never be memory (we should have returned) or |
| 1064 | // ComplexX87 (because this cannot be passed in a structure). |
| 1065 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1066 | "Invalid accumulated classification during merge."); |
| 1067 | if (Accum == Field || Field == NoClass) |
| 1068 | return Accum; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1069 | if (Field == Memory) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1070 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1071 | if (Accum == NoClass) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1072 | return Field; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1073 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1074 | return Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1075 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1076 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1077 | return Memory; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1078 | return SSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Chris Lattner | bcaedae | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1081 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1082 | Class &Lo, Class &Hi) const { |
| 1083 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1084 | // Class pairs with appropriate constructor methods for the various |
| 1085 | // situations. |
| 1086 | |
| 1087 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1088 | // shouldn't be passed in registers for example, so there is no chance they |
| 1089 | // can straddle an eightbyte. Verify & simplify. |
| 1090 | |
| 1091 | Lo = Hi = NoClass; |
| 1092 | |
| 1093 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1094 | Current = Memory; |
| 1095 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1096 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1097 | BuiltinType::Kind k = BT->getKind(); |
| 1098 | |
| 1099 | if (k == BuiltinType::Void) { |
| 1100 | Current = NoClass; |
| 1101 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1102 | Lo = Integer; |
| 1103 | Hi = Integer; |
| 1104 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1105 | Current = Integer; |
| 1106 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
| 1107 | Current = SSE; |
| 1108 | } else if (k == BuiltinType::LongDouble) { |
| 1109 | Lo = X87; |
| 1110 | Hi = X87Up; |
| 1111 | } |
| 1112 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1113 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1114 | return; |
| 1115 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1117 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1118 | // Classify the underlying integer type. |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1119 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1120 | return; |
| 1121 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1123 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1124 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1128 | if (Ty->isMemberPointerType()) { |
Daniel Dunbar | 67d438d | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1129 | if (Ty->isMemberFunctionPointerType()) |
| 1130 | Lo = Hi = Integer; |
| 1131 | else |
| 1132 | Current = Integer; |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1136 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1137 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1138 | if (Size == 32) { |
| 1139 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1140 | // float> as integer. |
| 1141 | Current = Integer; |
| 1142 | |
| 1143 | // If this type crosses an eightbyte boundary, it should be |
| 1144 | // split. |
| 1145 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1146 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1147 | if (EB_Real != EB_Imag) |
| 1148 | Hi = Lo; |
| 1149 | } else if (Size == 64) { |
| 1150 | // gcc passes <1 x double> in memory. :( |
| 1151 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1152 | return; |
| 1153 | |
| 1154 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 473f8e7 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1155 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 0fefa41 | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1156 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1157 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1158 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1159 | Current = Integer; |
| 1160 | else |
| 1161 | Current = SSE; |
| 1162 | |
| 1163 | // If this type crosses an eightbyte boundary, it should be |
| 1164 | // split. |
| 1165 | if (OffsetBase && OffsetBase != 64) |
| 1166 | Hi = Lo; |
Bruno Cardoso Lopes | 75d28b5 | 2011-07-12 02:47:38 +0000 | [diff] [blame] | 1167 | } else if (Size == 128 || Size == 256) { |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1168 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1169 | // least significant one belongs to class SSE and all the others to class |
| 1170 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1171 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1172 | // This design isn't correct for 256-bits, but since there're no cases |
| 1173 | // where the upper parts would need to be inspected, avoid adding |
| 1174 | // complexity and just consider Hi to match the 64-256 part. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1175 | Lo = SSE; |
| 1176 | Hi = SSEUp; |
| 1177 | } |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1178 | return; |
| 1179 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1180 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1181 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1182 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1184 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1185 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1186 | if (Size <= 64) |
| 1187 | Current = Integer; |
| 1188 | else if (Size <= 128) |
| 1189 | Lo = Hi = Integer; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1190 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1191 | Current = SSE; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1192 | else if (ET == getContext().DoubleTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1193 | Lo = Hi = SSE; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1194 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1195 | Current = ComplexX87; |
| 1196 | |
| 1197 | // If this complex type crosses an eightbyte boundary then it |
| 1198 | // should be split. |
| 1199 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1200 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1201 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1202 | Hi = Lo; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1204 | return; |
| 1205 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1207 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1208 | // Arrays are treated like structures. |
| 1209 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1210 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1211 | |
| 1212 | // 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] | 1213 | // than four eightbytes, ..., it has class MEMORY. |
| 1214 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1215 | return; |
| 1216 | |
| 1217 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1218 | // fields, it has class MEMORY. |
| 1219 | // |
| 1220 | // Only need to check alignment of array base. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1221 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1222 | return; |
| 1223 | |
| 1224 | // Otherwise implement simplified merge. We could be smarter about |
| 1225 | // this, but it isn't worth it and would be harder to verify. |
| 1226 | Current = NoClass; |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1227 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1228 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 089d892 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1229 | |
| 1230 | // The only case a 256-bit wide vector could be used is when the array |
| 1231 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1232 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1233 | if (Size > 128 && EltSize != 256) |
| 1234 | return; |
| 1235 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1236 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1237 | Class FieldLo, FieldHi; |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1238 | classify(AT->getElementType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1239 | Lo = merge(Lo, FieldLo); |
| 1240 | Hi = merge(Hi, FieldHi); |
| 1241 | if (Lo == Memory || Hi == Memory) |
| 1242 | break; |
| 1243 | } |
| 1244 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1245 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1246 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1247 | return; |
| 1248 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1250 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1251 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1252 | |
| 1253 | // 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] | 1254 | // than four eightbytes, ..., it has class MEMORY. |
| 1255 | if (Size > 256) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1256 | return; |
| 1257 | |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1258 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1259 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1260 | // reference. |
| 1261 | if (hasNonTrivialDestructorOrCopyConstructor(RT)) |
| 1262 | return; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1263 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1264 | const RecordDecl *RD = RT->getDecl(); |
| 1265 | |
| 1266 | // Assume variable sized types are passed in memory. |
| 1267 | if (RD->hasFlexibleArrayMember()) |
| 1268 | return; |
| 1269 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1270 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1271 | |
| 1272 | // Reset Lo class, this will be recomputed. |
| 1273 | Current = NoClass; |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1274 | |
| 1275 | // If this is a C++ record, classify the bases first. |
| 1276 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1277 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1278 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1279 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1280 | "Unexpected base class!"); |
| 1281 | const CXXRecordDecl *Base = |
| 1282 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 1283 | |
| 1284 | // Classify this field. |
| 1285 | // |
| 1286 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1287 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1288 | // initialized to class NO_CLASS. |
| 1289 | Class FieldLo, FieldHi; |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1290 | uint64_t Offset = OffsetBase + Layout.getBaseClassOffsetInBits(Base); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1291 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Daniel Dunbar | ce9f423 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1292 | Lo = merge(Lo, FieldLo); |
| 1293 | Hi = merge(Hi, FieldHi); |
| 1294 | if (Lo == Memory || Hi == Memory) |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1300 | unsigned idx = 0; |
Bruno Cardoso Lopes | 548e478 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1301 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1302 | i != e; ++i, ++idx) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1303 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1304 | bool BitField = i->isBitField(); |
| 1305 | |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1306 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1307 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1308 | // |
Bruno Cardoso Lopes | b8981df | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1309 | // The only case a 256-bit wide vector could be used is when the struct |
| 1310 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1311 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1312 | // |
| 1313 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1314 | Lo = Memory; |
| 1315 | return; |
| 1316 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1317 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1318 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1319 | Lo = Memory; |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | // Classify this field. |
| 1324 | // |
| 1325 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1326 | // exceeds a single eightbyte, each is classified |
| 1327 | // separately. Each eightbyte gets initialized to class |
| 1328 | // NO_CLASS. |
| 1329 | Class FieldLo, FieldHi; |
| 1330 | |
| 1331 | // Bit-fields require special handling, they do not force the |
| 1332 | // structure to be passed in memory even if unaligned, and |
| 1333 | // therefore they can straddle an eightbyte. |
| 1334 | if (BitField) { |
| 1335 | // Ignore padding bit-fields. |
| 1336 | if (i->isUnnamedBitfield()) |
| 1337 | continue; |
| 1338 | |
| 1339 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1340 | uint64_t Size = |
| 1341 | i->getBitWidth()->EvaluateAsInt(getContext()).getZExtValue(); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1342 | |
| 1343 | uint64_t EB_Lo = Offset / 64; |
| 1344 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
| 1345 | FieldLo = FieldHi = NoClass; |
| 1346 | if (EB_Lo) { |
| 1347 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1348 | FieldLo = NoClass; |
| 1349 | FieldHi = Integer; |
| 1350 | } else { |
| 1351 | FieldLo = Integer; |
| 1352 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1353 | } |
| 1354 | } else |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1355 | classify(i->getType(), Offset, FieldLo, FieldHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1356 | Lo = merge(Lo, FieldLo); |
| 1357 | Hi = merge(Hi, FieldHi); |
| 1358 | if (Lo == Memory || Hi == Memory) |
| 1359 | break; |
| 1360 | } |
| 1361 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1362 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1366 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1367 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1368 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1369 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1370 | // Treat an enum type as its underlying type. |
| 1371 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1372 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1373 | |
| 1374 | return (Ty->isPromotableIntegerType() ? |
| 1375 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1376 | } |
| 1377 | |
| 1378 | return ABIArgInfo::getIndirect(0); |
| 1379 | } |
| 1380 | |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1381 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1382 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1383 | // place naturally. |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1384 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1385 | // Treat an enum type as its underlying type. |
| 1386 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1387 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1388 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1389 | return (Ty->isPromotableIntegerType() ? |
| 1390 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1391 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1392 | |
Daniel Dunbar | 46c54fb | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1393 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 1394 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Anders Carlsson | 0a8f847 | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1395 | |
Chris Lattner | 855d227 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1396 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1397 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1398 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
| 1399 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1402 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1403 | /// 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] | 1404 | /// vector register. |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1405 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1406 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1407 | |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1408 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1409 | // strip them off if present. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1410 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 15842bd | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1411 | while (STy && STy->getNumElements() == 1) { |
| 1412 | IRType = STy->getElementType(0); |
| 1413 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1414 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1415 | |
Bruno Cardoso Lopes | 528a8c7 | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1416 | // 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] | 1417 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1418 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1419 | unsigned BitWidth = VT->getBitWidth(); |
| 1420 | if ((BitWidth == 128 || BitWidth == 256) && |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1421 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1422 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1423 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1424 | EltTy->isIntegerTy(128))) |
| 1425 | return VT; |
| 1426 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1427 | |
Chris Lattner | 0f408f5 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1428 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1429 | } |
| 1430 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1431 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1432 | /// is known to either be off the end of the specified type or being in |
| 1433 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1434 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1435 | /// classification that put one of the two halves in the INTEGER class. |
| 1436 | /// |
| 1437 | /// It is conservatively correct to return false. |
| 1438 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1439 | unsigned EndBit, ASTContext &Context) { |
| 1440 | // If the bytes being queried are off the end of the type, there is no user |
| 1441 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1442 | // types that don't contain interesting padding. |
| 1443 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1444 | if (TySize <= StartBit) |
| 1445 | return true; |
| 1446 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1447 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1448 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1449 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1450 | |
| 1451 | // Check each element to see if the element overlaps with the queried range. |
| 1452 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1453 | // If the element is after the span we care about, then we're done.. |
| 1454 | unsigned EltOffset = i*EltSize; |
| 1455 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1456 | |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1457 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1458 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1459 | EndBit-EltOffset, Context)) |
| 1460 | return false; |
| 1461 | } |
| 1462 | // If it overlaps no elements, then it is safe to process as padding. |
| 1463 | return true; |
| 1464 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1466 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1467 | const RecordDecl *RD = RT->getDecl(); |
| 1468 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1469 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1470 | // If this is a C++ record, check the bases first. |
| 1471 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 1472 | for (CXXRecordDecl::base_class_const_iterator i = CXXRD->bases_begin(), |
| 1473 | e = CXXRD->bases_end(); i != e; ++i) { |
| 1474 | assert(!i->isVirtual() && !i->getType()->isDependentType() && |
| 1475 | "Unexpected base class!"); |
| 1476 | const CXXRecordDecl *Base = |
| 1477 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1478 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1479 | // If the base is after the span we care about, ignore it. |
Anders Carlsson | a14f597 | 2010-10-31 23:22:37 +0000 | [diff] [blame] | 1480 | unsigned BaseOffset = (unsigned)Layout.getBaseClassOffsetInBits(Base); |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1481 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1482 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1483 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
| 1484 | if (!BitsContainNoUserData(i->getType(), BaseStart, |
| 1485 | EndBit-BaseOffset, Context)) |
| 1486 | return false; |
| 1487 | } |
| 1488 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1489 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1490 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1491 | // this could be sped up a lot by being smarter about queried fields, |
| 1492 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 1493 | // much. |
| 1494 | unsigned idx = 0; |
| 1495 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 1496 | i != e; ++i, ++idx) { |
| 1497 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1499 | // If we found a field after the region we care about, then we're done. |
| 1500 | if (FieldOffset >= EndBit) break; |
| 1501 | |
| 1502 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 1503 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 1504 | Context)) |
| 1505 | return false; |
| 1506 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1508 | // If nothing in this record overlapped the area of interest, then we're |
| 1509 | // clean. |
| 1510 | return true; |
| 1511 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1513 | return false; |
| 1514 | } |
| 1515 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1516 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 1517 | /// float member at the specified offset. For example, {int,{float}} has a |
| 1518 | /// float at offset 4. It is conservatively correct for this routine to return |
| 1519 | /// false. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1520 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1521 | const llvm::TargetData &TD) { |
| 1522 | // Base case if we find a float. |
| 1523 | if (IROffset == 0 && IRType->isFloatTy()) |
| 1524 | return true; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1525 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1526 | // 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] | 1527 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1528 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 1529 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 1530 | IROffset -= SL->getElementOffset(Elt); |
| 1531 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 1532 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1533 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1534 | // 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] | 1535 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 1536 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1537 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 1538 | IROffset -= IROffset/EltSize*EltSize; |
| 1539 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 1540 | } |
| 1541 | |
| 1542 | return false; |
| 1543 | } |
| 1544 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1545 | |
| 1546 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 1547 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1548 | llvm::Type *X86_64ABIInfo:: |
| 1549 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1550 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | cba8d31 | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 1551 | // 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] | 1552 | // pass as float if the last 4 bytes is just padding. This happens for |
| 1553 | // structs that contain 3 floats. |
| 1554 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 1555 | SourceOffset*8+64, getContext())) |
| 1556 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1557 | |
Chris Lattner | 0b36200 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 1558 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 1559 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 1560 | // case. |
| 1561 | if (ContainsFloatAtOffset(IRType, IROffset, getTargetData()) && |
Chris Lattner | 22fd4ba | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 1562 | ContainsFloatAtOffset(IRType, IROffset+4, getTargetData())) |
| 1563 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1564 | |
Chris Lattner | f47c944 | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 1565 | return llvm::Type::getDoubleTy(getVMContext()); |
| 1566 | } |
| 1567 | |
| 1568 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1569 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 1570 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 1571 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 1572 | /// 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] | 1573 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 1574 | /// etc). |
| 1575 | /// |
| 1576 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 1577 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 1578 | /// the 8-byte value references. PrefType may be null. |
| 1579 | /// |
| 1580 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 1581 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 1582 | /// |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1583 | llvm::Type *X86_64ABIInfo:: |
| 1584 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1585 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1586 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 1587 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 1588 | if (IROffset == 0) { |
| 1589 | // Pointers and int64's always fill the 8-byte unit. |
| 1590 | if (isa<llvm::PointerType>(IRType) || IRType->isIntegerTy(64)) |
| 1591 | return IRType; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1592 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1593 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 1594 | // goodness in the source type is just tail padding. This is allowed to |
| 1595 | // kick in for struct {double,int} on the int, but not on |
| 1596 | // struct{double,int,int} because we wouldn't return the second int. We |
| 1597 | // have to do this analysis on the source type because we can't depend on |
| 1598 | // unions being lowered a specific way etc. |
| 1599 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
| 1600 | IRType->isIntegerTy(32)) { |
| 1601 | unsigned BitWidth = cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1602 | |
Chris Lattner | e2962be | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1603 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 1604 | SourceOffset*8+64, getContext())) |
| 1605 | return IRType; |
| 1606 | } |
| 1607 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1608 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1609 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1610 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 44f0fd2 | 2010-07-29 02:20:19 +0000 | [diff] [blame] | 1611 | const llvm::StructLayout *SL = getTargetData().getStructLayout(STy); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1612 | if (IROffset < SL->getSizeInBytes()) { |
| 1613 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 1614 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1615 | |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1616 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 1617 | SourceTy, SourceOffset); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1618 | } |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1619 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1620 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1621 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1622 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1623 | unsigned EltSize = getTargetData().getTypeAllocSize(EltTy); |
| 1624 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 0d2656d | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 1625 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 1626 | SourceOffset); |
Chris Lattner | 021c3a3 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1627 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1628 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1629 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 1630 | // 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] | 1631 | unsigned TySizeInBytes = |
| 1632 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1633 | |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1634 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1636 | // It is always safe to classify this as an integer type up to i64 that |
| 1637 | // isn't larger than the structure. |
Chris Lattner | 9e45a3d | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 1638 | return llvm::IntegerType::get(getVMContext(), |
| 1639 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1642 | |
| 1643 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 1644 | /// be used as elements of a two register pair to pass or return, return a |
| 1645 | /// first class aggregate to represent them. For example, if the low part of |
| 1646 | /// a by-value argument should be passed as i32* and the high part as float, |
| 1647 | /// return {i32*, float}. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1648 | static llvm::Type * |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1649 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1650 | const llvm::TargetData &TD) { |
| 1651 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 1652 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 1653 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 1654 | // the second element at offset 8. Check for this: |
| 1655 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 1656 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
| 1657 | unsigned HiStart = llvm::TargetData::RoundUpAlignment(LoSize, HiAlign); |
| 1658 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1659 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1660 | // To handle this, we have to increase the size of the low part so that the |
| 1661 | // second element will start at an 8 byte offset. We can't increase the size |
| 1662 | // of the second element because it might make us access off the end of the |
| 1663 | // struct. |
| 1664 | if (HiStart != 8) { |
| 1665 | // There are only two sorts of types the ABI generation code can produce for |
| 1666 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 1667 | // Promote these to a larger type. |
| 1668 | if (Lo->isFloatTy()) |
| 1669 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 1670 | else { |
| 1671 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 1672 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 1673 | } |
| 1674 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1676 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1677 | |
| 1678 | |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1679 | // Verify that the second element is at an 8-byte offset. |
| 1680 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 1681 | "Invalid x86-64 argument pair!"); |
| 1682 | return Result; |
| 1683 | } |
| 1684 | |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1685 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1686 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1687 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 1688 | // classification algorithm. |
| 1689 | X86_64ABIInfo::Class Lo, Hi; |
| 1690 | classify(RetTy, 0, Lo, Hi); |
| 1691 | |
| 1692 | // Check some invariants. |
| 1693 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1694 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 1695 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1696 | llvm::Type *ResType = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1697 | switch (Lo) { |
| 1698 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1699 | if (Hi == NoClass) |
| 1700 | return ABIArgInfo::getIgnore(); |
| 1701 | // If the low part is just padding, it takes no register, leave ResType |
| 1702 | // null. |
| 1703 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 1704 | "Unknown missing lo part"); |
| 1705 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1706 | |
| 1707 | case SSEUp: |
| 1708 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1709 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1710 | |
| 1711 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 1712 | // hidden argument. |
| 1713 | case Memory: |
| 1714 | return getIndirectReturnResult(RetTy); |
| 1715 | |
| 1716 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 1717 | // available register of the sequence %rax, %rdx is used. |
| 1718 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1719 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1721 | // If we have a sign or zero extended integer, make sure to return Extend |
| 1722 | // so that the parameter gets the right LLVM IR attributes. |
| 1723 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 1724 | // Treat an enum type as its underlying type. |
| 1725 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1726 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1727 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1728 | if (RetTy->isIntegralOrEnumerationType() && |
| 1729 | RetTy->isPromotableIntegerType()) |
| 1730 | return ABIArgInfo::getExtend(); |
| 1731 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1732 | break; |
| 1733 | |
| 1734 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 1735 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 1736 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1737 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1738 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1739 | |
| 1740 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 1741 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 1742 | case X87: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1743 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1744 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1745 | |
| 1746 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 1747 | // part of the value is returned in %st0 and the imaginary part in |
| 1748 | // %st1. |
| 1749 | case ComplexX87: |
| 1750 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1751 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1752 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1753 | NULL); |
| 1754 | break; |
| 1755 | } |
| 1756 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1757 | llvm::Type *HighPart = 0; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1758 | switch (Hi) { |
| 1759 | // Memory was handled previously and X87 should |
| 1760 | // never occur as a hi class. |
| 1761 | case Memory: |
| 1762 | case X87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1763 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1764 | |
| 1765 | case ComplexX87: // Previously handled. |
Chris Lattner | 0b30c67 | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 1766 | case NoClass: |
| 1767 | break; |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1768 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1769 | case Integer: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1770 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1771 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1772 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1773 | break; |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1774 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1775 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1776 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1777 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1778 | break; |
| 1779 | |
| 1780 | // 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] | 1781 | // is passed in the next available eightbyte chunk if the last used |
| 1782 | // vector register. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1783 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1784 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1785 | case SSEUp: |
| 1786 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1787 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | |
| 1790 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 1791 | // returned together with the previous X87 value in %st0. |
| 1792 | case X87Up: |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1793 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1794 | // anything. However, in some cases with unions it may not be |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1795 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1796 | // extra bits in an SSE reg. |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 1797 | if (Lo != X87) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1798 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1799 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 1800 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 603519d | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 1801 | } |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1802 | break; |
| 1803 | } |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1804 | |
Chris Lattner | 3db4dde | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 1805 | // 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] | 1806 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 1807 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1808 | if (HighPart) |
| 1809 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1810 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1811 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 519f68c | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1814 | ABIArgInfo X86_64ABIInfo::classifyArgumentType(QualType Ty, unsigned &neededInt, |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1815 | unsigned &neededSSE) const { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1816 | X86_64ABIInfo::Class Lo, Hi; |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1817 | classify(Ty, 0, Lo, Hi); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1818 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1819 | // Check some invariants. |
| 1820 | // FIXME: Enforce these by construction. |
| 1821 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1822 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 1823 | |
| 1824 | neededInt = 0; |
| 1825 | neededSSE = 0; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1826 | llvm::Type *ResType = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1827 | switch (Lo) { |
| 1828 | case NoClass: |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1829 | if (Hi == NoClass) |
| 1830 | return ABIArgInfo::getIgnore(); |
| 1831 | // If the low part is just padding, it takes no register, leave ResType |
| 1832 | // null. |
| 1833 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 1834 | "Unknown missing lo part"); |
| 1835 | break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1836 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1837 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 1838 | // on the stack. |
| 1839 | case Memory: |
| 1840 | |
| 1841 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 1842 | // COMPLEX_X87, it is passed in memory. |
| 1843 | case X87: |
| 1844 | case ComplexX87: |
Eli Friedman | ded137f | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 1845 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 1846 | ++neededInt; |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1847 | return getIndirectResult(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1848 | |
| 1849 | case SSEUp: |
| 1850 | case X87Up: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1851 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1852 | |
| 1853 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 1854 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 1855 | // and %r9 is used. |
| 1856 | case Integer: |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1857 | ++neededInt; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1859 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1860 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1861 | |
| 1862 | // If we have a sign or zero extended integer, make sure to return Extend |
| 1863 | // so that the parameter gets the right LLVM IR attributes. |
| 1864 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 1865 | // Treat an enum type as its underlying type. |
| 1866 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1867 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1868 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1869 | if (Ty->isIntegralOrEnumerationType() && |
| 1870 | Ty->isPromotableIntegerType()) |
| 1871 | return ABIArgInfo::getExtend(); |
| 1872 | } |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1873 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1874 | break; |
| 1875 | |
| 1876 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 1877 | // available SSE register is used, the registers are taken in the |
| 1878 | // order from %xmm0 to %xmm7. |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1879 | case SSE: { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1880 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 14508ff | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 1881 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1882 | ++neededSSE; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1883 | break; |
| 1884 | } |
Bill Wendling | bb465d7 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1885 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1887 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1888 | switch (Hi) { |
| 1889 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 1890 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1891 | // which is passed in memory. |
| 1892 | case Memory: |
| 1893 | case X87: |
| 1894 | case ComplexX87: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1895 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1896 | |
| 1897 | case NoClass: break; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1898 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1899 | case Integer: |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1900 | ++neededInt; |
Chris Lattner | 49382de | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 1901 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1902 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1904 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 1905 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | |
| 1908 | // X87Up generally doesn't occur here (long double is passed in |
| 1909 | // memory), except in situations involving unions. |
| 1910 | case X87Up: |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1911 | case SSE: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1912 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1913 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1914 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 1915 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 117e3f4 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 1916 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1917 | ++neededSSE; |
| 1918 | break; |
| 1919 | |
| 1920 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 1921 | // 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] | 1922 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1923 | case SSEUp: |
Chris Lattner | ab5722e | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 1924 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 4943c15 | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1925 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1926 | break; |
| 1927 | } |
| 1928 | |
Chris Lattner | 645406a | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 1929 | // If a high part was specified, merge it together with the low part. It is |
| 1930 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 1931 | // first class struct aggregate with the high and low part: {low, high} |
| 1932 | if (HighPart) |
Chris Lattner | 66e7b68 | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 1933 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getTargetData()); |
Michael J. Spencer | 9cac494 | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1934 | |
Chris Lattner | eb518b4 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 1935 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 1938 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1939 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1940 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1941 | |
| 1942 | // Keep track of the number of assigned registers. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1943 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1944 | |
| 1945 | // If the return value is indirect, then the hidden argument is consuming one |
| 1946 | // integer register. |
| 1947 | if (FI.getReturnInfo().isIndirect()) |
| 1948 | --freeIntRegs; |
| 1949 | |
| 1950 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 1951 | // get assigned (in left-to-right order) for passing as follows... |
| 1952 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 1953 | it != ie; ++it) { |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1954 | unsigned neededInt, neededSSE; |
| 1955 | it->info = classifyArgumentType(it->type, neededInt, neededSSE); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1956 | |
| 1957 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 1958 | // eightbyte of an argument, the whole argument is passed on the |
| 1959 | // stack. If registers have already been assigned for some |
| 1960 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 1961 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1962 | freeIntRegs -= neededInt; |
| 1963 | freeSSERegs -= neededSSE; |
| 1964 | } else { |
Chris Lattner | 9c254f0 | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1965 | it->info = getIndirectResult(it->type); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 1971 | QualType Ty, |
| 1972 | CodeGenFunction &CGF) { |
| 1973 | llvm::Value *overflow_arg_area_p = |
| 1974 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 1975 | llvm::Value *overflow_arg_area = |
| 1976 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 1977 | |
| 1978 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 1979 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
| 1980 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 1981 | if (Align > 8) { |
| 1982 | // Note that we follow the ABI & gcc here, even though the type |
| 1983 | // could in theory have an alignment greater than 16. This case |
| 1984 | // shouldn't ever matter in practice. |
| 1985 | |
| 1986 | // overflow_arg_area = (overflow_arg_area + 15) & ~15; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1987 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1988 | llvm::ConstantInt::get(CGF.Int32Ty, 15); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1989 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 1990 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1991 | CGF.Int64Ty); |
| 1992 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~15LL); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1993 | overflow_arg_area = |
| 1994 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1995 | overflow_arg_area->getType(), |
| 1996 | "overflow_arg_area.align"); |
| 1997 | } |
| 1998 | |
| 1999 | // 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] | 2000 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2001 | llvm::Value *Res = |
| 2002 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2003 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2004 | |
| 2005 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2006 | // l->overflow_arg_area + sizeof(type). |
| 2007 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2008 | // an 8 byte boundary. |
| 2009 | |
| 2010 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2011 | llvm::Value *Offset = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2012 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2013 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2014 | "overflow_arg_area.next"); |
| 2015 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2016 | |
| 2017 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2018 | return Res; |
| 2019 | } |
| 2020 | |
| 2021 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2022 | CodeGenFunction &CGF) const { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2023 | llvm::LLVMContext &VMContext = CGF.getLLVMContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2024 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2025 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2026 | // struct { |
| 2027 | // i32 gp_offset; |
| 2028 | // i32 fp_offset; |
| 2029 | // i8* overflow_arg_area; |
| 2030 | // i8* reg_save_area; |
| 2031 | // }; |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2032 | unsigned neededInt, neededSSE; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2033 | |
Chris Lattner | a14db75 | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2034 | Ty = CGF.getContext().getCanonicalType(Ty); |
Bill Wendling | 99aaae8 | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2035 | ABIArgInfo AI = classifyArgumentType(Ty, neededInt, neededSSE); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2036 | |
| 2037 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2038 | // in the registers. If not go to step 7. |
| 2039 | if (!neededInt && !neededSSE) |
| 2040 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2041 | |
| 2042 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2043 | // general purpose registers needed to pass type and num_fp to hold |
| 2044 | // the number of floating point registers needed. |
| 2045 | |
| 2046 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2047 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2048 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2049 | // |
| 2050 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2051 | // register save space). |
| 2052 | |
| 2053 | llvm::Value *InRegs = 0; |
| 2054 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2055 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2056 | if (neededInt) { |
| 2057 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2058 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2059 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2060 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | if (neededSSE) { |
| 2064 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2065 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2066 | llvm::Value *FitsInFP = |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2067 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2068 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2069 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2070 | } |
| 2071 | |
| 2072 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2073 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2074 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2075 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2076 | |
| 2077 | // Emit code to load the value if it was passed in registers. |
| 2078 | |
| 2079 | CGF.EmitBlock(InRegBlock); |
| 2080 | |
| 2081 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2082 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2083 | // copying to a temporary location in case the parameter is passed |
| 2084 | // in different register classes or requires an alignment greater |
| 2085 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2086 | // |
| 2087 | // FIXME: This really results in shameful code when we end up needing to |
| 2088 | // collect arguments from different places; often what should result in a |
| 2089 | // simple assembling of a structure from scattered addresses has many more |
| 2090 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2091 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2092 | llvm::Value *RegAddr = |
| 2093 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2094 | "reg_save_area"); |
| 2095 | if (neededInt && neededSSE) { |
| 2096 | // FIXME: Cleanup. |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2097 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2098 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2099 | llvm::Value *Tmp = CGF.CreateTempAlloca(ST); |
| 2100 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2101 | llvm::Type *TyLo = ST->getElementType(0); |
| 2102 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | a8b7a7d | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2103 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2104 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2105 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2106 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2107 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2108 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | f177d9d | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2109 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2110 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2111 | llvm::Value *V = |
| 2112 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2113 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2114 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2115 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2116 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2117 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2118 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2119 | } else if (neededInt) { |
| 2120 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2121 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2122 | llvm::PointerType::getUnqual(LTy)); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2123 | } else if (neededSSE == 1) { |
| 2124 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2125 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2126 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2127 | } else { |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2128 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2129 | // SSE registers are spaced 16 bytes apart in the register save |
| 2130 | // area, we need to collect the two eightbytes together. |
| 2131 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | 1090a9b | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2132 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2133 | llvm::Type *DoubleTy = llvm::Type::getDoubleTy(VMContext); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2134 | llvm::Type *DblPtrTy = |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2135 | llvm::PointerType::getUnqual(DoubleTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2136 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2137 | DoubleTy, NULL); |
| 2138 | llvm::Value *V, *Tmp = CGF.CreateTempAlloca(ST); |
| 2139 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2140 | DblPtrTy)); |
| 2141 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2142 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2143 | DblPtrTy)); |
| 2144 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2145 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2146 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2150 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2151 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2152 | if (neededInt) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2153 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2154 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2155 | gp_offset_p); |
| 2156 | } |
| 2157 | if (neededSSE) { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2158 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2159 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2160 | fp_offset_p); |
| 2161 | } |
| 2162 | CGF.EmitBranch(ContBlock); |
| 2163 | |
| 2164 | // Emit code to load the value if it was passed in memory. |
| 2165 | |
| 2166 | CGF.EmitBlock(InMemBlock); |
| 2167 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2168 | |
| 2169 | // Return the appropriate result. |
| 2170 | |
| 2171 | CGF.EmitBlock(ContBlock); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2172 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2173 | "vaarg.addr"); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2174 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2175 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2176 | return ResAddr; |
| 2177 | } |
| 2178 | |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2179 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty) const { |
| 2180 | |
| 2181 | if (Ty->isVoidType()) |
| 2182 | return ABIArgInfo::getIgnore(); |
| 2183 | |
| 2184 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2185 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2186 | |
| 2187 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2188 | |
| 2189 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
NAKAMURA Takumi | ff8be0e | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2190 | if (hasNonTrivialDestructorOrCopyConstructor(RT) || |
| 2191 | RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2192 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2193 | |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2194 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
| 2195 | if (Size == 128 && |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2196 | getContext().getTargetInfo().getTriple().getOS() == llvm::Triple::MinGW32) |
NAKAMURA Takumi | 6f17433 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2197 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2198 | Size)); |
| 2199 | |
| 2200 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2201 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 2202 | if (Size <= 64 && |
NAKAMURA Takumi | ff8be0e | 2011-01-19 00:11:33 +0000 | [diff] [blame] | 2203 | (Size & (Size - 1)) == 0) |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2204 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2205 | Size)); |
| 2206 | |
| 2207 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2208 | } |
| 2209 | |
| 2210 | if (Ty->isPromotableIntegerType()) |
| 2211 | return ABIArgInfo::getExtend(); |
| 2212 | |
| 2213 | return ABIArgInfo::getDirect(); |
| 2214 | } |
| 2215 | |
| 2216 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2217 | |
| 2218 | QualType RetTy = FI.getReturnType(); |
| 2219 | FI.getReturnInfo() = classify(RetTy); |
| 2220 | |
NAKAMURA Takumi | a757322 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2221 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2222 | it != ie; ++it) |
| 2223 | it->info = classify(it->type); |
| 2224 | } |
| 2225 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2226 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2227 | CodeGenFunction &CGF) const { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2228 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 2229 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2230 | |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2231 | CGBuilderTy &Builder = CGF.Builder; |
| 2232 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2233 | "ap"); |
| 2234 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2235 | llvm::Type *PTy = |
| 2236 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2237 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2238 | |
| 2239 | uint64_t Offset = |
| 2240 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2241 | llvm::Value *NextAddr = |
| 2242 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2243 | "ap.next"); |
| 2244 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2245 | |
| 2246 | return AddrTyped; |
| 2247 | } |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2248 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2249 | // PowerPC-32 |
| 2250 | |
| 2251 | namespace { |
| 2252 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2253 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2254 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2255 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2256 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2257 | // This is recovered from gcc output. |
| 2258 | return 1; // r1 is the dedicated stack pointer |
| 2259 | } |
| 2260 | |
| 2261 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2262 | llvm::Value *Address) const; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2263 | }; |
| 2264 | |
| 2265 | } |
| 2266 | |
| 2267 | bool |
| 2268 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2269 | llvm::Value *Address) const { |
| 2270 | // This is calculated from the LLVM and GCC tables and verified |
| 2271 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2272 | |
| 2273 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2274 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 2275 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2276 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2277 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2278 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2279 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2280 | |
| 2281 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2282 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2283 | |
| 2284 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2285 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2286 | |
| 2287 | // 64-76 are various 4-byte special-purpose registers: |
| 2288 | // 64: mq |
| 2289 | // 65: lr |
| 2290 | // 66: ctr |
| 2291 | // 67: ap |
| 2292 | // 68-75 cr0-7 |
| 2293 | // 76: xer |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2294 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2295 | |
| 2296 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2297 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2298 | |
| 2299 | // 109: vrsave |
| 2300 | // 110: vscr |
| 2301 | // 111: spe_acc |
| 2302 | // 112: spefscr |
| 2303 | // 113: sfp |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2304 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2305 | |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2306 | return false; |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2310 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2311 | // ARM ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2312 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2313 | |
| 2314 | namespace { |
| 2315 | |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2316 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2317 | public: |
| 2318 | enum ABIKind { |
| 2319 | APCS = 0, |
| 2320 | AAPCS = 1, |
| 2321 | AAPCS_VFP |
| 2322 | }; |
| 2323 | |
| 2324 | private: |
| 2325 | ABIKind Kind; |
| 2326 | |
| 2327 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2328 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) {} |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2329 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2330 | bool isEABI() const { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2331 | StringRef Env = getContext().getTargetInfo().getTriple().getEnvironmentName(); |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2332 | return (Env == "gnueabi" || Env == "eabi"); |
| 2333 | } |
| 2334 | |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2335 | private: |
| 2336 | ABIKind getABIKind() const { return Kind; } |
| 2337 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2338 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2339 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2340 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2341 | virtual void computeInfo(CGFunctionInfo &FI) const; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2342 | |
| 2343 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2344 | CodeGenFunction &CGF) const; |
| 2345 | }; |
| 2346 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2347 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2348 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2349 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 2350 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2351 | |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2352 | const ARMABIInfo &getABIInfo() const { |
| 2353 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2354 | } |
| 2355 | |
John McCall | 6374c33 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2356 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { |
| 2357 | return 13; |
| 2358 | } |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 2359 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2360 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2361 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 2362 | } |
| 2363 | |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 2364 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2365 | llvm::Value *Address) const { |
| 2366 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 2367 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 2368 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2369 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
Roman Divacky | 09345d1 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 2370 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2371 | |
| 2372 | // 0-15 are the 16 integer registers. |
| 2373 | AssignToArrayRange(Builder, Address, Four8, 0, 15); |
| 2374 | |
| 2375 | return false; |
| 2376 | } |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2377 | |
| 2378 | unsigned getSizeOfUnwindException() const { |
| 2379 | if (getABIInfo().isEABI()) return 88; |
| 2380 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 2381 | } |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2382 | }; |
| 2383 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2386 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2387 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2388 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2389 | it != ie; ++it) |
| 2390 | it->info = classifyArgumentType(it->type); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2391 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2392 | // Always honor user-specified calling convention. |
| 2393 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 2394 | return; |
| 2395 | |
| 2396 | // Calling convention as default by an ABI. |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2397 | llvm::CallingConv::ID DefaultCC; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 2398 | if (isEABI()) |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2399 | DefaultCC = llvm::CallingConv::ARM_AAPCS; |
Rafael Espindola | 1ed1a59 | 2010-06-16 19:01:17 +0000 | [diff] [blame] | 2400 | else |
| 2401 | DefaultCC = llvm::CallingConv::ARM_APCS; |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2402 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2403 | // If user did not ask for specific calling convention explicitly (e.g. via |
| 2404 | // pcs attribute), set effective calling convention if it's different than ABI |
| 2405 | // default. |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2406 | switch (getABIKind()) { |
| 2407 | case APCS: |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2408 | if (DefaultCC != llvm::CallingConv::ARM_APCS) |
| 2409 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_APCS); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2410 | break; |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2411 | case AAPCS: |
Rafael Espindola | 25117ab | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 2412 | if (DefaultCC != llvm::CallingConv::ARM_AAPCS) |
| 2413 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2414 | break; |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2415 | case AAPCS_VFP: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2416 | if (DefaultCC != llvm::CallingConv::ARM_AAPCS_VFP) |
| 2417 | FI.setEffectiveCallingConvention(llvm::CallingConv::ARM_AAPCS_VFP); |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2422 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 2423 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 2424 | /// contained in the type is returned through it; this is used for the |
| 2425 | /// recursive calls that check aggregate component types. |
| 2426 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 2427 | ASTContext &Context, |
| 2428 | uint64_t *HAMembers = 0) { |
| 2429 | uint64_t Members; |
| 2430 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2431 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 2432 | return false; |
| 2433 | Members *= AT->getSize().getZExtValue(); |
| 2434 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2435 | const RecordDecl *RD = RT->getDecl(); |
| 2436 | if (RD->isUnion() || RD->hasFlexibleArrayMember()) |
| 2437 | return false; |
| 2438 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 2439 | if (!CXXRD->isAggregate()) |
| 2440 | return false; |
| 2441 | } |
| 2442 | Members = 0; |
| 2443 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2444 | i != e; ++i) { |
| 2445 | const FieldDecl *FD = *i; |
| 2446 | uint64_t FldMembers; |
| 2447 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 2448 | return false; |
| 2449 | Members += FldMembers; |
| 2450 | } |
| 2451 | } else { |
| 2452 | Members = 1; |
| 2453 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 2454 | Members = 2; |
| 2455 | Ty = CT->getElementType(); |
| 2456 | } |
| 2457 | |
| 2458 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 2459 | // double, or 64-bit or 128-bit vectors. |
| 2460 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 2461 | if (BT->getKind() != BuiltinType::Float && |
| 2462 | BT->getKind() != BuiltinType::Double) |
| 2463 | return false; |
| 2464 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 2465 | unsigned VecSize = Context.getTypeSize(VT); |
| 2466 | if (VecSize != 64 && VecSize != 128) |
| 2467 | return false; |
| 2468 | } else { |
| 2469 | return false; |
| 2470 | } |
| 2471 | |
| 2472 | // The base type must be the same for all members. Vector types of the |
| 2473 | // same total size are treated as being equivalent here. |
| 2474 | const Type *TyPtr = Ty.getTypePtr(); |
| 2475 | if (!Base) |
| 2476 | Base = TyPtr; |
| 2477 | if (Base != TyPtr && |
| 2478 | (!Base->isVectorType() || !TyPtr->isVectorType() || |
| 2479 | Context.getTypeSize(Base) != Context.getTypeSize(TyPtr))) |
| 2480 | return false; |
| 2481 | } |
| 2482 | |
| 2483 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 2484 | if (HAMembers) |
| 2485 | *HAMembers = Members; |
| 2486 | return (Members <= 4); |
| 2487 | } |
| 2488 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2489 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty) const { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2490 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2491 | // Treat an enum type as its underlying type. |
| 2492 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2493 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2494 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2495 | return (Ty->isPromotableIntegerType() ? |
| 2496 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2497 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2498 | |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 2499 | // Ignore empty records. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2500 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 4202557 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 2501 | return ABIArgInfo::getIgnore(); |
| 2502 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 2503 | // Structures with either a non-trivial destructor or a non-trivial |
| 2504 | // copy constructor are always indirect. |
| 2505 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 2506 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2507 | |
Bob Wilson | 194f06a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 2508 | if (getABIKind() == ARMABIInfo::AAPCS_VFP) { |
| 2509 | // Homogeneous Aggregates need to be expanded. |
| 2510 | const Type *Base = 0; |
| 2511 | if (isHomogeneousAggregate(Ty, Base, getContext())) |
| 2512 | return ABIArgInfo::getExpand(); |
| 2513 | } |
| 2514 | |
Daniel Dunbar | 8aa87c7 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 2515 | // Otherwise, pass by coercing to a structure of the appropriate size. |
| 2516 | // |
Bob Wilson | 53fc1a6 | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 2517 | // FIXME: This is kind of nasty... but there isn't much choice because the ARM |
| 2518 | // backend doesn't support byval. |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2519 | // FIXME: This doesn't handle alignment > 64 bits. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2520 | llvm::Type* ElemTy; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2521 | unsigned SizeRegs; |
Bob Wilson | 53fc1a6 | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 2522 | if (getContext().getTypeAlign(Ty) > 32) { |
Stuart Hastings | 67d097e | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 2523 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 2524 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Bob Wilson | 53fc1a6 | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 2525 | } else { |
| 2526 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 2527 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Stuart Hastings | 67d097e | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 2528 | } |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 2529 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2530 | llvm::Type *STy = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2531 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | b7f62d0 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 2532 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2535 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2536 | llvm::LLVMContext &VMContext) { |
| 2537 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 2538 | // is called integer-like if its size is less than or equal to one word, and |
| 2539 | // the offset of each of its addressable sub-fields is zero. |
| 2540 | |
| 2541 | uint64_t Size = Context.getTypeSize(Ty); |
| 2542 | |
| 2543 | // Check that the type fits in a word. |
| 2544 | if (Size > 32) |
| 2545 | return false; |
| 2546 | |
| 2547 | // FIXME: Handle vector types! |
| 2548 | if (Ty->isVectorType()) |
| 2549 | return false; |
| 2550 | |
Daniel Dunbar | b0d5819 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 2551 | // Float types are never treated as "integer like". |
| 2552 | if (Ty->isRealFloatingType()) |
| 2553 | return false; |
| 2554 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2555 | // If this is a builtin or pointer type then it is ok. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2556 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2557 | return true; |
| 2558 | |
Daniel Dunbar | 4581581 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 2559 | // Small complex integer types are "integer like". |
| 2560 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 2561 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2562 | |
| 2563 | // Single element and zero sized arrays should be allowed, by the definition |
| 2564 | // above, but they are not. |
| 2565 | |
| 2566 | // Otherwise, it must be a record type. |
| 2567 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2568 | if (!RT) return false; |
| 2569 | |
| 2570 | // Ignore records with flexible arrays. |
| 2571 | const RecordDecl *RD = RT->getDecl(); |
| 2572 | if (RD->hasFlexibleArrayMember()) |
| 2573 | return false; |
| 2574 | |
| 2575 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 2576 | // like". |
| 2577 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 2578 | |
| 2579 | bool HadField = false; |
| 2580 | unsigned idx = 0; |
| 2581 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2582 | i != e; ++i, ++idx) { |
| 2583 | const FieldDecl *FD = *i; |
| 2584 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2585 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 2586 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 2587 | // struct { int : 0; int x } |
| 2588 | // is non-integer like according to gcc. |
| 2589 | if (FD->isBitField()) { |
| 2590 | if (!RD->isUnion()) |
| 2591 | HadField = true; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2592 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2593 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 2594 | return false; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2595 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2596 | continue; |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2599 | // Check if this field is at offset 0. |
| 2600 | if (Layout.getFieldOffset(idx) != 0) |
| 2601 | return false; |
| 2602 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2603 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 2604 | return false; |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2605 | |
Daniel Dunbar | 679855a | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 2606 | // Only allow at most one field in a structure. This doesn't match the |
| 2607 | // wording above, but follows gcc in situations with a field following an |
| 2608 | // empty structure. |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2609 | if (!RD->isUnion()) { |
| 2610 | if (HadField) |
| 2611 | return false; |
| 2612 | |
| 2613 | HadField = true; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | return true; |
| 2618 | } |
| 2619 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2620 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy) const { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2621 | if (RetTy->isVoidType()) |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2622 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2623 | |
Daniel Dunbar | f554b1c | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 2624 | // Large vector types should be returned via memory. |
| 2625 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 2626 | return ABIArgInfo::getIndirect(0); |
| 2627 | |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2628 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2629 | // Treat an enum type as its underlying type. |
| 2630 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2631 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 2632 | |
Anton Korobeynikov | cc6fa88 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2633 | return (RetTy->isPromotableIntegerType() ? |
| 2634 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | aa74a1e | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2635 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2636 | |
Rafael Espindola | 0eb1d97 | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 2637 | // Structures with either a non-trivial destructor or a non-trivial |
| 2638 | // copy constructor are always indirect. |
| 2639 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(RetTy)) |
| 2640 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2641 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2642 | // Are we following APCS? |
| 2643 | if (getABIKind() == APCS) { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2644 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2645 | return ABIArgInfo::getIgnore(); |
| 2646 | |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 2647 | // Complex types are all returned as packed integers. |
| 2648 | // |
| 2649 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 2650 | // correctly. |
| 2651 | if (RetTy->isAnyComplexType()) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2652 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2653 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | 4cc753f | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 2654 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2655 | // Integer like structures are returned in r0. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2656 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2657 | // Return in the smallest viable integer type. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2658 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2659 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2660 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2661 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2662 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 2663 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | // Otherwise return in memory. |
| 2667 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2668 | } |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2669 | |
| 2670 | // Otherwise this is an AAPCS variant. |
| 2671 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2672 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2673 | return ABIArgInfo::getIgnore(); |
| 2674 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2675 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 2676 | // are returned indirectly. |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2677 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2678 | if (Size <= 32) { |
| 2679 | // Return in the smallest viable integer type. |
| 2680 | if (Size <= 8) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2681 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2682 | if (Size <= 16) |
Chris Lattner | 800588f | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2683 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 2684 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 16a0808 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Daniel Dunbar | 98303b9 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 2687 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2691 | CodeGenFunction &CGF) const { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2692 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 2693 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2694 | |
| 2695 | CGBuilderTy &Builder = CGF.Builder; |
| 2696 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2697 | "ap"); |
| 2698 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Rafael Espindola | e164c18 | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 2699 | // Handle address alignment for type alignment > 32 bits |
| 2700 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2701 | if (TyAlign > 4) { |
| 2702 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 2703 | "Alignment is not power of 2!"); |
| 2704 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 2705 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 2706 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
| 2707 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 2708 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2709 | llvm::Type *PTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2710 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2711 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2712 | |
| 2713 | uint64_t Offset = |
| 2714 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 2715 | llvm::Value *NextAddr = |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2716 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2717 | "ap.next"); |
| 2718 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2719 | |
| 2720 | return AddrTyped; |
| 2721 | } |
| 2722 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2723 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2724 | // PTX ABI Implementation |
| 2725 | //===----------------------------------------------------------------------===// |
| 2726 | |
| 2727 | namespace { |
| 2728 | |
| 2729 | class PTXABIInfo : public ABIInfo { |
| 2730 | public: |
| 2731 | PTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 2732 | |
| 2733 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2734 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2735 | |
| 2736 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 2737 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2738 | CodeGenFunction &CFG) const; |
| 2739 | }; |
| 2740 | |
| 2741 | class PTXTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2742 | public: |
| 2743 | PTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 2744 | : TargetCodeGenInfo(new PTXABIInfo(CGT)) {} |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2745 | |
| 2746 | virtual void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2747 | CodeGen::CodeGenModule &M) const; |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2748 | }; |
| 2749 | |
| 2750 | ABIArgInfo PTXABIInfo::classifyReturnType(QualType RetTy) const { |
| 2751 | if (RetTy->isVoidType()) |
| 2752 | return ABIArgInfo::getIgnore(); |
| 2753 | if (isAggregateTypeForABI(RetTy)) |
| 2754 | return ABIArgInfo::getIndirect(0); |
| 2755 | return ABIArgInfo::getDirect(); |
| 2756 | } |
| 2757 | |
| 2758 | ABIArgInfo PTXABIInfo::classifyArgumentType(QualType Ty) const { |
| 2759 | if (isAggregateTypeForABI(Ty)) |
| 2760 | return ABIArgInfo::getIndirect(0); |
| 2761 | |
| 2762 | return ABIArgInfo::getDirect(); |
| 2763 | } |
| 2764 | |
| 2765 | void PTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2766 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2767 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2768 | it != ie; ++it) |
| 2769 | it->info = classifyArgumentType(it->type); |
| 2770 | |
| 2771 | // Always honor user-specified calling convention. |
| 2772 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 2773 | return; |
| 2774 | |
| 2775 | // Calling convention as default by an ABI. |
| 2776 | llvm::CallingConv::ID DefaultCC; |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2777 | const LangOptions &LangOpts = getContext().getLangOptions(); |
| 2778 | if (LangOpts.OpenCL || LangOpts.CUDA) { |
| 2779 | // 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] | 2780 | DefaultCC = llvm::CallingConv::PTX_Device; |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2781 | } else { |
| 2782 | // If we are in standard C/C++ mode, use the triple to decide on the default |
| 2783 | StringRef Env = |
| 2784 | getContext().getTargetInfo().getTriple().getEnvironmentName(); |
| 2785 | if (Env == "device") |
| 2786 | DefaultCC = llvm::CallingConv::PTX_Device; |
| 2787 | else |
| 2788 | DefaultCC = llvm::CallingConv::PTX_Kernel; |
| 2789 | } |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2790 | FI.setEffectiveCallingConvention(DefaultCC); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2791 | |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | llvm::Value *PTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2795 | CodeGenFunction &CFG) const { |
| 2796 | llvm_unreachable("PTX does not support varargs"); |
| 2797 | return 0; |
| 2798 | } |
| 2799 | |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2800 | void PTXTargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 2801 | llvm::GlobalValue *GV, |
| 2802 | CodeGen::CodeGenModule &M) const{ |
| 2803 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 2804 | if (!FD) return; |
| 2805 | |
| 2806 | llvm::Function *F = cast<llvm::Function>(GV); |
| 2807 | |
| 2808 | // Perform special handling in OpenCL mode |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2809 | if (M.getLangOptions().OpenCL) { |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2810 | // Use OpenCL function attributes to set proper calling conventions |
| 2811 | // By default, all functions are device functions |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2812 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 2813 | // OpenCL __kernel functions get a kernel calling convention |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2814 | F->setCallingConv(llvm::CallingConv::PTX_Kernel); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2815 | // And kernel functions are not subject to inlining |
| 2816 | F->addFnAttr(llvm::Attribute::NoInline); |
| 2817 | } |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2818 | } |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2819 | |
Peter Collingbourne | 744d90b | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 2820 | // Perform special handling in CUDA mode. |
| 2821 | if (M.getLangOptions().CUDA) { |
| 2822 | // CUDA __global__ functions get a kernel calling convention. Since |
| 2823 | // __global__ functions cannot be called from the device, we do not |
| 2824 | // need to set the noinline attribute. |
| 2825 | if (FD->getAttr<CUDAGlobalAttr>()) |
| 2826 | F->setCallingConv(llvm::CallingConv::PTX_Kernel); |
Justin Holewinski | 818eafb | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2833 | // SystemZ ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2834 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2835 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2836 | namespace { |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2837 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2838 | class SystemZABIInfo : public ABIInfo { |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2839 | public: |
| 2840 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 2841 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2842 | bool isPromotableIntegerType(QualType Ty) const; |
| 2843 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2844 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2845 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2846 | |
Chris Lattner | ee5dcd0 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2847 | virtual void computeInfo(CGFunctionInfo &FI) const { |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2848 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2849 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2850 | it != ie; ++it) |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2851 | it->info = classifyArgumentType(it->type); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2855 | CodeGenFunction &CGF) const; |
| 2856 | }; |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 2857 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2858 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2859 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2860 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 2861 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2862 | }; |
| 2863 | |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 2867 | // SystemZ ABI requires all 8, 16 and 32 bit quantities to be extended. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2868 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2869 | switch (BT->getKind()) { |
| 2870 | case BuiltinType::Bool: |
| 2871 | case BuiltinType::Char_S: |
| 2872 | case BuiltinType::Char_U: |
| 2873 | case BuiltinType::SChar: |
| 2874 | case BuiltinType::UChar: |
| 2875 | case BuiltinType::Short: |
| 2876 | case BuiltinType::UShort: |
| 2877 | case BuiltinType::Int: |
| 2878 | case BuiltinType::UInt: |
| 2879 | return true; |
| 2880 | default: |
| 2881 | return false; |
| 2882 | } |
| 2883 | return false; |
| 2884 | } |
| 2885 | |
| 2886 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2887 | CodeGenFunction &CGF) const { |
| 2888 | // FIXME: Implement |
| 2889 | return 0; |
| 2890 | } |
| 2891 | |
| 2892 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2893 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 2894 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2895 | return ABIArgInfo::getIgnore(); |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2896 | if (isAggregateTypeForABI(RetTy)) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2897 | return ABIArgInfo::getIndirect(0); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2898 | |
| 2899 | return (isPromotableIntegerType(RetTy) ? |
| 2900 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2903 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
John McCall | d608cdb | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2904 | if (isAggregateTypeForABI(Ty)) |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2905 | return ABIArgInfo::getIndirect(0); |
Chris Lattner | a3c109b | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2906 | |
| 2907 | return (isPromotableIntegerType(Ty) ? |
| 2908 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 89e887f | 2009-07-16 20:09:57 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2911 | //===----------------------------------------------------------------------===// |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 2912 | // MBlaze ABI Implementation |
| 2913 | //===----------------------------------------------------------------------===// |
| 2914 | |
| 2915 | namespace { |
| 2916 | |
| 2917 | class MBlazeABIInfo : public ABIInfo { |
| 2918 | public: |
| 2919 | MBlazeABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 2920 | |
| 2921 | bool isPromotableIntegerType(QualType Ty) const; |
| 2922 | |
| 2923 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2924 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 2925 | |
| 2926 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 2927 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 2928 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2929 | it != ie; ++it) |
| 2930 | it->info = classifyArgumentType(it->type); |
| 2931 | } |
| 2932 | |
| 2933 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2934 | CodeGenFunction &CGF) const; |
| 2935 | }; |
| 2936 | |
| 2937 | class MBlazeTargetCodeGenInfo : public TargetCodeGenInfo { |
| 2938 | public: |
| 2939 | MBlazeTargetCodeGenInfo(CodeGenTypes &CGT) |
| 2940 | : TargetCodeGenInfo(new MBlazeABIInfo(CGT)) {} |
| 2941 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2942 | CodeGen::CodeGenModule &M) const; |
| 2943 | }; |
| 2944 | |
| 2945 | } |
| 2946 | |
| 2947 | bool MBlazeABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 2948 | // MBlaze ABI requires all 8 and 16 bit quantities to be extended. |
| 2949 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2950 | switch (BT->getKind()) { |
| 2951 | case BuiltinType::Bool: |
| 2952 | case BuiltinType::Char_S: |
| 2953 | case BuiltinType::Char_U: |
| 2954 | case BuiltinType::SChar: |
| 2955 | case BuiltinType::UChar: |
| 2956 | case BuiltinType::Short: |
| 2957 | case BuiltinType::UShort: |
| 2958 | return true; |
| 2959 | default: |
| 2960 | return false; |
| 2961 | } |
| 2962 | return false; |
| 2963 | } |
| 2964 | |
| 2965 | llvm::Value *MBlazeABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2966 | CodeGenFunction &CGF) const { |
| 2967 | // FIXME: Implement |
| 2968 | return 0; |
| 2969 | } |
| 2970 | |
| 2971 | |
| 2972 | ABIArgInfo MBlazeABIInfo::classifyReturnType(QualType RetTy) const { |
| 2973 | if (RetTy->isVoidType()) |
| 2974 | return ABIArgInfo::getIgnore(); |
| 2975 | if (isAggregateTypeForABI(RetTy)) |
| 2976 | return ABIArgInfo::getIndirect(0); |
| 2977 | |
| 2978 | return (isPromotableIntegerType(RetTy) ? |
| 2979 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2980 | } |
| 2981 | |
| 2982 | ABIArgInfo MBlazeABIInfo::classifyArgumentType(QualType Ty) const { |
| 2983 | if (isAggregateTypeForABI(Ty)) |
| 2984 | return ABIArgInfo::getIndirect(0); |
| 2985 | |
| 2986 | return (isPromotableIntegerType(Ty) ? |
| 2987 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2988 | } |
| 2989 | |
| 2990 | void MBlazeTargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 2991 | llvm::GlobalValue *GV, |
| 2992 | CodeGen::CodeGenModule &M) |
| 2993 | const { |
| 2994 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 2995 | if (!FD) return; |
NAKAMURA Takumi | 125b4cb | 2011-02-17 08:50:50 +0000 | [diff] [blame] | 2996 | |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 2997 | llvm::CallingConv::ID CC = llvm::CallingConv::C; |
| 2998 | if (FD->hasAttr<MBlazeInterruptHandlerAttr>()) |
| 2999 | CC = llvm::CallingConv::MBLAZE_INTR; |
| 3000 | else if (FD->hasAttr<MBlazeSaveVolatilesAttr>()) |
| 3001 | CC = llvm::CallingConv::MBLAZE_SVOL; |
| 3002 | |
| 3003 | if (CC != llvm::CallingConv::C) { |
| 3004 | // Handle 'interrupt_handler' attribute: |
| 3005 | llvm::Function *F = cast<llvm::Function>(GV); |
| 3006 | |
| 3007 | // Step 1: Set ISR calling convention. |
| 3008 | F->setCallingConv(CC); |
| 3009 | |
| 3010 | // Step 2: Add attributes goodness. |
| 3011 | F->addFnAttr(llvm::Attribute::NoInline); |
| 3012 | } |
| 3013 | |
| 3014 | // Step 3: Emit _interrupt_handler alias. |
| 3015 | if (CC == llvm::CallingConv::MBLAZE_INTR) |
| 3016 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
| 3017 | "_interrupt_handler", GV, &M.getModule()); |
| 3018 | } |
| 3019 | |
| 3020 | |
| 3021 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3022 | // MSP430 ABI Implementation |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3023 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3024 | |
| 3025 | namespace { |
| 3026 | |
| 3027 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3028 | public: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3029 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3030 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3031 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 3032 | CodeGen::CodeGenModule &M) const; |
| 3033 | }; |
| 3034 | |
| 3035 | } |
| 3036 | |
| 3037 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 3038 | llvm::GlobalValue *GV, |
| 3039 | CodeGen::CodeGenModule &M) const { |
| 3040 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 3041 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 3042 | // Handle 'interrupt' attribute: |
| 3043 | llvm::Function *F = cast<llvm::Function>(GV); |
| 3044 | |
| 3045 | // Step 1: Set ISR calling convention. |
| 3046 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 3047 | |
| 3048 | // Step 2: Add attributes goodness. |
| 3049 | F->addFnAttr(llvm::Attribute::NoInline); |
| 3050 | |
| 3051 | // Step 3: Emit ISR vector alias. |
| 3052 | unsigned Num = attr->getNumber() + 0xffe0; |
| 3053 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3054 | "vector_" + Twine::utohexstr(Num), |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3055 | GV, &M.getModule()); |
| 3056 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3060 | //===----------------------------------------------------------------------===// |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3061 | // MIPS ABI Implementation. This works for both little-endian and |
| 3062 | // big-endian variants. |
Chris Lattner | dce5ad0 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3063 | //===----------------------------------------------------------------------===// |
| 3064 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3065 | namespace { |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3066 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 3067 | static const unsigned MinABIStackAlignInBytes = 4; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3068 | public: |
| 3069 | MipsABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 3070 | |
| 3071 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3072 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 3073 | virtual void computeInfo(CGFunctionInfo &FI) const; |
| 3074 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3075 | CodeGenFunction &CGF) const; |
| 3076 | }; |
| 3077 | |
Akira Hatanaka | 3827e42 | 2011-08-12 01:43:14 +0000 | [diff] [blame] | 3078 | const unsigned MipsABIInfo::MinABIStackAlignInBytes; |
| 3079 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3080 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 3081 | unsigned SizeOfUnwindException; |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3082 | public: |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 3083 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, unsigned SZ) |
| 3084 | : TargetCodeGenInfo(new MipsABIInfo(CGT)), SizeOfUnwindException(SZ) {} |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3085 | |
| 3086 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const { |
| 3087 | return 29; |
| 3088 | } |
| 3089 | |
| 3090 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Michael J. Spencer | 8bea82f | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3091 | llvm::Value *Address) const; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3092 | |
| 3093 | unsigned getSizeOfUnwindException() const { |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 3094 | return SizeOfUnwindException; |
John McCall | 49e34be | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3095 | } |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3096 | }; |
| 3097 | } |
| 3098 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3099 | ABIArgInfo MipsABIInfo::classifyArgumentType(QualType Ty) const { |
| 3100 | if (isAggregateTypeForABI(Ty)) { |
| 3101 | // Ignore empty aggregates. |
| 3102 | if (getContext().getTypeSize(Ty) == 0) |
| 3103 | return ABIArgInfo::getIgnore(); |
| 3104 | |
Akira Hatanaka | 511949b | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 3105 | // Records with non trivial destructors/constructors should not be passed |
| 3106 | // by value. |
| 3107 | if (isRecordWithNonTrivialDestructorOrCopyConstructor(Ty)) |
| 3108 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3109 | |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3110 | return ABIArgInfo::getIndirect(0); |
| 3111 | } |
| 3112 | |
| 3113 | // Treat an enum type as its underlying type. |
| 3114 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3115 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3116 | |
| 3117 | return (Ty->isPromotableIntegerType() ? |
| 3118 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3119 | } |
| 3120 | |
| 3121 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
| 3122 | if (RetTy->isVoidType()) |
| 3123 | return ABIArgInfo::getIgnore(); |
| 3124 | |
| 3125 | if (isAggregateTypeForABI(RetTy)) { |
| 3126 | if (RetTy->isAnyComplexType()) |
| 3127 | return ABIArgInfo::getDirect(); |
| 3128 | |
| 3129 | return ABIArgInfo::getIndirect(0); |
| 3130 | } |
| 3131 | |
| 3132 | // Treat an enum type as its underlying type. |
| 3133 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3134 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3135 | |
| 3136 | return (RetTy->isPromotableIntegerType() ? |
| 3137 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3138 | } |
| 3139 | |
| 3140 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 3141 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3142 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3143 | it != ie; ++it) |
| 3144 | it->info = classifyArgumentType(it->type); |
| 3145 | } |
| 3146 | |
| 3147 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3148 | CodeGenFunction &CGF) const { |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 3149 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 3150 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 3151 | |
| 3152 | CGBuilderTy &Builder = CGF.Builder; |
| 3153 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3154 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3155 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 3156 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3157 | llvm::Value *AddrTyped; |
| 3158 | |
| 3159 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 3160 | llvm::Value *AddrAsInt32 = CGF.Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 3161 | llvm::Value *Inc = llvm::ConstantInt::get(CGF.Int32Ty, TypeAlign - 1); |
| 3162 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -TypeAlign); |
| 3163 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt32, Inc); |
| 3164 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 3165 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 3166 | } |
| 3167 | else |
| 3168 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3169 | |
| 3170 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 7b0a038 | 2011-08-12 02:30:14 +0000 | [diff] [blame] | 3171 | TypeAlign = std::max(TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | c35e69d | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 3172 | uint64_t Offset = |
| 3173 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 3174 | llvm::Value *NextAddr = |
| 3175 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3176 | "ap.next"); |
| 3177 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3178 | |
| 3179 | return AddrTyped; |
Akira Hatanaka | 619e887 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3182 | bool |
| 3183 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3184 | llvm::Value *Address) const { |
| 3185 | // This information comes from gcc's implementation, which seems to |
| 3186 | // as canonical as it gets. |
| 3187 | |
| 3188 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3189 | llvm::LLVMContext &Context = CGF.getLLVMContext(); |
| 3190 | |
| 3191 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 3192 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3193 | llvm::IntegerType *i8 = llvm::Type::getInt8Ty(Context); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3194 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3195 | |
| 3196 | // 0-31 are the general purpose registers, $0 - $31. |
| 3197 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 3198 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 3199 | // 66 is the (notional, I think) register for signal-handler return. |
| 3200 | AssignToArrayRange(Builder, Address, Four8, 0, 65); |
| 3201 | |
| 3202 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 3203 | // They are one bit wide and ignored here. |
| 3204 | |
| 3205 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 3206 | // (coprocessor 1 is the FP unit) |
| 3207 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 3208 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 3209 | // 176-181 are the DSP accumulator registers. |
| 3210 | AssignToArrayRange(Builder, Address, Four8, 80, 181); |
| 3211 | |
| 3212 | return false; |
| 3213 | } |
| 3214 | |
| 3215 | |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3216 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3217 | if (TheTargetCodeGenInfo) |
| 3218 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3219 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3220 | const llvm::Triple &Triple = getContext().getTargetInfo().getTriple(); |
Daniel Dunbar | 1752ee4 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 3221 | switch (Triple.getArch()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3222 | default: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3223 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3224 | |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3225 | case llvm::Triple::mips: |
| 3226 | case llvm::Triple::mipsel: |
Akira Hatanaka | e624fa0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 3227 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, 24)); |
John McCall | aeeb701 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3228 | |
Akira Hatanaka | 8c6dfbe | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 3229 | case llvm::Triple::mips64: |
| 3230 | case llvm::Triple::mips64el: |
| 3231 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, 32)); |
| 3232 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3233 | case llvm::Triple::arm: |
| 3234 | case llvm::Triple::thumb: |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 3235 | { |
| 3236 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Daniel Dunbar | 5e7bace | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3237 | |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3238 | if (strcmp(getContext().getTargetInfo().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 34c1af8 | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 3239 | Kind = ARMABIInfo::APCS; |
| 3240 | else if (CodeGenOpts.FloatABI == "hard") |
| 3241 | Kind = ARMABIInfo::AAPCS_VFP; |
| 3242 | |
| 3243 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
| 3244 | } |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3245 | |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3246 | case llvm::Triple::ppc: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3247 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
John McCall | ec853ba | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3248 | |
Justin Holewinski | 0259c3a | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 3249 | case llvm::Triple::ptx32: |
| 3250 | case llvm::Triple::ptx64: |
| 3251 | return *(TheTargetCodeGenInfo = new PTXTargetCodeGenInfo(Types)); |
| 3252 | |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3253 | case llvm::Triple::systemz: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3254 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3255 | |
Wesley Peck | 276fdf4 | 2010-12-19 19:57:51 +0000 | [diff] [blame] | 3256 | case llvm::Triple::mblaze: |
| 3257 | return *(TheTargetCodeGenInfo = new MBlazeTargetCodeGenInfo(Types)); |
| 3258 | |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3259 | case llvm::Triple::msp430: |
Chris Lattner | ea04432 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3260 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | 34d91fd | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3261 | |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3262 | case llvm::Triple::x86: { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3263 | bool DisableMMX = strcmp(getContext().getTargetInfo().getABI(), "no-mmx") == 0; |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3264 | |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 3265 | if (Triple.isOSDarwin()) |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3266 | return *(TheTargetCodeGenInfo = |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3267 | new X86_32TargetCodeGenInfo(Types, true, true, DisableMMX)); |
Daniel Dunbar | db57a4c | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 3268 | |
| 3269 | switch (Triple.getOS()) { |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3270 | case llvm::Triple::Cygwin: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3271 | case llvm::Triple::MinGW32: |
Edward O'Callaghan | 727e268 | 2009-10-21 11:58:24 +0000 | [diff] [blame] | 3272 | case llvm::Triple::AuroraUX: |
| 3273 | case llvm::Triple::DragonFly: |
David Chisnall | 75c135a | 2009-09-03 01:48:05 +0000 | [diff] [blame] | 3274 | case llvm::Triple::FreeBSD: |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3275 | case llvm::Triple::OpenBSD: |
Benjamin Kramer | 8e50a96 | 2011-02-02 18:59:27 +0000 | [diff] [blame] | 3276 | case llvm::Triple::NetBSD: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3277 | return *(TheTargetCodeGenInfo = |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3278 | new X86_32TargetCodeGenInfo(Types, false, true, DisableMMX)); |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3279 | |
| 3280 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3281 | return *(TheTargetCodeGenInfo = |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3282 | new X86_32TargetCodeGenInfo(Types, false, false, DisableMMX)); |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3283 | } |
Eli Friedman | c3e0fb4 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 3284 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3285 | |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3286 | case llvm::Triple::x86_64: |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3287 | switch (Triple.getOS()) { |
| 3288 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 0aa2057 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 3289 | case llvm::Triple::MinGW32: |
Chris Lattner | f13721d | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3290 | case llvm::Triple::Cygwin: |
| 3291 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
| 3292 | default: |
| 3293 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types)); |
| 3294 | } |
Daniel Dunbar | 2c0843f | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 3295 | } |
Anton Korobeynikov | c4a59eb | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3296 | } |