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