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