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 { |
| 1580 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1581 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1582 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1583 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1584 | const X86_64ABIInfo &getABIInfo() const { |
| 1585 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1586 | } |
| 1587 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1588 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1589 | return 7; |
| 1590 | } |
| 1591 | |
| 1592 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1593 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1594 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1595 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1596 | // 0-15 are the 16 integer registers. |
| 1597 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1598 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1599 | return false; |
| 1600 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1601 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1602 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1603 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1604 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1605 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1606 | } |
| 1607 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1608 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1609 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1610 | // The default CC on x86-64 sets %al to the number of SSA |
| 1611 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1612 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1613 | // that when AVX types are involved: the ABI explicitly states it is |
| 1614 | // undefined, and it doesn't work in practice because of how the ABI |
| 1615 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1616 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1617 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1618 | for (CallArgList::const_iterator |
| 1619 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1620 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1621 | HasAVXType = true; |
| 1622 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1623 | } |
| 1624 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1625 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1626 | if (!HasAVXType) |
| 1627 | return true; |
| 1628 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1629 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1630 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1633 | llvm::Constant * |
| 1634 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1635 | unsigned Sig; |
| 1636 | if (getABIInfo().has64BitPointers()) |
| 1637 | Sig = (0xeb << 0) | // jmp rel8 |
| 1638 | (0x0a << 8) | // .+0x0c |
| 1639 | ('F' << 16) | |
| 1640 | ('T' << 24); |
| 1641 | else |
| 1642 | Sig = (0xeb << 0) | // jmp rel8 |
| 1643 | (0x06 << 8) | // .+0x08 |
| 1644 | ('F' << 16) | |
| 1645 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1646 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1647 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1648 | }; |
| 1649 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1650 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1651 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1652 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1653 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1654 | |
| 1655 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1656 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1657 | Opt = "\01"; |
Yunzhong Gao | d65200c | 2015-07-20 17:46:56 +0000 | [diff] [blame] | 1658 | // If the argument contains a space, enclose it in quotes. |
| 1659 | if (Lib.find(" ") != StringRef::npos) |
| 1660 | Opt += "\"" + Lib.str() + "\""; |
| 1661 | else |
| 1662 | Opt += Lib; |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1663 | } |
| 1664 | }; |
| 1665 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1666 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1667 | // If the argument does not end in .lib, automatically add the suffix. |
| 1668 | // If the argument contains a space, enclose it in quotes. |
| 1669 | // This matches the behavior of MSVC. |
| 1670 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1671 | std::string ArgStr = Quote ? "\"" : ""; |
| 1672 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1673 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1674 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1675 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1676 | return ArgStr; |
| 1677 | } |
| 1678 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1679 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1680 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1681 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1682 | bool d, bool p, bool w, unsigned RegParms) |
| 1683 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1684 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1685 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1686 | CodeGen::CodeGenModule &CGM) const override; |
| 1687 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1688 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1689 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1690 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1691 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1692 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1693 | |
| 1694 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1695 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1696 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1697 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1698 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1699 | }; |
| 1700 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1701 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1702 | llvm::GlobalValue *GV, |
| 1703 | CodeGen::CodeGenModule &CGM) { |
| 1704 | if (isa<FunctionDecl>(D)) { |
| 1705 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1706 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1707 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1708 | Fn->addFnAttr("stack-probe-size", |
| 1709 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1714 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1715 | llvm::GlobalValue *GV, |
| 1716 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1717 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1718 | |
| 1719 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1720 | } |
| 1721 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1722 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1723 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1724 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1725 | X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1726 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1727 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1728 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1729 | CodeGen::CodeGenModule &CGM) const override; |
| 1730 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1731 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1732 | return 7; |
| 1733 | } |
| 1734 | |
| 1735 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1736 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1737 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1738 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1739 | // 0-15 are the 16 integer registers. |
| 1740 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1741 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1742 | return false; |
| 1743 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1744 | |
| 1745 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1746 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1747 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1748 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1749 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1750 | |
| 1751 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1752 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1753 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1754 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1755 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1756 | }; |
| 1757 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1758 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1759 | llvm::GlobalValue *GV, |
| 1760 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1761 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1762 | |
| 1763 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1764 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1765 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1766 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1767 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1768 | Class &Hi) const { |
| 1769 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1770 | // |
| 1771 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1772 | // memory. |
| 1773 | // |
| 1774 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1775 | // memory. |
| 1776 | // |
| 1777 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1778 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1779 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1780 | // ABI working for processors that don't support the __m256 type. |
| 1781 | // |
| 1782 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1783 | // |
| 1784 | // Some of these are enforced by the merging logic. Others can arise |
| 1785 | // only with unions; for example: |
| 1786 | // union { _Complex double; unsigned; } |
| 1787 | // |
| 1788 | // Note that clauses (b) and (c) were added in 0.98. |
| 1789 | // |
| 1790 | if (Hi == Memory) |
| 1791 | Lo = Memory; |
| 1792 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1793 | Lo = Memory; |
| 1794 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1795 | Lo = Memory; |
| 1796 | if (Hi == SSEUp && Lo != SSE) |
| 1797 | Hi = SSE; |
| 1798 | } |
| 1799 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1800 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1801 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1802 | // classified recursively so that always two fields are |
| 1803 | // considered. The resulting class is calculated according to |
| 1804 | // the classes of the fields in the eightbyte: |
| 1805 | // |
| 1806 | // (a) If both classes are equal, this is the resulting class. |
| 1807 | // |
| 1808 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1809 | // the other class. |
| 1810 | // |
| 1811 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1812 | // class. |
| 1813 | // |
| 1814 | // (d) If one of the classes is INTEGER, the result is the |
| 1815 | // INTEGER. |
| 1816 | // |
| 1817 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1818 | // MEMORY is used as class. |
| 1819 | // |
| 1820 | // (f) Otherwise class SSE is used. |
| 1821 | |
| 1822 | // Accum should never be memory (we should have returned) or |
| 1823 | // ComplexX87 (because this cannot be passed in a structure). |
| 1824 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1825 | "Invalid accumulated classification during merge."); |
| 1826 | if (Accum == Field || Field == NoClass) |
| 1827 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1828 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1829 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1830 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1831 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1832 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1833 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1834 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1835 | Accum == X87 || Accum == X87Up) |
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 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1840 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1841 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1842 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1843 | // Class pairs with appropriate constructor methods for the various |
| 1844 | // situations. |
| 1845 | |
| 1846 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1847 | // shouldn't be passed in registers for example, so there is no chance they |
| 1848 | // can straddle an eightbyte. Verify & simplify. |
| 1849 | |
| 1850 | Lo = Hi = NoClass; |
| 1851 | |
| 1852 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1853 | Current = Memory; |
| 1854 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1855 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1856 | BuiltinType::Kind k = BT->getKind(); |
| 1857 | |
| 1858 | if (k == BuiltinType::Void) { |
| 1859 | Current = NoClass; |
| 1860 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1861 | Lo = Integer; |
| 1862 | Hi = Integer; |
| 1863 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1864 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1865 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1866 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1867 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1868 | Current = SSE; |
| 1869 | } else if (k == BuiltinType::LongDouble) { |
| 1870 | Lo = X87; |
| 1871 | Hi = X87Up; |
| 1872 | } |
| 1873 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1874 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1875 | return; |
| 1876 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1877 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1878 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1879 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1880 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1881 | return; |
| 1882 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1884 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1885 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1889 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1890 | if (Ty->isMemberFunctionPointerType()) { |
| 1891 | if (Has64BitPointers) { |
| 1892 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 1893 | // Lo and Hi now. |
| 1894 | Lo = Hi = Integer; |
| 1895 | } else { |
| 1896 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 1897 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 1898 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 1899 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 1900 | if (EB_FuncPtr != EB_ThisAdj) { |
| 1901 | Lo = Hi = Integer; |
| 1902 | } else { |
| 1903 | Current = Integer; |
| 1904 | } |
| 1905 | } |
| 1906 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1907 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1908 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1909 | return; |
| 1910 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1911 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1912 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1913 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 1914 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 1915 | // gcc passes the following as integer: |
| 1916 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 1917 | // 2 bytes - <2 x char>, <1 x short> |
| 1918 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1919 | Current = Integer; |
| 1920 | |
| 1921 | // If this type crosses an eightbyte boundary, it should be |
| 1922 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 1923 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 1924 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 1925 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1926 | Hi = Lo; |
| 1927 | } else if (Size == 64) { |
| 1928 | // gcc passes <1 x double> in memory. :( |
| 1929 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1930 | return; |
| 1931 | |
| 1932 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1933 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1934 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1935 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1936 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1937 | Current = Integer; |
| 1938 | else |
| 1939 | Current = SSE; |
| 1940 | |
| 1941 | // If this type crosses an eightbyte boundary, it should be |
| 1942 | // split. |
| 1943 | if (OffsetBase && OffsetBase != 64) |
| 1944 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 1945 | } else if (Size == 128 || |
| 1946 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1947 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1948 | // least significant one belongs to class SSE and all the others to class |
| 1949 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1950 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1951 | // This design isn't correct for 256-bits, but since there're no cases |
| 1952 | // where the upper parts would need to be inspected, avoid adding |
| 1953 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1954 | // |
| 1955 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1956 | // registers if they are "named", i.e. not part of the "..." of a |
| 1957 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 1958 | // |
| 1959 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 1960 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1961 | Lo = SSE; |
| 1962 | Hi = SSEUp; |
| 1963 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1964 | return; |
| 1965 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1966 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1967 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1968 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1969 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1970 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1971 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1972 | if (Size <= 64) |
| 1973 | Current = Integer; |
| 1974 | else if (Size <= 128) |
| 1975 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1976 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1977 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1978 | else if (ET == getContext().DoubleTy || |
| 1979 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1980 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1981 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1982 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1983 | Current = ComplexX87; |
| 1984 | |
| 1985 | // If this complex type crosses an eightbyte boundary then it |
| 1986 | // should be split. |
| 1987 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1988 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1989 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1990 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1991 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1992 | return; |
| 1993 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1994 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1995 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1996 | // Arrays are treated like structures. |
| 1997 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1998 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1999 | |
| 2000 | // 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] | 2001 | // than four eightbytes, ..., it has class MEMORY. |
| 2002 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2003 | return; |
| 2004 | |
| 2005 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2006 | // fields, it has class MEMORY. |
| 2007 | // |
| 2008 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2009 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2010 | return; |
| 2011 | |
| 2012 | // Otherwise implement simplified merge. We could be smarter about |
| 2013 | // this, but it isn't worth it and would be harder to verify. |
| 2014 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2015 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2016 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2017 | |
| 2018 | // The only case a 256-bit wide vector could be used is when the array |
| 2019 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2020 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2021 | if (Size > 128 && EltSize != 256) |
| 2022 | return; |
| 2023 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2024 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2025 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2026 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2027 | Lo = merge(Lo, FieldLo); |
| 2028 | Hi = merge(Hi, FieldHi); |
| 2029 | if (Lo == Memory || Hi == Memory) |
| 2030 | break; |
| 2031 | } |
| 2032 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2033 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2034 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2035 | return; |
| 2036 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2037 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2038 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2039 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2040 | |
| 2041 | // 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] | 2042 | // than four eightbytes, ..., it has class MEMORY. |
| 2043 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2044 | return; |
| 2045 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2046 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2047 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2048 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2049 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2050 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2051 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2052 | const RecordDecl *RD = RT->getDecl(); |
| 2053 | |
| 2054 | // Assume variable sized types are passed in memory. |
| 2055 | if (RD->hasFlexibleArrayMember()) |
| 2056 | return; |
| 2057 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2058 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2059 | |
| 2060 | // Reset Lo class, this will be recomputed. |
| 2061 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2062 | |
| 2063 | // If this is a C++ record, classify the bases first. |
| 2064 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2065 | for (const auto &I : CXXRD->bases()) { |
| 2066 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2067 | "Unexpected base class!"); |
| 2068 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2069 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2070 | |
| 2071 | // Classify this field. |
| 2072 | // |
| 2073 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2074 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2075 | // initialized to class NO_CLASS. |
| 2076 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2077 | uint64_t Offset = |
| 2078 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2079 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2080 | Lo = merge(Lo, FieldLo); |
| 2081 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2082 | if (Lo == Memory || Hi == Memory) { |
| 2083 | postMerge(Size, Lo, Hi); |
| 2084 | return; |
| 2085 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2090 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2091 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2092 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2093 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2094 | bool BitField = i->isBitField(); |
| 2095 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2096 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2097 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2098 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2099 | // The only case a 256-bit wide vector could be used is when the struct |
| 2100 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2101 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2102 | // |
| 2103 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2104 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2105 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2106 | return; |
| 2107 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2108 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2109 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2110 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2111 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2112 | return; |
| 2113 | } |
| 2114 | |
| 2115 | // Classify this field. |
| 2116 | // |
| 2117 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2118 | // exceeds a single eightbyte, each is classified |
| 2119 | // separately. Each eightbyte gets initialized to class |
| 2120 | // NO_CLASS. |
| 2121 | Class FieldLo, FieldHi; |
| 2122 | |
| 2123 | // Bit-fields require special handling, they do not force the |
| 2124 | // structure to be passed in memory even if unaligned, and |
| 2125 | // therefore they can straddle an eightbyte. |
| 2126 | if (BitField) { |
| 2127 | // Ignore padding bit-fields. |
| 2128 | if (i->isUnnamedBitfield()) |
| 2129 | continue; |
| 2130 | |
| 2131 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2132 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2133 | |
| 2134 | uint64_t EB_Lo = Offset / 64; |
| 2135 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2136 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2137 | if (EB_Lo) { |
| 2138 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2139 | FieldLo = NoClass; |
| 2140 | FieldHi = Integer; |
| 2141 | } else { |
| 2142 | FieldLo = Integer; |
| 2143 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2144 | } |
| 2145 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2146 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2147 | Lo = merge(Lo, FieldLo); |
| 2148 | Hi = merge(Hi, FieldHi); |
| 2149 | if (Lo == Memory || Hi == Memory) |
| 2150 | break; |
| 2151 | } |
| 2152 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2153 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2157 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2158 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2159 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2160 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2161 | // Treat an enum type as its underlying type. |
| 2162 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2163 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2164 | |
| 2165 | return (Ty->isPromotableIntegerType() ? |
| 2166 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2167 | } |
| 2168 | |
| 2169 | return ABIArgInfo::getIndirect(0); |
| 2170 | } |
| 2171 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2172 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2173 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2174 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2175 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2176 | if (Size <= 64 || Size > LargestVector) |
| 2177 | return true; |
| 2178 | } |
| 2179 | |
| 2180 | return false; |
| 2181 | } |
| 2182 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2183 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2184 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2185 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2186 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2187 | // |
| 2188 | // This assumption is optimistic, as there could be free registers available |
| 2189 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2190 | // the argument in the free register. This does not seem to happen currently, |
| 2191 | // but this code would be much safer if we could mark the argument with |
| 2192 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2193 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2194 | // Treat an enum type as its underlying type. |
| 2195 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2196 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2197 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2198 | return (Ty->isPromotableIntegerType() ? |
| 2199 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2200 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2201 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2202 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2203 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2204 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2205 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2206 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2207 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2210 | // is important for good codegen. |
| 2211 | // |
| 2212 | // We do this by coercing the value into a scalar type which the backend can |
| 2213 | // handle naturally (i.e., without using byval). |
| 2214 | // |
| 2215 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2216 | // free integer registers. Doing this when there are free integer registers |
| 2217 | // would require more care, as we would have to ensure that the coerced value |
| 2218 | // did not claim the unused register. That would require either reording the |
| 2219 | // arguments to the function (so that any subsequent inreg values came first), |
| 2220 | // or only doing this optimization when there were no following arguments that |
| 2221 | // might be inreg. |
| 2222 | // |
| 2223 | // We currently expect it to be rare (particularly in well written code) for |
| 2224 | // arguments to be passed on the stack when there are still free integer |
| 2225 | // registers available (this would typically imply large structs being passed |
| 2226 | // by value), so this seems like a fair tradeoff for now. |
| 2227 | // |
| 2228 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2229 | // attributes. See PR12193. |
| 2230 | if (freeIntRegs == 0) { |
| 2231 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2232 | |
| 2233 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2234 | // type, which will end up on the stack (with alignment 8). |
| 2235 | if (Align == 8 && Size <= 64) |
| 2236 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2237 | Size)); |
| 2238 | } |
| 2239 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2240 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2243 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2244 | /// 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] | 2245 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2246 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2247 | // vectors; strip them off if present. |
| 2248 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2249 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2250 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2251 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2252 | if(isa<llvm::VectorType>(IRType)) |
| 2253 | return IRType; |
| 2254 | |
| 2255 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2256 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2257 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2258 | |
| 2259 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2260 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2261 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2264 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2265 | /// is known to either be off the end of the specified type or being in |
| 2266 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2267 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2268 | /// classification that put one of the two halves in the INTEGER class. |
| 2269 | /// |
| 2270 | /// It is conservatively correct to return false. |
| 2271 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2272 | unsigned EndBit, ASTContext &Context) { |
| 2273 | // If the bytes being queried are off the end of the type, there is no user |
| 2274 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2275 | // types that don't contain interesting padding. |
| 2276 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2277 | if (TySize <= StartBit) |
| 2278 | return true; |
| 2279 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2280 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2281 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2282 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2283 | |
| 2284 | // Check each element to see if the element overlaps with the queried range. |
| 2285 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2286 | // If the element is after the span we care about, then we're done.. |
| 2287 | unsigned EltOffset = i*EltSize; |
| 2288 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2289 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2290 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2291 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2292 | EndBit-EltOffset, Context)) |
| 2293 | return false; |
| 2294 | } |
| 2295 | // If it overlaps no elements, then it is safe to process as padding. |
| 2296 | return true; |
| 2297 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2298 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2299 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2300 | const RecordDecl *RD = RT->getDecl(); |
| 2301 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2303 | // If this is a C++ record, check the bases first. |
| 2304 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2305 | for (const auto &I : CXXRD->bases()) { |
| 2306 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2307 | "Unexpected base class!"); |
| 2308 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2309 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2310 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2311 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2312 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2313 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2314 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2315 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2316 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2317 | EndBit-BaseOffset, Context)) |
| 2318 | return false; |
| 2319 | } |
| 2320 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2321 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2322 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2323 | // this could be sped up a lot by being smarter about queried fields, |
| 2324 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2325 | // much. |
| 2326 | unsigned idx = 0; |
| 2327 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2328 | i != e; ++i, ++idx) { |
| 2329 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2330 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2331 | // If we found a field after the region we care about, then we're done. |
| 2332 | if (FieldOffset >= EndBit) break; |
| 2333 | |
| 2334 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2335 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2336 | Context)) |
| 2337 | return false; |
| 2338 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2340 | // If nothing in this record overlapped the area of interest, then we're |
| 2341 | // clean. |
| 2342 | return true; |
| 2343 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2344 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2345 | return false; |
| 2346 | } |
| 2347 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2348 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2349 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2350 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2351 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2352 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2353 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2354 | // Base case if we find a float. |
| 2355 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2356 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2357 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2358 | // 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] | 2359 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2360 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2361 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2362 | IROffset -= SL->getElementOffset(Elt); |
| 2363 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2364 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2365 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2366 | // 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] | 2367 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2368 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2369 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2370 | IROffset -= IROffset/EltSize*EltSize; |
| 2371 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2372 | } |
| 2373 | |
| 2374 | return false; |
| 2375 | } |
| 2376 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2377 | |
| 2378 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2379 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2380 | llvm::Type *X86_64ABIInfo:: |
| 2381 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2382 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2383 | // 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] | 2384 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2385 | // structs that contain 3 floats. |
| 2386 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2387 | SourceOffset*8+64, getContext())) |
| 2388 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2389 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2390 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2391 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2392 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2393 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2394 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2395 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2396 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2397 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2398 | } |
| 2399 | |
| 2400 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2401 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2402 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2403 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2404 | /// 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] | 2405 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2406 | /// etc). |
| 2407 | /// |
| 2408 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2409 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2410 | /// the 8-byte value references. PrefType may be null. |
| 2411 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2412 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2413 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2414 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2415 | llvm::Type *X86_64ABIInfo:: |
| 2416 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2417 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2418 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2419 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2420 | if (IROffset == 0) { |
| 2421 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2422 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2423 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2424 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2425 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2426 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2427 | // goodness in the source type is just tail padding. This is allowed to |
| 2428 | // kick in for struct {double,int} on the int, but not on |
| 2429 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2430 | // have to do this analysis on the source type because we can't depend on |
| 2431 | // unions being lowered a specific way etc. |
| 2432 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2433 | IRType->isIntegerTy(32) || |
| 2434 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2435 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2436 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2438 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2439 | SourceOffset*8+64, getContext())) |
| 2440 | return IRType; |
| 2441 | } |
| 2442 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2443 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2444 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2445 | // 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] | 2446 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2447 | if (IROffset < SL->getSizeInBytes()) { |
| 2448 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2449 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2450 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2451 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2452 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2453 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2454 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2456 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2457 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2458 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2459 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2460 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2461 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2462 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2463 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2464 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2465 | // 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] | 2466 | unsigned TySizeInBytes = |
| 2467 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2468 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2469 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2471 | // It is always safe to classify this as an integer type up to i64 that |
| 2472 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2473 | return llvm::IntegerType::get(getVMContext(), |
| 2474 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2477 | |
| 2478 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2479 | /// be used as elements of a two register pair to pass or return, return a |
| 2480 | /// first class aggregate to represent them. For example, if the low part of |
| 2481 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2482 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2483 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2484 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2485 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2486 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2487 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2488 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2489 | // the second element at offset 8. Check for this: |
| 2490 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2491 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2492 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2493 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2494 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2495 | // To handle this, we have to increase the size of the low part so that the |
| 2496 | // second element will start at an 8 byte offset. We can't increase the size |
| 2497 | // of the second element because it might make us access off the end of the |
| 2498 | // struct. |
| 2499 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 2500 | // There are usually two sorts of types the ABI generation code can produce |
| 2501 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 2502 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 2503 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2504 | // Promote these to a larger type. |
| 2505 | if (Lo->isFloatTy()) |
| 2506 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2507 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 2508 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 2509 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2510 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2511 | } |
| 2512 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2513 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2514 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2515 | |
| 2516 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2517 | // Verify that the second element is at an 8-byte offset. |
| 2518 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2519 | "Invalid x86-64 argument pair!"); |
| 2520 | return Result; |
| 2521 | } |
| 2522 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2523 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2524 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2525 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2526 | // classification algorithm. |
| 2527 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2528 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Check some invariants. |
| 2531 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2532 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2533 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2534 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2535 | switch (Lo) { |
| 2536 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2537 | if (Hi == NoClass) |
| 2538 | return ABIArgInfo::getIgnore(); |
| 2539 | // If the low part is just padding, it takes no register, leave ResType |
| 2540 | // null. |
| 2541 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2542 | "Unknown missing lo part"); |
| 2543 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2544 | |
| 2545 | case SSEUp: |
| 2546 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2547 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2548 | |
| 2549 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2550 | // hidden argument. |
| 2551 | case Memory: |
| 2552 | return getIndirectReturnResult(RetTy); |
| 2553 | |
| 2554 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2555 | // available register of the sequence %rax, %rdx is used. |
| 2556 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2557 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2558 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2559 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2560 | // so that the parameter gets the right LLVM IR attributes. |
| 2561 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2562 | // Treat an enum type as its underlying type. |
| 2563 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2564 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2565 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2566 | if (RetTy->isIntegralOrEnumerationType() && |
| 2567 | RetTy->isPromotableIntegerType()) |
| 2568 | return ABIArgInfo::getExtend(); |
| 2569 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2570 | break; |
| 2571 | |
| 2572 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2573 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2574 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2575 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2576 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2577 | |
| 2578 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2579 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2580 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2581 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2582 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2583 | |
| 2584 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2585 | // part of the value is returned in %st0 and the imaginary part in |
| 2586 | // %st1. |
| 2587 | case ComplexX87: |
| 2588 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2589 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2590 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2591 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2592 | break; |
| 2593 | } |
| 2594 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2595 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2596 | switch (Hi) { |
| 2597 | // Memory was handled previously and X87 should |
| 2598 | // never occur as a hi class. |
| 2599 | case Memory: |
| 2600 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2601 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2602 | |
| 2603 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2604 | case NoClass: |
| 2605 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2607 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2608 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2609 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2610 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2611 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2612 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2613 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2614 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2615 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2616 | break; |
| 2617 | |
| 2618 | // 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] | 2619 | // is passed in the next available eightbyte chunk if the last used |
| 2620 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2621 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2622 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2623 | case SSEUp: |
| 2624 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2625 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2626 | break; |
| 2627 | |
| 2628 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2629 | // returned together with the previous X87 value in %st0. |
| 2630 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2631 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2632 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2633 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2634 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2635 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2636 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2637 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2638 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2639 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2640 | break; |
| 2641 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2642 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2643 | // 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] | 2644 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2645 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2646 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2647 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2648 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2649 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2652 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2653 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2654 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2655 | const |
| 2656 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2657 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2658 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2659 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2660 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2661 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2662 | // Check some invariants. |
| 2663 | // FIXME: Enforce these by construction. |
| 2664 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2665 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2666 | |
| 2667 | neededInt = 0; |
| 2668 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2669 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2670 | switch (Lo) { |
| 2671 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2672 | if (Hi == NoClass) |
| 2673 | return ABIArgInfo::getIgnore(); |
| 2674 | // If the low part is just padding, it takes no register, leave ResType |
| 2675 | // null. |
| 2676 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2677 | "Unknown missing lo part"); |
| 2678 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2679 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2680 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2681 | // on the stack. |
| 2682 | case Memory: |
| 2683 | |
| 2684 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2685 | // COMPLEX_X87, it is passed in memory. |
| 2686 | case X87: |
| 2687 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2688 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2689 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2690 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2691 | |
| 2692 | case SSEUp: |
| 2693 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2694 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2695 | |
| 2696 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2697 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2698 | // and %r9 is used. |
| 2699 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2700 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2701 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2702 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2703 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2704 | |
| 2705 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2706 | // so that the parameter gets the right LLVM IR attributes. |
| 2707 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2708 | // Treat an enum type as its underlying type. |
| 2709 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2710 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2712 | if (Ty->isIntegralOrEnumerationType() && |
| 2713 | Ty->isPromotableIntegerType()) |
| 2714 | return ABIArgInfo::getExtend(); |
| 2715 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2716 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2717 | break; |
| 2718 | |
| 2719 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2720 | // available SSE register is used, the registers are taken in the |
| 2721 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2722 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2723 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2724 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2725 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2726 | break; |
| 2727 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2728 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2729 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2730 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2731 | switch (Hi) { |
| 2732 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2733 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2734 | // which is passed in memory. |
| 2735 | case Memory: |
| 2736 | case X87: |
| 2737 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2738 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2739 | |
| 2740 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2741 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2742 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2743 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2744 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2745 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2746 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2747 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2748 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2749 | break; |
| 2750 | |
| 2751 | // X87Up generally doesn't occur here (long double is passed in |
| 2752 | // memory), except in situations involving unions. |
| 2753 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2754 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2755 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2756 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2757 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2758 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2759 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2760 | ++neededSSE; |
| 2761 | break; |
| 2762 | |
| 2763 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2764 | // 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] | 2765 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2766 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2767 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2768 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2769 | break; |
| 2770 | } |
| 2771 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2772 | // If a high part was specified, merge it together with the low part. It is |
| 2773 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2774 | // first class struct aggregate with the high and low part: {low, high} |
| 2775 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2776 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2777 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2778 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2781 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2782 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2783 | if (!getCXXABI().classifyReturnType(FI)) |
| 2784 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2785 | |
| 2786 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2787 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2788 | |
| 2789 | // If the return value is indirect, then the hidden argument is consuming one |
| 2790 | // integer register. |
| 2791 | if (FI.getReturnInfo().isIndirect()) |
| 2792 | --freeIntRegs; |
| 2793 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 2794 | // The chain argument effectively gives us another free register. |
| 2795 | if (FI.isChainCall()) |
| 2796 | ++freeIntRegs; |
| 2797 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2798 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2799 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2800 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2801 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2802 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2803 | it != ie; ++it, ++ArgNo) { |
| 2804 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2805 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2806 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2807 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2808 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2809 | |
| 2810 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2811 | // eightbyte of an argument, the whole argument is passed on the |
| 2812 | // stack. If registers have already been assigned for some |
| 2813 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2814 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2815 | freeIntRegs -= neededInt; |
| 2816 | freeSSERegs -= neededSSE; |
| 2817 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2818 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2819 | } |
| 2820 | } |
| 2821 | } |
| 2822 | |
| 2823 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2824 | QualType Ty, |
| 2825 | CodeGenFunction &CGF) { |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 2826 | llvm::Value *overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 2827 | nullptr, VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2828 | llvm::Value *overflow_arg_area = |
| 2829 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2830 | |
| 2831 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2832 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2833 | // It isn't stated explicitly in the standard, but in practice we use |
| 2834 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2835 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2836 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2837 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2838 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2839 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2840 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2841 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2842 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2843 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2844 | overflow_arg_area = |
| 2845 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2846 | overflow_arg_area->getType(), |
| 2847 | "overflow_arg_area.align"); |
| 2848 | } |
| 2849 | |
| 2850 | // 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] | 2851 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2852 | llvm::Value *Res = |
| 2853 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2854 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2855 | |
| 2856 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2857 | // l->overflow_arg_area + sizeof(type). |
| 2858 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2859 | // an 8 byte boundary. |
| 2860 | |
| 2861 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2862 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2863 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2864 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2865 | "overflow_arg_area.next"); |
| 2866 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2867 | |
| 2868 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2869 | return Res; |
| 2870 | } |
| 2871 | |
| 2872 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2873 | CodeGenFunction &CGF) const { |
| 2874 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2875 | // struct { |
| 2876 | // i32 gp_offset; |
| 2877 | // i32 fp_offset; |
| 2878 | // i8* overflow_arg_area; |
| 2879 | // i8* reg_save_area; |
| 2880 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2881 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2882 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2883 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2884 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2885 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2886 | |
| 2887 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2888 | // in the registers. If not go to step 7. |
| 2889 | if (!neededInt && !neededSSE) |
| 2890 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2891 | |
| 2892 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2893 | // general purpose registers needed to pass type and num_fp to hold |
| 2894 | // the number of floating point registers needed. |
| 2895 | |
| 2896 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2897 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2898 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2899 | // |
| 2900 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2901 | // register save space). |
| 2902 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2903 | llvm::Value *InRegs = nullptr; |
| 2904 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2905 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2906 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2907 | gp_offset_p = |
| 2908 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2909 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2910 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2911 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2915 | fp_offset_p = |
| 2916 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2917 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2918 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2919 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2920 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2921 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2922 | } |
| 2923 | |
| 2924 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2925 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2926 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2927 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2928 | |
| 2929 | // Emit code to load the value if it was passed in registers. |
| 2930 | |
| 2931 | CGF.EmitBlock(InRegBlock); |
| 2932 | |
| 2933 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2934 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2935 | // copying to a temporary location in case the parameter is passed |
| 2936 | // in different register classes or requires an alignment greater |
| 2937 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2938 | // |
| 2939 | // FIXME: This really results in shameful code when we end up needing to |
| 2940 | // collect arguments from different places; often what should result in a |
| 2941 | // simple assembling of a structure from scattered addresses has many more |
| 2942 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2943 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2944 | llvm::Value *RegAddr = CGF.Builder.CreateLoad( |
| 2945 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3), "reg_save_area"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2946 | if (neededInt && neededSSE) { |
| 2947 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2948 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2949 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2950 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2951 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2952 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2953 | llvm::Type *TyLo = ST->getElementType(0); |
| 2954 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2955 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2956 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2957 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2958 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2959 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2960 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2961 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2962 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2963 | llvm::Value *V = |
| 2964 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2965 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2966 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2967 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2968 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2969 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2970 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2971 | } else if (neededInt) { |
| 2972 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2973 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2974 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2975 | |
| 2976 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2977 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2978 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2979 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2980 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2981 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2982 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2983 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2984 | RegAddr = Tmp; |
| 2985 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2986 | } else if (neededSSE == 1) { |
| 2987 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2988 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2989 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2990 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2991 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2992 | // SSE registers are spaced 16 bytes apart in the register save |
| 2993 | // area, we need to collect the two eightbytes together. |
| 2994 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2995 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2996 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2997 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2998 | llvm::PointerType::getUnqual(DoubleTy); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2999 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3000 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 3001 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3002 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 3003 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3004 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3005 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 3006 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 3007 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3008 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 3009 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3013 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3014 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3015 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3016 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3017 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3018 | gp_offset_p); |
| 3019 | } |
| 3020 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3021 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3022 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3023 | fp_offset_p); |
| 3024 | } |
| 3025 | CGF.EmitBranch(ContBlock); |
| 3026 | |
| 3027 | // Emit code to load the value if it was passed in memory. |
| 3028 | |
| 3029 | CGF.EmitBlock(InMemBlock); |
| 3030 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 3031 | |
| 3032 | // Return the appropriate result. |
| 3033 | |
| 3034 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 3035 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3036 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3037 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 3038 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3039 | return ResAddr; |
| 3040 | } |
| 3041 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3042 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3043 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3044 | |
| 3045 | if (Ty->isVoidType()) |
| 3046 | return ABIArgInfo::getIgnore(); |
| 3047 | |
| 3048 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3049 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3050 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3051 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3052 | uint64_t Width = Info.Width; |
| 3053 | unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3054 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3055 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3056 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3057 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3058 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3059 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 3060 | } |
| 3061 | |
| 3062 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3063 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3064 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3065 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3066 | if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3067 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3068 | Width)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3069 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3070 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3071 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3072 | // other targets. |
| 3073 | const Type *Base = nullptr; |
| 3074 | uint64_t NumElts = 0; |
| 3075 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3076 | if (FreeSSERegs >= NumElts) { |
| 3077 | FreeSSERegs -= NumElts; |
| 3078 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3079 | return ABIArgInfo::getDirect(); |
| 3080 | return ABIArgInfo::getExpand(); |
| 3081 | } |
| 3082 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3083 | } |
| 3084 | |
| 3085 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3086 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3087 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3088 | // directly. |
| 3089 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3090 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3091 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3094 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3095 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3096 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3097 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3098 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3099 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3100 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3101 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3104 | // Bool type is always extended to the ABI, other builtin types are not |
| 3105 | // extended. |
| 3106 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3107 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3108 | return ABIArgInfo::getExtend(); |
| 3109 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3110 | return ABIArgInfo::getDirect(); |
| 3111 | } |
| 3112 | |
| 3113 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3114 | bool IsVectorCall = |
| 3115 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3116 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3117 | // We can use up to 4 SSE return registers with vectorcall. |
| 3118 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3119 | if (!getCXXABI().classifyReturnType(FI)) |
| 3120 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3121 | |
| 3122 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3123 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3124 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3125 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3128 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3129 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3130 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3132 | CGBuilderTy &Builder = CGF.Builder; |
| 3133 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 3134 | "ap"); |
| 3135 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3136 | llvm::Type *PTy = |
| 3137 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3138 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3139 | |
| 3140 | uint64_t Offset = |
| 3141 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 3142 | llvm::Value *NextAddr = |
| 3143 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3144 | "ap.next"); |
| 3145 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3146 | |
| 3147 | return AddrTyped; |
| 3148 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3149 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3150 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3151 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3152 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3153 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3154 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3155 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3156 | |
| 3157 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3158 | CodeGenFunction &CGF) const override; |
| 3159 | }; |
| 3160 | |
| 3161 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3162 | public: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3163 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3164 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3165 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3166 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3167 | // This is recovered from gcc output. |
| 3168 | return 1; // r1 is the dedicated stack pointer |
| 3169 | } |
| 3170 | |
| 3171 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3172 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3173 | }; |
| 3174 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3175 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3176 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3177 | llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3178 | QualType Ty, |
| 3179 | CodeGenFunction &CGF) const { |
| 3180 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3181 | // TODO: Implement this. For now ignore. |
| 3182 | (void)CTy; |
| 3183 | return nullptr; |
| 3184 | } |
| 3185 | |
| 3186 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3187 | bool isInt = |
| 3188 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3189 | llvm::Type *CharPtr = CGF.Int8PtrTy; |
| 3190 | llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy; |
| 3191 | |
| 3192 | CGBuilderTy &Builder = CGF.Builder; |
| 3193 | llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr"); |
| 3194 | llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3195 | llvm::Value *FPRPtrAsInt = |
| 3196 | Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3197 | llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3198 | llvm::Value *OverflowAreaPtrAsInt = |
| 3199 | Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3)); |
| 3200 | llvm::Value *OverflowAreaPtr = |
| 3201 | Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr); |
| 3202 | llvm::Value *RegsaveAreaPtrAsInt = |
| 3203 | Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4)); |
| 3204 | llvm::Value *RegsaveAreaPtr = |
| 3205 | Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3206 | llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr"); |
| 3207 | // Align GPR when TY is i64. |
| 3208 | if (isI64) { |
| 3209 | llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1)); |
| 3210 | llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1)); |
| 3211 | llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1)); |
| 3212 | GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR); |
| 3213 | } |
| 3214 | llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr"); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3215 | llvm::Value *OverflowArea = |
| 3216 | Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area"); |
| 3217 | llvm::Value *OverflowAreaAsInt = |
| 3218 | Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty); |
| 3219 | llvm::Value *RegsaveArea = |
| 3220 | Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area"); |
| 3221 | llvm::Value *RegsaveAreaAsInt = |
| 3222 | Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3223 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3224 | llvm::Value *CC = |
| 3225 | Builder.CreateICmpULT(isInt ? GPR : FPR, Builder.getInt8(8), "cond"); |
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 *RegConstant = |
| 3228 | Builder.CreateMul(isInt ? GPR : FPR, Builder.getInt8(isInt ? 4 : 8)); |
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 *OurReg = Builder.CreateAdd( |
| 3231 | RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3232 | |
| 3233 | if (Ty->isFloatingType()) |
| 3234 | OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32)); |
| 3235 | |
| 3236 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3237 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3238 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3239 | |
| 3240 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3241 | |
| 3242 | CGF.EmitBlock(UsingRegs); |
| 3243 | |
| 3244 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3245 | llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy); |
| 3246 | // Increase the GPR/FPR indexes. |
| 3247 | if (isInt) { |
| 3248 | GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1)); |
| 3249 | Builder.CreateStore(GPR, GPRPtr); |
| 3250 | } else { |
| 3251 | FPR = Builder.CreateAdd(FPR, Builder.getInt8(1)); |
| 3252 | Builder.CreateStore(FPR, FPRPtr); |
| 3253 | } |
| 3254 | CGF.EmitBranch(Cont); |
| 3255 | |
| 3256 | CGF.EmitBlock(UsingOverflow); |
| 3257 | |
| 3258 | // Increase the overflow area. |
| 3259 | llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3260 | OverflowAreaAsInt = |
| 3261 | Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8)); |
| 3262 | Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), |
| 3263 | OverflowAreaPtr); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3264 | CGF.EmitBranch(Cont); |
| 3265 | |
| 3266 | CGF.EmitBlock(Cont); |
| 3267 | |
| 3268 | llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr"); |
| 3269 | Result->addIncoming(Result1, UsingRegs); |
| 3270 | Result->addIncoming(Result2, UsingOverflow); |
| 3271 | |
| 3272 | if (Ty->isAggregateType()) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3273 | llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3274 | return Builder.CreateLoad(AGGPtr, false, "aggr"); |
| 3275 | } |
| 3276 | |
| 3277 | return Result; |
| 3278 | } |
| 3279 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3280 | bool |
| 3281 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3282 | llvm::Value *Address) const { |
| 3283 | // This is calculated from the LLVM and GCC tables and verified |
| 3284 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3285 | |
| 3286 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3287 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3288 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3289 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3290 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3291 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3292 | |
| 3293 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3294 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3295 | |
| 3296 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3297 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3298 | |
| 3299 | // 64-76 are various 4-byte special-purpose registers: |
| 3300 | // 64: mq |
| 3301 | // 65: lr |
| 3302 | // 66: ctr |
| 3303 | // 67: ap |
| 3304 | // 68-75 cr0-7 |
| 3305 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3306 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3307 | |
| 3308 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3309 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3310 | |
| 3311 | // 109: vrsave |
| 3312 | // 110: vscr |
| 3313 | // 111: spe_acc |
| 3314 | // 112: spefscr |
| 3315 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3316 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3317 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3318 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3321 | // PowerPC-64 |
| 3322 | |
| 3323 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3324 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3325 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3326 | public: |
| 3327 | enum ABIKind { |
| 3328 | ELFv1 = 0, |
| 3329 | ELFv2 |
| 3330 | }; |
| 3331 | |
| 3332 | private: |
| 3333 | static const unsigned GPRBits = 64; |
| 3334 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3335 | bool HasQPX; |
| 3336 | |
| 3337 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3338 | // will be passed in a QPX register. |
| 3339 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3340 | if (!HasQPX) |
| 3341 | return false; |
| 3342 | |
| 3343 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3344 | unsigned NumElements = VT->getNumElements(); |
| 3345 | if (NumElements == 1) |
| 3346 | return false; |
| 3347 | |
| 3348 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3349 | if (getContext().getTypeSize(Ty) <= 256) |
| 3350 | return true; |
| 3351 | } else if (VT->getElementType()-> |
| 3352 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3353 | if (getContext().getTypeSize(Ty) <= 128) |
| 3354 | return true; |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | return false; |
| 3359 | } |
| 3360 | |
| 3361 | bool IsQPXVectorTy(QualType Ty) const { |
| 3362 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3363 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3364 | |
| 3365 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3366 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3367 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3368 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3369 | bool isPromotableTypeForABI(QualType Ty) const; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3370 | bool isAlignedParamType(QualType Ty, bool &Align32) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3371 | |
| 3372 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3373 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3374 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3375 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3376 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3377 | uint64_t Members) const override; |
| 3378 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3379 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3380 | // Example: For aggregate arguments that fit in a register, we could |
| 3381 | // use getDirectInReg (as is done below for structs containing a single |
| 3382 | // floating-point value) to avoid pushing them to memory on function |
| 3383 | // entry. This would require changing the logic in PPCISelLowering |
| 3384 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3385 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3386 | if (!getCXXABI().classifyReturnType(FI)) |
| 3387 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3388 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3389 | // We rely on the default argument classification for the most part. |
| 3390 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3391 | // 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] | 3392 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3393 | if (T) { |
| 3394 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3395 | if (IsQPXVectorTy(T) || |
| 3396 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3397 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3398 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3399 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3400 | continue; |
| 3401 | } |
| 3402 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3403 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3404 | } |
| 3405 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3406 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3407 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3408 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3409 | }; |
| 3410 | |
| 3411 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3412 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3413 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3414 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3415 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 3416 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3417 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3418 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3419 | // This is recovered from gcc output. |
| 3420 | return 1; // r1 is the dedicated stack pointer |
| 3421 | } |
| 3422 | |
| 3423 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3424 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3425 | }; |
| 3426 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3427 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3428 | public: |
| 3429 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3430 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3431 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3432 | // This is recovered from gcc output. |
| 3433 | return 1; // r1 is the dedicated stack pointer |
| 3434 | } |
| 3435 | |
| 3436 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3437 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3438 | }; |
| 3439 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3440 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3441 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3442 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3443 | // extended to 64 bits. |
| 3444 | bool |
| 3445 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3446 | // Treat an enum type as its underlying type. |
| 3447 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3448 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3449 | |
| 3450 | // Promotable integer types are required to be promoted by the ABI. |
| 3451 | if (Ty->isPromotableIntegerType()) |
| 3452 | return true; |
| 3453 | |
| 3454 | // In addition to the usual promotable integer types, we also need to |
| 3455 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3456 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3457 | switch (BT->getKind()) { |
| 3458 | case BuiltinType::Int: |
| 3459 | case BuiltinType::UInt: |
| 3460 | return true; |
| 3461 | default: |
| 3462 | break; |
| 3463 | } |
| 3464 | |
| 3465 | return false; |
| 3466 | } |
| 3467 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3468 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3469 | /// alignment in the parameter area. |
| 3470 | bool |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3471 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty, bool &Align32) const { |
| 3472 | Align32 = false; |
| 3473 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3474 | // Complex types are passed just like their elements. |
| 3475 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3476 | Ty = CTy->getElementType(); |
| 3477 | |
| 3478 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3479 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3480 | if (IsQPXVectorTy(Ty)) { |
| 3481 | if (getContext().getTypeSize(Ty) > 128) |
| 3482 | Align32 = true; |
| 3483 | |
| 3484 | return true; |
| 3485 | } else if (Ty->isVectorType()) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3486 | return getContext().getTypeSize(Ty) == 128; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3487 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3488 | |
| 3489 | // For single-element float/vector structs, we consider the whole type |
| 3490 | // to have the same alignment requirements as its single element. |
| 3491 | const Type *AlignAsType = nullptr; |
| 3492 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3493 | if (EltType) { |
| 3494 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3495 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3496 | getContext().getTypeSize(EltType) == 128) || |
| 3497 | (BT && BT->isFloatingPoint())) |
| 3498 | AlignAsType = EltType; |
| 3499 | } |
| 3500 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3501 | // Likewise for ELFv2 homogeneous aggregates. |
| 3502 | const Type *Base = nullptr; |
| 3503 | uint64_t Members = 0; |
| 3504 | if (!AlignAsType && Kind == ELFv2 && |
| 3505 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3506 | AlignAsType = Base; |
| 3507 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3508 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3509 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3510 | if (getContext().getTypeSize(AlignAsType) > 128) |
| 3511 | Align32 = true; |
| 3512 | |
| 3513 | return true; |
| 3514 | } else if (AlignAsType) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3515 | return AlignAsType->isVectorType(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3516 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3517 | |
| 3518 | // Otherwise, we only need alignment for any aggregate type that |
| 3519 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3520 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3521 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
| 3522 | Align32 = true; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3523 | return true; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3524 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3525 | |
| 3526 | return false; |
| 3527 | } |
| 3528 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3529 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3530 | /// aggregate. Base is set to the base element type, and Members is set |
| 3531 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3532 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3533 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3534 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3535 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3536 | if (NElements == 0) |
| 3537 | return false; |
| 3538 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3539 | return false; |
| 3540 | Members *= NElements; |
| 3541 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3542 | const RecordDecl *RD = RT->getDecl(); |
| 3543 | if (RD->hasFlexibleArrayMember()) |
| 3544 | return false; |
| 3545 | |
| 3546 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3547 | |
| 3548 | // If this is a C++ record, check the bases first. |
| 3549 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3550 | for (const auto &I : CXXRD->bases()) { |
| 3551 | // Ignore empty records. |
| 3552 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3553 | continue; |
| 3554 | |
| 3555 | uint64_t FldMembers; |
| 3556 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3557 | return false; |
| 3558 | |
| 3559 | Members += FldMembers; |
| 3560 | } |
| 3561 | } |
| 3562 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3563 | for (const auto *FD : RD->fields()) { |
| 3564 | // Ignore (non-zero arrays of) empty records. |
| 3565 | QualType FT = FD->getType(); |
| 3566 | while (const ConstantArrayType *AT = |
| 3567 | getContext().getAsConstantArrayType(FT)) { |
| 3568 | if (AT->getSize().getZExtValue() == 0) |
| 3569 | return false; |
| 3570 | FT = AT->getElementType(); |
| 3571 | } |
| 3572 | if (isEmptyRecord(getContext(), FT, true)) |
| 3573 | continue; |
| 3574 | |
| 3575 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3576 | if (getContext().getLangOpts().CPlusPlus && |
| 3577 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3578 | continue; |
| 3579 | |
| 3580 | uint64_t FldMembers; |
| 3581 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3582 | return false; |
| 3583 | |
| 3584 | Members = (RD->isUnion() ? |
| 3585 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3586 | } |
| 3587 | |
| 3588 | if (!Base) |
| 3589 | return false; |
| 3590 | |
| 3591 | // Ensure there is no padding. |
| 3592 | if (getContext().getTypeSize(Base) * Members != |
| 3593 | getContext().getTypeSize(Ty)) |
| 3594 | return false; |
| 3595 | } else { |
| 3596 | Members = 1; |
| 3597 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3598 | Members = 2; |
| 3599 | Ty = CT->getElementType(); |
| 3600 | } |
| 3601 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3602 | // Most ABIs only support float, double, and some vector type widths. |
| 3603 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3604 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3605 | |
| 3606 | // The base type must be the same for all members. Types that |
| 3607 | // agree in both total size and mode (float vs. vector) are |
| 3608 | // treated as being equivalent here. |
| 3609 | const Type *TyPtr = Ty.getTypePtr(); |
| 3610 | if (!Base) |
| 3611 | Base = TyPtr; |
| 3612 | |
| 3613 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3614 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3615 | return false; |
| 3616 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3617 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3618 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3619 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3620 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3621 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3622 | // double, long double, or 128-bit vectors. |
| 3623 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3624 | if (BT->getKind() == BuiltinType::Float || |
| 3625 | BT->getKind() == BuiltinType::Double || |
| 3626 | BT->getKind() == BuiltinType::LongDouble) |
| 3627 | return true; |
| 3628 | } |
| 3629 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3630 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3631 | return true; |
| 3632 | } |
| 3633 | return false; |
| 3634 | } |
| 3635 | |
| 3636 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3637 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3638 | // Vector types require one register, floating point types require one |
| 3639 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3640 | uint32_t NumRegs = |
| 3641 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3642 | |
| 3643 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3644 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3647 | ABIArgInfo |
| 3648 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3649 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3650 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3651 | if (Ty->isAnyComplexType()) |
| 3652 | return ABIArgInfo::getDirect(); |
| 3653 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3654 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3655 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3656 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3657 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3658 | if (Size > 128) |
| 3659 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3660 | else if (Size < 128) { |
| 3661 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3662 | return ABIArgInfo::getDirect(CoerceTy); |
| 3663 | } |
| 3664 | } |
| 3665 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3666 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3667 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3668 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3669 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3670 | bool Align32; |
| 3671 | uint64_t ABIAlign = isAlignedParamType(Ty, Align32) ? |
| 3672 | (Align32 ? 32 : 16) : 8; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3673 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3674 | |
| 3675 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3676 | const Type *Base = nullptr; |
| 3677 | uint64_t Members = 0; |
| 3678 | if (Kind == ELFv2 && |
| 3679 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3680 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3681 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3682 | return ABIArgInfo::getDirect(CoerceTy); |
| 3683 | } |
| 3684 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3685 | // If an aggregate may end up fully in registers, we do not |
| 3686 | // use the ByVal method, but pass the aggregate as array. |
| 3687 | // This is usually beneficial since we avoid forcing the |
| 3688 | // back-end to store the argument to memory. |
| 3689 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3690 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3691 | llvm::Type *CoerceTy; |
| 3692 | |
| 3693 | // Types up to 8 bytes are passed as integer type (which will be |
| 3694 | // properly aligned in the argument save area doubleword). |
| 3695 | if (Bits <= GPRBits) |
| 3696 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3697 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3698 | // Larger types are passed as arrays, with the base type selected |
| 3699 | // according to the required alignment in the save area. |
| 3700 | else { |
| 3701 | uint64_t RegBits = ABIAlign * 8; |
| 3702 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3703 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3704 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3705 | } |
| 3706 | |
| 3707 | return ABIArgInfo::getDirect(CoerceTy); |
| 3708 | } |
| 3709 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3710 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3711 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3712 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | return (isPromotableTypeForABI(Ty) ? |
| 3716 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3717 | } |
| 3718 | |
| 3719 | ABIArgInfo |
| 3720 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3721 | if (RetTy->isVoidType()) |
| 3722 | return ABIArgInfo::getIgnore(); |
| 3723 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3724 | if (RetTy->isAnyComplexType()) |
| 3725 | return ABIArgInfo::getDirect(); |
| 3726 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3727 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3728 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3729 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3730 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3731 | if (Size > 128) |
| 3732 | return ABIArgInfo::getIndirect(0); |
| 3733 | else if (Size < 128) { |
| 3734 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3735 | return ABIArgInfo::getDirect(CoerceTy); |
| 3736 | } |
| 3737 | } |
| 3738 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3739 | if (isAggregateTypeForABI(RetTy)) { |
| 3740 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3741 | const Type *Base = nullptr; |
| 3742 | uint64_t Members = 0; |
| 3743 | if (Kind == ELFv2 && |
| 3744 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3745 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3746 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3747 | return ABIArgInfo::getDirect(CoerceTy); |
| 3748 | } |
| 3749 | |
| 3750 | // ELFv2 small aggregates are returned in up to two registers. |
| 3751 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3752 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3753 | if (Bits == 0) |
| 3754 | return ABIArgInfo::getIgnore(); |
| 3755 | |
| 3756 | llvm::Type *CoerceTy; |
| 3757 | if (Bits > GPRBits) { |
| 3758 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3759 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3760 | } else |
| 3761 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3762 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3763 | return ABIArgInfo::getDirect(CoerceTy); |
| 3764 | } |
| 3765 | |
| 3766 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3767 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3768 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3769 | |
| 3770 | return (isPromotableTypeForABI(RetTy) ? |
| 3771 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3772 | } |
| 3773 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3774 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3775 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3776 | QualType Ty, |
| 3777 | CodeGenFunction &CGF) const { |
| 3778 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3779 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3780 | |
| 3781 | CGBuilderTy &Builder = CGF.Builder; |
| 3782 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3783 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3784 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3785 | // Handle types that require 16-byte alignment in the parameter save area. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3786 | bool Align32; |
| 3787 | if (isAlignedParamType(Ty, Align32)) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3788 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3789 | AddrAsInt = Builder.CreateAdd(AddrAsInt, |
| 3790 | Builder.getInt64(Align32 ? 31 : 15)); |
| 3791 | AddrAsInt = Builder.CreateAnd(AddrAsInt, |
| 3792 | Builder.getInt64(Align32 ? -32 : -16)); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3793 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3794 | } |
| 3795 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3796 | // Update the va_list pointer. The pointer should be bumped by the |
| 3797 | // size of the object. We can trust getTypeSize() except for a complex |
| 3798 | // type whose base type is smaller than a doubleword. For these, the |
| 3799 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3800 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3801 | QualType BaseTy; |
| 3802 | unsigned CplxBaseSize = 0; |
| 3803 | |
| 3804 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3805 | BaseTy = CTy->getElementType(); |
| 3806 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3807 | if (CplxBaseSize < 8) |
| 3808 | SizeInBytes = 16; |
| 3809 | } |
| 3810 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3811 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3812 | llvm::Value *NextAddr = |
| 3813 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3814 | "ap.next"); |
| 3815 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3816 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3817 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3818 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3819 | // in separate doublewords. However, Clang expects us to produce a |
| 3820 | // pointer to a structure with the two parts packed tightly. So generate |
| 3821 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3822 | // and store them to a temporary structure. |
| 3823 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3824 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3825 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3826 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3827 | RealAddr = |
| 3828 | Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3829 | ImagAddr = |
| 3830 | Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3831 | } else { |
| 3832 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3833 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3834 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3835 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3836 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3837 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3838 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3839 | llvm::AllocaInst *Ptr = |
| 3840 | CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), "vacplx"); |
| 3841 | llvm::Value *RealPtr = |
| 3842 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 0, ".real"); |
| 3843 | llvm::Value *ImagPtr = |
| 3844 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 1, ".imag"); |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3845 | Builder.CreateStore(Real, RealPtr, false); |
| 3846 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3847 | return Ptr; |
| 3848 | } |
| 3849 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3850 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3851 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3852 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3853 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3854 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3855 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3856 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3857 | } |
| 3858 | |
| 3859 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3860 | return Builder.CreateBitCast(Addr, PTy); |
| 3861 | } |
| 3862 | |
| 3863 | static bool |
| 3864 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3865 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3866 | // This is calculated from the LLVM and GCC tables and verified |
| 3867 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3868 | |
| 3869 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3870 | |
| 3871 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3872 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3873 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3874 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3875 | |
| 3876 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3877 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3878 | |
| 3879 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3880 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3881 | |
| 3882 | // 64-76 are various 4-byte special-purpose registers: |
| 3883 | // 64: mq |
| 3884 | // 65: lr |
| 3885 | // 66: ctr |
| 3886 | // 67: ap |
| 3887 | // 68-75 cr0-7 |
| 3888 | // 76: xer |
| 3889 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3890 | |
| 3891 | // 77-108: v0-31, the 16-byte vector registers |
| 3892 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3893 | |
| 3894 | // 109: vrsave |
| 3895 | // 110: vscr |
| 3896 | // 111: spe_acc |
| 3897 | // 112: spefscr |
| 3898 | // 113: sfp |
| 3899 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3900 | |
| 3901 | return false; |
| 3902 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3903 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3904 | bool |
| 3905 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3906 | CodeGen::CodeGenFunction &CGF, |
| 3907 | llvm::Value *Address) const { |
| 3908 | |
| 3909 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3910 | } |
| 3911 | |
| 3912 | bool |
| 3913 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3914 | llvm::Value *Address) const { |
| 3915 | |
| 3916 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3917 | } |
| 3918 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3919 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3920 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3921 | //===----------------------------------------------------------------------===// |
| 3922 | |
| 3923 | namespace { |
| 3924 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3925 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3926 | public: |
| 3927 | enum ABIKind { |
| 3928 | AAPCS = 0, |
| 3929 | DarwinPCS |
| 3930 | }; |
| 3931 | |
| 3932 | private: |
| 3933 | ABIKind Kind; |
| 3934 | |
| 3935 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3936 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3937 | |
| 3938 | private: |
| 3939 | ABIKind getABIKind() const { return Kind; } |
| 3940 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3941 | |
| 3942 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3943 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3944 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3945 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3946 | uint64_t Members) const override; |
| 3947 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3948 | bool isIllegalVectorType(QualType Ty) const; |
| 3949 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 3950 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3951 | if (!getCXXABI().classifyReturnType(FI)) |
| 3952 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3953 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3954 | for (auto &it : FI.arguments()) |
| 3955 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
| 3958 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3959 | CodeGenFunction &CGF) const; |
| 3960 | |
| 3961 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3962 | CodeGenFunction &CGF) const; |
| 3963 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3964 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3965 | CodeGenFunction &CGF) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3966 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3967 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3968 | } |
| 3969 | }; |
| 3970 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3971 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3972 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3973 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3974 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3975 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3976 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3977 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3978 | } |
| 3979 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3980 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 3981 | return 31; |
| 3982 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3983 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3984 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3985 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3986 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3987 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3988 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3989 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3990 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3991 | // Handle illegal vector types here. |
| 3992 | if (isIllegalVectorType(Ty)) { |
| 3993 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3994 | if (Size <= 32) { |
| 3995 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3996 | return ABIArgInfo::getDirect(ResType); |
| 3997 | } |
| 3998 | if (Size == 64) { |
| 3999 | llvm::Type *ResType = |
| 4000 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4001 | return ABIArgInfo::getDirect(ResType); |
| 4002 | } |
| 4003 | if (Size == 128) { |
| 4004 | llvm::Type *ResType = |
| 4005 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4006 | return ABIArgInfo::getDirect(ResType); |
| 4007 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4008 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4009 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4010 | |
| 4011 | if (!isAggregateTypeForABI(Ty)) { |
| 4012 | // Treat an enum type as its underlying type. |
| 4013 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4014 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4015 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4016 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4017 | ? ABIArgInfo::getExtend() |
| 4018 | : ABIArgInfo::getDirect()); |
| 4019 | } |
| 4020 | |
| 4021 | // Structures with either a non-trivial destructor or a non-trivial |
| 4022 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4023 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4024 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4025 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4029 | // elsewhere for GNU compatibility. |
| 4030 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4031 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4032 | return ABIArgInfo::getIgnore(); |
| 4033 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4034 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4035 | } |
| 4036 | |
| 4037 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4038 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4039 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4040 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4041 | return ABIArgInfo::getDirect( |
| 4042 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4046 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4047 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4048 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4049 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4050 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4051 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4052 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4053 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4054 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4055 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4056 | } |
| 4057 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4058 | } |
| 4059 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4060 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4061 | } |
| 4062 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4063 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4064 | if (RetTy->isVoidType()) |
| 4065 | return ABIArgInfo::getIgnore(); |
| 4066 | |
| 4067 | // Large vector types should be returned via memory. |
| 4068 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 4069 | return ABIArgInfo::getIndirect(0); |
| 4070 | |
| 4071 | if (!isAggregateTypeForABI(RetTy)) { |
| 4072 | // Treat an enum type as its underlying type. |
| 4073 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4074 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4075 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4076 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4077 | ? ABIArgInfo::getExtend() |
| 4078 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4081 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4082 | return ABIArgInfo::getIgnore(); |
| 4083 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4084 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4085 | uint64_t Members = 0; |
| 4086 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4087 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4088 | return ABIArgInfo::getDirect(); |
| 4089 | |
| 4090 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4091 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4092 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4093 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4094 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4095 | |
| 4096 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4097 | // For aggregates with 16-byte alignment, we use i128. |
| 4098 | if (Alignment < 128 && Size == 128) { |
| 4099 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4100 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4101 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4102 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4103 | } |
| 4104 | |
| 4105 | return ABIArgInfo::getIndirect(0); |
| 4106 | } |
| 4107 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4108 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4109 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4110 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4111 | // Check whether VT is legal. |
| 4112 | unsigned NumElements = VT->getNumElements(); |
| 4113 | uint64_t Size = getContext().getTypeSize(VT); |
| 4114 | // NumElements should be power of 2 between 1 and 16. |
| 4115 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4116 | return true; |
| 4117 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4118 | } |
| 4119 | return false; |
| 4120 | } |
| 4121 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4122 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4123 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4124 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4125 | // but with the difference that any floating-point type is allowed, |
| 4126 | // including __fp16. |
| 4127 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4128 | if (BT->isFloatingPoint()) |
| 4129 | return true; |
| 4130 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4131 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4132 | if (VecSize == 64 || VecSize == 128) |
| 4133 | return true; |
| 4134 | } |
| 4135 | return false; |
| 4136 | } |
| 4137 | |
| 4138 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4139 | uint64_t Members) const { |
| 4140 | return Members <= 4; |
| 4141 | } |
| 4142 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4143 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, |
| 4144 | QualType Ty, |
| 4145 | CodeGenFunction &CGF) const { |
| 4146 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4147 | bool IsIndirect = AI.isIndirect(); |
| 4148 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4149 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4150 | if (IsIndirect) |
| 4151 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4152 | else if (AI.getCoerceToType()) |
| 4153 | BaseTy = AI.getCoerceToType(); |
| 4154 | |
| 4155 | unsigned NumRegs = 1; |
| 4156 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4157 | BaseTy = ArrTy->getElementType(); |
| 4158 | NumRegs = ArrTy->getNumElements(); |
| 4159 | } |
| 4160 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4161 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4162 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4163 | // Standard, section B.4: |
| 4164 | // |
| 4165 | // struct { |
| 4166 | // void *__stack; |
| 4167 | // void *__gr_top; |
| 4168 | // void *__vr_top; |
| 4169 | // int __gr_offs; |
| 4170 | // int __vr_offs; |
| 4171 | // }; |
| 4172 | |
| 4173 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4174 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4175 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4176 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4177 | auto &Ctx = CGF.getContext(); |
| 4178 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4179 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4180 | int reg_top_index; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4181 | int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8; |
| 4182 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4183 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4184 | reg_offs_p = |
| 4185 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4186 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4187 | reg_top_index = 1; // field number for __gr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4188 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4189 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4190 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4191 | reg_offs_p = |
| 4192 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4193 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4194 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4195 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | //======================================= |
| 4199 | // Find out where argument was passed |
| 4200 | //======================================= |
| 4201 | |
| 4202 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4203 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4204 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4205 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4206 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4207 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4208 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4209 | |
| 4210 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4211 | |
| 4212 | // 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] | 4213 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4214 | CGF.EmitBlock(MaybeRegBlock); |
| 4215 | |
| 4216 | // Integer arguments may need to correct register alignment (for example a |
| 4217 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4218 | // align __gr_offs to calculate the potential address. |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4219 | if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4220 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4221 | |
| 4222 | reg_offs = CGF.Builder.CreateAdd( |
| 4223 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4224 | "align_regoffs"); |
| 4225 | reg_offs = CGF.Builder.CreateAnd( |
| 4226 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4227 | "aligned_regoffs"); |
| 4228 | } |
| 4229 | |
| 4230 | // 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] | 4231 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4232 | NewOffset = CGF.Builder.CreateAdd( |
| 4233 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4234 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4235 | |
| 4236 | // Now we're in a position to decide whether this argument really was in |
| 4237 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4238 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4239 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4240 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4241 | |
| 4242 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4243 | |
| 4244 | //======================================= |
| 4245 | // Argument was in registers |
| 4246 | //======================================= |
| 4247 | |
| 4248 | // Now we emit the code for if the argument was originally passed in |
| 4249 | // registers. First start the appropriate block: |
| 4250 | CGF.EmitBlock(InRegBlock); |
| 4251 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4252 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4253 | reg_top_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, reg_top_index, |
| 4254 | "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4255 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4256 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4257 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4258 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4259 | |
| 4260 | if (IsIndirect) { |
| 4261 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4262 | // stored registers or on the stack will actually be a struct **. |
| 4263 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4264 | } |
| 4265 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4266 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4267 | uint64_t NumMembers = 0; |
| 4268 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4269 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4270 | // Homogeneous aggregates passed in registers will have their elements split |
| 4271 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4272 | // qN+1, ...). We reload and store into a temporary local variable |
| 4273 | // contiguously. |
| 4274 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 4275 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4276 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4277 | llvm::AllocaInst *Tmp = CGF.CreateTempAlloca(HFATy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4278 | int Offset = 0; |
| 4279 | |
| 4280 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 4281 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 4282 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4283 | llvm::Value *BaseOffset = |
| 4284 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 4285 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4286 | LoadAddr = CGF.Builder.CreateBitCast( |
| 4287 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4288 | llvm::Value *StoreAddr = |
| 4289 | CGF.Builder.CreateStructGEP(Tmp->getAllocatedType(), Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4290 | |
| 4291 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4292 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4293 | } |
| 4294 | |
| 4295 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4296 | } else { |
| 4297 | // Otherwise the object is contiguous in memory |
| 4298 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4299 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4300 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4301 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 4302 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 4303 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 4304 | |
| 4305 | BaseAddr = CGF.Builder.CreateAdd( |
| 4306 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4307 | |
| 4308 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 4309 | } |
| 4310 | |
| 4311 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4312 | } |
| 4313 | |
| 4314 | CGF.EmitBranch(ContBlock); |
| 4315 | |
| 4316 | //======================================= |
| 4317 | // Argument was on the stack |
| 4318 | //======================================= |
| 4319 | CGF.EmitBlock(OnStackBlock); |
| 4320 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4321 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4322 | stack_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "stack_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4323 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4324 | |
| 4325 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4326 | // floating-point ones might be affected. |
| 4327 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 4328 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4329 | |
| 4330 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4331 | |
| 4332 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4333 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4334 | "align_stack"); |
| 4335 | OnStackAddr = CGF.Builder.CreateAnd( |
| 4336 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4337 | "align_stack"); |
| 4338 | |
| 4339 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4340 | } |
| 4341 | |
| 4342 | uint64_t StackSize; |
| 4343 | if (IsIndirect) |
| 4344 | StackSize = 8; |
| 4345 | else |
| 4346 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 4347 | |
| 4348 | // All stack slots are 8 bytes |
| 4349 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4350 | |
| 4351 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4352 | llvm::Value *NewStack = |
| 4353 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 4354 | |
| 4355 | // Write the new value of __stack for the next call to va_arg |
| 4356 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4357 | |
| 4358 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4359 | Ctx.getTypeSize(Ty) < 64) { |
| 4360 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 4361 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4362 | |
| 4363 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4364 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4365 | |
| 4366 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4367 | } |
| 4368 | |
| 4369 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4370 | |
| 4371 | CGF.EmitBranch(ContBlock); |
| 4372 | |
| 4373 | //======================================= |
| 4374 | // Tidy up |
| 4375 | //======================================= |
| 4376 | CGF.EmitBlock(ContBlock); |
| 4377 | |
| 4378 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4379 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4380 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4381 | |
| 4382 | if (IsIndirect) |
| 4383 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4384 | |
| 4385 | return ResAddr; |
| 4386 | } |
| 4387 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4388 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, |
| 4389 | QualType Ty, |
| 4390 | CodeGenFunction &CGF) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4391 | // We do not support va_arg for aggregates or illegal vector types. |
| 4392 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 4393 | // other cases. |
| 4394 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4395 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4396 | |
| 4397 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 4398 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 4399 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4400 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4401 | uint64_t Members = 0; |
| 4402 | bool isHA = isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4403 | |
| 4404 | bool isIndirect = false; |
| 4405 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 4406 | // be passed indirectly. |
| 4407 | if (Size > 16 && !isHA) { |
| 4408 | isIndirect = true; |
| 4409 | Size = 8; |
| 4410 | Align = 8; |
| 4411 | } |
| 4412 | |
| 4413 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 4414 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 4415 | |
| 4416 | CGBuilderTy &Builder = CGF.Builder; |
| 4417 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4418 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4419 | |
| 4420 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4421 | // These are ignored for parameter passing purposes. |
| 4422 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4423 | return Builder.CreateBitCast(Addr, PTy); |
| 4424 | } |
| 4425 | |
| 4426 | const uint64_t MinABIAlign = 8; |
| 4427 | if (Align > MinABIAlign) { |
| 4428 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 4429 | Addr = Builder.CreateGEP(Addr, Offset); |
| 4430 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 4431 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 4432 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 4433 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 4434 | } |
| 4435 | |
| 4436 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 4437 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 4438 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 4439 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4440 | |
| 4441 | if (isIndirect) |
| 4442 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 4443 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4444 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4445 | |
| 4446 | return AddrTyped; |
| 4447 | } |
| 4448 | |
| 4449 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4450 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4451 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4452 | |
| 4453 | namespace { |
| 4454 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4455 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4456 | public: |
| 4457 | enum ABIKind { |
| 4458 | APCS = 0, |
| 4459 | AAPCS = 1, |
| 4460 | AAPCS_VFP |
| 4461 | }; |
| 4462 | |
| 4463 | private: |
| 4464 | ABIKind Kind; |
| 4465 | |
| 4466 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4467 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4468 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4469 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4470 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4471 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4472 | switch (getTarget().getTriple().getEnvironment()) { |
| 4473 | case llvm::Triple::Android: |
| 4474 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4475 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4476 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4477 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4478 | return true; |
| 4479 | default: |
| 4480 | return false; |
| 4481 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4484 | bool isEABIHF() const { |
| 4485 | switch (getTarget().getTriple().getEnvironment()) { |
| 4486 | case llvm::Triple::EABIHF: |
| 4487 | case llvm::Triple::GNUEABIHF: |
| 4488 | return true; |
| 4489 | default: |
| 4490 | return false; |
| 4491 | } |
| 4492 | } |
| 4493 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4494 | ABIKind getABIKind() const { return Kind; } |
| 4495 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4496 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4497 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4498 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4499 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4500 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4501 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4502 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4503 | uint64_t Members) const override; |
| 4504 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4505 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4506 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4507 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4508 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4509 | |
| 4510 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4511 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4512 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4513 | }; |
| 4514 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4515 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4516 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4517 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4518 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4519 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4520 | const ARMABIInfo &getABIInfo() const { |
| 4521 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4522 | } |
| 4523 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4524 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4525 | return 13; |
| 4526 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4527 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4528 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4529 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4530 | } |
| 4531 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4532 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4533 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4534 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4535 | |
| 4536 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4537 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4538 | return false; |
| 4539 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4540 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4541 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4542 | if (getABIInfo().isEABI()) return 88; |
| 4543 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4544 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4545 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4546 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4547 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4548 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4549 | if (!FD) |
| 4550 | return; |
| 4551 | |
| 4552 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4553 | if (!Attr) |
| 4554 | return; |
| 4555 | |
| 4556 | const char *Kind; |
| 4557 | switch (Attr->getInterrupt()) { |
| 4558 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4559 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4560 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4561 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4562 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4563 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4564 | } |
| 4565 | |
| 4566 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4567 | |
| 4568 | Fn->addFnAttr("interrupt", Kind); |
| 4569 | |
| 4570 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4571 | return; |
| 4572 | |
| 4573 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4574 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4575 | // the backend to perform a realignment as part of the function prologue. |
| 4576 | llvm::AttrBuilder B; |
| 4577 | B.addStackAlignmentAttr(8); |
| 4578 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4579 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4580 | llvm::AttributeSet::FunctionIndex, |
| 4581 | B)); |
| 4582 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4583 | }; |
| 4584 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4585 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4586 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4587 | CodeGen::CodeGenModule &CGM) const; |
| 4588 | |
| 4589 | public: |
| 4590 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4591 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4592 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4593 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4594 | CodeGen::CodeGenModule &CGM) const override; |
| 4595 | }; |
| 4596 | |
| 4597 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4598 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4599 | if (!isa<FunctionDecl>(D)) |
| 4600 | return; |
| 4601 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4602 | return; |
| 4603 | |
| 4604 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4605 | F->addFnAttr("stack-probe-size", |
| 4606 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4607 | } |
| 4608 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4609 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4610 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4611 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4612 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4613 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4614 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4615 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4616 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4617 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4618 | FI.getReturnInfo() = |
| 4619 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4620 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4621 | for (auto &I : FI.arguments()) |
| 4622 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4623 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4624 | // Always honor user-specified calling convention. |
| 4625 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4626 | return; |
| 4627 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4628 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4629 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4630 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4631 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4632 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4633 | /// Return the default calling convention that LLVM will use. |
| 4634 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4635 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4636 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4637 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4638 | else if (isEABI()) |
| 4639 | return llvm::CallingConv::ARM_AAPCS; |
| 4640 | else |
| 4641 | return llvm::CallingConv::ARM_APCS; |
| 4642 | } |
| 4643 | |
| 4644 | /// Return the calling convention that our ABI would like us to use |
| 4645 | /// as the C calling convention. |
| 4646 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4647 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4648 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4649 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4650 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4651 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4652 | llvm_unreachable("bad ABI kind"); |
| 4653 | } |
| 4654 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4655 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4656 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4657 | |
| 4658 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4659 | // they'd just match what LLVM will infer from the triple. |
| 4660 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4661 | if (abiCC != getLLVMDefaultCC()) |
| 4662 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4663 | |
| 4664 | BuiltinCC = (getABIKind() == APCS ? |
| 4665 | llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4668 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4669 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4670 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4671 | // A single-precision floating-point type (including promoted |
| 4672 | // half-precision types); A double-precision floating-point type; |
| 4673 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4674 | // with a Base Type of a single- or double-precision floating-point type, |
| 4675 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4676 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4677 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4678 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4679 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4680 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4681 | // Handle illegal vector types here. |
| 4682 | if (isIllegalVectorType(Ty)) { |
| 4683 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4684 | if (Size <= 32) { |
| 4685 | llvm::Type *ResType = |
| 4686 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4687 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4688 | } |
| 4689 | if (Size == 64) { |
| 4690 | llvm::Type *ResType = llvm::VectorType::get( |
| 4691 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4692 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4693 | } |
| 4694 | if (Size == 128) { |
| 4695 | llvm::Type *ResType = llvm::VectorType::get( |
| 4696 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4697 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4698 | } |
| 4699 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4700 | } |
| 4701 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4702 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4703 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4704 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4705 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4706 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4707 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4708 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4709 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4710 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4711 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4712 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4713 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4714 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4715 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4716 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4717 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4718 | return ABIArgInfo::getIgnore(); |
| 4719 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4720 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4721 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4722 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4723 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4724 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4725 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4726 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4727 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4728 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4729 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4730 | } |
| 4731 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4732 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4733 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4734 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4735 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4736 | uint64_t ABIAlign = 4; |
| 4737 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4738 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4739 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4740 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4741 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4742 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4743 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4744 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4747 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4748 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4749 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4750 | // FIXME: Try to match the types of the arguments more accurately where |
| 4751 | // we can. |
| 4752 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4753 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4754 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4755 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4756 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4757 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4758 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4759 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4760 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4763 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4764 | llvm::LLVMContext &VMContext) { |
| 4765 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4766 | // is called integer-like if its size is less than or equal to one word, and |
| 4767 | // the offset of each of its addressable sub-fields is zero. |
| 4768 | |
| 4769 | uint64_t Size = Context.getTypeSize(Ty); |
| 4770 | |
| 4771 | // Check that the type fits in a word. |
| 4772 | if (Size > 32) |
| 4773 | return false; |
| 4774 | |
| 4775 | // FIXME: Handle vector types! |
| 4776 | if (Ty->isVectorType()) |
| 4777 | return false; |
| 4778 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4779 | // Float types are never treated as "integer like". |
| 4780 | if (Ty->isRealFloatingType()) |
| 4781 | return false; |
| 4782 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4783 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4784 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4785 | return true; |
| 4786 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4787 | // Small complex integer types are "integer like". |
| 4788 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4789 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4790 | |
| 4791 | // Single element and zero sized arrays should be allowed, by the definition |
| 4792 | // above, but they are not. |
| 4793 | |
| 4794 | // Otherwise, it must be a record type. |
| 4795 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4796 | if (!RT) return false; |
| 4797 | |
| 4798 | // Ignore records with flexible arrays. |
| 4799 | const RecordDecl *RD = RT->getDecl(); |
| 4800 | if (RD->hasFlexibleArrayMember()) |
| 4801 | return false; |
| 4802 | |
| 4803 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4804 | // like". |
| 4805 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4806 | |
| 4807 | bool HadField = false; |
| 4808 | unsigned idx = 0; |
| 4809 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4810 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4811 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4812 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4813 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4814 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4815 | // struct { int : 0; int x } |
| 4816 | // is non-integer like according to gcc. |
| 4817 | if (FD->isBitField()) { |
| 4818 | if (!RD->isUnion()) |
| 4819 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4820 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4821 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4822 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4823 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4824 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4827 | // Check if this field is at offset 0. |
| 4828 | if (Layout.getFieldOffset(idx) != 0) |
| 4829 | return false; |
| 4830 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4831 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4832 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4833 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4834 | // Only allow at most one field in a structure. This doesn't match the |
| 4835 | // wording above, but follows gcc in situations with a field following an |
| 4836 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4837 | if (!RD->isUnion()) { |
| 4838 | if (HadField) |
| 4839 | return false; |
| 4840 | |
| 4841 | HadField = true; |
| 4842 | } |
| 4843 | } |
| 4844 | |
| 4845 | return true; |
| 4846 | } |
| 4847 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4848 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4849 | bool isVariadic) const { |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4850 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4851 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4852 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4853 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4854 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4855 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4856 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4857 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4858 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4859 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4860 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4861 | // Treat an enum type as its underlying type. |
| 4862 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4863 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4864 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4865 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4866 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4867 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4868 | |
| 4869 | // Are we following APCS? |
| 4870 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4871 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4872 | return ABIArgInfo::getIgnore(); |
| 4873 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4874 | // Complex types are all returned as packed integers. |
| 4875 | // |
| 4876 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4877 | // correctly. |
| 4878 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4879 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 4880 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4881 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4882 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4883 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4884 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4885 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4886 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4887 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4888 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4889 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4890 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | // Otherwise return in memory. |
| 4894 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4895 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4896 | |
| 4897 | // Otherwise this is an AAPCS variant. |
| 4898 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4899 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4900 | return ABIArgInfo::getIgnore(); |
| 4901 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4902 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4903 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4904 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4905 | uint64_t Members; |
| 4906 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4907 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4908 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4909 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4910 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4913 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4914 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4915 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4916 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4917 | if (getDataLayout().isBigEndian()) |
| 4918 | // 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] | 4919 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4920 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4921 | // Return in the smallest viable integer type. |
| 4922 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4923 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4924 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4925 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4926 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4929 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4930 | } |
| 4931 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4932 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4933 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4934 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4935 | // Check whether VT is legal. |
| 4936 | unsigned NumElements = VT->getNumElements(); |
| 4937 | uint64_t Size = getContext().getTypeSize(VT); |
| 4938 | // NumElements should be power of 2. |
| 4939 | if ((NumElements & (NumElements - 1)) != 0) |
| 4940 | return true; |
| 4941 | // Size should be greater than 32 bits. |
| 4942 | return Size <= 32; |
| 4943 | } |
| 4944 | return false; |
| 4945 | } |
| 4946 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4947 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4948 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4949 | // double, or 64-bit or 128-bit vectors. |
| 4950 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4951 | if (BT->getKind() == BuiltinType::Float || |
| 4952 | BT->getKind() == BuiltinType::Double || |
| 4953 | BT->getKind() == BuiltinType::LongDouble) |
| 4954 | return true; |
| 4955 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4956 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4957 | if (VecSize == 64 || VecSize == 128) |
| 4958 | return true; |
| 4959 | } |
| 4960 | return false; |
| 4961 | } |
| 4962 | |
| 4963 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4964 | uint64_t Members) const { |
| 4965 | return Members <= 4; |
| 4966 | } |
| 4967 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4968 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4969 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4970 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4971 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4972 | |
| 4973 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4974 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4975 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4976 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4977 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4978 | // These are ignored for parameter passing purposes. |
| 4979 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4980 | return Builder.CreateBitCast(Addr, PTy); |
| 4981 | } |
| 4982 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4983 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4984 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4985 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4986 | |
| 4987 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4988 | // 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] | 4989 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4990 | getABIKind() == ARMABIInfo::AAPCS) |
| 4991 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4992 | else |
| 4993 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4994 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4995 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4996 | IsIndirect = true; |
| 4997 | Size = 4; |
| 4998 | TyAlign = 4; |
| 4999 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5000 | |
| 5001 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 5002 | if (TyAlign > 4) { |
| 5003 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 5004 | "Alignment is not power of 2!"); |
| 5005 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 5006 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 5007 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5008 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 5009 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5010 | |
| 5011 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5012 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5013 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 5014 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5015 | "ap.next"); |
| 5016 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5017 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5018 | if (IsIndirect) |
| 5019 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 5020 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5021 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 5022 | // may not be correctly aligned for the vector type. We create an aligned |
| 5023 | // temporary space and copy the content over from ap.cur to the temporary |
| 5024 | // space. This is necessary if the natural alignment of the type is greater |
| 5025 | // than the ABI alignment. |
| 5026 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 5027 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 5028 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 5029 | "var.align"); |
| 5030 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 5031 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 5032 | Builder.CreateMemCpy(Dst, Src, |
| 5033 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 5034 | TyAlign, false); |
| 5035 | Addr = AlignedTemp; //The content is in aligned location. |
| 5036 | } |
| 5037 | llvm::Type *PTy = |
| 5038 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5039 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5040 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5041 | return AddrTyped; |
| 5042 | } |
| 5043 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5044 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5045 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5046 | //===----------------------------------------------------------------------===// |
| 5047 | |
| 5048 | namespace { |
| 5049 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5050 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5051 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5052 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5053 | |
| 5054 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5055 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5056 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5057 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5058 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5059 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5060 | }; |
| 5061 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5062 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5063 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5064 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5065 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5066 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5067 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5068 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5069 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5070 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5071 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5072 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5073 | }; |
| 5074 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5075 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5076 | if (RetTy->isVoidType()) |
| 5077 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5078 | |
| 5079 | // note: this is different from default ABI |
| 5080 | if (!RetTy->isScalarType()) |
| 5081 | return ABIArgInfo::getDirect(); |
| 5082 | |
| 5083 | // Treat an enum type as its underlying type. |
| 5084 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5085 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5086 | |
| 5087 | return (RetTy->isPromotableIntegerType() ? |
| 5088 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5089 | } |
| 5090 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5091 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5092 | // Treat an enum type as its underlying type. |
| 5093 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5094 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5095 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5096 | // Return aggregates type as indirect by value |
| 5097 | if (isAggregateTypeForABI(Ty)) |
| 5098 | return ABIArgInfo::getIndirect(0, /* byval */ true); |
| 5099 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5100 | return (Ty->isPromotableIntegerType() ? |
| 5101 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5104 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5105 | if (!getCXXABI().classifyReturnType(FI)) |
| 5106 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5107 | for (auto &I : FI.arguments()) |
| 5108 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5109 | |
| 5110 | // Always honor user-specified calling convention. |
| 5111 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5112 | return; |
| 5113 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5114 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5115 | } |
| 5116 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5117 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5118 | CodeGenFunction &CFG) const { |
| 5119 | llvm_unreachable("NVPTX does not support varargs"); |
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 NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5123 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5124 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5125 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5126 | if (!FD) return; |
| 5127 | |
| 5128 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5129 | |
| 5130 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5131 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5132 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5133 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5134 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5135 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5136 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5137 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5138 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5139 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5140 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5141 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5142 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5143 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5144 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5145 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5146 | // __global__ functions cannot be called from the device, we do not |
| 5147 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5148 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5149 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5150 | addNVVMMetadata(F, "kernel", 1); |
| 5151 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5152 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5153 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5154 | llvm::APSInt MaxThreads(32); |
| 5155 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5156 | if (MaxThreads > 0) |
| 5157 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5158 | |
| 5159 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5160 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5161 | // we don't have to add a PTX directive. |
| 5162 | if (Attr->getMinBlocks()) { |
| 5163 | llvm::APSInt MinBlocks(32); |
| 5164 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5165 | if (MinBlocks > 0) |
| 5166 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5167 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5168 | } |
| 5169 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5170 | } |
| 5171 | } |
| 5172 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5173 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5174 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5175 | llvm::Module *M = F->getParent(); |
| 5176 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5177 | |
| 5178 | // Get "nvvm.annotations" metadata node |
| 5179 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5180 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5181 | llvm::Metadata *MDVals[] = { |
| 5182 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5183 | llvm::ConstantAsMetadata::get( |
| 5184 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5185 | // Append metadata to nvvm.annotations |
| 5186 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5187 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5188 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5189 | |
| 5190 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5191 | // SystemZ ABI Implementation |
| 5192 | //===----------------------------------------------------------------------===// |
| 5193 | |
| 5194 | namespace { |
| 5195 | |
| 5196 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5197 | bool HasVector; |
| 5198 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5199 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5200 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5201 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5202 | |
| 5203 | bool isPromotableIntegerType(QualType Ty) const; |
| 5204 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5205 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5206 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5207 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5208 | |
| 5209 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5210 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5211 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5212 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5213 | if (!getCXXABI().classifyReturnType(FI)) |
| 5214 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5215 | for (auto &I : FI.arguments()) |
| 5216 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5219 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5220 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5221 | }; |
| 5222 | |
| 5223 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5224 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5225 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5226 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5227 | }; |
| 5228 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5229 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5230 | |
| 5231 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5232 | // Treat an enum type as its underlying type. |
| 5233 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5234 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5235 | |
| 5236 | // Promotable integer types are required to be promoted by the ABI. |
| 5237 | if (Ty->isPromotableIntegerType()) |
| 5238 | return true; |
| 5239 | |
| 5240 | // 32-bit values must also be promoted. |
| 5241 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5242 | switch (BT->getKind()) { |
| 5243 | case BuiltinType::Int: |
| 5244 | case BuiltinType::UInt: |
| 5245 | return true; |
| 5246 | default: |
| 5247 | return false; |
| 5248 | } |
| 5249 | return false; |
| 5250 | } |
| 5251 | |
| 5252 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5253 | return (Ty->isAnyComplexType() || |
| 5254 | Ty->isVectorType() || |
| 5255 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5258 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5259 | return (HasVector && |
| 5260 | Ty->isVectorType() && |
| 5261 | getContext().getTypeSize(Ty) <= 128); |
| 5262 | } |
| 5263 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5264 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5265 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5266 | switch (BT->getKind()) { |
| 5267 | case BuiltinType::Float: |
| 5268 | case BuiltinType::Double: |
| 5269 | return true; |
| 5270 | default: |
| 5271 | return false; |
| 5272 | } |
| 5273 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5274 | return false; |
| 5275 | } |
| 5276 | |
| 5277 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5278 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5279 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5280 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5281 | |
| 5282 | // If this is a C++ record, check the bases first. |
| 5283 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5284 | for (const auto &I : CXXRD->bases()) { |
| 5285 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5286 | |
| 5287 | // Empty bases don't affect things either way. |
| 5288 | if (isEmptyRecord(getContext(), Base, true)) |
| 5289 | continue; |
| 5290 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5291 | if (!Found.isNull()) |
| 5292 | return Ty; |
| 5293 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5297 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5298 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5299 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5300 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5301 | if (getContext().getLangOpts().CPlusPlus && |
| 5302 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5303 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5304 | |
| 5305 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5306 | // Nested structures still do though. |
| 5307 | if (!Found.isNull()) |
| 5308 | return Ty; |
| 5309 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5313 | // 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] | 5314 | if (!Found.isNull()) |
| 5315 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5318 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5319 | } |
| 5320 | |
| 5321 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5322 | CodeGenFunction &CGF) const { |
| 5323 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5324 | // struct { |
| 5325 | // i64 __gpr; |
| 5326 | // i64 __fpr; |
| 5327 | // i8 *__overflow_arg_area; |
| 5328 | // i8 *__reg_save_area; |
| 5329 | // }; |
| 5330 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5331 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5332 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5333 | // always passed on the stack. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5334 | Ty = CGF.getContext().getCanonicalType(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5335 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
| 5336 | llvm::Type *APTy = llvm::PointerType::getUnqual(ArgTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5337 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5338 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5339 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5340 | bool IsVector = false; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5341 | unsigned UnpaddedBitSize; |
| 5342 | if (IsIndirect) { |
| 5343 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5344 | UnpaddedBitSize = 64; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5345 | } else { |
| 5346 | if (AI.getCoerceToType()) |
| 5347 | ArgTy = AI.getCoerceToType(); |
| 5348 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5349 | IsVector = ArgTy->isVectorTy(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5350 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5351 | } |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5352 | unsigned PaddedBitSize = (IsVector && UnpaddedBitSize > 64) ? 128 : 64; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5353 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5354 | |
| 5355 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5356 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5357 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5358 | llvm::Type *IndexTy = CGF.Int64Ty; |
| 5359 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5360 | |
| 5361 | if (IsVector) { |
| 5362 | // Work out the address of a vector argument on the stack. |
| 5363 | // Vector arguments are always passed in the high bits of a |
| 5364 | // single (8 byte) or double (16 byte) stack slot. |
| 5365 | llvm::Value *OverflowArgAreaPtr = |
| 5366 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 2, |
| 5367 | "overflow_arg_area_ptr"); |
| 5368 | llvm::Value *OverflowArgArea = |
| 5369 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5370 | llvm::Value *MemAddr = |
| 5371 | CGF.Builder.CreateBitCast(OverflowArgArea, APTy, "mem_addr"); |
| 5372 | |
| 5373 | // Update overflow_arg_area_ptr pointer |
| 5374 | llvm::Value *NewOverflowArgArea = |
| 5375 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5376 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5377 | |
| 5378 | return MemAddr; |
| 5379 | } |
| 5380 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5381 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5382 | if (InFPRs) { |
| 5383 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5384 | RegCountField = 1; // __fpr |
| 5385 | RegSaveIndex = 16; // save offset for f0 |
| 5386 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5387 | } else { |
| 5388 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5389 | RegCountField = 0; // __gpr |
| 5390 | RegSaveIndex = 2; // save offset for r2 |
| 5391 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5392 | } |
| 5393 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5394 | llvm::Value *RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5395 | nullptr, VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5396 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5397 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5398 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5399 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5400 | |
| 5401 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5402 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5403 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5404 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5405 | |
| 5406 | // Emit code to load the value if it was passed in registers. |
| 5407 | CGF.EmitBlock(InRegBlock); |
| 5408 | |
| 5409 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5410 | llvm::Value *ScaledRegCount = |
| 5411 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5412 | llvm::Value *RegBase = |
| 5413 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5414 | llvm::Value *RegOffset = |
| 5415 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5416 | llvm::Value *RegSaveAreaPtr = |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5417 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5418 | llvm::Value *RegSaveArea = |
| 5419 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5420 | llvm::Value *RawRegAddr = |
| 5421 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5422 | llvm::Value *RegAddr = |
| 5423 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5424 | |
| 5425 | // Update the register count |
| 5426 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5427 | llvm::Value *NewRegCount = |
| 5428 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5429 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5430 | CGF.EmitBranch(ContBlock); |
| 5431 | |
| 5432 | // Emit code to load the value if it was passed in memory. |
| 5433 | CGF.EmitBlock(InMemBlock); |
| 5434 | |
| 5435 | // Work out the address of a stack argument. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5436 | llvm::Value *OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5437 | nullptr, VAListAddr, 2, "overflow_arg_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5438 | llvm::Value *OverflowArgArea = |
| 5439 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5440 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5441 | llvm::Value *RawMemAddr = |
| 5442 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5443 | llvm::Value *MemAddr = |
| 5444 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5445 | |
| 5446 | // Update overflow_arg_area_ptr pointer |
| 5447 | llvm::Value *NewOverflowArgArea = |
| 5448 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5449 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5450 | CGF.EmitBranch(ContBlock); |
| 5451 | |
| 5452 | // Return the appropriate result. |
| 5453 | CGF.EmitBlock(ContBlock); |
| 5454 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5455 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5456 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5457 | |
| 5458 | if (IsIndirect) |
| 5459 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5460 | |
| 5461 | return ResAddr; |
| 5462 | } |
| 5463 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5464 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5465 | if (RetTy->isVoidType()) |
| 5466 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5467 | if (isVectorArgumentType(RetTy)) |
| 5468 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5469 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5470 | return ABIArgInfo::getIndirect(0); |
| 5471 | return (isPromotableIntegerType(RetTy) ? |
| 5472 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5473 | } |
| 5474 | |
| 5475 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5476 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5477 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5478 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5479 | |
| 5480 | // Integers and enums are extended to full register width. |
| 5481 | if (isPromotableIntegerType(Ty)) |
| 5482 | return ABIArgInfo::getExtend(); |
| 5483 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5484 | // Handle vector types and vector-like structure types. Note that |
| 5485 | // as opposed to float-like structure types, we do not allow any |
| 5486 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5487 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5488 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5489 | if (isVectorArgumentType(SingleElementTy) && |
| 5490 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5491 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5492 | |
| 5493 | // 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] | 5494 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5495 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5496 | |
| 5497 | // Handle small structures. |
| 5498 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5499 | // Structures with flexible arrays have variable length, so really |
| 5500 | // fail the size test above. |
| 5501 | const RecordDecl *RD = RT->getDecl(); |
| 5502 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5503 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5504 | |
| 5505 | // The structure is passed as an unextended integer, a float, or a double. |
| 5506 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5507 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5508 | assert(Size == 32 || Size == 64); |
| 5509 | if (Size == 32) |
| 5510 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5511 | else |
| 5512 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5513 | } else |
| 5514 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5515 | return ABIArgInfo::getDirect(PassTy); |
| 5516 | } |
| 5517 | |
| 5518 | // Non-structure compounds are passed indirectly. |
| 5519 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5520 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5521 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5522 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
| 5525 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5526 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5527 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5528 | |
| 5529 | namespace { |
| 5530 | |
| 5531 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5532 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5533 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5534 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5535 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5536 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5537 | }; |
| 5538 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5539 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5540 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5541 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5542 | llvm::GlobalValue *GV, |
| 5543 | CodeGen::CodeGenModule &M) const { |
| 5544 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5545 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5546 | // Handle 'interrupt' attribute: |
| 5547 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5548 | |
| 5549 | // Step 1: Set ISR calling convention. |
| 5550 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5551 | |
| 5552 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5553 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5554 | |
| 5555 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5556 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5557 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5558 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5559 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5560 | } |
| 5561 | } |
| 5562 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5563 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5564 | // MIPS ABI Implementation. This works for both little-endian and |
| 5565 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5566 | //===----------------------------------------------------------------------===// |
| 5567 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5568 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5569 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5570 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5571 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5572 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5573 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5574 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5575 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5576 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5577 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5578 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5579 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5580 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5581 | |
| 5582 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5583 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5584 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5585 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5586 | CodeGenFunction &CGF) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5587 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5588 | }; |
| 5589 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5590 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5591 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5592 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5593 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5594 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5595 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5596 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5597 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5598 | return 29; |
| 5599 | } |
| 5600 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5601 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5602 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5603 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5604 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5605 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5606 | if (FD->hasAttr<Mips16Attr>()) { |
| 5607 | Fn->addFnAttr("mips16"); |
| 5608 | } |
| 5609 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5610 | Fn->addFnAttr("nomips16"); |
| 5611 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5612 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5613 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5614 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5615 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5616 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5617 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5618 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5619 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5620 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5621 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5622 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5623 | void MipsABIInfo::CoerceToIntArgs( |
| 5624 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5625 | llvm::IntegerType *IntTy = |
| 5626 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5627 | |
| 5628 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5629 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5630 | ArgList.push_back(IntTy); |
| 5631 | |
| 5632 | // If necessary, add one more integer type to ArgList. |
| 5633 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5634 | |
| 5635 | if (R) |
| 5636 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5637 | } |
| 5638 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5639 | // In N32/64, an aligned double precision floating point field is passed in |
| 5640 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5641 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5642 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5643 | |
| 5644 | if (IsO32) { |
| 5645 | CoerceToIntArgs(TySize, ArgList); |
| 5646 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5647 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5648 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5649 | if (Ty->isComplexType()) |
| 5650 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5651 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5652 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5653 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5654 | // Unions/vectors are passed in integer registers. |
| 5655 | if (!RT || !RT->isStructureOrClassType()) { |
| 5656 | CoerceToIntArgs(TySize, ArgList); |
| 5657 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5658 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5659 | |
| 5660 | const RecordDecl *RD = RT->getDecl(); |
| 5661 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5662 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5663 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5664 | uint64_t LastOffset = 0; |
| 5665 | unsigned idx = 0; |
| 5666 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5667 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5668 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5669 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5670 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5671 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5672 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5673 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5674 | |
| 5675 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5676 | continue; |
| 5677 | |
| 5678 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5679 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5680 | continue; |
| 5681 | |
| 5682 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5683 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5684 | ArgList.push_back(I64); |
| 5685 | |
| 5686 | // Add double type. |
| 5687 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5688 | LastOffset = Offset + 64; |
| 5689 | } |
| 5690 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5691 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5692 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5693 | |
| 5694 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5695 | } |
| 5696 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5697 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5698 | uint64_t Offset) const { |
| 5699 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5700 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5701 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5702 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5703 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5704 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5705 | ABIArgInfo |
| 5706 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5707 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5708 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5709 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5710 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5711 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5712 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5713 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5714 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5715 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5716 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5717 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5718 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5719 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5720 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5721 | return ABIArgInfo::getIgnore(); |
| 5722 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5723 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5724 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5725 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5726 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5727 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5728 | // If we have reached here, aggregates are passed directly by coercing to |
| 5729 | // another structure type. Padding is inserted if the offset of the |
| 5730 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 5731 | ABIArgInfo ArgInfo = |
| 5732 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 5733 | getPaddingType(OrigOffset, CurrOffset)); |
| 5734 | ArgInfo.setInReg(true); |
| 5735 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
| 5738 | // Treat an enum type as its underlying type. |
| 5739 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5740 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5741 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 5742 | // All integral types are promoted to the GPR width. |
| 5743 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5744 | return ABIArgInfo::getExtend(); |
| 5745 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5746 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5747 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5750 | llvm::Type* |
| 5751 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5752 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5753 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5754 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5755 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5756 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5757 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5758 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5759 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5760 | // N32/64 returns struct/classes in floating point registers if the |
| 5761 | // following conditions are met: |
| 5762 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5763 | // 2. The struct/class has one or two fields all of which are floating |
| 5764 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5765 | // 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] | 5766 | // |
| 5767 | // Any other composite results are returned in integer registers. |
| 5768 | // |
| 5769 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5770 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5771 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5772 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5773 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5774 | if (!BT || !BT->isFloatingPoint()) |
| 5775 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5776 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5777 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5778 | } |
| 5779 | |
| 5780 | if (b == e) |
| 5781 | return llvm::StructType::get(getVMContext(), RTList, |
| 5782 | RD->hasAttr<PackedAttr>()); |
| 5783 | |
| 5784 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5785 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5786 | } |
| 5787 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5788 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5789 | return llvm::StructType::get(getVMContext(), RTList); |
| 5790 | } |
| 5791 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5792 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5793 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5794 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5795 | if (RetTy->isVoidType()) |
| 5796 | return ABIArgInfo::getIgnore(); |
| 5797 | |
| 5798 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5799 | // However, N32/N64 ignores zero sized return values. |
| 5800 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5801 | return ABIArgInfo::getIgnore(); |
| 5802 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5803 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5804 | if (Size <= 128) { |
| 5805 | if (RetTy->isAnyComplexType()) |
| 5806 | return ABIArgInfo::getDirect(); |
| 5807 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5808 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 5809 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5810 | if (!IsO32 || |
| 5811 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 5812 | ABIArgInfo ArgInfo = |
| 5813 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5814 | ArgInfo.setInReg(true); |
| 5815 | return ArgInfo; |
| 5816 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5817 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5818 | |
| 5819 | return ABIArgInfo::getIndirect(0); |
| 5820 | } |
| 5821 | |
| 5822 | // Treat an enum type as its underlying type. |
| 5823 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5824 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5825 | |
| 5826 | return (RetTy->isPromotableIntegerType() ? |
| 5827 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5828 | } |
| 5829 | |
| 5830 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5831 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5832 | if (!getCXXABI().classifyReturnType(FI)) |
| 5833 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5834 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5835 | // 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] | 5836 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5837 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5838 | for (auto &I : FI.arguments()) |
| 5839 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5840 | } |
| 5841 | |
| 5842 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5843 | CodeGenFunction &CGF) const { |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5844 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5845 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5846 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5847 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 5848 | // 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] | 5849 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5850 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
| 5851 | if ((Ty->isIntegerType() && |
| 5852 | CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) || |
| 5853 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5854 | Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 5855 | Ty->isSignedIntegerType()); |
| 5856 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5857 | |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5858 | CGBuilderTy &Builder = CGF.Builder; |
| 5859 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5860 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Daniel Sanders | 8d36a61 | 2014-09-22 13:27:06 +0000 | [diff] [blame] | 5861 | int64_t TypeAlign = |
| 5862 | std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5863 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5864 | llvm::Value *AddrTyped; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5865 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
| 5866 | |
| 5867 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 5868 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5869 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5870 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5871 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
| 5872 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5873 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5874 | } |
| 5875 | else |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5876 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5877 | |
| 5878 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
| 5879 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5880 | unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty); |
| 5881 | uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5882 | llvm::Value *NextAddr = |
| 5883 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
| 5884 | "ap.next"); |
| 5885 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5886 | |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5887 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5890 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 5891 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5892 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5893 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 5894 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 5895 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5896 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5897 | return false; |
| 5898 | } |
| 5899 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5900 | bool |
| 5901 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5902 | llvm::Value *Address) const { |
| 5903 | // This information comes from gcc's implementation, which seems to |
| 5904 | // as canonical as it gets. |
| 5905 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5906 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5907 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5908 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5909 | |
| 5910 | // 0-31 are the general purpose registers, $0 - $31. |
| 5911 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5912 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5913 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5914 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5915 | |
| 5916 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5917 | // They are one bit wide and ignored here. |
| 5918 | |
| 5919 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5920 | // (coprocessor 1 is the FP unit) |
| 5921 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5922 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5923 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5924 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5925 | return false; |
| 5926 | } |
| 5927 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5928 | //===----------------------------------------------------------------------===// |
| 5929 | // 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] | 5930 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5931 | // handling. |
| 5932 | //===----------------------------------------------------------------------===// |
| 5933 | |
| 5934 | namespace { |
| 5935 | |
| 5936 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5937 | public: |
| 5938 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5939 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5940 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5941 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5942 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5943 | }; |
| 5944 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5945 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5946 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5947 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5948 | if (!FD) return; |
| 5949 | |
| 5950 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5951 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5952 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5953 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5954 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5955 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5956 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5957 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5958 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5959 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5960 | llvm::NamedMDNode *OpenCLMetadata = |
| 5961 | M.getModule().getOrInsertNamedMetadata( |
| 5962 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5963 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5964 | SmallVector<llvm::Metadata *, 5> Operands; |
| 5965 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5966 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5967 | Operands.push_back( |
| 5968 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5969 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 5970 | Operands.push_back( |
| 5971 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5972 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 5973 | Operands.push_back( |
| 5974 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5975 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5976 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5977 | // Add a boolean constant operand for "required" (true) or "hint" |
| 5978 | // (false) for implementing the work_group_size_hint attr later. |
| 5979 | // 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] | 5980 | Operands.push_back( |
| 5981 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5982 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5983 | } |
| 5984 | } |
| 5985 | } |
| 5986 | } |
| 5987 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5988 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5989 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5990 | //===----------------------------------------------------------------------===// |
| 5991 | // Hexagon ABI Implementation |
| 5992 | //===----------------------------------------------------------------------===// |
| 5993 | |
| 5994 | namespace { |
| 5995 | |
| 5996 | class HexagonABIInfo : public ABIInfo { |
| 5997 | |
| 5998 | |
| 5999 | public: |
| 6000 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6001 | |
| 6002 | private: |
| 6003 | |
| 6004 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6005 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6006 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6007 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6008 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6009 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6010 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6011 | }; |
| 6012 | |
| 6013 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6014 | public: |
| 6015 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6016 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6017 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6018 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6019 | return 29; |
| 6020 | } |
| 6021 | }; |
| 6022 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6023 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6024 | |
| 6025 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6026 | if (!getCXXABI().classifyReturnType(FI)) |
| 6027 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6028 | for (auto &I : FI.arguments()) |
| 6029 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6030 | } |
| 6031 | |
| 6032 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6033 | if (!isAggregateTypeForABI(Ty)) { |
| 6034 | // Treat an enum type as its underlying type. |
| 6035 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6036 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6037 | |
| 6038 | return (Ty->isPromotableIntegerType() ? |
| 6039 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6040 | } |
| 6041 | |
| 6042 | // Ignore empty records. |
| 6043 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6044 | return ABIArgInfo::getIgnore(); |
| 6045 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6046 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 6047 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6048 | |
| 6049 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6050 | if (Size > 64) |
| 6051 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6052 | // Pass in the smallest viable integer type. |
| 6053 | else if (Size > 32) |
| 6054 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6055 | else if (Size > 16) |
| 6056 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6057 | else if (Size > 8) |
| 6058 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6059 | else |
| 6060 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6061 | } |
| 6062 | |
| 6063 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6064 | if (RetTy->isVoidType()) |
| 6065 | return ABIArgInfo::getIgnore(); |
| 6066 | |
| 6067 | // Large vector types should be returned via memory. |
| 6068 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 6069 | return ABIArgInfo::getIndirect(0); |
| 6070 | |
| 6071 | if (!isAggregateTypeForABI(RetTy)) { |
| 6072 | // Treat an enum type as its underlying type. |
| 6073 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6074 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6075 | |
| 6076 | return (RetTy->isPromotableIntegerType() ? |
| 6077 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6078 | } |
| 6079 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6080 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6081 | return ABIArgInfo::getIgnore(); |
| 6082 | |
| 6083 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6084 | // are returned indirectly. |
| 6085 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6086 | if (Size <= 64) { |
| 6087 | // Return in the smallest viable integer type. |
| 6088 | if (Size <= 8) |
| 6089 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6090 | if (Size <= 16) |
| 6091 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6092 | if (Size <= 32) |
| 6093 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6094 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6095 | } |
| 6096 | |
| 6097 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6098 | } |
| 6099 | |
| 6100 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6101 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6102 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6103 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6104 | |
| 6105 | CGBuilderTy &Builder = CGF.Builder; |
| 6106 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 6107 | "ap"); |
| 6108 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6109 | llvm::Type *PTy = |
| 6110 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 6111 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 6112 | |
| 6113 | uint64_t Offset = |
| 6114 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 6115 | llvm::Value *NextAddr = |
| 6116 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 6117 | "ap.next"); |
| 6118 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 6119 | |
| 6120 | return AddrTyped; |
| 6121 | } |
| 6122 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6123 | //===----------------------------------------------------------------------===// |
| 6124 | // AMDGPU ABI Implementation |
| 6125 | //===----------------------------------------------------------------------===// |
| 6126 | |
| 6127 | namespace { |
| 6128 | |
| 6129 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6130 | public: |
| 6131 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6132 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6133 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6134 | CodeGen::CodeGenModule &M) const override; |
| 6135 | }; |
| 6136 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6137 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6138 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6139 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6140 | const Decl *D, |
| 6141 | llvm::GlobalValue *GV, |
| 6142 | CodeGen::CodeGenModule &M) const { |
| 6143 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 6144 | if (!FD) |
| 6145 | return; |
| 6146 | |
| 6147 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6148 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6149 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6150 | if (NumVGPR != 0) |
| 6151 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6152 | } |
| 6153 | |
| 6154 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6155 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6156 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6157 | if (NumSGPR != 0) |
| 6158 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6159 | } |
| 6160 | } |
| 6161 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6162 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6163 | //===----------------------------------------------------------------------===// |
| 6164 | // SPARC v9 ABI Implementation. |
| 6165 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6166 | // |
| 6167 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6168 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6169 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6170 | // |
| 6171 | // One case requires special care: |
| 6172 | // |
| 6173 | // struct mixed { |
| 6174 | // int i; |
| 6175 | // float f; |
| 6176 | // }; |
| 6177 | // |
| 6178 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6179 | // parameter array, but the int is passed in an integer register, and the float |
| 6180 | // is passed in a floating point register. This is represented as two arguments |
| 6181 | // with the LLVM IR inreg attribute: |
| 6182 | // |
| 6183 | // declare void f(i32 inreg %i, float inreg %f) |
| 6184 | // |
| 6185 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6186 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6187 | // bytes. |
| 6188 | // |
| 6189 | namespace { |
| 6190 | class SparcV9ABIInfo : public ABIInfo { |
| 6191 | public: |
| 6192 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6193 | |
| 6194 | private: |
| 6195 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6196 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6197 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6198 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6199 | |
| 6200 | // Coercion type builder for structs passed in registers. The coercion type |
| 6201 | // serves two purposes: |
| 6202 | // |
| 6203 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6204 | // in registers. |
| 6205 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6206 | // code generator knows to pass them in floating point registers. |
| 6207 | // |
| 6208 | // We also compute the InReg flag which indicates that the struct contains |
| 6209 | // aligned 32-bit floats. |
| 6210 | // |
| 6211 | struct CoerceBuilder { |
| 6212 | llvm::LLVMContext &Context; |
| 6213 | const llvm::DataLayout &DL; |
| 6214 | SmallVector<llvm::Type*, 8> Elems; |
| 6215 | uint64_t Size; |
| 6216 | bool InReg; |
| 6217 | |
| 6218 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6219 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6220 | |
| 6221 | // Pad Elems with integers until Size is ToSize. |
| 6222 | void pad(uint64_t ToSize) { |
| 6223 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6224 | if (ToSize == Size) |
| 6225 | return; |
| 6226 | |
| 6227 | // Finish the current 64-bit word. |
| 6228 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6229 | if (Aligned > Size && Aligned <= ToSize) { |
| 6230 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6231 | Size = Aligned; |
| 6232 | } |
| 6233 | |
| 6234 | // Add whole 64-bit words. |
| 6235 | while (Size + 64 <= ToSize) { |
| 6236 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6237 | Size += 64; |
| 6238 | } |
| 6239 | |
| 6240 | // Final in-word padding. |
| 6241 | if (Size < ToSize) { |
| 6242 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6243 | Size = ToSize; |
| 6244 | } |
| 6245 | } |
| 6246 | |
| 6247 | // Add a floating point element at Offset. |
| 6248 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6249 | // Unaligned floats are treated as integers. |
| 6250 | if (Offset % Bits) |
| 6251 | return; |
| 6252 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6253 | if (Bits < 64) |
| 6254 | InReg = true; |
| 6255 | pad(Offset); |
| 6256 | Elems.push_back(Ty); |
| 6257 | Size = Offset + Bits; |
| 6258 | } |
| 6259 | |
| 6260 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6261 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6262 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6263 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6264 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6265 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6266 | switch (ElemTy->getTypeID()) { |
| 6267 | case llvm::Type::StructTyID: |
| 6268 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6269 | break; |
| 6270 | case llvm::Type::FloatTyID: |
| 6271 | addFloat(ElemOffset, ElemTy, 32); |
| 6272 | break; |
| 6273 | case llvm::Type::DoubleTyID: |
| 6274 | addFloat(ElemOffset, ElemTy, 64); |
| 6275 | break; |
| 6276 | case llvm::Type::FP128TyID: |
| 6277 | addFloat(ElemOffset, ElemTy, 128); |
| 6278 | break; |
| 6279 | case llvm::Type::PointerTyID: |
| 6280 | if (ElemOffset % 64 == 0) { |
| 6281 | pad(ElemOffset); |
| 6282 | Elems.push_back(ElemTy); |
| 6283 | Size += 64; |
| 6284 | } |
| 6285 | break; |
| 6286 | default: |
| 6287 | break; |
| 6288 | } |
| 6289 | } |
| 6290 | } |
| 6291 | |
| 6292 | // Check if Ty is a usable substitute for the coercion type. |
| 6293 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6294 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6295 | } |
| 6296 | |
| 6297 | // Get the coercion type as a literal struct type. |
| 6298 | llvm::Type *getType() const { |
| 6299 | if (Elems.size() == 1) |
| 6300 | return Elems.front(); |
| 6301 | else |
| 6302 | return llvm::StructType::get(Context, Elems); |
| 6303 | } |
| 6304 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6305 | }; |
| 6306 | } // end anonymous namespace |
| 6307 | |
| 6308 | ABIArgInfo |
| 6309 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6310 | if (Ty->isVoidType()) |
| 6311 | return ABIArgInfo::getIgnore(); |
| 6312 | |
| 6313 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6314 | |
| 6315 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6316 | // pointer / sret pointer. |
| 6317 | if (Size > SizeLimit) |
| 6318 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 6319 | |
| 6320 | // Treat an enum type as its underlying type. |
| 6321 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6322 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6323 | |
| 6324 | // Integer types smaller than a register are extended. |
| 6325 | if (Size < 64 && Ty->isIntegerType()) |
| 6326 | return ABIArgInfo::getExtend(); |
| 6327 | |
| 6328 | // Other non-aggregates go in registers. |
| 6329 | if (!isAggregateTypeForABI(Ty)) |
| 6330 | return ABIArgInfo::getDirect(); |
| 6331 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6332 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6333 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6334 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 6335 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 6336 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6337 | // 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] | 6338 | // Build a coercion type from the LLVM struct type. |
| 6339 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6340 | if (!StrTy) |
| 6341 | return ABIArgInfo::getDirect(); |
| 6342 | |
| 6343 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6344 | CB.addStruct(0, StrTy); |
| 6345 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6346 | |
| 6347 | // Try to use the original type for coercion. |
| 6348 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6349 | |
| 6350 | if (CB.InReg) |
| 6351 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6352 | else |
| 6353 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6357 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6358 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6359 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6360 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6361 | AI.setCoerceToType(ArgTy); |
| 6362 | |
| 6363 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6364 | CGBuilderTy &Builder = CGF.Builder; |
| 6365 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6366 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6367 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6368 | llvm::Value *ArgAddr; |
| 6369 | unsigned Stride; |
| 6370 | |
| 6371 | switch (AI.getKind()) { |
| 6372 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6373 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6374 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6375 | |
| 6376 | case ABIArgInfo::Extend: |
| 6377 | Stride = 8; |
| 6378 | ArgAddr = Builder |
| 6379 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6380 | "extend"); |
| 6381 | break; |
| 6382 | |
| 6383 | case ABIArgInfo::Direct: |
| 6384 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6385 | ArgAddr = Addr; |
| 6386 | break; |
| 6387 | |
| 6388 | case ABIArgInfo::Indirect: |
| 6389 | Stride = 8; |
| 6390 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6391 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6392 | "indirect"); |
| 6393 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6394 | break; |
| 6395 | |
| 6396 | case ABIArgInfo::Ignore: |
| 6397 | return llvm::UndefValue::get(ArgPtrTy); |
| 6398 | } |
| 6399 | |
| 6400 | // Update VAList. |
| 6401 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6402 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6403 | |
| 6404 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6408 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6409 | for (auto &I : FI.arguments()) |
| 6410 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | namespace { |
| 6414 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6415 | public: |
| 6416 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6417 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6418 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6419 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6420 | return 14; |
| 6421 | } |
| 6422 | |
| 6423 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6424 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6425 | }; |
| 6426 | } // end anonymous namespace |
| 6427 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6428 | bool |
| 6429 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6430 | llvm::Value *Address) const { |
| 6431 | // This is calculated from the LLVM and GCC tables and verified |
| 6432 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6433 | |
| 6434 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6435 | |
| 6436 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6437 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6438 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6439 | |
| 6440 | // 0-31: the 8-byte general-purpose registers |
| 6441 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6442 | |
| 6443 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6444 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6445 | |
| 6446 | // Y = 64 |
| 6447 | // PSR = 65 |
| 6448 | // WIM = 66 |
| 6449 | // TBR = 67 |
| 6450 | // PC = 68 |
| 6451 | // NPC = 69 |
| 6452 | // FSR = 70 |
| 6453 | // CSR = 71 |
| 6454 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6455 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6456 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6457 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6458 | |
| 6459 | return false; |
| 6460 | } |
| 6461 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6462 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6463 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6464 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6465 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6466 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6467 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6468 | |
| 6469 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6470 | /// it by reference between functions that append to it. |
| 6471 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6472 | |
| 6473 | /// TypeStringCache caches the meta encodings of Types. |
| 6474 | /// |
| 6475 | /// The reason for caching TypeStrings is two fold: |
| 6476 | /// 1. To cache a type's encoding for later uses; |
| 6477 | /// 2. As a means to break recursive member type inclusion. |
| 6478 | /// |
| 6479 | /// A cache Entry can have a Status of: |
| 6480 | /// NonRecursive: The type encoding is not recursive; |
| 6481 | /// Recursive: The type encoding is recursive; |
| 6482 | /// Incomplete: An incomplete TypeString; |
| 6483 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6484 | /// Recursive type encoding. |
| 6485 | /// |
| 6486 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6487 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6488 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6489 | /// the type is encountered. |
| 6490 | /// |
| 6491 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6492 | /// possible. The type itself is recursive and it may contain other types which |
| 6493 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6494 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6495 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6496 | /// |
| 6497 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6498 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6499 | /// are placed into the cache during type expansion as a means to identify and |
| 6500 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6501 | /// the entry becomes IncompleteUsed. |
| 6502 | /// |
| 6503 | /// During the expansion of a RecordType's members: |
| 6504 | /// |
| 6505 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6506 | /// cached encoding is used; |
| 6507 | /// |
| 6508 | /// If the cache contains a Recursive encoding for the member type, the |
| 6509 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6510 | /// |
| 6511 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6512 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6513 | /// |
| 6514 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6515 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6516 | /// it is swapped back in; |
| 6517 | /// |
| 6518 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6519 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6520 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6521 | /// |
| 6522 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6523 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6524 | /// Else the member is part of a recursive type and thus the recursion has |
| 6525 | /// been exited too soon for the encoding to be correct for the member. |
| 6526 | /// |
| 6527 | class TypeStringCache { |
| 6528 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6529 | struct Entry { |
| 6530 | std::string Str; // The encoded TypeString for the type. |
| 6531 | enum Status State; // Information about the encoding in 'Str'. |
| 6532 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6533 | // during the expansion of RecordType's members. |
| 6534 | }; |
| 6535 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6536 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6537 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6538 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame^] | 6539 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6540 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6541 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6542 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6543 | bool IsRecursive); |
| 6544 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6545 | }; |
| 6546 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6547 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6548 | /// FieldEncoding is a helper for this ordering process. |
| 6549 | class FieldEncoding { |
| 6550 | bool HasName; |
| 6551 | std::string Enc; |
| 6552 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame^] | 6553 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
| 6554 | StringRef str() {return Enc.c_str();} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6555 | bool operator<(const FieldEncoding &rhs) const { |
| 6556 | if (HasName != rhs.HasName) return HasName; |
| 6557 | return Enc < rhs.Enc; |
| 6558 | } |
| 6559 | }; |
| 6560 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6561 | class XCoreABIInfo : public DefaultABIInfo { |
| 6562 | public: |
| 6563 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6564 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6565 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6566 | }; |
| 6567 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6568 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6569 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6570 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6571 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6572 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6573 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6574 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6575 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6576 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6577 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6578 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6579 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6580 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6581 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6582 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6583 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6584 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6585 | CGF.Int8PtrPtrTy); |
| 6586 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6587 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6588 | // Handle the argument. |
| 6589 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6590 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6591 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6592 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6593 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6594 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6595 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6596 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6597 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6598 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6599 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6600 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6601 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6602 | ArgSize = 0; |
| 6603 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6604 | case ABIArgInfo::Extend: |
| 6605 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6606 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6607 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6608 | if (ArgSize < 4) |
| 6609 | ArgSize = 4; |
| 6610 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6611 | case ABIArgInfo::Indirect: |
| 6612 | llvm::Value *ArgAddr; |
| 6613 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6614 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6615 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6616 | ArgSize = 4; |
| 6617 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6618 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6619 | |
| 6620 | // Increment the VAList. |
| 6621 | if (ArgSize) { |
| 6622 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6623 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6624 | } |
| 6625 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6626 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6627 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6628 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6629 | /// into the cache as a means to identify and break recursion. |
| 6630 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6631 | /// be reinserted by removeIncomplete(). |
| 6632 | /// All other types of encoding should have been used rather than arriving here. |
| 6633 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6634 | std::string StubEnc) { |
| 6635 | if (!ID) |
| 6636 | return; |
| 6637 | Entry &E = Map[ID]; |
| 6638 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6639 | "Incorrectly use of addIncomplete"); |
| 6640 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6641 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6642 | E.Str.swap(StubEnc); |
| 6643 | E.State = Incomplete; |
| 6644 | ++IncompleteCount; |
| 6645 | } |
| 6646 | |
| 6647 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6648 | /// must be removed from the cache. |
| 6649 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6650 | /// Returns true if the RecordType was defined recursively. |
| 6651 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6652 | if (!ID) |
| 6653 | return false; |
| 6654 | auto I = Map.find(ID); |
| 6655 | assert(I != Map.end() && "Entry not present"); |
| 6656 | Entry &E = I->second; |
| 6657 | assert( (E.State == Incomplete || |
| 6658 | E.State == IncompleteUsed) && |
| 6659 | "Entry must be an incomplete type"); |
| 6660 | bool IsRecursive = false; |
| 6661 | if (E.State == IncompleteUsed) { |
| 6662 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6663 | IsRecursive = true; |
| 6664 | --IncompleteUsedCount; |
| 6665 | } |
| 6666 | if (E.Swapped.empty()) |
| 6667 | Map.erase(I); |
| 6668 | else { |
| 6669 | // Swap the Recursive back. |
| 6670 | E.Swapped.swap(E.Str); |
| 6671 | E.Swapped.clear(); |
| 6672 | E.State = Recursive; |
| 6673 | } |
| 6674 | --IncompleteCount; |
| 6675 | return IsRecursive; |
| 6676 | } |
| 6677 | |
| 6678 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6679 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6680 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6681 | bool IsRecursive) { |
| 6682 | if (!ID || IncompleteUsedCount) |
| 6683 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6684 | Entry &E = Map[ID]; |
| 6685 | if (IsRecursive && !E.Str.empty()) { |
| 6686 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6687 | "This is not the same Recursive entry"); |
| 6688 | // The parent container was not recursive after all, so we could have used |
| 6689 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6690 | // we started viz: IncompleteCount!=0. |
| 6691 | return; |
| 6692 | } |
| 6693 | assert(E.Str.empty() && "Entry already present"); |
| 6694 | E.Str = Str.str(); |
| 6695 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6696 | } |
| 6697 | |
| 6698 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6699 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6700 | /// encoding is Recursive, return an empty StringRef. |
| 6701 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6702 | if (!ID) |
| 6703 | return StringRef(); // We have no key. |
| 6704 | auto I = Map.find(ID); |
| 6705 | if (I == Map.end()) |
| 6706 | return StringRef(); // We have no encoding. |
| 6707 | Entry &E = I->second; |
| 6708 | if (E.State == Recursive && IncompleteCount) |
| 6709 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6710 | |
| 6711 | if (E.State == Incomplete) { |
| 6712 | // The incomplete type is being used to break out of recursion. |
| 6713 | E.State = IncompleteUsed; |
| 6714 | ++IncompleteUsedCount; |
| 6715 | } |
| 6716 | return E.Str.c_str(); |
| 6717 | } |
| 6718 | |
| 6719 | /// The XCore ABI includes a type information section that communicates symbol |
| 6720 | /// type information to the linker. The linker uses this information to verify |
| 6721 | /// safety/correctness of things such as array bound and pointers et al. |
| 6722 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6723 | /// This type information (TypeString) is emitted into meta data for all global |
| 6724 | /// symbols: definitions, declarations, functions & variables. |
| 6725 | /// |
| 6726 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6727 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6728 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6729 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6730 | /// |
| 6731 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6732 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6733 | |
| 6734 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6735 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6736 | CodeGen::CodeGenModule &CGM) const { |
| 6737 | SmallStringEnc Enc; |
| 6738 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6739 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6740 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 6741 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6742 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6743 | llvm::NamedMDNode *MD = |
| 6744 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6745 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6746 | } |
| 6747 | } |
| 6748 | |
| 6749 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6750 | const CodeGen::CodeGenModule &CGM, |
| 6751 | TypeStringCache &TSC); |
| 6752 | |
| 6753 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6754 | /// Builds a SmallVector containing the encoded field types in declaration |
| 6755 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6756 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6757 | const RecordDecl *RD, |
| 6758 | const CodeGen::CodeGenModule &CGM, |
| 6759 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6760 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6761 | SmallStringEnc Enc; |
| 6762 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6763 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6764 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6765 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6766 | Enc += "b("; |
| 6767 | llvm::raw_svector_ostream OS(Enc); |
| 6768 | OS.resync(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6769 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6770 | OS.flush(); |
| 6771 | Enc += ':'; |
| 6772 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6773 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6774 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6775 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6776 | Enc += ')'; |
| 6777 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 6778 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6779 | } |
| 6780 | return true; |
| 6781 | } |
| 6782 | |
| 6783 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6784 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6785 | /// Union types have their fields ordered according to the ABI. |
| 6786 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6787 | const CodeGen::CodeGenModule &CGM, |
| 6788 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6789 | // Append the cached TypeString if we have one. |
| 6790 | StringRef TypeString = TSC.lookupStr(ID); |
| 6791 | if (!TypeString.empty()) { |
| 6792 | Enc += TypeString; |
| 6793 | return true; |
| 6794 | } |
| 6795 | |
| 6796 | // Start to emit an incomplete TypeString. |
| 6797 | size_t Start = Enc.size(); |
| 6798 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6799 | Enc += '('; |
| 6800 | if (ID) |
| 6801 | Enc += ID->getName(); |
| 6802 | Enc += "){"; |
| 6803 | |
| 6804 | // We collect all encoded fields and order as necessary. |
| 6805 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6806 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6807 | if (RD && !RD->field_empty()) { |
| 6808 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6809 | // so that recursive calls to this RecordType will use it whilst building a |
| 6810 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6811 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6812 | std::string StubEnc(Enc.substr(Start).str()); |
| 6813 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6814 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6815 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6816 | (void) TSC.removeIncomplete(ID); |
| 6817 | return false; |
| 6818 | } |
| 6819 | IsRecursive = TSC.removeIncomplete(ID); |
| 6820 | // The ABI requires unions to be sorted but not structures. |
| 6821 | // See FieldEncoding::operator< for sort algorithm. |
| 6822 | if (RT->isUnionType()) |
| 6823 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6824 | // We can now complete the TypeString. |
| 6825 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6826 | for (unsigned I = 0; I != E; ++I) { |
| 6827 | if (I) |
| 6828 | Enc += ','; |
| 6829 | Enc += FE[I].str(); |
| 6830 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6831 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6832 | Enc += '}'; |
| 6833 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6834 | return true; |
| 6835 | } |
| 6836 | |
| 6837 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6838 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6839 | TypeStringCache &TSC, |
| 6840 | const IdentifierInfo *ID) { |
| 6841 | // Append the cached TypeString if we have one. |
| 6842 | StringRef TypeString = TSC.lookupStr(ID); |
| 6843 | if (!TypeString.empty()) { |
| 6844 | Enc += TypeString; |
| 6845 | return true; |
| 6846 | } |
| 6847 | |
| 6848 | size_t Start = Enc.size(); |
| 6849 | Enc += "e("; |
| 6850 | if (ID) |
| 6851 | Enc += ID->getName(); |
| 6852 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6853 | |
| 6854 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6855 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6856 | SmallVector<FieldEncoding, 16> FE; |
| 6857 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6858 | ++I) { |
| 6859 | SmallStringEnc EnumEnc; |
| 6860 | EnumEnc += "m("; |
| 6861 | EnumEnc += I->getName(); |
| 6862 | EnumEnc += "){"; |
| 6863 | I->getInitVal().toString(EnumEnc); |
| 6864 | EnumEnc += '}'; |
| 6865 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6866 | } |
| 6867 | std::sort(FE.begin(), FE.end()); |
| 6868 | unsigned E = FE.size(); |
| 6869 | for (unsigned I = 0; I != E; ++I) { |
| 6870 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6871 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6872 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6873 | } |
| 6874 | } |
| 6875 | Enc += '}'; |
| 6876 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6877 | return true; |
| 6878 | } |
| 6879 | |
| 6880 | /// Appends type's qualifier to Enc. |
| 6881 | /// This is done prior to appending the type's encoding. |
| 6882 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6883 | // Qualifiers are emitted in alphabetical order. |
| 6884 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6885 | int Lookup = 0; |
| 6886 | if (QT.isConstQualified()) |
| 6887 | Lookup += 1<<0; |
| 6888 | if (QT.isRestrictQualified()) |
| 6889 | Lookup += 1<<1; |
| 6890 | if (QT.isVolatileQualified()) |
| 6891 | Lookup += 1<<2; |
| 6892 | Enc += Table[Lookup]; |
| 6893 | } |
| 6894 | |
| 6895 | /// Appends built-in types to Enc. |
| 6896 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6897 | const char *EncType; |
| 6898 | switch (BT->getKind()) { |
| 6899 | case BuiltinType::Void: |
| 6900 | EncType = "0"; |
| 6901 | break; |
| 6902 | case BuiltinType::Bool: |
| 6903 | EncType = "b"; |
| 6904 | break; |
| 6905 | case BuiltinType::Char_U: |
| 6906 | EncType = "uc"; |
| 6907 | break; |
| 6908 | case BuiltinType::UChar: |
| 6909 | EncType = "uc"; |
| 6910 | break; |
| 6911 | case BuiltinType::SChar: |
| 6912 | EncType = "sc"; |
| 6913 | break; |
| 6914 | case BuiltinType::UShort: |
| 6915 | EncType = "us"; |
| 6916 | break; |
| 6917 | case BuiltinType::Short: |
| 6918 | EncType = "ss"; |
| 6919 | break; |
| 6920 | case BuiltinType::UInt: |
| 6921 | EncType = "ui"; |
| 6922 | break; |
| 6923 | case BuiltinType::Int: |
| 6924 | EncType = "si"; |
| 6925 | break; |
| 6926 | case BuiltinType::ULong: |
| 6927 | EncType = "ul"; |
| 6928 | break; |
| 6929 | case BuiltinType::Long: |
| 6930 | EncType = "sl"; |
| 6931 | break; |
| 6932 | case BuiltinType::ULongLong: |
| 6933 | EncType = "ull"; |
| 6934 | break; |
| 6935 | case BuiltinType::LongLong: |
| 6936 | EncType = "sll"; |
| 6937 | break; |
| 6938 | case BuiltinType::Float: |
| 6939 | EncType = "ft"; |
| 6940 | break; |
| 6941 | case BuiltinType::Double: |
| 6942 | EncType = "d"; |
| 6943 | break; |
| 6944 | case BuiltinType::LongDouble: |
| 6945 | EncType = "ld"; |
| 6946 | break; |
| 6947 | default: |
| 6948 | return false; |
| 6949 | } |
| 6950 | Enc += EncType; |
| 6951 | return true; |
| 6952 | } |
| 6953 | |
| 6954 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6955 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6956 | const CodeGen::CodeGenModule &CGM, |
| 6957 | TypeStringCache &TSC) { |
| 6958 | Enc += "p("; |
| 6959 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6960 | return false; |
| 6961 | Enc += ')'; |
| 6962 | return true; |
| 6963 | } |
| 6964 | |
| 6965 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6966 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6967 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6968 | const CodeGen::CodeGenModule &CGM, |
| 6969 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6970 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6971 | return false; |
| 6972 | Enc += "a("; |
| 6973 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6974 | CAT->getSize().toStringUnsigned(Enc); |
| 6975 | else |
| 6976 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6977 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6978 | // The Qualifiers should be attached to the type rather than the array. |
| 6979 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6980 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6981 | return false; |
| 6982 | Enc += ')'; |
| 6983 | return true; |
| 6984 | } |
| 6985 | |
| 6986 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6987 | /// and the arguments. |
| 6988 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6989 | const CodeGen::CodeGenModule &CGM, |
| 6990 | TypeStringCache &TSC) { |
| 6991 | Enc += "f{"; |
| 6992 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 6993 | return false; |
| 6994 | Enc += "}("; |
| 6995 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 6996 | // N.B. we are only interested in the adjusted param types. |
| 6997 | auto I = FPT->param_type_begin(); |
| 6998 | auto E = FPT->param_type_end(); |
| 6999 | if (I != E) { |
| 7000 | do { |
| 7001 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7002 | return false; |
| 7003 | ++I; |
| 7004 | if (I != E) |
| 7005 | Enc += ','; |
| 7006 | } while (I != E); |
| 7007 | if (FPT->isVariadic()) |
| 7008 | Enc += ",va"; |
| 7009 | } else { |
| 7010 | if (FPT->isVariadic()) |
| 7011 | Enc += "va"; |
| 7012 | else |
| 7013 | Enc += '0'; |
| 7014 | } |
| 7015 | } |
| 7016 | Enc += ')'; |
| 7017 | return true; |
| 7018 | } |
| 7019 | |
| 7020 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7021 | /// type encodings. |
| 7022 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7023 | const CodeGen::CodeGenModule &CGM, |
| 7024 | TypeStringCache &TSC) { |
| 7025 | |
| 7026 | QualType QT = QType.getCanonicalType(); |
| 7027 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7028 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7029 | // The Qualifiers should be attached to the type rather than the array. |
| 7030 | // Thus we don't call appendQualifier() here. |
| 7031 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7032 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7033 | appendQualifier(Enc, QT); |
| 7034 | |
| 7035 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7036 | return appendBuiltinType(Enc, BT); |
| 7037 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7038 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7039 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7040 | |
| 7041 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7042 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7043 | |
| 7044 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7045 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7046 | |
| 7047 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7048 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7049 | |
| 7050 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7051 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7052 | |
| 7053 | return false; |
| 7054 | } |
| 7055 | |
| 7056 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7057 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7058 | if (!D) |
| 7059 | return false; |
| 7060 | |
| 7061 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7062 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7063 | return false; |
| 7064 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7065 | } |
| 7066 | |
| 7067 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7068 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7069 | return false; |
| 7070 | QualType QT = VD->getType().getCanonicalType(); |
| 7071 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7072 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7073 | // The Qualifiers should be attached to the type rather than the array. |
| 7074 | // Thus we don't call appendQualifier() here. |
| 7075 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7076 | } |
| 7077 | return appendType(Enc, QT, CGM, TSC); |
| 7078 | } |
| 7079 | return false; |
| 7080 | } |
| 7081 | |
| 7082 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7083 | //===----------------------------------------------------------------------===// |
| 7084 | // Driver code |
| 7085 | //===----------------------------------------------------------------------===// |
| 7086 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7087 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7088 | return getTarget().getTriple(); |
| 7089 | } |
| 7090 | |
| 7091 | bool CodeGenModule::supportsCOMDAT() const { |
| 7092 | return !getTriple().isOSBinFormatMachO(); |
| 7093 | } |
| 7094 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7095 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7096 | if (TheTargetCodeGenInfo) |
| 7097 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7098 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7099 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7100 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7101 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7102 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7103 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7104 | case llvm::Triple::le32: |
| 7105 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7106 | case llvm::Triple::mips: |
| 7107 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 7108 | if (Triple.getOS() == llvm::Triple::NaCl) |
| 7109 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7110 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7111 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7112 | case llvm::Triple::mips64: |
| 7113 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7114 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7115 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7116 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7117 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7118 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7119 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7120 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7121 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7122 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7123 | } |
| 7124 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7125 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7126 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7127 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7128 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7129 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7130 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7131 | TheTargetCodeGenInfo = |
| 7132 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7133 | return *TheTargetCodeGenInfo; |
| 7134 | } |
| 7135 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7136 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7137 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7138 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7139 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7140 | (CodeGenOpts.FloatABI != "soft" && |
| 7141 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7142 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7143 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7144 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7145 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7146 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7147 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7148 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7149 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7150 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7151 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7152 | if (getTarget().getABI() == "elfv2") |
| 7153 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7154 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7155 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7156 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7157 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7158 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7159 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7160 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7161 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7162 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7163 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7164 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7165 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7166 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7167 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7168 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7169 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7170 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7171 | case llvm::Triple::nvptx: |
| 7172 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7173 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7174 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7175 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7176 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7177 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7178 | case llvm::Triple::systemz: { |
| 7179 | bool HasVector = getTarget().getABI() == "vector"; |
| 7180 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7181 | HasVector)); |
| 7182 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7183 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7184 | case llvm::Triple::tce: |
| 7185 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7186 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7187 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7188 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 7189 | bool IsSmallStructInRegABI = |
| 7190 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7191 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7192 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7193 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7194 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
| 7195 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7196 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7197 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7198 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
| 7199 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7200 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7201 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7202 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7203 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7204 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7205 | StringRef ABI = getTarget().getABI(); |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 7206 | X86AVXABILevel AVXLevel = (ABI == "avx512" ? X86AVXABILevel::AVX512 : |
| 7207 | ABI == "avx" ? X86AVXABILevel::AVX : |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7208 | X86AVXABILevel::None); |
| 7209 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7210 | switch (Triple.getOS()) { |
| 7211 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7212 | return *(TheTargetCodeGenInfo = |
| 7213 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7214 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7215 | return *(TheTargetCodeGenInfo = |
| 7216 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7217 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 7218 | return *(TheTargetCodeGenInfo = |
| 7219 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7220 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7221 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7222 | case llvm::Triple::hexagon: |
| 7223 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7224 | case llvm::Triple::r600: |
| 7225 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7226 | case llvm::Triple::amdgcn: |
| 7227 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7228 | case llvm::Triple::sparcv9: |
| 7229 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7230 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7231 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7232 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7233 | } |