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 | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 688 | } // namespace |
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, |
| 1397 | AVX |
| 1398 | }; |
| 1399 | |
| 1400 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 1401 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 1402 | switch (AVXLevel) { |
| 1403 | case X86AVXABILevel::AVX: |
| 1404 | return 256; |
| 1405 | case X86AVXABILevel::None: |
| 1406 | return 128; |
| 1407 | } |
| 1408 | } |
| 1409 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1410 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1411 | class X86_64ABIInfo : public ABIInfo { |
| 1412 | enum Class { |
| 1413 | Integer = 0, |
| 1414 | SSE, |
| 1415 | SSEUp, |
| 1416 | X87, |
| 1417 | X87Up, |
| 1418 | ComplexX87, |
| 1419 | NoClass, |
| 1420 | Memory |
| 1421 | }; |
| 1422 | |
| 1423 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1424 | /// |
| 1425 | /// Merge an accumulating classification \arg Accum with a field |
| 1426 | /// classification \arg Field. |
| 1427 | /// |
| 1428 | /// \param Accum - The accumulating classification. This should |
| 1429 | /// always be either NoClass or the result of a previous merge |
| 1430 | /// call. In addition, this should never be Memory (the caller |
| 1431 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1432 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1433 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1434 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1435 | /// |
| 1436 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1437 | /// final MEMORY or SSE classes when necessary. |
| 1438 | /// |
| 1439 | /// \param AggregateSize - The size of the current aggregate in |
| 1440 | /// the classification process. |
| 1441 | /// |
| 1442 | /// \param Lo - The classification for the parts of the type |
| 1443 | /// residing in the low word of the containing object. |
| 1444 | /// |
| 1445 | /// \param Hi - The classification for the parts of the type |
| 1446 | /// residing in the higher words of the containing object. |
| 1447 | /// |
| 1448 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1449 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1450 | /// classify - Determine the x86_64 register classes in which the |
| 1451 | /// given type T should be passed. |
| 1452 | /// |
| 1453 | /// \param Lo - The classification for the parts of the type |
| 1454 | /// residing in the low word of the containing object. |
| 1455 | /// |
| 1456 | /// \param Hi - The classification for the parts of the type |
| 1457 | /// residing in the high word of the containing object. |
| 1458 | /// |
| 1459 | /// \param OffsetBase - The bit offset of this type in the |
| 1460 | /// containing object. Some parameters are classified different |
| 1461 | /// depending on whether they straddle an eightbyte boundary. |
| 1462 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1463 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1464 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1465 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1466 | /// If a word is unused its result will be NoClass; if a type should |
| 1467 | /// be passed in Memory then at least the classification of \arg Lo |
| 1468 | /// will be Memory. |
| 1469 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1470 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1471 | /// |
| 1472 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1473 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1474 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1475 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1476 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1477 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1478 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1479 | unsigned IROffset, QualType SourceTy, |
| 1480 | unsigned SourceOffset) const; |
| 1481 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1482 | unsigned IROffset, QualType SourceTy, |
| 1483 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1484 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1485 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1486 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1487 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1488 | |
| 1489 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1490 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1491 | /// |
| 1492 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1493 | /// available. |
| 1494 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1495 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1496 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1497 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1498 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1499 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1500 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1501 | unsigned &neededSSE, |
| 1502 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1503 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1504 | bool IsIllegalVectorType(QualType Ty) const; |
| 1505 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1506 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1507 | /// unfortunately in ways that were not always consistent with |
| 1508 | /// certain previous compilers. In particular, platforms which |
| 1509 | /// required strict binary compatibility with older versions of GCC |
| 1510 | /// may need to exempt themselves. |
| 1511 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1512 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1515 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1516 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1517 | // 64-bit hardware. |
| 1518 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1519 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1520 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1521 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
| 1522 | ABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1523 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1524 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1525 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1526 | bool isPassedUsingAVXType(QualType type) const { |
| 1527 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1528 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1529 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1530 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1531 | if (info.isDirect()) { |
| 1532 | llvm::Type *ty = info.getCoerceToType(); |
| 1533 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1534 | return (vectorTy->getBitWidth() > 128); |
| 1535 | } |
| 1536 | return false; |
| 1537 | } |
| 1538 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1539 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1540 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1541 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1542 | CodeGenFunction &CGF) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1543 | |
| 1544 | bool has64BitPointers() const { |
| 1545 | return Has64BitPointers; |
| 1546 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1547 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1549 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1550 | class WinX86_64ABIInfo : public ABIInfo { |
| 1551 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1552 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1553 | bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1554 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1555 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1556 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1557 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1558 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1559 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1560 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1561 | CodeGenFunction &CGF) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1562 | |
| 1563 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1564 | // FIXME: Assumes vectorcall is in use. |
| 1565 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1566 | } |
| 1567 | |
| 1568 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1569 | uint64_t NumMembers) const override { |
| 1570 | // FIXME: Assumes vectorcall is in use. |
| 1571 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1572 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1573 | }; |
| 1574 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1575 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1576 | X86AVXABILevel AVXLevel; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1577 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1578 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1579 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)), |
| 1580 | AVXLevel(AVXLevel) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1581 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1582 | const X86_64ABIInfo &getABIInfo() const { |
| 1583 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1584 | } |
| 1585 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1586 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1587 | return 7; |
| 1588 | } |
| 1589 | |
| 1590 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1591 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1592 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1593 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1594 | // 0-15 are the 16 integer registers. |
| 1595 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1596 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1597 | return false; |
| 1598 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1599 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1600 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1601 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1602 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1603 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1604 | } |
| 1605 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1606 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1607 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1608 | // The default CC on x86-64 sets %al to the number of SSA |
| 1609 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1610 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1611 | // that when AVX types are involved: the ABI explicitly states it is |
| 1612 | // undefined, and it doesn't work in practice because of how the ABI |
| 1613 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1614 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1615 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1616 | for (CallArgList::const_iterator |
| 1617 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1618 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1619 | HasAVXType = true; |
| 1620 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1623 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1624 | if (!HasAVXType) |
| 1625 | return true; |
| 1626 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1627 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1628 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1631 | llvm::Constant * |
| 1632 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1633 | unsigned Sig; |
| 1634 | if (getABIInfo().has64BitPointers()) |
| 1635 | Sig = (0xeb << 0) | // jmp rel8 |
| 1636 | (0x0a << 8) | // .+0x0c |
| 1637 | ('F' << 16) | |
| 1638 | ('T' << 24); |
| 1639 | else |
| 1640 | Sig = (0xeb << 0) | // jmp rel8 |
| 1641 | (0x06 << 8) | // .+0x08 |
| 1642 | ('F' << 16) | |
| 1643 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1644 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1645 | } |
| 1646 | |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1647 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1648 | return getNativeVectorSizeForAVXABI(AVXLevel) / 8; |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1649 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1650 | }; |
| 1651 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1652 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1653 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1654 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 1655 | : X86_64TargetCodeGenInfo(CGT, AVXLevel) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1656 | |
| 1657 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1658 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1659 | Opt = "\01"; |
| 1660 | Opt += Lib; |
| 1661 | } |
| 1662 | }; |
| 1663 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1664 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1665 | // If the argument does not end in .lib, automatically add the suffix. |
| 1666 | // If the argument contains a space, enclose it in quotes. |
| 1667 | // This matches the behavior of MSVC. |
| 1668 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1669 | std::string ArgStr = Quote ? "\"" : ""; |
| 1670 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1671 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1672 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1673 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1674 | return ArgStr; |
| 1675 | } |
| 1676 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1677 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1678 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1679 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1680 | bool d, bool p, bool w, unsigned RegParms) |
| 1681 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1682 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1683 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1684 | CodeGen::CodeGenModule &CGM) const override; |
| 1685 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1686 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1687 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1688 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1689 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1690 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1691 | |
| 1692 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1693 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1694 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1695 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1696 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1697 | }; |
| 1698 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1699 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1700 | llvm::GlobalValue *GV, |
| 1701 | CodeGen::CodeGenModule &CGM) { |
| 1702 | if (isa<FunctionDecl>(D)) { |
| 1703 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1704 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1705 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1706 | Fn->addFnAttr("stack-probe-size", |
| 1707 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1712 | void WinX86_32TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1713 | llvm::GlobalValue *GV, |
| 1714 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1715 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1716 | |
| 1717 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1718 | } |
| 1719 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1720 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1721 | X86AVXABILevel AVXLevel; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1722 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1723 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1724 | X86AVXABILevel AVXLevel) |
| 1725 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)), AVXLevel(AVXLevel) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1726 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1727 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1728 | CodeGen::CodeGenModule &CGM) const override; |
| 1729 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1730 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1731 | return 7; |
| 1732 | } |
| 1733 | |
| 1734 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1735 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1736 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1738 | // 0-15 are the 16 integer registers. |
| 1739 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1740 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1741 | return false; |
| 1742 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1743 | |
| 1744 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1745 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1746 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1747 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1748 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1749 | |
| 1750 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1751 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1752 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1753 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1754 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1755 | |
| 1756 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1757 | return getNativeVectorSizeForAVXABI(AVXLevel) / 8; |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1758 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1759 | }; |
| 1760 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1761 | void WinX86_64TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1762 | llvm::GlobalValue *GV, |
| 1763 | CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1764 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1765 | |
| 1766 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1767 | } |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 1768 | } // namespace |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1769 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1770 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1771 | Class &Hi) const { |
| 1772 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1773 | // |
| 1774 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1775 | // memory. |
| 1776 | // |
| 1777 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1778 | // memory. |
| 1779 | // |
| 1780 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1781 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1782 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1783 | // ABI working for processors that don't support the __m256 type. |
| 1784 | // |
| 1785 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1786 | // |
| 1787 | // Some of these are enforced by the merging logic. Others can arise |
| 1788 | // only with unions; for example: |
| 1789 | // union { _Complex double; unsigned; } |
| 1790 | // |
| 1791 | // Note that clauses (b) and (c) were added in 0.98. |
| 1792 | // |
| 1793 | if (Hi == Memory) |
| 1794 | Lo = Memory; |
| 1795 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1796 | Lo = Memory; |
| 1797 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1798 | Lo = Memory; |
| 1799 | if (Hi == SSEUp && Lo != SSE) |
| 1800 | Hi = SSE; |
| 1801 | } |
| 1802 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1803 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1804 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1805 | // classified recursively so that always two fields are |
| 1806 | // considered. The resulting class is calculated according to |
| 1807 | // the classes of the fields in the eightbyte: |
| 1808 | // |
| 1809 | // (a) If both classes are equal, this is the resulting class. |
| 1810 | // |
| 1811 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1812 | // the other class. |
| 1813 | // |
| 1814 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1815 | // class. |
| 1816 | // |
| 1817 | // (d) If one of the classes is INTEGER, the result is the |
| 1818 | // INTEGER. |
| 1819 | // |
| 1820 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1821 | // MEMORY is used as class. |
| 1822 | // |
| 1823 | // (f) Otherwise class SSE is used. |
| 1824 | |
| 1825 | // Accum should never be memory (we should have returned) or |
| 1826 | // ComplexX87 (because this cannot be passed in a structure). |
| 1827 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1828 | "Invalid accumulated classification during merge."); |
| 1829 | if (Accum == Field || Field == NoClass) |
| 1830 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1831 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1832 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1833 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1834 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1835 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1836 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1837 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1838 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1839 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1840 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1843 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1844 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1845 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1846 | // Class pairs with appropriate constructor methods for the various |
| 1847 | // situations. |
| 1848 | |
| 1849 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1850 | // shouldn't be passed in registers for example, so there is no chance they |
| 1851 | // can straddle an eightbyte. Verify & simplify. |
| 1852 | |
| 1853 | Lo = Hi = NoClass; |
| 1854 | |
| 1855 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1856 | Current = Memory; |
| 1857 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1858 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1859 | BuiltinType::Kind k = BT->getKind(); |
| 1860 | |
| 1861 | if (k == BuiltinType::Void) { |
| 1862 | Current = NoClass; |
| 1863 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1864 | Lo = Integer; |
| 1865 | Hi = Integer; |
| 1866 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1867 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1868 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1869 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1870 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1871 | Current = SSE; |
| 1872 | } else if (k == BuiltinType::LongDouble) { |
| 1873 | Lo = X87; |
| 1874 | Hi = X87Up; |
| 1875 | } |
| 1876 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1877 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1881 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1882 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1883 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1884 | return; |
| 1885 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1886 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1887 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1888 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1889 | return; |
| 1890 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1891 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1892 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1893 | if (Ty->isMemberFunctionPointerType()) { |
| 1894 | if (Has64BitPointers) { |
| 1895 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 1896 | // Lo and Hi now. |
| 1897 | Lo = Hi = Integer; |
| 1898 | } else { |
| 1899 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 1900 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 1901 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 1902 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 1903 | if (EB_FuncPtr != EB_ThisAdj) { |
| 1904 | Lo = Hi = Integer; |
| 1905 | } else { |
| 1906 | Current = Integer; |
| 1907 | } |
| 1908 | } |
| 1909 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1910 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1911 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1912 | return; |
| 1913 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1914 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1915 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1916 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1917 | if (Size == 32) { |
| 1918 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1919 | // float> as integer. |
| 1920 | Current = Integer; |
| 1921 | |
| 1922 | // If this type crosses an eightbyte boundary, it should be |
| 1923 | // split. |
| 1924 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1925 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1926 | if (EB_Real != EB_Imag) |
| 1927 | Hi = Lo; |
| 1928 | } else if (Size == 64) { |
| 1929 | // gcc passes <1 x double> in memory. :( |
| 1930 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1931 | return; |
| 1932 | |
| 1933 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1934 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1935 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1936 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1937 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1938 | Current = Integer; |
| 1939 | else |
| 1940 | Current = SSE; |
| 1941 | |
| 1942 | // If this type crosses an eightbyte boundary, it should be |
| 1943 | // split. |
| 1944 | if (OffsetBase && OffsetBase != 64) |
| 1945 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 1946 | } else if (Size == 128 || |
| 1947 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1948 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1949 | // least significant one belongs to class SSE and all the others to class |
| 1950 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1951 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1952 | // This design isn't correct for 256-bits, but since there're no cases |
| 1953 | // where the upper parts would need to be inspected, avoid adding |
| 1954 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1955 | // |
| 1956 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1957 | // registers if they are "named", i.e. not part of the "..." of a |
| 1958 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1959 | Lo = SSE; |
| 1960 | Hi = SSEUp; |
| 1961 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1962 | return; |
| 1963 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1964 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1965 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1966 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1967 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1968 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1969 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1970 | if (Size <= 64) |
| 1971 | Current = Integer; |
| 1972 | else if (Size <= 128) |
| 1973 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1974 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1975 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1976 | else if (ET == getContext().DoubleTy || |
| 1977 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1978 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1979 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1980 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1981 | Current = ComplexX87; |
| 1982 | |
| 1983 | // If this complex type crosses an eightbyte boundary then it |
| 1984 | // should be split. |
| 1985 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1986 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1987 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1988 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1989 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1993 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1994 | // Arrays are treated like structures. |
| 1995 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1996 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1997 | |
| 1998 | // 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] | 1999 | // than four eightbytes, ..., it has class MEMORY. |
| 2000 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2001 | return; |
| 2002 | |
| 2003 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2004 | // fields, it has class MEMORY. |
| 2005 | // |
| 2006 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2007 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2008 | return; |
| 2009 | |
| 2010 | // Otherwise implement simplified merge. We could be smarter about |
| 2011 | // this, but it isn't worth it and would be harder to verify. |
| 2012 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2013 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2014 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2015 | |
| 2016 | // The only case a 256-bit wide vector could be used is when the array |
| 2017 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2018 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2019 | if (Size > 128 && EltSize != 256) |
| 2020 | return; |
| 2021 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2022 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2023 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2024 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2025 | Lo = merge(Lo, FieldLo); |
| 2026 | Hi = merge(Hi, FieldHi); |
| 2027 | if (Lo == Memory || Hi == Memory) |
| 2028 | break; |
| 2029 | } |
| 2030 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2031 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2032 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2033 | return; |
| 2034 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2035 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2036 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2037 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2038 | |
| 2039 | // 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] | 2040 | // than four eightbytes, ..., it has class MEMORY. |
| 2041 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2042 | return; |
| 2043 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2044 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2045 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2046 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2047 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2048 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2049 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2050 | const RecordDecl *RD = RT->getDecl(); |
| 2051 | |
| 2052 | // Assume variable sized types are passed in memory. |
| 2053 | if (RD->hasFlexibleArrayMember()) |
| 2054 | return; |
| 2055 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2056 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Reset Lo class, this will be recomputed. |
| 2059 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2060 | |
| 2061 | // If this is a C++ record, classify the bases first. |
| 2062 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2063 | for (const auto &I : CXXRD->bases()) { |
| 2064 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2065 | "Unexpected base class!"); |
| 2066 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2067 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2068 | |
| 2069 | // Classify this field. |
| 2070 | // |
| 2071 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2072 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2073 | // initialized to class NO_CLASS. |
| 2074 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2075 | uint64_t Offset = |
| 2076 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2077 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2078 | Lo = merge(Lo, FieldLo); |
| 2079 | Hi = merge(Hi, FieldHi); |
| 2080 | if (Lo == Memory || Hi == Memory) |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2086 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2087 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2088 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2089 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2090 | bool BitField = i->isBitField(); |
| 2091 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2092 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2093 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2094 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2095 | // The only case a 256-bit wide vector could be used is when the struct |
| 2096 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2097 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2098 | // |
| 2099 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2100 | Lo = Memory; |
| 2101 | return; |
| 2102 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2103 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2104 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2105 | Lo = Memory; |
| 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | // Classify this field. |
| 2110 | // |
| 2111 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2112 | // exceeds a single eightbyte, each is classified |
| 2113 | // separately. Each eightbyte gets initialized to class |
| 2114 | // NO_CLASS. |
| 2115 | Class FieldLo, FieldHi; |
| 2116 | |
| 2117 | // Bit-fields require special handling, they do not force the |
| 2118 | // structure to be passed in memory even if unaligned, and |
| 2119 | // therefore they can straddle an eightbyte. |
| 2120 | if (BitField) { |
| 2121 | // Ignore padding bit-fields. |
| 2122 | if (i->isUnnamedBitfield()) |
| 2123 | continue; |
| 2124 | |
| 2125 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2126 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2127 | |
| 2128 | uint64_t EB_Lo = Offset / 64; |
| 2129 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2130 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2131 | if (EB_Lo) { |
| 2132 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2133 | FieldLo = NoClass; |
| 2134 | FieldHi = Integer; |
| 2135 | } else { |
| 2136 | FieldLo = Integer; |
| 2137 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2138 | } |
| 2139 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2140 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2141 | Lo = merge(Lo, FieldLo); |
| 2142 | Hi = merge(Hi, FieldHi); |
| 2143 | if (Lo == Memory || Hi == Memory) |
| 2144 | break; |
| 2145 | } |
| 2146 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2147 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2148 | } |
| 2149 | } |
| 2150 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2151 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2152 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2153 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2154 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2155 | // Treat an enum type as its underlying type. |
| 2156 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2157 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2158 | |
| 2159 | return (Ty->isPromotableIntegerType() ? |
| 2160 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2161 | } |
| 2162 | |
| 2163 | return ABIArgInfo::getIndirect(0); |
| 2164 | } |
| 2165 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2166 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2167 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2168 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 2169 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2170 | if (Size <= 64 || Size > LargestVector) |
| 2171 | return true; |
| 2172 | } |
| 2173 | |
| 2174 | return false; |
| 2175 | } |
| 2176 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2177 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2178 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2179 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2180 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2181 | // |
| 2182 | // This assumption is optimistic, as there could be free registers available |
| 2183 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2184 | // the argument in the free register. This does not seem to happen currently, |
| 2185 | // but this code would be much safer if we could mark the argument with |
| 2186 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2187 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2188 | // Treat an enum type as its underlying type. |
| 2189 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2190 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2191 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2192 | return (Ty->isPromotableIntegerType() ? |
| 2193 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2194 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2195 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2196 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2197 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2198 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2199 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2200 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2201 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2202 | |
| 2203 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2204 | // is important for good codegen. |
| 2205 | // |
| 2206 | // We do this by coercing the value into a scalar type which the backend can |
| 2207 | // handle naturally (i.e., without using byval). |
| 2208 | // |
| 2209 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2210 | // free integer registers. Doing this when there are free integer registers |
| 2211 | // would require more care, as we would have to ensure that the coerced value |
| 2212 | // did not claim the unused register. That would require either reording the |
| 2213 | // arguments to the function (so that any subsequent inreg values came first), |
| 2214 | // or only doing this optimization when there were no following arguments that |
| 2215 | // might be inreg. |
| 2216 | // |
| 2217 | // We currently expect it to be rare (particularly in well written code) for |
| 2218 | // arguments to be passed on the stack when there are still free integer |
| 2219 | // registers available (this would typically imply large structs being passed |
| 2220 | // by value), so this seems like a fair tradeoff for now. |
| 2221 | // |
| 2222 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2223 | // attributes. See PR12193. |
| 2224 | if (freeIntRegs == 0) { |
| 2225 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2226 | |
| 2227 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2228 | // type, which will end up on the stack (with alignment 8). |
| 2229 | if (Align == 8 && Size <= 64) |
| 2230 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2231 | Size)); |
| 2232 | } |
| 2233 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2234 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2237 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2238 | /// 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] | 2239 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2240 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2241 | // vectors; strip them off if present. |
| 2242 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2243 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2244 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2245 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 2246 | if(isa<llvm::VectorType>(IRType)) |
| 2247 | return IRType; |
| 2248 | |
| 2249 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 2250 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2251 | assert((Size == 128 || Size == 256) && "Invalid type found!"); |
| 2252 | |
| 2253 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 2254 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 2255 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2258 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2259 | /// is known to either be off the end of the specified type or being in |
| 2260 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2261 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2262 | /// classification that put one of the two halves in the INTEGER class. |
| 2263 | /// |
| 2264 | /// It is conservatively correct to return false. |
| 2265 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2266 | unsigned EndBit, ASTContext &Context) { |
| 2267 | // If the bytes being queried are off the end of the type, there is no user |
| 2268 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2269 | // types that don't contain interesting padding. |
| 2270 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2271 | if (TySize <= StartBit) |
| 2272 | return true; |
| 2273 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2274 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2275 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2276 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2277 | |
| 2278 | // Check each element to see if the element overlaps with the queried range. |
| 2279 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2280 | // If the element is after the span we care about, then we're done.. |
| 2281 | unsigned EltOffset = i*EltSize; |
| 2282 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2283 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2284 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2285 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2286 | EndBit-EltOffset, Context)) |
| 2287 | return false; |
| 2288 | } |
| 2289 | // If it overlaps no elements, then it is safe to process as padding. |
| 2290 | return true; |
| 2291 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2292 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2293 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2294 | const RecordDecl *RD = RT->getDecl(); |
| 2295 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2296 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2297 | // If this is a C++ record, check the bases first. |
| 2298 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2299 | for (const auto &I : CXXRD->bases()) { |
| 2300 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2301 | "Unexpected base class!"); |
| 2302 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2303 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2304 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2305 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2306 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2307 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2308 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2309 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2310 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2311 | EndBit-BaseOffset, Context)) |
| 2312 | return false; |
| 2313 | } |
| 2314 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2316 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2317 | // this could be sped up a lot by being smarter about queried fields, |
| 2318 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2319 | // much. |
| 2320 | unsigned idx = 0; |
| 2321 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2322 | i != e; ++i, ++idx) { |
| 2323 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2325 | // If we found a field after the region we care about, then we're done. |
| 2326 | if (FieldOffset >= EndBit) break; |
| 2327 | |
| 2328 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2329 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2330 | Context)) |
| 2331 | return false; |
| 2332 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2333 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2334 | // If nothing in this record overlapped the area of interest, then we're |
| 2335 | // clean. |
| 2336 | return true; |
| 2337 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2338 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2339 | return false; |
| 2340 | } |
| 2341 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2342 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2343 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2344 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2345 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2346 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2347 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2348 | // Base case if we find a float. |
| 2349 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2350 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2351 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2352 | // 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] | 2353 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2354 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2355 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2356 | IROffset -= SL->getElementOffset(Elt); |
| 2357 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2358 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2359 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2360 | // 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] | 2361 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2362 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2363 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2364 | IROffset -= IROffset/EltSize*EltSize; |
| 2365 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2366 | } |
| 2367 | |
| 2368 | return false; |
| 2369 | } |
| 2370 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2371 | |
| 2372 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2373 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2374 | llvm::Type *X86_64ABIInfo:: |
| 2375 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2376 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2377 | // 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] | 2378 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2379 | // structs that contain 3 floats. |
| 2380 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2381 | SourceOffset*8+64, getContext())) |
| 2382 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2383 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2384 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2385 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2386 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2387 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2388 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2389 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2390 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2391 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2392 | } |
| 2393 | |
| 2394 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2395 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2396 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2397 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2398 | /// 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] | 2399 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2400 | /// etc). |
| 2401 | /// |
| 2402 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2403 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2404 | /// the 8-byte value references. PrefType may be null. |
| 2405 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2406 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2407 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2408 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2409 | llvm::Type *X86_64ABIInfo:: |
| 2410 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2411 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2412 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2413 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2414 | if (IROffset == 0) { |
| 2415 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2416 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2417 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2418 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2419 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2420 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2421 | // goodness in the source type is just tail padding. This is allowed to |
| 2422 | // kick in for struct {double,int} on the int, but not on |
| 2423 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2424 | // have to do this analysis on the source type because we can't depend on |
| 2425 | // unions being lowered a specific way etc. |
| 2426 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2427 | IRType->isIntegerTy(32) || |
| 2428 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2429 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2430 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2431 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2432 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2433 | SourceOffset*8+64, getContext())) |
| 2434 | return IRType; |
| 2435 | } |
| 2436 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2438 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2439 | // 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] | 2440 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2441 | if (IROffset < SL->getSizeInBytes()) { |
| 2442 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2443 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2444 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2445 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2446 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2447 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2448 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2449 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2450 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2451 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2452 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2453 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2454 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2455 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2456 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2457 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2458 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2459 | // 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] | 2460 | unsigned TySizeInBytes = |
| 2461 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2462 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2463 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2464 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2465 | // It is always safe to classify this as an integer type up to i64 that |
| 2466 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2467 | return llvm::IntegerType::get(getVMContext(), |
| 2468 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2471 | |
| 2472 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2473 | /// be used as elements of a two register pair to pass or return, return a |
| 2474 | /// first class aggregate to represent them. For example, if the low part of |
| 2475 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2476 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2477 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2478 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2479 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2480 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2481 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2482 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2483 | // the second element at offset 8. Check for this: |
| 2484 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2485 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2486 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2487 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2488 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2489 | // To handle this, we have to increase the size of the low part so that the |
| 2490 | // second element will start at an 8 byte offset. We can't increase the size |
| 2491 | // of the second element because it might make us access off the end of the |
| 2492 | // struct. |
| 2493 | if (HiStart != 8) { |
| 2494 | // There are only two sorts of types the ABI generation code can produce for |
| 2495 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2496 | // Promote these to a larger type. |
| 2497 | if (Lo->isFloatTy()) |
| 2498 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2499 | else { |
| 2500 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2501 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2502 | } |
| 2503 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2504 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2505 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2506 | |
| 2507 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2508 | // Verify that the second element is at an 8-byte offset. |
| 2509 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2510 | "Invalid x86-64 argument pair!"); |
| 2511 | return Result; |
| 2512 | } |
| 2513 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2514 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2515 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2516 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2517 | // classification algorithm. |
| 2518 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2519 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2520 | |
| 2521 | // Check some invariants. |
| 2522 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2523 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2524 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2525 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2526 | switch (Lo) { |
| 2527 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2528 | if (Hi == NoClass) |
| 2529 | return ABIArgInfo::getIgnore(); |
| 2530 | // If the low part is just padding, it takes no register, leave ResType |
| 2531 | // null. |
| 2532 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2533 | "Unknown missing lo part"); |
| 2534 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2535 | |
| 2536 | case SSEUp: |
| 2537 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2538 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2539 | |
| 2540 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2541 | // hidden argument. |
| 2542 | case Memory: |
| 2543 | return getIndirectReturnResult(RetTy); |
| 2544 | |
| 2545 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2546 | // available register of the sequence %rax, %rdx is used. |
| 2547 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2548 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2549 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2550 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2551 | // so that the parameter gets the right LLVM IR attributes. |
| 2552 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2553 | // Treat an enum type as its underlying type. |
| 2554 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2555 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2556 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2557 | if (RetTy->isIntegralOrEnumerationType() && |
| 2558 | RetTy->isPromotableIntegerType()) |
| 2559 | return ABIArgInfo::getExtend(); |
| 2560 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2561 | break; |
| 2562 | |
| 2563 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2564 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2565 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2566 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2567 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2568 | |
| 2569 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2570 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2571 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2572 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2573 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2574 | |
| 2575 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2576 | // part of the value is returned in %st0 and the imaginary part in |
| 2577 | // %st1. |
| 2578 | case ComplexX87: |
| 2579 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2580 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2581 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2582 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2583 | break; |
| 2584 | } |
| 2585 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2586 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2587 | switch (Hi) { |
| 2588 | // Memory was handled previously and X87 should |
| 2589 | // never occur as a hi class. |
| 2590 | case Memory: |
| 2591 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2592 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2593 | |
| 2594 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2595 | case NoClass: |
| 2596 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2597 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2598 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2599 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2600 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2601 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2602 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2603 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2604 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2605 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2606 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2607 | break; |
| 2608 | |
| 2609 | // 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] | 2610 | // is passed in the next available eightbyte chunk if the last used |
| 2611 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2612 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2613 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2614 | case SSEUp: |
| 2615 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2616 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2617 | break; |
| 2618 | |
| 2619 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2620 | // returned together with the previous X87 value in %st0. |
| 2621 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2622 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2623 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2624 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2625 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2626 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2627 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2628 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2629 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2630 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2631 | break; |
| 2632 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2633 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2634 | // 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] | 2635 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2636 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2637 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2638 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2640 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2643 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2644 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2645 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2646 | const |
| 2647 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2648 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2649 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2650 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2651 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2652 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2653 | // Check some invariants. |
| 2654 | // FIXME: Enforce these by construction. |
| 2655 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2656 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2657 | |
| 2658 | neededInt = 0; |
| 2659 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2660 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2661 | switch (Lo) { |
| 2662 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2663 | if (Hi == NoClass) |
| 2664 | return ABIArgInfo::getIgnore(); |
| 2665 | // If the low part is just padding, it takes no register, leave ResType |
| 2666 | // null. |
| 2667 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2668 | "Unknown missing lo part"); |
| 2669 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2670 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2671 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2672 | // on the stack. |
| 2673 | case Memory: |
| 2674 | |
| 2675 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2676 | // COMPLEX_X87, it is passed in memory. |
| 2677 | case X87: |
| 2678 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2679 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2680 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2681 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2682 | |
| 2683 | case SSEUp: |
| 2684 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2685 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2686 | |
| 2687 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2688 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2689 | // and %r9 is used. |
| 2690 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2691 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2693 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2694 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2695 | |
| 2696 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2697 | // so that the parameter gets the right LLVM IR attributes. |
| 2698 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2699 | // Treat an enum type as its underlying type. |
| 2700 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2701 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2702 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2703 | if (Ty->isIntegralOrEnumerationType() && |
| 2704 | Ty->isPromotableIntegerType()) |
| 2705 | return ABIArgInfo::getExtend(); |
| 2706 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2707 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2708 | break; |
| 2709 | |
| 2710 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2711 | // available SSE register is used, the registers are taken in the |
| 2712 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2713 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2714 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2715 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2716 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2717 | break; |
| 2718 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2719 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2720 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2721 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2722 | switch (Hi) { |
| 2723 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2724 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2725 | // which is passed in memory. |
| 2726 | case Memory: |
| 2727 | case X87: |
| 2728 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2729 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2730 | |
| 2731 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2732 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2733 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2734 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2735 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2736 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2737 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2738 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2739 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2740 | break; |
| 2741 | |
| 2742 | // X87Up generally doesn't occur here (long double is passed in |
| 2743 | // memory), except in situations involving unions. |
| 2744 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2745 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2746 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2747 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2748 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2749 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2750 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2751 | ++neededSSE; |
| 2752 | break; |
| 2753 | |
| 2754 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2755 | // 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] | 2756 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2757 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2758 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2759 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2760 | break; |
| 2761 | } |
| 2762 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2763 | // If a high part was specified, merge it together with the low part. It is |
| 2764 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2765 | // first class struct aggregate with the high and low part: {low, high} |
| 2766 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2767 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2768 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2769 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2772 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2773 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2774 | if (!getCXXABI().classifyReturnType(FI)) |
| 2775 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2776 | |
| 2777 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2778 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2779 | |
| 2780 | // If the return value is indirect, then the hidden argument is consuming one |
| 2781 | // integer register. |
| 2782 | if (FI.getReturnInfo().isIndirect()) |
| 2783 | --freeIntRegs; |
| 2784 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 2785 | // The chain argument effectively gives us another free register. |
| 2786 | if (FI.isChainCall()) |
| 2787 | ++freeIntRegs; |
| 2788 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2789 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2790 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2791 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2792 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2793 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2794 | it != ie; ++it, ++ArgNo) { |
| 2795 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2796 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2797 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2798 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2799 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2800 | |
| 2801 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2802 | // eightbyte of an argument, the whole argument is passed on the |
| 2803 | // stack. If registers have already been assigned for some |
| 2804 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2805 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2806 | freeIntRegs -= neededInt; |
| 2807 | freeSSERegs -= neededSSE; |
| 2808 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2809 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2815 | QualType Ty, |
| 2816 | CodeGenFunction &CGF) { |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 2817 | llvm::Value *overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 2818 | nullptr, VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2819 | llvm::Value *overflow_arg_area = |
| 2820 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2821 | |
| 2822 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2823 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2824 | // It isn't stated explicitly in the standard, but in practice we use |
| 2825 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2826 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2827 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2828 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2829 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2830 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2831 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2832 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2833 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2834 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2835 | overflow_arg_area = |
| 2836 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2837 | overflow_arg_area->getType(), |
| 2838 | "overflow_arg_area.align"); |
| 2839 | } |
| 2840 | |
| 2841 | // 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] | 2842 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2843 | llvm::Value *Res = |
| 2844 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2845 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2846 | |
| 2847 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2848 | // l->overflow_arg_area + sizeof(type). |
| 2849 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2850 | // an 8 byte boundary. |
| 2851 | |
| 2852 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2853 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2854 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2855 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2856 | "overflow_arg_area.next"); |
| 2857 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2858 | |
| 2859 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2860 | return Res; |
| 2861 | } |
| 2862 | |
| 2863 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2864 | CodeGenFunction &CGF) const { |
| 2865 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2866 | // struct { |
| 2867 | // i32 gp_offset; |
| 2868 | // i32 fp_offset; |
| 2869 | // i8* overflow_arg_area; |
| 2870 | // i8* reg_save_area; |
| 2871 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2872 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2873 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2874 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2875 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2876 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2877 | |
| 2878 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2879 | // in the registers. If not go to step 7. |
| 2880 | if (!neededInt && !neededSSE) |
| 2881 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2882 | |
| 2883 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2884 | // general purpose registers needed to pass type and num_fp to hold |
| 2885 | // the number of floating point registers needed. |
| 2886 | |
| 2887 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2888 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2889 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2890 | // |
| 2891 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2892 | // register save space). |
| 2893 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2894 | llvm::Value *InRegs = nullptr; |
| 2895 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2896 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2897 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2898 | gp_offset_p = |
| 2899 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2900 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2901 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2902 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2906 | fp_offset_p = |
| 2907 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2908 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2909 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2910 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2911 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2912 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2913 | } |
| 2914 | |
| 2915 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2916 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2917 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2918 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2919 | |
| 2920 | // Emit code to load the value if it was passed in registers. |
| 2921 | |
| 2922 | CGF.EmitBlock(InRegBlock); |
| 2923 | |
| 2924 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2925 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2926 | // copying to a temporary location in case the parameter is passed |
| 2927 | // in different register classes or requires an alignment greater |
| 2928 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2929 | // |
| 2930 | // FIXME: This really results in shameful code when we end up needing to |
| 2931 | // collect arguments from different places; often what should result in a |
| 2932 | // simple assembling of a structure from scattered addresses has many more |
| 2933 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2934 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2935 | llvm::Value *RegAddr = CGF.Builder.CreateLoad( |
| 2936 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3), "reg_save_area"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2937 | if (neededInt && neededSSE) { |
| 2938 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2939 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2940 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2941 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2942 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2943 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2944 | llvm::Type *TyLo = ST->getElementType(0); |
| 2945 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2946 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2947 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2948 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2949 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2950 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2951 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2952 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2953 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2954 | llvm::Value *V = |
| 2955 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2956 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2957 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2958 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2959 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2960 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2961 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2962 | } else if (neededInt) { |
| 2963 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2964 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2965 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2966 | |
| 2967 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2968 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2969 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2970 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2971 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2972 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2973 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2974 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2975 | RegAddr = Tmp; |
| 2976 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2977 | } else if (neededSSE == 1) { |
| 2978 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2979 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2980 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2981 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2982 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2983 | // SSE registers are spaced 16 bytes apart in the register save |
| 2984 | // area, we need to collect the two eightbytes together. |
| 2985 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2986 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2987 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2988 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2989 | llvm::PointerType::getUnqual(DoubleTy); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2990 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2991 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2992 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2993 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2994 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2995 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2996 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2997 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2998 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2999 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 3000 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3004 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3005 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3006 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3007 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3008 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3009 | gp_offset_p); |
| 3010 | } |
| 3011 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3012 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3013 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3014 | fp_offset_p); |
| 3015 | } |
| 3016 | CGF.EmitBranch(ContBlock); |
| 3017 | |
| 3018 | // Emit code to load the value if it was passed in memory. |
| 3019 | |
| 3020 | CGF.EmitBlock(InMemBlock); |
| 3021 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 3022 | |
| 3023 | // Return the appropriate result. |
| 3024 | |
| 3025 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 3026 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3027 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3028 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 3029 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3030 | return ResAddr; |
| 3031 | } |
| 3032 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3033 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3034 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3035 | |
| 3036 | if (Ty->isVoidType()) |
| 3037 | return ABIArgInfo::getIgnore(); |
| 3038 | |
| 3039 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3040 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3041 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3042 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3043 | uint64_t Width = Info.Width; |
| 3044 | unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3045 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3046 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3047 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3048 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3049 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3050 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 3051 | } |
| 3052 | |
| 3053 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3054 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3055 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3056 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3057 | if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3058 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3059 | Width)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3060 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3061 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3062 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3063 | // other targets. |
| 3064 | const Type *Base = nullptr; |
| 3065 | uint64_t NumElts = 0; |
| 3066 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3067 | if (FreeSSERegs >= NumElts) { |
| 3068 | FreeSSERegs -= NumElts; |
| 3069 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3070 | return ABIArgInfo::getDirect(); |
| 3071 | return ABIArgInfo::getExpand(); |
| 3072 | } |
| 3073 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3074 | } |
| 3075 | |
| 3076 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3077 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3078 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3079 | // directly. |
| 3080 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3081 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3082 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3085 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3086 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3087 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3088 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3089 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3090 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3091 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3092 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3095 | // Bool type is always extended to the ABI, other builtin types are not |
| 3096 | // extended. |
| 3097 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3098 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3099 | return ABIArgInfo::getExtend(); |
| 3100 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3101 | return ABIArgInfo::getDirect(); |
| 3102 | } |
| 3103 | |
| 3104 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3105 | bool IsVectorCall = |
| 3106 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3107 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3108 | // We can use up to 4 SSE return registers with vectorcall. |
| 3109 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3110 | if (!getCXXABI().classifyReturnType(FI)) |
| 3111 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3112 | |
| 3113 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3114 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3115 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3116 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3119 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3120 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3121 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3123 | CGBuilderTy &Builder = CGF.Builder; |
| 3124 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 3125 | "ap"); |
| 3126 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3127 | llvm::Type *PTy = |
| 3128 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3129 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3130 | |
| 3131 | uint64_t Offset = |
| 3132 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 3133 | llvm::Value *NextAddr = |
| 3134 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3135 | "ap.next"); |
| 3136 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3137 | |
| 3138 | return AddrTyped; |
| 3139 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3140 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3141 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3142 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3143 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3144 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3145 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3146 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3147 | |
| 3148 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3149 | CodeGenFunction &CGF) const override; |
| 3150 | }; |
| 3151 | |
| 3152 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3153 | public: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3154 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) |
| 3155 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3156 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3157 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3158 | // This is recovered from gcc output. |
| 3159 | return 1; // r1 is the dedicated stack pointer |
| 3160 | } |
| 3161 | |
| 3162 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3163 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3164 | |
| 3165 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3166 | return 16; // Natural alignment for Altivec vectors. |
| 3167 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3168 | }; |
| 3169 | |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 3170 | } // namespace |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3171 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3172 | llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3173 | QualType Ty, |
| 3174 | CodeGenFunction &CGF) const { |
| 3175 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3176 | // TODO: Implement this. For now ignore. |
| 3177 | (void)CTy; |
| 3178 | return nullptr; |
| 3179 | } |
| 3180 | |
| 3181 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3182 | bool isInt = |
| 3183 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3184 | llvm::Type *CharPtr = CGF.Int8PtrTy; |
| 3185 | llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy; |
| 3186 | |
| 3187 | CGBuilderTy &Builder = CGF.Builder; |
| 3188 | llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr"); |
| 3189 | llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3190 | llvm::Value *FPRPtrAsInt = |
| 3191 | Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3192 | llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3193 | llvm::Value *OverflowAreaPtrAsInt = |
| 3194 | Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3)); |
| 3195 | llvm::Value *OverflowAreaPtr = |
| 3196 | Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr); |
| 3197 | llvm::Value *RegsaveAreaPtrAsInt = |
| 3198 | Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4)); |
| 3199 | llvm::Value *RegsaveAreaPtr = |
| 3200 | Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3201 | llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr"); |
| 3202 | // Align GPR when TY is i64. |
| 3203 | if (isI64) { |
| 3204 | llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1)); |
| 3205 | llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1)); |
| 3206 | llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1)); |
| 3207 | GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR); |
| 3208 | } |
| 3209 | llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr"); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3210 | llvm::Value *OverflowArea = |
| 3211 | Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area"); |
| 3212 | llvm::Value *OverflowAreaAsInt = |
| 3213 | Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty); |
| 3214 | llvm::Value *RegsaveArea = |
| 3215 | Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area"); |
| 3216 | llvm::Value *RegsaveAreaAsInt = |
| 3217 | Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3218 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3219 | llvm::Value *CC = |
| 3220 | Builder.CreateICmpULT(isInt ? GPR : FPR, Builder.getInt8(8), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3221 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3222 | llvm::Value *RegConstant = |
| 3223 | Builder.CreateMul(isInt ? GPR : FPR, Builder.getInt8(isInt ? 4 : 8)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3224 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3225 | llvm::Value *OurReg = Builder.CreateAdd( |
| 3226 | RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3227 | |
| 3228 | if (Ty->isFloatingType()) |
| 3229 | OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32)); |
| 3230 | |
| 3231 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3232 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3233 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3234 | |
| 3235 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3236 | |
| 3237 | CGF.EmitBlock(UsingRegs); |
| 3238 | |
| 3239 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3240 | llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy); |
| 3241 | // Increase the GPR/FPR indexes. |
| 3242 | if (isInt) { |
| 3243 | GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1)); |
| 3244 | Builder.CreateStore(GPR, GPRPtr); |
| 3245 | } else { |
| 3246 | FPR = Builder.CreateAdd(FPR, Builder.getInt8(1)); |
| 3247 | Builder.CreateStore(FPR, FPRPtr); |
| 3248 | } |
| 3249 | CGF.EmitBranch(Cont); |
| 3250 | |
| 3251 | CGF.EmitBlock(UsingOverflow); |
| 3252 | |
| 3253 | // Increase the overflow area. |
| 3254 | llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3255 | OverflowAreaAsInt = |
| 3256 | Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8)); |
| 3257 | Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), |
| 3258 | OverflowAreaPtr); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3259 | CGF.EmitBranch(Cont); |
| 3260 | |
| 3261 | CGF.EmitBlock(Cont); |
| 3262 | |
| 3263 | llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr"); |
| 3264 | Result->addIncoming(Result1, UsingRegs); |
| 3265 | Result->addIncoming(Result2, UsingOverflow); |
| 3266 | |
| 3267 | if (Ty->isAggregateType()) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3268 | llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3269 | return Builder.CreateLoad(AGGPtr, false, "aggr"); |
| 3270 | } |
| 3271 | |
| 3272 | return Result; |
| 3273 | } |
| 3274 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3275 | bool |
| 3276 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3277 | llvm::Value *Address) const { |
| 3278 | // This is calculated from the LLVM and GCC tables and verified |
| 3279 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3280 | |
| 3281 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3282 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3283 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3284 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3285 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3286 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3287 | |
| 3288 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3289 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3290 | |
| 3291 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3292 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3293 | |
| 3294 | // 64-76 are various 4-byte special-purpose registers: |
| 3295 | // 64: mq |
| 3296 | // 65: lr |
| 3297 | // 66: ctr |
| 3298 | // 67: ap |
| 3299 | // 68-75 cr0-7 |
| 3300 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3301 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3302 | |
| 3303 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3304 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3305 | |
| 3306 | // 109: vrsave |
| 3307 | // 110: vscr |
| 3308 | // 111: spe_acc |
| 3309 | // 112: spefscr |
| 3310 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3311 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3312 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3313 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3316 | // PowerPC-64 |
| 3317 | |
| 3318 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3319 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3320 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3321 | public: |
| 3322 | enum ABIKind { |
| 3323 | ELFv1 = 0, |
| 3324 | ELFv2 |
| 3325 | }; |
| 3326 | |
| 3327 | private: |
| 3328 | static const unsigned GPRBits = 64; |
| 3329 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3330 | bool HasQPX; |
| 3331 | |
| 3332 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3333 | // will be passed in a QPX register. |
| 3334 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3335 | if (!HasQPX) |
| 3336 | return false; |
| 3337 | |
| 3338 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3339 | unsigned NumElements = VT->getNumElements(); |
| 3340 | if (NumElements == 1) |
| 3341 | return false; |
| 3342 | |
| 3343 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3344 | if (getContext().getTypeSize(Ty) <= 256) |
| 3345 | return true; |
| 3346 | } else if (VT->getElementType()-> |
| 3347 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3348 | if (getContext().getTypeSize(Ty) <= 128) |
| 3349 | return true; |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | return false; |
| 3354 | } |
| 3355 | |
| 3356 | bool IsQPXVectorTy(QualType Ty) const { |
| 3357 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3358 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3359 | |
| 3360 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3361 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3362 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3363 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3364 | bool isPromotableTypeForABI(QualType Ty) const; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3365 | bool isAlignedParamType(QualType Ty, bool &Align32) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3366 | |
| 3367 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3368 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3369 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3370 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3371 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3372 | uint64_t Members) const override; |
| 3373 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3374 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3375 | // Example: For aggregate arguments that fit in a register, we could |
| 3376 | // use getDirectInReg (as is done below for structs containing a single |
| 3377 | // floating-point value) to avoid pushing them to memory on function |
| 3378 | // entry. This would require changing the logic in PPCISelLowering |
| 3379 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3380 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3381 | if (!getCXXABI().classifyReturnType(FI)) |
| 3382 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3383 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3384 | // We rely on the default argument classification for the most part. |
| 3385 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3386 | // 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] | 3387 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3388 | if (T) { |
| 3389 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3390 | if (IsQPXVectorTy(T) || |
| 3391 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3392 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3393 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3394 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3395 | continue; |
| 3396 | } |
| 3397 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3398 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3399 | } |
| 3400 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3401 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3402 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3403 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3404 | }; |
| 3405 | |
| 3406 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3407 | bool HasQPX; |
| 3408 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3409 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3410 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3411 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
| 3412 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)), |
| 3413 | HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3414 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3415 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3416 | // This is recovered from gcc output. |
| 3417 | return 1; // r1 is the dedicated stack pointer |
| 3418 | } |
| 3419 | |
| 3420 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3421 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3422 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3423 | unsigned getOpenMPSimdDefaultAlignment(QualType QT) const override { |
| 3424 | if (HasQPX) |
| 3425 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 3426 | if (PT->getPointeeType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 3427 | return 32; // Natural alignment for QPX doubles. |
| 3428 | |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3429 | return 16; // Natural alignment for Altivec and VSX vectors. |
| 3430 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3431 | }; |
| 3432 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3433 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3434 | public: |
| 3435 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3436 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3437 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3438 | // This is recovered from gcc output. |
| 3439 | return 1; // r1 is the dedicated stack pointer |
| 3440 | } |
| 3441 | |
| 3442 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3443 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3444 | |
| 3445 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3446 | return 16; // Natural alignment for Altivec vectors. |
| 3447 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3448 | }; |
| 3449 | |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 3450 | } // namespace |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3451 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3452 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3453 | // extended to 64 bits. |
| 3454 | bool |
| 3455 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3456 | // Treat an enum type as its underlying type. |
| 3457 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3458 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3459 | |
| 3460 | // Promotable integer types are required to be promoted by the ABI. |
| 3461 | if (Ty->isPromotableIntegerType()) |
| 3462 | return true; |
| 3463 | |
| 3464 | // In addition to the usual promotable integer types, we also need to |
| 3465 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3466 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3467 | switch (BT->getKind()) { |
| 3468 | case BuiltinType::Int: |
| 3469 | case BuiltinType::UInt: |
| 3470 | return true; |
| 3471 | default: |
| 3472 | break; |
| 3473 | } |
| 3474 | |
| 3475 | return false; |
| 3476 | } |
| 3477 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3478 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3479 | /// alignment in the parameter area. |
| 3480 | bool |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3481 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty, bool &Align32) const { |
| 3482 | Align32 = false; |
| 3483 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3484 | // Complex types are passed just like their elements. |
| 3485 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3486 | Ty = CTy->getElementType(); |
| 3487 | |
| 3488 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3489 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3490 | if (IsQPXVectorTy(Ty)) { |
| 3491 | if (getContext().getTypeSize(Ty) > 128) |
| 3492 | Align32 = true; |
| 3493 | |
| 3494 | return true; |
| 3495 | } else if (Ty->isVectorType()) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3496 | return getContext().getTypeSize(Ty) == 128; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3497 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3498 | |
| 3499 | // For single-element float/vector structs, we consider the whole type |
| 3500 | // to have the same alignment requirements as its single element. |
| 3501 | const Type *AlignAsType = nullptr; |
| 3502 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3503 | if (EltType) { |
| 3504 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3505 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3506 | getContext().getTypeSize(EltType) == 128) || |
| 3507 | (BT && BT->isFloatingPoint())) |
| 3508 | AlignAsType = EltType; |
| 3509 | } |
| 3510 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3511 | // Likewise for ELFv2 homogeneous aggregates. |
| 3512 | const Type *Base = nullptr; |
| 3513 | uint64_t Members = 0; |
| 3514 | if (!AlignAsType && Kind == ELFv2 && |
| 3515 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3516 | AlignAsType = Base; |
| 3517 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3518 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3519 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3520 | if (getContext().getTypeSize(AlignAsType) > 128) |
| 3521 | Align32 = true; |
| 3522 | |
| 3523 | return true; |
| 3524 | } else if (AlignAsType) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3525 | return AlignAsType->isVectorType(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3526 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3527 | |
| 3528 | // Otherwise, we only need alignment for any aggregate type that |
| 3529 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3530 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3531 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
| 3532 | Align32 = true; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3533 | return true; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3534 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3535 | |
| 3536 | return false; |
| 3537 | } |
| 3538 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3539 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3540 | /// aggregate. Base is set to the base element type, and Members is set |
| 3541 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3542 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3543 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3544 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3545 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3546 | if (NElements == 0) |
| 3547 | return false; |
| 3548 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3549 | return false; |
| 3550 | Members *= NElements; |
| 3551 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3552 | const RecordDecl *RD = RT->getDecl(); |
| 3553 | if (RD->hasFlexibleArrayMember()) |
| 3554 | return false; |
| 3555 | |
| 3556 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3557 | |
| 3558 | // If this is a C++ record, check the bases first. |
| 3559 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3560 | for (const auto &I : CXXRD->bases()) { |
| 3561 | // Ignore empty records. |
| 3562 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3563 | continue; |
| 3564 | |
| 3565 | uint64_t FldMembers; |
| 3566 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3567 | return false; |
| 3568 | |
| 3569 | Members += FldMembers; |
| 3570 | } |
| 3571 | } |
| 3572 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3573 | for (const auto *FD : RD->fields()) { |
| 3574 | // Ignore (non-zero arrays of) empty records. |
| 3575 | QualType FT = FD->getType(); |
| 3576 | while (const ConstantArrayType *AT = |
| 3577 | getContext().getAsConstantArrayType(FT)) { |
| 3578 | if (AT->getSize().getZExtValue() == 0) |
| 3579 | return false; |
| 3580 | FT = AT->getElementType(); |
| 3581 | } |
| 3582 | if (isEmptyRecord(getContext(), FT, true)) |
| 3583 | continue; |
| 3584 | |
| 3585 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3586 | if (getContext().getLangOpts().CPlusPlus && |
| 3587 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3588 | continue; |
| 3589 | |
| 3590 | uint64_t FldMembers; |
| 3591 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3592 | return false; |
| 3593 | |
| 3594 | Members = (RD->isUnion() ? |
| 3595 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3596 | } |
| 3597 | |
| 3598 | if (!Base) |
| 3599 | return false; |
| 3600 | |
| 3601 | // Ensure there is no padding. |
| 3602 | if (getContext().getTypeSize(Base) * Members != |
| 3603 | getContext().getTypeSize(Ty)) |
| 3604 | return false; |
| 3605 | } else { |
| 3606 | Members = 1; |
| 3607 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3608 | Members = 2; |
| 3609 | Ty = CT->getElementType(); |
| 3610 | } |
| 3611 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3612 | // Most ABIs only support float, double, and some vector type widths. |
| 3613 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3614 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3615 | |
| 3616 | // The base type must be the same for all members. Types that |
| 3617 | // agree in both total size and mode (float vs. vector) are |
| 3618 | // treated as being equivalent here. |
| 3619 | const Type *TyPtr = Ty.getTypePtr(); |
| 3620 | if (!Base) |
| 3621 | Base = TyPtr; |
| 3622 | |
| 3623 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3624 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3625 | return false; |
| 3626 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3627 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3628 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3629 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3630 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3631 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3632 | // double, long double, or 128-bit vectors. |
| 3633 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3634 | if (BT->getKind() == BuiltinType::Float || |
| 3635 | BT->getKind() == BuiltinType::Double || |
| 3636 | BT->getKind() == BuiltinType::LongDouble) |
| 3637 | return true; |
| 3638 | } |
| 3639 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3640 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3641 | return true; |
| 3642 | } |
| 3643 | return false; |
| 3644 | } |
| 3645 | |
| 3646 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3647 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3648 | // Vector types require one register, floating point types require one |
| 3649 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3650 | uint32_t NumRegs = |
| 3651 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3652 | |
| 3653 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3654 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3657 | ABIArgInfo |
| 3658 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3659 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3660 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3661 | if (Ty->isAnyComplexType()) |
| 3662 | return ABIArgInfo::getDirect(); |
| 3663 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3664 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3665 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3666 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3667 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3668 | if (Size > 128) |
| 3669 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3670 | else if (Size < 128) { |
| 3671 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3672 | return ABIArgInfo::getDirect(CoerceTy); |
| 3673 | } |
| 3674 | } |
| 3675 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3676 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3677 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3678 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3679 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3680 | bool Align32; |
| 3681 | uint64_t ABIAlign = isAlignedParamType(Ty, Align32) ? |
| 3682 | (Align32 ? 32 : 16) : 8; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3683 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3684 | |
| 3685 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3686 | const Type *Base = nullptr; |
| 3687 | uint64_t Members = 0; |
| 3688 | if (Kind == ELFv2 && |
| 3689 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3690 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3691 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3692 | return ABIArgInfo::getDirect(CoerceTy); |
| 3693 | } |
| 3694 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3695 | // If an aggregate may end up fully in registers, we do not |
| 3696 | // use the ByVal method, but pass the aggregate as array. |
| 3697 | // This is usually beneficial since we avoid forcing the |
| 3698 | // back-end to store the argument to memory. |
| 3699 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3700 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3701 | llvm::Type *CoerceTy; |
| 3702 | |
| 3703 | // Types up to 8 bytes are passed as integer type (which will be |
| 3704 | // properly aligned in the argument save area doubleword). |
| 3705 | if (Bits <= GPRBits) |
| 3706 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3707 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3708 | // Larger types are passed as arrays, with the base type selected |
| 3709 | // according to the required alignment in the save area. |
| 3710 | else { |
| 3711 | uint64_t RegBits = ABIAlign * 8; |
| 3712 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3713 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3714 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3715 | } |
| 3716 | |
| 3717 | return ABIArgInfo::getDirect(CoerceTy); |
| 3718 | } |
| 3719 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3720 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3721 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3722 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | return (isPromotableTypeForABI(Ty) ? |
| 3726 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3727 | } |
| 3728 | |
| 3729 | ABIArgInfo |
| 3730 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3731 | if (RetTy->isVoidType()) |
| 3732 | return ABIArgInfo::getIgnore(); |
| 3733 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3734 | if (RetTy->isAnyComplexType()) |
| 3735 | return ABIArgInfo::getDirect(); |
| 3736 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3737 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3738 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3739 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3740 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3741 | if (Size > 128) |
| 3742 | return ABIArgInfo::getIndirect(0); |
| 3743 | else if (Size < 128) { |
| 3744 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3745 | return ABIArgInfo::getDirect(CoerceTy); |
| 3746 | } |
| 3747 | } |
| 3748 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3749 | if (isAggregateTypeForABI(RetTy)) { |
| 3750 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3751 | const Type *Base = nullptr; |
| 3752 | uint64_t Members = 0; |
| 3753 | if (Kind == ELFv2 && |
| 3754 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3755 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3756 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3757 | return ABIArgInfo::getDirect(CoerceTy); |
| 3758 | } |
| 3759 | |
| 3760 | // ELFv2 small aggregates are returned in up to two registers. |
| 3761 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3762 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3763 | if (Bits == 0) |
| 3764 | return ABIArgInfo::getIgnore(); |
| 3765 | |
| 3766 | llvm::Type *CoerceTy; |
| 3767 | if (Bits > GPRBits) { |
| 3768 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3769 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3770 | } else |
| 3771 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3772 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3773 | return ABIArgInfo::getDirect(CoerceTy); |
| 3774 | } |
| 3775 | |
| 3776 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3777 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3778 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3779 | |
| 3780 | return (isPromotableTypeForABI(RetTy) ? |
| 3781 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3782 | } |
| 3783 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3784 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3785 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3786 | QualType Ty, |
| 3787 | CodeGenFunction &CGF) const { |
| 3788 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3789 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3790 | |
| 3791 | CGBuilderTy &Builder = CGF.Builder; |
| 3792 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3793 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3794 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3795 | // Handle types that require 16-byte alignment in the parameter save area. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3796 | bool Align32; |
| 3797 | if (isAlignedParamType(Ty, Align32)) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3798 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3799 | AddrAsInt = Builder.CreateAdd(AddrAsInt, |
| 3800 | Builder.getInt64(Align32 ? 31 : 15)); |
| 3801 | AddrAsInt = Builder.CreateAnd(AddrAsInt, |
| 3802 | Builder.getInt64(Align32 ? -32 : -16)); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3803 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3804 | } |
| 3805 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3806 | // Update the va_list pointer. The pointer should be bumped by the |
| 3807 | // size of the object. We can trust getTypeSize() except for a complex |
| 3808 | // type whose base type is smaller than a doubleword. For these, the |
| 3809 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3810 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3811 | QualType BaseTy; |
| 3812 | unsigned CplxBaseSize = 0; |
| 3813 | |
| 3814 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3815 | BaseTy = CTy->getElementType(); |
| 3816 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3817 | if (CplxBaseSize < 8) |
| 3818 | SizeInBytes = 16; |
| 3819 | } |
| 3820 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3821 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3822 | llvm::Value *NextAddr = |
| 3823 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3824 | "ap.next"); |
| 3825 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3826 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3827 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3828 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3829 | // in separate doublewords. However, Clang expects us to produce a |
| 3830 | // pointer to a structure with the two parts packed tightly. So generate |
| 3831 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3832 | // and store them to a temporary structure. |
| 3833 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3834 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3835 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3836 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3837 | RealAddr = |
| 3838 | Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3839 | ImagAddr = |
| 3840 | Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3841 | } else { |
| 3842 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3843 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3844 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3845 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3846 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3847 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3848 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3849 | llvm::AllocaInst *Ptr = |
| 3850 | CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), "vacplx"); |
| 3851 | llvm::Value *RealPtr = |
| 3852 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 0, ".real"); |
| 3853 | llvm::Value *ImagPtr = |
| 3854 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 1, ".imag"); |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3855 | Builder.CreateStore(Real, RealPtr, false); |
| 3856 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3857 | return Ptr; |
| 3858 | } |
| 3859 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3860 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3861 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3862 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3863 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3864 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3865 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3866 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3867 | } |
| 3868 | |
| 3869 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3870 | return Builder.CreateBitCast(Addr, PTy); |
| 3871 | } |
| 3872 | |
| 3873 | static bool |
| 3874 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3875 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3876 | // This is calculated from the LLVM and GCC tables and verified |
| 3877 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3878 | |
| 3879 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3880 | |
| 3881 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3882 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3883 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3884 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3885 | |
| 3886 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3887 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3888 | |
| 3889 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3890 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3891 | |
| 3892 | // 64-76 are various 4-byte special-purpose registers: |
| 3893 | // 64: mq |
| 3894 | // 65: lr |
| 3895 | // 66: ctr |
| 3896 | // 67: ap |
| 3897 | // 68-75 cr0-7 |
| 3898 | // 76: xer |
| 3899 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3900 | |
| 3901 | // 77-108: v0-31, the 16-byte vector registers |
| 3902 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3903 | |
| 3904 | // 109: vrsave |
| 3905 | // 110: vscr |
| 3906 | // 111: spe_acc |
| 3907 | // 112: spefscr |
| 3908 | // 113: sfp |
| 3909 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3910 | |
| 3911 | return false; |
| 3912 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3913 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3914 | bool |
| 3915 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3916 | CodeGen::CodeGenFunction &CGF, |
| 3917 | llvm::Value *Address) const { |
| 3918 | |
| 3919 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3920 | } |
| 3921 | |
| 3922 | bool |
| 3923 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3924 | llvm::Value *Address) const { |
| 3925 | |
| 3926 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3927 | } |
| 3928 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3929 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3930 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3931 | //===----------------------------------------------------------------------===// |
| 3932 | |
| 3933 | namespace { |
| 3934 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3935 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3936 | public: |
| 3937 | enum ABIKind { |
| 3938 | AAPCS = 0, |
| 3939 | DarwinPCS |
| 3940 | }; |
| 3941 | |
| 3942 | private: |
| 3943 | ABIKind Kind; |
| 3944 | |
| 3945 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3946 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3947 | |
| 3948 | private: |
| 3949 | ABIKind getABIKind() const { return Kind; } |
| 3950 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3951 | |
| 3952 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3953 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3954 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3955 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3956 | uint64_t Members) const override; |
| 3957 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3958 | bool isIllegalVectorType(QualType Ty) const; |
| 3959 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 3960 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3961 | if (!getCXXABI().classifyReturnType(FI)) |
| 3962 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3963 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3964 | for (auto &it : FI.arguments()) |
| 3965 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3969 | CodeGenFunction &CGF) const; |
| 3970 | |
| 3971 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3972 | CodeGenFunction &CGF) const; |
| 3973 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3974 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3975 | CodeGenFunction &CGF) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3976 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3977 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3978 | } |
| 3979 | }; |
| 3980 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3981 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3982 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3983 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3984 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3985 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3986 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3987 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3988 | } |
| 3989 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3990 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 3991 | return 31; |
| 3992 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3993 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3994 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3995 | }; |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 3996 | } // namespace |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3997 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3998 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3999 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4000 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4001 | // Handle illegal vector types here. |
| 4002 | if (isIllegalVectorType(Ty)) { |
| 4003 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4004 | if (Size <= 32) { |
| 4005 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4006 | return ABIArgInfo::getDirect(ResType); |
| 4007 | } |
| 4008 | if (Size == 64) { |
| 4009 | llvm::Type *ResType = |
| 4010 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4011 | return ABIArgInfo::getDirect(ResType); |
| 4012 | } |
| 4013 | if (Size == 128) { |
| 4014 | llvm::Type *ResType = |
| 4015 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4016 | return ABIArgInfo::getDirect(ResType); |
| 4017 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4018 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4019 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4020 | |
| 4021 | if (!isAggregateTypeForABI(Ty)) { |
| 4022 | // Treat an enum type as its underlying type. |
| 4023 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4024 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4025 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4026 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 4027 | ? ABIArgInfo::getExtend() |
| 4028 | : ABIArgInfo::getDirect()); |
| 4029 | } |
| 4030 | |
| 4031 | // Structures with either a non-trivial destructor or a non-trivial |
| 4032 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4033 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4034 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4035 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 4039 | // elsewhere for GNU compatibility. |
| 4040 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4041 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 4042 | return ABIArgInfo::getIgnore(); |
| 4043 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4044 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4045 | } |
| 4046 | |
| 4047 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4048 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4049 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4050 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4051 | return ABIArgInfo::getDirect( |
| 4052 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
| 4055 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4056 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4057 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4058 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4059 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4060 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4061 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4062 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4063 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4064 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4065 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4066 | } |
| 4067 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4068 | } |
| 4069 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4070 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4071 | } |
| 4072 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4073 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4074 | if (RetTy->isVoidType()) |
| 4075 | return ABIArgInfo::getIgnore(); |
| 4076 | |
| 4077 | // Large vector types should be returned via memory. |
| 4078 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 4079 | return ABIArgInfo::getIndirect(0); |
| 4080 | |
| 4081 | if (!isAggregateTypeForABI(RetTy)) { |
| 4082 | // Treat an enum type as its underlying type. |
| 4083 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4084 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4085 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4086 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4087 | ? ABIArgInfo::getExtend() |
| 4088 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4091 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4092 | return ABIArgInfo::getIgnore(); |
| 4093 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4094 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4095 | uint64_t Members = 0; |
| 4096 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4097 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4098 | return ABIArgInfo::getDirect(); |
| 4099 | |
| 4100 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4101 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4102 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4103 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4104 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4105 | |
| 4106 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4107 | // For aggregates with 16-byte alignment, we use i128. |
| 4108 | if (Alignment < 128 && Size == 128) { |
| 4109 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4110 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4111 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4112 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4113 | } |
| 4114 | |
| 4115 | return ABIArgInfo::getIndirect(0); |
| 4116 | } |
| 4117 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4118 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4119 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4120 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4121 | // Check whether VT is legal. |
| 4122 | unsigned NumElements = VT->getNumElements(); |
| 4123 | uint64_t Size = getContext().getTypeSize(VT); |
| 4124 | // NumElements should be power of 2 between 1 and 16. |
| 4125 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4126 | return true; |
| 4127 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4128 | } |
| 4129 | return false; |
| 4130 | } |
| 4131 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4132 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4133 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4134 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4135 | // but with the difference that any floating-point type is allowed, |
| 4136 | // including __fp16. |
| 4137 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4138 | if (BT->isFloatingPoint()) |
| 4139 | return true; |
| 4140 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4141 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4142 | if (VecSize == 64 || VecSize == 128) |
| 4143 | return true; |
| 4144 | } |
| 4145 | return false; |
| 4146 | } |
| 4147 | |
| 4148 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4149 | uint64_t Members) const { |
| 4150 | return Members <= 4; |
| 4151 | } |
| 4152 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4153 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, |
| 4154 | QualType Ty, |
| 4155 | CodeGenFunction &CGF) const { |
| 4156 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4157 | bool IsIndirect = AI.isIndirect(); |
| 4158 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4159 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4160 | if (IsIndirect) |
| 4161 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4162 | else if (AI.getCoerceToType()) |
| 4163 | BaseTy = AI.getCoerceToType(); |
| 4164 | |
| 4165 | unsigned NumRegs = 1; |
| 4166 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4167 | BaseTy = ArrTy->getElementType(); |
| 4168 | NumRegs = ArrTy->getNumElements(); |
| 4169 | } |
| 4170 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4171 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4172 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4173 | // Standard, section B.4: |
| 4174 | // |
| 4175 | // struct { |
| 4176 | // void *__stack; |
| 4177 | // void *__gr_top; |
| 4178 | // void *__vr_top; |
| 4179 | // int __gr_offs; |
| 4180 | // int __vr_offs; |
| 4181 | // }; |
| 4182 | |
| 4183 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4184 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4185 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4186 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4187 | auto &Ctx = CGF.getContext(); |
| 4188 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4189 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4190 | int reg_top_index; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4191 | int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8; |
| 4192 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4193 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4194 | reg_offs_p = |
| 4195 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4196 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4197 | reg_top_index = 1; // field number for __gr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4198 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4199 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4200 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4201 | reg_offs_p = |
| 4202 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4203 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4204 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4205 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | //======================================= |
| 4209 | // Find out where argument was passed |
| 4210 | //======================================= |
| 4211 | |
| 4212 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4213 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4214 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4215 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4216 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4217 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4218 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4219 | |
| 4220 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4221 | |
| 4222 | // 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] | 4223 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4224 | CGF.EmitBlock(MaybeRegBlock); |
| 4225 | |
| 4226 | // Integer arguments may need to correct register alignment (for example a |
| 4227 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4228 | // align __gr_offs to calculate the potential address. |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4229 | if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4230 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4231 | |
| 4232 | reg_offs = CGF.Builder.CreateAdd( |
| 4233 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4234 | "align_regoffs"); |
| 4235 | reg_offs = CGF.Builder.CreateAnd( |
| 4236 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4237 | "aligned_regoffs"); |
| 4238 | } |
| 4239 | |
| 4240 | // 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] | 4241 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4242 | NewOffset = CGF.Builder.CreateAdd( |
| 4243 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4244 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4245 | |
| 4246 | // Now we're in a position to decide whether this argument really was in |
| 4247 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4248 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4249 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4250 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4251 | |
| 4252 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4253 | |
| 4254 | //======================================= |
| 4255 | // Argument was in registers |
| 4256 | //======================================= |
| 4257 | |
| 4258 | // Now we emit the code for if the argument was originally passed in |
| 4259 | // registers. First start the appropriate block: |
| 4260 | CGF.EmitBlock(InRegBlock); |
| 4261 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4262 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4263 | reg_top_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, reg_top_index, |
| 4264 | "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4265 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4266 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4267 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4268 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4269 | |
| 4270 | if (IsIndirect) { |
| 4271 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4272 | // stored registers or on the stack will actually be a struct **. |
| 4273 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4274 | } |
| 4275 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4276 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4277 | uint64_t NumMembers = 0; |
| 4278 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4279 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4280 | // Homogeneous aggregates passed in registers will have their elements split |
| 4281 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4282 | // qN+1, ...). We reload and store into a temporary local variable |
| 4283 | // contiguously. |
| 4284 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 4285 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4286 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4287 | llvm::AllocaInst *Tmp = CGF.CreateTempAlloca(HFATy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4288 | int Offset = 0; |
| 4289 | |
| 4290 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 4291 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 4292 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4293 | llvm::Value *BaseOffset = |
| 4294 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 4295 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4296 | LoadAddr = CGF.Builder.CreateBitCast( |
| 4297 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4298 | llvm::Value *StoreAddr = |
| 4299 | CGF.Builder.CreateStructGEP(Tmp->getAllocatedType(), Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4300 | |
| 4301 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4302 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4303 | } |
| 4304 | |
| 4305 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4306 | } else { |
| 4307 | // Otherwise the object is contiguous in memory |
| 4308 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4309 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4310 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4311 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 4312 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 4313 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 4314 | |
| 4315 | BaseAddr = CGF.Builder.CreateAdd( |
| 4316 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4317 | |
| 4318 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 4319 | } |
| 4320 | |
| 4321 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4322 | } |
| 4323 | |
| 4324 | CGF.EmitBranch(ContBlock); |
| 4325 | |
| 4326 | //======================================= |
| 4327 | // Argument was on the stack |
| 4328 | //======================================= |
| 4329 | CGF.EmitBlock(OnStackBlock); |
| 4330 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4331 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4332 | stack_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "stack_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4333 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4334 | |
| 4335 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4336 | // floating-point ones might be affected. |
| 4337 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 4338 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4339 | |
| 4340 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4341 | |
| 4342 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4343 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4344 | "align_stack"); |
| 4345 | OnStackAddr = CGF.Builder.CreateAnd( |
| 4346 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4347 | "align_stack"); |
| 4348 | |
| 4349 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4350 | } |
| 4351 | |
| 4352 | uint64_t StackSize; |
| 4353 | if (IsIndirect) |
| 4354 | StackSize = 8; |
| 4355 | else |
| 4356 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 4357 | |
| 4358 | // All stack slots are 8 bytes |
| 4359 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4360 | |
| 4361 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4362 | llvm::Value *NewStack = |
| 4363 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 4364 | |
| 4365 | // Write the new value of __stack for the next call to va_arg |
| 4366 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4367 | |
| 4368 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4369 | Ctx.getTypeSize(Ty) < 64) { |
| 4370 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 4371 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4372 | |
| 4373 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4374 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4375 | |
| 4376 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4377 | } |
| 4378 | |
| 4379 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4380 | |
| 4381 | CGF.EmitBranch(ContBlock); |
| 4382 | |
| 4383 | //======================================= |
| 4384 | // Tidy up |
| 4385 | //======================================= |
| 4386 | CGF.EmitBlock(ContBlock); |
| 4387 | |
| 4388 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4389 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4390 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4391 | |
| 4392 | if (IsIndirect) |
| 4393 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4394 | |
| 4395 | return ResAddr; |
| 4396 | } |
| 4397 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4398 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, |
| 4399 | QualType Ty, |
| 4400 | CodeGenFunction &CGF) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4401 | // We do not support va_arg for aggregates or illegal vector types. |
| 4402 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 4403 | // other cases. |
| 4404 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4405 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4406 | |
| 4407 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 4408 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 4409 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4410 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4411 | uint64_t Members = 0; |
| 4412 | bool isHA = isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4413 | |
| 4414 | bool isIndirect = false; |
| 4415 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 4416 | // be passed indirectly. |
| 4417 | if (Size > 16 && !isHA) { |
| 4418 | isIndirect = true; |
| 4419 | Size = 8; |
| 4420 | Align = 8; |
| 4421 | } |
| 4422 | |
| 4423 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 4424 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 4425 | |
| 4426 | CGBuilderTy &Builder = CGF.Builder; |
| 4427 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4428 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4429 | |
| 4430 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4431 | // These are ignored for parameter passing purposes. |
| 4432 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4433 | return Builder.CreateBitCast(Addr, PTy); |
| 4434 | } |
| 4435 | |
| 4436 | const uint64_t MinABIAlign = 8; |
| 4437 | if (Align > MinABIAlign) { |
| 4438 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 4439 | Addr = Builder.CreateGEP(Addr, Offset); |
| 4440 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 4441 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 4442 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 4443 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 4444 | } |
| 4445 | |
| 4446 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 4447 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 4448 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 4449 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4450 | |
| 4451 | if (isIndirect) |
| 4452 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 4453 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4454 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4455 | |
| 4456 | return AddrTyped; |
| 4457 | } |
| 4458 | |
| 4459 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4460 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4461 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4462 | |
| 4463 | namespace { |
| 4464 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4465 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4466 | public: |
| 4467 | enum ABIKind { |
| 4468 | APCS = 0, |
| 4469 | AAPCS = 1, |
| 4470 | AAPCS_VFP |
| 4471 | }; |
| 4472 | |
| 4473 | private: |
| 4474 | ABIKind Kind; |
| 4475 | |
| 4476 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4477 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4478 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4479 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4480 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4481 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4482 | switch (getTarget().getTriple().getEnvironment()) { |
| 4483 | case llvm::Triple::Android: |
| 4484 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4485 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4486 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4487 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4488 | return true; |
| 4489 | default: |
| 4490 | return false; |
| 4491 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4494 | bool isEABIHF() const { |
| 4495 | switch (getTarget().getTriple().getEnvironment()) { |
| 4496 | case llvm::Triple::EABIHF: |
| 4497 | case llvm::Triple::GNUEABIHF: |
| 4498 | return true; |
| 4499 | default: |
| 4500 | return false; |
| 4501 | } |
| 4502 | } |
| 4503 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4504 | ABIKind getABIKind() const { return Kind; } |
| 4505 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4506 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4507 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4508 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4509 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4510 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4511 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4512 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4513 | uint64_t Members) const override; |
| 4514 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4515 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4516 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4517 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4518 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4519 | |
| 4520 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4521 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4522 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4523 | }; |
| 4524 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4525 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4526 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4527 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4528 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4529 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4530 | const ARMABIInfo &getABIInfo() const { |
| 4531 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4532 | } |
| 4533 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4534 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4535 | return 13; |
| 4536 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4537 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4538 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4539 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4540 | } |
| 4541 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4542 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4543 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4544 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4545 | |
| 4546 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4547 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4548 | return false; |
| 4549 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4550 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4551 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4552 | if (getABIInfo().isEABI()) return 88; |
| 4553 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4554 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4555 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4556 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4557 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4558 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4559 | if (!FD) |
| 4560 | return; |
| 4561 | |
| 4562 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4563 | if (!Attr) |
| 4564 | return; |
| 4565 | |
| 4566 | const char *Kind; |
| 4567 | switch (Attr->getInterrupt()) { |
| 4568 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4569 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4570 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4571 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4572 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4573 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4574 | } |
| 4575 | |
| 4576 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4577 | |
| 4578 | Fn->addFnAttr("interrupt", Kind); |
| 4579 | |
| 4580 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4581 | return; |
| 4582 | |
| 4583 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4584 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4585 | // the backend to perform a realignment as part of the function prologue. |
| 4586 | llvm::AttrBuilder B; |
| 4587 | B.addStackAlignmentAttr(8); |
| 4588 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4589 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4590 | llvm::AttributeSet::FunctionIndex, |
| 4591 | B)); |
| 4592 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4593 | }; |
| 4594 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4595 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4596 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4597 | CodeGen::CodeGenModule &CGM) const; |
| 4598 | |
| 4599 | public: |
| 4600 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4601 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4602 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4603 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4604 | CodeGen::CodeGenModule &CGM) const override; |
| 4605 | }; |
| 4606 | |
| 4607 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4608 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4609 | if (!isa<FunctionDecl>(D)) |
| 4610 | return; |
| 4611 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4612 | return; |
| 4613 | |
| 4614 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4615 | F->addFnAttr("stack-probe-size", |
| 4616 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4617 | } |
| 4618 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4619 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4620 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 4621 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4622 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4623 | } |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 4624 | } // namespace |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4625 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4626 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4627 | if (!getCXXABI().classifyReturnType(FI)) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4628 | FI.getReturnInfo() = |
| 4629 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4630 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4631 | for (auto &I : FI.arguments()) |
| 4632 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4633 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4634 | // Always honor user-specified calling convention. |
| 4635 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4636 | return; |
| 4637 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4638 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4639 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4640 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4641 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4642 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4643 | /// Return the default calling convention that LLVM will use. |
| 4644 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4645 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4646 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4647 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4648 | else if (isEABI()) |
| 4649 | return llvm::CallingConv::ARM_AAPCS; |
| 4650 | else |
| 4651 | return llvm::CallingConv::ARM_APCS; |
| 4652 | } |
| 4653 | |
| 4654 | /// Return the calling convention that our ABI would like us to use |
| 4655 | /// as the C calling convention. |
| 4656 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4657 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4658 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4659 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4660 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4661 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4662 | llvm_unreachable("bad ABI kind"); |
| 4663 | } |
| 4664 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4665 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4666 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4667 | |
| 4668 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4669 | // they'd just match what LLVM will infer from the triple. |
| 4670 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4671 | if (abiCC != getLLVMDefaultCC()) |
| 4672 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4673 | |
| 4674 | BuiltinCC = (getABIKind() == APCS ? |
| 4675 | llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4676 | } |
| 4677 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4678 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4679 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4680 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4681 | // A single-precision floating-point type (including promoted |
| 4682 | // half-precision types); A double-precision floating-point type; |
| 4683 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4684 | // with a Base Type of a single- or double-precision floating-point type, |
| 4685 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4686 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4687 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4688 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4689 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4690 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4691 | // Handle illegal vector types here. |
| 4692 | if (isIllegalVectorType(Ty)) { |
| 4693 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4694 | if (Size <= 32) { |
| 4695 | llvm::Type *ResType = |
| 4696 | llvm::Type::getInt32Ty(getVMContext()); |
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 | if (Size == 64) { |
| 4700 | llvm::Type *ResType = llvm::VectorType::get( |
| 4701 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4702 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4703 | } |
| 4704 | if (Size == 128) { |
| 4705 | llvm::Type *ResType = llvm::VectorType::get( |
| 4706 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4707 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4708 | } |
| 4709 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4710 | } |
| 4711 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4712 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4713 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4714 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4715 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4716 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4717 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4718 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4719 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4720 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4721 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4722 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4723 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4724 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4725 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4726 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4727 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4728 | return ABIArgInfo::getIgnore(); |
| 4729 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4730 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4731 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4732 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4733 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4734 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4735 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4736 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4737 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4738 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4739 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4742 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4743 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4744 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4745 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4746 | uint64_t ABIAlign = 4; |
| 4747 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4748 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4749 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4750 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4751 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4752 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4753 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4754 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4757 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4758 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4759 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4760 | // FIXME: Try to match the types of the arguments more accurately where |
| 4761 | // we can. |
| 4762 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4763 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4764 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4765 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4766 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4767 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4768 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4769 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4770 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4773 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4774 | llvm::LLVMContext &VMContext) { |
| 4775 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4776 | // is called integer-like if its size is less than or equal to one word, and |
| 4777 | // the offset of each of its addressable sub-fields is zero. |
| 4778 | |
| 4779 | uint64_t Size = Context.getTypeSize(Ty); |
| 4780 | |
| 4781 | // Check that the type fits in a word. |
| 4782 | if (Size > 32) |
| 4783 | return false; |
| 4784 | |
| 4785 | // FIXME: Handle vector types! |
| 4786 | if (Ty->isVectorType()) |
| 4787 | return false; |
| 4788 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4789 | // Float types are never treated as "integer like". |
| 4790 | if (Ty->isRealFloatingType()) |
| 4791 | return false; |
| 4792 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4793 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4794 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4795 | return true; |
| 4796 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4797 | // Small complex integer types are "integer like". |
| 4798 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4799 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4800 | |
| 4801 | // Single element and zero sized arrays should be allowed, by the definition |
| 4802 | // above, but they are not. |
| 4803 | |
| 4804 | // Otherwise, it must be a record type. |
| 4805 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4806 | if (!RT) return false; |
| 4807 | |
| 4808 | // Ignore records with flexible arrays. |
| 4809 | const RecordDecl *RD = RT->getDecl(); |
| 4810 | if (RD->hasFlexibleArrayMember()) |
| 4811 | return false; |
| 4812 | |
| 4813 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4814 | // like". |
| 4815 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4816 | |
| 4817 | bool HadField = false; |
| 4818 | unsigned idx = 0; |
| 4819 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4820 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4821 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4822 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4823 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4824 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4825 | // struct { int : 0; int x } |
| 4826 | // is non-integer like according to gcc. |
| 4827 | if (FD->isBitField()) { |
| 4828 | if (!RD->isUnion()) |
| 4829 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4830 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4831 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4832 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4833 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4834 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4835 | } |
| 4836 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4837 | // Check if this field is at offset 0. |
| 4838 | if (Layout.getFieldOffset(idx) != 0) |
| 4839 | return false; |
| 4840 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4841 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4842 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4843 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4844 | // Only allow at most one field in a structure. This doesn't match the |
| 4845 | // wording above, but follows gcc in situations with a field following an |
| 4846 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4847 | if (!RD->isUnion()) { |
| 4848 | if (HadField) |
| 4849 | return false; |
| 4850 | |
| 4851 | HadField = true; |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | return true; |
| 4856 | } |
| 4857 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4858 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4859 | bool isVariadic) const { |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4860 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4861 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4862 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4863 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4864 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4865 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4866 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4867 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4868 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4869 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4870 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4871 | // Treat an enum type as its underlying type. |
| 4872 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4873 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4874 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4875 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4876 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4877 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4878 | |
| 4879 | // Are we following APCS? |
| 4880 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4881 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4882 | return ABIArgInfo::getIgnore(); |
| 4883 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4884 | // Complex types are all returned as packed integers. |
| 4885 | // |
| 4886 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4887 | // correctly. |
| 4888 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4889 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 4890 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4891 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4892 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4893 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4894 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4895 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4896 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4897 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4898 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4899 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4900 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | // Otherwise return in memory. |
| 4904 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4905 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4906 | |
| 4907 | // Otherwise this is an AAPCS variant. |
| 4908 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4909 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4910 | return ABIArgInfo::getIgnore(); |
| 4911 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4912 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4913 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4914 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4915 | uint64_t Members; |
| 4916 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4917 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4918 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4919 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4920 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4921 | } |
| 4922 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4923 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4924 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4925 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4926 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4927 | if (getDataLayout().isBigEndian()) |
| 4928 | // 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] | 4929 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4930 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4931 | // Return in the smallest viable integer type. |
| 4932 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4933 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4934 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4935 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4936 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4939 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4942 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4943 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4944 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4945 | // Check whether VT is legal. |
| 4946 | unsigned NumElements = VT->getNumElements(); |
| 4947 | uint64_t Size = getContext().getTypeSize(VT); |
| 4948 | // NumElements should be power of 2. |
| 4949 | if ((NumElements & (NumElements - 1)) != 0) |
| 4950 | return true; |
| 4951 | // Size should be greater than 32 bits. |
| 4952 | return Size <= 32; |
| 4953 | } |
| 4954 | return false; |
| 4955 | } |
| 4956 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4957 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4958 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4959 | // double, or 64-bit or 128-bit vectors. |
| 4960 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4961 | if (BT->getKind() == BuiltinType::Float || |
| 4962 | BT->getKind() == BuiltinType::Double || |
| 4963 | BT->getKind() == BuiltinType::LongDouble) |
| 4964 | return true; |
| 4965 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4966 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4967 | if (VecSize == 64 || VecSize == 128) |
| 4968 | return true; |
| 4969 | } |
| 4970 | return false; |
| 4971 | } |
| 4972 | |
| 4973 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4974 | uint64_t Members) const { |
| 4975 | return Members <= 4; |
| 4976 | } |
| 4977 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4978 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4979 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4980 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4981 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4982 | |
| 4983 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4984 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4985 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4986 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4987 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4988 | // These are ignored for parameter passing purposes. |
| 4989 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4990 | return Builder.CreateBitCast(Addr, PTy); |
| 4991 | } |
| 4992 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4993 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4994 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4995 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4996 | |
| 4997 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4998 | // 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] | 4999 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 5000 | getABIKind() == ARMABIInfo::AAPCS) |
| 5001 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 5002 | else |
| 5003 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5004 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 5005 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 5006 | IsIndirect = true; |
| 5007 | Size = 4; |
| 5008 | TyAlign = 4; |
| 5009 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5010 | |
| 5011 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 5012 | if (TyAlign > 4) { |
| 5013 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 5014 | "Alignment is not power of 2!"); |
| 5015 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 5016 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 5017 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5018 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 5019 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5020 | |
| 5021 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5022 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5023 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 5024 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5025 | "ap.next"); |
| 5026 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5027 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5028 | if (IsIndirect) |
| 5029 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 5030 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 5031 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 5032 | // may not be correctly aligned for the vector type. We create an aligned |
| 5033 | // temporary space and copy the content over from ap.cur to the temporary |
| 5034 | // space. This is necessary if the natural alignment of the type is greater |
| 5035 | // than the ABI alignment. |
| 5036 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 5037 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 5038 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 5039 | "var.align"); |
| 5040 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 5041 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 5042 | Builder.CreateMemCpy(Dst, Src, |
| 5043 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 5044 | TyAlign, false); |
| 5045 | Addr = AlignedTemp; //The content is in aligned location. |
| 5046 | } |
| 5047 | llvm::Type *PTy = |
| 5048 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5049 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5050 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5051 | return AddrTyped; |
| 5052 | } |
| 5053 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5054 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5055 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5056 | //===----------------------------------------------------------------------===// |
| 5057 | |
| 5058 | namespace { |
| 5059 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5060 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5061 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5062 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5063 | |
| 5064 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5065 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5066 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5067 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5068 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5069 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5070 | }; |
| 5071 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5072 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5073 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5074 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5075 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5076 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5077 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5078 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5079 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5080 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5081 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5082 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5083 | }; |
| 5084 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5085 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5086 | if (RetTy->isVoidType()) |
| 5087 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5088 | |
| 5089 | // note: this is different from default ABI |
| 5090 | if (!RetTy->isScalarType()) |
| 5091 | return ABIArgInfo::getDirect(); |
| 5092 | |
| 5093 | // Treat an enum type as its underlying type. |
| 5094 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5095 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5096 | |
| 5097 | return (RetTy->isPromotableIntegerType() ? |
| 5098 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5101 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5102 | // Treat an enum type as its underlying type. |
| 5103 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5104 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5105 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5106 | // Return aggregates type as indirect by value |
| 5107 | if (isAggregateTypeForABI(Ty)) |
| 5108 | return ABIArgInfo::getIndirect(0, /* byval */ true); |
| 5109 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5110 | return (Ty->isPromotableIntegerType() ? |
| 5111 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5112 | } |
| 5113 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5114 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5115 | if (!getCXXABI().classifyReturnType(FI)) |
| 5116 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5117 | for (auto &I : FI.arguments()) |
| 5118 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5119 | |
| 5120 | // Always honor user-specified calling convention. |
| 5121 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5122 | return; |
| 5123 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5124 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5125 | } |
| 5126 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5127 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5128 | CodeGenFunction &CFG) const { |
| 5129 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5132 | void NVPTXTargetCodeGenInfo:: |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5133 | setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5134 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5135 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5136 | if (!FD) return; |
| 5137 | |
| 5138 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5139 | |
| 5140 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5141 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5142 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5143 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5144 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5145 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5146 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5147 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5148 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5149 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5150 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5151 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5152 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5153 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5154 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5155 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5156 | // __global__ functions cannot be called from the device, we do not |
| 5157 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5158 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5159 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5160 | addNVVMMetadata(F, "kernel", 1); |
| 5161 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5162 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5163 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5164 | llvm::APSInt MaxThreads(32); |
| 5165 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5166 | if (MaxThreads > 0) |
| 5167 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5168 | |
| 5169 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5170 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5171 | // we don't have to add a PTX directive. |
| 5172 | if (Attr->getMinBlocks()) { |
| 5173 | llvm::APSInt MinBlocks(32); |
| 5174 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5175 | if (MinBlocks > 0) |
| 5176 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5177 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5178 | } |
| 5179 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5180 | } |
| 5181 | } |
| 5182 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5183 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5184 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5185 | llvm::Module *M = F->getParent(); |
| 5186 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5187 | |
| 5188 | // Get "nvvm.annotations" metadata node |
| 5189 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5190 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5191 | llvm::Metadata *MDVals[] = { |
| 5192 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5193 | llvm::ConstantAsMetadata::get( |
| 5194 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5195 | // Append metadata to nvvm.annotations |
| 5196 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5197 | } |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 5198 | } // namespace |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5199 | |
| 5200 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5201 | // SystemZ ABI Implementation |
| 5202 | //===----------------------------------------------------------------------===// |
| 5203 | |
| 5204 | namespace { |
| 5205 | |
| 5206 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5207 | bool HasVector; |
| 5208 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5209 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5210 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5211 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5212 | |
| 5213 | bool isPromotableIntegerType(QualType Ty) const; |
| 5214 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5215 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5216 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5217 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5218 | |
| 5219 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5220 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5221 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5222 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5223 | if (!getCXXABI().classifyReturnType(FI)) |
| 5224 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5225 | for (auto &I : FI.arguments()) |
| 5226 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5229 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5230 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5231 | }; |
| 5232 | |
| 5233 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5234 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5235 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5236 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5237 | }; |
| 5238 | |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 5239 | } // namespace |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5240 | |
| 5241 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5242 | // Treat an enum type as its underlying type. |
| 5243 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5244 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5245 | |
| 5246 | // Promotable integer types are required to be promoted by the ABI. |
| 5247 | if (Ty->isPromotableIntegerType()) |
| 5248 | return true; |
| 5249 | |
| 5250 | // 32-bit values must also be promoted. |
| 5251 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5252 | switch (BT->getKind()) { |
| 5253 | case BuiltinType::Int: |
| 5254 | case BuiltinType::UInt: |
| 5255 | return true; |
| 5256 | default: |
| 5257 | return false; |
| 5258 | } |
| 5259 | return false; |
| 5260 | } |
| 5261 | |
| 5262 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5263 | return (Ty->isAnyComplexType() || |
| 5264 | Ty->isVectorType() || |
| 5265 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5266 | } |
| 5267 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5268 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5269 | return (HasVector && |
| 5270 | Ty->isVectorType() && |
| 5271 | getContext().getTypeSize(Ty) <= 128); |
| 5272 | } |
| 5273 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5274 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5275 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5276 | switch (BT->getKind()) { |
| 5277 | case BuiltinType::Float: |
| 5278 | case BuiltinType::Double: |
| 5279 | return true; |
| 5280 | default: |
| 5281 | return false; |
| 5282 | } |
| 5283 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5284 | return false; |
| 5285 | } |
| 5286 | |
| 5287 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5288 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5289 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5290 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5291 | |
| 5292 | // If this is a C++ record, check the bases first. |
| 5293 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5294 | for (const auto &I : CXXRD->bases()) { |
| 5295 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5296 | |
| 5297 | // Empty bases don't affect things either way. |
| 5298 | if (isEmptyRecord(getContext(), Base, true)) |
| 5299 | continue; |
| 5300 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5301 | if (!Found.isNull()) |
| 5302 | return Ty; |
| 5303 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5307 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5308 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5309 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5310 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5311 | if (getContext().getLangOpts().CPlusPlus && |
| 5312 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5313 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5314 | |
| 5315 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5316 | // Nested structures still do though. |
| 5317 | if (!Found.isNull()) |
| 5318 | return Ty; |
| 5319 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5323 | // 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] | 5324 | if (!Found.isNull()) |
| 5325 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5328 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5329 | } |
| 5330 | |
| 5331 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5332 | CodeGenFunction &CGF) const { |
| 5333 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5334 | // struct { |
| 5335 | // i64 __gpr; |
| 5336 | // i64 __fpr; |
| 5337 | // i8 *__overflow_arg_area; |
| 5338 | // i8 *__reg_save_area; |
| 5339 | // }; |
| 5340 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5341 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5342 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5343 | // always passed on the stack. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5344 | Ty = CGF.getContext().getCanonicalType(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5345 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
| 5346 | llvm::Type *APTy = llvm::PointerType::getUnqual(ArgTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5347 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5348 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5349 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5350 | bool IsVector = false; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5351 | unsigned UnpaddedBitSize; |
| 5352 | if (IsIndirect) { |
| 5353 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5354 | UnpaddedBitSize = 64; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5355 | } else { |
| 5356 | if (AI.getCoerceToType()) |
| 5357 | ArgTy = AI.getCoerceToType(); |
| 5358 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5359 | IsVector = ArgTy->isVectorTy(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5360 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5361 | } |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5362 | unsigned PaddedBitSize = (IsVector && UnpaddedBitSize > 64) ? 128 : 64; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5363 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5364 | |
| 5365 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5366 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5367 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5368 | llvm::Type *IndexTy = CGF.Int64Ty; |
| 5369 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5370 | |
| 5371 | if (IsVector) { |
| 5372 | // Work out the address of a vector argument on the stack. |
| 5373 | // Vector arguments are always passed in the high bits of a |
| 5374 | // single (8 byte) or double (16 byte) stack slot. |
| 5375 | llvm::Value *OverflowArgAreaPtr = |
| 5376 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 2, |
| 5377 | "overflow_arg_area_ptr"); |
| 5378 | llvm::Value *OverflowArgArea = |
| 5379 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5380 | llvm::Value *MemAddr = |
| 5381 | CGF.Builder.CreateBitCast(OverflowArgArea, APTy, "mem_addr"); |
| 5382 | |
| 5383 | // Update overflow_arg_area_ptr pointer |
| 5384 | llvm::Value *NewOverflowArgArea = |
| 5385 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5386 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5387 | |
| 5388 | return MemAddr; |
| 5389 | } |
| 5390 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5391 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5392 | if (InFPRs) { |
| 5393 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5394 | RegCountField = 1; // __fpr |
| 5395 | RegSaveIndex = 16; // save offset for f0 |
| 5396 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5397 | } else { |
| 5398 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5399 | RegCountField = 0; // __gpr |
| 5400 | RegSaveIndex = 2; // save offset for r2 |
| 5401 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5402 | } |
| 5403 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5404 | llvm::Value *RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5405 | nullptr, VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5406 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5407 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5408 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5409 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5410 | |
| 5411 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5412 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5413 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5414 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5415 | |
| 5416 | // Emit code to load the value if it was passed in registers. |
| 5417 | CGF.EmitBlock(InRegBlock); |
| 5418 | |
| 5419 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5420 | llvm::Value *ScaledRegCount = |
| 5421 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5422 | llvm::Value *RegBase = |
| 5423 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5424 | llvm::Value *RegOffset = |
| 5425 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5426 | llvm::Value *RegSaveAreaPtr = |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5427 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5428 | llvm::Value *RegSaveArea = |
| 5429 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5430 | llvm::Value *RawRegAddr = |
| 5431 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5432 | llvm::Value *RegAddr = |
| 5433 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5434 | |
| 5435 | // Update the register count |
| 5436 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5437 | llvm::Value *NewRegCount = |
| 5438 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5439 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5440 | CGF.EmitBranch(ContBlock); |
| 5441 | |
| 5442 | // Emit code to load the value if it was passed in memory. |
| 5443 | CGF.EmitBlock(InMemBlock); |
| 5444 | |
| 5445 | // Work out the address of a stack argument. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5446 | llvm::Value *OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5447 | nullptr, VAListAddr, 2, "overflow_arg_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5448 | llvm::Value *OverflowArgArea = |
| 5449 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5450 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5451 | llvm::Value *RawMemAddr = |
| 5452 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5453 | llvm::Value *MemAddr = |
| 5454 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5455 | |
| 5456 | // Update overflow_arg_area_ptr pointer |
| 5457 | llvm::Value *NewOverflowArgArea = |
| 5458 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5459 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5460 | CGF.EmitBranch(ContBlock); |
| 5461 | |
| 5462 | // Return the appropriate result. |
| 5463 | CGF.EmitBlock(ContBlock); |
| 5464 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5465 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5466 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5467 | |
| 5468 | if (IsIndirect) |
| 5469 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5470 | |
| 5471 | return ResAddr; |
| 5472 | } |
| 5473 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5474 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5475 | if (RetTy->isVoidType()) |
| 5476 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5477 | if (isVectorArgumentType(RetTy)) |
| 5478 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5479 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5480 | return ABIArgInfo::getIndirect(0); |
| 5481 | return (isPromotableIntegerType(RetTy) ? |
| 5482 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5483 | } |
| 5484 | |
| 5485 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5486 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5487 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5488 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5489 | |
| 5490 | // Integers and enums are extended to full register width. |
| 5491 | if (isPromotableIntegerType(Ty)) |
| 5492 | return ABIArgInfo::getExtend(); |
| 5493 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5494 | // Handle vector types and vector-like structure types. Note that |
| 5495 | // as opposed to float-like structure types, we do not allow any |
| 5496 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5497 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5498 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5499 | if (isVectorArgumentType(SingleElementTy) && |
| 5500 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5501 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5502 | |
| 5503 | // 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] | 5504 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5505 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5506 | |
| 5507 | // Handle small structures. |
| 5508 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5509 | // Structures with flexible arrays have variable length, so really |
| 5510 | // fail the size test above. |
| 5511 | const RecordDecl *RD = RT->getDecl(); |
| 5512 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5513 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5514 | |
| 5515 | // The structure is passed as an unextended integer, a float, or a double. |
| 5516 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5517 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5518 | assert(Size == 32 || Size == 64); |
| 5519 | if (Size == 32) |
| 5520 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5521 | else |
| 5522 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5523 | } else |
| 5524 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5525 | return ABIArgInfo::getDirect(PassTy); |
| 5526 | } |
| 5527 | |
| 5528 | // Non-structure compounds are passed indirectly. |
| 5529 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5530 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5531 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5532 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5536 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5537 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5538 | |
| 5539 | namespace { |
| 5540 | |
| 5541 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5542 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5543 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5544 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5545 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5546 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5547 | }; |
| 5548 | |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 5549 | } // namespace |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5550 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5551 | void MSP430TargetCodeGenInfo::setTargetAttributes(const Decl *D, |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5552 | llvm::GlobalValue *GV, |
| 5553 | CodeGen::CodeGenModule &M) const { |
| 5554 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5555 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5556 | // Handle 'interrupt' attribute: |
| 5557 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5558 | |
| 5559 | // Step 1: Set ISR calling convention. |
| 5560 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5561 | |
| 5562 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5563 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5564 | |
| 5565 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5566 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5567 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5568 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5569 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5570 | } |
| 5571 | } |
| 5572 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5573 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5574 | // MIPS ABI Implementation. This works for both little-endian and |
| 5575 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5576 | //===----------------------------------------------------------------------===// |
| 5577 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5578 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5579 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5580 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5581 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5582 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5583 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5584 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5585 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5586 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5587 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5588 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5589 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5590 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5591 | |
| 5592 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5593 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5594 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5595 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5596 | CodeGenFunction &CGF) const override; |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5597 | bool shouldSignExtUnsignedType(QualType Ty) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5598 | }; |
| 5599 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5600 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5601 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5602 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5603 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5604 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5605 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5606 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5607 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5608 | return 29; |
| 5609 | } |
| 5610 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5611 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5612 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5613 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5614 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5615 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5616 | if (FD->hasAttr<Mips16Attr>()) { |
| 5617 | Fn->addFnAttr("mips16"); |
| 5618 | } |
| 5619 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5620 | Fn->addFnAttr("nomips16"); |
| 5621 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5622 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5623 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5624 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5625 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5626 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5627 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5628 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5629 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5630 | }; |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 5631 | } // namespace |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5632 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5633 | void MipsABIInfo::CoerceToIntArgs( |
| 5634 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5635 | llvm::IntegerType *IntTy = |
| 5636 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5637 | |
| 5638 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5639 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5640 | ArgList.push_back(IntTy); |
| 5641 | |
| 5642 | // If necessary, add one more integer type to ArgList. |
| 5643 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5644 | |
| 5645 | if (R) |
| 5646 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5647 | } |
| 5648 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5649 | // In N32/64, an aligned double precision floating point field is passed in |
| 5650 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5651 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5652 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5653 | |
| 5654 | if (IsO32) { |
| 5655 | CoerceToIntArgs(TySize, ArgList); |
| 5656 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5657 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5658 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5659 | if (Ty->isComplexType()) |
| 5660 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5661 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5662 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5663 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5664 | // Unions/vectors are passed in integer registers. |
| 5665 | if (!RT || !RT->isStructureOrClassType()) { |
| 5666 | CoerceToIntArgs(TySize, ArgList); |
| 5667 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5668 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5669 | |
| 5670 | const RecordDecl *RD = RT->getDecl(); |
| 5671 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5672 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5673 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5674 | uint64_t LastOffset = 0; |
| 5675 | unsigned idx = 0; |
| 5676 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5677 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5678 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5679 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5680 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5681 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5682 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5683 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5684 | |
| 5685 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5686 | continue; |
| 5687 | |
| 5688 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5689 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5690 | continue; |
| 5691 | |
| 5692 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5693 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5694 | ArgList.push_back(I64); |
| 5695 | |
| 5696 | // Add double type. |
| 5697 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5698 | LastOffset = Offset + 64; |
| 5699 | } |
| 5700 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5701 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5702 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5703 | |
| 5704 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5705 | } |
| 5706 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5707 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5708 | uint64_t Offset) const { |
| 5709 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5710 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5711 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5712 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5713 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5714 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5715 | ABIArgInfo |
| 5716 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5717 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5718 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5719 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5720 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5721 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5722 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5723 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5724 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5725 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5726 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5727 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5728 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5729 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5730 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5731 | return ABIArgInfo::getIgnore(); |
| 5732 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5733 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5734 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5735 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5736 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5737 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5738 | // If we have reached here, aggregates are passed directly by coercing to |
| 5739 | // another structure type. Padding is inserted if the offset of the |
| 5740 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 5741 | ABIArgInfo ArgInfo = |
| 5742 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 5743 | getPaddingType(OrigOffset, CurrOffset)); |
| 5744 | ArgInfo.setInReg(true); |
| 5745 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | // Treat an enum type as its underlying type. |
| 5749 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5750 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5751 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 5752 | // All integral types are promoted to the GPR width. |
| 5753 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5754 | return ABIArgInfo::getExtend(); |
| 5755 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5756 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5757 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5760 | llvm::Type* |
| 5761 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5762 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5763 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5764 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5765 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5766 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5767 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5768 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5769 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5770 | // N32/64 returns struct/classes in floating point registers if the |
| 5771 | // following conditions are met: |
| 5772 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5773 | // 2. The struct/class has one or two fields all of which are floating |
| 5774 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5775 | // 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] | 5776 | // |
| 5777 | // Any other composite results are returned in integer registers. |
| 5778 | // |
| 5779 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5780 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5781 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5782 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5783 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5784 | if (!BT || !BT->isFloatingPoint()) |
| 5785 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5786 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5787 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | if (b == e) |
| 5791 | return llvm::StructType::get(getVMContext(), RTList, |
| 5792 | RD->hasAttr<PackedAttr>()); |
| 5793 | |
| 5794 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5795 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5796 | } |
| 5797 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5798 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5799 | return llvm::StructType::get(getVMContext(), RTList); |
| 5800 | } |
| 5801 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5802 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5803 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5804 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5805 | if (RetTy->isVoidType()) |
| 5806 | return ABIArgInfo::getIgnore(); |
| 5807 | |
| 5808 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5809 | // However, N32/N64 ignores zero sized return values. |
| 5810 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5811 | return ABIArgInfo::getIgnore(); |
| 5812 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5813 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5814 | if (Size <= 128) { |
| 5815 | if (RetTy->isAnyComplexType()) |
| 5816 | return ABIArgInfo::getDirect(); |
| 5817 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5818 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 5819 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5820 | if (!IsO32 || |
| 5821 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 5822 | ABIArgInfo ArgInfo = |
| 5823 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5824 | ArgInfo.setInReg(true); |
| 5825 | return ArgInfo; |
| 5826 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5827 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5828 | |
| 5829 | return ABIArgInfo::getIndirect(0); |
| 5830 | } |
| 5831 | |
| 5832 | // Treat an enum type as its underlying type. |
| 5833 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5834 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5835 | |
| 5836 | return (RetTy->isPromotableIntegerType() ? |
| 5837 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5838 | } |
| 5839 | |
| 5840 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5841 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5842 | if (!getCXXABI().classifyReturnType(FI)) |
| 5843 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5844 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5845 | // 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] | 5846 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5847 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5848 | for (auto &I : FI.arguments()) |
| 5849 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5853 | CodeGenFunction &CGF) const { |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5854 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5855 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5856 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5857 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 5858 | // 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] | 5859 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5860 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
| 5861 | if ((Ty->isIntegerType() && |
| 5862 | CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) || |
| 5863 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5864 | Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 5865 | Ty->isSignedIntegerType()); |
| 5866 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5867 | |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5868 | CGBuilderTy &Builder = CGF.Builder; |
| 5869 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5870 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Daniel Sanders | 8d36a61 | 2014-09-22 13:27:06 +0000 | [diff] [blame] | 5871 | int64_t TypeAlign = |
| 5872 | std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5873 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5874 | llvm::Value *AddrTyped; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5875 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
| 5876 | |
| 5877 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 5878 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5879 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5880 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5881 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
| 5882 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5883 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5884 | } |
| 5885 | else |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5886 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5887 | |
| 5888 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
| 5889 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5890 | unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty); |
| 5891 | uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5892 | llvm::Value *NextAddr = |
| 5893 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
| 5894 | "ap.next"); |
| 5895 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5896 | |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5897 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5898 | } |
| 5899 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5900 | bool MipsABIInfo::shouldSignExtUnsignedType(QualType Ty) const { |
| 5901 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5902 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5903 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 5904 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 5905 | return true; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5906 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 5907 | return false; |
| 5908 | } |
| 5909 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5910 | bool |
| 5911 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5912 | llvm::Value *Address) const { |
| 5913 | // This information comes from gcc's implementation, which seems to |
| 5914 | // as canonical as it gets. |
| 5915 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5916 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5917 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5918 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5919 | |
| 5920 | // 0-31 are the general purpose registers, $0 - $31. |
| 5921 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5922 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5923 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5924 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5925 | |
| 5926 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5927 | // They are one bit wide and ignored here. |
| 5928 | |
| 5929 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5930 | // (coprocessor 1 is the FP unit) |
| 5931 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5932 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5933 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5934 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5935 | return false; |
| 5936 | } |
| 5937 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5938 | //===----------------------------------------------------------------------===// |
| 5939 | // 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] | 5940 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5941 | // handling. |
| 5942 | //===----------------------------------------------------------------------===// |
| 5943 | |
| 5944 | namespace { |
| 5945 | |
| 5946 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5947 | public: |
| 5948 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5949 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5950 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5951 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5952 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5953 | }; |
| 5954 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5955 | void TCETargetCodeGenInfo::setTargetAttributes( |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5956 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5957 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5958 | if (!FD) return; |
| 5959 | |
| 5960 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5961 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5962 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5963 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5964 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5965 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5966 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5967 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5968 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5969 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5970 | llvm::NamedMDNode *OpenCLMetadata = |
| 5971 | M.getModule().getOrInsertNamedMetadata( |
| 5972 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5973 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5974 | SmallVector<llvm::Metadata *, 5> Operands; |
| 5975 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5976 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5977 | Operands.push_back( |
| 5978 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5979 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 5980 | Operands.push_back( |
| 5981 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5982 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 5983 | Operands.push_back( |
| 5984 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5985 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5986 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 5987 | // Add a boolean constant operand for "required" (true) or "hint" |
| 5988 | // (false) for implementing the work_group_size_hint attr later. |
| 5989 | // 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] | 5990 | Operands.push_back( |
| 5991 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5992 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5993 | } |
| 5994 | } |
| 5995 | } |
| 5996 | } |
| 5997 | |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 5998 | } // namespace |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5999 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6000 | //===----------------------------------------------------------------------===// |
| 6001 | // Hexagon ABI Implementation |
| 6002 | //===----------------------------------------------------------------------===// |
| 6003 | |
| 6004 | namespace { |
| 6005 | |
| 6006 | class HexagonABIInfo : public ABIInfo { |
| 6007 | |
| 6008 | |
| 6009 | public: |
| 6010 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6011 | |
| 6012 | private: |
| 6013 | |
| 6014 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6015 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 6016 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6017 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6018 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6019 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6020 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6021 | }; |
| 6022 | |
| 6023 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6024 | public: |
| 6025 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6026 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 6027 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6028 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6029 | return 29; |
| 6030 | } |
| 6031 | }; |
| 6032 | |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 6033 | } // namespace |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6034 | |
| 6035 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6036 | if (!getCXXABI().classifyReturnType(FI)) |
| 6037 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6038 | for (auto &I : FI.arguments()) |
| 6039 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 6043 | if (!isAggregateTypeForABI(Ty)) { |
| 6044 | // Treat an enum type as its underlying type. |
| 6045 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6046 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6047 | |
| 6048 | return (Ty->isPromotableIntegerType() ? |
| 6049 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6050 | } |
| 6051 | |
| 6052 | // Ignore empty records. |
| 6053 | if (isEmptyRecord(getContext(), Ty, true)) |
| 6054 | return ABIArgInfo::getIgnore(); |
| 6055 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6056 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 6057 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6058 | |
| 6059 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6060 | if (Size > 64) |
| 6061 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6062 | // Pass in the smallest viable integer type. |
| 6063 | else if (Size > 32) |
| 6064 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6065 | else if (Size > 16) |
| 6066 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6067 | else if (Size > 8) |
| 6068 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6069 | else |
| 6070 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6071 | } |
| 6072 | |
| 6073 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6074 | if (RetTy->isVoidType()) |
| 6075 | return ABIArgInfo::getIgnore(); |
| 6076 | |
| 6077 | // Large vector types should be returned via memory. |
| 6078 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 6079 | return ABIArgInfo::getIndirect(0); |
| 6080 | |
| 6081 | if (!isAggregateTypeForABI(RetTy)) { |
| 6082 | // Treat an enum type as its underlying type. |
| 6083 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6084 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6085 | |
| 6086 | return (RetTy->isPromotableIntegerType() ? |
| 6087 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6088 | } |
| 6089 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6090 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6091 | return ABIArgInfo::getIgnore(); |
| 6092 | |
| 6093 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6094 | // are returned indirectly. |
| 6095 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6096 | if (Size <= 64) { |
| 6097 | // Return in the smallest viable integer type. |
| 6098 | if (Size <= 8) |
| 6099 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6100 | if (Size <= 16) |
| 6101 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6102 | if (Size <= 32) |
| 6103 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6104 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6105 | } |
| 6106 | |
| 6107 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6108 | } |
| 6109 | |
| 6110 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6111 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6112 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6113 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6114 | |
| 6115 | CGBuilderTy &Builder = CGF.Builder; |
| 6116 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 6117 | "ap"); |
| 6118 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6119 | llvm::Type *PTy = |
| 6120 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 6121 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 6122 | |
| 6123 | uint64_t Offset = |
| 6124 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 6125 | llvm::Value *NextAddr = |
| 6126 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 6127 | "ap.next"); |
| 6128 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 6129 | |
| 6130 | return AddrTyped; |
| 6131 | } |
| 6132 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6133 | //===----------------------------------------------------------------------===// |
| 6134 | // AMDGPU ABI Implementation |
| 6135 | //===----------------------------------------------------------------------===// |
| 6136 | |
| 6137 | namespace { |
| 6138 | |
| 6139 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6140 | public: |
| 6141 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6142 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6143 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6144 | CodeGen::CodeGenModule &M) const override; |
| 6145 | }; |
| 6146 | |
Alexander Kornienko | 3d9d929 | 2015-06-22 09:47:44 +0000 | [diff] [blame] | 6147 | } // namespace |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6148 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6149 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6150 | const Decl *D, |
| 6151 | llvm::GlobalValue *GV, |
| 6152 | CodeGen::CodeGenModule &M) const { |
| 6153 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 6154 | if (!FD) |
| 6155 | return; |
| 6156 | |
| 6157 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6158 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6159 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6160 | if (NumVGPR != 0) |
| 6161 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6162 | } |
| 6163 | |
| 6164 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6165 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6166 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6167 | if (NumSGPR != 0) |
| 6168 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6169 | } |
| 6170 | } |
| 6171 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6172 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6173 | //===----------------------------------------------------------------------===// |
| 6174 | // SPARC v9 ABI Implementation. |
| 6175 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6176 | // |
| 6177 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6178 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6179 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6180 | // |
| 6181 | // One case requires special care: |
| 6182 | // |
| 6183 | // struct mixed { |
| 6184 | // int i; |
| 6185 | // float f; |
| 6186 | // }; |
| 6187 | // |
| 6188 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6189 | // parameter array, but the int is passed in an integer register, and the float |
| 6190 | // is passed in a floating point register. This is represented as two arguments |
| 6191 | // with the LLVM IR inreg attribute: |
| 6192 | // |
| 6193 | // declare void f(i32 inreg %i, float inreg %f) |
| 6194 | // |
| 6195 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6196 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6197 | // bytes. |
| 6198 | // |
| 6199 | namespace { |
| 6200 | class SparcV9ABIInfo : public ABIInfo { |
| 6201 | public: |
| 6202 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6203 | |
| 6204 | private: |
| 6205 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6206 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6207 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6208 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6209 | |
| 6210 | // Coercion type builder for structs passed in registers. The coercion type |
| 6211 | // serves two purposes: |
| 6212 | // |
| 6213 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6214 | // in registers. |
| 6215 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6216 | // code generator knows to pass them in floating point registers. |
| 6217 | // |
| 6218 | // We also compute the InReg flag which indicates that the struct contains |
| 6219 | // aligned 32-bit floats. |
| 6220 | // |
| 6221 | struct CoerceBuilder { |
| 6222 | llvm::LLVMContext &Context; |
| 6223 | const llvm::DataLayout &DL; |
| 6224 | SmallVector<llvm::Type*, 8> Elems; |
| 6225 | uint64_t Size; |
| 6226 | bool InReg; |
| 6227 | |
| 6228 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6229 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6230 | |
| 6231 | // Pad Elems with integers until Size is ToSize. |
| 6232 | void pad(uint64_t ToSize) { |
| 6233 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6234 | if (ToSize == Size) |
| 6235 | return; |
| 6236 | |
| 6237 | // Finish the current 64-bit word. |
| 6238 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6239 | if (Aligned > Size && Aligned <= ToSize) { |
| 6240 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6241 | Size = Aligned; |
| 6242 | } |
| 6243 | |
| 6244 | // Add whole 64-bit words. |
| 6245 | while (Size + 64 <= ToSize) { |
| 6246 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6247 | Size += 64; |
| 6248 | } |
| 6249 | |
| 6250 | // Final in-word padding. |
| 6251 | if (Size < ToSize) { |
| 6252 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6253 | Size = ToSize; |
| 6254 | } |
| 6255 | } |
| 6256 | |
| 6257 | // Add a floating point element at Offset. |
| 6258 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6259 | // Unaligned floats are treated as integers. |
| 6260 | if (Offset % Bits) |
| 6261 | return; |
| 6262 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6263 | if (Bits < 64) |
| 6264 | InReg = true; |
| 6265 | pad(Offset); |
| 6266 | Elems.push_back(Ty); |
| 6267 | Size = Offset + Bits; |
| 6268 | } |
| 6269 | |
| 6270 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6271 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6272 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6273 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6274 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6275 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6276 | switch (ElemTy->getTypeID()) { |
| 6277 | case llvm::Type::StructTyID: |
| 6278 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6279 | break; |
| 6280 | case llvm::Type::FloatTyID: |
| 6281 | addFloat(ElemOffset, ElemTy, 32); |
| 6282 | break; |
| 6283 | case llvm::Type::DoubleTyID: |
| 6284 | addFloat(ElemOffset, ElemTy, 64); |
| 6285 | break; |
| 6286 | case llvm::Type::FP128TyID: |
| 6287 | addFloat(ElemOffset, ElemTy, 128); |
| 6288 | break; |
| 6289 | case llvm::Type::PointerTyID: |
| 6290 | if (ElemOffset % 64 == 0) { |
| 6291 | pad(ElemOffset); |
| 6292 | Elems.push_back(ElemTy); |
| 6293 | Size += 64; |
| 6294 | } |
| 6295 | break; |
| 6296 | default: |
| 6297 | break; |
| 6298 | } |
| 6299 | } |
| 6300 | } |
| 6301 | |
| 6302 | // Check if Ty is a usable substitute for the coercion type. |
| 6303 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6304 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
| 6307 | // Get the coercion type as a literal struct type. |
| 6308 | llvm::Type *getType() const { |
| 6309 | if (Elems.size() == 1) |
| 6310 | return Elems.front(); |
| 6311 | else |
| 6312 | return llvm::StructType::get(Context, Elems); |
| 6313 | } |
| 6314 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6315 | }; |
| 6316 | } // end anonymous namespace |
| 6317 | |
| 6318 | ABIArgInfo |
| 6319 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6320 | if (Ty->isVoidType()) |
| 6321 | return ABIArgInfo::getIgnore(); |
| 6322 | |
| 6323 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6324 | |
| 6325 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6326 | // pointer / sret pointer. |
| 6327 | if (Size > SizeLimit) |
| 6328 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 6329 | |
| 6330 | // Treat an enum type as its underlying type. |
| 6331 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6332 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6333 | |
| 6334 | // Integer types smaller than a register are extended. |
| 6335 | if (Size < 64 && Ty->isIntegerType()) |
| 6336 | return ABIArgInfo::getExtend(); |
| 6337 | |
| 6338 | // Other non-aggregates go in registers. |
| 6339 | if (!isAggregateTypeForABI(Ty)) |
| 6340 | return ABIArgInfo::getDirect(); |
| 6341 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6342 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6343 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6344 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 6345 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 6346 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6347 | // 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] | 6348 | // Build a coercion type from the LLVM struct type. |
| 6349 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6350 | if (!StrTy) |
| 6351 | return ABIArgInfo::getDirect(); |
| 6352 | |
| 6353 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6354 | CB.addStruct(0, StrTy); |
| 6355 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6356 | |
| 6357 | // Try to use the original type for coercion. |
| 6358 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6359 | |
| 6360 | if (CB.InReg) |
| 6361 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6362 | else |
| 6363 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6364 | } |
| 6365 | |
| 6366 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6367 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6368 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6369 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6370 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6371 | AI.setCoerceToType(ArgTy); |
| 6372 | |
| 6373 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6374 | CGBuilderTy &Builder = CGF.Builder; |
| 6375 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6376 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6377 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6378 | llvm::Value *ArgAddr; |
| 6379 | unsigned Stride; |
| 6380 | |
| 6381 | switch (AI.getKind()) { |
| 6382 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6383 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6384 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6385 | |
| 6386 | case ABIArgInfo::Extend: |
| 6387 | Stride = 8; |
| 6388 | ArgAddr = Builder |
| 6389 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6390 | "extend"); |
| 6391 | break; |
| 6392 | |
| 6393 | case ABIArgInfo::Direct: |
| 6394 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6395 | ArgAddr = Addr; |
| 6396 | break; |
| 6397 | |
| 6398 | case ABIArgInfo::Indirect: |
| 6399 | Stride = 8; |
| 6400 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6401 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6402 | "indirect"); |
| 6403 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6404 | break; |
| 6405 | |
| 6406 | case ABIArgInfo::Ignore: |
| 6407 | return llvm::UndefValue::get(ArgPtrTy); |
| 6408 | } |
| 6409 | |
| 6410 | // Update VAList. |
| 6411 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6412 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6413 | |
| 6414 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6418 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6419 | for (auto &I : FI.arguments()) |
| 6420 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | namespace { |
| 6424 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6425 | public: |
| 6426 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6427 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6428 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6429 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6430 | return 14; |
| 6431 | } |
| 6432 | |
| 6433 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6434 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6435 | }; |
| 6436 | } // end anonymous namespace |
| 6437 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6438 | bool |
| 6439 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6440 | llvm::Value *Address) const { |
| 6441 | // This is calculated from the LLVM and GCC tables and verified |
| 6442 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6443 | |
| 6444 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6445 | |
| 6446 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6447 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6448 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6449 | |
| 6450 | // 0-31: the 8-byte general-purpose registers |
| 6451 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6452 | |
| 6453 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6454 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6455 | |
| 6456 | // Y = 64 |
| 6457 | // PSR = 65 |
| 6458 | // WIM = 66 |
| 6459 | // TBR = 67 |
| 6460 | // PC = 68 |
| 6461 | // NPC = 69 |
| 6462 | // FSR = 70 |
| 6463 | // CSR = 71 |
| 6464 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6465 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6466 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6467 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6468 | |
| 6469 | return false; |
| 6470 | } |
| 6471 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6472 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6473 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6474 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6475 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6476 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6477 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6478 | |
| 6479 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6480 | /// it by reference between functions that append to it. |
| 6481 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6482 | |
| 6483 | /// TypeStringCache caches the meta encodings of Types. |
| 6484 | /// |
| 6485 | /// The reason for caching TypeStrings is two fold: |
| 6486 | /// 1. To cache a type's encoding for later uses; |
| 6487 | /// 2. As a means to break recursive member type inclusion. |
| 6488 | /// |
| 6489 | /// A cache Entry can have a Status of: |
| 6490 | /// NonRecursive: The type encoding is not recursive; |
| 6491 | /// Recursive: The type encoding is recursive; |
| 6492 | /// Incomplete: An incomplete TypeString; |
| 6493 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6494 | /// Recursive type encoding. |
| 6495 | /// |
| 6496 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6497 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6498 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6499 | /// the type is encountered. |
| 6500 | /// |
| 6501 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6502 | /// possible. The type itself is recursive and it may contain other types which |
| 6503 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6504 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6505 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6506 | /// |
| 6507 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6508 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6509 | /// are placed into the cache during type expansion as a means to identify and |
| 6510 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6511 | /// the entry becomes IncompleteUsed. |
| 6512 | /// |
| 6513 | /// During the expansion of a RecordType's members: |
| 6514 | /// |
| 6515 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6516 | /// cached encoding is used; |
| 6517 | /// |
| 6518 | /// If the cache contains a Recursive encoding for the member type, the |
| 6519 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6520 | /// |
| 6521 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6522 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6523 | /// |
| 6524 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6525 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6526 | /// it is swapped back in; |
| 6527 | /// |
| 6528 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6529 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6530 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6531 | /// |
| 6532 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6533 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6534 | /// Else the member is part of a recursive type and thus the recursion has |
| 6535 | /// been exited too soon for the encoding to be correct for the member. |
| 6536 | /// |
| 6537 | class TypeStringCache { |
| 6538 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6539 | struct Entry { |
| 6540 | std::string Str; // The encoded TypeString for the type. |
| 6541 | enum Status State; // Information about the encoding in 'Str'. |
| 6542 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6543 | // during the expansion of RecordType's members. |
| 6544 | }; |
| 6545 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6546 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6547 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6548 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6549 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6550 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6551 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6552 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6553 | bool IsRecursive); |
| 6554 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6555 | }; |
| 6556 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6557 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6558 | /// FieldEncoding is a helper for this ordering process. |
| 6559 | class FieldEncoding { |
| 6560 | bool HasName; |
| 6561 | std::string Enc; |
| 6562 | public: |
| 6563 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6564 | StringRef str() {return Enc.c_str();}; |
| 6565 | bool operator<(const FieldEncoding &rhs) const { |
| 6566 | if (HasName != rhs.HasName) return HasName; |
| 6567 | return Enc < rhs.Enc; |
| 6568 | } |
| 6569 | }; |
| 6570 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6571 | class XCoreABIInfo : public DefaultABIInfo { |
| 6572 | public: |
| 6573 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6574 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6575 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6576 | }; |
| 6577 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6578 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6579 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6580 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6581 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6582 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6583 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6584 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6585 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6586 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6587 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6588 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6589 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6590 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6591 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6592 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6593 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6594 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6595 | CGF.Int8PtrPtrTy); |
| 6596 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6597 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6598 | // Handle the argument. |
| 6599 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6600 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6601 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6602 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6603 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6604 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6605 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6606 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6607 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6608 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6609 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6610 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6611 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6612 | ArgSize = 0; |
| 6613 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6614 | case ABIArgInfo::Extend: |
| 6615 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6616 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6617 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6618 | if (ArgSize < 4) |
| 6619 | ArgSize = 4; |
| 6620 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6621 | case ABIArgInfo::Indirect: |
| 6622 | llvm::Value *ArgAddr; |
| 6623 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6624 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6625 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6626 | ArgSize = 4; |
| 6627 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6628 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6629 | |
| 6630 | // Increment the VAList. |
| 6631 | if (ArgSize) { |
| 6632 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6633 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6634 | } |
| 6635 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6636 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6637 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6638 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6639 | /// into the cache as a means to identify and break recursion. |
| 6640 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6641 | /// be reinserted by removeIncomplete(). |
| 6642 | /// All other types of encoding should have been used rather than arriving here. |
| 6643 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6644 | std::string StubEnc) { |
| 6645 | if (!ID) |
| 6646 | return; |
| 6647 | Entry &E = Map[ID]; |
| 6648 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6649 | "Incorrectly use of addIncomplete"); |
| 6650 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6651 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6652 | E.Str.swap(StubEnc); |
| 6653 | E.State = Incomplete; |
| 6654 | ++IncompleteCount; |
| 6655 | } |
| 6656 | |
| 6657 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6658 | /// must be removed from the cache. |
| 6659 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6660 | /// Returns true if the RecordType was defined recursively. |
| 6661 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6662 | if (!ID) |
| 6663 | return false; |
| 6664 | auto I = Map.find(ID); |
| 6665 | assert(I != Map.end() && "Entry not present"); |
| 6666 | Entry &E = I->second; |
| 6667 | assert( (E.State == Incomplete || |
| 6668 | E.State == IncompleteUsed) && |
| 6669 | "Entry must be an incomplete type"); |
| 6670 | bool IsRecursive = false; |
| 6671 | if (E.State == IncompleteUsed) { |
| 6672 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6673 | IsRecursive = true; |
| 6674 | --IncompleteUsedCount; |
| 6675 | } |
| 6676 | if (E.Swapped.empty()) |
| 6677 | Map.erase(I); |
| 6678 | else { |
| 6679 | // Swap the Recursive back. |
| 6680 | E.Swapped.swap(E.Str); |
| 6681 | E.Swapped.clear(); |
| 6682 | E.State = Recursive; |
| 6683 | } |
| 6684 | --IncompleteCount; |
| 6685 | return IsRecursive; |
| 6686 | } |
| 6687 | |
| 6688 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6689 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6690 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6691 | bool IsRecursive) { |
| 6692 | if (!ID || IncompleteUsedCount) |
| 6693 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6694 | Entry &E = Map[ID]; |
| 6695 | if (IsRecursive && !E.Str.empty()) { |
| 6696 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6697 | "This is not the same Recursive entry"); |
| 6698 | // The parent container was not recursive after all, so we could have used |
| 6699 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6700 | // we started viz: IncompleteCount!=0. |
| 6701 | return; |
| 6702 | } |
| 6703 | assert(E.Str.empty() && "Entry already present"); |
| 6704 | E.Str = Str.str(); |
| 6705 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6706 | } |
| 6707 | |
| 6708 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6709 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6710 | /// encoding is Recursive, return an empty StringRef. |
| 6711 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6712 | if (!ID) |
| 6713 | return StringRef(); // We have no key. |
| 6714 | auto I = Map.find(ID); |
| 6715 | if (I == Map.end()) |
| 6716 | return StringRef(); // We have no encoding. |
| 6717 | Entry &E = I->second; |
| 6718 | if (E.State == Recursive && IncompleteCount) |
| 6719 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6720 | |
| 6721 | if (E.State == Incomplete) { |
| 6722 | // The incomplete type is being used to break out of recursion. |
| 6723 | E.State = IncompleteUsed; |
| 6724 | ++IncompleteUsedCount; |
| 6725 | } |
| 6726 | return E.Str.c_str(); |
| 6727 | } |
| 6728 | |
| 6729 | /// The XCore ABI includes a type information section that communicates symbol |
| 6730 | /// type information to the linker. The linker uses this information to verify |
| 6731 | /// safety/correctness of things such as array bound and pointers et al. |
| 6732 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6733 | /// This type information (TypeString) is emitted into meta data for all global |
| 6734 | /// symbols: definitions, declarations, functions & variables. |
| 6735 | /// |
| 6736 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6737 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6738 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6739 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6740 | /// |
| 6741 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6742 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6743 | |
| 6744 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6745 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6746 | CodeGen::CodeGenModule &CGM) const { |
| 6747 | SmallStringEnc Enc; |
| 6748 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6749 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6750 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 6751 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6752 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6753 | llvm::NamedMDNode *MD = |
| 6754 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6755 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6756 | } |
| 6757 | } |
| 6758 | |
| 6759 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6760 | const CodeGen::CodeGenModule &CGM, |
| 6761 | TypeStringCache &TSC); |
| 6762 | |
| 6763 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 6764 | /// Builds a SmallVector containing the encoded field types in declaration |
| 6765 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6766 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6767 | const RecordDecl *RD, |
| 6768 | const CodeGen::CodeGenModule &CGM, |
| 6769 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6770 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6771 | SmallStringEnc Enc; |
| 6772 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6773 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6774 | Enc += "){"; |
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 += "b("; |
| 6777 | llvm::raw_svector_ostream OS(Enc); |
| 6778 | OS.resync(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6779 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6780 | OS.flush(); |
| 6781 | Enc += ':'; |
| 6782 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6783 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6784 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6785 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6786 | Enc += ')'; |
| 6787 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 6788 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6789 | } |
| 6790 | return true; |
| 6791 | } |
| 6792 | |
| 6793 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6794 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6795 | /// Union types have their fields ordered according to the ABI. |
| 6796 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6797 | const CodeGen::CodeGenModule &CGM, |
| 6798 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6799 | // Append the cached TypeString if we have one. |
| 6800 | StringRef TypeString = TSC.lookupStr(ID); |
| 6801 | if (!TypeString.empty()) { |
| 6802 | Enc += TypeString; |
| 6803 | return true; |
| 6804 | } |
| 6805 | |
| 6806 | // Start to emit an incomplete TypeString. |
| 6807 | size_t Start = Enc.size(); |
| 6808 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6809 | Enc += '('; |
| 6810 | if (ID) |
| 6811 | Enc += ID->getName(); |
| 6812 | Enc += "){"; |
| 6813 | |
| 6814 | // We collect all encoded fields and order as necessary. |
| 6815 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6816 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6817 | if (RD && !RD->field_empty()) { |
| 6818 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6819 | // so that recursive calls to this RecordType will use it whilst building a |
| 6820 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6821 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6822 | std::string StubEnc(Enc.substr(Start).str()); |
| 6823 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6824 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6825 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6826 | (void) TSC.removeIncomplete(ID); |
| 6827 | return false; |
| 6828 | } |
| 6829 | IsRecursive = TSC.removeIncomplete(ID); |
| 6830 | // The ABI requires unions to be sorted but not structures. |
| 6831 | // See FieldEncoding::operator< for sort algorithm. |
| 6832 | if (RT->isUnionType()) |
| 6833 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6834 | // We can now complete the TypeString. |
| 6835 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6836 | for (unsigned I = 0; I != E; ++I) { |
| 6837 | if (I) |
| 6838 | Enc += ','; |
| 6839 | Enc += FE[I].str(); |
| 6840 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6841 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6842 | Enc += '}'; |
| 6843 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6844 | return true; |
| 6845 | } |
| 6846 | |
| 6847 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6848 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6849 | TypeStringCache &TSC, |
| 6850 | const IdentifierInfo *ID) { |
| 6851 | // Append the cached TypeString if we have one. |
| 6852 | StringRef TypeString = TSC.lookupStr(ID); |
| 6853 | if (!TypeString.empty()) { |
| 6854 | Enc += TypeString; |
| 6855 | return true; |
| 6856 | } |
| 6857 | |
| 6858 | size_t Start = Enc.size(); |
| 6859 | Enc += "e("; |
| 6860 | if (ID) |
| 6861 | Enc += ID->getName(); |
| 6862 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6863 | |
| 6864 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6865 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6866 | SmallVector<FieldEncoding, 16> FE; |
| 6867 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6868 | ++I) { |
| 6869 | SmallStringEnc EnumEnc; |
| 6870 | EnumEnc += "m("; |
| 6871 | EnumEnc += I->getName(); |
| 6872 | EnumEnc += "){"; |
| 6873 | I->getInitVal().toString(EnumEnc); |
| 6874 | EnumEnc += '}'; |
| 6875 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6876 | } |
| 6877 | std::sort(FE.begin(), FE.end()); |
| 6878 | unsigned E = FE.size(); |
| 6879 | for (unsigned I = 0; I != E; ++I) { |
| 6880 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6881 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6882 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6883 | } |
| 6884 | } |
| 6885 | Enc += '}'; |
| 6886 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6887 | return true; |
| 6888 | } |
| 6889 | |
| 6890 | /// Appends type's qualifier to Enc. |
| 6891 | /// This is done prior to appending the type's encoding. |
| 6892 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6893 | // Qualifiers are emitted in alphabetical order. |
| 6894 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6895 | int Lookup = 0; |
| 6896 | if (QT.isConstQualified()) |
| 6897 | Lookup += 1<<0; |
| 6898 | if (QT.isRestrictQualified()) |
| 6899 | Lookup += 1<<1; |
| 6900 | if (QT.isVolatileQualified()) |
| 6901 | Lookup += 1<<2; |
| 6902 | Enc += Table[Lookup]; |
| 6903 | } |
| 6904 | |
| 6905 | /// Appends built-in types to Enc. |
| 6906 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6907 | const char *EncType; |
| 6908 | switch (BT->getKind()) { |
| 6909 | case BuiltinType::Void: |
| 6910 | EncType = "0"; |
| 6911 | break; |
| 6912 | case BuiltinType::Bool: |
| 6913 | EncType = "b"; |
| 6914 | break; |
| 6915 | case BuiltinType::Char_U: |
| 6916 | EncType = "uc"; |
| 6917 | break; |
| 6918 | case BuiltinType::UChar: |
| 6919 | EncType = "uc"; |
| 6920 | break; |
| 6921 | case BuiltinType::SChar: |
| 6922 | EncType = "sc"; |
| 6923 | break; |
| 6924 | case BuiltinType::UShort: |
| 6925 | EncType = "us"; |
| 6926 | break; |
| 6927 | case BuiltinType::Short: |
| 6928 | EncType = "ss"; |
| 6929 | break; |
| 6930 | case BuiltinType::UInt: |
| 6931 | EncType = "ui"; |
| 6932 | break; |
| 6933 | case BuiltinType::Int: |
| 6934 | EncType = "si"; |
| 6935 | break; |
| 6936 | case BuiltinType::ULong: |
| 6937 | EncType = "ul"; |
| 6938 | break; |
| 6939 | case BuiltinType::Long: |
| 6940 | EncType = "sl"; |
| 6941 | break; |
| 6942 | case BuiltinType::ULongLong: |
| 6943 | EncType = "ull"; |
| 6944 | break; |
| 6945 | case BuiltinType::LongLong: |
| 6946 | EncType = "sll"; |
| 6947 | break; |
| 6948 | case BuiltinType::Float: |
| 6949 | EncType = "ft"; |
| 6950 | break; |
| 6951 | case BuiltinType::Double: |
| 6952 | EncType = "d"; |
| 6953 | break; |
| 6954 | case BuiltinType::LongDouble: |
| 6955 | EncType = "ld"; |
| 6956 | break; |
| 6957 | default: |
| 6958 | return false; |
| 6959 | } |
| 6960 | Enc += EncType; |
| 6961 | return true; |
| 6962 | } |
| 6963 | |
| 6964 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6965 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6966 | const CodeGen::CodeGenModule &CGM, |
| 6967 | TypeStringCache &TSC) { |
| 6968 | Enc += "p("; |
| 6969 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6970 | return false; |
| 6971 | Enc += ')'; |
| 6972 | return true; |
| 6973 | } |
| 6974 | |
| 6975 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6976 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6977 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6978 | const CodeGen::CodeGenModule &CGM, |
| 6979 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6980 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6981 | return false; |
| 6982 | Enc += "a("; |
| 6983 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6984 | CAT->getSize().toStringUnsigned(Enc); |
| 6985 | else |
| 6986 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6987 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6988 | // The Qualifiers should be attached to the type rather than the array. |
| 6989 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6990 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6991 | return false; |
| 6992 | Enc += ')'; |
| 6993 | return true; |
| 6994 | } |
| 6995 | |
| 6996 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6997 | /// and the arguments. |
| 6998 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6999 | const CodeGen::CodeGenModule &CGM, |
| 7000 | TypeStringCache &TSC) { |
| 7001 | Enc += "f{"; |
| 7002 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 7003 | return false; |
| 7004 | Enc += "}("; |
| 7005 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 7006 | // N.B. we are only interested in the adjusted param types. |
| 7007 | auto I = FPT->param_type_begin(); |
| 7008 | auto E = FPT->param_type_end(); |
| 7009 | if (I != E) { |
| 7010 | do { |
| 7011 | if (!appendType(Enc, *I, CGM, TSC)) |
| 7012 | return false; |
| 7013 | ++I; |
| 7014 | if (I != E) |
| 7015 | Enc += ','; |
| 7016 | } while (I != E); |
| 7017 | if (FPT->isVariadic()) |
| 7018 | Enc += ",va"; |
| 7019 | } else { |
| 7020 | if (FPT->isVariadic()) |
| 7021 | Enc += "va"; |
| 7022 | else |
| 7023 | Enc += '0'; |
| 7024 | } |
| 7025 | } |
| 7026 | Enc += ')'; |
| 7027 | return true; |
| 7028 | } |
| 7029 | |
| 7030 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 7031 | /// type encodings. |
| 7032 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 7033 | const CodeGen::CodeGenModule &CGM, |
| 7034 | TypeStringCache &TSC) { |
| 7035 | |
| 7036 | QualType QT = QType.getCanonicalType(); |
| 7037 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7038 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 7039 | // The Qualifiers should be attached to the type rather than the array. |
| 7040 | // Thus we don't call appendQualifier() here. |
| 7041 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 7042 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7043 | appendQualifier(Enc, QT); |
| 7044 | |
| 7045 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 7046 | return appendBuiltinType(Enc, BT); |
| 7047 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7048 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 7049 | return appendPointerType(Enc, PT, CGM, TSC); |
| 7050 | |
| 7051 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 7052 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 7053 | |
| 7054 | if (const RecordType *RT = QT->getAsStructureType()) |
| 7055 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7056 | |
| 7057 | if (const RecordType *RT = QT->getAsUnionType()) |
| 7058 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7059 | |
| 7060 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7061 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7062 | |
| 7063 | return false; |
| 7064 | } |
| 7065 | |
| 7066 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7067 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7068 | if (!D) |
| 7069 | return false; |
| 7070 | |
| 7071 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7072 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7073 | return false; |
| 7074 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7075 | } |
| 7076 | |
| 7077 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7078 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7079 | return false; |
| 7080 | QualType QT = VD->getType().getCanonicalType(); |
| 7081 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7082 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7083 | // The Qualifiers should be attached to the type rather than the array. |
| 7084 | // Thus we don't call appendQualifier() here. |
| 7085 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7086 | } |
| 7087 | return appendType(Enc, QT, CGM, TSC); |
| 7088 | } |
| 7089 | return false; |
| 7090 | } |
| 7091 | |
| 7092 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7093 | //===----------------------------------------------------------------------===// |
| 7094 | // Driver code |
| 7095 | //===----------------------------------------------------------------------===// |
| 7096 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7097 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7098 | return getTarget().getTriple(); |
| 7099 | } |
| 7100 | |
| 7101 | bool CodeGenModule::supportsCOMDAT() const { |
| 7102 | return !getTriple().isOSBinFormatMachO(); |
| 7103 | } |
| 7104 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7105 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7106 | if (TheTargetCodeGenInfo) |
| 7107 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7108 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7109 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7110 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7111 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7112 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7113 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7114 | case llvm::Triple::le32: |
| 7115 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7116 | case llvm::Triple::mips: |
| 7117 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7118 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7119 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7120 | case llvm::Triple::mips64: |
| 7121 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7122 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7123 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7124 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7125 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7126 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7127 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7128 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7129 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7130 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7131 | } |
| 7132 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7133 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7134 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7135 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7136 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7137 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7138 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7139 | TheTargetCodeGenInfo = |
| 7140 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7141 | return *TheTargetCodeGenInfo; |
| 7142 | } |
| 7143 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7144 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7145 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7146 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7147 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7148 | (CodeGenOpts.FloatABI != "soft" && |
| 7149 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7150 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7151 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7152 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7153 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7154 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7155 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7156 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7157 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7158 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7159 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7160 | if (getTarget().getABI() == "elfv2") |
| 7161 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7162 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7163 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7164 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7165 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7166 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7167 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7168 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7169 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7170 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7171 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7172 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7173 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7174 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7175 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7176 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7177 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7178 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7179 | case llvm::Triple::nvptx: |
| 7180 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7181 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7182 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7183 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7184 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7185 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7186 | case llvm::Triple::systemz: { |
| 7187 | bool HasVector = getTarget().getABI() == "vector"; |
| 7188 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7189 | HasVector)); |
| 7190 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7191 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7192 | case llvm::Triple::tce: |
| 7193 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7194 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7195 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7196 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 7197 | bool IsSmallStructInRegABI = |
| 7198 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7199 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7200 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7201 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7202 | return *(TheTargetCodeGenInfo = new WinX86_32TargetCodeGenInfo( |
| 7203 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7204 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7205 | } else { |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7206 | return *(TheTargetCodeGenInfo = new X86_32TargetCodeGenInfo( |
| 7207 | Types, IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7208 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7209 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7210 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7211 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7212 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 7213 | StringRef ABI = getTarget().getABI(); |
| 7214 | X86AVXABILevel AVXLevel = (ABI == "avx" ? X86AVXABILevel::AVX : |
| 7215 | X86AVXABILevel::None); |
| 7216 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7217 | switch (Triple.getOS()) { |
| 7218 | case llvm::Triple::Win32: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 7219 | return *(TheTargetCodeGenInfo = |
| 7220 | new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7221 | case llvm::Triple::PS4: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 7222 | return *(TheTargetCodeGenInfo = |
| 7223 | new PS4TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7224 | default: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame^] | 7225 | return *(TheTargetCodeGenInfo = |
| 7226 | new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7227 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7228 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7229 | case llvm::Triple::hexagon: |
| 7230 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7231 | case llvm::Triple::r600: |
| 7232 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7233 | case llvm::Triple::amdgcn: |
| 7234 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7235 | case llvm::Triple::sparcv9: |
| 7236 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7237 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7238 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7239 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7240 | } |