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