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) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 40 | llvm::Value *Cell = |
| 41 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 42 | Builder.CreateStore(Value, Cell); |
| 43 | } |
| 44 | } |
| 45 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 46 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 47 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 48 | T->isMemberFunctionPointerType(); |
| 49 | } |
| 50 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 51 | ABIInfo::~ABIInfo() {} |
| 52 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 53 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 54 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 55 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 56 | if (!RD) |
| 57 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 58 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 62 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 63 | const RecordType *RT = T->getAs<RecordType>(); |
| 64 | if (!RT) |
| 65 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 66 | return getRecordArgABI(RT, CXXABI); |
| 67 | } |
| 68 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 69 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 70 | /// should ensure that all elements of the union have the same "machine type". |
| 71 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 72 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 73 | const RecordDecl *UD = UT->getDecl(); |
| 74 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 75 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 76 | return UD->field_begin()->getType(); |
| 77 | } |
| 78 | } |
| 79 | return Ty; |
| 80 | } |
| 81 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 82 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 83 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 86 | ASTContext &ABIInfo::getContext() const { |
| 87 | return CGT.getContext(); |
| 88 | } |
| 89 | |
| 90 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 91 | return CGT.getLLVMContext(); |
| 92 | } |
| 93 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 94 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 95 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 98 | const TargetInfo &ABIInfo::getTarget() const { |
| 99 | return CGT.getTarget(); |
| 100 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 101 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 102 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 107 | uint64_t Members) const { |
| 108 | return false; |
| 109 | } |
| 110 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 111 | bool ABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 112 | return false; |
| 113 | } |
| 114 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 115 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 116 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 117 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 118 | switch (TheKind) { |
| 119 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 120 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 121 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 122 | Ty->print(OS); |
| 123 | else |
| 124 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 125 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 126 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 127 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 128 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 129 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 130 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 131 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 132 | case InAlloca: |
| 133 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 134 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 135 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 136 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 137 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 138 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 139 | break; |
| 140 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 141 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 142 | break; |
| 143 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 144 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 147 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 148 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 149 | // If someone can figure out a general rule for this, that would be great. |
| 150 | // It's probably just doomed to be platform-dependent, though. |
| 151 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 152 | // Verified for: |
| 153 | // x86-64 FreeBSD, Linux, Darwin |
| 154 | // x86-32 FreeBSD, Linux, Darwin |
| 155 | // PowerPC Linux, Darwin |
| 156 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 157 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 158 | return 32; |
| 159 | } |
| 160 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 161 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 162 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 163 | // The following conventions are known to require this to be false: |
| 164 | // x86_stdcall |
| 165 | // MIPS |
| 166 | // For everything else, we just prefer false unless we opt out. |
| 167 | return false; |
| 168 | } |
| 169 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 170 | void |
| 171 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 172 | llvm::SmallString<24> &Opt) const { |
| 173 | // This assumes the user is passing a library name like "rt" instead of a |
| 174 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 175 | // dynamic. |
| 176 | Opt = "-l"; |
| 177 | Opt += Lib; |
| 178 | } |
| 179 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 180 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 181 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 182 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 183 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 184 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 185 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 186 | if (FD->isUnnamedBitfield()) |
| 187 | return true; |
| 188 | |
| 189 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 190 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 191 | // Constant arrays of empty records count as empty, strip them off. |
| 192 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 193 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 194 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 195 | if (AT->getSize() == 0) |
| 196 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 197 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 198 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 199 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 200 | const RecordType *RT = FT->getAs<RecordType>(); |
| 201 | if (!RT) |
| 202 | return false; |
| 203 | |
| 204 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 205 | // |
| 206 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 207 | // current ABI. |
| 208 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 209 | return false; |
| 210 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 211 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 214 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 215 | /// fields. Note that a structure with a flexible array member is not |
| 216 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 217 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 218 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 219 | if (!RT) |
| 220 | return 0; |
| 221 | const RecordDecl *RD = RT->getDecl(); |
| 222 | if (RD->hasFlexibleArrayMember()) |
| 223 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 224 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 225 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 226 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 227 | for (const auto &I : CXXRD->bases()) |
| 228 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 229 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 230 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 231 | for (const auto *I : RD->fields()) |
| 232 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 233 | return false; |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | /// isSingleElementStruct - Determine if a structure is a "single |
| 238 | /// element struct", i.e. it has exactly one non-empty field or |
| 239 | /// exactly one field which is itself a single element |
| 240 | /// struct. Structures with flexible array members are never |
| 241 | /// considered single element structs. |
| 242 | /// |
| 243 | /// \return The field declaration for the single non-empty field, if |
| 244 | /// it exists. |
| 245 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 246 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 247 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 248 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 249 | |
| 250 | const RecordDecl *RD = RT->getDecl(); |
| 251 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 252 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 253 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 254 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 255 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 256 | // If this is a C++ record, check the bases first. |
| 257 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 258 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 259 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 260 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 261 | continue; |
| 262 | |
| 263 | // If we already found an element then this isn't a single-element struct. |
| 264 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 265 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 266 | |
| 267 | // If this is non-empty and not a single element struct, the composite |
| 268 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 269 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 270 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 271 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 276 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 277 | QualType FT = FD->getType(); |
| 278 | |
| 279 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 280 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 281 | continue; |
| 282 | |
| 283 | // If we already found an element then this isn't a single-element |
| 284 | // struct. |
| 285 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 286 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 287 | |
| 288 | // Treat single element arrays as the element. |
| 289 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 290 | if (AT->getSize().getZExtValue() != 1) |
| 291 | break; |
| 292 | FT = AT->getElementType(); |
| 293 | } |
| 294 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 295 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 296 | Found = FT.getTypePtr(); |
| 297 | } else { |
| 298 | Found = isSingleElementStruct(FT, Context); |
| 299 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 300 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 304 | // We don't consider a struct a single-element struct if it has |
| 305 | // padding beyond the element type. |
| 306 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 307 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 308 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 309 | return Found; |
| 310 | } |
| 311 | |
| 312 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 313 | // Treat complex types as the element type. |
| 314 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 315 | Ty = CTy->getElementType(); |
| 316 | |
| 317 | // Check for a type which we know has a simple scalar argument-passing |
| 318 | // convention without any padding. (We're specifically looking for 32 |
| 319 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 320 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 321 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 322 | return false; |
| 323 | |
| 324 | uint64_t Size = Context.getTypeSize(Ty); |
| 325 | return Size == 32 || Size == 64; |
| 326 | } |
| 327 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 328 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 329 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 330 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 331 | /// inhibiting optimizations. |
| 332 | /// |
| 333 | // FIXME: This predicate is missing many cases, currently it just follows |
| 334 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 335 | // should probably make this smarter, or better yet make the LLVM backend |
| 336 | // capable of handling it. |
| 337 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 338 | // We can only expand structure types. |
| 339 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 340 | if (!RT) |
| 341 | return false; |
| 342 | |
| 343 | // We can only expand (C) structures. |
| 344 | // |
| 345 | // FIXME: This needs to be generalized to handle classes as well. |
| 346 | const RecordDecl *RD = RT->getDecl(); |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 347 | if (!RD->isStruct()) |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 348 | return false; |
| 349 | |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 350 | // We try to expand CLike CXXRecordDecl. |
| 351 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 352 | if (!CXXRD->isCLike()) |
| 353 | return false; |
| 354 | } |
| 355 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 356 | uint64_t Size = 0; |
| 357 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 358 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 359 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 360 | return false; |
| 361 | |
| 362 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 363 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 364 | // counts as "basic" is more complicated than what we were doing previously. |
| 365 | if (FD->isBitField()) |
| 366 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 367 | |
| 368 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 371 | // Make sure there are not any holes in the struct. |
| 372 | if (Size != Context.getTypeSize(Ty)) |
| 373 | return false; |
| 374 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | |
| 378 | namespace { |
| 379 | /// DefaultABIInfo - The default implementation for ABI specific |
| 380 | /// details. This implementation provides information which results in |
| 381 | /// self-consistent and sensible LLVM IR generation, but does not |
| 382 | /// conform to any particular ABI. |
| 383 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 384 | public: |
| 385 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 387 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 388 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 389 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 390 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 391 | if (!getCXXABI().classifyReturnType(FI)) |
| 392 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 393 | for (auto &I : FI.arguments()) |
| 394 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 397 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 398 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 399 | }; |
| 400 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 401 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 402 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 403 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 404 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 408 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 409 | return nullptr; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 412 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 413 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 414 | |
| 415 | if (isAggregateTypeForABI(Ty)) { |
| 416 | // Records with non-trivial destructors/copy-constructors should not be |
| 417 | // passed by value. |
| 418 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 419 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 420 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 421 | return ABIArgInfo::getIndirect(0); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 422 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 424 | // Treat an enum type as its underlying type. |
| 425 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 426 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 428 | return (Ty->isPromotableIntegerType() ? |
| 429 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 432 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 433 | if (RetTy->isVoidType()) |
| 434 | return ABIArgInfo::getIgnore(); |
| 435 | |
| 436 | if (isAggregateTypeForABI(RetTy)) |
| 437 | return ABIArgInfo::getIndirect(0); |
| 438 | |
| 439 | // Treat an enum type as its underlying type. |
| 440 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 441 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 442 | |
| 443 | return (RetTy->isPromotableIntegerType() ? |
| 444 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 445 | } |
| 446 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 447 | //===----------------------------------------------------------------------===// |
| 448 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 449 | // |
| 450 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 451 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// |
| 453 | |
| 454 | class PNaClABIInfo : public ABIInfo { |
| 455 | public: |
| 456 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 457 | |
| 458 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 459 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 460 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 461 | void computeInfo(CGFunctionInfo &FI) const override; |
| 462 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 463 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 464 | }; |
| 465 | |
| 466 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 467 | public: |
| 468 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 469 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 470 | }; |
| 471 | |
| 472 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 473 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 474 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 475 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 476 | for (auto &I : FI.arguments()) |
| 477 | I.info = classifyArgumentType(I.type); |
| 478 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 479 | |
| 480 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 481 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 482 | return nullptr; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 485 | /// \brief Classify argument of given type \p Ty. |
| 486 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 487 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 488 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 489 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 490 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 491 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 492 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 493 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 494 | } else if (Ty->isFloatingType()) { |
| 495 | // Floating-point types don't go inreg. |
| 496 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 497 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 498 | |
| 499 | return (Ty->isPromotableIntegerType() ? |
| 500 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 504 | if (RetTy->isVoidType()) |
| 505 | return ABIArgInfo::getIgnore(); |
| 506 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 507 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 508 | if (isAggregateTypeForABI(RetTy)) |
| 509 | return ABIArgInfo::getIndirect(0); |
| 510 | |
| 511 | // Treat an enum type as its underlying type. |
| 512 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 513 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 514 | |
| 515 | return (RetTy->isPromotableIntegerType() ? |
| 516 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 517 | } |
| 518 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 519 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 520 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 521 | // 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] | 522 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 523 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 524 | IRType->getScalarSizeInBits() != 64; |
| 525 | } |
| 526 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 527 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 528 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 529 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 530 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 531 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 532 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 533 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 536 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 540 | return Ty; |
| 541 | } |
| 542 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 543 | /// Returns true if this type can be passed in SSE registers with the |
| 544 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 545 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 546 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 547 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 548 | return true; |
| 549 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 550 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 551 | // registers specially. |
| 552 | unsigned VecSize = Context.getTypeSize(VT); |
| 553 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 554 | return true; |
| 555 | } |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 560 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 561 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 562 | return NumMembers <= 4; |
| 563 | } |
| 564 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 565 | //===----------------------------------------------------------------------===// |
| 566 | // X86-32 ABI Implementation |
| 567 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 568 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 569 | /// \brief Similar to llvm::CCState, but for Clang. |
| 570 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 571 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 572 | |
| 573 | unsigned CC; |
| 574 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 575 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 576 | }; |
| 577 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 578 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 579 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 580 | enum Class { |
| 581 | Integer, |
| 582 | Float |
| 583 | }; |
| 584 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 585 | static const unsigned MinABIStackAlignInBytes = 4; |
| 586 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 587 | bool IsDarwinVectorABI; |
| 588 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 589 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 590 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 591 | |
| 592 | static bool isRegisterSize(unsigned Size) { |
| 593 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 594 | } |
| 595 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 596 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 597 | // FIXME: Assumes vectorcall is in use. |
| 598 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 599 | } |
| 600 | |
| 601 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 602 | uint64_t NumMembers) const override { |
| 603 | // FIXME: Assumes vectorcall is in use. |
| 604 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 605 | } |
| 606 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 607 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 608 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 609 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 610 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 611 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 612 | |
| 613 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 614 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 615 | /// \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] | 616 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 617 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 618 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 619 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 620 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 621 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 622 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 623 | /// \brief Rewrite the function info so that all memory arguments use |
| 624 | /// inalloca. |
| 625 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 626 | |
| 627 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 628 | unsigned &StackOffset, ABIArgInfo &Info, |
| 629 | QualType Type) const; |
| 630 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 631 | public: |
| 632 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 633 | void computeInfo(CGFunctionInfo &FI) const override; |
| 634 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 635 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 636 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 637 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 638 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 639 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 640 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 641 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 642 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 643 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 644 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 645 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 646 | bool d, bool p, bool w, unsigned r) |
| 647 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 648 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 649 | static bool isStructReturnInRegABI( |
| 650 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 651 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 652 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 653 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 654 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 655 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 656 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 657 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 658 | return 4; |
| 659 | } |
| 660 | |
| 661 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 662 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 663 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 664 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 665 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 666 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 667 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 668 | } |
| 669 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 670 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 671 | std::string &Constraints, |
| 672 | std::vector<llvm::Type *> &ResultRegTypes, |
| 673 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 674 | std::vector<LValue> &ResultRegDests, |
| 675 | std::string &AsmString, |
| 676 | unsigned NumOutputs) const override; |
| 677 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 678 | llvm::Constant * |
| 679 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 680 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 681 | (0x06 << 8) | // .+0x08 |
| 682 | ('F' << 16) | |
| 683 | ('T' << 24); |
| 684 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 685 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 686 | }; |
| 687 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 688 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 689 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 690 | /// Rewrite input constraint references after adding some output constraints. |
| 691 | /// In the case where there is one output and one input and we add one output, |
| 692 | /// we need to replace all operand references greater than or equal to 1: |
| 693 | /// mov $0, $1 |
| 694 | /// mov eax, $1 |
| 695 | /// The result will be: |
| 696 | /// mov $0, $2 |
| 697 | /// mov eax, $2 |
| 698 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 699 | unsigned NumNewOuts, |
| 700 | std::string &AsmString) { |
| 701 | std::string Buf; |
| 702 | llvm::raw_string_ostream OS(Buf); |
| 703 | size_t Pos = 0; |
| 704 | while (Pos < AsmString.size()) { |
| 705 | size_t DollarStart = AsmString.find('$', Pos); |
| 706 | if (DollarStart == std::string::npos) |
| 707 | DollarStart = AsmString.size(); |
| 708 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 709 | if (DollarEnd == std::string::npos) |
| 710 | DollarEnd = AsmString.size(); |
| 711 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 712 | Pos = DollarEnd; |
| 713 | size_t NumDollars = DollarEnd - DollarStart; |
| 714 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 715 | // We have an operand reference. |
| 716 | size_t DigitStart = Pos; |
| 717 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 718 | if (DigitEnd == std::string::npos) |
| 719 | DigitEnd = AsmString.size(); |
| 720 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 721 | unsigned OperandIndex; |
| 722 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 723 | if (OperandIndex >= FirstIn) |
| 724 | OperandIndex += NumNewOuts; |
| 725 | OS << OperandIndex; |
| 726 | } else { |
| 727 | OS << OperandStr; |
| 728 | } |
| 729 | Pos = DigitEnd; |
| 730 | } |
| 731 | } |
| 732 | AsmString = std::move(OS.str()); |
| 733 | } |
| 734 | |
| 735 | /// Add output constraints for EAX:EDX because they are return registers. |
| 736 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 737 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 738 | std::vector<llvm::Type *> &ResultRegTypes, |
| 739 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 740 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 741 | unsigned NumOutputs) const { |
| 742 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 743 | |
| 744 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 745 | // larger. |
| 746 | if (!Constraints.empty()) |
| 747 | Constraints += ','; |
| 748 | if (RetWidth <= 32) { |
| 749 | Constraints += "={eax}"; |
| 750 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 751 | } else { |
| 752 | // Use the 'A' constraint for EAX:EDX. |
| 753 | Constraints += "=A"; |
| 754 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 755 | } |
| 756 | |
| 757 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 758 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 759 | ResultTruncRegTypes.push_back(CoerceTy); |
| 760 | |
| 761 | // Coerce the integer by bitcasting the return slot pointer. |
| 762 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 763 | CoerceTy->getPointerTo())); |
| 764 | ResultRegDests.push_back(ReturnSlot); |
| 765 | |
| 766 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 767 | } |
| 768 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 769 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 770 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 771 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 772 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 773 | uint64_t Size = Context.getTypeSize(Ty); |
| 774 | |
| 775 | // Type must be register sized. |
| 776 | if (!isRegisterSize(Size)) |
| 777 | return false; |
| 778 | |
| 779 | if (Ty->isVectorType()) { |
| 780 | // 64- and 128- bit vectors inside structures are not returned in |
| 781 | // registers. |
| 782 | if (Size == 64 || Size == 128) |
| 783 | return false; |
| 784 | |
| 785 | return true; |
| 786 | } |
| 787 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 788 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 789 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 790 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 791 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 792 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 793 | return true; |
| 794 | |
| 795 | // Arrays are treated like records. |
| 796 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 797 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 798 | |
| 799 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 800 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 801 | if (!RT) return false; |
| 802 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 803 | // FIXME: Traverse bases here too. |
| 804 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 805 | // Structure types are passed in register if all fields would be |
| 806 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 807 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 808 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 809 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 810 | continue; |
| 811 | |
| 812 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 813 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 814 | return false; |
| 815 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 816 | return true; |
| 817 | } |
| 818 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 819 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 820 | // If the return value is indirect, then the hidden argument is consuming one |
| 821 | // integer register. |
| 822 | if (State.FreeRegs) { |
| 823 | --State.FreeRegs; |
| 824 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 825 | } |
| 826 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 827 | } |
| 828 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 829 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 830 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 831 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 832 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 833 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 834 | const Type *Base = nullptr; |
| 835 | uint64_t NumElts = 0; |
| 836 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 837 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 838 | // The LLVM struct type for such an aggregate should lower properly. |
| 839 | return ABIArgInfo::getDirect(); |
| 840 | } |
| 841 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 842 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 843 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 844 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 845 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 846 | |
| 847 | // 128-bit vectors are a special case; they are returned in |
| 848 | // registers and we need to make sure to pick a type the LLVM |
| 849 | // backend will like. |
| 850 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 851 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 852 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 853 | |
| 854 | // Always return in register if it fits in a general purpose |
| 855 | // register, or if it is 64 bits and has a single element. |
| 856 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 857 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 858 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 859 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 860 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 861 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 865 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 866 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 867 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 868 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 869 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 870 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 871 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 872 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 873 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 874 | // If specified, structs and unions are always indirect. |
| 875 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 876 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 877 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 878 | // Small structures which are register sized are generally returned |
| 879 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 880 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 881 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 882 | |
| 883 | // As a special-case, if the struct is a "single-element" struct, and |
| 884 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 885 | // floating-point register. (MSVC does not apply this special case.) |
| 886 | // We apply a similar transformation for pointer types to improve the |
| 887 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 888 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 889 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 890 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 891 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 892 | |
| 893 | // FIXME: We should be able to narrow this integer in cases with dead |
| 894 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 895 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 898 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 899 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 901 | // Treat an enum type as its underlying type. |
| 902 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 903 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 904 | |
| 905 | return (RetTy->isPromotableIntegerType() ? |
| 906 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 909 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 910 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 911 | } |
| 912 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 913 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 914 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 915 | if (!RT) |
| 916 | return 0; |
| 917 | const RecordDecl *RD = RT->getDecl(); |
| 918 | |
| 919 | // If this is a C++ record, check the bases first. |
| 920 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 921 | for (const auto &I : CXXRD->bases()) |
| 922 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 923 | return false; |
| 924 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 925 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 926 | QualType FT = i->getType(); |
| 927 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 928 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 929 | return true; |
| 930 | |
| 931 | if (isRecordWithSSEVectorType(Context, FT)) |
| 932 | return true; |
| 933 | } |
| 934 | |
| 935 | return false; |
| 936 | } |
| 937 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 938 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 939 | unsigned Align) const { |
| 940 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 941 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 942 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 943 | return 0; // Use default alignment. |
| 944 | |
| 945 | // On non-Darwin, the stack type alignment is always 4. |
| 946 | if (!IsDarwinVectorABI) { |
| 947 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 948 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 949 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 950 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 951 | // 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] | 952 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 953 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 954 | return 16; |
| 955 | |
| 956 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 959 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 960 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 961 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 962 | if (State.FreeRegs) { |
| 963 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 964 | return ABIArgInfo::getIndirectInReg(0, false); |
| 965 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 966 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 967 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 968 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 969 | // Compute the byval alignment. |
| 970 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 971 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 972 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 973 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 974 | |
| 975 | // If the stack alignment is less than the type alignment, realign the |
| 976 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 977 | bool Realign = TypeAlign > StackAlign; |
| 978 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 981 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 982 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 983 | if (!T) |
| 984 | T = Ty.getTypePtr(); |
| 985 | |
| 986 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 987 | BuiltinType::Kind K = BT->getKind(); |
| 988 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 989 | return Float; |
| 990 | } |
| 991 | return Integer; |
| 992 | } |
| 993 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 994 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 995 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 996 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 997 | Class C = classify(Ty); |
| 998 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 999 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1000 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1001 | unsigned Size = getContext().getTypeSize(Ty); |
| 1002 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1003 | |
| 1004 | if (SizeInRegs == 0) |
| 1005 | return false; |
| 1006 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1007 | if (SizeInRegs > State.FreeRegs) { |
| 1008 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1009 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1010 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1011 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1012 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1013 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1014 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1015 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1016 | if (Size > 32) |
| 1017 | return false; |
| 1018 | |
| 1019 | if (Ty->isIntegralOrEnumerationType()) |
| 1020 | return true; |
| 1021 | |
| 1022 | if (Ty->isPointerType()) |
| 1023 | return true; |
| 1024 | |
| 1025 | if (Ty->isReferenceType()) |
| 1026 | return true; |
| 1027 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1028 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1029 | NeedsPadding = true; |
| 1030 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1031 | return false; |
| 1032 | } |
| 1033 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1034 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1037 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1038 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1039 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1040 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1041 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1042 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1043 | // Check with the C++ ABI first. |
| 1044 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1045 | if (RT) { |
| 1046 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1047 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1048 | return getIndirectResult(Ty, false, State); |
| 1049 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1050 | // The field index doesn't matter, we'll fix it up later. |
| 1051 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1056 | // to other targets. |
| 1057 | const Type *Base = nullptr; |
| 1058 | uint64_t NumElts = 0; |
| 1059 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1060 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1061 | if (State.FreeSSERegs >= NumElts) { |
| 1062 | State.FreeSSERegs -= NumElts; |
| 1063 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1064 | return ABIArgInfo::getDirect(); |
| 1065 | return ABIArgInfo::getExpand(); |
| 1066 | } |
| 1067 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1068 | } |
| 1069 | |
| 1070 | if (isAggregateTypeForABI(Ty)) { |
| 1071 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1072 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1073 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1074 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1075 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1076 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1077 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1078 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1079 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1080 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1081 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1082 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1083 | return ABIArgInfo::getIgnore(); |
| 1084 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1085 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1086 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 1087 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1088 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1089 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1090 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1091 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 1092 | return ABIArgInfo::getDirectInReg(Result); |
| 1093 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1094 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1095 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1096 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1097 | // of those arguments will match the struct. This is important because the |
| 1098 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1099 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1100 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 1101 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1102 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1103 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1104 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1105 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1106 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1107 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1110 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1111 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1112 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1113 | if (IsDarwinVectorABI) { |
| 1114 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1115 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1116 | (Size == 64 && VT->getNumElements() == 1)) |
| 1117 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1118 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1119 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1120 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1121 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1122 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1124 | return ABIArgInfo::getDirect(); |
| 1125 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1126 | |
| 1127 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1128 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1129 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1130 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1131 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1132 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1133 | |
| 1134 | if (Ty->isPromotableIntegerType()) { |
| 1135 | if (InReg) |
| 1136 | return ABIArgInfo::getExtendInReg(); |
| 1137 | return ABIArgInfo::getExtend(); |
| 1138 | } |
| 1139 | if (InReg) |
| 1140 | return ABIArgInfo::getDirectInReg(); |
| 1141 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1144 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1145 | CCState State(FI.getCallingConvention()); |
| 1146 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1147 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1148 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1149 | State.FreeRegs = 2; |
| 1150 | State.FreeSSERegs = 6; |
| 1151 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1152 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1153 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1154 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1155 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1156 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1157 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1158 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1159 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1160 | // return value was sret and put it in a register ourselves if appropriate. |
| 1161 | if (State.FreeRegs) { |
| 1162 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1163 | FI.getReturnInfo().setInReg(true); |
| 1164 | } |
| 1165 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1166 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1167 | // The chain argument effectively gives us another free register. |
| 1168 | if (FI.isChainCall()) |
| 1169 | ++State.FreeRegs; |
| 1170 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1171 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1172 | for (auto &I : FI.arguments()) { |
| 1173 | I.info = classifyArgumentType(I.type, State); |
| 1174 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1178 | // all the memory arguments to use inalloca. |
| 1179 | if (UsedInAlloca) |
| 1180 | rewriteWithInAlloca(FI); |
| 1181 | } |
| 1182 | |
| 1183 | void |
| 1184 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1185 | unsigned &StackOffset, |
| 1186 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1187 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 1188 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1189 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1190 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1191 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1192 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1193 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1194 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1195 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1196 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1197 | unsigned NumBytes = StackOffset - OldOffset; |
| 1198 | assert(NumBytes); |
| 1199 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1200 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1201 | FrameFields.push_back(Ty); |
| 1202 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1205 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1206 | // Leave ignored and inreg arguments alone. |
| 1207 | switch (Info.getKind()) { |
| 1208 | case ABIArgInfo::InAlloca: |
| 1209 | return true; |
| 1210 | case ABIArgInfo::Indirect: |
| 1211 | assert(Info.getIndirectByVal()); |
| 1212 | return true; |
| 1213 | case ABIArgInfo::Ignore: |
| 1214 | return false; |
| 1215 | case ABIArgInfo::Direct: |
| 1216 | case ABIArgInfo::Extend: |
| 1217 | case ABIArgInfo::Expand: |
| 1218 | if (Info.getInReg()) |
| 1219 | return false; |
| 1220 | return true; |
| 1221 | } |
| 1222 | llvm_unreachable("invalid enum"); |
| 1223 | } |
| 1224 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1225 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1226 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1227 | |
| 1228 | // Build a packed struct type for all of the arguments in memory. |
| 1229 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1230 | |
| 1231 | unsigned StackOffset = 0; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1232 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1233 | |
| 1234 | // Put 'this' into the struct before 'sret', if necessary. |
| 1235 | bool IsThisCall = |
| 1236 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1237 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1238 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1239 | isArgInAlloca(I->info)) { |
| 1240 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1241 | ++I; |
| 1242 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1243 | |
| 1244 | // 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] | 1245 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1246 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1247 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1248 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1249 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1253 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1254 | ++I; |
| 1255 | |
| 1256 | // Put arguments passed in memory into the struct. |
| 1257 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1258 | if (isArgInAlloca(I->info)) |
| 1259 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1263 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1266 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1267 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1268 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1269 | |
| 1270 | CGBuilderTy &Builder = CGF.Builder; |
| 1271 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1272 | "ap"); |
| 1273 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Compute if the address needs to be aligned |
| 1276 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1277 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1278 | Align = std::max(Align, 4U); |
| 1279 | if (Align > 4) { |
| 1280 | // addr = (addr + align - 1) & -align; |
| 1281 | llvm::Value *Offset = |
| 1282 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1283 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1284 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1285 | CGF.Int32Ty); |
| 1286 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1287 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1288 | Addr->getType(), |
| 1289 | "ap.cur.aligned"); |
| 1290 | } |
| 1291 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1292 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1293 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1294 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1295 | |
| 1296 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1297 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1298 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1299 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1300 | "ap.next"); |
| 1301 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1302 | |
| 1303 | return AddrTyped; |
| 1304 | } |
| 1305 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1306 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1307 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1308 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1309 | |
| 1310 | switch (Opts.getStructReturnConvention()) { |
| 1311 | case CodeGenOptions::SRCK_Default: |
| 1312 | break; |
| 1313 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1314 | return false; |
| 1315 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1316 | return true; |
| 1317 | } |
| 1318 | |
| 1319 | if (Triple.isOSDarwin()) |
| 1320 | return true; |
| 1321 | |
| 1322 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1323 | case llvm::Triple::DragonFly: |
| 1324 | case llvm::Triple::FreeBSD: |
| 1325 | case llvm::Triple::OpenBSD: |
| 1326 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1327 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1328 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1329 | default: |
| 1330 | return false; |
| 1331 | } |
| 1332 | } |
| 1333 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1334 | void X86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1335 | llvm::GlobalValue *GV, |
| 1336 | CodeGen::CodeGenModule &CGM) const { |
| 1337 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1338 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1339 | // Get the LLVM function. |
| 1340 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1341 | |
| 1342 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1343 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1344 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1345 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1346 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1347 | llvm::AttributeSet::FunctionIndex, |
| 1348 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1353 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1354 | CodeGen::CodeGenFunction &CGF, |
| 1355 | llvm::Value *Address) const { |
| 1356 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1357 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1358 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1359 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1360 | // 0-7 are the eight integer registers; the order is different |
| 1361 | // on Darwin (for EH), but the range is the same. |
| 1362 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1363 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1364 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1365 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1366 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1367 | // These have size 16, which is sizeof(long double) on |
| 1368 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1369 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1370 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1371 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1372 | } else { |
| 1373 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1374 | // reason. |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1375 | Builder.CreateStore( |
| 1376 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9)); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1377 | |
| 1378 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1379 | // These have size 12, which is sizeof(long double) on |
| 1380 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1381 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1382 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1383 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1384 | |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1388 | //===----------------------------------------------------------------------===// |
| 1389 | // X86-64 ABI Implementation |
| 1390 | //===----------------------------------------------------------------------===// |
| 1391 | |
| 1392 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1393 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1394 | /// The AVX ABI level for X86 targets. |
| 1395 | enum class X86AVXABILevel { |
| 1396 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1397 | AVX, |
| 1398 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1399 | }; |
| 1400 | |
| 1401 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1402 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1403 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1404 | case X86AVXABILevel::AVX512: |
| 1405 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1406 | case X86AVXABILevel::AVX: |
| 1407 | return 256; |
| 1408 | case X86AVXABILevel::None: |
| 1409 | return 128; |
| 1410 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 1411 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1414 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1415 | class X86_64ABIInfo : public ABIInfo { |
| 1416 | enum Class { |
| 1417 | Integer = 0, |
| 1418 | SSE, |
| 1419 | SSEUp, |
| 1420 | X87, |
| 1421 | X87Up, |
| 1422 | ComplexX87, |
| 1423 | NoClass, |
| 1424 | Memory |
| 1425 | }; |
| 1426 | |
| 1427 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1428 | /// |
| 1429 | /// Merge an accumulating classification \arg Accum with a field |
| 1430 | /// classification \arg Field. |
| 1431 | /// |
| 1432 | /// \param Accum - The accumulating classification. This should |
| 1433 | /// always be either NoClass or the result of a previous merge |
| 1434 | /// call. In addition, this should never be Memory (the caller |
| 1435 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1436 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1437 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1438 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1439 | /// |
| 1440 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1441 | /// final MEMORY or SSE classes when necessary. |
| 1442 | /// |
| 1443 | /// \param AggregateSize - The size of the current aggregate in |
| 1444 | /// the classification process. |
| 1445 | /// |
| 1446 | /// \param Lo - The classification for the parts of the type |
| 1447 | /// residing in the low word of the containing object. |
| 1448 | /// |
| 1449 | /// \param Hi - The classification for the parts of the type |
| 1450 | /// residing in the higher words of the containing object. |
| 1451 | /// |
| 1452 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1453 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1454 | /// classify - Determine the x86_64 register classes in which the |
| 1455 | /// given type T should be passed. |
| 1456 | /// |
| 1457 | /// \param Lo - The classification for the parts of the type |
| 1458 | /// residing in the low word of the containing object. |
| 1459 | /// |
| 1460 | /// \param Hi - The classification for the parts of the type |
| 1461 | /// residing in the high word of the containing object. |
| 1462 | /// |
| 1463 | /// \param OffsetBase - The bit offset of this type in the |
| 1464 | /// containing object. Some parameters are classified different |
| 1465 | /// depending on whether they straddle an eightbyte boundary. |
| 1466 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1467 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1468 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1469 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1470 | /// If a word is unused its result will be NoClass; if a type should |
| 1471 | /// be passed in Memory then at least the classification of \arg Lo |
| 1472 | /// will be Memory. |
| 1473 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1474 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1475 | /// |
| 1476 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1477 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1478 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1479 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1480 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1481 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1482 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1483 | unsigned IROffset, QualType SourceTy, |
| 1484 | unsigned SourceOffset) const; |
| 1485 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1486 | unsigned IROffset, QualType SourceTy, |
| 1487 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1488 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1489 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1490 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1491 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1492 | |
| 1493 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1494 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1495 | /// |
| 1496 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1497 | /// available. |
| 1498 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1499 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1500 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1501 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1502 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1503 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1504 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1505 | unsigned &neededSSE, |
| 1506 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1507 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1508 | bool IsIllegalVectorType(QualType Ty) const; |
| 1509 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1510 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1511 | /// unfortunately in ways that were not always consistent with |
| 1512 | /// certain previous compilers. In particular, platforms which |
| 1513 | /// required strict binary compatibility with older versions of GCC |
| 1514 | /// may need to exempt themselves. |
| 1515 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1516 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1519 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1520 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1521 | // 64-bit hardware. |
| 1522 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1523 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1524 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1525 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| 1526 | ABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1527 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1528 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1529 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1530 | bool isPassedUsingAVXType(QualType type) const { |
| 1531 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1532 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1533 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1534 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1535 | if (info.isDirect()) { |
| 1536 | llvm::Type *ty = info.getCoerceToType(); |
| 1537 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1538 | return (vectorTy->getBitWidth() > 128); |
| 1539 | } |
| 1540 | return false; |
| 1541 | } |
| 1542 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1543 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1544 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1545 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1546 | CodeGenFunction &CGF) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1547 | |
| 1548 | bool has64BitPointers() const { |
| 1549 | return Has64BitPointers; |
| 1550 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1551 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1553 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1554 | class WinX86_64ABIInfo : public ABIInfo { |
| 1555 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1556 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1557 | bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1558 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1559 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1560 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1561 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1562 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1563 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1564 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1565 | CodeGenFunction &CGF) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1566 | |
| 1567 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1568 | // FIXME: Assumes vectorcall is in use. |
| 1569 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1570 | } |
| 1571 | |
| 1572 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1573 | uint64_t NumMembers) const override { |
| 1574 | // FIXME: Assumes vectorcall is in use. |
| 1575 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1576 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1577 | }; |
| 1578 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1579 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1580 | X86AVXABILevel AVXLevel; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1581 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1582 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1583 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)), |
| 1584 | AVXLevel(AVXLevel) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1585 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1586 | const X86_64ABIInfo &getABIInfo() const { |
| 1587 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1588 | } |
| 1589 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1590 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1591 | return 7; |
| 1592 | } |
| 1593 | |
| 1594 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1595 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1596 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1597 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1598 | // 0-15 are the 16 integer registers. |
| 1599 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1600 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1601 | return false; |
| 1602 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1603 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1604 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1605 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1606 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1607 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1608 | } |
| 1609 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1610 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1611 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1612 | // The default CC on x86-64 sets %al to the number of SSA |
| 1613 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1614 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1615 | // that when AVX types are involved: the ABI explicitly states it is |
| 1616 | // undefined, and it doesn't work in practice because of how the ABI |
| 1617 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1618 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1619 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1620 | for (CallArgList::const_iterator |
| 1621 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1622 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1623 | HasAVXType = true; |
| 1624 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1627 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1628 | if (!HasAVXType) |
| 1629 | return true; |
| 1630 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1631 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1632 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1635 | llvm::Constant * |
| 1636 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1637 | unsigned Sig; |
| 1638 | if (getABIInfo().has64BitPointers()) |
| 1639 | Sig = (0xeb << 0) | // jmp rel8 |
| 1640 | (0x0a << 8) | // .+0x0c |
| 1641 | ('F' << 16) | |
| 1642 | ('T' << 24); |
| 1643 | else |
| 1644 | Sig = (0xeb << 0) | // jmp rel8 |
| 1645 | (0x06 << 8) | // .+0x08 |
| 1646 | ('F' << 16) | |
| 1647 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1648 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1649 | } |
| 1650 | |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1651 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1652 | return getNativeVectorSizeForAVXABI(AVXLevel) / 8; |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1653 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1654 | }; |
| 1655 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1656 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1657 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1658 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1659 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1660 | |
| 1661 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1662 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1663 | Opt = "\01"; |
| 1664 | Opt += Lib; |
| 1665 | } |
| 1666 | }; |
| 1667 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1668 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1669 | // If the argument does not end in .lib, automatically add the suffix. |
| 1670 | // If the argument contains a space, enclose it in quotes. |
| 1671 | // This matches the behavior of MSVC. |
| 1672 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1673 | std::string ArgStr = Quote ? "\"" : ""; |
| 1674 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1675 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1676 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1677 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1678 | return ArgStr; |
| 1679 | } |
| 1680 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1681 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1682 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1683 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1684 | bool d, bool p, bool w, unsigned RegParms) |
| 1685 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1686 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1687 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1688 | CodeGen::CodeGenModule &CGM) const override; |
| 1689 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1690 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1691 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1692 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1693 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1694 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1695 | |
| 1696 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1697 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1698 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1699 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1700 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1701 | }; |
| 1702 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1703 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1704 | llvm::GlobalValue *GV, |
| 1705 | CodeGen::CodeGenModule &CGM) { |
| 1706 | if (isa<FunctionDecl>(D)) { |
| 1707 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1708 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1709 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1710 | Fn->addFnAttr("stack-probe-size", |
| 1711 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | } |
| 1715 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1716 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1717 | llvm::GlobalValue *GV, |
| 1718 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1719 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1720 | |
| 1721 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1722 | } |
| 1723 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1724 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1725 | X86AVXABILevel AVXLevel; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1726 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1727 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1728 | X86AVXABILevel AVXLevel) |
| 1729 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)), AVXLevel(AVXLevel) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1730 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1731 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1732 | CodeGen::CodeGenModule &CGM) const override; |
| 1733 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1734 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1735 | return 7; |
| 1736 | } |
| 1737 | |
| 1738 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1739 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1740 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1741 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1742 | // 0-15 are the 16 integer registers. |
| 1743 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1744 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1745 | return false; |
| 1746 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1747 | |
| 1748 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1749 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1750 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1751 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1752 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1753 | |
| 1754 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1755 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1756 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1757 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1758 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1759 | |
| 1760 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1761 | return getNativeVectorSizeForAVXABI(AVXLevel) / 8; |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1762 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1763 | }; |
| 1764 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1765 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1766 | llvm::GlobalValue *GV, |
| 1767 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1768 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1769 | |
| 1770 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1771 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1772 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1773 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1774 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1775 | Class &Hi) const { |
| 1776 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1777 | // |
| 1778 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1779 | // memory. |
| 1780 | // |
| 1781 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1782 | // memory. |
| 1783 | // |
| 1784 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1785 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1786 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1787 | // ABI working for processors that don't support the __m256 type. |
| 1788 | // |
| 1789 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1790 | // |
| 1791 | // Some of these are enforced by the merging logic. Others can arise |
| 1792 | // only with unions; for example: |
| 1793 | // union { _Complex double; unsigned; } |
| 1794 | // |
| 1795 | // Note that clauses (b) and (c) were added in 0.98. |
| 1796 | // |
| 1797 | if (Hi == Memory) |
| 1798 | Lo = Memory; |
| 1799 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1800 | Lo = Memory; |
| 1801 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1802 | Lo = Memory; |
| 1803 | if (Hi == SSEUp && Lo != SSE) |
| 1804 | Hi = SSE; |
| 1805 | } |
| 1806 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1807 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1808 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1809 | // classified recursively so that always two fields are |
| 1810 | // considered. The resulting class is calculated according to |
| 1811 | // the classes of the fields in the eightbyte: |
| 1812 | // |
| 1813 | // (a) If both classes are equal, this is the resulting class. |
| 1814 | // |
| 1815 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1816 | // the other class. |
| 1817 | // |
| 1818 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1819 | // class. |
| 1820 | // |
| 1821 | // (d) If one of the classes is INTEGER, the result is the |
| 1822 | // INTEGER. |
| 1823 | // |
| 1824 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1825 | // MEMORY is used as class. |
| 1826 | // |
| 1827 | // (f) Otherwise class SSE is used. |
| 1828 | |
| 1829 | // Accum should never be memory (we should have returned) or |
| 1830 | // ComplexX87 (because this cannot be passed in a structure). |
| 1831 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1832 | "Invalid accumulated classification during merge."); |
| 1833 | if (Accum == Field || Field == NoClass) |
| 1834 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1835 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1836 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1837 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1838 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1839 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1840 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1841 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1842 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1843 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1844 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1847 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1848 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1849 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1850 | // Class pairs with appropriate constructor methods for the various |
| 1851 | // situations. |
| 1852 | |
| 1853 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1854 | // shouldn't be passed in registers for example, so there is no chance they |
| 1855 | // can straddle an eightbyte. Verify & simplify. |
| 1856 | |
| 1857 | Lo = Hi = NoClass; |
| 1858 | |
| 1859 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1860 | Current = Memory; |
| 1861 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1862 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1863 | BuiltinType::Kind k = BT->getKind(); |
| 1864 | |
| 1865 | if (k == BuiltinType::Void) { |
| 1866 | Current = NoClass; |
| 1867 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1868 | Lo = Integer; |
| 1869 | Hi = Integer; |
| 1870 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1871 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1872 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1873 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1874 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1875 | Current = SSE; |
| 1876 | } else if (k == BuiltinType::LongDouble) { |
| 1877 | Lo = X87; |
| 1878 | Hi = X87Up; |
| 1879 | } |
| 1880 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1881 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1882 | return; |
| 1883 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1884 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1885 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1886 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1887 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1888 | return; |
| 1889 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1890 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1891 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1892 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1893 | return; |
| 1894 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1895 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1896 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1897 | if (Ty->isMemberFunctionPointerType()) { |
| 1898 | if (Has64BitPointers) { |
| 1899 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 1900 | // Lo and Hi now. |
| 1901 | Lo = Hi = Integer; |
| 1902 | } else { |
| 1903 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 1904 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 1905 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 1906 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 1907 | if (EB_FuncPtr != EB_ThisAdj) { |
| 1908 | Lo = Hi = Integer; |
| 1909 | } else { |
| 1910 | Current = Integer; |
| 1911 | } |
| 1912 | } |
| 1913 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1914 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1915 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1916 | return; |
| 1917 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1919 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1920 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1921 | if (Size == 32) { |
| 1922 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1923 | // float> as integer. |
| 1924 | Current = Integer; |
| 1925 | |
| 1926 | // If this type crosses an eightbyte boundary, it should be |
| 1927 | // split. |
| 1928 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1929 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1930 | if (EB_Real != EB_Imag) |
| 1931 | Hi = Lo; |
| 1932 | } else if (Size == 64) { |
| 1933 | // gcc passes <1 x double> in memory. :( |
| 1934 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1935 | return; |
| 1936 | |
| 1937 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1938 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1939 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1940 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1941 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1942 | Current = Integer; |
| 1943 | else |
| 1944 | Current = SSE; |
| 1945 | |
| 1946 | // If this type crosses an eightbyte boundary, it should be |
| 1947 | // split. |
| 1948 | if (OffsetBase && OffsetBase != 64) |
| 1949 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1950 | } else if (Size == 128 || |
| 1951 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1952 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1953 | // least significant one belongs to class SSE and all the others to class |
| 1954 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1955 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1956 | // This design isn't correct for 256-bits, but since there're no cases |
| 1957 | // where the upper parts would need to be inspected, avoid adding |
| 1958 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1959 | // |
| 1960 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1961 | // registers if they are "named", i.e. not part of the "..." of a |
| 1962 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1963 | // |
| 1964 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 1965 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1966 | Lo = SSE; |
| 1967 | Hi = SSEUp; |
| 1968 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1969 | return; |
| 1970 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1971 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1972 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1973 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1975 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1976 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1977 | if (Size <= 64) |
| 1978 | Current = Integer; |
| 1979 | else if (Size <= 128) |
| 1980 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1981 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1982 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1983 | else if (ET == getContext().DoubleTy || |
| 1984 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1985 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1986 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1987 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1988 | Current = ComplexX87; |
| 1989 | |
| 1990 | // If this complex type crosses an eightbyte boundary then it |
| 1991 | // should be split. |
| 1992 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1993 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1994 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1995 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1997 | return; |
| 1998 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2000 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2001 | // Arrays are treated like structures. |
| 2002 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2003 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2004 | |
| 2005 | // 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] | 2006 | // than four eightbytes, ..., it has class MEMORY. |
| 2007 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2008 | return; |
| 2009 | |
| 2010 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2011 | // fields, it has class MEMORY. |
| 2012 | // |
| 2013 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2014 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2015 | return; |
| 2016 | |
| 2017 | // Otherwise implement simplified merge. We could be smarter about |
| 2018 | // this, but it isn't worth it and would be harder to verify. |
| 2019 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2020 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2021 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2022 | |
| 2023 | // The only case a 256-bit wide vector could be used is when the array |
| 2024 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2025 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2026 | if (Size > 128 && EltSize != 256) |
| 2027 | return; |
| 2028 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2029 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2030 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2031 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2032 | Lo = merge(Lo, FieldLo); |
| 2033 | Hi = merge(Hi, FieldHi); |
| 2034 | if (Lo == Memory || Hi == Memory) |
| 2035 | break; |
| 2036 | } |
| 2037 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2038 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2039 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2040 | return; |
| 2041 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2042 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2043 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2044 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2045 | |
| 2046 | // 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] | 2047 | // than four eightbytes, ..., it has class MEMORY. |
| 2048 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2049 | return; |
| 2050 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2051 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2052 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2053 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2054 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2055 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2056 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2057 | const RecordDecl *RD = RT->getDecl(); |
| 2058 | |
| 2059 | // Assume variable sized types are passed in memory. |
| 2060 | if (RD->hasFlexibleArrayMember()) |
| 2061 | return; |
| 2062 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2063 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2064 | |
| 2065 | // Reset Lo class, this will be recomputed. |
| 2066 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2067 | |
| 2068 | // If this is a C++ record, classify the bases first. |
| 2069 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2070 | for (const auto &I : CXXRD->bases()) { |
| 2071 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2072 | "Unexpected base class!"); |
| 2073 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2074 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2075 | |
| 2076 | // Classify this field. |
| 2077 | // |
| 2078 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2079 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2080 | // initialized to class NO_CLASS. |
| 2081 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2082 | uint64_t Offset = |
| 2083 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2084 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2085 | Lo = merge(Lo, FieldLo); |
| 2086 | Hi = merge(Hi, FieldHi); |
| 2087 | if (Lo == Memory || Hi == Memory) |
| 2088 | break; |
| 2089 | } |
| 2090 | } |
| 2091 | |
| 2092 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2093 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2094 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2095 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2096 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2097 | bool BitField = i->isBitField(); |
| 2098 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2099 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2100 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2101 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2102 | // The only case a 256-bit wide vector could be used is when the struct |
| 2103 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2104 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2105 | // |
| 2106 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2107 | Lo = Memory; |
| 2108 | return; |
| 2109 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2110 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2111 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2112 | Lo = Memory; |
| 2113 | return; |
| 2114 | } |
| 2115 | |
| 2116 | // Classify this field. |
| 2117 | // |
| 2118 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2119 | // exceeds a single eightbyte, each is classified |
| 2120 | // separately. Each eightbyte gets initialized to class |
| 2121 | // NO_CLASS. |
| 2122 | Class FieldLo, FieldHi; |
| 2123 | |
| 2124 | // Bit-fields require special handling, they do not force the |
| 2125 | // structure to be passed in memory even if unaligned, and |
| 2126 | // therefore they can straddle an eightbyte. |
| 2127 | if (BitField) { |
| 2128 | // Ignore padding bit-fields. |
| 2129 | if (i->isUnnamedBitfield()) |
| 2130 | continue; |
| 2131 | |
| 2132 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2133 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2134 | |
| 2135 | uint64_t EB_Lo = Offset / 64; |
| 2136 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2137 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2138 | if (EB_Lo) { |
| 2139 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2140 | FieldLo = NoClass; |
| 2141 | FieldHi = Integer; |
| 2142 | } else { |
| 2143 | FieldLo = Integer; |
| 2144 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2145 | } |
| 2146 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2147 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2148 | Lo = merge(Lo, FieldLo); |
| 2149 | Hi = merge(Hi, FieldHi); |
| 2150 | if (Lo == Memory || Hi == Memory) |
| 2151 | break; |
| 2152 | } |
| 2153 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2154 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2158 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2159 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2160 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2161 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2162 | // Treat an enum type as its underlying type. |
| 2163 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2164 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2165 | |
| 2166 | return (Ty->isPromotableIntegerType() ? |
| 2167 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2168 | } |
| 2169 | |
| 2170 | return ABIArgInfo::getIndirect(0); |
| 2171 | } |
| 2172 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2173 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2174 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2175 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2176 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2177 | if (Size <= 64 || Size > LargestVector) |
| 2178 | return true; |
| 2179 | } |
| 2180 | |
| 2181 | return false; |
| 2182 | } |
| 2183 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2184 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2185 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2186 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2187 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2188 | // |
| 2189 | // This assumption is optimistic, as there could be free registers available |
| 2190 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2191 | // the argument in the free register. This does not seem to happen currently, |
| 2192 | // but this code would be much safer if we could mark the argument with |
| 2193 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2194 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2195 | // Treat an enum type as its underlying type. |
| 2196 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2197 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2198 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2199 | return (Ty->isPromotableIntegerType() ? |
| 2200 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2201 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2202 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2203 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2204 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2205 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2206 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2207 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2208 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2209 | |
| 2210 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2211 | // is important for good codegen. |
| 2212 | // |
| 2213 | // We do this by coercing the value into a scalar type which the backend can |
| 2214 | // handle naturally (i.e., without using byval). |
| 2215 | // |
| 2216 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2217 | // free integer registers. Doing this when there are free integer registers |
| 2218 | // would require more care, as we would have to ensure that the coerced value |
| 2219 | // did not claim the unused register. That would require either reording the |
| 2220 | // arguments to the function (so that any subsequent inreg values came first), |
| 2221 | // or only doing this optimization when there were no following arguments that |
| 2222 | // might be inreg. |
| 2223 | // |
| 2224 | // We currently expect it to be rare (particularly in well written code) for |
| 2225 | // arguments to be passed on the stack when there are still free integer |
| 2226 | // registers available (this would typically imply large structs being passed |
| 2227 | // by value), so this seems like a fair tradeoff for now. |
| 2228 | // |
| 2229 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2230 | // attributes. See PR12193. |
| 2231 | if (freeIntRegs == 0) { |
| 2232 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2233 | |
| 2234 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2235 | // type, which will end up on the stack (with alignment 8). |
| 2236 | if (Align == 8 && Size <= 64) |
| 2237 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2238 | Size)); |
| 2239 | } |
| 2240 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2241 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2244 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2245 | /// 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] | 2246 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2247 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2248 | // vectors; strip them off if present. |
| 2249 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2250 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2251 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2252 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2253 | if(isa<llvm::VectorType>(IRType)) |
| 2254 | return IRType; |
| 2255 | |
| 2256 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2257 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2258 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2259 | |
| 2260 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2261 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2262 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2265 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2266 | /// is known to either be off the end of the specified type or being in |
| 2267 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2268 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2269 | /// classification that put one of the two halves in the INTEGER class. |
| 2270 | /// |
| 2271 | /// It is conservatively correct to return false. |
| 2272 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2273 | unsigned EndBit, ASTContext &Context) { |
| 2274 | // If the bytes being queried are off the end of the type, there is no user |
| 2275 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2276 | // types that don't contain interesting padding. |
| 2277 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2278 | if (TySize <= StartBit) |
| 2279 | return true; |
| 2280 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2281 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2282 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2283 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2284 | |
| 2285 | // Check each element to see if the element overlaps with the queried range. |
| 2286 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2287 | // If the element is after the span we care about, then we're done.. |
| 2288 | unsigned EltOffset = i*EltSize; |
| 2289 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2291 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2292 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2293 | EndBit-EltOffset, Context)) |
| 2294 | return false; |
| 2295 | } |
| 2296 | // If it overlaps no elements, then it is safe to process as padding. |
| 2297 | return true; |
| 2298 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2299 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2300 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2301 | const RecordDecl *RD = RT->getDecl(); |
| 2302 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2303 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2304 | // If this is a C++ record, check the bases first. |
| 2305 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2306 | for (const auto &I : CXXRD->bases()) { |
| 2307 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2308 | "Unexpected base class!"); |
| 2309 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2310 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2311 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2312 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2313 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2314 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2316 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2317 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2318 | EndBit-BaseOffset, Context)) |
| 2319 | return false; |
| 2320 | } |
| 2321 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2322 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2323 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2324 | // this could be sped up a lot by being smarter about queried fields, |
| 2325 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2326 | // much. |
| 2327 | unsigned idx = 0; |
| 2328 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2329 | i != e; ++i, ++idx) { |
| 2330 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2331 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2332 | // If we found a field after the region we care about, then we're done. |
| 2333 | if (FieldOffset >= EndBit) break; |
| 2334 | |
| 2335 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2336 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2337 | Context)) |
| 2338 | return false; |
| 2339 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2340 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2341 | // If nothing in this record overlapped the area of interest, then we're |
| 2342 | // clean. |
| 2343 | return true; |
| 2344 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2345 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2346 | return false; |
| 2347 | } |
| 2348 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2349 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2350 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2351 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2352 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2353 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2354 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2355 | // Base case if we find a float. |
| 2356 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2357 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2358 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2359 | // 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] | 2360 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2361 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2362 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2363 | IROffset -= SL->getElementOffset(Elt); |
| 2364 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2365 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2366 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2367 | // 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] | 2368 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2369 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2370 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2371 | IROffset -= IROffset/EltSize*EltSize; |
| 2372 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2373 | } |
| 2374 | |
| 2375 | return false; |
| 2376 | } |
| 2377 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2378 | |
| 2379 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2380 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2381 | llvm::Type *X86_64ABIInfo:: |
| 2382 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2383 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2384 | // 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] | 2385 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2386 | // structs that contain 3 floats. |
| 2387 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2388 | SourceOffset*8+64, getContext())) |
| 2389 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2390 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2391 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2392 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2393 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2394 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2395 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2396 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2397 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2398 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2399 | } |
| 2400 | |
| 2401 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2402 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2403 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2404 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2405 | /// 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] | 2406 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2407 | /// etc). |
| 2408 | /// |
| 2409 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2410 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2411 | /// the 8-byte value references. PrefType may be null. |
| 2412 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2413 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2414 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2415 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2416 | llvm::Type *X86_64ABIInfo:: |
| 2417 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2418 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2419 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2420 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2421 | if (IROffset == 0) { |
| 2422 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2423 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2424 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2425 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2426 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2427 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2428 | // goodness in the source type is just tail padding. This is allowed to |
| 2429 | // kick in for struct {double,int} on the int, but not on |
| 2430 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2431 | // have to do this analysis on the source type because we can't depend on |
| 2432 | // unions being lowered a specific way etc. |
| 2433 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2434 | IRType->isIntegerTy(32) || |
| 2435 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2436 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2437 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2438 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2439 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2440 | SourceOffset*8+64, getContext())) |
| 2441 | return IRType; |
| 2442 | } |
| 2443 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2444 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2445 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2446 | // 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] | 2447 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2448 | if (IROffset < SL->getSizeInBytes()) { |
| 2449 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2450 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2451 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2452 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2453 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2454 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2455 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2456 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2457 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2458 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2459 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2460 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2461 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2462 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2463 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2464 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2465 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2466 | // 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] | 2467 | unsigned TySizeInBytes = |
| 2468 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2470 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2471 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2472 | // It is always safe to classify this as an integer type up to i64 that |
| 2473 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2474 | return llvm::IntegerType::get(getVMContext(), |
| 2475 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2476 | } |
| 2477 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2478 | |
| 2479 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2480 | /// be used as elements of a two register pair to pass or return, return a |
| 2481 | /// first class aggregate to represent them. For example, if the low part of |
| 2482 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2483 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2484 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2485 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2486 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2487 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2488 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2489 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2490 | // the second element at offset 8. Check for this: |
| 2491 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2492 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2493 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2494 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2495 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2496 | // To handle this, we have to increase the size of the low part so that the |
| 2497 | // second element will start at an 8 byte offset. We can't increase the size |
| 2498 | // of the second element because it might make us access off the end of the |
| 2499 | // struct. |
| 2500 | if (HiStart != 8) { |
| 2501 | // There are only two sorts of types the ABI generation code can produce for |
| 2502 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2503 | // Promote these to a larger type. |
| 2504 | if (Lo->isFloatTy()) |
| 2505 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2506 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame^] | 2507 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 2508 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2509 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2510 | } |
| 2511 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2512 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2513 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2514 | |
| 2515 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2516 | // Verify that the second element is at an 8-byte offset. |
| 2517 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2518 | "Invalid x86-64 argument pair!"); |
| 2519 | return Result; |
| 2520 | } |
| 2521 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2522 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2523 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2524 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2525 | // classification algorithm. |
| 2526 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2527 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2528 | |
| 2529 | // Check some invariants. |
| 2530 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2531 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2532 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2533 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2534 | switch (Lo) { |
| 2535 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2536 | if (Hi == NoClass) |
| 2537 | return ABIArgInfo::getIgnore(); |
| 2538 | // If the low part is just padding, it takes no register, leave ResType |
| 2539 | // null. |
| 2540 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2541 | "Unknown missing lo part"); |
| 2542 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2543 | |
| 2544 | case SSEUp: |
| 2545 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2546 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2547 | |
| 2548 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2549 | // hidden argument. |
| 2550 | case Memory: |
| 2551 | return getIndirectReturnResult(RetTy); |
| 2552 | |
| 2553 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2554 | // available register of the sequence %rax, %rdx is used. |
| 2555 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2556 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2558 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2559 | // so that the parameter gets the right LLVM IR attributes. |
| 2560 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2561 | // Treat an enum type as its underlying type. |
| 2562 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2563 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2564 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2565 | if (RetTy->isIntegralOrEnumerationType() && |
| 2566 | RetTy->isPromotableIntegerType()) |
| 2567 | return ABIArgInfo::getExtend(); |
| 2568 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2569 | break; |
| 2570 | |
| 2571 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2572 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2573 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2574 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2575 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2576 | |
| 2577 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2578 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2579 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2580 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2581 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2582 | |
| 2583 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2584 | // part of the value is returned in %st0 and the imaginary part in |
| 2585 | // %st1. |
| 2586 | case ComplexX87: |
| 2587 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2588 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2589 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2590 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | } |
| 2593 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2594 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2595 | switch (Hi) { |
| 2596 | // Memory was handled previously and X87 should |
| 2597 | // never occur as a hi class. |
| 2598 | case Memory: |
| 2599 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2600 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2601 | |
| 2602 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2603 | case NoClass: |
| 2604 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2606 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2607 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2608 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2609 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2610 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2611 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2612 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2613 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2614 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2615 | break; |
| 2616 | |
| 2617 | // 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] | 2618 | // is passed in the next available eightbyte chunk if the last used |
| 2619 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2620 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2621 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2622 | case SSEUp: |
| 2623 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2624 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2625 | break; |
| 2626 | |
| 2627 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2628 | // returned together with the previous X87 value in %st0. |
| 2629 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2630 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2631 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2632 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2633 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2634 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2635 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2636 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2637 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2638 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2639 | break; |
| 2640 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2641 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2642 | // 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] | 2643 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2644 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2645 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2646 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2648 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2651 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2652 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2653 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2654 | const |
| 2655 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2656 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2657 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2658 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2659 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2660 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2661 | // Check some invariants. |
| 2662 | // FIXME: Enforce these by construction. |
| 2663 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2664 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2665 | |
| 2666 | neededInt = 0; |
| 2667 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2668 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2669 | switch (Lo) { |
| 2670 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2671 | if (Hi == NoClass) |
| 2672 | return ABIArgInfo::getIgnore(); |
| 2673 | // If the low part is just padding, it takes no register, leave ResType |
| 2674 | // null. |
| 2675 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2676 | "Unknown missing lo part"); |
| 2677 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2678 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2679 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2680 | // on the stack. |
| 2681 | case Memory: |
| 2682 | |
| 2683 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2684 | // COMPLEX_X87, it is passed in memory. |
| 2685 | case X87: |
| 2686 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2687 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2688 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2689 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2690 | |
| 2691 | case SSEUp: |
| 2692 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2693 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2694 | |
| 2695 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2696 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2697 | // and %r9 is used. |
| 2698 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2699 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2700 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2701 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2702 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2703 | |
| 2704 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2705 | // so that the parameter gets the right LLVM IR attributes. |
| 2706 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2707 | // Treat an enum type as its underlying type. |
| 2708 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2709 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2710 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2711 | if (Ty->isIntegralOrEnumerationType() && |
| 2712 | Ty->isPromotableIntegerType()) |
| 2713 | return ABIArgInfo::getExtend(); |
| 2714 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2715 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2716 | break; |
| 2717 | |
| 2718 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2719 | // available SSE register is used, the registers are taken in the |
| 2720 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2721 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2722 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2723 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2724 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2725 | break; |
| 2726 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2727 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2728 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2729 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2730 | switch (Hi) { |
| 2731 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2732 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2733 | // which is passed in memory. |
| 2734 | case Memory: |
| 2735 | case X87: |
| 2736 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2737 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2738 | |
| 2739 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2740 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2741 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2742 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2743 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2744 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2745 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2746 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2747 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2748 | break; |
| 2749 | |
| 2750 | // X87Up generally doesn't occur here (long double is passed in |
| 2751 | // memory), except in situations involving unions. |
| 2752 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2753 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2754 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2756 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2757 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2758 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2759 | ++neededSSE; |
| 2760 | break; |
| 2761 | |
| 2762 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2763 | // 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] | 2764 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2765 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2766 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2767 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2768 | break; |
| 2769 | } |
| 2770 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2771 | // If a high part was specified, merge it together with the low part. It is |
| 2772 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2773 | // first class struct aggregate with the high and low part: {low, high} |
| 2774 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2775 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2776 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2777 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2780 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2781 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2782 | if (!getCXXABI().classifyReturnType(FI)) |
| 2783 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2784 | |
| 2785 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2786 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2787 | |
| 2788 | // If the return value is indirect, then the hidden argument is consuming one |
| 2789 | // integer register. |
| 2790 | if (FI.getReturnInfo().isIndirect()) |
| 2791 | --freeIntRegs; |
| 2792 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 2793 | // The chain argument effectively gives us another free register. |
| 2794 | if (FI.isChainCall()) |
| 2795 | ++freeIntRegs; |
| 2796 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2797 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2798 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2799 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2800 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2801 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2802 | it != ie; ++it, ++ArgNo) { |
| 2803 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2804 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2805 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2806 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2807 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2808 | |
| 2809 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2810 | // eightbyte of an argument, the whole argument is passed on the |
| 2811 | // stack. If registers have already been assigned for some |
| 2812 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2813 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2814 | freeIntRegs -= neededInt; |
| 2815 | freeSSERegs -= neededSSE; |
| 2816 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2817 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | } |
| 2821 | |
| 2822 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2823 | QualType Ty, |
| 2824 | CodeGenFunction &CGF) { |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 2825 | llvm::Value *overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 2826 | nullptr, VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2827 | llvm::Value *overflow_arg_area = |
| 2828 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2829 | |
| 2830 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2831 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2832 | // It isn't stated explicitly in the standard, but in practice we use |
| 2833 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2834 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2835 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2836 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2837 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2838 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2839 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2840 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2841 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2842 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2843 | overflow_arg_area = |
| 2844 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2845 | overflow_arg_area->getType(), |
| 2846 | "overflow_arg_area.align"); |
| 2847 | } |
| 2848 | |
| 2849 | // 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] | 2850 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2851 | llvm::Value *Res = |
| 2852 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2853 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2854 | |
| 2855 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2856 | // l->overflow_arg_area + sizeof(type). |
| 2857 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2858 | // an 8 byte boundary. |
| 2859 | |
| 2860 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2861 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2862 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2863 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2864 | "overflow_arg_area.next"); |
| 2865 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2866 | |
| 2867 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2868 | return Res; |
| 2869 | } |
| 2870 | |
| 2871 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2872 | CodeGenFunction &CGF) const { |
| 2873 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2874 | // struct { |
| 2875 | // i32 gp_offset; |
| 2876 | // i32 fp_offset; |
| 2877 | // i8* overflow_arg_area; |
| 2878 | // i8* reg_save_area; |
| 2879 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2880 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2881 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2882 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2883 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2884 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2885 | |
| 2886 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2887 | // in the registers. If not go to step 7. |
| 2888 | if (!neededInt && !neededSSE) |
| 2889 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2890 | |
| 2891 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2892 | // general purpose registers needed to pass type and num_fp to hold |
| 2893 | // the number of floating point registers needed. |
| 2894 | |
| 2895 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2896 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2897 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2898 | // |
| 2899 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2900 | // register save space). |
| 2901 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2902 | llvm::Value *InRegs = nullptr; |
| 2903 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2904 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2905 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2906 | gp_offset_p = |
| 2907 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2908 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2909 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2910 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
| 2913 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2914 | fp_offset_p = |
| 2915 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2916 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2917 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2918 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2919 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2920 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2921 | } |
| 2922 | |
| 2923 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2924 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2925 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2926 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2927 | |
| 2928 | // Emit code to load the value if it was passed in registers. |
| 2929 | |
| 2930 | CGF.EmitBlock(InRegBlock); |
| 2931 | |
| 2932 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2933 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2934 | // copying to a temporary location in case the parameter is passed |
| 2935 | // in different register classes or requires an alignment greater |
| 2936 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2937 | // |
| 2938 | // FIXME: This really results in shameful code when we end up needing to |
| 2939 | // collect arguments from different places; often what should result in a |
| 2940 | // simple assembling of a structure from scattered addresses has many more |
| 2941 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2942 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2943 | llvm::Value *RegAddr = CGF.Builder.CreateLoad( |
| 2944 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3), "reg_save_area"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2945 | if (neededInt && neededSSE) { |
| 2946 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2947 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2948 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2949 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2950 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2951 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2952 | llvm::Type *TyLo = ST->getElementType(0); |
| 2953 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2954 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2955 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2956 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2957 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2958 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2959 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2960 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2961 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2962 | llvm::Value *V = |
| 2963 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2964 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2965 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2966 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2967 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2968 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2969 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2970 | } else if (neededInt) { |
| 2971 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2972 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2973 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2974 | |
| 2975 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2976 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2977 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2978 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2979 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2980 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2981 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2982 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2983 | RegAddr = Tmp; |
| 2984 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2985 | } else if (neededSSE == 1) { |
| 2986 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2987 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2988 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2989 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2990 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2991 | // SSE registers are spaced 16 bytes apart in the register save |
| 2992 | // area, we need to collect the two eightbytes together. |
| 2993 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2994 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2995 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2996 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2997 | llvm::PointerType::getUnqual(DoubleTy); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2998 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2999 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 3000 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3001 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 3002 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3003 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3004 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 3005 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3006 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3007 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 3008 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3012 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3013 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3014 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3015 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3016 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3017 | gp_offset_p); |
| 3018 | } |
| 3019 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3020 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3021 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3022 | fp_offset_p); |
| 3023 | } |
| 3024 | CGF.EmitBranch(ContBlock); |
| 3025 | |
| 3026 | // Emit code to load the value if it was passed in memory. |
| 3027 | |
| 3028 | CGF.EmitBlock(InMemBlock); |
| 3029 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 3030 | |
| 3031 | // Return the appropriate result. |
| 3032 | |
| 3033 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 3034 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3035 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3036 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 3037 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3038 | return ResAddr; |
| 3039 | } |
| 3040 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3041 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3042 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3043 | |
| 3044 | if (Ty->isVoidType()) |
| 3045 | return ABIArgInfo::getIgnore(); |
| 3046 | |
| 3047 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3048 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3049 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3050 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3051 | uint64_t Width = Info.Width; |
| 3052 | unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); |
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 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3055 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3056 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3057 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3058 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 3059 | } |
| 3060 | |
| 3061 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3062 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3063 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3064 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3065 | if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3066 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3067 | Width)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3068 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3069 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3070 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3071 | // other targets. |
| 3072 | const Type *Base = nullptr; |
| 3073 | uint64_t NumElts = 0; |
| 3074 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3075 | if (FreeSSERegs >= NumElts) { |
| 3076 | FreeSSERegs -= NumElts; |
| 3077 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3078 | return ABIArgInfo::getDirect(); |
| 3079 | return ABIArgInfo::getExpand(); |
| 3080 | } |
| 3081 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3082 | } |
| 3083 | |
| 3084 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3085 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3086 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3087 | // directly. |
| 3088 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3089 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3090 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3093 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3094 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3095 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3096 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3097 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3098 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3099 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3100 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3103 | // Bool type is always extended to the ABI, other builtin types are not |
| 3104 | // extended. |
| 3105 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3106 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3107 | return ABIArgInfo::getExtend(); |
| 3108 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3109 | return ABIArgInfo::getDirect(); |
| 3110 | } |
| 3111 | |
| 3112 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3113 | bool IsVectorCall = |
| 3114 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3115 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3116 | // We can use up to 4 SSE return registers with vectorcall. |
| 3117 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3118 | if (!getCXXABI().classifyReturnType(FI)) |
| 3119 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3120 | |
| 3121 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3122 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3123 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3124 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3127 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3128 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3129 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3131 | CGBuilderTy &Builder = CGF.Builder; |
| 3132 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 3133 | "ap"); |
| 3134 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3135 | llvm::Type *PTy = |
| 3136 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3137 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3138 | |
| 3139 | uint64_t Offset = |
| 3140 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 3141 | llvm::Value *NextAddr = |
| 3142 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3143 | "ap.next"); |
| 3144 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3145 | |
| 3146 | return AddrTyped; |
| 3147 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3148 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3149 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3150 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3151 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3152 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3153 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3154 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3155 | |
| 3156 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3157 | CodeGenFunction &CGF) const override; |
| 3158 | }; |
| 3159 | |
| 3160 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3161 | public: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3162 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3163 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3164 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3165 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3166 | // This is recovered from gcc output. |
| 3167 | return 1; // r1 is the dedicated stack pointer |
| 3168 | } |
| 3169 | |
| 3170 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3171 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3172 | |
| 3173 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3174 | return 16; // Natural alignment for Altivec vectors. |
| 3175 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3176 | }; |
| 3177 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3178 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3179 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3180 | llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3181 | QualType Ty, |
| 3182 | CodeGenFunction &CGF) const { |
| 3183 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3184 | // TODO: Implement this. For now ignore. |
| 3185 | (void)CTy; |
| 3186 | return nullptr; |
| 3187 | } |
| 3188 | |
| 3189 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3190 | bool isInt = |
| 3191 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3192 | llvm::Type *CharPtr = CGF.Int8PtrTy; |
| 3193 | llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy; |
| 3194 | |
| 3195 | CGBuilderTy &Builder = CGF.Builder; |
| 3196 | llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr"); |
| 3197 | llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3198 | llvm::Value *FPRPtrAsInt = |
| 3199 | Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3200 | llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3201 | llvm::Value *OverflowAreaPtrAsInt = |
| 3202 | Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3)); |
| 3203 | llvm::Value *OverflowAreaPtr = |
| 3204 | Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr); |
| 3205 | llvm::Value *RegsaveAreaPtrAsInt = |
| 3206 | Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4)); |
| 3207 | llvm::Value *RegsaveAreaPtr = |
| 3208 | Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3209 | llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr"); |
| 3210 | // Align GPR when TY is i64. |
| 3211 | if (isI64) { |
| 3212 | llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1)); |
| 3213 | llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1)); |
| 3214 | llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1)); |
| 3215 | GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR); |
| 3216 | } |
| 3217 | llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr"); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3218 | llvm::Value *OverflowArea = |
| 3219 | Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area"); |
| 3220 | llvm::Value *OverflowAreaAsInt = |
| 3221 | Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty); |
| 3222 | llvm::Value *RegsaveArea = |
| 3223 | Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area"); |
| 3224 | llvm::Value *RegsaveAreaAsInt = |
| 3225 | Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3226 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3227 | llvm::Value *CC = |
| 3228 | Builder.CreateICmpULT(isInt ? GPR : FPR, Builder.getInt8(8), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3229 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3230 | llvm::Value *RegConstant = |
| 3231 | Builder.CreateMul(isInt ? GPR : FPR, Builder.getInt8(isInt ? 4 : 8)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3232 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3233 | llvm::Value *OurReg = Builder.CreateAdd( |
| 3234 | RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3235 | |
| 3236 | if (Ty->isFloatingType()) |
| 3237 | OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32)); |
| 3238 | |
| 3239 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3240 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3241 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3242 | |
| 3243 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3244 | |
| 3245 | CGF.EmitBlock(UsingRegs); |
| 3246 | |
| 3247 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3248 | llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy); |
| 3249 | // Increase the GPR/FPR indexes. |
| 3250 | if (isInt) { |
| 3251 | GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1)); |
| 3252 | Builder.CreateStore(GPR, GPRPtr); |
| 3253 | } else { |
| 3254 | FPR = Builder.CreateAdd(FPR, Builder.getInt8(1)); |
| 3255 | Builder.CreateStore(FPR, FPRPtr); |
| 3256 | } |
| 3257 | CGF.EmitBranch(Cont); |
| 3258 | |
| 3259 | CGF.EmitBlock(UsingOverflow); |
| 3260 | |
| 3261 | // Increase the overflow area. |
| 3262 | llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3263 | OverflowAreaAsInt = |
| 3264 | Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8)); |
| 3265 | Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), |
| 3266 | OverflowAreaPtr); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3267 | CGF.EmitBranch(Cont); |
| 3268 | |
| 3269 | CGF.EmitBlock(Cont); |
| 3270 | |
| 3271 | llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr"); |
| 3272 | Result->addIncoming(Result1, UsingRegs); |
| 3273 | Result->addIncoming(Result2, UsingOverflow); |
| 3274 | |
| 3275 | if (Ty->isAggregateType()) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3276 | llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3277 | return Builder.CreateLoad(AGGPtr, false, "aggr"); |
| 3278 | } |
| 3279 | |
| 3280 | return Result; |
| 3281 | } |
| 3282 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3283 | bool |
| 3284 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3285 | llvm::Value *Address) const { |
| 3286 | // This is calculated from the LLVM and GCC tables and verified |
| 3287 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3288 | |
| 3289 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3290 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3291 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3292 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3293 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3294 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3295 | |
| 3296 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3297 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3298 | |
| 3299 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3300 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3301 | |
| 3302 | // 64-76 are various 4-byte special-purpose registers: |
| 3303 | // 64: mq |
| 3304 | // 65: lr |
| 3305 | // 66: ctr |
| 3306 | // 67: ap |
| 3307 | // 68-75 cr0-7 |
| 3308 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3309 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3310 | |
| 3311 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3312 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3313 | |
| 3314 | // 109: vrsave |
| 3315 | // 110: vscr |
| 3316 | // 111: spe_acc |
| 3317 | // 112: spefscr |
| 3318 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3319 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3320 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3321 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3324 | // PowerPC-64 |
| 3325 | |
| 3326 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3327 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3328 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3329 | public: |
| 3330 | enum ABIKind { |
| 3331 | ELFv1 = 0, |
| 3332 | ELFv2 |
| 3333 | }; |
| 3334 | |
| 3335 | private: |
| 3336 | static const unsigned GPRBits = 64; |
| 3337 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3338 | bool HasQPX; |
| 3339 | |
| 3340 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3341 | // will be passed in a QPX register. |
| 3342 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3343 | if (!HasQPX) |
| 3344 | return false; |
| 3345 | |
| 3346 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3347 | unsigned NumElements = VT->getNumElements(); |
| 3348 | if (NumElements == 1) |
| 3349 | return false; |
| 3350 | |
| 3351 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3352 | if (getContext().getTypeSize(Ty) <= 256) |
| 3353 | return true; |
| 3354 | } else if (VT->getElementType()-> |
| 3355 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3356 | if (getContext().getTypeSize(Ty) <= 128) |
| 3357 | return true; |
| 3358 | } |
| 3359 | } |
| 3360 | |
| 3361 | return false; |
| 3362 | } |
| 3363 | |
| 3364 | bool IsQPXVectorTy(QualType Ty) const { |
| 3365 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3366 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3367 | |
| 3368 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3369 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3370 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3371 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3372 | bool isPromotableTypeForABI(QualType Ty) const; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3373 | bool isAlignedParamType(QualType Ty, bool &Align32) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3374 | |
| 3375 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3376 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3377 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3378 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3379 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3380 | uint64_t Members) const override; |
| 3381 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3382 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3383 | // Example: For aggregate arguments that fit in a register, we could |
| 3384 | // use getDirectInReg (as is done below for structs containing a single |
| 3385 | // floating-point value) to avoid pushing them to memory on function |
| 3386 | // entry. This would require changing the logic in PPCISelLowering |
| 3387 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3388 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3389 | if (!getCXXABI().classifyReturnType(FI)) |
| 3390 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3391 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3392 | // We rely on the default argument classification for the most part. |
| 3393 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3394 | // 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] | 3395 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3396 | if (T) { |
| 3397 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3398 | if (IsQPXVectorTy(T) || |
| 3399 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3400 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3401 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3402 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3403 | continue; |
| 3404 | } |
| 3405 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3406 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3407 | } |
| 3408 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3409 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3410 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3411 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3412 | }; |
| 3413 | |
| 3414 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3415 | bool HasQPX; |
| 3416 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3417 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3418 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3419 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
| 3420 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)), |
| 3421 | HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3422 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3423 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3424 | // This is recovered from gcc output. |
| 3425 | return 1; // r1 is the dedicated stack pointer |
| 3426 | } |
| 3427 | |
| 3428 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3429 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3430 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3431 | unsigned getOpenMPSimdDefaultAlignment(QualType QT) const override { |
| 3432 | if (HasQPX) |
| 3433 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 3434 | if (PT->getPointeeType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 3435 | return 32; // Natural alignment for QPX doubles. |
| 3436 | |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3437 | return 16; // Natural alignment for Altivec and VSX vectors. |
| 3438 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3439 | }; |
| 3440 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3441 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3442 | public: |
| 3443 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3444 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3445 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3446 | // This is recovered from gcc output. |
| 3447 | return 1; // r1 is the dedicated stack pointer |
| 3448 | } |
| 3449 | |
| 3450 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3451 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3452 | |
| 3453 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3454 | return 16; // Natural alignment for Altivec vectors. |
| 3455 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3456 | }; |
| 3457 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3458 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3459 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3460 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3461 | // extended to 64 bits. |
| 3462 | bool |
| 3463 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3464 | // Treat an enum type as its underlying type. |
| 3465 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3466 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3467 | |
| 3468 | // Promotable integer types are required to be promoted by the ABI. |
| 3469 | if (Ty->isPromotableIntegerType()) |
| 3470 | return true; |
| 3471 | |
| 3472 | // In addition to the usual promotable integer types, we also need to |
| 3473 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3474 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3475 | switch (BT->getKind()) { |
| 3476 | case BuiltinType::Int: |
| 3477 | case BuiltinType::UInt: |
| 3478 | return true; |
| 3479 | default: |
| 3480 | break; |
| 3481 | } |
| 3482 | |
| 3483 | return false; |
| 3484 | } |
| 3485 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3486 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3487 | /// alignment in the parameter area. |
| 3488 | bool |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3489 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty, bool &Align32) const { |
| 3490 | Align32 = false; |
| 3491 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3492 | // Complex types are passed just like their elements. |
| 3493 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3494 | Ty = CTy->getElementType(); |
| 3495 | |
| 3496 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3497 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3498 | if (IsQPXVectorTy(Ty)) { |
| 3499 | if (getContext().getTypeSize(Ty) > 128) |
| 3500 | Align32 = true; |
| 3501 | |
| 3502 | return true; |
| 3503 | } else if (Ty->isVectorType()) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3504 | return getContext().getTypeSize(Ty) == 128; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3505 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3506 | |
| 3507 | // For single-element float/vector structs, we consider the whole type |
| 3508 | // to have the same alignment requirements as its single element. |
| 3509 | const Type *AlignAsType = nullptr; |
| 3510 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3511 | if (EltType) { |
| 3512 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3513 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3514 | getContext().getTypeSize(EltType) == 128) || |
| 3515 | (BT && BT->isFloatingPoint())) |
| 3516 | AlignAsType = EltType; |
| 3517 | } |
| 3518 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3519 | // Likewise for ELFv2 homogeneous aggregates. |
| 3520 | const Type *Base = nullptr; |
| 3521 | uint64_t Members = 0; |
| 3522 | if (!AlignAsType && Kind == ELFv2 && |
| 3523 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3524 | AlignAsType = Base; |
| 3525 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3526 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3527 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3528 | if (getContext().getTypeSize(AlignAsType) > 128) |
| 3529 | Align32 = true; |
| 3530 | |
| 3531 | return true; |
| 3532 | } else if (AlignAsType) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3533 | return AlignAsType->isVectorType(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3534 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3535 | |
| 3536 | // Otherwise, we only need alignment for any aggregate type that |
| 3537 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3538 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3539 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
| 3540 | Align32 = true; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3541 | return true; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3542 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3543 | |
| 3544 | return false; |
| 3545 | } |
| 3546 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3547 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3548 | /// aggregate. Base is set to the base element type, and Members is set |
| 3549 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3550 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3551 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3552 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3553 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3554 | if (NElements == 0) |
| 3555 | return false; |
| 3556 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3557 | return false; |
| 3558 | Members *= NElements; |
| 3559 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3560 | const RecordDecl *RD = RT->getDecl(); |
| 3561 | if (RD->hasFlexibleArrayMember()) |
| 3562 | return false; |
| 3563 | |
| 3564 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3565 | |
| 3566 | // If this is a C++ record, check the bases first. |
| 3567 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3568 | for (const auto &I : CXXRD->bases()) { |
| 3569 | // Ignore empty records. |
| 3570 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3571 | continue; |
| 3572 | |
| 3573 | uint64_t FldMembers; |
| 3574 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3575 | return false; |
| 3576 | |
| 3577 | Members += FldMembers; |
| 3578 | } |
| 3579 | } |
| 3580 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3581 | for (const auto *FD : RD->fields()) { |
| 3582 | // Ignore (non-zero arrays of) empty records. |
| 3583 | QualType FT = FD->getType(); |
| 3584 | while (const ConstantArrayType *AT = |
| 3585 | getContext().getAsConstantArrayType(FT)) { |
| 3586 | if (AT->getSize().getZExtValue() == 0) |
| 3587 | return false; |
| 3588 | FT = AT->getElementType(); |
| 3589 | } |
| 3590 | if (isEmptyRecord(getContext(), FT, true)) |
| 3591 | continue; |
| 3592 | |
| 3593 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3594 | if (getContext().getLangOpts().CPlusPlus && |
| 3595 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3596 | continue; |
| 3597 | |
| 3598 | uint64_t FldMembers; |
| 3599 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3600 | return false; |
| 3601 | |
| 3602 | Members = (RD->isUnion() ? |
| 3603 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3604 | } |
| 3605 | |
| 3606 | if (!Base) |
| 3607 | return false; |
| 3608 | |
| 3609 | // Ensure there is no padding. |
| 3610 | if (getContext().getTypeSize(Base) * Members != |
| 3611 | getContext().getTypeSize(Ty)) |
| 3612 | return false; |
| 3613 | } else { |
| 3614 | Members = 1; |
| 3615 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3616 | Members = 2; |
| 3617 | Ty = CT->getElementType(); |
| 3618 | } |
| 3619 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3620 | // Most ABIs only support float, double, and some vector type widths. |
| 3621 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3622 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3623 | |
| 3624 | // The base type must be the same for all members. Types that |
| 3625 | // agree in both total size and mode (float vs. vector) are |
| 3626 | // treated as being equivalent here. |
| 3627 | const Type *TyPtr = Ty.getTypePtr(); |
| 3628 | if (!Base) |
| 3629 | Base = TyPtr; |
| 3630 | |
| 3631 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3632 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3633 | return false; |
| 3634 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3635 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3636 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3637 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3638 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3639 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3640 | // double, long double, or 128-bit vectors. |
| 3641 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3642 | if (BT->getKind() == BuiltinType::Float || |
| 3643 | BT->getKind() == BuiltinType::Double || |
| 3644 | BT->getKind() == BuiltinType::LongDouble) |
| 3645 | return true; |
| 3646 | } |
| 3647 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3648 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3649 | return true; |
| 3650 | } |
| 3651 | return false; |
| 3652 | } |
| 3653 | |
| 3654 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3655 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3656 | // Vector types require one register, floating point types require one |
| 3657 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3658 | uint32_t NumRegs = |
| 3659 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3660 | |
| 3661 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3662 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3665 | ABIArgInfo |
| 3666 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3667 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3668 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3669 | if (Ty->isAnyComplexType()) |
| 3670 | return ABIArgInfo::getDirect(); |
| 3671 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3672 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3673 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3674 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3675 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3676 | if (Size > 128) |
| 3677 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3678 | else if (Size < 128) { |
| 3679 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3680 | return ABIArgInfo::getDirect(CoerceTy); |
| 3681 | } |
| 3682 | } |
| 3683 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3684 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3685 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3686 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3687 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3688 | bool Align32; |
| 3689 | uint64_t ABIAlign = isAlignedParamType(Ty, Align32) ? |
| 3690 | (Align32 ? 32 : 16) : 8; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3691 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3692 | |
| 3693 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3694 | const Type *Base = nullptr; |
| 3695 | uint64_t Members = 0; |
| 3696 | if (Kind == ELFv2 && |
| 3697 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3698 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3699 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3700 | return ABIArgInfo::getDirect(CoerceTy); |
| 3701 | } |
| 3702 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3703 | // If an aggregate may end up fully in registers, we do not |
| 3704 | // use the ByVal method, but pass the aggregate as array. |
| 3705 | // This is usually beneficial since we avoid forcing the |
| 3706 | // back-end to store the argument to memory. |
| 3707 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3708 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3709 | llvm::Type *CoerceTy; |
| 3710 | |
| 3711 | // Types up to 8 bytes are passed as integer type (which will be |
| 3712 | // properly aligned in the argument save area doubleword). |
| 3713 | if (Bits <= GPRBits) |
| 3714 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3715 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3716 | // Larger types are passed as arrays, with the base type selected |
| 3717 | // according to the required alignment in the save area. |
| 3718 | else { |
| 3719 | uint64_t RegBits = ABIAlign * 8; |
| 3720 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3721 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3722 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3723 | } |
| 3724 | |
| 3725 | return ABIArgInfo::getDirect(CoerceTy); |
| 3726 | } |
| 3727 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3728 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3729 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3730 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | return (isPromotableTypeForABI(Ty) ? |
| 3734 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3735 | } |
| 3736 | |
| 3737 | ABIArgInfo |
| 3738 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3739 | if (RetTy->isVoidType()) |
| 3740 | return ABIArgInfo::getIgnore(); |
| 3741 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3742 | if (RetTy->isAnyComplexType()) |
| 3743 | return ABIArgInfo::getDirect(); |
| 3744 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3745 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3746 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3747 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3748 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3749 | if (Size > 128) |
| 3750 | return ABIArgInfo::getIndirect(0); |
| 3751 | else if (Size < 128) { |
| 3752 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3753 | return ABIArgInfo::getDirect(CoerceTy); |
| 3754 | } |
| 3755 | } |
| 3756 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3757 | if (isAggregateTypeForABI(RetTy)) { |
| 3758 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3759 | const Type *Base = nullptr; |
| 3760 | uint64_t Members = 0; |
| 3761 | if (Kind == ELFv2 && |
| 3762 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3763 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3764 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3765 | return ABIArgInfo::getDirect(CoerceTy); |
| 3766 | } |
| 3767 | |
| 3768 | // ELFv2 small aggregates are returned in up to two registers. |
| 3769 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3770 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3771 | if (Bits == 0) |
| 3772 | return ABIArgInfo::getIgnore(); |
| 3773 | |
| 3774 | llvm::Type *CoerceTy; |
| 3775 | if (Bits > GPRBits) { |
| 3776 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3777 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3778 | } else |
| 3779 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3780 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3781 | return ABIArgInfo::getDirect(CoerceTy); |
| 3782 | } |
| 3783 | |
| 3784 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3785 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3786 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3787 | |
| 3788 | return (isPromotableTypeForABI(RetTy) ? |
| 3789 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3790 | } |
| 3791 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3792 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3793 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3794 | QualType Ty, |
| 3795 | CodeGenFunction &CGF) const { |
| 3796 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3797 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3798 | |
| 3799 | CGBuilderTy &Builder = CGF.Builder; |
| 3800 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3801 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3802 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3803 | // Handle types that require 16-byte alignment in the parameter save area. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3804 | bool Align32; |
| 3805 | if (isAlignedParamType(Ty, Align32)) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3806 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3807 | AddrAsInt = Builder.CreateAdd(AddrAsInt, |
| 3808 | Builder.getInt64(Align32 ? 31 : 15)); |
| 3809 | AddrAsInt = Builder.CreateAnd(AddrAsInt, |
| 3810 | Builder.getInt64(Align32 ? -32 : -16)); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3811 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3812 | } |
| 3813 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3814 | // Update the va_list pointer. The pointer should be bumped by the |
| 3815 | // size of the object. We can trust getTypeSize() except for a complex |
| 3816 | // type whose base type is smaller than a doubleword. For these, the |
| 3817 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3818 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3819 | QualType BaseTy; |
| 3820 | unsigned CplxBaseSize = 0; |
| 3821 | |
| 3822 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3823 | BaseTy = CTy->getElementType(); |
| 3824 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3825 | if (CplxBaseSize < 8) |
| 3826 | SizeInBytes = 16; |
| 3827 | } |
| 3828 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3829 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3830 | llvm::Value *NextAddr = |
| 3831 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3832 | "ap.next"); |
| 3833 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3834 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3835 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3836 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3837 | // in separate doublewords. However, Clang expects us to produce a |
| 3838 | // pointer to a structure with the two parts packed tightly. So generate |
| 3839 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3840 | // and store them to a temporary structure. |
| 3841 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3842 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3843 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3844 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3845 | RealAddr = |
| 3846 | Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3847 | ImagAddr = |
| 3848 | Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3849 | } else { |
| 3850 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3851 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3852 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3853 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3854 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3855 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3856 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3857 | llvm::AllocaInst *Ptr = |
| 3858 | CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), "vacplx"); |
| 3859 | llvm::Value *RealPtr = |
| 3860 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 0, ".real"); |
| 3861 | llvm::Value *ImagPtr = |
| 3862 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 1, ".imag"); |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3863 | Builder.CreateStore(Real, RealPtr, false); |
| 3864 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3865 | return Ptr; |
| 3866 | } |
| 3867 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3868 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3869 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3870 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3871 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3872 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3873 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3874 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3875 | } |
| 3876 | |
| 3877 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3878 | return Builder.CreateBitCast(Addr, PTy); |
| 3879 | } |
| 3880 | |
| 3881 | static bool |
| 3882 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3883 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3884 | // This is calculated from the LLVM and GCC tables and verified |
| 3885 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3886 | |
| 3887 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3888 | |
| 3889 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3890 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3891 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3892 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3893 | |
| 3894 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3895 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3896 | |
| 3897 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3898 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3899 | |
| 3900 | // 64-76 are various 4-byte special-purpose registers: |
| 3901 | // 64: mq |
| 3902 | // 65: lr |
| 3903 | // 66: ctr |
| 3904 | // 67: ap |
| 3905 | // 68-75 cr0-7 |
| 3906 | // 76: xer |
| 3907 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3908 | |
| 3909 | // 77-108: v0-31, the 16-byte vector registers |
| 3910 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3911 | |
| 3912 | // 109: vrsave |
| 3913 | // 110: vscr |
| 3914 | // 111: spe_acc |
| 3915 | // 112: spefscr |
| 3916 | // 113: sfp |
| 3917 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3918 | |
| 3919 | return false; |
| 3920 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3921 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3922 | bool |
| 3923 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3924 | CodeGen::CodeGenFunction &CGF, |
| 3925 | llvm::Value *Address) const { |
| 3926 | |
| 3927 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3928 | } |
| 3929 | |
| 3930 | bool |
| 3931 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3932 | llvm::Value *Address) const { |
| 3933 | |
| 3934 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3935 | } |
| 3936 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3937 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3938 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3939 | //===----------------------------------------------------------------------===// |
| 3940 | |
| 3941 | namespace { |
| 3942 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3943 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3944 | public: |
| 3945 | enum ABIKind { |
| 3946 | AAPCS = 0, |
| 3947 | DarwinPCS |
| 3948 | }; |
| 3949 | |
| 3950 | private: |
| 3951 | ABIKind Kind; |
| 3952 | |
| 3953 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3954 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3955 | |
| 3956 | private: |
| 3957 | ABIKind getABIKind() const { return Kind; } |
| 3958 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3959 | |
| 3960 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3961 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3962 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3963 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3964 | uint64_t Members) const override; |
| 3965 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3966 | bool isIllegalVectorType(QualType Ty) const; |
| 3967 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 3968 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3969 | if (!getCXXABI().classifyReturnType(FI)) |
| 3970 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3971 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3972 | for (auto &it : FI.arguments()) |
| 3973 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
| 3976 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3977 | CodeGenFunction &CGF) const; |
| 3978 | |
| 3979 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3980 | CodeGenFunction &CGF) const; |
| 3981 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3982 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3983 | CodeGenFunction &CGF) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3984 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3985 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3986 | } |
| 3987 | }; |
| 3988 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3989 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3990 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3991 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3992 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3993 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3994 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3995 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3996 | } |
| 3997 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3998 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 3999 | return 31; |
| 4000 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4001 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 4002 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4003 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4004 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4005 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4006 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4007 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4008 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4009 | // Handle illegal vector types here. |
| 4010 | if (isIllegalVectorType(Ty)) { |
| 4011 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4012 | if (Size <= 32) { |
| 4013 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4014 | return ABIArgInfo::getDirect(ResType); |
| 4015 | } |
| 4016 | if (Size == 64) { |
| 4017 | llvm::Type *ResType = |
| 4018 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4019 | return ABIArgInfo::getDirect(ResType); |
| 4020 | } |
| 4021 | if (Size == 128) { |
| 4022 | llvm::Type *ResType = |
| 4023 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4024 | return ABIArgInfo::getDirect(ResType); |
| 4025 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4026 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4027 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4028 | |
| 4029 | if (!isAggregateTypeForABI(Ty)) { |
| 4030 | // Treat an enum type as its underlying type. |
| 4031 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4032 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4033 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4034 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4035 | ? ABIArgInfo::getExtend() |
| 4036 | : ABIArgInfo::getDirect()); |
| 4037 | } |
| 4038 | |
| 4039 | // Structures with either a non-trivial destructor or a non-trivial |
| 4040 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4041 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4042 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4043 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4047 | // elsewhere for GNU compatibility. |
| 4048 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4049 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4050 | return ABIArgInfo::getIgnore(); |
| 4051 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4052 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4053 | } |
| 4054 | |
| 4055 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4056 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4057 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4058 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4059 | return ABIArgInfo::getDirect( |
| 4060 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4064 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4065 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4066 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4067 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4068 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4069 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4070 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4071 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4072 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4073 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4074 | } |
| 4075 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4076 | } |
| 4077 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4078 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4079 | } |
| 4080 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4081 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4082 | if (RetTy->isVoidType()) |
| 4083 | return ABIArgInfo::getIgnore(); |
| 4084 | |
| 4085 | // Large vector types should be returned via memory. |
| 4086 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 4087 | return ABIArgInfo::getIndirect(0); |
| 4088 | |
| 4089 | if (!isAggregateTypeForABI(RetTy)) { |
| 4090 | // Treat an enum type as its underlying type. |
| 4091 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4092 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4093 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4094 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4095 | ? ABIArgInfo::getExtend() |
| 4096 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4099 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4100 | return ABIArgInfo::getIgnore(); |
| 4101 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4102 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4103 | uint64_t Members = 0; |
| 4104 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4105 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4106 | return ABIArgInfo::getDirect(); |
| 4107 | |
| 4108 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4109 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4110 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4111 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4112 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4113 | |
| 4114 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4115 | // For aggregates with 16-byte alignment, we use i128. |
| 4116 | if (Alignment < 128 && Size == 128) { |
| 4117 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4118 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4119 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4120 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4121 | } |
| 4122 | |
| 4123 | return ABIArgInfo::getIndirect(0); |
| 4124 | } |
| 4125 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4126 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4127 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4128 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4129 | // Check whether VT is legal. |
| 4130 | unsigned NumElements = VT->getNumElements(); |
| 4131 | uint64_t Size = getContext().getTypeSize(VT); |
| 4132 | // NumElements should be power of 2 between 1 and 16. |
| 4133 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4134 | return true; |
| 4135 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4136 | } |
| 4137 | return false; |
| 4138 | } |
| 4139 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4140 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4141 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4142 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4143 | // but with the difference that any floating-point type is allowed, |
| 4144 | // including __fp16. |
| 4145 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4146 | if (BT->isFloatingPoint()) |
| 4147 | return true; |
| 4148 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4149 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4150 | if (VecSize == 64 || VecSize == 128) |
| 4151 | return true; |
| 4152 | } |
| 4153 | return false; |
| 4154 | } |
| 4155 | |
| 4156 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4157 | uint64_t Members) const { |
| 4158 | return Members <= 4; |
| 4159 | } |
| 4160 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4161 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, |
| 4162 | QualType Ty, |
| 4163 | CodeGenFunction &CGF) const { |
| 4164 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4165 | bool IsIndirect = AI.isIndirect(); |
| 4166 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4167 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4168 | if (IsIndirect) |
| 4169 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4170 | else if (AI.getCoerceToType()) |
| 4171 | BaseTy = AI.getCoerceToType(); |
| 4172 | |
| 4173 | unsigned NumRegs = 1; |
| 4174 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4175 | BaseTy = ArrTy->getElementType(); |
| 4176 | NumRegs = ArrTy->getNumElements(); |
| 4177 | } |
| 4178 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4179 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4180 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4181 | // Standard, section B.4: |
| 4182 | // |
| 4183 | // struct { |
| 4184 | // void *__stack; |
| 4185 | // void *__gr_top; |
| 4186 | // void *__vr_top; |
| 4187 | // int __gr_offs; |
| 4188 | // int __vr_offs; |
| 4189 | // }; |
| 4190 | |
| 4191 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4192 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4193 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4194 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4195 | auto &Ctx = CGF.getContext(); |
| 4196 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4197 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4198 | int reg_top_index; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4199 | int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8; |
| 4200 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4201 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4202 | reg_offs_p = |
| 4203 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4204 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4205 | reg_top_index = 1; // field number for __gr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4206 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4207 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4208 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4209 | reg_offs_p = |
| 4210 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4211 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4212 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4213 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | //======================================= |
| 4217 | // Find out where argument was passed |
| 4218 | //======================================= |
| 4219 | |
| 4220 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4221 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4222 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4223 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4224 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4225 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4226 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4227 | |
| 4228 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4229 | |
| 4230 | // 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] | 4231 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4232 | CGF.EmitBlock(MaybeRegBlock); |
| 4233 | |
| 4234 | // Integer arguments may need to correct register alignment (for example a |
| 4235 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4236 | // align __gr_offs to calculate the potential address. |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4237 | if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4238 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4239 | |
| 4240 | reg_offs = CGF.Builder.CreateAdd( |
| 4241 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4242 | "align_regoffs"); |
| 4243 | reg_offs = CGF.Builder.CreateAnd( |
| 4244 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4245 | "aligned_regoffs"); |
| 4246 | } |
| 4247 | |
| 4248 | // 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] | 4249 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4250 | NewOffset = CGF.Builder.CreateAdd( |
| 4251 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4252 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4253 | |
| 4254 | // Now we're in a position to decide whether this argument really was in |
| 4255 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4256 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4257 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4258 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4259 | |
| 4260 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4261 | |
| 4262 | //======================================= |
| 4263 | // Argument was in registers |
| 4264 | //======================================= |
| 4265 | |
| 4266 | // Now we emit the code for if the argument was originally passed in |
| 4267 | // registers. First start the appropriate block: |
| 4268 | CGF.EmitBlock(InRegBlock); |
| 4269 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4270 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4271 | reg_top_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, reg_top_index, |
| 4272 | "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4273 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4274 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4275 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4276 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4277 | |
| 4278 | if (IsIndirect) { |
| 4279 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4280 | // stored registers or on the stack will actually be a struct **. |
| 4281 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4282 | } |
| 4283 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4284 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4285 | uint64_t NumMembers = 0; |
| 4286 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4287 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4288 | // Homogeneous aggregates passed in registers will have their elements split |
| 4289 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4290 | // qN+1, ...). We reload and store into a temporary local variable |
| 4291 | // contiguously. |
| 4292 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 4293 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4294 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4295 | llvm::AllocaInst *Tmp = CGF.CreateTempAlloca(HFATy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4296 | int Offset = 0; |
| 4297 | |
| 4298 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 4299 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 4300 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4301 | llvm::Value *BaseOffset = |
| 4302 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 4303 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4304 | LoadAddr = CGF.Builder.CreateBitCast( |
| 4305 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4306 | llvm::Value *StoreAddr = |
| 4307 | CGF.Builder.CreateStructGEP(Tmp->getAllocatedType(), Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4308 | |
| 4309 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4310 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4311 | } |
| 4312 | |
| 4313 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4314 | } else { |
| 4315 | // Otherwise the object is contiguous in memory |
| 4316 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4317 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4318 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4319 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 4320 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 4321 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 4322 | |
| 4323 | BaseAddr = CGF.Builder.CreateAdd( |
| 4324 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4325 | |
| 4326 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 4327 | } |
| 4328 | |
| 4329 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4330 | } |
| 4331 | |
| 4332 | CGF.EmitBranch(ContBlock); |
| 4333 | |
| 4334 | //======================================= |
| 4335 | // Argument was on the stack |
| 4336 | //======================================= |
| 4337 | CGF.EmitBlock(OnStackBlock); |
| 4338 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4339 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4340 | stack_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "stack_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4341 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4342 | |
| 4343 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4344 | // floating-point ones might be affected. |
| 4345 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 4346 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4347 | |
| 4348 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4349 | |
| 4350 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4351 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4352 | "align_stack"); |
| 4353 | OnStackAddr = CGF.Builder.CreateAnd( |
| 4354 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4355 | "align_stack"); |
| 4356 | |
| 4357 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4358 | } |
| 4359 | |
| 4360 | uint64_t StackSize; |
| 4361 | if (IsIndirect) |
| 4362 | StackSize = 8; |
| 4363 | else |
| 4364 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 4365 | |
| 4366 | // All stack slots are 8 bytes |
| 4367 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4368 | |
| 4369 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4370 | llvm::Value *NewStack = |
| 4371 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 4372 | |
| 4373 | // Write the new value of __stack for the next call to va_arg |
| 4374 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4375 | |
| 4376 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4377 | Ctx.getTypeSize(Ty) < 64) { |
| 4378 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 4379 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4380 | |
| 4381 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4382 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4383 | |
| 4384 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4385 | } |
| 4386 | |
| 4387 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4388 | |
| 4389 | CGF.EmitBranch(ContBlock); |
| 4390 | |
| 4391 | //======================================= |
| 4392 | // Tidy up |
| 4393 | //======================================= |
| 4394 | CGF.EmitBlock(ContBlock); |
| 4395 | |
| 4396 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4397 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4398 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4399 | |
| 4400 | if (IsIndirect) |
| 4401 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4402 | |
| 4403 | return ResAddr; |
| 4404 | } |
| 4405 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4406 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, |
| 4407 | QualType Ty, |
| 4408 | CodeGenFunction &CGF) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4409 | // We do not support va_arg for aggregates or illegal vector types. |
| 4410 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 4411 | // other cases. |
| 4412 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4413 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4414 | |
| 4415 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 4416 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 4417 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4418 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4419 | uint64_t Members = 0; |
| 4420 | bool isHA = isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4421 | |
| 4422 | bool isIndirect = false; |
| 4423 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 4424 | // be passed indirectly. |
| 4425 | if (Size > 16 && !isHA) { |
| 4426 | isIndirect = true; |
| 4427 | Size = 8; |
| 4428 | Align = 8; |
| 4429 | } |
| 4430 | |
| 4431 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 4432 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 4433 | |
| 4434 | CGBuilderTy &Builder = CGF.Builder; |
| 4435 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4436 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4437 | |
| 4438 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4439 | // These are ignored for parameter passing purposes. |
| 4440 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4441 | return Builder.CreateBitCast(Addr, PTy); |
| 4442 | } |
| 4443 | |
| 4444 | const uint64_t MinABIAlign = 8; |
| 4445 | if (Align > MinABIAlign) { |
| 4446 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 4447 | Addr = Builder.CreateGEP(Addr, Offset); |
| 4448 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 4449 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 4450 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 4451 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 4452 | } |
| 4453 | |
| 4454 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 4455 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 4456 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 4457 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4458 | |
| 4459 | if (isIndirect) |
| 4460 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 4461 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4462 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4463 | |
| 4464 | return AddrTyped; |
| 4465 | } |
| 4466 | |
| 4467 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4468 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4469 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4470 | |
| 4471 | namespace { |
| 4472 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4473 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4474 | public: |
| 4475 | enum ABIKind { |
| 4476 | APCS = 0, |
| 4477 | AAPCS = 1, |
| 4478 | AAPCS_VFP |
| 4479 | }; |
| 4480 | |
| 4481 | private: |
| 4482 | ABIKind Kind; |
| 4483 | |
| 4484 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4485 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4486 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4487 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4488 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4489 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4490 | switch (getTarget().getTriple().getEnvironment()) { |
| 4491 | case llvm::Triple::Android: |
| 4492 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4493 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4494 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4495 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4496 | return true; |
| 4497 | default: |
| 4498 | return false; |
| 4499 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4502 | bool isEABIHF() const { |
| 4503 | switch (getTarget().getTriple().getEnvironment()) { |
| 4504 | case llvm::Triple::EABIHF: |
| 4505 | case llvm::Triple::GNUEABIHF: |
| 4506 | return true; |
| 4507 | default: |
| 4508 | return false; |
| 4509 | } |
| 4510 | } |
| 4511 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4512 | ABIKind getABIKind() const { return Kind; } |
| 4513 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4514 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4515 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4516 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4517 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4518 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4519 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4520 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4521 | uint64_t Members) const override; |
| 4522 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4523 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4524 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4525 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4526 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4527 | |
| 4528 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4529 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4530 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4531 | }; |
| 4532 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4533 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4534 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4535 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4536 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4537 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4538 | const ARMABIInfo &getABIInfo() const { |
| 4539 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4540 | } |
| 4541 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4542 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4543 | return 13; |
| 4544 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4545 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4546 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4547 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4548 | } |
| 4549 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4550 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4551 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4552 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4553 | |
| 4554 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4555 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4556 | return false; |
| 4557 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4558 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4559 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4560 | if (getABIInfo().isEABI()) return 88; |
| 4561 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4562 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4563 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4564 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4565 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4566 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4567 | if (!FD) |
| 4568 | return; |
| 4569 | |
| 4570 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4571 | if (!Attr) |
| 4572 | return; |
| 4573 | |
| 4574 | const char *Kind; |
| 4575 | switch (Attr->getInterrupt()) { |
| 4576 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4577 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4578 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4579 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4580 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4581 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4582 | } |
| 4583 | |
| 4584 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4585 | |
| 4586 | Fn->addFnAttr("interrupt", Kind); |
| 4587 | |
| 4588 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4589 | return; |
| 4590 | |
| 4591 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4592 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4593 | // the backend to perform a realignment as part of the function prologue. |
| 4594 | llvm::AttrBuilder B; |
| 4595 | B.addStackAlignmentAttr(8); |
| 4596 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4597 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4598 | llvm::AttributeSet::FunctionIndex, |
| 4599 | B)); |
| 4600 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4601 | }; |
| 4602 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4603 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4604 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4605 | CodeGen::CodeGenModule &CGM) const; |
| 4606 | |
| 4607 | public: |
| 4608 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4609 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4610 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4611 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4612 | CodeGen::CodeGenModule &CGM) const override; |
| 4613 | }; |
| 4614 | |
| 4615 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4616 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4617 | if (!isa<FunctionDecl>(D)) |
| 4618 | return; |
| 4619 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4620 | return; |
| 4621 | |
| 4622 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4623 | F->addFnAttr("stack-probe-size", |
| 4624 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4625 | } |
| 4626 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4627 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4628 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4629 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4630 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4631 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4632 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4633 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4634 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4635 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4636 | FI.getReturnInfo() = |
| 4637 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4638 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4639 | for (auto &I : FI.arguments()) |
| 4640 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4641 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4642 | // Always honor user-specified calling convention. |
| 4643 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4644 | return; |
| 4645 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4646 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4647 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4648 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4649 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4650 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4651 | /// Return the default calling convention that LLVM will use. |
| 4652 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4653 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4654 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4655 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4656 | else if (isEABI()) |
| 4657 | return llvm::CallingConv::ARM_AAPCS; |
| 4658 | else |
| 4659 | return llvm::CallingConv::ARM_APCS; |
| 4660 | } |
| 4661 | |
| 4662 | /// Return the calling convention that our ABI would like us to use |
| 4663 | /// as the C calling convention. |
| 4664 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4665 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4666 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4667 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4668 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4669 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4670 | llvm_unreachable("bad ABI kind"); |
| 4671 | } |
| 4672 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4673 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4674 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4675 | |
| 4676 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4677 | // they'd just match what LLVM will infer from the triple. |
| 4678 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4679 | if (abiCC != getLLVMDefaultCC()) |
| 4680 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4681 | |
| 4682 | BuiltinCC = (getABIKind() == APCS ? |
| 4683 | llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4686 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4687 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4688 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4689 | // A single-precision floating-point type (including promoted |
| 4690 | // half-precision types); A double-precision floating-point type; |
| 4691 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4692 | // with a Base Type of a single- or double-precision floating-point type, |
| 4693 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4694 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4695 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4696 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4697 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4698 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4699 | // Handle illegal vector types here. |
| 4700 | if (isIllegalVectorType(Ty)) { |
| 4701 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4702 | if (Size <= 32) { |
| 4703 | llvm::Type *ResType = |
| 4704 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4705 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4706 | } |
| 4707 | if (Size == 64) { |
| 4708 | llvm::Type *ResType = llvm::VectorType::get( |
| 4709 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4710 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4711 | } |
| 4712 | if (Size == 128) { |
| 4713 | llvm::Type *ResType = llvm::VectorType::get( |
| 4714 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4715 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4716 | } |
| 4717 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4718 | } |
| 4719 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4720 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4721 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4722 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4723 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4724 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4725 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4726 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4727 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4728 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4729 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4730 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4731 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4732 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4733 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4734 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4735 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4736 | return ABIArgInfo::getIgnore(); |
| 4737 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4738 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4739 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4740 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4741 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4742 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4743 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4744 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4745 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4746 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4747 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4750 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4751 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4752 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4753 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4754 | uint64_t ABIAlign = 4; |
| 4755 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4756 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4757 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4758 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4759 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4760 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4761 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4762 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4765 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4766 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4767 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4768 | // FIXME: Try to match the types of the arguments more accurately where |
| 4769 | // we can. |
| 4770 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4771 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4772 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4773 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4774 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4775 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4776 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4777 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4778 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4781 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4782 | llvm::LLVMContext &VMContext) { |
| 4783 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4784 | // is called integer-like if its size is less than or equal to one word, and |
| 4785 | // the offset of each of its addressable sub-fields is zero. |
| 4786 | |
| 4787 | uint64_t Size = Context.getTypeSize(Ty); |
| 4788 | |
| 4789 | // Check that the type fits in a word. |
| 4790 | if (Size > 32) |
| 4791 | return false; |
| 4792 | |
| 4793 | // FIXME: Handle vector types! |
| 4794 | if (Ty->isVectorType()) |
| 4795 | return false; |
| 4796 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4797 | // Float types are never treated as "integer like". |
| 4798 | if (Ty->isRealFloatingType()) |
| 4799 | return false; |
| 4800 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4801 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4802 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4803 | return true; |
| 4804 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4805 | // Small complex integer types are "integer like". |
| 4806 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4807 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4808 | |
| 4809 | // Single element and zero sized arrays should be allowed, by the definition |
| 4810 | // above, but they are not. |
| 4811 | |
| 4812 | // Otherwise, it must be a record type. |
| 4813 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4814 | if (!RT) return false; |
| 4815 | |
| 4816 | // Ignore records with flexible arrays. |
| 4817 | const RecordDecl *RD = RT->getDecl(); |
| 4818 | if (RD->hasFlexibleArrayMember()) |
| 4819 | return false; |
| 4820 | |
| 4821 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4822 | // like". |
| 4823 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4824 | |
| 4825 | bool HadField = false; |
| 4826 | unsigned idx = 0; |
| 4827 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4828 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4829 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4830 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4831 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4832 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4833 | // struct { int : 0; int x } |
| 4834 | // is non-integer like according to gcc. |
| 4835 | if (FD->isBitField()) { |
| 4836 | if (!RD->isUnion()) |
| 4837 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4838 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4839 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4840 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4841 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4842 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4845 | // Check if this field is at offset 0. |
| 4846 | if (Layout.getFieldOffset(idx) != 0) |
| 4847 | return false; |
| 4848 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4849 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4850 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4851 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4852 | // Only allow at most one field in a structure. This doesn't match the |
| 4853 | // wording above, but follows gcc in situations with a field following an |
| 4854 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4855 | if (!RD->isUnion()) { |
| 4856 | if (HadField) |
| 4857 | return false; |
| 4858 | |
| 4859 | HadField = true; |
| 4860 | } |
| 4861 | } |
| 4862 | |
| 4863 | return true; |
| 4864 | } |
| 4865 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4866 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4867 | bool isVariadic) const { |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4868 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4869 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4870 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4871 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4872 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4873 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4874 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4875 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4876 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4877 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4878 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4879 | // Treat an enum type as its underlying type. |
| 4880 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4881 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4882 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4883 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4884 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4885 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4886 | |
| 4887 | // Are we following APCS? |
| 4888 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4889 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4890 | return ABIArgInfo::getIgnore(); |
| 4891 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4892 | // Complex types are all returned as packed integers. |
| 4893 | // |
| 4894 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4895 | // correctly. |
| 4896 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4897 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 4898 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4899 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4900 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4901 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4902 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4903 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4904 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4905 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4906 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4907 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4908 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | // Otherwise return in memory. |
| 4912 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4913 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4914 | |
| 4915 | // Otherwise this is an AAPCS variant. |
| 4916 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4917 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4918 | return ABIArgInfo::getIgnore(); |
| 4919 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4920 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4921 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4922 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4923 | uint64_t Members; |
| 4924 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4925 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4926 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4927 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4928 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4931 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4932 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4933 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4934 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4935 | if (getDataLayout().isBigEndian()) |
| 4936 | // 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] | 4937 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4938 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4939 | // Return in the smallest viable integer type. |
| 4940 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4941 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4942 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4943 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4944 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4945 | } |
| 4946 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4947 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4950 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4951 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4952 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4953 | // Check whether VT is legal. |
| 4954 | unsigned NumElements = VT->getNumElements(); |
| 4955 | uint64_t Size = getContext().getTypeSize(VT); |
| 4956 | // NumElements should be power of 2. |
| 4957 | if ((NumElements & (NumElements - 1)) != 0) |
| 4958 | return true; |
| 4959 | // Size should be greater than 32 bits. |
| 4960 | return Size <= 32; |
| 4961 | } |
| 4962 | return false; |
| 4963 | } |
| 4964 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4965 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4966 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4967 | // double, or 64-bit or 128-bit vectors. |
| 4968 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4969 | if (BT->getKind() == BuiltinType::Float || |
| 4970 | BT->getKind() == BuiltinType::Double || |
| 4971 | BT->getKind() == BuiltinType::LongDouble) |
| 4972 | return true; |
| 4973 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4974 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4975 | if (VecSize == 64 || VecSize == 128) |
| 4976 | return true; |
| 4977 | } |
| 4978 | return false; |
| 4979 | } |
| 4980 | |
| 4981 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4982 | uint64_t Members) const { |
| 4983 | return Members <= 4; |
| 4984 | } |
| 4985 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4986 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4987 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4988 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4989 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4990 | |
| 4991 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4992 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4993 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4994 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4995 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4996 | // These are ignored for parameter passing purposes. |
| 4997 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4998 | return Builder.CreateBitCast(Addr, PTy); |
| 4999 | } |
| 5000 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5001 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 5002 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5003 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5004 | |
| 5005 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 5006 | // 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] | 5007 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5008 | getABIKind() == ARMABIInfo::AAPCS) |
| 5009 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 5010 | else |
| 5011 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5012 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5013 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 5014 | IsIndirect = true; |
| 5015 | Size = 4; |
| 5016 | TyAlign = 4; |
| 5017 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5018 | |
| 5019 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 5020 | if (TyAlign > 4) { |
| 5021 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 5022 | "Alignment is not power of 2!"); |
| 5023 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 5024 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 5025 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5026 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 5027 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5028 | |
| 5029 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5030 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5031 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 5032 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5033 | "ap.next"); |
| 5034 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5035 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5036 | if (IsIndirect) |
| 5037 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 5038 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5039 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 5040 | // may not be correctly aligned for the vector type. We create an aligned |
| 5041 | // temporary space and copy the content over from ap.cur to the temporary |
| 5042 | // space. This is necessary if the natural alignment of the type is greater |
| 5043 | // than the ABI alignment. |
| 5044 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 5045 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 5046 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 5047 | "var.align"); |
| 5048 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 5049 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 5050 | Builder.CreateMemCpy(Dst, Src, |
| 5051 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 5052 | TyAlign, false); |
| 5053 | Addr = AlignedTemp; //The content is in aligned location. |
| 5054 | } |
| 5055 | llvm::Type *PTy = |
| 5056 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5057 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5058 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5059 | return AddrTyped; |
| 5060 | } |
| 5061 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5062 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5063 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5064 | //===----------------------------------------------------------------------===// |
| 5065 | |
| 5066 | namespace { |
| 5067 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5068 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5069 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5070 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5071 | |
| 5072 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5073 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5074 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5075 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5076 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5077 | CodeGenFunction &CFG) const override; |
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 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5081 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5082 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5083 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5084 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5085 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5086 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5087 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5088 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5089 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5090 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5091 | }; |
| 5092 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5093 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5094 | if (RetTy->isVoidType()) |
| 5095 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5096 | |
| 5097 | // note: this is different from default ABI |
| 5098 | if (!RetTy->isScalarType()) |
| 5099 | return ABIArgInfo::getDirect(); |
| 5100 | |
| 5101 | // Treat an enum type as its underlying type. |
| 5102 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5103 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5104 | |
| 5105 | return (RetTy->isPromotableIntegerType() ? |
| 5106 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5107 | } |
| 5108 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5109 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5110 | // Treat an enum type as its underlying type. |
| 5111 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5112 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5113 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5114 | // Return aggregates type as indirect by value |
| 5115 | if (isAggregateTypeForABI(Ty)) |
| 5116 | return ABIArgInfo::getIndirect(0, /* byval */ true); |
| 5117 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5118 | return (Ty->isPromotableIntegerType() ? |
| 5119 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5122 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5123 | if (!getCXXABI().classifyReturnType(FI)) |
| 5124 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5125 | for (auto &I : FI.arguments()) |
| 5126 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5127 | |
| 5128 | // Always honor user-specified calling convention. |
| 5129 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5130 | return; |
| 5131 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5132 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5133 | } |
| 5134 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5135 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5136 | CodeGenFunction &CFG) const { |
| 5137 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5138 | } |
| 5139 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5140 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5141 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5142 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5143 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5144 | if (!FD) return; |
| 5145 | |
| 5146 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5147 | |
| 5148 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5149 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5150 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5151 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5152 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5153 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5154 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5155 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5156 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5157 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5158 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5159 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5160 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5161 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5162 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5163 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5164 | // __global__ functions cannot be called from the device, we do not |
| 5165 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5166 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5167 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5168 | addNVVMMetadata(F, "kernel", 1); |
| 5169 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5170 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5171 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5172 | llvm::APSInt MaxThreads(32); |
| 5173 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5174 | if (MaxThreads > 0) |
| 5175 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5176 | |
| 5177 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5178 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5179 | // we don't have to add a PTX directive. |
| 5180 | if (Attr->getMinBlocks()) { |
| 5181 | llvm::APSInt MinBlocks(32); |
| 5182 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5183 | if (MinBlocks > 0) |
| 5184 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5185 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5186 | } |
| 5187 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5188 | } |
| 5189 | } |
| 5190 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5191 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5192 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5193 | llvm::Module *M = F->getParent(); |
| 5194 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5195 | |
| 5196 | // Get "nvvm.annotations" metadata node |
| 5197 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5198 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5199 | llvm::Metadata *MDVals[] = { |
| 5200 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5201 | llvm::ConstantAsMetadata::get( |
| 5202 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5203 | // Append metadata to nvvm.annotations |
| 5204 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5205 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5206 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5207 | |
| 5208 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5209 | // SystemZ ABI Implementation |
| 5210 | //===----------------------------------------------------------------------===// |
| 5211 | |
| 5212 | namespace { |
| 5213 | |
| 5214 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5215 | bool HasVector; |
| 5216 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5217 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5218 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5219 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5220 | |
| 5221 | bool isPromotableIntegerType(QualType Ty) const; |
| 5222 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5223 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5224 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5225 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5226 | |
| 5227 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5228 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5229 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5230 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5231 | if (!getCXXABI().classifyReturnType(FI)) |
| 5232 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5233 | for (auto &I : FI.arguments()) |
| 5234 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5237 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5238 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5239 | }; |
| 5240 | |
| 5241 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5242 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5243 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5244 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5245 | }; |
| 5246 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5247 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5248 | |
| 5249 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5250 | // Treat an enum type as its underlying type. |
| 5251 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5252 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5253 | |
| 5254 | // Promotable integer types are required to be promoted by the ABI. |
| 5255 | if (Ty->isPromotableIntegerType()) |
| 5256 | return true; |
| 5257 | |
| 5258 | // 32-bit values must also be promoted. |
| 5259 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5260 | switch (BT->getKind()) { |
| 5261 | case BuiltinType::Int: |
| 5262 | case BuiltinType::UInt: |
| 5263 | return true; |
| 5264 | default: |
| 5265 | return false; |
| 5266 | } |
| 5267 | return false; |
| 5268 | } |
| 5269 | |
| 5270 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5271 | return (Ty->isAnyComplexType() || |
| 5272 | Ty->isVectorType() || |
| 5273 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5274 | } |
| 5275 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5276 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5277 | return (HasVector && |
| 5278 | Ty->isVectorType() && |
| 5279 | getContext().getTypeSize(Ty) <= 128); |
| 5280 | } |
| 5281 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5282 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5283 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5284 | switch (BT->getKind()) { |
| 5285 | case BuiltinType::Float: |
| 5286 | case BuiltinType::Double: |
| 5287 | return true; |
| 5288 | default: |
| 5289 | return false; |
| 5290 | } |
| 5291 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5292 | return false; |
| 5293 | } |
| 5294 | |
| 5295 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5296 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5297 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5298 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5299 | |
| 5300 | // If this is a C++ record, check the bases first. |
| 5301 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5302 | for (const auto &I : CXXRD->bases()) { |
| 5303 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5304 | |
| 5305 | // Empty bases don't affect things either way. |
| 5306 | if (isEmptyRecord(getContext(), Base, true)) |
| 5307 | continue; |
| 5308 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5309 | if (!Found.isNull()) |
| 5310 | return Ty; |
| 5311 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5315 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5316 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5317 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5318 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5319 | if (getContext().getLangOpts().CPlusPlus && |
| 5320 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5321 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5322 | |
| 5323 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5324 | // Nested structures still do though. |
| 5325 | if (!Found.isNull()) |
| 5326 | return Ty; |
| 5327 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5331 | // An 8-byte aligned struct s { float f; } is passed as a double. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5332 | if (!Found.isNull()) |
| 5333 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5336 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5340 | CodeGenFunction &CGF) const { |
| 5341 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5342 | // struct { |
| 5343 | // i64 __gpr; |
| 5344 | // i64 __fpr; |
| 5345 | // i8 *__overflow_arg_area; |
| 5346 | // i8 *__reg_save_area; |
| 5347 | // }; |
| 5348 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5349 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5350 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5351 | // always passed on the stack. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5352 | Ty = CGF.getContext().getCanonicalType(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5353 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
| 5354 | llvm::Type *APTy = llvm::PointerType::getUnqual(ArgTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5355 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5356 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5357 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5358 | bool IsVector = false; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5359 | unsigned UnpaddedBitSize; |
| 5360 | if (IsIndirect) { |
| 5361 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5362 | UnpaddedBitSize = 64; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5363 | } else { |
| 5364 | if (AI.getCoerceToType()) |
| 5365 | ArgTy = AI.getCoerceToType(); |
| 5366 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5367 | IsVector = ArgTy->isVectorTy(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5368 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5369 | } |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5370 | unsigned PaddedBitSize = (IsVector && UnpaddedBitSize > 64) ? 128 : 64; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5371 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5372 | |
| 5373 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5374 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5375 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5376 | llvm::Type *IndexTy = CGF.Int64Ty; |
| 5377 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5378 | |
| 5379 | if (IsVector) { |
| 5380 | // Work out the address of a vector argument on the stack. |
| 5381 | // Vector arguments are always passed in the high bits of a |
| 5382 | // single (8 byte) or double (16 byte) stack slot. |
| 5383 | llvm::Value *OverflowArgAreaPtr = |
| 5384 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 2, |
| 5385 | "overflow_arg_area_ptr"); |
| 5386 | llvm::Value *OverflowArgArea = |
| 5387 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5388 | llvm::Value *MemAddr = |
| 5389 | CGF.Builder.CreateBitCast(OverflowArgArea, APTy, "mem_addr"); |
| 5390 | |
| 5391 | // Update overflow_arg_area_ptr pointer |
| 5392 | llvm::Value *NewOverflowArgArea = |
| 5393 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5394 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5395 | |
| 5396 | return MemAddr; |
| 5397 | } |
| 5398 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5399 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5400 | if (InFPRs) { |
| 5401 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5402 | RegCountField = 1; // __fpr |
| 5403 | RegSaveIndex = 16; // save offset for f0 |
| 5404 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5405 | } else { |
| 5406 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5407 | RegCountField = 0; // __gpr |
| 5408 | RegSaveIndex = 2; // save offset for r2 |
| 5409 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5410 | } |
| 5411 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5412 | llvm::Value *RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5413 | nullptr, VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5414 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5415 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5416 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5417 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5418 | |
| 5419 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5420 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5421 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5422 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5423 | |
| 5424 | // Emit code to load the value if it was passed in registers. |
| 5425 | CGF.EmitBlock(InRegBlock); |
| 5426 | |
| 5427 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5428 | llvm::Value *ScaledRegCount = |
| 5429 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5430 | llvm::Value *RegBase = |
| 5431 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5432 | llvm::Value *RegOffset = |
| 5433 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5434 | llvm::Value *RegSaveAreaPtr = |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5435 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5436 | llvm::Value *RegSaveArea = |
| 5437 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5438 | llvm::Value *RawRegAddr = |
| 5439 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5440 | llvm::Value *RegAddr = |
| 5441 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5442 | |
| 5443 | // Update the register count |
| 5444 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5445 | llvm::Value *NewRegCount = |
| 5446 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5447 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5448 | CGF.EmitBranch(ContBlock); |
| 5449 | |
| 5450 | // Emit code to load the value if it was passed in memory. |
| 5451 | CGF.EmitBlock(InMemBlock); |
| 5452 | |
| 5453 | // Work out the address of a stack argument. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5454 | llvm::Value *OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5455 | nullptr, VAListAddr, 2, "overflow_arg_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5456 | llvm::Value *OverflowArgArea = |
| 5457 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5458 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5459 | llvm::Value *RawMemAddr = |
| 5460 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5461 | llvm::Value *MemAddr = |
| 5462 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5463 | |
| 5464 | // Update overflow_arg_area_ptr pointer |
| 5465 | llvm::Value *NewOverflowArgArea = |
| 5466 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5467 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5468 | CGF.EmitBranch(ContBlock); |
| 5469 | |
| 5470 | // Return the appropriate result. |
| 5471 | CGF.EmitBlock(ContBlock); |
| 5472 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5473 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5474 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5475 | |
| 5476 | if (IsIndirect) |
| 5477 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5478 | |
| 5479 | return ResAddr; |
| 5480 | } |
| 5481 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5482 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5483 | if (RetTy->isVoidType()) |
| 5484 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5485 | if (isVectorArgumentType(RetTy)) |
| 5486 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5487 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5488 | return ABIArgInfo::getIndirect(0); |
| 5489 | return (isPromotableIntegerType(RetTy) ? |
| 5490 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5491 | } |
| 5492 | |
| 5493 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5494 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5495 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5496 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5497 | |
| 5498 | // Integers and enums are extended to full register width. |
| 5499 | if (isPromotableIntegerType(Ty)) |
| 5500 | return ABIArgInfo::getExtend(); |
| 5501 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5502 | // Handle vector types and vector-like structure types. Note that |
| 5503 | // as opposed to float-like structure types, we do not allow any |
| 5504 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5505 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5506 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5507 | if (isVectorArgumentType(SingleElementTy) && |
| 5508 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5509 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5510 | |
| 5511 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5512 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5513 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5514 | |
| 5515 | // Handle small structures. |
| 5516 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5517 | // Structures with flexible arrays have variable length, so really |
| 5518 | // fail the size test above. |
| 5519 | const RecordDecl *RD = RT->getDecl(); |
| 5520 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5521 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5522 | |
| 5523 | // The structure is passed as an unextended integer, a float, or a double. |
| 5524 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5525 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5526 | assert(Size == 32 || Size == 64); |
| 5527 | if (Size == 32) |
| 5528 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5529 | else |
| 5530 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5531 | } else |
| 5532 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5533 | return ABIArgInfo::getDirect(PassTy); |
| 5534 | } |
| 5535 | |
| 5536 | // Non-structure compounds are passed indirectly. |
| 5537 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5538 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5539 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5540 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5541 | } |
| 5542 | |
| 5543 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5544 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5545 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5546 | |
| 5547 | namespace { |
| 5548 | |
| 5549 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5550 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5551 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5552 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5553 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5554 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5555 | }; |
| 5556 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5557 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5558 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5559 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5560 | llvm::GlobalValue *GV, |
| 5561 | CodeGen::CodeGenModule &M) const { |
| 5562 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5563 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5564 | // Handle 'interrupt' attribute: |
| 5565 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5566 | |
| 5567 | // Step 1: Set ISR calling convention. |
| 5568 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5569 | |
| 5570 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5571 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5572 | |
| 5573 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5574 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5575 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5576 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5577 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5578 | } |
| 5579 | } |
| 5580 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5581 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5582 | // MIPS ABI Implementation. This works for both little-endian and |
| 5583 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5584 | //===----------------------------------------------------------------------===// |
| 5585 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5586 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5587 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5588 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5589 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5590 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5591 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5592 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5593 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5594 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5595 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5596 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5597 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5598 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5599 | |
| 5600 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5601 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5602 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5603 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5604 | CodeGenFunction &CGF) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5605 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5606 | }; |
| 5607 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5608 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5609 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5610 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5611 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5612 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5613 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5614 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5615 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5616 | return 29; |
| 5617 | } |
| 5618 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5619 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5620 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5621 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5622 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5623 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5624 | if (FD->hasAttr<Mips16Attr>()) { |
| 5625 | Fn->addFnAttr("mips16"); |
| 5626 | } |
| 5627 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5628 | Fn->addFnAttr("nomips16"); |
| 5629 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5630 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5631 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5632 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5633 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5634 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5635 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5636 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5637 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5638 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5639 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5640 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5641 | void MipsABIInfo::CoerceToIntArgs( |
| 5642 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5643 | llvm::IntegerType *IntTy = |
| 5644 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5645 | |
| 5646 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5647 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5648 | ArgList.push_back(IntTy); |
| 5649 | |
| 5650 | // If necessary, add one more integer type to ArgList. |
| 5651 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5652 | |
| 5653 | if (R) |
| 5654 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5657 | // In N32/64, an aligned double precision floating point field is passed in |
| 5658 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5659 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5660 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5661 | |
| 5662 | if (IsO32) { |
| 5663 | CoerceToIntArgs(TySize, ArgList); |
| 5664 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5665 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5666 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5667 | if (Ty->isComplexType()) |
| 5668 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5669 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5670 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5671 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5672 | // Unions/vectors are passed in integer registers. |
| 5673 | if (!RT || !RT->isStructureOrClassType()) { |
| 5674 | CoerceToIntArgs(TySize, ArgList); |
| 5675 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5676 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5677 | |
| 5678 | const RecordDecl *RD = RT->getDecl(); |
| 5679 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5680 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5681 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5682 | uint64_t LastOffset = 0; |
| 5683 | unsigned idx = 0; |
| 5684 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5685 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5686 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5687 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5688 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5689 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5690 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5691 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5692 | |
| 5693 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5694 | continue; |
| 5695 | |
| 5696 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5697 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5698 | continue; |
| 5699 | |
| 5700 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5701 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5702 | ArgList.push_back(I64); |
| 5703 | |
| 5704 | // Add double type. |
| 5705 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5706 | LastOffset = Offset + 64; |
| 5707 | } |
| 5708 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5709 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5710 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5711 | |
| 5712 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5713 | } |
| 5714 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5715 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5716 | uint64_t Offset) const { |
| 5717 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5718 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5719 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5720 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5721 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5722 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5723 | ABIArgInfo |
| 5724 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5725 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5726 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5727 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5728 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5729 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5730 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5731 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5732 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5733 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5734 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5735 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5736 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5737 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5738 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5739 | return ABIArgInfo::getIgnore(); |
| 5740 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5741 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5742 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5743 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5744 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5745 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5746 | // If we have reached here, aggregates are passed directly by coercing to |
| 5747 | // another structure type. Padding is inserted if the offset of the |
| 5748 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 5749 | ABIArgInfo ArgInfo = |
| 5750 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 5751 | getPaddingType(OrigOffset, CurrOffset)); |
| 5752 | ArgInfo.setInReg(true); |
| 5753 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | // Treat an enum type as its underlying type. |
| 5757 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5758 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5759 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 5760 | // All integral types are promoted to the GPR width. |
| 5761 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5762 | return ABIArgInfo::getExtend(); |
| 5763 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5764 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5765 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5768 | llvm::Type* |
| 5769 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5770 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5771 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5772 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5773 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5774 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5775 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5776 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5777 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5778 | // N32/64 returns struct/classes in floating point registers if the |
| 5779 | // following conditions are met: |
| 5780 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5781 | // 2. The struct/class has one or two fields all of which are floating |
| 5782 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5783 | // 3. The offset of the first field is zero (this follows what gcc does). |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5784 | // |
| 5785 | // Any other composite results are returned in integer registers. |
| 5786 | // |
| 5787 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5788 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5789 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5790 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5791 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5792 | if (!BT || !BT->isFloatingPoint()) |
| 5793 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5794 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5795 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
| 5798 | if (b == e) |
| 5799 | return llvm::StructType::get(getVMContext(), RTList, |
| 5800 | RD->hasAttr<PackedAttr>()); |
| 5801 | |
| 5802 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5803 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5806 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5807 | return llvm::StructType::get(getVMContext(), RTList); |
| 5808 | } |
| 5809 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5810 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5811 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5812 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5813 | if (RetTy->isVoidType()) |
| 5814 | return ABIArgInfo::getIgnore(); |
| 5815 | |
| 5816 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5817 | // However, N32/N64 ignores zero sized return values. |
| 5818 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5819 | return ABIArgInfo::getIgnore(); |
| 5820 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5821 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5822 | if (Size <= 128) { |
| 5823 | if (RetTy->isAnyComplexType()) |
| 5824 | return ABIArgInfo::getDirect(); |
| 5825 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5826 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 5827 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5828 | if (!IsO32 || |
| 5829 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 5830 | ABIArgInfo ArgInfo = |
| 5831 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5832 | ArgInfo.setInReg(true); |
| 5833 | return ArgInfo; |
| 5834 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5835 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5836 | |
| 5837 | return ABIArgInfo::getIndirect(0); |
| 5838 | } |
| 5839 | |
| 5840 | // Treat an enum type as its underlying type. |
| 5841 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5842 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5843 | |
| 5844 | return (RetTy->isPromotableIntegerType() ? |
| 5845 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5846 | } |
| 5847 | |
| 5848 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5849 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5850 | if (!getCXXABI().classifyReturnType(FI)) |
| 5851 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5852 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5853 | // 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] | 5854 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5855 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5856 | for (auto &I : FI.arguments()) |
| 5857 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5861 | CodeGenFunction &CGF) const { |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5862 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5863 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5864 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5865 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 5866 | // 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] | 5867 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5868 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
| 5869 | if ((Ty->isIntegerType() && |
| 5870 | CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) || |
| 5871 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5872 | Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 5873 | Ty->isSignedIntegerType()); |
| 5874 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5875 | |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5876 | CGBuilderTy &Builder = CGF.Builder; |
| 5877 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5878 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Daniel Sanders | 8d36a61 | 2014-09-22 13:27:06 +0000 | [diff] [blame] | 5879 | int64_t TypeAlign = |
| 5880 | std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5881 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5882 | llvm::Value *AddrTyped; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5883 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
| 5884 | |
| 5885 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 5886 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5887 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5888 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5889 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
| 5890 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5891 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5892 | } |
| 5893 | else |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5894 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5895 | |
| 5896 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
| 5897 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5898 | unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty); |
| 5899 | uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5900 | llvm::Value *NextAddr = |
| 5901 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
| 5902 | "ap.next"); |
| 5903 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5904 | |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5905 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5906 | } |
| 5907 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5908 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 5909 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5910 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5911 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 5912 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 5913 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5914 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5915 | return false; |
| 5916 | } |
| 5917 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5918 | bool |
| 5919 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5920 | llvm::Value *Address) const { |
| 5921 | // This information comes from gcc's implementation, which seems to |
| 5922 | // as canonical as it gets. |
| 5923 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5924 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5925 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5926 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5927 | |
| 5928 | // 0-31 are the general purpose registers, $0 - $31. |
| 5929 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5930 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5931 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5932 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5933 | |
| 5934 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5935 | // They are one bit wide and ignored here. |
| 5936 | |
| 5937 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5938 | // (coprocessor 1 is the FP unit) |
| 5939 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5940 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5941 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5942 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5943 | return false; |
| 5944 | } |
| 5945 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5946 | //===----------------------------------------------------------------------===// |
| 5947 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5948 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5949 | // handling. |
| 5950 | //===----------------------------------------------------------------------===// |
| 5951 | |
| 5952 | namespace { |
| 5953 | |
| 5954 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5955 | public: |
| 5956 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5957 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5958 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5959 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5960 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5961 | }; |
| 5962 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5963 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5964 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5965 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5966 | if (!FD) return; |
| 5967 | |
| 5968 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5969 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5970 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5971 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5972 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5973 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5974 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5975 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5976 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5977 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5978 | llvm::NamedMDNode *OpenCLMetadata = |
| 5979 | M.getModule().getOrInsertNamedMetadata( |
| 5980 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5981 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5982 | SmallVector<llvm::Metadata *, 5> Operands; |
| 5983 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5984 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5985 | Operands.push_back( |
| 5986 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5987 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 5988 | Operands.push_back( |
| 5989 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5990 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 5991 | Operands.push_back( |
| 5992 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5993 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5994 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5995 | // Add a boolean constant operand for "required" (true) or "hint" |
| 5996 | // (false) for implementing the work_group_size_hint attr later. |
| 5997 | // Currently always true as the hint is not yet implemented. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5998 | Operands.push_back( |
| 5999 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6000 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 6001 | } |
| 6002 | } |
| 6003 | } |
| 6004 | } |
| 6005 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6006 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6007 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6008 | //===----------------------------------------------------------------------===// |
| 6009 | // Hexagon ABI Implementation |
| 6010 | //===----------------------------------------------------------------------===// |
| 6011 | |
| 6012 | namespace { |
| 6013 | |
| 6014 | class HexagonABIInfo : public ABIInfo { |
| 6015 | |
| 6016 | |
| 6017 | public: |
| 6018 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6019 | |
| 6020 | private: |
| 6021 | |
| 6022 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6023 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6024 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6025 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6026 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6027 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6028 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6029 | }; |
| 6030 | |
| 6031 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6032 | public: |
| 6033 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6034 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6035 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6036 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6037 | return 29; |
| 6038 | } |
| 6039 | }; |
| 6040 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6041 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6042 | |
| 6043 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6044 | if (!getCXXABI().classifyReturnType(FI)) |
| 6045 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6046 | for (auto &I : FI.arguments()) |
| 6047 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6051 | if (!isAggregateTypeForABI(Ty)) { |
| 6052 | // Treat an enum type as its underlying type. |
| 6053 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6054 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6055 | |
| 6056 | return (Ty->isPromotableIntegerType() ? |
| 6057 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6058 | } |
| 6059 | |
| 6060 | // Ignore empty records. |
| 6061 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6062 | return ABIArgInfo::getIgnore(); |
| 6063 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6064 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 6065 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6066 | |
| 6067 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6068 | if (Size > 64) |
| 6069 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6070 | // Pass in the smallest viable integer type. |
| 6071 | else if (Size > 32) |
| 6072 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6073 | else if (Size > 16) |
| 6074 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6075 | else if (Size > 8) |
| 6076 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6077 | else |
| 6078 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6079 | } |
| 6080 | |
| 6081 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6082 | if (RetTy->isVoidType()) |
| 6083 | return ABIArgInfo::getIgnore(); |
| 6084 | |
| 6085 | // Large vector types should be returned via memory. |
| 6086 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 6087 | return ABIArgInfo::getIndirect(0); |
| 6088 | |
| 6089 | if (!isAggregateTypeForABI(RetTy)) { |
| 6090 | // Treat an enum type as its underlying type. |
| 6091 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6092 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6093 | |
| 6094 | return (RetTy->isPromotableIntegerType() ? |
| 6095 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6096 | } |
| 6097 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6098 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6099 | return ABIArgInfo::getIgnore(); |
| 6100 | |
| 6101 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6102 | // are returned indirectly. |
| 6103 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6104 | if (Size <= 64) { |
| 6105 | // Return in the smallest viable integer type. |
| 6106 | if (Size <= 8) |
| 6107 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6108 | if (Size <= 16) |
| 6109 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6110 | if (Size <= 32) |
| 6111 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6112 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6113 | } |
| 6114 | |
| 6115 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6116 | } |
| 6117 | |
| 6118 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6119 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6120 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6121 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6122 | |
| 6123 | CGBuilderTy &Builder = CGF.Builder; |
| 6124 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 6125 | "ap"); |
| 6126 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6127 | llvm::Type *PTy = |
| 6128 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 6129 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 6130 | |
| 6131 | uint64_t Offset = |
| 6132 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 6133 | llvm::Value *NextAddr = |
| 6134 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 6135 | "ap.next"); |
| 6136 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 6137 | |
| 6138 | return AddrTyped; |
| 6139 | } |
| 6140 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6141 | //===----------------------------------------------------------------------===// |
| 6142 | // AMDGPU ABI Implementation |
| 6143 | //===----------------------------------------------------------------------===// |
| 6144 | |
| 6145 | namespace { |
| 6146 | |
| 6147 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6148 | public: |
| 6149 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6150 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6151 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6152 | CodeGen::CodeGenModule &M) const override; |
| 6153 | }; |
| 6154 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6155 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6156 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6157 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6158 | const Decl *D, |
| 6159 | llvm::GlobalValue *GV, |
| 6160 | CodeGen::CodeGenModule &M) const { |
| 6161 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 6162 | if (!FD) |
| 6163 | return; |
| 6164 | |
| 6165 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6166 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6167 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6168 | if (NumVGPR != 0) |
| 6169 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6170 | } |
| 6171 | |
| 6172 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6173 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6174 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6175 | if (NumSGPR != 0) |
| 6176 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6177 | } |
| 6178 | } |
| 6179 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6180 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6181 | //===----------------------------------------------------------------------===// |
| 6182 | // SPARC v9 ABI Implementation. |
| 6183 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6184 | // |
| 6185 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6186 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6187 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6188 | // |
| 6189 | // One case requires special care: |
| 6190 | // |
| 6191 | // struct mixed { |
| 6192 | // int i; |
| 6193 | // float f; |
| 6194 | // }; |
| 6195 | // |
| 6196 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6197 | // parameter array, but the int is passed in an integer register, and the float |
| 6198 | // is passed in a floating point register. This is represented as two arguments |
| 6199 | // with the LLVM IR inreg attribute: |
| 6200 | // |
| 6201 | // declare void f(i32 inreg %i, float inreg %f) |
| 6202 | // |
| 6203 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6204 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6205 | // bytes. |
| 6206 | // |
| 6207 | namespace { |
| 6208 | class SparcV9ABIInfo : public ABIInfo { |
| 6209 | public: |
| 6210 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6211 | |
| 6212 | private: |
| 6213 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6214 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6215 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6216 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6217 | |
| 6218 | // Coercion type builder for structs passed in registers. The coercion type |
| 6219 | // serves two purposes: |
| 6220 | // |
| 6221 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6222 | // in registers. |
| 6223 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6224 | // code generator knows to pass them in floating point registers. |
| 6225 | // |
| 6226 | // We also compute the InReg flag which indicates that the struct contains |
| 6227 | // aligned 32-bit floats. |
| 6228 | // |
| 6229 | struct CoerceBuilder { |
| 6230 | llvm::LLVMContext &Context; |
| 6231 | const llvm::DataLayout &DL; |
| 6232 | SmallVector<llvm::Type*, 8> Elems; |
| 6233 | uint64_t Size; |
| 6234 | bool InReg; |
| 6235 | |
| 6236 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6237 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6238 | |
| 6239 | // Pad Elems with integers until Size is ToSize. |
| 6240 | void pad(uint64_t ToSize) { |
| 6241 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6242 | if (ToSize == Size) |
| 6243 | return; |
| 6244 | |
| 6245 | // Finish the current 64-bit word. |
| 6246 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6247 | if (Aligned > Size && Aligned <= ToSize) { |
| 6248 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6249 | Size = Aligned; |
| 6250 | } |
| 6251 | |
| 6252 | // Add whole 64-bit words. |
| 6253 | while (Size + 64 <= ToSize) { |
| 6254 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6255 | Size += 64; |
| 6256 | } |
| 6257 | |
| 6258 | // Final in-word padding. |
| 6259 | if (Size < ToSize) { |
| 6260 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6261 | Size = ToSize; |
| 6262 | } |
| 6263 | } |
| 6264 | |
| 6265 | // Add a floating point element at Offset. |
| 6266 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6267 | // Unaligned floats are treated as integers. |
| 6268 | if (Offset % Bits) |
| 6269 | return; |
| 6270 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6271 | if (Bits < 64) |
| 6272 | InReg = true; |
| 6273 | pad(Offset); |
| 6274 | Elems.push_back(Ty); |
| 6275 | Size = Offset + Bits; |
| 6276 | } |
| 6277 | |
| 6278 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6279 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6280 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6281 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6282 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6283 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6284 | switch (ElemTy->getTypeID()) { |
| 6285 | case llvm::Type::StructTyID: |
| 6286 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6287 | break; |
| 6288 | case llvm::Type::FloatTyID: |
| 6289 | addFloat(ElemOffset, ElemTy, 32); |
| 6290 | break; |
| 6291 | case llvm::Type::DoubleTyID: |
| 6292 | addFloat(ElemOffset, ElemTy, 64); |
| 6293 | break; |
| 6294 | case llvm::Type::FP128TyID: |
| 6295 | addFloat(ElemOffset, ElemTy, 128); |
| 6296 | break; |
| 6297 | case llvm::Type::PointerTyID: |
| 6298 | if (ElemOffset % 64 == 0) { |
| 6299 | pad(ElemOffset); |
| 6300 | Elems.push_back(ElemTy); |
| 6301 | Size += 64; |
| 6302 | } |
| 6303 | break; |
| 6304 | default: |
| 6305 | break; |
| 6306 | } |
| 6307 | } |
| 6308 | } |
| 6309 | |
| 6310 | // Check if Ty is a usable substitute for the coercion type. |
| 6311 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6312 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6313 | } |
| 6314 | |
| 6315 | // Get the coercion type as a literal struct type. |
| 6316 | llvm::Type *getType() const { |
| 6317 | if (Elems.size() == 1) |
| 6318 | return Elems.front(); |
| 6319 | else |
| 6320 | return llvm::StructType::get(Context, Elems); |
| 6321 | } |
| 6322 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6323 | }; |
| 6324 | } // end anonymous namespace |
| 6325 | |
| 6326 | ABIArgInfo |
| 6327 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6328 | if (Ty->isVoidType()) |
| 6329 | return ABIArgInfo::getIgnore(); |
| 6330 | |
| 6331 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6332 | |
| 6333 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6334 | // pointer / sret pointer. |
| 6335 | if (Size > SizeLimit) |
| 6336 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 6337 | |
| 6338 | // Treat an enum type as its underlying type. |
| 6339 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6340 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6341 | |
| 6342 | // Integer types smaller than a register are extended. |
| 6343 | if (Size < 64 && Ty->isIntegerType()) |
| 6344 | return ABIArgInfo::getExtend(); |
| 6345 | |
| 6346 | // Other non-aggregates go in registers. |
| 6347 | if (!isAggregateTypeForABI(Ty)) |
| 6348 | return ABIArgInfo::getDirect(); |
| 6349 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6350 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6351 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6352 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 6353 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 6354 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6355 | // 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] | 6356 | // Build a coercion type from the LLVM struct type. |
| 6357 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6358 | if (!StrTy) |
| 6359 | return ABIArgInfo::getDirect(); |
| 6360 | |
| 6361 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6362 | CB.addStruct(0, StrTy); |
| 6363 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6364 | |
| 6365 | // Try to use the original type for coercion. |
| 6366 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6367 | |
| 6368 | if (CB.InReg) |
| 6369 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6370 | else |
| 6371 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6375 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6376 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6377 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6378 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6379 | AI.setCoerceToType(ArgTy); |
| 6380 | |
| 6381 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6382 | CGBuilderTy &Builder = CGF.Builder; |
| 6383 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6384 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6385 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6386 | llvm::Value *ArgAddr; |
| 6387 | unsigned Stride; |
| 6388 | |
| 6389 | switch (AI.getKind()) { |
| 6390 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6391 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6392 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6393 | |
| 6394 | case ABIArgInfo::Extend: |
| 6395 | Stride = 8; |
| 6396 | ArgAddr = Builder |
| 6397 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6398 | "extend"); |
| 6399 | break; |
| 6400 | |
| 6401 | case ABIArgInfo::Direct: |
| 6402 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6403 | ArgAddr = Addr; |
| 6404 | break; |
| 6405 | |
| 6406 | case ABIArgInfo::Indirect: |
| 6407 | Stride = 8; |
| 6408 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6409 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6410 | "indirect"); |
| 6411 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6412 | break; |
| 6413 | |
| 6414 | case ABIArgInfo::Ignore: |
| 6415 | return llvm::UndefValue::get(ArgPtrTy); |
| 6416 | } |
| 6417 | |
| 6418 | // Update VAList. |
| 6419 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6420 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6421 | |
| 6422 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6423 | } |
| 6424 | |
| 6425 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6426 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6427 | for (auto &I : FI.arguments()) |
| 6428 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6429 | } |
| 6430 | |
| 6431 | namespace { |
| 6432 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6433 | public: |
| 6434 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6435 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6436 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6437 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6438 | return 14; |
| 6439 | } |
| 6440 | |
| 6441 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6442 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6443 | }; |
| 6444 | } // end anonymous namespace |
| 6445 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6446 | bool |
| 6447 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6448 | llvm::Value *Address) const { |
| 6449 | // This is calculated from the LLVM and GCC tables and verified |
| 6450 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6451 | |
| 6452 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6453 | |
| 6454 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6455 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6456 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6457 | |
| 6458 | // 0-31: the 8-byte general-purpose registers |
| 6459 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6460 | |
| 6461 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6462 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6463 | |
| 6464 | // Y = 64 |
| 6465 | // PSR = 65 |
| 6466 | // WIM = 66 |
| 6467 | // TBR = 67 |
| 6468 | // PC = 68 |
| 6469 | // NPC = 69 |
| 6470 | // FSR = 70 |
| 6471 | // CSR = 71 |
| 6472 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6473 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6474 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6475 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6476 | |
| 6477 | return false; |
| 6478 | } |
| 6479 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6480 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6481 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6482 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6483 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6484 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6485 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6486 | |
| 6487 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6488 | /// it by reference between functions that append to it. |
| 6489 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6490 | |
| 6491 | /// TypeStringCache caches the meta encodings of Types. |
| 6492 | /// |
| 6493 | /// The reason for caching TypeStrings is two fold: |
| 6494 | /// 1. To cache a type's encoding for later uses; |
| 6495 | /// 2. As a means to break recursive member type inclusion. |
| 6496 | /// |
| 6497 | /// A cache Entry can have a Status of: |
| 6498 | /// NonRecursive: The type encoding is not recursive; |
| 6499 | /// Recursive: The type encoding is recursive; |
| 6500 | /// Incomplete: An incomplete TypeString; |
| 6501 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6502 | /// Recursive type encoding. |
| 6503 | /// |
| 6504 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6505 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6506 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6507 | /// the type is encountered. |
| 6508 | /// |
| 6509 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6510 | /// possible. The type itself is recursive and it may contain other types which |
| 6511 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6512 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6513 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6514 | /// |
| 6515 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6516 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6517 | /// are placed into the cache during type expansion as a means to identify and |
| 6518 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6519 | /// the entry becomes IncompleteUsed. |
| 6520 | /// |
| 6521 | /// During the expansion of a RecordType's members: |
| 6522 | /// |
| 6523 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6524 | /// cached encoding is used; |
| 6525 | /// |
| 6526 | /// If the cache contains a Recursive encoding for the member type, the |
| 6527 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6528 | /// |
| 6529 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6530 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6531 | /// |
| 6532 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6533 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6534 | /// it is swapped back in; |
| 6535 | /// |
| 6536 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6537 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6538 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6539 | /// |
| 6540 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6541 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6542 | /// Else the member is part of a recursive type and thus the recursion has |
| 6543 | /// been exited too soon for the encoding to be correct for the member. |
| 6544 | /// |
| 6545 | class TypeStringCache { |
| 6546 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6547 | struct Entry { |
| 6548 | std::string Str; // The encoded TypeString for the type. |
| 6549 | enum Status State; // Information about the encoding in 'Str'. |
| 6550 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6551 | // during the expansion of RecordType's members. |
| 6552 | }; |
| 6553 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6554 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6555 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6556 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6557 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6558 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6559 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6560 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6561 | bool IsRecursive); |
| 6562 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6563 | }; |
| 6564 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6565 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6566 | /// FieldEncoding is a helper for this ordering process. |
| 6567 | class FieldEncoding { |
| 6568 | bool HasName; |
| 6569 | std::string Enc; |
| 6570 | public: |
| 6571 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6572 | StringRef str() {return Enc.c_str();}; |
| 6573 | bool operator<(const FieldEncoding &rhs) const { |
| 6574 | if (HasName != rhs.HasName) return HasName; |
| 6575 | return Enc < rhs.Enc; |
| 6576 | } |
| 6577 | }; |
| 6578 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6579 | class XCoreABIInfo : public DefaultABIInfo { |
| 6580 | public: |
| 6581 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6582 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6583 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6584 | }; |
| 6585 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6586 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6587 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6588 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6589 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6590 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6591 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6592 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6593 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6594 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6595 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6596 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6597 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6598 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6599 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6600 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6601 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6602 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6603 | CGF.Int8PtrPtrTy); |
| 6604 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6605 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6606 | // Handle the argument. |
| 6607 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6608 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6609 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6610 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6611 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6612 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6613 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6614 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6615 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6616 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6617 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6618 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6619 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6620 | ArgSize = 0; |
| 6621 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6622 | case ABIArgInfo::Extend: |
| 6623 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6624 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6625 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6626 | if (ArgSize < 4) |
| 6627 | ArgSize = 4; |
| 6628 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6629 | case ABIArgInfo::Indirect: |
| 6630 | llvm::Value *ArgAddr; |
| 6631 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6632 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6633 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6634 | ArgSize = 4; |
| 6635 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6636 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6637 | |
| 6638 | // Increment the VAList. |
| 6639 | if (ArgSize) { |
| 6640 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6641 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6642 | } |
| 6643 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6644 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6645 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6646 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6647 | /// into the cache as a means to identify and break recursion. |
| 6648 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6649 | /// be reinserted by removeIncomplete(). |
| 6650 | /// All other types of encoding should have been used rather than arriving here. |
| 6651 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6652 | std::string StubEnc) { |
| 6653 | if (!ID) |
| 6654 | return; |
| 6655 | Entry &E = Map[ID]; |
| 6656 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6657 | "Incorrectly use of addIncomplete"); |
| 6658 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6659 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6660 | E.Str.swap(StubEnc); |
| 6661 | E.State = Incomplete; |
| 6662 | ++IncompleteCount; |
| 6663 | } |
| 6664 | |
| 6665 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6666 | /// must be removed from the cache. |
| 6667 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6668 | /// Returns true if the RecordType was defined recursively. |
| 6669 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6670 | if (!ID) |
| 6671 | return false; |
| 6672 | auto I = Map.find(ID); |
| 6673 | assert(I != Map.end() && "Entry not present"); |
| 6674 | Entry &E = I->second; |
| 6675 | assert( (E.State == Incomplete || |
| 6676 | E.State == IncompleteUsed) && |
| 6677 | "Entry must be an incomplete type"); |
| 6678 | bool IsRecursive = false; |
| 6679 | if (E.State == IncompleteUsed) { |
| 6680 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6681 | IsRecursive = true; |
| 6682 | --IncompleteUsedCount; |
| 6683 | } |
| 6684 | if (E.Swapped.empty()) |
| 6685 | Map.erase(I); |
| 6686 | else { |
| 6687 | // Swap the Recursive back. |
| 6688 | E.Swapped.swap(E.Str); |
| 6689 | E.Swapped.clear(); |
| 6690 | E.State = Recursive; |
| 6691 | } |
| 6692 | --IncompleteCount; |
| 6693 | return IsRecursive; |
| 6694 | } |
| 6695 | |
| 6696 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6697 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6698 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6699 | bool IsRecursive) { |
| 6700 | if (!ID || IncompleteUsedCount) |
| 6701 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6702 | Entry &E = Map[ID]; |
| 6703 | if (IsRecursive && !E.Str.empty()) { |
| 6704 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6705 | "This is not the same Recursive entry"); |
| 6706 | // The parent container was not recursive after all, so we could have used |
| 6707 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6708 | // we started viz: IncompleteCount!=0. |
| 6709 | return; |
| 6710 | } |
| 6711 | assert(E.Str.empty() && "Entry already present"); |
| 6712 | E.Str = Str.str(); |
| 6713 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6714 | } |
| 6715 | |
| 6716 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6717 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6718 | /// encoding is Recursive, return an empty StringRef. |
| 6719 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6720 | if (!ID) |
| 6721 | return StringRef(); // We have no key. |
| 6722 | auto I = Map.find(ID); |
| 6723 | if (I == Map.end()) |
| 6724 | return StringRef(); // We have no encoding. |
| 6725 | Entry &E = I->second; |
| 6726 | if (E.State == Recursive && IncompleteCount) |
| 6727 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6728 | |
| 6729 | if (E.State == Incomplete) { |
| 6730 | // The incomplete type is being used to break out of recursion. |
| 6731 | E.State = IncompleteUsed; |
| 6732 | ++IncompleteUsedCount; |
| 6733 | } |
| 6734 | return E.Str.c_str(); |
| 6735 | } |
| 6736 | |
| 6737 | /// The XCore ABI includes a type information section that communicates symbol |
| 6738 | /// type information to the linker. The linker uses this information to verify |
| 6739 | /// safety/correctness of things such as array bound and pointers et al. |
| 6740 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6741 | /// This type information (TypeString) is emitted into meta data for all global |
| 6742 | /// symbols: definitions, declarations, functions & variables. |
| 6743 | /// |
| 6744 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6745 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6746 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6747 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6748 | /// |
| 6749 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6750 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6751 | |
| 6752 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6753 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6754 | CodeGen::CodeGenModule &CGM) const { |
| 6755 | SmallStringEnc Enc; |
| 6756 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6757 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6758 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 6759 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6760 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6761 | llvm::NamedMDNode *MD = |
| 6762 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6763 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6764 | } |
| 6765 | } |
| 6766 | |
| 6767 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6768 | const CodeGen::CodeGenModule &CGM, |
| 6769 | TypeStringCache &TSC); |
| 6770 | |
| 6771 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6772 | /// Builds a SmallVector containing the encoded field types in declaration |
| 6773 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6774 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6775 | const RecordDecl *RD, |
| 6776 | const CodeGen::CodeGenModule &CGM, |
| 6777 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6778 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6779 | SmallStringEnc Enc; |
| 6780 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6781 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6782 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6783 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6784 | Enc += "b("; |
| 6785 | llvm::raw_svector_ostream OS(Enc); |
| 6786 | OS.resync(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6787 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6788 | OS.flush(); |
| 6789 | Enc += ':'; |
| 6790 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6791 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6792 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6793 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6794 | Enc += ')'; |
| 6795 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 6796 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6797 | } |
| 6798 | return true; |
| 6799 | } |
| 6800 | |
| 6801 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6802 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6803 | /// Union types have their fields ordered according to the ABI. |
| 6804 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6805 | const CodeGen::CodeGenModule &CGM, |
| 6806 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6807 | // Append the cached TypeString if we have one. |
| 6808 | StringRef TypeString = TSC.lookupStr(ID); |
| 6809 | if (!TypeString.empty()) { |
| 6810 | Enc += TypeString; |
| 6811 | return true; |
| 6812 | } |
| 6813 | |
| 6814 | // Start to emit an incomplete TypeString. |
| 6815 | size_t Start = Enc.size(); |
| 6816 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6817 | Enc += '('; |
| 6818 | if (ID) |
| 6819 | Enc += ID->getName(); |
| 6820 | Enc += "){"; |
| 6821 | |
| 6822 | // We collect all encoded fields and order as necessary. |
| 6823 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6824 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6825 | if (RD && !RD->field_empty()) { |
| 6826 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6827 | // so that recursive calls to this RecordType will use it whilst building a |
| 6828 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6829 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6830 | std::string StubEnc(Enc.substr(Start).str()); |
| 6831 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6832 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6833 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6834 | (void) TSC.removeIncomplete(ID); |
| 6835 | return false; |
| 6836 | } |
| 6837 | IsRecursive = TSC.removeIncomplete(ID); |
| 6838 | // The ABI requires unions to be sorted but not structures. |
| 6839 | // See FieldEncoding::operator< for sort algorithm. |
| 6840 | if (RT->isUnionType()) |
| 6841 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6842 | // We can now complete the TypeString. |
| 6843 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6844 | for (unsigned I = 0; I != E; ++I) { |
| 6845 | if (I) |
| 6846 | Enc += ','; |
| 6847 | Enc += FE[I].str(); |
| 6848 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6849 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6850 | Enc += '}'; |
| 6851 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6852 | return true; |
| 6853 | } |
| 6854 | |
| 6855 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6856 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6857 | TypeStringCache &TSC, |
| 6858 | const IdentifierInfo *ID) { |
| 6859 | // Append the cached TypeString if we have one. |
| 6860 | StringRef TypeString = TSC.lookupStr(ID); |
| 6861 | if (!TypeString.empty()) { |
| 6862 | Enc += TypeString; |
| 6863 | return true; |
| 6864 | } |
| 6865 | |
| 6866 | size_t Start = Enc.size(); |
| 6867 | Enc += "e("; |
| 6868 | if (ID) |
| 6869 | Enc += ID->getName(); |
| 6870 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6871 | |
| 6872 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6873 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6874 | SmallVector<FieldEncoding, 16> FE; |
| 6875 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6876 | ++I) { |
| 6877 | SmallStringEnc EnumEnc; |
| 6878 | EnumEnc += "m("; |
| 6879 | EnumEnc += I->getName(); |
| 6880 | EnumEnc += "){"; |
| 6881 | I->getInitVal().toString(EnumEnc); |
| 6882 | EnumEnc += '}'; |
| 6883 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6884 | } |
| 6885 | std::sort(FE.begin(), FE.end()); |
| 6886 | unsigned E = FE.size(); |
| 6887 | for (unsigned I = 0; I != E; ++I) { |
| 6888 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6889 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6890 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6891 | } |
| 6892 | } |
| 6893 | Enc += '}'; |
| 6894 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6895 | return true; |
| 6896 | } |
| 6897 | |
| 6898 | /// Appends type's qualifier to Enc. |
| 6899 | /// This is done prior to appending the type's encoding. |
| 6900 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6901 | // Qualifiers are emitted in alphabetical order. |
| 6902 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6903 | int Lookup = 0; |
| 6904 | if (QT.isConstQualified()) |
| 6905 | Lookup += 1<<0; |
| 6906 | if (QT.isRestrictQualified()) |
| 6907 | Lookup += 1<<1; |
| 6908 | if (QT.isVolatileQualified()) |
| 6909 | Lookup += 1<<2; |
| 6910 | Enc += Table[Lookup]; |
| 6911 | } |
| 6912 | |
| 6913 | /// Appends built-in types to Enc. |
| 6914 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6915 | const char *EncType; |
| 6916 | switch (BT->getKind()) { |
| 6917 | case BuiltinType::Void: |
| 6918 | EncType = "0"; |
| 6919 | break; |
| 6920 | case BuiltinType::Bool: |
| 6921 | EncType = "b"; |
| 6922 | break; |
| 6923 | case BuiltinType::Char_U: |
| 6924 | EncType = "uc"; |
| 6925 | break; |
| 6926 | case BuiltinType::UChar: |
| 6927 | EncType = "uc"; |
| 6928 | break; |
| 6929 | case BuiltinType::SChar: |
| 6930 | EncType = "sc"; |
| 6931 | break; |
| 6932 | case BuiltinType::UShort: |
| 6933 | EncType = "us"; |
| 6934 | break; |
| 6935 | case BuiltinType::Short: |
| 6936 | EncType = "ss"; |
| 6937 | break; |
| 6938 | case BuiltinType::UInt: |
| 6939 | EncType = "ui"; |
| 6940 | break; |
| 6941 | case BuiltinType::Int: |
| 6942 | EncType = "si"; |
| 6943 | break; |
| 6944 | case BuiltinType::ULong: |
| 6945 | EncType = "ul"; |
| 6946 | break; |
| 6947 | case BuiltinType::Long: |
| 6948 | EncType = "sl"; |
| 6949 | break; |
| 6950 | case BuiltinType::ULongLong: |
| 6951 | EncType = "ull"; |
| 6952 | break; |
| 6953 | case BuiltinType::LongLong: |
| 6954 | EncType = "sll"; |
| 6955 | break; |
| 6956 | case BuiltinType::Float: |
| 6957 | EncType = "ft"; |
| 6958 | break; |
| 6959 | case BuiltinType::Double: |
| 6960 | EncType = "d"; |
| 6961 | break; |
| 6962 | case BuiltinType::LongDouble: |
| 6963 | EncType = "ld"; |
| 6964 | break; |
| 6965 | default: |
| 6966 | return false; |
| 6967 | } |
| 6968 | Enc += EncType; |
| 6969 | return true; |
| 6970 | } |
| 6971 | |
| 6972 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6973 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6974 | const CodeGen::CodeGenModule &CGM, |
| 6975 | TypeStringCache &TSC) { |
| 6976 | Enc += "p("; |
| 6977 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6978 | return false; |
| 6979 | Enc += ')'; |
| 6980 | return true; |
| 6981 | } |
| 6982 | |
| 6983 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6984 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6985 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6986 | const CodeGen::CodeGenModule &CGM, |
| 6987 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6988 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6989 | return false; |
| 6990 | Enc += "a("; |
| 6991 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6992 | CAT->getSize().toStringUnsigned(Enc); |
| 6993 | else |
| 6994 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6995 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6996 | // The Qualifiers should be attached to the type rather than the array. |
| 6997 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6998 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6999 | return false; |
| 7000 | Enc += ')'; |
| 7001 | return true; |
| 7002 | } |
| 7003 | |
| 7004 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 7005 | /// and the arguments. |
| 7006 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 7007 | const CodeGen::CodeGenModule &CGM, |
| 7008 | TypeStringCache &TSC) { |
| 7009 | Enc += "f{"; |
| 7010 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7011 | return false; |
| 7012 | Enc += "}("; |
| 7013 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7014 | // N.B. we are only interested in the adjusted param types. |
| 7015 | auto I = FPT->param_type_begin(); |
| 7016 | auto E = FPT->param_type_end(); |
| 7017 | if (I != E) { |
| 7018 | do { |
| 7019 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7020 | return false; |
| 7021 | ++I; |
| 7022 | if (I != E) |
| 7023 | Enc += ','; |
| 7024 | } while (I != E); |
| 7025 | if (FPT->isVariadic()) |
| 7026 | Enc += ",va"; |
| 7027 | } else { |
| 7028 | if (FPT->isVariadic()) |
| 7029 | Enc += "va"; |
| 7030 | else |
| 7031 | Enc += '0'; |
| 7032 | } |
| 7033 | } |
| 7034 | Enc += ')'; |
| 7035 | return true; |
| 7036 | } |
| 7037 | |
| 7038 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7039 | /// type encodings. |
| 7040 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7041 | const CodeGen::CodeGenModule &CGM, |
| 7042 | TypeStringCache &TSC) { |
| 7043 | |
| 7044 | QualType QT = QType.getCanonicalType(); |
| 7045 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7046 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7047 | // The Qualifiers should be attached to the type rather than the array. |
| 7048 | // Thus we don't call appendQualifier() here. |
| 7049 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7050 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7051 | appendQualifier(Enc, QT); |
| 7052 | |
| 7053 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7054 | return appendBuiltinType(Enc, BT); |
| 7055 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7056 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7057 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7058 | |
| 7059 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7060 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7061 | |
| 7062 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7063 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7064 | |
| 7065 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7066 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7067 | |
| 7068 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7069 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7070 | |
| 7071 | return false; |
| 7072 | } |
| 7073 | |
| 7074 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7075 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7076 | if (!D) |
| 7077 | return false; |
| 7078 | |
| 7079 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7080 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7081 | return false; |
| 7082 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7083 | } |
| 7084 | |
| 7085 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7086 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7087 | return false; |
| 7088 | QualType QT = VD->getType().getCanonicalType(); |
| 7089 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7090 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7091 | // The Qualifiers should be attached to the type rather than the array. |
| 7092 | // Thus we don't call appendQualifier() here. |
| 7093 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7094 | } |
| 7095 | return appendType(Enc, QT, CGM, TSC); |
| 7096 | } |
| 7097 | return false; |
| 7098 | } |
| 7099 | |
| 7100 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7101 | //===----------------------------------------------------------------------===// |
| 7102 | // Driver code |
| 7103 | //===----------------------------------------------------------------------===// |
| 7104 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7105 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7106 | return getTarget().getTriple(); |
| 7107 | } |
| 7108 | |
| 7109 | bool CodeGenModule::supportsCOMDAT() const { |
| 7110 | return !getTriple().isOSBinFormatMachO(); |
| 7111 | } |
| 7112 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7113 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7114 | if (TheTargetCodeGenInfo) |
| 7115 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7116 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7117 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7118 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7119 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7120 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7121 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7122 | case llvm::Triple::le32: |
| 7123 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7124 | case llvm::Triple::mips: |
| 7125 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7126 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7127 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7128 | case llvm::Triple::mips64: |
| 7129 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7130 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7131 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7132 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7133 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7134 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7135 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7136 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7137 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7138 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7139 | } |
| 7140 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7141 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7142 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7143 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7144 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7145 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7146 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7147 | TheTargetCodeGenInfo = |
| 7148 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7149 | return *TheTargetCodeGenInfo; |
| 7150 | } |
| 7151 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7152 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7153 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7154 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7155 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7156 | (CodeGenOpts.FloatABI != "soft" && |
| 7157 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7158 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7159 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7160 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7161 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7162 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7163 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7164 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7165 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7166 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7167 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7168 | if (getTarget().getABI() == "elfv2") |
| 7169 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7170 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7171 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7172 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7173 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7174 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7175 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7176 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7177 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7178 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7179 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7180 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7181 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7182 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7183 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7184 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7185 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7186 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7187 | case llvm::Triple::nvptx: |
| 7188 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7189 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7190 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7191 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7192 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7193 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7194 | case llvm::Triple::systemz: { |
| 7195 | bool HasVector = getTarget().getABI() == "vector"; |
| 7196 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7197 | HasVector)); |
| 7198 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7199 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7200 | case llvm::Triple::tce: |
| 7201 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7202 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7203 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7204 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 7205 | bool IsSmallStructInRegABI = |
| 7206 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7207 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7208 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7209 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7210 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
| 7211 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7212 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7213 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7214 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
| 7215 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7216 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7217 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7218 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7219 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7220 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7221 | StringRef ABI = getTarget().getABI(); |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 7222 | X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 : |
| 7223 | ABI == "avx" ? X86AVXABILevel::AVX : |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7224 | X86AVXABILevel::None); |
| 7225 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7226 | switch (Triple.getOS()) { |
| 7227 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7228 | return *(TheTargetCodeGenInfo = |
| 7229 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7230 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7231 | return *(TheTargetCodeGenInfo = |
| 7232 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7233 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7234 | return *(TheTargetCodeGenInfo = |
| 7235 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7236 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7237 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7238 | case llvm::Triple::hexagon: |
| 7239 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7240 | case llvm::Triple::r600: |
| 7241 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7242 | case llvm::Triple::amdgcn: |
| 7243 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7244 | case llvm::Triple::sparcv9: |
| 7245 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7246 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7247 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7248 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7249 | } |