Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1 | //===---- TargetInfo.cpp - Encapsulate target details -----------*- C++ -*-===// |
Anton Korobeynikov | 244360d | 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 | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 16 | #include "ABIInfo.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CGFunctionInfo.h" |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CodeGenOptions.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
| 26 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 28 | #include <algorithm> // std::sort |
| 29 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | using namespace CodeGen; |
| 32 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 33 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 34 | llvm::Value *Array, |
| 35 | llvm::Value *Value, |
| 36 | unsigned FirstIndex, |
| 37 | unsigned LastIndex) { |
| 38 | // Alternatively, we could emit this as a loop in the source. |
| 39 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| 40 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 41 | Builder.CreateStore(Value, Cell); |
| 42 | } |
| 43 | } |
| 44 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 45 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 46 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 47 | T->isMemberFunctionPointerType(); |
| 48 | } |
| 49 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 50 | ABIInfo::~ABIInfo() {} |
| 51 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 52 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 53 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 54 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 55 | if (!RD) |
| 56 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 57 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 61 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 62 | const RecordType *RT = T->getAs<RecordType>(); |
| 63 | if (!RT) |
| 64 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 65 | return getRecordArgABI(RT, CXXABI); |
| 66 | } |
| 67 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 68 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 69 | /// should ensure that all elements of the union have the same "machine type". |
| 70 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 71 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 72 | const RecordDecl *UD = UT->getDecl(); |
| 73 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 74 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 75 | return UD->field_begin()->getType(); |
| 76 | } |
| 77 | } |
| 78 | return Ty; |
| 79 | } |
| 80 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 81 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 82 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 85 | ASTContext &ABIInfo::getContext() const { |
| 86 | return CGT.getContext(); |
| 87 | } |
| 88 | |
| 89 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 90 | return CGT.getLLVMContext(); |
| 91 | } |
| 92 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 93 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 94 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 95 | } |
| 96 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 97 | const TargetInfo &ABIInfo::getTarget() const { |
| 98 | return CGT.getTarget(); |
| 99 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 100 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 101 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 106 | uint64_t Members) const { |
| 107 | return false; |
| 108 | } |
| 109 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 110 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 111 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 112 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 113 | switch (TheKind) { |
| 114 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 115 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 116 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 117 | Ty->print(OS); |
| 118 | else |
| 119 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 120 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 121 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 122 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 123 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 124 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 125 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 126 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 127 | case InAlloca: |
| 128 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 129 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 130 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 131 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 132 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 133 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 134 | break; |
| 135 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 136 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 137 | break; |
| 138 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 139 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 142 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 143 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 144 | // If someone can figure out a general rule for this, that would be great. |
| 145 | // It's probably just doomed to be platform-dependent, though. |
| 146 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 147 | // Verified for: |
| 148 | // x86-64 FreeBSD, Linux, Darwin |
| 149 | // x86-32 FreeBSD, Linux, Darwin |
| 150 | // PowerPC Linux, Darwin |
| 151 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 152 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 153 | return 32; |
| 154 | } |
| 155 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 156 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 157 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 158 | // The following conventions are known to require this to be false: |
| 159 | // x86_stdcall |
| 160 | // MIPS |
| 161 | // For everything else, we just prefer false unless we opt out. |
| 162 | return false; |
| 163 | } |
| 164 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 165 | void |
| 166 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 167 | llvm::SmallString<24> &Opt) const { |
| 168 | // This assumes the user is passing a library name like "rt" instead of a |
| 169 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 170 | // dynamic. |
| 171 | Opt = "-l"; |
| 172 | Opt += Lib; |
| 173 | } |
| 174 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 175 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 176 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 177 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 178 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 179 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 180 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 181 | if (FD->isUnnamedBitfield()) |
| 182 | return true; |
| 183 | |
| 184 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 185 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 186 | // Constant arrays of empty records count as empty, strip them off. |
| 187 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 188 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 189 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 190 | if (AT->getSize() == 0) |
| 191 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 192 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 193 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 194 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 195 | const RecordType *RT = FT->getAs<RecordType>(); |
| 196 | if (!RT) |
| 197 | return false; |
| 198 | |
| 199 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 200 | // |
| 201 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 202 | // current ABI. |
| 203 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 204 | return false; |
| 205 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 206 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 209 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 210 | /// fields. Note that a structure with a flexible array member is not |
| 211 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 212 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 213 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 214 | if (!RT) |
| 215 | return 0; |
| 216 | const RecordDecl *RD = RT->getDecl(); |
| 217 | if (RD->hasFlexibleArrayMember()) |
| 218 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 219 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 220 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 221 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 222 | for (const auto &I : CXXRD->bases()) |
| 223 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 224 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 225 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 226 | for (const auto *I : RD->fields()) |
| 227 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 228 | return false; |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | /// isSingleElementStruct - Determine if a structure is a "single |
| 233 | /// element struct", i.e. it has exactly one non-empty field or |
| 234 | /// exactly one field which is itself a single element |
| 235 | /// struct. Structures with flexible array members are never |
| 236 | /// considered single element structs. |
| 237 | /// |
| 238 | /// \return The field declaration for the single non-empty field, if |
| 239 | /// it exists. |
| 240 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 241 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 242 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 243 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 244 | |
| 245 | const RecordDecl *RD = RT->getDecl(); |
| 246 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 247 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 248 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 249 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 250 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 251 | // If this is a C++ record, check the bases first. |
| 252 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 253 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 254 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 255 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 256 | continue; |
| 257 | |
| 258 | // If we already found an element then this isn't a single-element struct. |
| 259 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 260 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 261 | |
| 262 | // If this is non-empty and not a single element struct, the composite |
| 263 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 264 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 265 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 266 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 271 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 272 | QualType FT = FD->getType(); |
| 273 | |
| 274 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 275 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 276 | continue; |
| 277 | |
| 278 | // If we already found an element then this isn't a single-element |
| 279 | // struct. |
| 280 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 281 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 282 | |
| 283 | // Treat single element arrays as the element. |
| 284 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 285 | if (AT->getSize().getZExtValue() != 1) |
| 286 | break; |
| 287 | FT = AT->getElementType(); |
| 288 | } |
| 289 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 290 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 291 | Found = FT.getTypePtr(); |
| 292 | } else { |
| 293 | Found = isSingleElementStruct(FT, Context); |
| 294 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 295 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 299 | // We don't consider a struct a single-element struct if it has |
| 300 | // padding beyond the element type. |
| 301 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 302 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 303 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 304 | return Found; |
| 305 | } |
| 306 | |
| 307 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 308 | // Treat complex types as the element type. |
| 309 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 310 | Ty = CTy->getElementType(); |
| 311 | |
| 312 | // Check for a type which we know has a simple scalar argument-passing |
| 313 | // convention without any padding. (We're specifically looking for 32 |
| 314 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 315 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 316 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 317 | return false; |
| 318 | |
| 319 | uint64_t Size = Context.getTypeSize(Ty); |
| 320 | return Size == 32 || Size == 64; |
| 321 | } |
| 322 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 323 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 324 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 325 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 326 | /// inhibiting optimizations. |
| 327 | /// |
| 328 | // FIXME: This predicate is missing many cases, currently it just follows |
| 329 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 330 | // should probably make this smarter, or better yet make the LLVM backend |
| 331 | // capable of handling it. |
| 332 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 333 | // We can only expand structure types. |
| 334 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 335 | if (!RT) |
| 336 | return false; |
| 337 | |
| 338 | // We can only expand (C) structures. |
| 339 | // |
| 340 | // FIXME: This needs to be generalized to handle classes as well. |
| 341 | const RecordDecl *RD = RT->getDecl(); |
| 342 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 343 | return false; |
| 344 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 345 | uint64_t Size = 0; |
| 346 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 347 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 348 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 349 | return false; |
| 350 | |
| 351 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 352 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 353 | // counts as "basic" is more complicated than what we were doing previously. |
| 354 | if (FD->isBitField()) |
| 355 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 356 | |
| 357 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 360 | // Make sure there are not any holes in the struct. |
| 361 | if (Size != Context.getTypeSize(Ty)) |
| 362 | return false; |
| 363 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | |
| 367 | namespace { |
| 368 | /// DefaultABIInfo - The default implementation for ABI specific |
| 369 | /// details. This implementation provides information which results in |
| 370 | /// self-consistent and sensible LLVM IR generation, but does not |
| 371 | /// conform to any particular ABI. |
| 372 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 373 | public: |
| 374 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 376 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 377 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 378 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 379 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 380 | if (!getCXXABI().classifyReturnType(FI)) |
| 381 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 382 | for (auto &I : FI.arguments()) |
| 383 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 386 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 387 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 388 | }; |
| 389 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 390 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 391 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 392 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 393 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 397 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 398 | return nullptr; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 401 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 402 | if (isAggregateTypeForABI(Ty)) |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 403 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 405 | // Treat an enum type as its underlying type. |
| 406 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 407 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 409 | return (Ty->isPromotableIntegerType() ? |
| 410 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 413 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 414 | if (RetTy->isVoidType()) |
| 415 | return ABIArgInfo::getIgnore(); |
| 416 | |
| 417 | if (isAggregateTypeForABI(RetTy)) |
| 418 | return ABIArgInfo::getIndirect(0); |
| 419 | |
| 420 | // Treat an enum type as its underlying type. |
| 421 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 422 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 423 | |
| 424 | return (RetTy->isPromotableIntegerType() ? |
| 425 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 426 | } |
| 427 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 428 | //===----------------------------------------------------------------------===// |
| 429 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 430 | // |
| 431 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 432 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 433 | //===----------------------------------------------------------------------===// |
| 434 | |
| 435 | class PNaClABIInfo : public ABIInfo { |
| 436 | public: |
| 437 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 438 | |
| 439 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 440 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 441 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 442 | void computeInfo(CGFunctionInfo &FI) const override; |
| 443 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 444 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 445 | }; |
| 446 | |
| 447 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 448 | public: |
| 449 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 450 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 451 | }; |
| 452 | |
| 453 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 454 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 455 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 456 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 457 | for (auto &I : FI.arguments()) |
| 458 | I.info = classifyArgumentType(I.type); |
| 459 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 460 | |
| 461 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 462 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 463 | return nullptr; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 466 | /// \brief Classify argument of given type \p Ty. |
| 467 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 468 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 469 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 470 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 471 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 472 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 473 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 474 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 475 | } else if (Ty->isFloatingType()) { |
| 476 | // Floating-point types don't go inreg. |
| 477 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 478 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 479 | |
| 480 | return (Ty->isPromotableIntegerType() ? |
| 481 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 485 | if (RetTy->isVoidType()) |
| 486 | return ABIArgInfo::getIgnore(); |
| 487 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 488 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 489 | if (isAggregateTypeForABI(RetTy)) |
| 490 | return ABIArgInfo::getIndirect(0); |
| 491 | |
| 492 | // Treat an enum type as its underlying type. |
| 493 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 494 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 495 | |
| 496 | return (RetTy->isPromotableIntegerType() ? |
| 497 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 498 | } |
| 499 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 500 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 501 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 502 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 503 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 504 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 505 | IRType->getScalarSizeInBits() != 64; |
| 506 | } |
| 507 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 508 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 509 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 510 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 511 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 512 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 513 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 514 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 517 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 521 | return Ty; |
| 522 | } |
| 523 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 524 | /// Returns true if this type can be passed in SSE registers with the |
| 525 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 526 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 527 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 528 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 529 | return true; |
| 530 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 531 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 532 | // registers specially. |
| 533 | unsigned VecSize = Context.getTypeSize(VT); |
| 534 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 535 | return true; |
| 536 | } |
| 537 | return false; |
| 538 | } |
| 539 | |
| 540 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 541 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 542 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 543 | return NumMembers <= 4; |
| 544 | } |
| 545 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 546 | //===----------------------------------------------------------------------===// |
| 547 | // X86-32 ABI Implementation |
| 548 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 549 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 550 | /// \brief Similar to llvm::CCState, but for Clang. |
| 551 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 552 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 553 | |
| 554 | unsigned CC; |
| 555 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 556 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 557 | }; |
| 558 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 559 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 560 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 561 | enum Class { |
| 562 | Integer, |
| 563 | Float |
| 564 | }; |
| 565 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 566 | static const unsigned MinABIStackAlignInBytes = 4; |
| 567 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 568 | bool IsDarwinVectorABI; |
| 569 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 570 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 571 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 572 | |
| 573 | static bool isRegisterSize(unsigned Size) { |
| 574 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 575 | } |
| 576 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 577 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 578 | // FIXME: Assumes vectorcall is in use. |
| 579 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 580 | } |
| 581 | |
| 582 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 583 | uint64_t NumMembers) const override { |
| 584 | // FIXME: Assumes vectorcall is in use. |
| 585 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 586 | } |
| 587 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 588 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 589 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 590 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 591 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 592 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 593 | |
| 594 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 595 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 596 | /// \brief Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 597 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 598 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 599 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 600 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 601 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 602 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 603 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 604 | /// \brief Rewrite the function info so that all memory arguments use |
| 605 | /// inalloca. |
| 606 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 607 | |
| 608 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 609 | unsigned &StackOffset, ABIArgInfo &Info, |
| 610 | QualType Type) const; |
| 611 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 612 | public: |
| 613 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 614 | void computeInfo(CGFunctionInfo &FI) const override; |
| 615 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 616 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 617 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 618 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 619 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 620 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 621 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 622 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 623 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 624 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 625 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 626 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 627 | bool d, bool p, bool w, unsigned r) |
| 628 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 629 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 630 | static bool isStructReturnInRegABI( |
| 631 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 632 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 633 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 634 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 635 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 636 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 637 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 638 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 639 | return 4; |
| 640 | } |
| 641 | |
| 642 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 643 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 644 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 645 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 646 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 647 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 648 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 649 | } |
| 650 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 651 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 652 | std::string &Constraints, |
| 653 | std::vector<llvm::Type *> &ResultRegTypes, |
| 654 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 655 | std::vector<LValue> &ResultRegDests, |
| 656 | std::string &AsmString, |
| 657 | unsigned NumOutputs) const override; |
| 658 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 659 | llvm::Constant * |
| 660 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 661 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 662 | (0x06 << 8) | // .+0x08 |
| 663 | ('F' << 16) | |
| 664 | ('T' << 24); |
| 665 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 666 | } |
| 667 | |
Joerg Sonnenberger | 096feeb | 2015-02-23 20:23:47 +0000 | [diff] [blame] | 668 | bool hasSjLjLowering(CodeGen::CodeGenFunction &CGF) const override { |
| 669 | return true; |
| 670 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 674 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 675 | /// Rewrite input constraint references after adding some output constraints. |
| 676 | /// In the case where there is one output and one input and we add one output, |
| 677 | /// we need to replace all operand references greater than or equal to 1: |
| 678 | /// mov $0, $1 |
| 679 | /// mov eax, $1 |
| 680 | /// The result will be: |
| 681 | /// mov $0, $2 |
| 682 | /// mov eax, $2 |
| 683 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 684 | unsigned NumNewOuts, |
| 685 | std::string &AsmString) { |
| 686 | std::string Buf; |
| 687 | llvm::raw_string_ostream OS(Buf); |
| 688 | size_t Pos = 0; |
| 689 | while (Pos < AsmString.size()) { |
| 690 | size_t DollarStart = AsmString.find('$', Pos); |
| 691 | if (DollarStart == std::string::npos) |
| 692 | DollarStart = AsmString.size(); |
| 693 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 694 | if (DollarEnd == std::string::npos) |
| 695 | DollarEnd = AsmString.size(); |
| 696 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 697 | Pos = DollarEnd; |
| 698 | size_t NumDollars = DollarEnd - DollarStart; |
| 699 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 700 | // We have an operand reference. |
| 701 | size_t DigitStart = Pos; |
| 702 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 703 | if (DigitEnd == std::string::npos) |
| 704 | DigitEnd = AsmString.size(); |
| 705 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 706 | unsigned OperandIndex; |
| 707 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 708 | if (OperandIndex >= FirstIn) |
| 709 | OperandIndex += NumNewOuts; |
| 710 | OS << OperandIndex; |
| 711 | } else { |
| 712 | OS << OperandStr; |
| 713 | } |
| 714 | Pos = DigitEnd; |
| 715 | } |
| 716 | } |
| 717 | AsmString = std::move(OS.str()); |
| 718 | } |
| 719 | |
| 720 | /// Add output constraints for EAX:EDX because they are return registers. |
| 721 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 722 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 723 | std::vector<llvm::Type *> &ResultRegTypes, |
| 724 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 725 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 726 | unsigned NumOutputs) const { |
| 727 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 728 | |
| 729 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 730 | // larger. |
| 731 | if (!Constraints.empty()) |
| 732 | Constraints += ','; |
| 733 | if (RetWidth <= 32) { |
| 734 | Constraints += "={eax}"; |
| 735 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 736 | } else { |
| 737 | // Use the 'A' constraint for EAX:EDX. |
| 738 | Constraints += "=A"; |
| 739 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 740 | } |
| 741 | |
| 742 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 743 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 744 | ResultTruncRegTypes.push_back(CoerceTy); |
| 745 | |
| 746 | // Coerce the integer by bitcasting the return slot pointer. |
| 747 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 748 | CoerceTy->getPointerTo())); |
| 749 | ResultRegDests.push_back(ReturnSlot); |
| 750 | |
| 751 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 752 | } |
| 753 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 754 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 755 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 756 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 757 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 758 | uint64_t Size = Context.getTypeSize(Ty); |
| 759 | |
| 760 | // Type must be register sized. |
| 761 | if (!isRegisterSize(Size)) |
| 762 | return false; |
| 763 | |
| 764 | if (Ty->isVectorType()) { |
| 765 | // 64- and 128- bit vectors inside structures are not returned in |
| 766 | // registers. |
| 767 | if (Size == 64 || Size == 128) |
| 768 | return false; |
| 769 | |
| 770 | return true; |
| 771 | } |
| 772 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 773 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 774 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 775 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 776 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 777 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 778 | return true; |
| 779 | |
| 780 | // Arrays are treated like records. |
| 781 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 782 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 783 | |
| 784 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 785 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 786 | if (!RT) return false; |
| 787 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 788 | // FIXME: Traverse bases here too. |
| 789 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 790 | // Structure types are passed in register if all fields would be |
| 791 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 792 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 793 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 794 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 795 | continue; |
| 796 | |
| 797 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 798 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 799 | return false; |
| 800 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 801 | return true; |
| 802 | } |
| 803 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 804 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 805 | // If the return value is indirect, then the hidden argument is consuming one |
| 806 | // integer register. |
| 807 | if (State.FreeRegs) { |
| 808 | --State.FreeRegs; |
| 809 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 810 | } |
| 811 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 812 | } |
| 813 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 814 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 815 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 816 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 817 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 818 | const Type *Base = nullptr; |
| 819 | uint64_t NumElts = 0; |
| 820 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 821 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 822 | // The LLVM struct type for such an aggregate should lower properly. |
| 823 | return ABIArgInfo::getDirect(); |
| 824 | } |
| 825 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 826 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 827 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 828 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 829 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 830 | |
| 831 | // 128-bit vectors are a special case; they are returned in |
| 832 | // registers and we need to make sure to pick a type the LLVM |
| 833 | // backend will like. |
| 834 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 835 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 836 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 837 | |
| 838 | // Always return in register if it fits in a general purpose |
| 839 | // register, or if it is 64 bits and has a single element. |
| 840 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 841 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 842 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 843 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 844 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 845 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 849 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 850 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 851 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 852 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 853 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 854 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 855 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 856 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 857 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 858 | // If specified, structs and unions are always indirect. |
| 859 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 860 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 861 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 862 | // Small structures which are register sized are generally returned |
| 863 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 864 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 865 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 866 | |
| 867 | // As a special-case, if the struct is a "single-element" struct, and |
| 868 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 869 | // floating-point register. (MSVC does not apply this special case.) |
| 870 | // We apply a similar transformation for pointer types to improve the |
| 871 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 872 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 873 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 874 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 875 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 876 | |
| 877 | // FIXME: We should be able to narrow this integer in cases with dead |
| 878 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 879 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 882 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 883 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 884 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 885 | // Treat an enum type as its underlying type. |
| 886 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 887 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 888 | |
| 889 | return (RetTy->isPromotableIntegerType() ? |
| 890 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 893 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 894 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 895 | } |
| 896 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 897 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 898 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 899 | if (!RT) |
| 900 | return 0; |
| 901 | const RecordDecl *RD = RT->getDecl(); |
| 902 | |
| 903 | // If this is a C++ record, check the bases first. |
| 904 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 905 | for (const auto &I : CXXRD->bases()) |
| 906 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 907 | return false; |
| 908 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 909 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 910 | QualType FT = i->getType(); |
| 911 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 912 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 913 | return true; |
| 914 | |
| 915 | if (isRecordWithSSEVectorType(Context, FT)) |
| 916 | return true; |
| 917 | } |
| 918 | |
| 919 | return false; |
| 920 | } |
| 921 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 922 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 923 | unsigned Align) const { |
| 924 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 925 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 926 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 927 | return 0; // Use default alignment. |
| 928 | |
| 929 | // On non-Darwin, the stack type alignment is always 4. |
| 930 | if (!IsDarwinVectorABI) { |
| 931 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 932 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 933 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 934 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 935 | // Otherwise, if the type contains an SSE vector type, the alignment is 16. |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 936 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 937 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 938 | return 16; |
| 939 | |
| 940 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 943 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 944 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 945 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 946 | if (State.FreeRegs) { |
| 947 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 948 | return ABIArgInfo::getIndirectInReg(0, false); |
| 949 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 950 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 951 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 952 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 953 | // Compute the byval alignment. |
| 954 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 955 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 956 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 957 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 958 | |
| 959 | // If the stack alignment is less than the type alignment, realign the |
| 960 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 961 | bool Realign = TypeAlign > StackAlign; |
| 962 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 965 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 966 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 967 | if (!T) |
| 968 | T = Ty.getTypePtr(); |
| 969 | |
| 970 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 971 | BuiltinType::Kind K = BT->getKind(); |
| 972 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 973 | return Float; |
| 974 | } |
| 975 | return Integer; |
| 976 | } |
| 977 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 978 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 979 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 980 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 981 | Class C = classify(Ty); |
| 982 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 983 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 984 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 985 | unsigned Size = getContext().getTypeSize(Ty); |
| 986 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 987 | |
| 988 | if (SizeInRegs == 0) |
| 989 | return false; |
| 990 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 991 | if (SizeInRegs > State.FreeRegs) { |
| 992 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 993 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 994 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 995 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 996 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 997 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 998 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 999 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1000 | if (Size > 32) |
| 1001 | return false; |
| 1002 | |
| 1003 | if (Ty->isIntegralOrEnumerationType()) |
| 1004 | return true; |
| 1005 | |
| 1006 | if (Ty->isPointerType()) |
| 1007 | return true; |
| 1008 | |
| 1009 | if (Ty->isReferenceType()) |
| 1010 | return true; |
| 1011 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1012 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1013 | NeedsPadding = true; |
| 1014 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1015 | return false; |
| 1016 | } |
| 1017 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1018 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1021 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1022 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1023 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1024 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1025 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1026 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1027 | // Check with the C++ ABI first. |
| 1028 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1029 | if (RT) { |
| 1030 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1031 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1032 | return getIndirectResult(Ty, false, State); |
| 1033 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1034 | // The field index doesn't matter, we'll fix it up later. |
| 1035 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1040 | // to other targets. |
| 1041 | const Type *Base = nullptr; |
| 1042 | uint64_t NumElts = 0; |
| 1043 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1044 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1045 | if (State.FreeSSERegs >= NumElts) { |
| 1046 | State.FreeSSERegs -= NumElts; |
| 1047 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1048 | return ABIArgInfo::getDirect(); |
| 1049 | return ABIArgInfo::getExpand(); |
| 1050 | } |
| 1051 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1052 | } |
| 1053 | |
| 1054 | if (isAggregateTypeForABI(Ty)) { |
| 1055 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1056 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1057 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1058 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1059 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1060 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1061 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1062 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1063 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1064 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1065 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1066 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1067 | return ABIArgInfo::getIgnore(); |
| 1068 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1069 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1070 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 1071 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1072 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1073 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1074 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1075 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 1076 | return ABIArgInfo::getDirectInReg(Result); |
| 1077 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1078 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1079 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1080 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1081 | // of those arguments will match the struct. This is important because the |
| 1082 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1083 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1084 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 1085 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1086 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1087 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1088 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1089 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1090 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1091 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1094 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1095 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1096 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1097 | if (IsDarwinVectorABI) { |
| 1098 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1099 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1100 | (Size == 64 && VT->getNumElements() == 1)) |
| 1101 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1102 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1103 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1104 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1105 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1106 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1108 | return ABIArgInfo::getDirect(); |
| 1109 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1110 | |
| 1111 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1112 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1113 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1114 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1115 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1116 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1117 | |
| 1118 | if (Ty->isPromotableIntegerType()) { |
| 1119 | if (InReg) |
| 1120 | return ABIArgInfo::getExtendInReg(); |
| 1121 | return ABIArgInfo::getExtend(); |
| 1122 | } |
| 1123 | if (InReg) |
| 1124 | return ABIArgInfo::getDirectInReg(); |
| 1125 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1128 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1129 | CCState State(FI.getCallingConvention()); |
| 1130 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1131 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1132 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1133 | State.FreeRegs = 2; |
| 1134 | State.FreeSSERegs = 6; |
| 1135 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1136 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1137 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1138 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1139 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1140 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1141 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1142 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1143 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1144 | // return value was sret and put it in a register ourselves if appropriate. |
| 1145 | if (State.FreeRegs) { |
| 1146 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1147 | FI.getReturnInfo().setInReg(true); |
| 1148 | } |
| 1149 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1150 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1151 | // The chain argument effectively gives us another free register. |
| 1152 | if (FI.isChainCall()) |
| 1153 | ++State.FreeRegs; |
| 1154 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1155 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1156 | for (auto &I : FI.arguments()) { |
| 1157 | I.info = classifyArgumentType(I.type, State); |
| 1158 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1162 | // all the memory arguments to use inalloca. |
| 1163 | if (UsedInAlloca) |
| 1164 | rewriteWithInAlloca(FI); |
| 1165 | } |
| 1166 | |
| 1167 | void |
| 1168 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1169 | unsigned &StackOffset, |
| 1170 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1171 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 1172 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1173 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1174 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1175 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1176 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1177 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1178 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1179 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1180 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1181 | unsigned NumBytes = StackOffset - OldOffset; |
| 1182 | assert(NumBytes); |
| 1183 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1184 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1185 | FrameFields.push_back(Ty); |
| 1186 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1189 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1190 | // Leave ignored and inreg arguments alone. |
| 1191 | switch (Info.getKind()) { |
| 1192 | case ABIArgInfo::InAlloca: |
| 1193 | return true; |
| 1194 | case ABIArgInfo::Indirect: |
| 1195 | assert(Info.getIndirectByVal()); |
| 1196 | return true; |
| 1197 | case ABIArgInfo::Ignore: |
| 1198 | return false; |
| 1199 | case ABIArgInfo::Direct: |
| 1200 | case ABIArgInfo::Extend: |
| 1201 | case ABIArgInfo::Expand: |
| 1202 | if (Info.getInReg()) |
| 1203 | return false; |
| 1204 | return true; |
| 1205 | } |
| 1206 | llvm_unreachable("invalid enum"); |
| 1207 | } |
| 1208 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1209 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1210 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1211 | |
| 1212 | // Build a packed struct type for all of the arguments in memory. |
| 1213 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1214 | |
| 1215 | unsigned StackOffset = 0; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1216 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1217 | |
| 1218 | // Put 'this' into the struct before 'sret', if necessary. |
| 1219 | bool IsThisCall = |
| 1220 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1221 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1222 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1223 | isArgInAlloca(I->info)) { |
| 1224 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1225 | ++I; |
| 1226 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Put the sret parameter into the inalloca struct if it's in memory. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1229 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1230 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1231 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1232 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1233 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1237 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1238 | ++I; |
| 1239 | |
| 1240 | // Put arguments passed in memory into the struct. |
| 1241 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1242 | if (isArgInAlloca(I->info)) |
| 1243 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1247 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1250 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1251 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1252 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1253 | |
| 1254 | CGBuilderTy &Builder = CGF.Builder; |
| 1255 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1256 | "ap"); |
| 1257 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1258 | |
| 1259 | // Compute if the address needs to be aligned |
| 1260 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1261 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1262 | Align = std::max(Align, 4U); |
| 1263 | if (Align > 4) { |
| 1264 | // addr = (addr + align - 1) & -align; |
| 1265 | llvm::Value *Offset = |
| 1266 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1267 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1268 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1269 | CGF.Int32Ty); |
| 1270 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1271 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1272 | Addr->getType(), |
| 1273 | "ap.cur.aligned"); |
| 1274 | } |
| 1275 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1276 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1277 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1278 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1279 | |
| 1280 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1281 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1282 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1283 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1284 | "ap.next"); |
| 1285 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1286 | |
| 1287 | return AddrTyped; |
| 1288 | } |
| 1289 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1290 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1291 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1292 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1293 | |
| 1294 | switch (Opts.getStructReturnConvention()) { |
| 1295 | case CodeGenOptions::SRCK_Default: |
| 1296 | break; |
| 1297 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1298 | return false; |
| 1299 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1300 | return true; |
| 1301 | } |
| 1302 | |
| 1303 | if (Triple.isOSDarwin()) |
| 1304 | return true; |
| 1305 | |
| 1306 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1307 | case llvm::Triple::DragonFly: |
| 1308 | case llvm::Triple::FreeBSD: |
| 1309 | case llvm::Triple::OpenBSD: |
| 1310 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1311 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1312 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1313 | default: |
| 1314 | return false; |
| 1315 | } |
| 1316 | } |
| 1317 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1318 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1319 | llvm::GlobalValue *GV, |
| 1320 | CodeGen::CodeGenModule &CGM) const { |
| 1321 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1322 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1323 | // Get the LLVM function. |
| 1324 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1325 | |
| 1326 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1327 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1328 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1329 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1330 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1331 | llvm::AttributeSet::FunctionIndex, |
| 1332 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1337 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1338 | CodeGen::CodeGenFunction &CGF, |
| 1339 | llvm::Value *Address) const { |
| 1340 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1341 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1342 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1343 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1344 | // 0-7 are the eight integer registers; the order is different |
| 1345 | // on Darwin (for EH), but the range is the same. |
| 1346 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1347 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1348 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1349 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1350 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1351 | // These have size 16, which is sizeof(long double) on |
| 1352 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1353 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1354 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1355 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1356 | } else { |
| 1357 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1358 | // reason. |
| 1359 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1360 | |
| 1361 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1362 | // These have size 12, which is sizeof(long double) on |
| 1363 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1364 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1365 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1366 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1367 | |
| 1368 | return false; |
| 1369 | } |
| 1370 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1371 | //===----------------------------------------------------------------------===// |
| 1372 | // X86-64 ABI Implementation |
| 1373 | //===----------------------------------------------------------------------===// |
| 1374 | |
| 1375 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1376 | namespace { |
| 1377 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1378 | class X86_64ABIInfo : public ABIInfo { |
| 1379 | enum Class { |
| 1380 | Integer = 0, |
| 1381 | SSE, |
| 1382 | SSEUp, |
| 1383 | X87, |
| 1384 | X87Up, |
| 1385 | ComplexX87, |
| 1386 | NoClass, |
| 1387 | Memory |
| 1388 | }; |
| 1389 | |
| 1390 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1391 | /// |
| 1392 | /// Merge an accumulating classification \arg Accum with a field |
| 1393 | /// classification \arg Field. |
| 1394 | /// |
| 1395 | /// \param Accum - The accumulating classification. This should |
| 1396 | /// always be either NoClass or the result of a previous merge |
| 1397 | /// call. In addition, this should never be Memory (the caller |
| 1398 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1399 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1400 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1401 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1402 | /// |
| 1403 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1404 | /// final MEMORY or SSE classes when necessary. |
| 1405 | /// |
| 1406 | /// \param AggregateSize - The size of the current aggregate in |
| 1407 | /// the classification process. |
| 1408 | /// |
| 1409 | /// \param Lo - The classification for the parts of the type |
| 1410 | /// residing in the low word of the containing object. |
| 1411 | /// |
| 1412 | /// \param Hi - The classification for the parts of the type |
| 1413 | /// residing in the higher words of the containing object. |
| 1414 | /// |
| 1415 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1416 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1417 | /// classify - Determine the x86_64 register classes in which the |
| 1418 | /// given type T should be passed. |
| 1419 | /// |
| 1420 | /// \param Lo - The classification for the parts of the type |
| 1421 | /// residing in the low word of the containing object. |
| 1422 | /// |
| 1423 | /// \param Hi - The classification for the parts of the type |
| 1424 | /// residing in the high word of the containing object. |
| 1425 | /// |
| 1426 | /// \param OffsetBase - The bit offset of this type in the |
| 1427 | /// containing object. Some parameters are classified different |
| 1428 | /// depending on whether they straddle an eightbyte boundary. |
| 1429 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1430 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1431 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1432 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1433 | /// If a word is unused its result will be NoClass; if a type should |
| 1434 | /// be passed in Memory then at least the classification of \arg Lo |
| 1435 | /// will be Memory. |
| 1436 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1437 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1438 | /// |
| 1439 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1440 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1441 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1442 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1443 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1444 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1445 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1446 | unsigned IROffset, QualType SourceTy, |
| 1447 | unsigned SourceOffset) const; |
| 1448 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1449 | unsigned IROffset, QualType SourceTy, |
| 1450 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1451 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1452 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1453 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1454 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1455 | |
| 1456 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1457 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1458 | /// |
| 1459 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1460 | /// available. |
| 1461 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1463 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1464 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1465 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1466 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1467 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1468 | unsigned &neededSSE, |
| 1469 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1470 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1471 | bool IsIllegalVectorType(QualType Ty) const; |
| 1472 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1473 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1474 | /// unfortunately in ways that were not always consistent with |
| 1475 | /// certain previous compilers. In particular, platforms which |
| 1476 | /// required strict binary compatibility with older versions of GCC |
| 1477 | /// may need to exempt themselves. |
| 1478 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1479 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1482 | bool HasAVX; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1483 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1484 | // 64-bit hardware. |
| 1485 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1486 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1487 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1488 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1489 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1490 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1491 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1492 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1493 | bool isPassedUsingAVXType(QualType type) const { |
| 1494 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1495 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1496 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1497 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1498 | if (info.isDirect()) { |
| 1499 | llvm::Type *ty = info.getCoerceToType(); |
| 1500 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1501 | return (vectorTy->getBitWidth() > 128); |
| 1502 | } |
| 1503 | return false; |
| 1504 | } |
| 1505 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1506 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1507 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1508 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1509 | CodeGenFunction &CGF) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1510 | |
| 1511 | bool has64BitPointers() const { |
| 1512 | return Has64BitPointers; |
| 1513 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1514 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1515 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1516 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1517 | class WinX86_64ABIInfo : public ABIInfo { |
| 1518 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1519 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1520 | bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1521 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1522 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1523 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1524 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1525 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1526 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1527 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1528 | CodeGenFunction &CGF) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1529 | |
| 1530 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1531 | // FIXME: Assumes vectorcall is in use. |
| 1532 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1533 | } |
| 1534 | |
| 1535 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1536 | uint64_t NumMembers) const override { |
| 1537 | // FIXME: Assumes vectorcall is in use. |
| 1538 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1539 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1540 | }; |
| 1541 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1542 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1543 | bool HasAVX; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1544 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1545 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1546 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1547 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1548 | const X86_64ABIInfo &getABIInfo() const { |
| 1549 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1550 | } |
| 1551 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1552 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1553 | return 7; |
| 1554 | } |
| 1555 | |
| 1556 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1557 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1558 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1559 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1560 | // 0-15 are the 16 integer registers. |
| 1561 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1562 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1563 | return false; |
| 1564 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1565 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1566 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1567 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1568 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1569 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1570 | } |
| 1571 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1572 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1573 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1574 | // The default CC on x86-64 sets %al to the number of SSA |
| 1575 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1576 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1577 | // that when AVX types are involved: the ABI explicitly states it is |
| 1578 | // undefined, and it doesn't work in practice because of how the ABI |
| 1579 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1580 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1581 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1582 | for (CallArgList::const_iterator |
| 1583 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1584 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1585 | HasAVXType = true; |
| 1586 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1587 | } |
| 1588 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1589 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1590 | if (!HasAVXType) |
| 1591 | return true; |
| 1592 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1593 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1594 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1597 | llvm::Constant * |
| 1598 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1599 | unsigned Sig; |
| 1600 | if (getABIInfo().has64BitPointers()) |
| 1601 | Sig = (0xeb << 0) | // jmp rel8 |
| 1602 | (0x0a << 8) | // .+0x0c |
| 1603 | ('F' << 16) | |
| 1604 | ('T' << 24); |
| 1605 | else |
| 1606 | Sig = (0xeb << 0) | // jmp rel8 |
| 1607 | (0x06 << 8) | // .+0x08 |
| 1608 | ('F' << 16) | |
| 1609 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1610 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1611 | } |
| 1612 | |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1613 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 1614 | return HasAVX ? 32 : 16; |
| 1615 | } |
Joerg Sonnenberger | 096feeb | 2015-02-23 20:23:47 +0000 | [diff] [blame] | 1616 | |
| 1617 | bool hasSjLjLowering(CodeGen::CodeGenFunction &CGF) const override { |
| 1618 | return true; |
| 1619 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1620 | }; |
| 1621 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1622 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1623 | public: |
| 1624 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 1625 | : X86_64TargetCodeGenInfo(CGT, HasAVX) {} |
| 1626 | |
| 1627 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 1628 | llvm::SmallString<24> &Opt) const { |
| 1629 | Opt = "\01"; |
| 1630 | Opt += Lib; |
| 1631 | } |
| 1632 | }; |
| 1633 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1634 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1635 | // If the argument does not end in .lib, automatically add the suffix. |
| 1636 | // If the argument contains a space, enclose it in quotes. |
| 1637 | // This matches the behavior of MSVC. |
| 1638 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1639 | std::string ArgStr = Quote ? "\"" : ""; |
| 1640 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1641 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1642 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1643 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1644 | return ArgStr; |
| 1645 | } |
| 1646 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1647 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1648 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1649 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1650 | bool d, bool p, bool w, unsigned RegParms) |
| 1651 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1652 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1653 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1654 | CodeGen::CodeGenModule &CGM) const override; |
| 1655 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1656 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1657 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1658 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1659 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1660 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1661 | |
| 1662 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1663 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1664 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1665 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1666 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1667 | }; |
| 1668 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1669 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1670 | llvm::GlobalValue *GV, |
| 1671 | CodeGen::CodeGenModule &CGM) { |
| 1672 | if (isa<FunctionDecl>(D)) { |
| 1673 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1674 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1675 | |
| 1676 | Fn->addFnAttr("stack-probe-size", llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | void WinX86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1682 | llvm::GlobalValue *GV, |
| 1683 | CodeGen::CodeGenModule &CGM) const { |
| 1684 | X86_32TargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 1685 | |
| 1686 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1687 | } |
| 1688 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1689 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1690 | bool HasAVX; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1691 | public: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1692 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 1693 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)), HasAVX(HasAVX) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1694 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1695 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1696 | CodeGen::CodeGenModule &CGM) const override; |
| 1697 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1698 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1699 | return 7; |
| 1700 | } |
| 1701 | |
| 1702 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1703 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1704 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1706 | // 0-15 are the 16 integer registers. |
| 1707 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1708 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1709 | return false; |
| 1710 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1711 | |
| 1712 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1713 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1714 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1715 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1716 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1717 | |
| 1718 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1719 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1720 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1721 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1722 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1723 | |
| 1724 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 1725 | return HasAVX ? 32 : 16; |
| 1726 | } |
Reid Kleckner | 533bd17 | 2015-03-04 19:24:16 +0000 | [diff] [blame] | 1727 | |
| 1728 | bool hasSjLjLowering(CodeGen::CodeGenFunction &CGF) const override { |
| 1729 | return true; |
| 1730 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1731 | }; |
| 1732 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1733 | void WinX86_64TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1734 | llvm::GlobalValue *GV, |
| 1735 | CodeGen::CodeGenModule &CGM) const { |
| 1736 | TargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 1737 | |
| 1738 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1739 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1742 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1743 | Class &Hi) const { |
| 1744 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1745 | // |
| 1746 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1747 | // memory. |
| 1748 | // |
| 1749 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1750 | // memory. |
| 1751 | // |
| 1752 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1753 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1754 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1755 | // ABI working for processors that don't support the __m256 type. |
| 1756 | // |
| 1757 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1758 | // |
| 1759 | // Some of these are enforced by the merging logic. Others can arise |
| 1760 | // only with unions; for example: |
| 1761 | // union { _Complex double; unsigned; } |
| 1762 | // |
| 1763 | // Note that clauses (b) and (c) were added in 0.98. |
| 1764 | // |
| 1765 | if (Hi == Memory) |
| 1766 | Lo = Memory; |
| 1767 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1768 | Lo = Memory; |
| 1769 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1770 | Lo = Memory; |
| 1771 | if (Hi == SSEUp && Lo != SSE) |
| 1772 | Hi = SSE; |
| 1773 | } |
| 1774 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1775 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1776 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1777 | // classified recursively so that always two fields are |
| 1778 | // considered. The resulting class is calculated according to |
| 1779 | // the classes of the fields in the eightbyte: |
| 1780 | // |
| 1781 | // (a) If both classes are equal, this is the resulting class. |
| 1782 | // |
| 1783 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1784 | // the other class. |
| 1785 | // |
| 1786 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1787 | // class. |
| 1788 | // |
| 1789 | // (d) If one of the classes is INTEGER, the result is the |
| 1790 | // INTEGER. |
| 1791 | // |
| 1792 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1793 | // MEMORY is used as class. |
| 1794 | // |
| 1795 | // (f) Otherwise class SSE is used. |
| 1796 | |
| 1797 | // Accum should never be memory (we should have returned) or |
| 1798 | // ComplexX87 (because this cannot be passed in a structure). |
| 1799 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1800 | "Invalid accumulated classification during merge."); |
| 1801 | if (Accum == Field || Field == NoClass) |
| 1802 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1803 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1804 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1805 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1806 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1807 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1808 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1809 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1810 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1811 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1812 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1815 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1816 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1817 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1818 | // Class pairs with appropriate constructor methods for the various |
| 1819 | // situations. |
| 1820 | |
| 1821 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1822 | // shouldn't be passed in registers for example, so there is no chance they |
| 1823 | // can straddle an eightbyte. Verify & simplify. |
| 1824 | |
| 1825 | Lo = Hi = NoClass; |
| 1826 | |
| 1827 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1828 | Current = Memory; |
| 1829 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1830 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1831 | BuiltinType::Kind k = BT->getKind(); |
| 1832 | |
| 1833 | if (k == BuiltinType::Void) { |
| 1834 | Current = NoClass; |
| 1835 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1836 | Lo = Integer; |
| 1837 | Hi = Integer; |
| 1838 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1839 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1840 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1841 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1842 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1843 | Current = SSE; |
| 1844 | } else if (k == BuiltinType::LongDouble) { |
| 1845 | Lo = X87; |
| 1846 | Hi = X87Up; |
| 1847 | } |
| 1848 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1849 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1850 | return; |
| 1851 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1852 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1853 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1854 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1855 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1856 | return; |
| 1857 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1858 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1859 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1860 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1861 | return; |
| 1862 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1863 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1864 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1865 | if (Ty->isMemberFunctionPointerType()) { |
| 1866 | if (Has64BitPointers) { |
| 1867 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 1868 | // Lo and Hi now. |
| 1869 | Lo = Hi = Integer; |
| 1870 | } else { |
| 1871 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 1872 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 1873 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 1874 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 1875 | if (EB_FuncPtr != EB_ThisAdj) { |
| 1876 | Lo = Hi = Integer; |
| 1877 | } else { |
| 1878 | Current = Integer; |
| 1879 | } |
| 1880 | } |
| 1881 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1882 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1883 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1884 | return; |
| 1885 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1887 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1888 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1889 | if (Size == 32) { |
| 1890 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1891 | // float> as integer. |
| 1892 | Current = Integer; |
| 1893 | |
| 1894 | // If this type crosses an eightbyte boundary, it should be |
| 1895 | // split. |
| 1896 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1897 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1898 | if (EB_Real != EB_Imag) |
| 1899 | Hi = Lo; |
| 1900 | } else if (Size == 64) { |
| 1901 | // gcc passes <1 x double> in memory. :( |
| 1902 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1903 | return; |
| 1904 | |
| 1905 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1906 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1907 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1908 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1909 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1910 | Current = Integer; |
| 1911 | else |
| 1912 | Current = SSE; |
| 1913 | |
| 1914 | // If this type crosses an eightbyte boundary, it should be |
| 1915 | // split. |
| 1916 | if (OffsetBase && OffsetBase != 64) |
| 1917 | Hi = Lo; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1918 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1919 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1920 | // least significant one belongs to class SSE and all the others to class |
| 1921 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1922 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1923 | // This design isn't correct for 256-bits, but since there're no cases |
| 1924 | // where the upper parts would need to be inspected, avoid adding |
| 1925 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1926 | // |
| 1927 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1928 | // registers if they are "named", i.e. not part of the "..." of a |
| 1929 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1930 | Lo = SSE; |
| 1931 | Hi = SSEUp; |
| 1932 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1933 | return; |
| 1934 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1936 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1937 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1938 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1939 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1940 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1941 | if (Size <= 64) |
| 1942 | Current = Integer; |
| 1943 | else if (Size <= 128) |
| 1944 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1945 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1946 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1947 | else if (ET == getContext().DoubleTy || |
| 1948 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1949 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1950 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1951 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1952 | Current = ComplexX87; |
| 1953 | |
| 1954 | // If this complex type crosses an eightbyte boundary then it |
| 1955 | // should be split. |
| 1956 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1957 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1958 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1959 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1960 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1961 | return; |
| 1962 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1963 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1964 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1965 | // Arrays are treated like structures. |
| 1966 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1967 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1968 | |
| 1969 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1970 | // than four eightbytes, ..., it has class MEMORY. |
| 1971 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1972 | return; |
| 1973 | |
| 1974 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1975 | // fields, it has class MEMORY. |
| 1976 | // |
| 1977 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1978 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1979 | return; |
| 1980 | |
| 1981 | // Otherwise implement simplified merge. We could be smarter about |
| 1982 | // this, but it isn't worth it and would be harder to verify. |
| 1983 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1984 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1985 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1986 | |
| 1987 | // The only case a 256-bit wide vector could be used is when the array |
| 1988 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1989 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1990 | if (Size > 128 && EltSize != 256) |
| 1991 | return; |
| 1992 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1993 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1994 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1995 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1996 | Lo = merge(Lo, FieldLo); |
| 1997 | Hi = merge(Hi, FieldHi); |
| 1998 | if (Lo == Memory || Hi == Memory) |
| 1999 | break; |
| 2000 | } |
| 2001 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2002 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2003 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2004 | return; |
| 2005 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2007 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2008 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2009 | |
| 2010 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2011 | // than four eightbytes, ..., it has class MEMORY. |
| 2012 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2013 | return; |
| 2014 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2015 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2016 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2017 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2018 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2019 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2020 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2021 | const RecordDecl *RD = RT->getDecl(); |
| 2022 | |
| 2023 | // Assume variable sized types are passed in memory. |
| 2024 | if (RD->hasFlexibleArrayMember()) |
| 2025 | return; |
| 2026 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2027 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2028 | |
| 2029 | // Reset Lo class, this will be recomputed. |
| 2030 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2031 | |
| 2032 | // If this is a C++ record, classify the bases first. |
| 2033 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2034 | for (const auto &I : CXXRD->bases()) { |
| 2035 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2036 | "Unexpected base class!"); |
| 2037 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2038 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Classify this field. |
| 2041 | // |
| 2042 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2043 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2044 | // initialized to class NO_CLASS. |
| 2045 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2046 | uint64_t Offset = |
| 2047 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2048 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2049 | Lo = merge(Lo, FieldLo); |
| 2050 | Hi = merge(Hi, FieldHi); |
| 2051 | if (Lo == Memory || Hi == Memory) |
| 2052 | break; |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2057 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2058 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2059 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2060 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2061 | bool BitField = i->isBitField(); |
| 2062 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2063 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2064 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2065 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2066 | // The only case a 256-bit wide vector could be used is when the struct |
| 2067 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2068 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2069 | // |
| 2070 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2071 | Lo = Memory; |
| 2072 | return; |
| 2073 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2074 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2075 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2076 | Lo = Memory; |
| 2077 | return; |
| 2078 | } |
| 2079 | |
| 2080 | // Classify this field. |
| 2081 | // |
| 2082 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2083 | // exceeds a single eightbyte, each is classified |
| 2084 | // separately. Each eightbyte gets initialized to class |
| 2085 | // NO_CLASS. |
| 2086 | Class FieldLo, FieldHi; |
| 2087 | |
| 2088 | // Bit-fields require special handling, they do not force the |
| 2089 | // structure to be passed in memory even if unaligned, and |
| 2090 | // therefore they can straddle an eightbyte. |
| 2091 | if (BitField) { |
| 2092 | // Ignore padding bit-fields. |
| 2093 | if (i->isUnnamedBitfield()) |
| 2094 | continue; |
| 2095 | |
| 2096 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2097 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2098 | |
| 2099 | uint64_t EB_Lo = Offset / 64; |
| 2100 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2101 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2102 | if (EB_Lo) { |
| 2103 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2104 | FieldLo = NoClass; |
| 2105 | FieldHi = Integer; |
| 2106 | } else { |
| 2107 | FieldLo = Integer; |
| 2108 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2109 | } |
| 2110 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2111 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2112 | Lo = merge(Lo, FieldLo); |
| 2113 | Hi = merge(Hi, FieldHi); |
| 2114 | if (Lo == Memory || Hi == Memory) |
| 2115 | break; |
| 2116 | } |
| 2117 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2118 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2119 | } |
| 2120 | } |
| 2121 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2122 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2123 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2124 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2125 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2126 | // Treat an enum type as its underlying type. |
| 2127 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2128 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2129 | |
| 2130 | return (Ty->isPromotableIntegerType() ? |
| 2131 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2132 | } |
| 2133 | |
| 2134 | return ABIArgInfo::getIndirect(0); |
| 2135 | } |
| 2136 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2137 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2138 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2139 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 2140 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 2141 | if (Size <= 64 || Size > LargestVector) |
| 2142 | return true; |
| 2143 | } |
| 2144 | |
| 2145 | return false; |
| 2146 | } |
| 2147 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2148 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2149 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2150 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2151 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2152 | // |
| 2153 | // This assumption is optimistic, as there could be free registers available |
| 2154 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2155 | // the argument in the free register. This does not seem to happen currently, |
| 2156 | // but this code would be much safer if we could mark the argument with |
| 2157 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2158 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2159 | // Treat an enum type as its underlying type. |
| 2160 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2161 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2162 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2163 | return (Ty->isPromotableIntegerType() ? |
| 2164 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2165 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2166 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2167 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2168 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2169 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2170 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2171 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2172 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2173 | |
| 2174 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2175 | // is important for good codegen. |
| 2176 | // |
| 2177 | // We do this by coercing the value into a scalar type which the backend can |
| 2178 | // handle naturally (i.e., without using byval). |
| 2179 | // |
| 2180 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2181 | // free integer registers. Doing this when there are free integer registers |
| 2182 | // would require more care, as we would have to ensure that the coerced value |
| 2183 | // did not claim the unused register. That would require either reording the |
| 2184 | // arguments to the function (so that any subsequent inreg values came first), |
| 2185 | // or only doing this optimization when there were no following arguments that |
| 2186 | // might be inreg. |
| 2187 | // |
| 2188 | // We currently expect it to be rare (particularly in well written code) for |
| 2189 | // arguments to be passed on the stack when there are still free integer |
| 2190 | // registers available (this would typically imply large structs being passed |
| 2191 | // by value), so this seems like a fair tradeoff for now. |
| 2192 | // |
| 2193 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2194 | // attributes. See PR12193. |
| 2195 | if (freeIntRegs == 0) { |
| 2196 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2197 | |
| 2198 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2199 | // type, which will end up on the stack (with alignment 8). |
| 2200 | if (Align == 8 && Size <= 64) |
| 2201 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2202 | Size)); |
| 2203 | } |
| 2204 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2205 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2208 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2209 | /// register. Pick an LLVM IR type that will be passed as a vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2210 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2211 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2212 | // vectors; strip them off if present. |
| 2213 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2214 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2215 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2216 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 2217 | assert(isa<llvm::VectorType>(IRType) && |
| 2218 | "Trying to return a non-vector type in a vector register!"); |
| 2219 | return IRType; |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2222 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2223 | /// is known to either be off the end of the specified type or being in |
| 2224 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2225 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2226 | /// classification that put one of the two halves in the INTEGER class. |
| 2227 | /// |
| 2228 | /// It is conservatively correct to return false. |
| 2229 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2230 | unsigned EndBit, ASTContext &Context) { |
| 2231 | // If the bytes being queried are off the end of the type, there is no user |
| 2232 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2233 | // types that don't contain interesting padding. |
| 2234 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2235 | if (TySize <= StartBit) |
| 2236 | return true; |
| 2237 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2238 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2239 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2240 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2241 | |
| 2242 | // Check each element to see if the element overlaps with the queried range. |
| 2243 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2244 | // If the element is after the span we care about, then we're done.. |
| 2245 | unsigned EltOffset = i*EltSize; |
| 2246 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2248 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2249 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2250 | EndBit-EltOffset, Context)) |
| 2251 | return false; |
| 2252 | } |
| 2253 | // If it overlaps no elements, then it is safe to process as padding. |
| 2254 | return true; |
| 2255 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2256 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2257 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2258 | const RecordDecl *RD = RT->getDecl(); |
| 2259 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2260 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2261 | // If this is a C++ record, check the bases first. |
| 2262 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2263 | for (const auto &I : CXXRD->bases()) { |
| 2264 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2265 | "Unexpected base class!"); |
| 2266 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2267 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2268 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2269 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2270 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2271 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2272 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2273 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2274 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2275 | EndBit-BaseOffset, Context)) |
| 2276 | return false; |
| 2277 | } |
| 2278 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2279 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2280 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2281 | // this could be sped up a lot by being smarter about queried fields, |
| 2282 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2283 | // much. |
| 2284 | unsigned idx = 0; |
| 2285 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2286 | i != e; ++i, ++idx) { |
| 2287 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2288 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2289 | // If we found a field after the region we care about, then we're done. |
| 2290 | if (FieldOffset >= EndBit) break; |
| 2291 | |
| 2292 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2293 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2294 | Context)) |
| 2295 | return false; |
| 2296 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2298 | // If nothing in this record overlapped the area of interest, then we're |
| 2299 | // clean. |
| 2300 | return true; |
| 2301 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2303 | return false; |
| 2304 | } |
| 2305 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2306 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2307 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2308 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2309 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2310 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2311 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2312 | // Base case if we find a float. |
| 2313 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2314 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2316 | // If this is a struct, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2317 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2318 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2319 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2320 | IROffset -= SL->getElementOffset(Elt); |
| 2321 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2322 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2323 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2324 | // If this is an array, recurse into the field at the specified offset. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2325 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2326 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2327 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2328 | IROffset -= IROffset/EltSize*EltSize; |
| 2329 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2330 | } |
| 2331 | |
| 2332 | return false; |
| 2333 | } |
| 2334 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2335 | |
| 2336 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2337 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2338 | llvm::Type *X86_64ABIInfo:: |
| 2339 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2340 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2341 | // The only three choices we have are either double, <2 x float>, or float. We |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2342 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2343 | // structs that contain 3 floats. |
| 2344 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2345 | SourceOffset*8+64, getContext())) |
| 2346 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2347 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2348 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2349 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2350 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2351 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2352 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2353 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2354 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2355 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2356 | } |
| 2357 | |
| 2358 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2359 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2360 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2361 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2362 | /// the best LLVM IR type to represent this, which may be i64 or may be anything |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2363 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2364 | /// etc). |
| 2365 | /// |
| 2366 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2367 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2368 | /// the 8-byte value references. PrefType may be null. |
| 2369 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2370 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2371 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2372 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2373 | llvm::Type *X86_64ABIInfo:: |
| 2374 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2375 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2376 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2377 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2378 | if (IROffset == 0) { |
| 2379 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2380 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2381 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2382 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2383 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2384 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2385 | // goodness in the source type is just tail padding. This is allowed to |
| 2386 | // kick in for struct {double,int} on the int, but not on |
| 2387 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2388 | // have to do this analysis on the source type because we can't depend on |
| 2389 | // unions being lowered a specific way etc. |
| 2390 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2391 | IRType->isIntegerTy(32) || |
| 2392 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2393 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2394 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2395 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2396 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2397 | SourceOffset*8+64, getContext())) |
| 2398 | return IRType; |
| 2399 | } |
| 2400 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2401 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2402 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2403 | // If this is a struct, recurse into the field at the specified offset. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2404 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2405 | if (IROffset < SL->getSizeInBytes()) { |
| 2406 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2407 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2408 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2409 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2410 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2411 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2412 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2414 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2415 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2416 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2417 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2418 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2419 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2420 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2422 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2423 | // integer register that isn't too big to fit the rest of the struct. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2424 | unsigned TySizeInBytes = |
| 2425 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2426 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2427 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2429 | // It is always safe to classify this as an integer type up to i64 that |
| 2430 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2431 | return llvm::IntegerType::get(getVMContext(), |
| 2432 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2435 | |
| 2436 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2437 | /// be used as elements of a two register pair to pass or return, return a |
| 2438 | /// first class aggregate to represent them. For example, if the low part of |
| 2439 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2440 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2441 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2442 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2443 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2444 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2445 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2446 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2447 | // the second element at offset 8. Check for this: |
| 2448 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2449 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2450 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2451 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2452 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2453 | // To handle this, we have to increase the size of the low part so that the |
| 2454 | // second element will start at an 8 byte offset. We can't increase the size |
| 2455 | // of the second element because it might make us access off the end of the |
| 2456 | // struct. |
| 2457 | if (HiStart != 8) { |
| 2458 | // There are only two sorts of types the ABI generation code can produce for |
| 2459 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2460 | // Promote these to a larger type. |
| 2461 | if (Lo->isFloatTy()) |
| 2462 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2463 | else { |
| 2464 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2465 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2466 | } |
| 2467 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2468 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2469 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2470 | |
| 2471 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2472 | // Verify that the second element is at an 8-byte offset. |
| 2473 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2474 | "Invalid x86-64 argument pair!"); |
| 2475 | return Result; |
| 2476 | } |
| 2477 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2478 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2479 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2480 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2481 | // classification algorithm. |
| 2482 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2483 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2484 | |
| 2485 | // Check some invariants. |
| 2486 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2487 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2488 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2489 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2490 | switch (Lo) { |
| 2491 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2492 | if (Hi == NoClass) |
| 2493 | return ABIArgInfo::getIgnore(); |
| 2494 | // If the low part is just padding, it takes no register, leave ResType |
| 2495 | // null. |
| 2496 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2497 | "Unknown missing lo part"); |
| 2498 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2499 | |
| 2500 | case SSEUp: |
| 2501 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2502 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2503 | |
| 2504 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2505 | // hidden argument. |
| 2506 | case Memory: |
| 2507 | return getIndirectReturnResult(RetTy); |
| 2508 | |
| 2509 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2510 | // available register of the sequence %rax, %rdx is used. |
| 2511 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2512 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2514 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2515 | // so that the parameter gets the right LLVM IR attributes. |
| 2516 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2517 | // Treat an enum type as its underlying type. |
| 2518 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2519 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2520 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2521 | if (RetTy->isIntegralOrEnumerationType() && |
| 2522 | RetTy->isPromotableIntegerType()) |
| 2523 | return ABIArgInfo::getExtend(); |
| 2524 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2525 | break; |
| 2526 | |
| 2527 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2528 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2529 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2530 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2531 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2532 | |
| 2533 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2534 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2535 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2536 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2537 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2538 | |
| 2539 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2540 | // part of the value is returned in %st0 and the imaginary part in |
| 2541 | // %st1. |
| 2542 | case ComplexX87: |
| 2543 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2544 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2545 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2546 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2547 | break; |
| 2548 | } |
| 2549 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2550 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2551 | switch (Hi) { |
| 2552 | // Memory was handled previously and X87 should |
| 2553 | // never occur as a hi class. |
| 2554 | case Memory: |
| 2555 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2556 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2557 | |
| 2558 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2559 | case NoClass: |
| 2560 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2562 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2563 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2564 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2565 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2566 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2567 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2568 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2569 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2570 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2571 | break; |
| 2572 | |
| 2573 | // AMD64-ABI 3.2.3p4: Rule 5. If the class is SSEUP, the eightbyte |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2574 | // is passed in the next available eightbyte chunk if the last used |
| 2575 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2576 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2577 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2578 | case SSEUp: |
| 2579 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2580 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2581 | break; |
| 2582 | |
| 2583 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2584 | // returned together with the previous X87 value in %st0. |
| 2585 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2586 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2587 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2588 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2589 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2590 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2591 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2592 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2593 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2594 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2595 | break; |
| 2596 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2597 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2598 | // If a high part was specified, merge it together with the low part. It is |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2599 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2600 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2601 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2602 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2603 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2604 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2607 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2608 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2609 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2610 | const |
| 2611 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2612 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2613 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2614 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2615 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2616 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2617 | // Check some invariants. |
| 2618 | // FIXME: Enforce these by construction. |
| 2619 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2620 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2621 | |
| 2622 | neededInt = 0; |
| 2623 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2624 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2625 | switch (Lo) { |
| 2626 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2627 | if (Hi == NoClass) |
| 2628 | return ABIArgInfo::getIgnore(); |
| 2629 | // If the low part is just padding, it takes no register, leave ResType |
| 2630 | // null. |
| 2631 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2632 | "Unknown missing lo part"); |
| 2633 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2634 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2635 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2636 | // on the stack. |
| 2637 | case Memory: |
| 2638 | |
| 2639 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2640 | // COMPLEX_X87, it is passed in memory. |
| 2641 | case X87: |
| 2642 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2643 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2644 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2645 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2646 | |
| 2647 | case SSEUp: |
| 2648 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2649 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2650 | |
| 2651 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2652 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2653 | // and %r9 is used. |
| 2654 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2655 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2656 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2657 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2658 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2659 | |
| 2660 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2661 | // so that the parameter gets the right LLVM IR attributes. |
| 2662 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2663 | // Treat an enum type as its underlying type. |
| 2664 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2665 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2666 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2667 | if (Ty->isIntegralOrEnumerationType() && |
| 2668 | Ty->isPromotableIntegerType()) |
| 2669 | return ABIArgInfo::getExtend(); |
| 2670 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2671 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2672 | break; |
| 2673 | |
| 2674 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2675 | // available SSE register is used, the registers are taken in the |
| 2676 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2677 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2678 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2679 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2680 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2681 | break; |
| 2682 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2683 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2684 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2685 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2686 | switch (Hi) { |
| 2687 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2688 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2689 | // which is passed in memory. |
| 2690 | case Memory: |
| 2691 | case X87: |
| 2692 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2693 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2694 | |
| 2695 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2697 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2698 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2699 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2700 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2702 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2703 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2704 | break; |
| 2705 | |
| 2706 | // X87Up generally doesn't occur here (long double is passed in |
| 2707 | // memory), except in situations involving unions. |
| 2708 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2709 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2710 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2712 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2713 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2714 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2715 | ++neededSSE; |
| 2716 | break; |
| 2717 | |
| 2718 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2719 | // eightbyte is passed in the upper half of the last used SSE |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2720 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2721 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2722 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2723 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2724 | break; |
| 2725 | } |
| 2726 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2727 | // If a high part was specified, merge it together with the low part. It is |
| 2728 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2729 | // first class struct aggregate with the high and low part: {low, high} |
| 2730 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2731 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2732 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2733 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2736 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2737 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2738 | if (!getCXXABI().classifyReturnType(FI)) |
| 2739 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2740 | |
| 2741 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2742 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2743 | |
| 2744 | // If the return value is indirect, then the hidden argument is consuming one |
| 2745 | // integer register. |
| 2746 | if (FI.getReturnInfo().isIndirect()) |
| 2747 | --freeIntRegs; |
| 2748 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 2749 | // The chain argument effectively gives us another free register. |
| 2750 | if (FI.isChainCall()) |
| 2751 | ++freeIntRegs; |
| 2752 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2753 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2754 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2755 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2756 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2757 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2758 | it != ie; ++it, ++ArgNo) { |
| 2759 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2760 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2761 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2762 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2763 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2764 | |
| 2765 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2766 | // eightbyte of an argument, the whole argument is passed on the |
| 2767 | // stack. If registers have already been assigned for some |
| 2768 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2769 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2770 | freeIntRegs -= neededInt; |
| 2771 | freeSSERegs -= neededSSE; |
| 2772 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2773 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2779 | QualType Ty, |
| 2780 | CodeGenFunction &CGF) { |
| 2781 | llvm::Value *overflow_arg_area_p = |
| 2782 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2783 | llvm::Value *overflow_arg_area = |
| 2784 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2785 | |
| 2786 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2787 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2788 | // It isn't stated explicitly in the standard, but in practice we use |
| 2789 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2790 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2791 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2792 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2793 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2794 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2795 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2796 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2797 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2798 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2799 | overflow_arg_area = |
| 2800 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2801 | overflow_arg_area->getType(), |
| 2802 | "overflow_arg_area.align"); |
| 2803 | } |
| 2804 | |
| 2805 | // AMD64-ABI 3.5.7p5: Step 8. Fetch type from l->overflow_arg_area. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2806 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2807 | llvm::Value *Res = |
| 2808 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2809 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2810 | |
| 2811 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2812 | // l->overflow_arg_area + sizeof(type). |
| 2813 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2814 | // an 8 byte boundary. |
| 2815 | |
| 2816 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2817 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2818 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2819 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2820 | "overflow_arg_area.next"); |
| 2821 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2822 | |
| 2823 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2824 | return Res; |
| 2825 | } |
| 2826 | |
| 2827 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2828 | CodeGenFunction &CGF) const { |
| 2829 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2830 | // struct { |
| 2831 | // i32 gp_offset; |
| 2832 | // i32 fp_offset; |
| 2833 | // i8* overflow_arg_area; |
| 2834 | // i8* reg_save_area; |
| 2835 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2836 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2837 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2838 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2839 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2840 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2841 | |
| 2842 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2843 | // in the registers. If not go to step 7. |
| 2844 | if (!neededInt && !neededSSE) |
| 2845 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2846 | |
| 2847 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2848 | // general purpose registers needed to pass type and num_fp to hold |
| 2849 | // the number of floating point registers needed. |
| 2850 | |
| 2851 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2852 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2853 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2854 | // |
| 2855 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2856 | // register save space). |
| 2857 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2858 | llvm::Value *InRegs = nullptr; |
| 2859 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2860 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2861 | if (neededInt) { |
| 2862 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2863 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2864 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2865 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | if (neededSSE) { |
| 2869 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2870 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2871 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2872 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2873 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2874 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2875 | } |
| 2876 | |
| 2877 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2878 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2879 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2880 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2881 | |
| 2882 | // Emit code to load the value if it was passed in registers. |
| 2883 | |
| 2884 | CGF.EmitBlock(InRegBlock); |
| 2885 | |
| 2886 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2887 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2888 | // copying to a temporary location in case the parameter is passed |
| 2889 | // in different register classes or requires an alignment greater |
| 2890 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2891 | // |
| 2892 | // FIXME: This really results in shameful code when we end up needing to |
| 2893 | // collect arguments from different places; often what should result in a |
| 2894 | // simple assembling of a structure from scattered addresses has many more |
| 2895 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2896 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2897 | llvm::Value *RegAddr = |
| 2898 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2899 | "reg_save_area"); |
| 2900 | if (neededInt && neededSSE) { |
| 2901 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2902 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2903 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2904 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2905 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2906 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2907 | llvm::Type *TyLo = ST->getElementType(0); |
| 2908 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2909 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2910 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2911 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2912 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2913 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2914 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2915 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2916 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2917 | llvm::Value *V = |
| 2918 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2919 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2920 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2921 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2922 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2923 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2924 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2925 | } else if (neededInt) { |
| 2926 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2927 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2928 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2931 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2932 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2933 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2934 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2935 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2936 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2937 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2938 | RegAddr = Tmp; |
| 2939 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2940 | } else if (neededSSE == 1) { |
| 2941 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2942 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2943 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2944 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2945 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2946 | // SSE registers are spaced 16 bytes apart in the register save |
| 2947 | // area, we need to collect the two eightbytes together. |
| 2948 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2949 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2950 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2951 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2952 | llvm::PointerType::getUnqual(DoubleTy); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2953 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2954 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2955 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2956 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2957 | DblPtrTy)); |
| 2958 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2959 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2960 | DblPtrTy)); |
| 2961 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2962 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2963 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2967 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2968 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2969 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2970 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2971 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2972 | gp_offset_p); |
| 2973 | } |
| 2974 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2975 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2976 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2977 | fp_offset_p); |
| 2978 | } |
| 2979 | CGF.EmitBranch(ContBlock); |
| 2980 | |
| 2981 | // Emit code to load the value if it was passed in memory. |
| 2982 | |
| 2983 | CGF.EmitBlock(InMemBlock); |
| 2984 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2985 | |
| 2986 | // Return the appropriate result. |
| 2987 | |
| 2988 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2989 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2990 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2991 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2992 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2993 | return ResAddr; |
| 2994 | } |
| 2995 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2996 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 2997 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2998 | |
| 2999 | if (Ty->isVoidType()) |
| 3000 | return ABIArgInfo::getIgnore(); |
| 3001 | |
| 3002 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3003 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3004 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3005 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3006 | uint64_t Width = Info.Width; |
| 3007 | unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3008 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3009 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3010 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3011 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3012 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3013 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 3014 | } |
| 3015 | |
| 3016 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3017 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3018 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3019 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3020 | if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3021 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3022 | Width)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3023 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3024 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3025 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3026 | // other targets. |
| 3027 | const Type *Base = nullptr; |
| 3028 | uint64_t NumElts = 0; |
| 3029 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3030 | if (FreeSSERegs >= NumElts) { |
| 3031 | FreeSSERegs -= NumElts; |
| 3032 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3033 | return ABIArgInfo::getDirect(); |
| 3034 | return ABIArgInfo::getExpand(); |
| 3035 | } |
| 3036 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3037 | } |
| 3038 | |
| 3039 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3040 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3041 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3042 | // directly. |
| 3043 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3044 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3045 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3048 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3049 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3050 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3051 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3052 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3053 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3054 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3055 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3058 | // Bool type is always extended to the ABI, other builtin types are not |
| 3059 | // extended. |
| 3060 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3061 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3062 | return ABIArgInfo::getExtend(); |
| 3063 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3064 | return ABIArgInfo::getDirect(); |
| 3065 | } |
| 3066 | |
| 3067 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3068 | bool IsVectorCall = |
| 3069 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3070 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3071 | // We can use up to 4 SSE return registers with vectorcall. |
| 3072 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3073 | if (!getCXXABI().classifyReturnType(FI)) |
| 3074 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3075 | |
| 3076 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3077 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3078 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3079 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3082 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3083 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3084 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3085 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3086 | CGBuilderTy &Builder = CGF.Builder; |
| 3087 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 3088 | "ap"); |
| 3089 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3090 | llvm::Type *PTy = |
| 3091 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3092 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3093 | |
| 3094 | uint64_t Offset = |
| 3095 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 3096 | llvm::Value *NextAddr = |
| 3097 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3098 | "ap.next"); |
| 3099 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3100 | |
| 3101 | return AddrTyped; |
| 3102 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3103 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3104 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3105 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3106 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3107 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3108 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3109 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3110 | |
| 3111 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3112 | CodeGenFunction &CGF) const override; |
| 3113 | }; |
| 3114 | |
| 3115 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3116 | public: |
| 3117 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3118 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3119 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3120 | // This is recovered from gcc output. |
| 3121 | return 1; // r1 is the dedicated stack pointer |
| 3122 | } |
| 3123 | |
| 3124 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3125 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3126 | |
| 3127 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3128 | return 16; // Natural alignment for Altivec vectors. |
| 3129 | } |
Joerg Sonnenberger | 096feeb | 2015-02-23 20:23:47 +0000 | [diff] [blame] | 3130 | |
| 3131 | bool hasSjLjLowering(CodeGen::CodeGenFunction &CGF) const override { |
| 3132 | return true; |
| 3133 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3134 | }; |
| 3135 | |
| 3136 | } |
| 3137 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3138 | llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3139 | QualType Ty, |
| 3140 | CodeGenFunction &CGF) const { |
| 3141 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3142 | // TODO: Implement this. For now ignore. |
| 3143 | (void)CTy; |
| 3144 | return nullptr; |
| 3145 | } |
| 3146 | |
| 3147 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
| 3148 | bool isInt = Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
| 3149 | llvm::Type *CharPtr = CGF.Int8PtrTy; |
| 3150 | llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy; |
| 3151 | |
| 3152 | CGBuilderTy &Builder = CGF.Builder; |
| 3153 | llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr"); |
| 3154 | llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty); |
| 3155 | llvm::Value *FPRPtrAsInt = Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1)); |
| 3156 | llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr); |
| 3157 | llvm::Value *OverflowAreaPtrAsInt = Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3)); |
| 3158 | llvm::Value *OverflowAreaPtr = Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr); |
| 3159 | llvm::Value *RegsaveAreaPtrAsInt = Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4)); |
| 3160 | llvm::Value *RegsaveAreaPtr = Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr); |
| 3161 | llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr"); |
| 3162 | // Align GPR when TY is i64. |
| 3163 | if (isI64) { |
| 3164 | llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1)); |
| 3165 | llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1)); |
| 3166 | llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1)); |
| 3167 | GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR); |
| 3168 | } |
| 3169 | llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr"); |
| 3170 | llvm::Value *OverflowArea = Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area"); |
| 3171 | llvm::Value *OverflowAreaAsInt = Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty); |
| 3172 | llvm::Value *RegsaveArea = Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area"); |
| 3173 | llvm::Value *RegsaveAreaAsInt = Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty); |
| 3174 | |
| 3175 | llvm::Value *CC = Builder.CreateICmpULT(isInt ? GPR : FPR, |
| 3176 | Builder.getInt8(8), "cond"); |
| 3177 | |
| 3178 | llvm::Value *RegConstant = Builder.CreateMul(isInt ? GPR : FPR, |
| 3179 | Builder.getInt8(isInt ? 4 : 8)); |
| 3180 | |
| 3181 | llvm::Value *OurReg = Builder.CreateAdd(RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty)); |
| 3182 | |
| 3183 | if (Ty->isFloatingType()) |
| 3184 | OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32)); |
| 3185 | |
| 3186 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3187 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3188 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3189 | |
| 3190 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3191 | |
| 3192 | CGF.EmitBlock(UsingRegs); |
| 3193 | |
| 3194 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3195 | llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy); |
| 3196 | // Increase the GPR/FPR indexes. |
| 3197 | if (isInt) { |
| 3198 | GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1)); |
| 3199 | Builder.CreateStore(GPR, GPRPtr); |
| 3200 | } else { |
| 3201 | FPR = Builder.CreateAdd(FPR, Builder.getInt8(1)); |
| 3202 | Builder.CreateStore(FPR, FPRPtr); |
| 3203 | } |
| 3204 | CGF.EmitBranch(Cont); |
| 3205 | |
| 3206 | CGF.EmitBlock(UsingOverflow); |
| 3207 | |
| 3208 | // Increase the overflow area. |
| 3209 | llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy); |
| 3210 | OverflowAreaAsInt = Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8)); |
| 3211 | Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), OverflowAreaPtr); |
| 3212 | CGF.EmitBranch(Cont); |
| 3213 | |
| 3214 | CGF.EmitBlock(Cont); |
| 3215 | |
| 3216 | llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr"); |
| 3217 | Result->addIncoming(Result1, UsingRegs); |
| 3218 | Result->addIncoming(Result2, UsingOverflow); |
| 3219 | |
| 3220 | if (Ty->isAggregateType()) { |
| 3221 | llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr") ; |
| 3222 | return Builder.CreateLoad(AGGPtr, false, "aggr"); |
| 3223 | } |
| 3224 | |
| 3225 | return Result; |
| 3226 | } |
| 3227 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3228 | bool |
| 3229 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3230 | llvm::Value *Address) const { |
| 3231 | // This is calculated from the LLVM and GCC tables and verified |
| 3232 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3233 | |
| 3234 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3235 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3236 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3237 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3238 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3239 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3240 | |
| 3241 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3242 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3243 | |
| 3244 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3245 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3246 | |
| 3247 | // 64-76 are various 4-byte special-purpose registers: |
| 3248 | // 64: mq |
| 3249 | // 65: lr |
| 3250 | // 66: ctr |
| 3251 | // 67: ap |
| 3252 | // 68-75 cr0-7 |
| 3253 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3254 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3255 | |
| 3256 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3257 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3258 | |
| 3259 | // 109: vrsave |
| 3260 | // 110: vscr |
| 3261 | // 111: spe_acc |
| 3262 | // 112: spefscr |
| 3263 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3264 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3265 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3266 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3267 | } |
| 3268 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3269 | // PowerPC-64 |
| 3270 | |
| 3271 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3272 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3273 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3274 | public: |
| 3275 | enum ABIKind { |
| 3276 | ELFv1 = 0, |
| 3277 | ELFv2 |
| 3278 | }; |
| 3279 | |
| 3280 | private: |
| 3281 | static const unsigned GPRBits = 64; |
| 3282 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3283 | bool HasQPX; |
| 3284 | |
| 3285 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3286 | // will be passed in a QPX register. |
| 3287 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3288 | if (!HasQPX) |
| 3289 | return false; |
| 3290 | |
| 3291 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3292 | unsigned NumElements = VT->getNumElements(); |
| 3293 | if (NumElements == 1) |
| 3294 | return false; |
| 3295 | |
| 3296 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3297 | if (getContext().getTypeSize(Ty) <= 256) |
| 3298 | return true; |
| 3299 | } else if (VT->getElementType()-> |
| 3300 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3301 | if (getContext().getTypeSize(Ty) <= 128) |
| 3302 | return true; |
| 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | return false; |
| 3307 | } |
| 3308 | |
| 3309 | bool IsQPXVectorTy(QualType Ty) const { |
| 3310 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3311 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3312 | |
| 3313 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3314 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3315 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3316 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3317 | bool isPromotableTypeForABI(QualType Ty) const; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3318 | bool isAlignedParamType(QualType Ty, bool &Align32) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3319 | |
| 3320 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3321 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3322 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3323 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3324 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3325 | uint64_t Members) const override; |
| 3326 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3327 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3328 | // Example: For aggregate arguments that fit in a register, we could |
| 3329 | // use getDirectInReg (as is done below for structs containing a single |
| 3330 | // floating-point value) to avoid pushing them to memory on function |
| 3331 | // entry. This would require changing the logic in PPCISelLowering |
| 3332 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3333 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3334 | if (!getCXXABI().classifyReturnType(FI)) |
| 3335 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3336 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3337 | // We rely on the default argument classification for the most part. |
| 3338 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3339 | // or vector item must be passed in a register if one is available. |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3340 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3341 | if (T) { |
| 3342 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3343 | if (IsQPXVectorTy(T) || |
| 3344 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3345 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3346 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3347 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3348 | continue; |
| 3349 | } |
| 3350 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3351 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3352 | } |
| 3353 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3354 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3355 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3356 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3357 | }; |
| 3358 | |
| 3359 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3360 | bool HasQPX; |
| 3361 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3362 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3363 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3364 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
| 3365 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)), |
| 3366 | HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3367 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3368 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3369 | // This is recovered from gcc output. |
| 3370 | return 1; // r1 is the dedicated stack pointer |
| 3371 | } |
| 3372 | |
| 3373 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3374 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3375 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3376 | unsigned getOpenMPSimdDefaultAlignment(QualType QT) const override { |
| 3377 | if (HasQPX) |
| 3378 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 3379 | if (PT->getPointeeType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 3380 | return 32; // Natural alignment for QPX doubles. |
| 3381 | |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3382 | return 16; // Natural alignment for Altivec and VSX vectors. |
| 3383 | } |
Joerg Sonnenberger | 096feeb | 2015-02-23 20:23:47 +0000 | [diff] [blame] | 3384 | |
| 3385 | bool hasSjLjLowering(CodeGen::CodeGenFunction &CGF) const override { |
| 3386 | return true; |
| 3387 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3388 | }; |
| 3389 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3390 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3391 | public: |
| 3392 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3393 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3394 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3395 | // This is recovered from gcc output. |
| 3396 | return 1; // r1 is the dedicated stack pointer |
| 3397 | } |
| 3398 | |
| 3399 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3400 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3401 | |
| 3402 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3403 | return 16; // Natural alignment for Altivec vectors. |
| 3404 | } |
Joerg Sonnenberger | 096feeb | 2015-02-23 20:23:47 +0000 | [diff] [blame] | 3405 | |
| 3406 | bool hasSjLjLowering(CodeGen::CodeGenFunction &CGF) const override { |
| 3407 | return true; |
| 3408 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3409 | }; |
| 3410 | |
| 3411 | } |
| 3412 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3413 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3414 | // extended to 64 bits. |
| 3415 | bool |
| 3416 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3417 | // Treat an enum type as its underlying type. |
| 3418 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3419 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3420 | |
| 3421 | // Promotable integer types are required to be promoted by the ABI. |
| 3422 | if (Ty->isPromotableIntegerType()) |
| 3423 | return true; |
| 3424 | |
| 3425 | // In addition to the usual promotable integer types, we also need to |
| 3426 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3427 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3428 | switch (BT->getKind()) { |
| 3429 | case BuiltinType::Int: |
| 3430 | case BuiltinType::UInt: |
| 3431 | return true; |
| 3432 | default: |
| 3433 | break; |
| 3434 | } |
| 3435 | |
| 3436 | return false; |
| 3437 | } |
| 3438 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3439 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3440 | /// alignment in the parameter area. |
| 3441 | bool |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3442 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty, bool &Align32) const { |
| 3443 | Align32 = false; |
| 3444 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3445 | // Complex types are passed just like their elements. |
| 3446 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3447 | Ty = CTy->getElementType(); |
| 3448 | |
| 3449 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3450 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3451 | if (IsQPXVectorTy(Ty)) { |
| 3452 | if (getContext().getTypeSize(Ty) > 128) |
| 3453 | Align32 = true; |
| 3454 | |
| 3455 | return true; |
| 3456 | } else if (Ty->isVectorType()) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3457 | return getContext().getTypeSize(Ty) == 128; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3458 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3459 | |
| 3460 | // For single-element float/vector structs, we consider the whole type |
| 3461 | // to have the same alignment requirements as its single element. |
| 3462 | const Type *AlignAsType = nullptr; |
| 3463 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3464 | if (EltType) { |
| 3465 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3466 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3467 | getContext().getTypeSize(EltType) == 128) || |
| 3468 | (BT && BT->isFloatingPoint())) |
| 3469 | AlignAsType = EltType; |
| 3470 | } |
| 3471 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3472 | // Likewise for ELFv2 homogeneous aggregates. |
| 3473 | const Type *Base = nullptr; |
| 3474 | uint64_t Members = 0; |
| 3475 | if (!AlignAsType && Kind == ELFv2 && |
| 3476 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3477 | AlignAsType = Base; |
| 3478 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3479 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3480 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3481 | if (getContext().getTypeSize(AlignAsType) > 128) |
| 3482 | Align32 = true; |
| 3483 | |
| 3484 | return true; |
| 3485 | } else if (AlignAsType) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3486 | return AlignAsType->isVectorType(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3487 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3488 | |
| 3489 | // Otherwise, we only need alignment for any aggregate type that |
| 3490 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3491 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3492 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
| 3493 | Align32 = true; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3494 | return true; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3495 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3496 | |
| 3497 | return false; |
| 3498 | } |
| 3499 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3500 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3501 | /// aggregate. Base is set to the base element type, and Members is set |
| 3502 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3503 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3504 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3505 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3506 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3507 | if (NElements == 0) |
| 3508 | return false; |
| 3509 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3510 | return false; |
| 3511 | Members *= NElements; |
| 3512 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3513 | const RecordDecl *RD = RT->getDecl(); |
| 3514 | if (RD->hasFlexibleArrayMember()) |
| 3515 | return false; |
| 3516 | |
| 3517 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3518 | |
| 3519 | // If this is a C++ record, check the bases first. |
| 3520 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3521 | for (const auto &I : CXXRD->bases()) { |
| 3522 | // Ignore empty records. |
| 3523 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3524 | continue; |
| 3525 | |
| 3526 | uint64_t FldMembers; |
| 3527 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3528 | return false; |
| 3529 | |
| 3530 | Members += FldMembers; |
| 3531 | } |
| 3532 | } |
| 3533 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3534 | for (const auto *FD : RD->fields()) { |
| 3535 | // Ignore (non-zero arrays of) empty records. |
| 3536 | QualType FT = FD->getType(); |
| 3537 | while (const ConstantArrayType *AT = |
| 3538 | getContext().getAsConstantArrayType(FT)) { |
| 3539 | if (AT->getSize().getZExtValue() == 0) |
| 3540 | return false; |
| 3541 | FT = AT->getElementType(); |
| 3542 | } |
| 3543 | if (isEmptyRecord(getContext(), FT, true)) |
| 3544 | continue; |
| 3545 | |
| 3546 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3547 | if (getContext().getLangOpts().CPlusPlus && |
| 3548 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3549 | continue; |
| 3550 | |
| 3551 | uint64_t FldMembers; |
| 3552 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3553 | return false; |
| 3554 | |
| 3555 | Members = (RD->isUnion() ? |
| 3556 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3557 | } |
| 3558 | |
| 3559 | if (!Base) |
| 3560 | return false; |
| 3561 | |
| 3562 | // Ensure there is no padding. |
| 3563 | if (getContext().getTypeSize(Base) * Members != |
| 3564 | getContext().getTypeSize(Ty)) |
| 3565 | return false; |
| 3566 | } else { |
| 3567 | Members = 1; |
| 3568 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3569 | Members = 2; |
| 3570 | Ty = CT->getElementType(); |
| 3571 | } |
| 3572 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3573 | // Most ABIs only support float, double, and some vector type widths. |
| 3574 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3575 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3576 | |
| 3577 | // The base type must be the same for all members. Types that |
| 3578 | // agree in both total size and mode (float vs. vector) are |
| 3579 | // treated as being equivalent here. |
| 3580 | const Type *TyPtr = Ty.getTypePtr(); |
| 3581 | if (!Base) |
| 3582 | Base = TyPtr; |
| 3583 | |
| 3584 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3585 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3586 | return false; |
| 3587 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3588 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3589 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3590 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3591 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3592 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3593 | // double, long double, or 128-bit vectors. |
| 3594 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3595 | if (BT->getKind() == BuiltinType::Float || |
| 3596 | BT->getKind() == BuiltinType::Double || |
| 3597 | BT->getKind() == BuiltinType::LongDouble) |
| 3598 | return true; |
| 3599 | } |
| 3600 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3601 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3602 | return true; |
| 3603 | } |
| 3604 | return false; |
| 3605 | } |
| 3606 | |
| 3607 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3608 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3609 | // Vector types require one register, floating point types require one |
| 3610 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3611 | uint32_t NumRegs = |
| 3612 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3613 | |
| 3614 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3615 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3618 | ABIArgInfo |
| 3619 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3620 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3621 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3622 | if (Ty->isAnyComplexType()) |
| 3623 | return ABIArgInfo::getDirect(); |
| 3624 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3625 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3626 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3627 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3628 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3629 | if (Size > 128) |
| 3630 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3631 | else if (Size < 128) { |
| 3632 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3633 | return ABIArgInfo::getDirect(CoerceTy); |
| 3634 | } |
| 3635 | } |
| 3636 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3637 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3638 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3639 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3640 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3641 | bool Align32; |
| 3642 | uint64_t ABIAlign = isAlignedParamType(Ty, Align32) ? |
| 3643 | (Align32 ? 32 : 16) : 8; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3644 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3645 | |
| 3646 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3647 | const Type *Base = nullptr; |
| 3648 | uint64_t Members = 0; |
| 3649 | if (Kind == ELFv2 && |
| 3650 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3651 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3652 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3653 | return ABIArgInfo::getDirect(CoerceTy); |
| 3654 | } |
| 3655 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3656 | // If an aggregate may end up fully in registers, we do not |
| 3657 | // use the ByVal method, but pass the aggregate as array. |
| 3658 | // This is usually beneficial since we avoid forcing the |
| 3659 | // back-end to store the argument to memory. |
| 3660 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3661 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3662 | llvm::Type *CoerceTy; |
| 3663 | |
| 3664 | // Types up to 8 bytes are passed as integer type (which will be |
| 3665 | // properly aligned in the argument save area doubleword). |
| 3666 | if (Bits <= GPRBits) |
| 3667 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3668 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3669 | // Larger types are passed as arrays, with the base type selected |
| 3670 | // according to the required alignment in the save area. |
| 3671 | else { |
| 3672 | uint64_t RegBits = ABIAlign * 8; |
| 3673 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3674 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3675 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3676 | } |
| 3677 | |
| 3678 | return ABIArgInfo::getDirect(CoerceTy); |
| 3679 | } |
| 3680 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3681 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3682 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3683 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | return (isPromotableTypeForABI(Ty) ? |
| 3687 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3688 | } |
| 3689 | |
| 3690 | ABIArgInfo |
| 3691 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3692 | if (RetTy->isVoidType()) |
| 3693 | return ABIArgInfo::getIgnore(); |
| 3694 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3695 | if (RetTy->isAnyComplexType()) |
| 3696 | return ABIArgInfo::getDirect(); |
| 3697 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3698 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3699 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3700 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3701 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3702 | if (Size > 128) |
| 3703 | return ABIArgInfo::getIndirect(0); |
| 3704 | else if (Size < 128) { |
| 3705 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3706 | return ABIArgInfo::getDirect(CoerceTy); |
| 3707 | } |
| 3708 | } |
| 3709 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3710 | if (isAggregateTypeForABI(RetTy)) { |
| 3711 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3712 | const Type *Base = nullptr; |
| 3713 | uint64_t Members = 0; |
| 3714 | if (Kind == ELFv2 && |
| 3715 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3716 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3717 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3718 | return ABIArgInfo::getDirect(CoerceTy); |
| 3719 | } |
| 3720 | |
| 3721 | // ELFv2 small aggregates are returned in up to two registers. |
| 3722 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3723 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3724 | if (Bits == 0) |
| 3725 | return ABIArgInfo::getIgnore(); |
| 3726 | |
| 3727 | llvm::Type *CoerceTy; |
| 3728 | if (Bits > GPRBits) { |
| 3729 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3730 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3731 | } else |
| 3732 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3733 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3734 | return ABIArgInfo::getDirect(CoerceTy); |
| 3735 | } |
| 3736 | |
| 3737 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3738 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3739 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3740 | |
| 3741 | return (isPromotableTypeForABI(RetTy) ? |
| 3742 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3743 | } |
| 3744 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3745 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3746 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3747 | QualType Ty, |
| 3748 | CodeGenFunction &CGF) const { |
| 3749 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3750 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3751 | |
| 3752 | CGBuilderTy &Builder = CGF.Builder; |
| 3753 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3754 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3755 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3756 | // Handle types that require 16-byte alignment in the parameter save area. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3757 | bool Align32; |
| 3758 | if (isAlignedParamType(Ty, Align32)) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3759 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 3760 | AddrAsInt = Builder.CreateAdd(AddrAsInt, |
| 3761 | Builder.getInt64(Align32 ? 31 : 15)); |
| 3762 | AddrAsInt = Builder.CreateAnd(AddrAsInt, |
| 3763 | Builder.getInt64(Align32 ? -32 : -16)); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3764 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3765 | } |
| 3766 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3767 | // Update the va_list pointer. The pointer should be bumped by the |
| 3768 | // size of the object. We can trust getTypeSize() except for a complex |
| 3769 | // type whose base type is smaller than a doubleword. For these, the |
| 3770 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3771 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3772 | QualType BaseTy; |
| 3773 | unsigned CplxBaseSize = 0; |
| 3774 | |
| 3775 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3776 | BaseTy = CTy->getElementType(); |
| 3777 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3778 | if (CplxBaseSize < 8) |
| 3779 | SizeInBytes = 16; |
| 3780 | } |
| 3781 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3782 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3783 | llvm::Value *NextAddr = |
| 3784 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3785 | "ap.next"); |
| 3786 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3787 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3788 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3789 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3790 | // in separate doublewords. However, Clang expects us to produce a |
| 3791 | // pointer to a structure with the two parts packed tightly. So generate |
| 3792 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3793 | // and store them to a temporary structure. |
| 3794 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3795 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3796 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3797 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 3798 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3799 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3800 | } else { |
| 3801 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3802 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3803 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3804 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3805 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3806 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3807 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 3808 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 3809 | "vacplx"); |
| 3810 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 3811 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 3812 | Builder.CreateStore(Real, RealPtr, false); |
| 3813 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3814 | return Ptr; |
| 3815 | } |
| 3816 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3817 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3818 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3819 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3820 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3821 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3822 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3823 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3824 | } |
| 3825 | |
| 3826 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3827 | return Builder.CreateBitCast(Addr, PTy); |
| 3828 | } |
| 3829 | |
| 3830 | static bool |
| 3831 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3832 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3833 | // This is calculated from the LLVM and GCC tables and verified |
| 3834 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3835 | |
| 3836 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3837 | |
| 3838 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3839 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3840 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3841 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3842 | |
| 3843 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3844 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3845 | |
| 3846 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3847 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3848 | |
| 3849 | // 64-76 are various 4-byte special-purpose registers: |
| 3850 | // 64: mq |
| 3851 | // 65: lr |
| 3852 | // 66: ctr |
| 3853 | // 67: ap |
| 3854 | // 68-75 cr0-7 |
| 3855 | // 76: xer |
| 3856 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3857 | |
| 3858 | // 77-108: v0-31, the 16-byte vector registers |
| 3859 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3860 | |
| 3861 | // 109: vrsave |
| 3862 | // 110: vscr |
| 3863 | // 111: spe_acc |
| 3864 | // 112: spefscr |
| 3865 | // 113: sfp |
| 3866 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3867 | |
| 3868 | return false; |
| 3869 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3870 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3871 | bool |
| 3872 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3873 | CodeGen::CodeGenFunction &CGF, |
| 3874 | llvm::Value *Address) const { |
| 3875 | |
| 3876 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3877 | } |
| 3878 | |
| 3879 | bool |
| 3880 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3881 | llvm::Value *Address) const { |
| 3882 | |
| 3883 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3884 | } |
| 3885 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3886 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3887 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3888 | //===----------------------------------------------------------------------===// |
| 3889 | |
| 3890 | namespace { |
| 3891 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3892 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3893 | public: |
| 3894 | enum ABIKind { |
| 3895 | AAPCS = 0, |
| 3896 | DarwinPCS |
| 3897 | }; |
| 3898 | |
| 3899 | private: |
| 3900 | ABIKind Kind; |
| 3901 | |
| 3902 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3903 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3904 | |
| 3905 | private: |
| 3906 | ABIKind getABIKind() const { return Kind; } |
| 3907 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3908 | |
| 3909 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3910 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3911 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3912 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3913 | uint64_t Members) const override; |
| 3914 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3915 | bool isIllegalVectorType(QualType Ty) const; |
| 3916 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 3917 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3918 | if (!getCXXABI().classifyReturnType(FI)) |
| 3919 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3920 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3921 | for (auto &it : FI.arguments()) |
| 3922 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3926 | CodeGenFunction &CGF) const; |
| 3927 | |
| 3928 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3929 | CodeGenFunction &CGF) const; |
| 3930 | |
| 3931 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
NAKAMURA Takumi | 8c89496 | 2014-11-01 01:32:27 +0000 | [diff] [blame] | 3932 | CodeGenFunction &CGF) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3933 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3934 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3935 | } |
| 3936 | }; |
| 3937 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3938 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3939 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3940 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3941 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3942 | |
| 3943 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
| 3944 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3945 | } |
| 3946 | |
| 3947 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; } |
| 3948 | |
| 3949 | virtual bool doesReturnSlotInterfereWithArgs() const { return false; } |
| 3950 | }; |
| 3951 | } |
| 3952 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3953 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3954 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3955 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3956 | // Handle illegal vector types here. |
| 3957 | if (isIllegalVectorType(Ty)) { |
| 3958 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3959 | if (Size <= 32) { |
| 3960 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3961 | return ABIArgInfo::getDirect(ResType); |
| 3962 | } |
| 3963 | if (Size == 64) { |
| 3964 | llvm::Type *ResType = |
| 3965 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3966 | return ABIArgInfo::getDirect(ResType); |
| 3967 | } |
| 3968 | if (Size == 128) { |
| 3969 | llvm::Type *ResType = |
| 3970 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3971 | return ABIArgInfo::getDirect(ResType); |
| 3972 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3973 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3974 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3975 | |
| 3976 | if (!isAggregateTypeForABI(Ty)) { |
| 3977 | // Treat an enum type as its underlying type. |
| 3978 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3979 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3980 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3981 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 3982 | ? ABIArgInfo::getExtend() |
| 3983 | : ABIArgInfo::getDirect()); |
| 3984 | } |
| 3985 | |
| 3986 | // Structures with either a non-trivial destructor or a non-trivial |
| 3987 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3988 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3989 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3990 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 3994 | // elsewhere for GNU compatibility. |
| 3995 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3996 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 3997 | return ABIArgInfo::getIgnore(); |
| 3998 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3999 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4000 | } |
| 4001 | |
| 4002 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4003 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4004 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4005 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4006 | return ABIArgInfo::getDirect( |
| 4007 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4011 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4012 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4013 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4014 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4015 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4016 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4017 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4018 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4019 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4020 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4021 | } |
| 4022 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4023 | } |
| 4024 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4025 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4026 | } |
| 4027 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4028 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4029 | if (RetTy->isVoidType()) |
| 4030 | return ABIArgInfo::getIgnore(); |
| 4031 | |
| 4032 | // Large vector types should be returned via memory. |
| 4033 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 4034 | return ABIArgInfo::getIndirect(0); |
| 4035 | |
| 4036 | if (!isAggregateTypeForABI(RetTy)) { |
| 4037 | // Treat an enum type as its underlying type. |
| 4038 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4039 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4040 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4041 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4042 | ? ABIArgInfo::getExtend() |
| 4043 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4046 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4047 | return ABIArgInfo::getIgnore(); |
| 4048 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4049 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4050 | uint64_t Members = 0; |
| 4051 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4052 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4053 | return ABIArgInfo::getDirect(); |
| 4054 | |
| 4055 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4056 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4057 | if (Size <= 128) { |
| 4058 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 4059 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4060 | } |
| 4061 | |
| 4062 | return ABIArgInfo::getIndirect(0); |
| 4063 | } |
| 4064 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4065 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4066 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4067 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4068 | // Check whether VT is legal. |
| 4069 | unsigned NumElements = VT->getNumElements(); |
| 4070 | uint64_t Size = getContext().getTypeSize(VT); |
| 4071 | // NumElements should be power of 2 between 1 and 16. |
| 4072 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4073 | return true; |
| 4074 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4075 | } |
| 4076 | return false; |
| 4077 | } |
| 4078 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4079 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4080 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4081 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4082 | // but with the difference that any floating-point type is allowed, |
| 4083 | // including __fp16. |
| 4084 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4085 | if (BT->isFloatingPoint()) |
| 4086 | return true; |
| 4087 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4088 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4089 | if (VecSize == 64 || VecSize == 128) |
| 4090 | return true; |
| 4091 | } |
| 4092 | return false; |
| 4093 | } |
| 4094 | |
| 4095 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4096 | uint64_t Members) const { |
| 4097 | return Members <= 4; |
| 4098 | } |
| 4099 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4100 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, |
| 4101 | QualType Ty, |
| 4102 | CodeGenFunction &CGF) const { |
| 4103 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4104 | bool IsIndirect = AI.isIndirect(); |
| 4105 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4106 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4107 | if (IsIndirect) |
| 4108 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4109 | else if (AI.getCoerceToType()) |
| 4110 | BaseTy = AI.getCoerceToType(); |
| 4111 | |
| 4112 | unsigned NumRegs = 1; |
| 4113 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4114 | BaseTy = ArrTy->getElementType(); |
| 4115 | NumRegs = ArrTy->getNumElements(); |
| 4116 | } |
| 4117 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4118 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4119 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4120 | // Standard, section B.4: |
| 4121 | // |
| 4122 | // struct { |
| 4123 | // void *__stack; |
| 4124 | // void *__gr_top; |
| 4125 | // void *__vr_top; |
| 4126 | // int __gr_offs; |
| 4127 | // int __vr_offs; |
| 4128 | // }; |
| 4129 | |
| 4130 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4131 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4132 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4133 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4134 | auto &Ctx = CGF.getContext(); |
| 4135 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4136 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4137 | int reg_top_index; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4138 | int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8; |
| 4139 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4140 | // 3 is the field number of __gr_offs |
| 4141 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 4142 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4143 | reg_top_index = 1; // field number for __gr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4144 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4145 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4146 | // 4 is the field number of __vr_offs. |
| 4147 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 4148 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4149 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4150 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
| 4153 | //======================================= |
| 4154 | // Find out where argument was passed |
| 4155 | //======================================= |
| 4156 | |
| 4157 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4158 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4159 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4160 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4161 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4162 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4163 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4164 | |
| 4165 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4166 | |
| 4167 | // Otherwise, at least some kind of argument could go in these registers, the |
Bob Wilson | 3abf169 | 2014-04-21 01:23:36 +0000 | [diff] [blame] | 4168 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4169 | CGF.EmitBlock(MaybeRegBlock); |
| 4170 | |
| 4171 | // Integer arguments may need to correct register alignment (for example a |
| 4172 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4173 | // align __gr_offs to calculate the potential address. |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4174 | if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4175 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4176 | |
| 4177 | reg_offs = CGF.Builder.CreateAdd( |
| 4178 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4179 | "align_regoffs"); |
| 4180 | reg_offs = CGF.Builder.CreateAnd( |
| 4181 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4182 | "aligned_regoffs"); |
| 4183 | } |
| 4184 | |
| 4185 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4186 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4187 | NewOffset = CGF.Builder.CreateAdd( |
| 4188 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4189 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4190 | |
| 4191 | // Now we're in a position to decide whether this argument really was in |
| 4192 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4193 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4194 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4195 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4196 | |
| 4197 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4198 | |
| 4199 | //======================================= |
| 4200 | // Argument was in registers |
| 4201 | //======================================= |
| 4202 | |
| 4203 | // Now we emit the code for if the argument was originally passed in |
| 4204 | // registers. First start the appropriate block: |
| 4205 | CGF.EmitBlock(InRegBlock); |
| 4206 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4207 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4208 | reg_top_p = |
| 4209 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 4210 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4211 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4212 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4213 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4214 | |
| 4215 | if (IsIndirect) { |
| 4216 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4217 | // stored registers or on the stack will actually be a struct **. |
| 4218 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4219 | } |
| 4220 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4221 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4222 | uint64_t NumMembers = 0; |
| 4223 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4224 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4225 | // Homogeneous aggregates passed in registers will have their elements split |
| 4226 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4227 | // qN+1, ...). We reload and store into a temporary local variable |
| 4228 | // contiguously. |
| 4229 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 4230 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4231 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 4232 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 4233 | int Offset = 0; |
| 4234 | |
| 4235 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 4236 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 4237 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4238 | llvm::Value *BaseOffset = |
| 4239 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 4240 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4241 | LoadAddr = CGF.Builder.CreateBitCast( |
| 4242 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
| 4243 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 4244 | |
| 4245 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4246 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4247 | } |
| 4248 | |
| 4249 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4250 | } else { |
| 4251 | // Otherwise the object is contiguous in memory |
| 4252 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4253 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4254 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4255 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 4256 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 4257 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 4258 | |
| 4259 | BaseAddr = CGF.Builder.CreateAdd( |
| 4260 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4261 | |
| 4262 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 4263 | } |
| 4264 | |
| 4265 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4266 | } |
| 4267 | |
| 4268 | CGF.EmitBranch(ContBlock); |
| 4269 | |
| 4270 | //======================================= |
| 4271 | // Argument was on the stack |
| 4272 | //======================================= |
| 4273 | CGF.EmitBlock(OnStackBlock); |
| 4274 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4275 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4276 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 4277 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4278 | |
| 4279 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4280 | // floating-point ones might be affected. |
| 4281 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 4282 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4283 | |
| 4284 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4285 | |
| 4286 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4287 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4288 | "align_stack"); |
| 4289 | OnStackAddr = CGF.Builder.CreateAnd( |
| 4290 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4291 | "align_stack"); |
| 4292 | |
| 4293 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4294 | } |
| 4295 | |
| 4296 | uint64_t StackSize; |
| 4297 | if (IsIndirect) |
| 4298 | StackSize = 8; |
| 4299 | else |
| 4300 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 4301 | |
| 4302 | // All stack slots are 8 bytes |
| 4303 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4304 | |
| 4305 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4306 | llvm::Value *NewStack = |
| 4307 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 4308 | |
| 4309 | // Write the new value of __stack for the next call to va_arg |
| 4310 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4311 | |
| 4312 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4313 | Ctx.getTypeSize(Ty) < 64) { |
| 4314 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 4315 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4316 | |
| 4317 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4318 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4319 | |
| 4320 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4321 | } |
| 4322 | |
| 4323 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4324 | |
| 4325 | CGF.EmitBranch(ContBlock); |
| 4326 | |
| 4327 | //======================================= |
| 4328 | // Tidy up |
| 4329 | //======================================= |
| 4330 | CGF.EmitBlock(ContBlock); |
| 4331 | |
| 4332 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4333 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4334 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4335 | |
| 4336 | if (IsIndirect) |
| 4337 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4338 | |
| 4339 | return ResAddr; |
| 4340 | } |
| 4341 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4342 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4343 | CodeGenFunction &CGF) const { |
| 4344 | // We do not support va_arg for aggregates or illegal vector types. |
| 4345 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 4346 | // other cases. |
| 4347 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4348 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4349 | |
| 4350 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 4351 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 4352 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4353 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4354 | uint64_t Members = 0; |
| 4355 | bool isHA = isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4356 | |
| 4357 | bool isIndirect = false; |
| 4358 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 4359 | // be passed indirectly. |
| 4360 | if (Size > 16 && !isHA) { |
| 4361 | isIndirect = true; |
| 4362 | Size = 8; |
| 4363 | Align = 8; |
| 4364 | } |
| 4365 | |
| 4366 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 4367 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 4368 | |
| 4369 | CGBuilderTy &Builder = CGF.Builder; |
| 4370 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4371 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4372 | |
| 4373 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4374 | // These are ignored for parameter passing purposes. |
| 4375 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4376 | return Builder.CreateBitCast(Addr, PTy); |
| 4377 | } |
| 4378 | |
| 4379 | const uint64_t MinABIAlign = 8; |
| 4380 | if (Align > MinABIAlign) { |
| 4381 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 4382 | Addr = Builder.CreateGEP(Addr, Offset); |
| 4383 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 4384 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 4385 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 4386 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 4387 | } |
| 4388 | |
| 4389 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 4390 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 4391 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 4392 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4393 | |
| 4394 | if (isIndirect) |
| 4395 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 4396 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4397 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4398 | |
| 4399 | return AddrTyped; |
| 4400 | } |
| 4401 | |
| 4402 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4403 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4404 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4405 | |
| 4406 | namespace { |
| 4407 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4408 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4409 | public: |
| 4410 | enum ABIKind { |
| 4411 | APCS = 0, |
| 4412 | AAPCS = 1, |
| 4413 | AAPCS_VFP |
| 4414 | }; |
| 4415 | |
| 4416 | private: |
| 4417 | ABIKind Kind; |
| 4418 | |
| 4419 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4420 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4421 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4422 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4423 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4424 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4425 | switch (getTarget().getTriple().getEnvironment()) { |
| 4426 | case llvm::Triple::Android: |
| 4427 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4428 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4429 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4430 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4431 | return true; |
| 4432 | default: |
| 4433 | return false; |
| 4434 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4437 | bool isEABIHF() const { |
| 4438 | switch (getTarget().getTriple().getEnvironment()) { |
| 4439 | case llvm::Triple::EABIHF: |
| 4440 | case llvm::Triple::GNUEABIHF: |
| 4441 | return true; |
| 4442 | default: |
| 4443 | return false; |
| 4444 | } |
| 4445 | } |
| 4446 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4447 | ABIKind getABIKind() const { return Kind; } |
| 4448 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4449 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4450 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4451 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4452 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4453 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4454 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4455 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4456 | uint64_t Members) const override; |
| 4457 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4458 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4459 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4460 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4461 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4462 | |
| 4463 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4464 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4465 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4466 | }; |
| 4467 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4468 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4469 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4470 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4471 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4472 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4473 | const ARMABIInfo &getABIInfo() const { |
| 4474 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4475 | } |
| 4476 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4477 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4478 | return 13; |
| 4479 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4480 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4481 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4482 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4483 | } |
| 4484 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4485 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4486 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4487 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4488 | |
| 4489 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4490 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4491 | return false; |
| 4492 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4493 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4494 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4495 | if (getABIInfo().isEABI()) return 88; |
| 4496 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4497 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4498 | |
| 4499 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4500 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4501 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4502 | if (!FD) |
| 4503 | return; |
| 4504 | |
| 4505 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4506 | if (!Attr) |
| 4507 | return; |
| 4508 | |
| 4509 | const char *Kind; |
| 4510 | switch (Attr->getInterrupt()) { |
| 4511 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4512 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4513 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4514 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4515 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4516 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4517 | } |
| 4518 | |
| 4519 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4520 | |
| 4521 | Fn->addFnAttr("interrupt", Kind); |
| 4522 | |
| 4523 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4524 | return; |
| 4525 | |
| 4526 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4527 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4528 | // the backend to perform a realignment as part of the function prologue. |
| 4529 | llvm::AttrBuilder B; |
| 4530 | B.addStackAlignmentAttr(8); |
| 4531 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4532 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4533 | llvm::AttributeSet::FunctionIndex, |
| 4534 | B)); |
| 4535 | } |
Joerg Sonnenberger | 096feeb | 2015-02-23 20:23:47 +0000 | [diff] [blame] | 4536 | |
| 4537 | bool hasSjLjLowering(CodeGen::CodeGenFunction &CGF) const override { |
| 4538 | return false; |
| 4539 | // FIXME: backend implementation too restricted, even on Darwin. |
| 4540 | // return CGF.getTarget().getTriple().isOSDarwin(); |
| 4541 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4542 | }; |
| 4543 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4544 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4545 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4546 | CodeGen::CodeGenModule &CGM) const; |
| 4547 | |
| 4548 | public: |
| 4549 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4550 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4551 | |
| 4552 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4553 | CodeGen::CodeGenModule &CGM) const override; |
| 4554 | }; |
| 4555 | |
| 4556 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4557 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4558 | if (!isa<FunctionDecl>(D)) |
| 4559 | return; |
| 4560 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4561 | return; |
| 4562 | |
| 4563 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4564 | F->addFnAttr("stack-probe-size", |
| 4565 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4566 | } |
| 4567 | |
| 4568 | void WindowsARMTargetCodeGenInfo::SetTargetAttributes( |
| 4569 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4570 | ARMTargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 4571 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4572 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4575 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4576 | if (!getCXXABI().classifyReturnType(FI)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4577 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4578 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4579 | for (auto &I : FI.arguments()) |
| 4580 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4581 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4582 | // Always honor user-specified calling convention. |
| 4583 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4584 | return; |
| 4585 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4586 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4587 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4588 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4589 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4590 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4591 | /// Return the default calling convention that LLVM will use. |
| 4592 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4593 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4594 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4595 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4596 | else if (isEABI()) |
| 4597 | return llvm::CallingConv::ARM_AAPCS; |
| 4598 | else |
| 4599 | return llvm::CallingConv::ARM_APCS; |
| 4600 | } |
| 4601 | |
| 4602 | /// Return the calling convention that our ABI would like us to use |
| 4603 | /// as the C calling convention. |
| 4604 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4605 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4606 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4607 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4608 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4609 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4610 | llvm_unreachable("bad ABI kind"); |
| 4611 | } |
| 4612 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4613 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4614 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4615 | |
| 4616 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4617 | // they'd just match what LLVM will infer from the triple. |
| 4618 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4619 | if (abiCC != getLLVMDefaultCC()) |
| 4620 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4621 | |
| 4622 | BuiltinCC = (getABIKind() == APCS ? |
| 4623 | llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4626 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4627 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4628 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4629 | // A single-precision floating-point type (including promoted |
| 4630 | // half-precision types); A double-precision floating-point type; |
| 4631 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4632 | // with a Base Type of a single- or double-precision floating-point type, |
| 4633 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4634 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4635 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4636 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4637 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4638 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4639 | // Handle illegal vector types here. |
| 4640 | if (isIllegalVectorType(Ty)) { |
| 4641 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4642 | if (Size <= 32) { |
| 4643 | llvm::Type *ResType = |
| 4644 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4645 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4646 | } |
| 4647 | if (Size == 64) { |
| 4648 | llvm::Type *ResType = llvm::VectorType::get( |
| 4649 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4650 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4651 | } |
| 4652 | if (Size == 128) { |
| 4653 | llvm::Type *ResType = llvm::VectorType::get( |
| 4654 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4655 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4656 | } |
| 4657 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4658 | } |
| 4659 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4660 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4661 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4662 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4663 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4664 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4665 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4666 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4667 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4668 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4669 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4670 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4671 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4672 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4673 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4674 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4675 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4676 | return ABIArgInfo::getIgnore(); |
| 4677 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4678 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4679 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4680 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4681 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4682 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4683 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4684 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4685 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4686 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4687 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4690 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4691 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4692 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4693 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4694 | uint64_t ABIAlign = 4; |
| 4695 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4696 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4697 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4698 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4699 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4700 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4701 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4702 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4705 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4706 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4707 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4708 | // FIXME: Try to match the types of the arguments more accurately where |
| 4709 | // we can. |
| 4710 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4711 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4712 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4713 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4714 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4715 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4716 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4717 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4718 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4721 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4722 | llvm::LLVMContext &VMContext) { |
| 4723 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4724 | // is called integer-like if its size is less than or equal to one word, and |
| 4725 | // the offset of each of its addressable sub-fields is zero. |
| 4726 | |
| 4727 | uint64_t Size = Context.getTypeSize(Ty); |
| 4728 | |
| 4729 | // Check that the type fits in a word. |
| 4730 | if (Size > 32) |
| 4731 | return false; |
| 4732 | |
| 4733 | // FIXME: Handle vector types! |
| 4734 | if (Ty->isVectorType()) |
| 4735 | return false; |
| 4736 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4737 | // Float types are never treated as "integer like". |
| 4738 | if (Ty->isRealFloatingType()) |
| 4739 | return false; |
| 4740 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4741 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4742 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4743 | return true; |
| 4744 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4745 | // Small complex integer types are "integer like". |
| 4746 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4747 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4748 | |
| 4749 | // Single element and zero sized arrays should be allowed, by the definition |
| 4750 | // above, but they are not. |
| 4751 | |
| 4752 | // Otherwise, it must be a record type. |
| 4753 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4754 | if (!RT) return false; |
| 4755 | |
| 4756 | // Ignore records with flexible arrays. |
| 4757 | const RecordDecl *RD = RT->getDecl(); |
| 4758 | if (RD->hasFlexibleArrayMember()) |
| 4759 | return false; |
| 4760 | |
| 4761 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4762 | // like". |
| 4763 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4764 | |
| 4765 | bool HadField = false; |
| 4766 | unsigned idx = 0; |
| 4767 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4768 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4769 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4770 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4771 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4772 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4773 | // struct { int : 0; int x } |
| 4774 | // is non-integer like according to gcc. |
| 4775 | if (FD->isBitField()) { |
| 4776 | if (!RD->isUnion()) |
| 4777 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4778 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4779 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4780 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4781 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4782 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4785 | // Check if this field is at offset 0. |
| 4786 | if (Layout.getFieldOffset(idx) != 0) |
| 4787 | return false; |
| 4788 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4789 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4790 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4791 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4792 | // Only allow at most one field in a structure. This doesn't match the |
| 4793 | // wording above, but follows gcc in situations with a field following an |
| 4794 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4795 | if (!RD->isUnion()) { |
| 4796 | if (HadField) |
| 4797 | return false; |
| 4798 | |
| 4799 | HadField = true; |
| 4800 | } |
| 4801 | } |
| 4802 | |
| 4803 | return true; |
| 4804 | } |
| 4805 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4806 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4807 | bool isVariadic) const { |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4808 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4809 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4810 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4811 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4812 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4813 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4814 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4815 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4816 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4817 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4818 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4819 | // Treat an enum type as its underlying type. |
| 4820 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4821 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4822 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4823 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4824 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4825 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4826 | |
| 4827 | // Are we following APCS? |
| 4828 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4829 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4830 | return ABIArgInfo::getIgnore(); |
| 4831 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4832 | // Complex types are all returned as packed integers. |
| 4833 | // |
| 4834 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4835 | // correctly. |
| 4836 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4837 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 4838 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4839 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4840 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4841 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4842 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4843 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4844 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4845 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4846 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4847 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4848 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | // Otherwise return in memory. |
| 4852 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4853 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4854 | |
| 4855 | // Otherwise this is an AAPCS variant. |
| 4856 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4857 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4858 | return ABIArgInfo::getIgnore(); |
| 4859 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4860 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4861 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4862 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4863 | uint64_t Members; |
| 4864 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4865 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4866 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4867 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4868 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4871 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4872 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4873 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4874 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4875 | if (getDataLayout().isBigEndian()) |
| 4876 | // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4877 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4878 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4879 | // Return in the smallest viable integer type. |
| 4880 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4881 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4882 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4883 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4884 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4885 | } |
| 4886 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4887 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4890 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4891 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4892 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4893 | // Check whether VT is legal. |
| 4894 | unsigned NumElements = VT->getNumElements(); |
| 4895 | uint64_t Size = getContext().getTypeSize(VT); |
| 4896 | // NumElements should be power of 2. |
| 4897 | if ((NumElements & (NumElements - 1)) != 0) |
| 4898 | return true; |
| 4899 | // Size should be greater than 32 bits. |
| 4900 | return Size <= 32; |
| 4901 | } |
| 4902 | return false; |
| 4903 | } |
| 4904 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4905 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4906 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4907 | // double, or 64-bit or 128-bit vectors. |
| 4908 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4909 | if (BT->getKind() == BuiltinType::Float || |
| 4910 | BT->getKind() == BuiltinType::Double || |
| 4911 | BT->getKind() == BuiltinType::LongDouble) |
| 4912 | return true; |
| 4913 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4914 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4915 | if (VecSize == 64 || VecSize == 128) |
| 4916 | return true; |
| 4917 | } |
| 4918 | return false; |
| 4919 | } |
| 4920 | |
| 4921 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4922 | uint64_t Members) const { |
| 4923 | return Members <= 4; |
| 4924 | } |
| 4925 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4926 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4927 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4928 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4929 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4930 | |
| 4931 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4932 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4933 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4934 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4935 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4936 | // These are ignored for parameter passing purposes. |
| 4937 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4938 | return Builder.CreateBitCast(Addr, PTy); |
| 4939 | } |
| 4940 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4941 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4942 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4943 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4944 | |
| 4945 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4946 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4947 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4948 | getABIKind() == ARMABIInfo::AAPCS) |
| 4949 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4950 | else |
| 4951 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4952 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4953 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4954 | IsIndirect = true; |
| 4955 | Size = 4; |
| 4956 | TyAlign = 4; |
| 4957 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4958 | |
| 4959 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4960 | if (TyAlign > 4) { |
| 4961 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 4962 | "Alignment is not power of 2!"); |
| 4963 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 4964 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 4965 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4966 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4967 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4968 | |
| 4969 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4970 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4971 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4972 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4973 | "ap.next"); |
| 4974 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4975 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4976 | if (IsIndirect) |
| 4977 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4978 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4979 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 4980 | // may not be correctly aligned for the vector type. We create an aligned |
| 4981 | // temporary space and copy the content over from ap.cur to the temporary |
| 4982 | // space. This is necessary if the natural alignment of the type is greater |
| 4983 | // than the ABI alignment. |
| 4984 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 4985 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 4986 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 4987 | "var.align"); |
| 4988 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 4989 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 4990 | Builder.CreateMemCpy(Dst, Src, |
| 4991 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 4992 | TyAlign, false); |
| 4993 | Addr = AlignedTemp; //The content is in aligned location. |
| 4994 | } |
| 4995 | llvm::Type *PTy = |
| 4996 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4997 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4998 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4999 | return AddrTyped; |
| 5000 | } |
| 5001 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5002 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5003 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5004 | //===----------------------------------------------------------------------===// |
| 5005 | |
| 5006 | namespace { |
| 5007 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5008 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5009 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5010 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5011 | |
| 5012 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5013 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5014 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5015 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5016 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5017 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5018 | }; |
| 5019 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5020 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5021 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5022 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5023 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5024 | |
| 5025 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5026 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5027 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5028 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5029 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5030 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5031 | }; |
| 5032 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5033 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5034 | if (RetTy->isVoidType()) |
| 5035 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5036 | |
| 5037 | // note: this is different from default ABI |
| 5038 | if (!RetTy->isScalarType()) |
| 5039 | return ABIArgInfo::getDirect(); |
| 5040 | |
| 5041 | // Treat an enum type as its underlying type. |
| 5042 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5043 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5044 | |
| 5045 | return (RetTy->isPromotableIntegerType() ? |
| 5046 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5049 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5050 | // Treat an enum type as its underlying type. |
| 5051 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5052 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5053 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5054 | // Return aggregates type as indirect by value |
| 5055 | if (isAggregateTypeForABI(Ty)) |
| 5056 | return ABIArgInfo::getIndirect(0, /* byval */ true); |
| 5057 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5058 | return (Ty->isPromotableIntegerType() ? |
| 5059 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5062 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5063 | if (!getCXXABI().classifyReturnType(FI)) |
| 5064 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5065 | for (auto &I : FI.arguments()) |
| 5066 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5067 | |
| 5068 | // Always honor user-specified calling convention. |
| 5069 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5070 | return; |
| 5071 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5072 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5073 | } |
| 5074 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5075 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5076 | CodeGenFunction &CFG) const { |
| 5077 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5080 | void NVPTXTargetCodeGenInfo:: |
| 5081 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5082 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5083 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5084 | if (!FD) return; |
| 5085 | |
| 5086 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5087 | |
| 5088 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5089 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5090 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5091 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5092 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5093 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5094 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5095 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5096 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5097 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5098 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5099 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5100 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5101 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5102 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5103 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5104 | // __global__ functions cannot be called from the device, we do not |
| 5105 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5106 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5107 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5108 | addNVVMMetadata(F, "kernel", 1); |
| 5109 | } |
| 5110 | if (FD->hasAttr<CUDALaunchBoundsAttr>()) { |
| 5111 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
| 5112 | addNVVMMetadata(F, "maxntidx", |
| 5113 | FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads()); |
| 5114 | // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a |
| 5115 | // zero value from getMinBlocks either means it was not specified in |
| 5116 | // __launch_bounds__ or the user specified a 0 value. In both cases, we |
| 5117 | // don't have to add a PTX directive. |
| 5118 | int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks(); |
| 5119 | if (MinCTASM > 0) { |
| 5120 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5121 | addNVVMMetadata(F, "minctasm", MinCTASM); |
| 5122 | } |
| 5123 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5124 | } |
| 5125 | } |
| 5126 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5127 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5128 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5129 | llvm::Module *M = F->getParent(); |
| 5130 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5131 | |
| 5132 | // Get "nvvm.annotations" metadata node |
| 5133 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5134 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5135 | llvm::Metadata *MDVals[] = { |
| 5136 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5137 | llvm::ConstantAsMetadata::get( |
| 5138 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5139 | // Append metadata to nvvm.annotations |
| 5140 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5141 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5145 | // SystemZ ABI Implementation |
| 5146 | //===----------------------------------------------------------------------===// |
| 5147 | |
| 5148 | namespace { |
| 5149 | |
| 5150 | class SystemZABIInfo : public ABIInfo { |
| 5151 | public: |
| 5152 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5153 | |
| 5154 | bool isPromotableIntegerType(QualType Ty) const; |
| 5155 | bool isCompoundType(QualType Ty) const; |
| 5156 | bool isFPArgumentType(QualType Ty) const; |
| 5157 | |
| 5158 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5159 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5160 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5161 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5162 | if (!getCXXABI().classifyReturnType(FI)) |
| 5163 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5164 | for (auto &I : FI.arguments()) |
| 5165 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5168 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5169 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5170 | }; |
| 5171 | |
| 5172 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5173 | public: |
| 5174 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5175 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 5176 | }; |
| 5177 | |
| 5178 | } |
| 5179 | |
| 5180 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5181 | // Treat an enum type as its underlying type. |
| 5182 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5183 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5184 | |
| 5185 | // Promotable integer types are required to be promoted by the ABI. |
| 5186 | if (Ty->isPromotableIntegerType()) |
| 5187 | return true; |
| 5188 | |
| 5189 | // 32-bit values must also be promoted. |
| 5190 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5191 | switch (BT->getKind()) { |
| 5192 | case BuiltinType::Int: |
| 5193 | case BuiltinType::UInt: |
| 5194 | return true; |
| 5195 | default: |
| 5196 | return false; |
| 5197 | } |
| 5198 | return false; |
| 5199 | } |
| 5200 | |
| 5201 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 5202 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 5203 | } |
| 5204 | |
| 5205 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5206 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5207 | switch (BT->getKind()) { |
| 5208 | case BuiltinType::Float: |
| 5209 | case BuiltinType::Double: |
| 5210 | return true; |
| 5211 | default: |
| 5212 | return false; |
| 5213 | } |
| 5214 | |
| 5215 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5216 | const RecordDecl *RD = RT->getDecl(); |
| 5217 | bool Found = false; |
| 5218 | |
| 5219 | // If this is a C++ record, check the bases first. |
| 5220 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5221 | for (const auto &I : CXXRD->bases()) { |
| 5222 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5223 | |
| 5224 | // Empty bases don't affect things either way. |
| 5225 | if (isEmptyRecord(getContext(), Base, true)) |
| 5226 | continue; |
| 5227 | |
| 5228 | if (Found) |
| 5229 | return false; |
| 5230 | Found = isFPArgumentType(Base); |
| 5231 | if (!Found) |
| 5232 | return false; |
| 5233 | } |
| 5234 | |
| 5235 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5236 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5237 | // Empty bitfields don't affect things either way. |
| 5238 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5239 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 5240 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5241 | return true; |
| 5242 | |
| 5243 | // Unlike isSingleElementStruct(), arrays do not count. |
| 5244 | // Nested isFPArgumentType structures still do though. |
| 5245 | if (Found) |
| 5246 | return false; |
| 5247 | Found = isFPArgumentType(FD->getType()); |
| 5248 | if (!Found) |
| 5249 | return false; |
| 5250 | } |
| 5251 | |
| 5252 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5253 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 5254 | return Found; |
| 5255 | } |
| 5256 | |
| 5257 | return false; |
| 5258 | } |
| 5259 | |
| 5260 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5261 | CodeGenFunction &CGF) const { |
| 5262 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5263 | // struct { |
| 5264 | // i64 __gpr; |
| 5265 | // i64 __fpr; |
| 5266 | // i8 *__overflow_arg_area; |
| 5267 | // i8 *__reg_save_area; |
| 5268 | // }; |
| 5269 | |
| 5270 | // Every argument occupies 8 bytes and is passed by preference in either |
| 5271 | // GPRs or FPRs. |
| 5272 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 5273 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 5274 | bool InFPRs = isFPArgumentType(Ty); |
| 5275 | |
| 5276 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 5277 | bool IsIndirect = AI.isIndirect(); |
| 5278 | unsigned UnpaddedBitSize; |
| 5279 | if (IsIndirect) { |
| 5280 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5281 | UnpaddedBitSize = 64; |
| 5282 | } else |
| 5283 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 5284 | unsigned PaddedBitSize = 64; |
| 5285 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5286 | |
| 5287 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5288 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5289 | |
| 5290 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5291 | if (InFPRs) { |
| 5292 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5293 | RegCountField = 1; // __fpr |
| 5294 | RegSaveIndex = 16; // save offset for f0 |
| 5295 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5296 | } else { |
| 5297 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5298 | RegCountField = 0; // __gpr |
| 5299 | RegSaveIndex = 2; // save offset for r2 |
| 5300 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5301 | } |
| 5302 | |
| 5303 | llvm::Value *RegCountPtr = |
| 5304 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 5305 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 5306 | llvm::Type *IndexTy = RegCount->getType(); |
| 5307 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5308 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5309 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5310 | |
| 5311 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5312 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5313 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5314 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5315 | |
| 5316 | // Emit code to load the value if it was passed in registers. |
| 5317 | CGF.EmitBlock(InRegBlock); |
| 5318 | |
| 5319 | // Work out the address of an argument register. |
| 5320 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5321 | llvm::Value *ScaledRegCount = |
| 5322 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5323 | llvm::Value *RegBase = |
| 5324 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5325 | llvm::Value *RegOffset = |
| 5326 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5327 | llvm::Value *RegSaveAreaPtr = |
| 5328 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 5329 | llvm::Value *RegSaveArea = |
| 5330 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5331 | llvm::Value *RawRegAddr = |
| 5332 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5333 | llvm::Value *RegAddr = |
| 5334 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5335 | |
| 5336 | // Update the register count |
| 5337 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5338 | llvm::Value *NewRegCount = |
| 5339 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5340 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5341 | CGF.EmitBranch(ContBlock); |
| 5342 | |
| 5343 | // Emit code to load the value if it was passed in memory. |
| 5344 | CGF.EmitBlock(InMemBlock); |
| 5345 | |
| 5346 | // Work out the address of a stack argument. |
| 5347 | llvm::Value *OverflowArgAreaPtr = |
| 5348 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 5349 | llvm::Value *OverflowArgArea = |
| 5350 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5351 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5352 | llvm::Value *RawMemAddr = |
| 5353 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5354 | llvm::Value *MemAddr = |
| 5355 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5356 | |
| 5357 | // Update overflow_arg_area_ptr pointer |
| 5358 | llvm::Value *NewOverflowArgArea = |
| 5359 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5360 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5361 | CGF.EmitBranch(ContBlock); |
| 5362 | |
| 5363 | // Return the appropriate result. |
| 5364 | CGF.EmitBlock(ContBlock); |
| 5365 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5366 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5367 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5368 | |
| 5369 | if (IsIndirect) |
| 5370 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5371 | |
| 5372 | return ResAddr; |
| 5373 | } |
| 5374 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5375 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5376 | if (RetTy->isVoidType()) |
| 5377 | return ABIArgInfo::getIgnore(); |
| 5378 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5379 | return ABIArgInfo::getIndirect(0); |
| 5380 | return (isPromotableIntegerType(RetTy) ? |
| 5381 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5382 | } |
| 5383 | |
| 5384 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5385 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5386 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5387 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5388 | |
| 5389 | // Integers and enums are extended to full register width. |
| 5390 | if (isPromotableIntegerType(Ty)) |
| 5391 | return ABIArgInfo::getExtend(); |
| 5392 | |
| 5393 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 5394 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5395 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5396 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5397 | |
| 5398 | // Handle small structures. |
| 5399 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5400 | // Structures with flexible arrays have variable length, so really |
| 5401 | // fail the size test above. |
| 5402 | const RecordDecl *RD = RT->getDecl(); |
| 5403 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5404 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5405 | |
| 5406 | // The structure is passed as an unextended integer, a float, or a double. |
| 5407 | llvm::Type *PassTy; |
| 5408 | if (isFPArgumentType(Ty)) { |
| 5409 | assert(Size == 32 || Size == 64); |
| 5410 | if (Size == 32) |
| 5411 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5412 | else |
| 5413 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5414 | } else |
| 5415 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5416 | return ABIArgInfo::getDirect(PassTy); |
| 5417 | } |
| 5418 | |
| 5419 | // Non-structure compounds are passed indirectly. |
| 5420 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5421 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5422 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5423 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5424 | } |
| 5425 | |
| 5426 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5427 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5428 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5429 | |
| 5430 | namespace { |
| 5431 | |
| 5432 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5433 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5434 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5435 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5436 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5437 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5438 | }; |
| 5439 | |
| 5440 | } |
| 5441 | |
| 5442 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5443 | llvm::GlobalValue *GV, |
| 5444 | CodeGen::CodeGenModule &M) const { |
| 5445 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5446 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5447 | // Handle 'interrupt' attribute: |
| 5448 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5449 | |
| 5450 | // Step 1: Set ISR calling convention. |
| 5451 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5452 | |
| 5453 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5454 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5455 | |
| 5456 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5457 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5458 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5459 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5460 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5461 | } |
| 5462 | } |
| 5463 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5464 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5465 | // MIPS ABI Implementation. This works for both little-endian and |
| 5466 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5467 | //===----------------------------------------------------------------------===// |
| 5468 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5469 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5470 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5471 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5472 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5473 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5474 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5475 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5476 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5477 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5478 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5479 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5480 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5481 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5482 | |
| 5483 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5484 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5485 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5486 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5487 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5488 | }; |
| 5489 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5490 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5491 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5492 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5493 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5494 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5495 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5496 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5497 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5498 | return 29; |
| 5499 | } |
| 5500 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5501 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5502 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5503 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5504 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5505 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5506 | if (FD->hasAttr<Mips16Attr>()) { |
| 5507 | Fn->addFnAttr("mips16"); |
| 5508 | } |
| 5509 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5510 | Fn->addFnAttr("nomips16"); |
| 5511 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5512 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5513 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5514 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5515 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5516 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5517 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5518 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5519 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5520 | }; |
| 5521 | } |
| 5522 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5523 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5524 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5525 | llvm::IntegerType *IntTy = |
| 5526 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5527 | |
| 5528 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5529 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5530 | ArgList.push_back(IntTy); |
| 5531 | |
| 5532 | // If necessary, add one more integer type to ArgList. |
| 5533 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5534 | |
| 5535 | if (R) |
| 5536 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5537 | } |
| 5538 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5539 | // In N32/64, an aligned double precision floating point field is passed in |
| 5540 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5541 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5542 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5543 | |
| 5544 | if (IsO32) { |
| 5545 | CoerceToIntArgs(TySize, ArgList); |
| 5546 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5547 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5548 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5549 | if (Ty->isComplexType()) |
| 5550 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5551 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5552 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5553 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5554 | // Unions/vectors are passed in integer registers. |
| 5555 | if (!RT || !RT->isStructureOrClassType()) { |
| 5556 | CoerceToIntArgs(TySize, ArgList); |
| 5557 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5558 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5559 | |
| 5560 | const RecordDecl *RD = RT->getDecl(); |
| 5561 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5562 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5563 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5564 | uint64_t LastOffset = 0; |
| 5565 | unsigned idx = 0; |
| 5566 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5567 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5568 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5569 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5570 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5571 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5572 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5573 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5574 | |
| 5575 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5576 | continue; |
| 5577 | |
| 5578 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5579 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5580 | continue; |
| 5581 | |
| 5582 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5583 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5584 | ArgList.push_back(I64); |
| 5585 | |
| 5586 | // Add double type. |
| 5587 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5588 | LastOffset = Offset + 64; |
| 5589 | } |
| 5590 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5591 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5592 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5593 | |
| 5594 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5595 | } |
| 5596 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5597 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5598 | uint64_t Offset) const { |
| 5599 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5600 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5601 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5602 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5603 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5604 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5605 | ABIArgInfo |
| 5606 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5607 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5608 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5609 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5610 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5611 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5612 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5613 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5614 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5615 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5616 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5617 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5618 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5619 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5620 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5621 | return ABIArgInfo::getIgnore(); |
| 5622 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5623 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5624 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5625 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5626 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5627 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5628 | // If we have reached here, aggregates are passed directly by coercing to |
| 5629 | // another structure type. Padding is inserted if the offset of the |
| 5630 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 5631 | ABIArgInfo ArgInfo = |
| 5632 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 5633 | getPaddingType(OrigOffset, CurrOffset)); |
| 5634 | ArgInfo.setInReg(true); |
| 5635 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
| 5638 | // Treat an enum type as its underlying type. |
| 5639 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5640 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5641 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 5642 | // All integral types are promoted to the GPR width. |
| 5643 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5644 | return ABIArgInfo::getExtend(); |
| 5645 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5646 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5647 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5648 | } |
| 5649 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5650 | llvm::Type* |
| 5651 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5652 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5653 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5654 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5655 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5656 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5657 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5658 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5659 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5660 | // N32/64 returns struct/classes in floating point registers if the |
| 5661 | // following conditions are met: |
| 5662 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5663 | // 2. The struct/class has one or two fields all of which are floating |
| 5664 | // point types. |
| 5665 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5666 | // |
| 5667 | // Any other composite results are returned in integer registers. |
| 5668 | // |
| 5669 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5670 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5671 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5672 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5673 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5674 | if (!BT || !BT->isFloatingPoint()) |
| 5675 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5676 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5677 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5678 | } |
| 5679 | |
| 5680 | if (b == e) |
| 5681 | return llvm::StructType::get(getVMContext(), RTList, |
| 5682 | RD->hasAttr<PackedAttr>()); |
| 5683 | |
| 5684 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5685 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5688 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5689 | return llvm::StructType::get(getVMContext(), RTList); |
| 5690 | } |
| 5691 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5692 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5693 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5694 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5695 | if (RetTy->isVoidType()) |
| 5696 | return ABIArgInfo::getIgnore(); |
| 5697 | |
| 5698 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5699 | // However, N32/N64 ignores zero sized return values. |
| 5700 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5701 | return ABIArgInfo::getIgnore(); |
| 5702 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5703 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5704 | if (Size <= 128) { |
| 5705 | if (RetTy->isAnyComplexType()) |
| 5706 | return ABIArgInfo::getDirect(); |
| 5707 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5708 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 5709 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5710 | if (!IsO32 || |
| 5711 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 5712 | ABIArgInfo ArgInfo = |
| 5713 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5714 | ArgInfo.setInReg(true); |
| 5715 | return ArgInfo; |
| 5716 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5717 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5718 | |
| 5719 | return ABIArgInfo::getIndirect(0); |
| 5720 | } |
| 5721 | |
| 5722 | // Treat an enum type as its underlying type. |
| 5723 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5724 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5725 | |
| 5726 | return (RetTy->isPromotableIntegerType() ? |
| 5727 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5728 | } |
| 5729 | |
| 5730 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5731 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5732 | if (!getCXXABI().classifyReturnType(FI)) |
| 5733 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5734 | |
| 5735 | // Check if a pointer to an aggregate is passed as a hidden argument. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5736 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5737 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5738 | for (auto &I : FI.arguments()) |
| 5739 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5743 | CodeGenFunction &CGF) const { |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5744 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5745 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5746 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5747 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 5748 | // Pointers are also promoted in the same way but this only matters for N32. |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5749 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5750 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
| 5751 | if ((Ty->isIntegerType() && |
| 5752 | CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) || |
| 5753 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5754 | Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 5755 | Ty->isSignedIntegerType()); |
| 5756 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5757 | |
| 5758 | CGBuilderTy &Builder = CGF.Builder; |
| 5759 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5760 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Daniel Sanders | 8d36a61 | 2014-09-22 13:27:06 +0000 | [diff] [blame] | 5761 | int64_t TypeAlign = |
| 5762 | std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5763 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5764 | llvm::Value *AddrTyped; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5765 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
| 5766 | |
| 5767 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 5768 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5769 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5770 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5771 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
| 5772 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5773 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5774 | } |
| 5775 | else |
| 5776 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5777 | |
| 5778 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
| 5779 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5780 | unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty); |
| 5781 | uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5782 | llvm::Value *NextAddr = |
| 5783 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
| 5784 | "ap.next"); |
| 5785 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5786 | |
| 5787 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5790 | bool |
| 5791 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5792 | llvm::Value *Address) const { |
| 5793 | // This information comes from gcc's implementation, which seems to |
| 5794 | // as canonical as it gets. |
| 5795 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5796 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5797 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5798 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5799 | |
| 5800 | // 0-31 are the general purpose registers, $0 - $31. |
| 5801 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5802 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5803 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5804 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5805 | |
| 5806 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5807 | // They are one bit wide and ignored here. |
| 5808 | |
| 5809 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5810 | // (coprocessor 1 is the FP unit) |
| 5811 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5812 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5813 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5814 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5815 | return false; |
| 5816 | } |
| 5817 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5818 | //===----------------------------------------------------------------------===// |
| 5819 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5820 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5821 | // handling. |
| 5822 | //===----------------------------------------------------------------------===// |
| 5823 | |
| 5824 | namespace { |
| 5825 | |
| 5826 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5827 | public: |
| 5828 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5829 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5830 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5831 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5832 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5833 | }; |
| 5834 | |
| 5835 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5836 | llvm::GlobalValue *GV, |
| 5837 | CodeGen::CodeGenModule &M) const { |
| 5838 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5839 | if (!FD) return; |
| 5840 | |
| 5841 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5842 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5843 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5844 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5845 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5846 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5847 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5848 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5849 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5850 | llvm::LLVMContext &Context = F->getContext(); |
| 5851 | llvm::NamedMDNode *OpenCLMetadata = |
| 5852 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5853 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5854 | SmallVector<llvm::Metadata *, 5> Operands; |
| 5855 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5856 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5857 | Operands.push_back( |
| 5858 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5859 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 5860 | Operands.push_back( |
| 5861 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5862 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 5863 | Operands.push_back( |
| 5864 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5865 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5866 | |
| 5867 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5868 | // for implementing the work_group_size_hint attr later. Currently |
| 5869 | // always true as the hint is not yet implemented. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5870 | Operands.push_back( |
| 5871 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5872 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5873 | } |
| 5874 | } |
| 5875 | } |
| 5876 | } |
| 5877 | |
| 5878 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5879 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5880 | //===----------------------------------------------------------------------===// |
| 5881 | // Hexagon ABI Implementation |
| 5882 | //===----------------------------------------------------------------------===// |
| 5883 | |
| 5884 | namespace { |
| 5885 | |
| 5886 | class HexagonABIInfo : public ABIInfo { |
| 5887 | |
| 5888 | |
| 5889 | public: |
| 5890 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5891 | |
| 5892 | private: |
| 5893 | |
| 5894 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5895 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5896 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5897 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5898 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5899 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5900 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5901 | }; |
| 5902 | |
| 5903 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5904 | public: |
| 5905 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5906 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5907 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5908 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5909 | return 29; |
| 5910 | } |
| 5911 | }; |
| 5912 | |
| 5913 | } |
| 5914 | |
| 5915 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5916 | if (!getCXXABI().classifyReturnType(FI)) |
| 5917 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5918 | for (auto &I : FI.arguments()) |
| 5919 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5920 | } |
| 5921 | |
| 5922 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5923 | if (!isAggregateTypeForABI(Ty)) { |
| 5924 | // Treat an enum type as its underlying type. |
| 5925 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5926 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5927 | |
| 5928 | return (Ty->isPromotableIntegerType() ? |
| 5929 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5930 | } |
| 5931 | |
| 5932 | // Ignore empty records. |
| 5933 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5934 | return ABIArgInfo::getIgnore(); |
| 5935 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5936 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5937 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5938 | |
| 5939 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5940 | if (Size > 64) |
| 5941 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5942 | // Pass in the smallest viable integer type. |
| 5943 | else if (Size > 32) |
| 5944 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5945 | else if (Size > 16) |
| 5946 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5947 | else if (Size > 8) |
| 5948 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5949 | else |
| 5950 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5951 | } |
| 5952 | |
| 5953 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5954 | if (RetTy->isVoidType()) |
| 5955 | return ABIArgInfo::getIgnore(); |
| 5956 | |
| 5957 | // Large vector types should be returned via memory. |
| 5958 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5959 | return ABIArgInfo::getIndirect(0); |
| 5960 | |
| 5961 | if (!isAggregateTypeForABI(RetTy)) { |
| 5962 | // Treat an enum type as its underlying type. |
| 5963 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5964 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5965 | |
| 5966 | return (RetTy->isPromotableIntegerType() ? |
| 5967 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5968 | } |
| 5969 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5970 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5971 | return ABIArgInfo::getIgnore(); |
| 5972 | |
| 5973 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5974 | // are returned indirectly. |
| 5975 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5976 | if (Size <= 64) { |
| 5977 | // Return in the smallest viable integer type. |
| 5978 | if (Size <= 8) |
| 5979 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5980 | if (Size <= 16) |
| 5981 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5982 | if (Size <= 32) |
| 5983 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5984 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5985 | } |
| 5986 | |
| 5987 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5988 | } |
| 5989 | |
| 5990 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5991 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5992 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5993 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5994 | |
| 5995 | CGBuilderTy &Builder = CGF.Builder; |
| 5996 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5997 | "ap"); |
| 5998 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5999 | llvm::Type *PTy = |
| 6000 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 6001 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 6002 | |
| 6003 | uint64_t Offset = |
| 6004 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 6005 | llvm::Value *NextAddr = |
| 6006 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 6007 | "ap.next"); |
| 6008 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 6009 | |
| 6010 | return AddrTyped; |
| 6011 | } |
| 6012 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6013 | //===----------------------------------------------------------------------===// |
| 6014 | // AMDGPU ABI Implementation |
| 6015 | //===----------------------------------------------------------------------===// |
| 6016 | |
| 6017 | namespace { |
| 6018 | |
| 6019 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6020 | public: |
| 6021 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6022 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| 6023 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 6024 | CodeGen::CodeGenModule &M) const override; |
| 6025 | }; |
| 6026 | |
| 6027 | } |
| 6028 | |
| 6029 | void AMDGPUTargetCodeGenInfo::SetTargetAttributes( |
| 6030 | const Decl *D, |
| 6031 | llvm::GlobalValue *GV, |
| 6032 | CodeGen::CodeGenModule &M) const { |
| 6033 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 6034 | if (!FD) |
| 6035 | return; |
| 6036 | |
| 6037 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6038 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6039 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6040 | if (NumVGPR != 0) |
| 6041 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6042 | } |
| 6043 | |
| 6044 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6045 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6046 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6047 | if (NumSGPR != 0) |
| 6048 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6049 | } |
| 6050 | } |
| 6051 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6052 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6053 | //===----------------------------------------------------------------------===// |
| 6054 | // SPARC v9 ABI Implementation. |
| 6055 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6056 | // |
| 6057 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6058 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6059 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6060 | // |
| 6061 | // One case requires special care: |
| 6062 | // |
| 6063 | // struct mixed { |
| 6064 | // int i; |
| 6065 | // float f; |
| 6066 | // }; |
| 6067 | // |
| 6068 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6069 | // parameter array, but the int is passed in an integer register, and the float |
| 6070 | // is passed in a floating point register. This is represented as two arguments |
| 6071 | // with the LLVM IR inreg attribute: |
| 6072 | // |
| 6073 | // declare void f(i32 inreg %i, float inreg %f) |
| 6074 | // |
| 6075 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6076 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6077 | // bytes. |
| 6078 | // |
| 6079 | namespace { |
| 6080 | class SparcV9ABIInfo : public ABIInfo { |
| 6081 | public: |
| 6082 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6083 | |
| 6084 | private: |
| 6085 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6086 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6087 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6088 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6089 | |
| 6090 | // Coercion type builder for structs passed in registers. The coercion type |
| 6091 | // serves two purposes: |
| 6092 | // |
| 6093 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6094 | // in registers. |
| 6095 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6096 | // code generator knows to pass them in floating point registers. |
| 6097 | // |
| 6098 | // We also compute the InReg flag which indicates that the struct contains |
| 6099 | // aligned 32-bit floats. |
| 6100 | // |
| 6101 | struct CoerceBuilder { |
| 6102 | llvm::LLVMContext &Context; |
| 6103 | const llvm::DataLayout &DL; |
| 6104 | SmallVector<llvm::Type*, 8> Elems; |
| 6105 | uint64_t Size; |
| 6106 | bool InReg; |
| 6107 | |
| 6108 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6109 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6110 | |
| 6111 | // Pad Elems with integers until Size is ToSize. |
| 6112 | void pad(uint64_t ToSize) { |
| 6113 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6114 | if (ToSize == Size) |
| 6115 | return; |
| 6116 | |
| 6117 | // Finish the current 64-bit word. |
| 6118 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6119 | if (Aligned > Size && Aligned <= ToSize) { |
| 6120 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6121 | Size = Aligned; |
| 6122 | } |
| 6123 | |
| 6124 | // Add whole 64-bit words. |
| 6125 | while (Size + 64 <= ToSize) { |
| 6126 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6127 | Size += 64; |
| 6128 | } |
| 6129 | |
| 6130 | // Final in-word padding. |
| 6131 | if (Size < ToSize) { |
| 6132 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6133 | Size = ToSize; |
| 6134 | } |
| 6135 | } |
| 6136 | |
| 6137 | // Add a floating point element at Offset. |
| 6138 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6139 | // Unaligned floats are treated as integers. |
| 6140 | if (Offset % Bits) |
| 6141 | return; |
| 6142 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6143 | if (Bits < 64) |
| 6144 | InReg = true; |
| 6145 | pad(Offset); |
| 6146 | Elems.push_back(Ty); |
| 6147 | Size = Offset + Bits; |
| 6148 | } |
| 6149 | |
| 6150 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6151 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6152 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6153 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6154 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6155 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6156 | switch (ElemTy->getTypeID()) { |
| 6157 | case llvm::Type::StructTyID: |
| 6158 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6159 | break; |
| 6160 | case llvm::Type::FloatTyID: |
| 6161 | addFloat(ElemOffset, ElemTy, 32); |
| 6162 | break; |
| 6163 | case llvm::Type::DoubleTyID: |
| 6164 | addFloat(ElemOffset, ElemTy, 64); |
| 6165 | break; |
| 6166 | case llvm::Type::FP128TyID: |
| 6167 | addFloat(ElemOffset, ElemTy, 128); |
| 6168 | break; |
| 6169 | case llvm::Type::PointerTyID: |
| 6170 | if (ElemOffset % 64 == 0) { |
| 6171 | pad(ElemOffset); |
| 6172 | Elems.push_back(ElemTy); |
| 6173 | Size += 64; |
| 6174 | } |
| 6175 | break; |
| 6176 | default: |
| 6177 | break; |
| 6178 | } |
| 6179 | } |
| 6180 | } |
| 6181 | |
| 6182 | // Check if Ty is a usable substitute for the coercion type. |
| 6183 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6184 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6185 | } |
| 6186 | |
| 6187 | // Get the coercion type as a literal struct type. |
| 6188 | llvm::Type *getType() const { |
| 6189 | if (Elems.size() == 1) |
| 6190 | return Elems.front(); |
| 6191 | else |
| 6192 | return llvm::StructType::get(Context, Elems); |
| 6193 | } |
| 6194 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6195 | }; |
| 6196 | } // end anonymous namespace |
| 6197 | |
| 6198 | ABIArgInfo |
| 6199 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6200 | if (Ty->isVoidType()) |
| 6201 | return ABIArgInfo::getIgnore(); |
| 6202 | |
| 6203 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6204 | |
| 6205 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6206 | // pointer / sret pointer. |
| 6207 | if (Size > SizeLimit) |
| 6208 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 6209 | |
| 6210 | // Treat an enum type as its underlying type. |
| 6211 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6212 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6213 | |
| 6214 | // Integer types smaller than a register are extended. |
| 6215 | if (Size < 64 && Ty->isIntegerType()) |
| 6216 | return ABIArgInfo::getExtend(); |
| 6217 | |
| 6218 | // Other non-aggregates go in registers. |
| 6219 | if (!isAggregateTypeForABI(Ty)) |
| 6220 | return ABIArgInfo::getDirect(); |
| 6221 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6222 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6223 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6224 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 6225 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 6226 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6227 | // This is a small aggregate type that should be passed in registers. |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6228 | // Build a coercion type from the LLVM struct type. |
| 6229 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6230 | if (!StrTy) |
| 6231 | return ABIArgInfo::getDirect(); |
| 6232 | |
| 6233 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6234 | CB.addStruct(0, StrTy); |
| 6235 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6236 | |
| 6237 | // Try to use the original type for coercion. |
| 6238 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6239 | |
| 6240 | if (CB.InReg) |
| 6241 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6242 | else |
| 6243 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
| 6246 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6247 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6248 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6249 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6250 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6251 | AI.setCoerceToType(ArgTy); |
| 6252 | |
| 6253 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6254 | CGBuilderTy &Builder = CGF.Builder; |
| 6255 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6256 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6257 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6258 | llvm::Value *ArgAddr; |
| 6259 | unsigned Stride; |
| 6260 | |
| 6261 | switch (AI.getKind()) { |
| 6262 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6263 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6264 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6265 | |
| 6266 | case ABIArgInfo::Extend: |
| 6267 | Stride = 8; |
| 6268 | ArgAddr = Builder |
| 6269 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6270 | "extend"); |
| 6271 | break; |
| 6272 | |
| 6273 | case ABIArgInfo::Direct: |
| 6274 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6275 | ArgAddr = Addr; |
| 6276 | break; |
| 6277 | |
| 6278 | case ABIArgInfo::Indirect: |
| 6279 | Stride = 8; |
| 6280 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6281 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6282 | "indirect"); |
| 6283 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6284 | break; |
| 6285 | |
| 6286 | case ABIArgInfo::Ignore: |
| 6287 | return llvm::UndefValue::get(ArgPtrTy); |
| 6288 | } |
| 6289 | |
| 6290 | // Update VAList. |
| 6291 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6292 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6293 | |
| 6294 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6295 | } |
| 6296 | |
| 6297 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6298 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6299 | for (auto &I : FI.arguments()) |
| 6300 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6301 | } |
| 6302 | |
| 6303 | namespace { |
| 6304 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6305 | public: |
| 6306 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6307 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6308 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6309 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6310 | return 14; |
| 6311 | } |
| 6312 | |
| 6313 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6314 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6315 | }; |
| 6316 | } // end anonymous namespace |
| 6317 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6318 | bool |
| 6319 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6320 | llvm::Value *Address) const { |
| 6321 | // This is calculated from the LLVM and GCC tables and verified |
| 6322 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6323 | |
| 6324 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6325 | |
| 6326 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6327 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6328 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6329 | |
| 6330 | // 0-31: the 8-byte general-purpose registers |
| 6331 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6332 | |
| 6333 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6334 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6335 | |
| 6336 | // Y = 64 |
| 6337 | // PSR = 65 |
| 6338 | // WIM = 66 |
| 6339 | // TBR = 67 |
| 6340 | // PC = 68 |
| 6341 | // NPC = 69 |
| 6342 | // FSR = 70 |
| 6343 | // CSR = 71 |
| 6344 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 6345 | |
| 6346 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6347 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6348 | |
| 6349 | return false; |
| 6350 | } |
| 6351 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6352 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6353 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6354 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6355 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6356 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6357 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6358 | |
| 6359 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6360 | /// it by reference between functions that append to it. |
| 6361 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6362 | |
| 6363 | /// TypeStringCache caches the meta encodings of Types. |
| 6364 | /// |
| 6365 | /// The reason for caching TypeStrings is two fold: |
| 6366 | /// 1. To cache a type's encoding for later uses; |
| 6367 | /// 2. As a means to break recursive member type inclusion. |
| 6368 | /// |
| 6369 | /// A cache Entry can have a Status of: |
| 6370 | /// NonRecursive: The type encoding is not recursive; |
| 6371 | /// Recursive: The type encoding is recursive; |
| 6372 | /// Incomplete: An incomplete TypeString; |
| 6373 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6374 | /// Recursive type encoding. |
| 6375 | /// |
| 6376 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6377 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6378 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6379 | /// the type is encountered. |
| 6380 | /// |
| 6381 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6382 | /// possible. The type itself is recursive and it may contain other types which |
| 6383 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6384 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6385 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6386 | /// |
| 6387 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6388 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6389 | /// are placed into the cache during type expansion as a means to identify and |
| 6390 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6391 | /// the entry becomes IncompleteUsed. |
| 6392 | /// |
| 6393 | /// During the expansion of a RecordType's members: |
| 6394 | /// |
| 6395 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6396 | /// cached encoding is used; |
| 6397 | /// |
| 6398 | /// If the cache contains a Recursive encoding for the member type, the |
| 6399 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6400 | /// |
| 6401 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6402 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6403 | /// |
| 6404 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6405 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6406 | /// it is swapped back in; |
| 6407 | /// |
| 6408 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6409 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6410 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6411 | /// |
| 6412 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6413 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6414 | /// Else the member is part of a recursive type and thus the recursion has |
| 6415 | /// been exited too soon for the encoding to be correct for the member. |
| 6416 | /// |
| 6417 | class TypeStringCache { |
| 6418 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6419 | struct Entry { |
| 6420 | std::string Str; // The encoded TypeString for the type. |
| 6421 | enum Status State; // Information about the encoding in 'Str'. |
| 6422 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6423 | // during the expansion of RecordType's members. |
| 6424 | }; |
| 6425 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6426 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6427 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6428 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6429 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6430 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6431 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6432 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6433 | bool IsRecursive); |
| 6434 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6435 | }; |
| 6436 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6437 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6438 | /// FieldEncoding is a helper for this ordering process. |
| 6439 | class FieldEncoding { |
| 6440 | bool HasName; |
| 6441 | std::string Enc; |
| 6442 | public: |
| 6443 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6444 | StringRef str() {return Enc.c_str();}; |
| 6445 | bool operator<(const FieldEncoding &rhs) const { |
| 6446 | if (HasName != rhs.HasName) return HasName; |
| 6447 | return Enc < rhs.Enc; |
| 6448 | } |
| 6449 | }; |
| 6450 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6451 | class XCoreABIInfo : public DefaultABIInfo { |
| 6452 | public: |
| 6453 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6454 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6455 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6456 | }; |
| 6457 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6458 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6459 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6460 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6461 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6462 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6463 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6464 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6465 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6466 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6467 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6468 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6469 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6470 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6471 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6472 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6473 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6474 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6475 | CGF.Int8PtrPtrTy); |
| 6476 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6477 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6478 | // Handle the argument. |
| 6479 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6480 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6481 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6482 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6483 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6484 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6485 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6486 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6487 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6488 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6489 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6490 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6491 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6492 | ArgSize = 0; |
| 6493 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6494 | case ABIArgInfo::Extend: |
| 6495 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6496 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6497 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6498 | if (ArgSize < 4) |
| 6499 | ArgSize = 4; |
| 6500 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6501 | case ABIArgInfo::Indirect: |
| 6502 | llvm::Value *ArgAddr; |
| 6503 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6504 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6505 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6506 | ArgSize = 4; |
| 6507 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6508 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6509 | |
| 6510 | // Increment the VAList. |
| 6511 | if (ArgSize) { |
| 6512 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6513 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6514 | } |
| 6515 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6516 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6517 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6518 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6519 | /// into the cache as a means to identify and break recursion. |
| 6520 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6521 | /// be reinserted by removeIncomplete(). |
| 6522 | /// All other types of encoding should have been used rather than arriving here. |
| 6523 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6524 | std::string StubEnc) { |
| 6525 | if (!ID) |
| 6526 | return; |
| 6527 | Entry &E = Map[ID]; |
| 6528 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6529 | "Incorrectly use of addIncomplete"); |
| 6530 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6531 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6532 | E.Str.swap(StubEnc); |
| 6533 | E.State = Incomplete; |
| 6534 | ++IncompleteCount; |
| 6535 | } |
| 6536 | |
| 6537 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6538 | /// must be removed from the cache. |
| 6539 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6540 | /// Returns true if the RecordType was defined recursively. |
| 6541 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6542 | if (!ID) |
| 6543 | return false; |
| 6544 | auto I = Map.find(ID); |
| 6545 | assert(I != Map.end() && "Entry not present"); |
| 6546 | Entry &E = I->second; |
| 6547 | assert( (E.State == Incomplete || |
| 6548 | E.State == IncompleteUsed) && |
| 6549 | "Entry must be an incomplete type"); |
| 6550 | bool IsRecursive = false; |
| 6551 | if (E.State == IncompleteUsed) { |
| 6552 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6553 | IsRecursive = true; |
| 6554 | --IncompleteUsedCount; |
| 6555 | } |
| 6556 | if (E.Swapped.empty()) |
| 6557 | Map.erase(I); |
| 6558 | else { |
| 6559 | // Swap the Recursive back. |
| 6560 | E.Swapped.swap(E.Str); |
| 6561 | E.Swapped.clear(); |
| 6562 | E.State = Recursive; |
| 6563 | } |
| 6564 | --IncompleteCount; |
| 6565 | return IsRecursive; |
| 6566 | } |
| 6567 | |
| 6568 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6569 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6570 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6571 | bool IsRecursive) { |
| 6572 | if (!ID || IncompleteUsedCount) |
| 6573 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6574 | Entry &E = Map[ID]; |
| 6575 | if (IsRecursive && !E.Str.empty()) { |
| 6576 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6577 | "This is not the same Recursive entry"); |
| 6578 | // The parent container was not recursive after all, so we could have used |
| 6579 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6580 | // we started viz: IncompleteCount!=0. |
| 6581 | return; |
| 6582 | } |
| 6583 | assert(E.Str.empty() && "Entry already present"); |
| 6584 | E.Str = Str.str(); |
| 6585 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6586 | } |
| 6587 | |
| 6588 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6589 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6590 | /// encoding is Recursive, return an empty StringRef. |
| 6591 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6592 | if (!ID) |
| 6593 | return StringRef(); // We have no key. |
| 6594 | auto I = Map.find(ID); |
| 6595 | if (I == Map.end()) |
| 6596 | return StringRef(); // We have no encoding. |
| 6597 | Entry &E = I->second; |
| 6598 | if (E.State == Recursive && IncompleteCount) |
| 6599 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6600 | |
| 6601 | if (E.State == Incomplete) { |
| 6602 | // The incomplete type is being used to break out of recursion. |
| 6603 | E.State = IncompleteUsed; |
| 6604 | ++IncompleteUsedCount; |
| 6605 | } |
| 6606 | return E.Str.c_str(); |
| 6607 | } |
| 6608 | |
| 6609 | /// The XCore ABI includes a type information section that communicates symbol |
| 6610 | /// type information to the linker. The linker uses this information to verify |
| 6611 | /// safety/correctness of things such as array bound and pointers et al. |
| 6612 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6613 | /// This type information (TypeString) is emitted into meta data for all global |
| 6614 | /// symbols: definitions, declarations, functions & variables. |
| 6615 | /// |
| 6616 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6617 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
| 6618 | /// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf> |
| 6619 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6620 | /// |
| 6621 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6622 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6623 | |
| 6624 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6625 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6626 | CodeGen::CodeGenModule &CGM) const { |
| 6627 | SmallStringEnc Enc; |
| 6628 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6629 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6630 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 6631 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6632 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6633 | llvm::NamedMDNode *MD = |
| 6634 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6635 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6636 | } |
| 6637 | } |
| 6638 | |
| 6639 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6640 | const CodeGen::CodeGenModule &CGM, |
| 6641 | TypeStringCache &TSC); |
| 6642 | |
| 6643 | /// Helper function for appendRecordType(). |
| 6644 | /// Builds a SmallVector containing the encoded field types in declaration order. |
| 6645 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6646 | const RecordDecl *RD, |
| 6647 | const CodeGen::CodeGenModule &CGM, |
| 6648 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6649 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6650 | SmallStringEnc Enc; |
| 6651 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6652 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6653 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6654 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6655 | Enc += "b("; |
| 6656 | llvm::raw_svector_ostream OS(Enc); |
| 6657 | OS.resync(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6658 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6659 | OS.flush(); |
| 6660 | Enc += ':'; |
| 6661 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6662 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6663 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6664 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6665 | Enc += ')'; |
| 6666 | Enc += '}'; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6667 | FE.push_back(FieldEncoding(!Field->getName().empty(), Enc)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6668 | } |
| 6669 | return true; |
| 6670 | } |
| 6671 | |
| 6672 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6673 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6674 | /// Union types have their fields ordered according to the ABI. |
| 6675 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6676 | const CodeGen::CodeGenModule &CGM, |
| 6677 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6678 | // Append the cached TypeString if we have one. |
| 6679 | StringRef TypeString = TSC.lookupStr(ID); |
| 6680 | if (!TypeString.empty()) { |
| 6681 | Enc += TypeString; |
| 6682 | return true; |
| 6683 | } |
| 6684 | |
| 6685 | // Start to emit an incomplete TypeString. |
| 6686 | size_t Start = Enc.size(); |
| 6687 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6688 | Enc += '('; |
| 6689 | if (ID) |
| 6690 | Enc += ID->getName(); |
| 6691 | Enc += "){"; |
| 6692 | |
| 6693 | // We collect all encoded fields and order as necessary. |
| 6694 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6695 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6696 | if (RD && !RD->field_empty()) { |
| 6697 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6698 | // so that recursive calls to this RecordType will use it whilst building a |
| 6699 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6700 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6701 | std::string StubEnc(Enc.substr(Start).str()); |
| 6702 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6703 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6704 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6705 | (void) TSC.removeIncomplete(ID); |
| 6706 | return false; |
| 6707 | } |
| 6708 | IsRecursive = TSC.removeIncomplete(ID); |
| 6709 | // The ABI requires unions to be sorted but not structures. |
| 6710 | // See FieldEncoding::operator< for sort algorithm. |
| 6711 | if (RT->isUnionType()) |
| 6712 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6713 | // We can now complete the TypeString. |
| 6714 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6715 | for (unsigned I = 0; I != E; ++I) { |
| 6716 | if (I) |
| 6717 | Enc += ','; |
| 6718 | Enc += FE[I].str(); |
| 6719 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6720 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6721 | Enc += '}'; |
| 6722 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6723 | return true; |
| 6724 | } |
| 6725 | |
| 6726 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6727 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6728 | TypeStringCache &TSC, |
| 6729 | const IdentifierInfo *ID) { |
| 6730 | // Append the cached TypeString if we have one. |
| 6731 | StringRef TypeString = TSC.lookupStr(ID); |
| 6732 | if (!TypeString.empty()) { |
| 6733 | Enc += TypeString; |
| 6734 | return true; |
| 6735 | } |
| 6736 | |
| 6737 | size_t Start = Enc.size(); |
| 6738 | Enc += "e("; |
| 6739 | if (ID) |
| 6740 | Enc += ID->getName(); |
| 6741 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6742 | |
| 6743 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6744 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6745 | SmallVector<FieldEncoding, 16> FE; |
| 6746 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6747 | ++I) { |
| 6748 | SmallStringEnc EnumEnc; |
| 6749 | EnumEnc += "m("; |
| 6750 | EnumEnc += I->getName(); |
| 6751 | EnumEnc += "){"; |
| 6752 | I->getInitVal().toString(EnumEnc); |
| 6753 | EnumEnc += '}'; |
| 6754 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6755 | } |
| 6756 | std::sort(FE.begin(), FE.end()); |
| 6757 | unsigned E = FE.size(); |
| 6758 | for (unsigned I = 0; I != E; ++I) { |
| 6759 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6760 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6761 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6762 | } |
| 6763 | } |
| 6764 | Enc += '}'; |
| 6765 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6766 | return true; |
| 6767 | } |
| 6768 | |
| 6769 | /// Appends type's qualifier to Enc. |
| 6770 | /// This is done prior to appending the type's encoding. |
| 6771 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6772 | // Qualifiers are emitted in alphabetical order. |
| 6773 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6774 | int Lookup = 0; |
| 6775 | if (QT.isConstQualified()) |
| 6776 | Lookup += 1<<0; |
| 6777 | if (QT.isRestrictQualified()) |
| 6778 | Lookup += 1<<1; |
| 6779 | if (QT.isVolatileQualified()) |
| 6780 | Lookup += 1<<2; |
| 6781 | Enc += Table[Lookup]; |
| 6782 | } |
| 6783 | |
| 6784 | /// Appends built-in types to Enc. |
| 6785 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6786 | const char *EncType; |
| 6787 | switch (BT->getKind()) { |
| 6788 | case BuiltinType::Void: |
| 6789 | EncType = "0"; |
| 6790 | break; |
| 6791 | case BuiltinType::Bool: |
| 6792 | EncType = "b"; |
| 6793 | break; |
| 6794 | case BuiltinType::Char_U: |
| 6795 | EncType = "uc"; |
| 6796 | break; |
| 6797 | case BuiltinType::UChar: |
| 6798 | EncType = "uc"; |
| 6799 | break; |
| 6800 | case BuiltinType::SChar: |
| 6801 | EncType = "sc"; |
| 6802 | break; |
| 6803 | case BuiltinType::UShort: |
| 6804 | EncType = "us"; |
| 6805 | break; |
| 6806 | case BuiltinType::Short: |
| 6807 | EncType = "ss"; |
| 6808 | break; |
| 6809 | case BuiltinType::UInt: |
| 6810 | EncType = "ui"; |
| 6811 | break; |
| 6812 | case BuiltinType::Int: |
| 6813 | EncType = "si"; |
| 6814 | break; |
| 6815 | case BuiltinType::ULong: |
| 6816 | EncType = "ul"; |
| 6817 | break; |
| 6818 | case BuiltinType::Long: |
| 6819 | EncType = "sl"; |
| 6820 | break; |
| 6821 | case BuiltinType::ULongLong: |
| 6822 | EncType = "ull"; |
| 6823 | break; |
| 6824 | case BuiltinType::LongLong: |
| 6825 | EncType = "sll"; |
| 6826 | break; |
| 6827 | case BuiltinType::Float: |
| 6828 | EncType = "ft"; |
| 6829 | break; |
| 6830 | case BuiltinType::Double: |
| 6831 | EncType = "d"; |
| 6832 | break; |
| 6833 | case BuiltinType::LongDouble: |
| 6834 | EncType = "ld"; |
| 6835 | break; |
| 6836 | default: |
| 6837 | return false; |
| 6838 | } |
| 6839 | Enc += EncType; |
| 6840 | return true; |
| 6841 | } |
| 6842 | |
| 6843 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6844 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6845 | const CodeGen::CodeGenModule &CGM, |
| 6846 | TypeStringCache &TSC) { |
| 6847 | Enc += "p("; |
| 6848 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6849 | return false; |
| 6850 | Enc += ')'; |
| 6851 | return true; |
| 6852 | } |
| 6853 | |
| 6854 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6855 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6856 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6857 | const CodeGen::CodeGenModule &CGM, |
| 6858 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6859 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6860 | return false; |
| 6861 | Enc += "a("; |
| 6862 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6863 | CAT->getSize().toStringUnsigned(Enc); |
| 6864 | else |
| 6865 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6866 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6867 | // The Qualifiers should be attached to the type rather than the array. |
| 6868 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6869 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6870 | return false; |
| 6871 | Enc += ')'; |
| 6872 | return true; |
| 6873 | } |
| 6874 | |
| 6875 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6876 | /// and the arguments. |
| 6877 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6878 | const CodeGen::CodeGenModule &CGM, |
| 6879 | TypeStringCache &TSC) { |
| 6880 | Enc += "f{"; |
| 6881 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 6882 | return false; |
| 6883 | Enc += "}("; |
| 6884 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 6885 | // N.B. we are only interested in the adjusted param types. |
| 6886 | auto I = FPT->param_type_begin(); |
| 6887 | auto E = FPT->param_type_end(); |
| 6888 | if (I != E) { |
| 6889 | do { |
| 6890 | if (!appendType(Enc, *I, CGM, TSC)) |
| 6891 | return false; |
| 6892 | ++I; |
| 6893 | if (I != E) |
| 6894 | Enc += ','; |
| 6895 | } while (I != E); |
| 6896 | if (FPT->isVariadic()) |
| 6897 | Enc += ",va"; |
| 6898 | } else { |
| 6899 | if (FPT->isVariadic()) |
| 6900 | Enc += "va"; |
| 6901 | else |
| 6902 | Enc += '0'; |
| 6903 | } |
| 6904 | } |
| 6905 | Enc += ')'; |
| 6906 | return true; |
| 6907 | } |
| 6908 | |
| 6909 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 6910 | /// type encodings. |
| 6911 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6912 | const CodeGen::CodeGenModule &CGM, |
| 6913 | TypeStringCache &TSC) { |
| 6914 | |
| 6915 | QualType QT = QType.getCanonicalType(); |
| 6916 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6917 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 6918 | // The Qualifiers should be attached to the type rather than the array. |
| 6919 | // Thus we don't call appendQualifier() here. |
| 6920 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 6921 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6922 | appendQualifier(Enc, QT); |
| 6923 | |
| 6924 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 6925 | return appendBuiltinType(Enc, BT); |
| 6926 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6927 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 6928 | return appendPointerType(Enc, PT, CGM, TSC); |
| 6929 | |
| 6930 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 6931 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 6932 | |
| 6933 | if (const RecordType *RT = QT->getAsStructureType()) |
| 6934 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6935 | |
| 6936 | if (const RecordType *RT = QT->getAsUnionType()) |
| 6937 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6938 | |
| 6939 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 6940 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 6941 | |
| 6942 | return false; |
| 6943 | } |
| 6944 | |
| 6945 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6946 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 6947 | if (!D) |
| 6948 | return false; |
| 6949 | |
| 6950 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6951 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 6952 | return false; |
| 6953 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 6954 | } |
| 6955 | |
| 6956 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6957 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 6958 | return false; |
| 6959 | QualType QT = VD->getType().getCanonicalType(); |
| 6960 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 6961 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6962 | // The Qualifiers should be attached to the type rather than the array. |
| 6963 | // Thus we don't call appendQualifier() here. |
| 6964 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6965 | } |
| 6966 | return appendType(Enc, QT, CGM, TSC); |
| 6967 | } |
| 6968 | return false; |
| 6969 | } |
| 6970 | |
| 6971 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6972 | //===----------------------------------------------------------------------===// |
| 6973 | // Driver code |
| 6974 | //===----------------------------------------------------------------------===// |
| 6975 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 6976 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 6977 | return getTarget().getTriple(); |
| 6978 | } |
| 6979 | |
| 6980 | bool CodeGenModule::supportsCOMDAT() const { |
| 6981 | return !getTriple().isOSBinFormatMachO(); |
| 6982 | } |
| 6983 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6984 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6985 | if (TheTargetCodeGenInfo) |
| 6986 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6987 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6988 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 6989 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6990 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6991 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6992 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 6993 | case llvm::Triple::le32: |
| 6994 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6995 | case llvm::Triple::mips: |
| 6996 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6997 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 6998 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 6999 | case llvm::Triple::mips64: |
| 7000 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7001 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7002 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7003 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7004 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7005 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7006 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7007 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7008 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7009 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7010 | } |
| 7011 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7012 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7013 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7014 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7015 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7016 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7017 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7018 | TheTargetCodeGenInfo = |
| 7019 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7020 | return *TheTargetCodeGenInfo; |
| 7021 | } |
| 7022 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7023 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7024 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7025 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7026 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7027 | (CodeGenOpts.FloatABI != "soft" && |
| 7028 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7029 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7030 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7031 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7032 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7033 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7034 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7035 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7036 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7037 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7038 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7039 | if (getTarget().getABI() == "elfv2") |
| 7040 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 7041 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7042 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7043 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 7044 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7045 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7046 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7047 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7048 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7049 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 7050 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7051 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 7052 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7053 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7054 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame^] | 7055 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7056 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7057 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7058 | case llvm::Triple::nvptx: |
| 7059 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7060 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7061 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7062 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7063 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7064 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7065 | case llvm::Triple::systemz: |
| 7066 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 7067 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7068 | case llvm::Triple::tce: |
| 7069 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7070 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7071 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7072 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 7073 | bool IsSmallStructInRegABI = |
| 7074 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7075 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7076 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7077 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 7078 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 7079 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7080 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7081 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 7082 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7083 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7084 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7085 | new X86_32TargetCodeGenInfo(Types, |
| 7086 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7087 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 7088 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7089 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7090 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7091 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7092 | case llvm::Triple::x86_64: { |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7093 | bool HasAVX = getTarget().getABI() == "avx"; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7094 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7095 | switch (Triple.getOS()) { |
| 7096 | case llvm::Triple::Win32: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 7097 | return *(TheTargetCodeGenInfo = |
| 7098 | new WinX86_64TargetCodeGenInfo(Types, HasAVX)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7099 | case llvm::Triple::PS4: |
| 7100 | return *(TheTargetCodeGenInfo = new PS4TargetCodeGenInfo(Types, HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7101 | default: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 7102 | return *(TheTargetCodeGenInfo = |
| 7103 | new X86_64TargetCodeGenInfo(Types, HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7104 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7105 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7106 | case llvm::Triple::hexagon: |
| 7107 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7108 | case llvm::Triple::r600: |
| 7109 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7110 | case llvm::Triple::amdgcn: |
| 7111 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7112 | case llvm::Triple::sparcv9: |
| 7113 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7114 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7115 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7116 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7117 | } |