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 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 111 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 112 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 113 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 114 | switch (TheKind) { |
| 115 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 116 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 117 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 118 | Ty->print(OS); |
| 119 | else |
| 120 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 121 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 122 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 123 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 124 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 125 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 126 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 127 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 128 | case InAlloca: |
| 129 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 130 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 131 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 132 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 133 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 134 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 135 | break; |
| 136 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 137 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 140 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 143 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 144 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 145 | // If someone can figure out a general rule for this, that would be great. |
| 146 | // It's probably just doomed to be platform-dependent, though. |
| 147 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 148 | // Verified for: |
| 149 | // x86-64 FreeBSD, Linux, Darwin |
| 150 | // x86-32 FreeBSD, Linux, Darwin |
| 151 | // PowerPC Linux, Darwin |
| 152 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 153 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 154 | return 32; |
| 155 | } |
| 156 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 157 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 158 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 159 | // The following conventions are known to require this to be false: |
| 160 | // x86_stdcall |
| 161 | // MIPS |
| 162 | // For everything else, we just prefer false unless we opt out. |
| 163 | return false; |
| 164 | } |
| 165 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 166 | void |
| 167 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 168 | llvm::SmallString<24> &Opt) const { |
| 169 | // This assumes the user is passing a library name like "rt" instead of a |
| 170 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 171 | // dynamic. |
| 172 | Opt = "-l"; |
| 173 | Opt += Lib; |
| 174 | } |
| 175 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 176 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 177 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 178 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 179 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 180 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 181 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 182 | if (FD->isUnnamedBitfield()) |
| 183 | return true; |
| 184 | |
| 185 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 186 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 187 | // Constant arrays of empty records count as empty, strip them off. |
| 188 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 189 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 190 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 191 | if (AT->getSize() == 0) |
| 192 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 193 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 194 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 195 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 196 | const RecordType *RT = FT->getAs<RecordType>(); |
| 197 | if (!RT) |
| 198 | return false; |
| 199 | |
| 200 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 201 | // |
| 202 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 203 | // current ABI. |
| 204 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 205 | return false; |
| 206 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 207 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 210 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 211 | /// fields. Note that a structure with a flexible array member is not |
| 212 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 213 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 214 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 215 | if (!RT) |
| 216 | return 0; |
| 217 | const RecordDecl *RD = RT->getDecl(); |
| 218 | if (RD->hasFlexibleArrayMember()) |
| 219 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 220 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 221 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 222 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 223 | for (const auto &I : CXXRD->bases()) |
| 224 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 225 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 226 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 227 | for (const auto *I : RD->fields()) |
| 228 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 229 | return false; |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | /// isSingleElementStruct - Determine if a structure is a "single |
| 234 | /// element struct", i.e. it has exactly one non-empty field or |
| 235 | /// exactly one field which is itself a single element |
| 236 | /// struct. Structures with flexible array members are never |
| 237 | /// considered single element structs. |
| 238 | /// |
| 239 | /// \return The field declaration for the single non-empty field, if |
| 240 | /// it exists. |
| 241 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 242 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 243 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 244 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 245 | |
| 246 | const RecordDecl *RD = RT->getDecl(); |
| 247 | if (RD->hasFlexibleArrayMember()) |
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 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 250 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 251 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 252 | // If this is a C++ record, check the bases first. |
| 253 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 254 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 255 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 256 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 257 | continue; |
| 258 | |
| 259 | // If we already found an element then this isn't a single-element struct. |
| 260 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 261 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 262 | |
| 263 | // If this is non-empty and not a single element struct, the composite |
| 264 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 265 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 266 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 267 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 272 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 273 | QualType FT = FD->getType(); |
| 274 | |
| 275 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 276 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 277 | continue; |
| 278 | |
| 279 | // If we already found an element then this isn't a single-element |
| 280 | // struct. |
| 281 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 282 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 283 | |
| 284 | // Treat single element arrays as the element. |
| 285 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 286 | if (AT->getSize().getZExtValue() != 1) |
| 287 | break; |
| 288 | FT = AT->getElementType(); |
| 289 | } |
| 290 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 291 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 292 | Found = FT.getTypePtr(); |
| 293 | } else { |
| 294 | Found = isSingleElementStruct(FT, Context); |
| 295 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 296 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 300 | // We don't consider a struct a single-element struct if it has |
| 301 | // padding beyond the element type. |
| 302 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 303 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 304 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 305 | return Found; |
| 306 | } |
| 307 | |
| 308 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 309 | // Treat complex types as the element type. |
| 310 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 311 | Ty = CTy->getElementType(); |
| 312 | |
| 313 | // Check for a type which we know has a simple scalar argument-passing |
| 314 | // convention without any padding. (We're specifically looking for 32 |
| 315 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 316 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 317 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 318 | return false; |
| 319 | |
| 320 | uint64_t Size = Context.getTypeSize(Ty); |
| 321 | return Size == 32 || Size == 64; |
| 322 | } |
| 323 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 324 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 325 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 326 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 327 | /// inhibiting optimizations. |
| 328 | /// |
| 329 | // FIXME: This predicate is missing many cases, currently it just follows |
| 330 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 331 | // should probably make this smarter, or better yet make the LLVM backend |
| 332 | // capable of handling it. |
| 333 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 334 | // We can only expand structure types. |
| 335 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 336 | if (!RT) |
| 337 | return false; |
| 338 | |
| 339 | // We can only expand (C) structures. |
| 340 | // |
| 341 | // FIXME: This needs to be generalized to handle classes as well. |
| 342 | const RecordDecl *RD = RT->getDecl(); |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 343 | if (!RD->isStruct()) |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 344 | return false; |
| 345 | |
Manman Ren | 2738278 | 2015-04-03 18:10:29 +0000 | [diff] [blame] | 346 | // We try to expand CLike CXXRecordDecl. |
| 347 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 348 | if (!CXXRD->isCLike()) |
| 349 | return false; |
| 350 | } |
| 351 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 352 | uint64_t Size = 0; |
| 353 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 354 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 355 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 356 | return false; |
| 357 | |
| 358 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 359 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 360 | // counts as "basic" is more complicated than what we were doing previously. |
| 361 | if (FD->isBitField()) |
| 362 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 363 | |
| 364 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 367 | // Make sure there are not any holes in the struct. |
| 368 | if (Size != Context.getTypeSize(Ty)) |
| 369 | return false; |
| 370 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 371 | return true; |
| 372 | } |
| 373 | |
| 374 | namespace { |
| 375 | /// DefaultABIInfo - The default implementation for ABI specific |
| 376 | /// details. This implementation provides information which results in |
| 377 | /// self-consistent and sensible LLVM IR generation, but does not |
| 378 | /// conform to any particular ABI. |
| 379 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 380 | public: |
| 381 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 383 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 384 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 385 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 386 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 387 | if (!getCXXABI().classifyReturnType(FI)) |
| 388 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 389 | for (auto &I : FI.arguments()) |
| 390 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 393 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 394 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 395 | }; |
| 396 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 397 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 398 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 399 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 400 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 401 | }; |
| 402 | |
| 403 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 404 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 405 | return nullptr; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 408 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 409 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 410 | |
| 411 | if (isAggregateTypeForABI(Ty)) { |
| 412 | // Records with non-trivial destructors/copy-constructors should not be |
| 413 | // passed by value. |
| 414 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 415 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 416 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 417 | return ABIArgInfo::getIndirect(0); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 418 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 419 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 420 | // Treat an enum type as its underlying type. |
| 421 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 422 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 424 | return (Ty->isPromotableIntegerType() ? |
| 425 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 428 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 429 | if (RetTy->isVoidType()) |
| 430 | return ABIArgInfo::getIgnore(); |
| 431 | |
| 432 | if (isAggregateTypeForABI(RetTy)) |
| 433 | return ABIArgInfo::getIndirect(0); |
| 434 | |
| 435 | // Treat an enum type as its underlying type. |
| 436 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 437 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 438 | |
| 439 | return (RetTy->isPromotableIntegerType() ? |
| 440 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 441 | } |
| 442 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 443 | //===----------------------------------------------------------------------===// |
| 444 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 445 | // |
| 446 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 447 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 448 | //===----------------------------------------------------------------------===// |
| 449 | |
| 450 | class PNaClABIInfo : public ABIInfo { |
| 451 | public: |
| 452 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 453 | |
| 454 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 455 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 456 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 457 | void computeInfo(CGFunctionInfo &FI) const override; |
| 458 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 459 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 463 | public: |
| 464 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 465 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 466 | }; |
| 467 | |
| 468 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 469 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 470 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 471 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 472 | for (auto &I : FI.arguments()) |
| 473 | I.info = classifyArgumentType(I.type); |
| 474 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 475 | |
| 476 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 477 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 478 | return nullptr; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 481 | /// \brief Classify argument of given type \p Ty. |
| 482 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 483 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 484 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 485 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 486 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 487 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 488 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 489 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 490 | } else if (Ty->isFloatingType()) { |
| 491 | // Floating-point types don't go inreg. |
| 492 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 493 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 494 | |
| 495 | return (Ty->isPromotableIntegerType() ? |
| 496 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 500 | if (RetTy->isVoidType()) |
| 501 | return ABIArgInfo::getIgnore(); |
| 502 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 503 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 504 | if (isAggregateTypeForABI(RetTy)) |
| 505 | return ABIArgInfo::getIndirect(0); |
| 506 | |
| 507 | // Treat an enum type as its underlying type. |
| 508 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 509 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 510 | |
| 511 | return (RetTy->isPromotableIntegerType() ? |
| 512 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 513 | } |
| 514 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 515 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 516 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 517 | // 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] | 518 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 519 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 520 | IRType->getScalarSizeInBits() != 64; |
| 521 | } |
| 522 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 523 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 524 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 525 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 526 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 527 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 528 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 529 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 532 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 536 | return Ty; |
| 537 | } |
| 538 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 539 | /// Returns true if this type can be passed in SSE registers with the |
| 540 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 541 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 542 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 543 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 544 | return true; |
| 545 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 546 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 547 | // registers specially. |
| 548 | unsigned VecSize = Context.getTypeSize(VT); |
| 549 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 550 | return true; |
| 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 556 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 557 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 558 | return NumMembers <= 4; |
| 559 | } |
| 560 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 561 | //===----------------------------------------------------------------------===// |
| 562 | // X86-32 ABI Implementation |
| 563 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 564 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 565 | /// \brief Similar to llvm::CCState, but for Clang. |
| 566 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 567 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 568 | |
| 569 | unsigned CC; |
| 570 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 571 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 572 | }; |
| 573 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 574 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 575 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 576 | enum Class { |
| 577 | Integer, |
| 578 | Float |
| 579 | }; |
| 580 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 581 | static const unsigned MinABIStackAlignInBytes = 4; |
| 582 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 583 | bool IsDarwinVectorABI; |
| 584 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 585 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 586 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 587 | |
| 588 | static bool isRegisterSize(unsigned Size) { |
| 589 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 590 | } |
| 591 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 592 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 593 | // FIXME: Assumes vectorcall is in use. |
| 594 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 595 | } |
| 596 | |
| 597 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 598 | uint64_t NumMembers) const override { |
| 599 | // FIXME: Assumes vectorcall is in use. |
| 600 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 601 | } |
| 602 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 603 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 604 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 605 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 606 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 607 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 608 | |
| 609 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 610 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 611 | /// \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] | 612 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 613 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 614 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 615 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 616 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 617 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 618 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 619 | /// \brief Rewrite the function info so that all memory arguments use |
| 620 | /// inalloca. |
| 621 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 622 | |
| 623 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 624 | unsigned &StackOffset, ABIArgInfo &Info, |
| 625 | QualType Type) const; |
| 626 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 627 | public: |
| 628 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 629 | void computeInfo(CGFunctionInfo &FI) const override; |
| 630 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 631 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 632 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 633 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 634 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 635 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 636 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 637 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 638 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 639 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 640 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 641 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 642 | bool d, bool p, bool w, unsigned r) |
| 643 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 644 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 645 | static bool isStructReturnInRegABI( |
| 646 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 647 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 648 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 649 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 650 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 651 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 652 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 653 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 654 | return 4; |
| 655 | } |
| 656 | |
| 657 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 658 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 659 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 660 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 661 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 662 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 663 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 664 | } |
| 665 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 666 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 667 | std::string &Constraints, |
| 668 | std::vector<llvm::Type *> &ResultRegTypes, |
| 669 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 670 | std::vector<LValue> &ResultRegDests, |
| 671 | std::string &AsmString, |
| 672 | unsigned NumOutputs) const override; |
| 673 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 674 | llvm::Constant * |
| 675 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 676 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 677 | (0x06 << 8) | // .+0x08 |
| 678 | ('F' << 16) | |
| 679 | ('T' << 24); |
| 680 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 681 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 682 | }; |
| 683 | |
| 684 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 685 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 686 | /// Rewrite input constraint references after adding some output constraints. |
| 687 | /// In the case where there is one output and one input and we add one output, |
| 688 | /// we need to replace all operand references greater than or equal to 1: |
| 689 | /// mov $0, $1 |
| 690 | /// mov eax, $1 |
| 691 | /// The result will be: |
| 692 | /// mov $0, $2 |
| 693 | /// mov eax, $2 |
| 694 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 695 | unsigned NumNewOuts, |
| 696 | std::string &AsmString) { |
| 697 | std::string Buf; |
| 698 | llvm::raw_string_ostream OS(Buf); |
| 699 | size_t Pos = 0; |
| 700 | while (Pos < AsmString.size()) { |
| 701 | size_t DollarStart = AsmString.find('$', Pos); |
| 702 | if (DollarStart == std::string::npos) |
| 703 | DollarStart = AsmString.size(); |
| 704 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 705 | if (DollarEnd == std::string::npos) |
| 706 | DollarEnd = AsmString.size(); |
| 707 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 708 | Pos = DollarEnd; |
| 709 | size_t NumDollars = DollarEnd - DollarStart; |
| 710 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 711 | // We have an operand reference. |
| 712 | size_t DigitStart = Pos; |
| 713 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 714 | if (DigitEnd == std::string::npos) |
| 715 | DigitEnd = AsmString.size(); |
| 716 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 717 | unsigned OperandIndex; |
| 718 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 719 | if (OperandIndex >= FirstIn) |
| 720 | OperandIndex += NumNewOuts; |
| 721 | OS << OperandIndex; |
| 722 | } else { |
| 723 | OS << OperandStr; |
| 724 | } |
| 725 | Pos = DigitEnd; |
| 726 | } |
| 727 | } |
| 728 | AsmString = std::move(OS.str()); |
| 729 | } |
| 730 | |
| 731 | /// Add output constraints for EAX:EDX because they are return registers. |
| 732 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 733 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 734 | std::vector<llvm::Type *> &ResultRegTypes, |
| 735 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 736 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 737 | unsigned NumOutputs) const { |
| 738 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 739 | |
| 740 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 741 | // larger. |
| 742 | if (!Constraints.empty()) |
| 743 | Constraints += ','; |
| 744 | if (RetWidth <= 32) { |
| 745 | Constraints += "={eax}"; |
| 746 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 747 | } else { |
| 748 | // Use the 'A' constraint for EAX:EDX. |
| 749 | Constraints += "=A"; |
| 750 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 751 | } |
| 752 | |
| 753 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 754 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 755 | ResultTruncRegTypes.push_back(CoerceTy); |
| 756 | |
| 757 | // Coerce the integer by bitcasting the return slot pointer. |
| 758 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 759 | CoerceTy->getPointerTo())); |
| 760 | ResultRegDests.push_back(ReturnSlot); |
| 761 | |
| 762 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 763 | } |
| 764 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 765 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 766 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 767 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 768 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 769 | uint64_t Size = Context.getTypeSize(Ty); |
| 770 | |
| 771 | // Type must be register sized. |
| 772 | if (!isRegisterSize(Size)) |
| 773 | return false; |
| 774 | |
| 775 | if (Ty->isVectorType()) { |
| 776 | // 64- and 128- bit vectors inside structures are not returned in |
| 777 | // registers. |
| 778 | if (Size == 64 || Size == 128) |
| 779 | return false; |
| 780 | |
| 781 | return true; |
| 782 | } |
| 783 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 784 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 785 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 786 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 787 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 788 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 789 | return true; |
| 790 | |
| 791 | // Arrays are treated like records. |
| 792 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 793 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 794 | |
| 795 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 796 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 797 | if (!RT) return false; |
| 798 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 799 | // FIXME: Traverse bases here too. |
| 800 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 801 | // Structure types are passed in register if all fields would be |
| 802 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 803 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 804 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 805 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 806 | continue; |
| 807 | |
| 808 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 809 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 810 | return false; |
| 811 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 812 | return true; |
| 813 | } |
| 814 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 815 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 816 | // If the return value is indirect, then the hidden argument is consuming one |
| 817 | // integer register. |
| 818 | if (State.FreeRegs) { |
| 819 | --State.FreeRegs; |
| 820 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 821 | } |
| 822 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 823 | } |
| 824 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 825 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 826 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 827 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 828 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 829 | const Type *Base = nullptr; |
| 830 | uint64_t NumElts = 0; |
| 831 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 832 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 833 | // The LLVM struct type for such an aggregate should lower properly. |
| 834 | return ABIArgInfo::getDirect(); |
| 835 | } |
| 836 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 837 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 838 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 839 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 840 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 841 | |
| 842 | // 128-bit vectors are a special case; they are returned in |
| 843 | // registers and we need to make sure to pick a type the LLVM |
| 844 | // backend will like. |
| 845 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 846 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 847 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 848 | |
| 849 | // Always return in register if it fits in a general purpose |
| 850 | // register, or if it is 64 bits and has a single element. |
| 851 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 852 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 853 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 854 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 855 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 856 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 860 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 861 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 862 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 863 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 864 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 865 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 866 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 867 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 868 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 869 | // If specified, structs and unions are always indirect. |
| 870 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 871 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 872 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 873 | // Small structures which are register sized are generally returned |
| 874 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 875 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 876 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 877 | |
| 878 | // As a special-case, if the struct is a "single-element" struct, and |
| 879 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 880 | // floating-point register. (MSVC does not apply this special case.) |
| 881 | // We apply a similar transformation for pointer types to improve the |
| 882 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 883 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 884 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 885 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 886 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 887 | |
| 888 | // FIXME: We should be able to narrow this integer in cases with dead |
| 889 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 890 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 893 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 894 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 896 | // Treat an enum type as its underlying type. |
| 897 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 898 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 899 | |
| 900 | return (RetTy->isPromotableIntegerType() ? |
| 901 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 904 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 905 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 906 | } |
| 907 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 908 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 909 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 910 | if (!RT) |
| 911 | return 0; |
| 912 | const RecordDecl *RD = RT->getDecl(); |
| 913 | |
| 914 | // If this is a C++ record, check the bases first. |
| 915 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 916 | for (const auto &I : CXXRD->bases()) |
| 917 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 918 | return false; |
| 919 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 920 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 921 | QualType FT = i->getType(); |
| 922 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 923 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 924 | return true; |
| 925 | |
| 926 | if (isRecordWithSSEVectorType(Context, FT)) |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | return false; |
| 931 | } |
| 932 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 933 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 934 | unsigned Align) const { |
| 935 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 936 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 937 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 938 | return 0; // Use default alignment. |
| 939 | |
| 940 | // On non-Darwin, the stack type alignment is always 4. |
| 941 | if (!IsDarwinVectorABI) { |
| 942 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 943 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 944 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 945 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 946 | // 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] | 947 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 948 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 949 | return 16; |
| 950 | |
| 951 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 954 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 955 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 956 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 957 | if (State.FreeRegs) { |
| 958 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 959 | return ABIArgInfo::getIndirectInReg(0, false); |
| 960 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 961 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 962 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 963 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 964 | // Compute the byval alignment. |
| 965 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 966 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 967 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 968 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 969 | |
| 970 | // If the stack alignment is less than the type alignment, realign the |
| 971 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 972 | bool Realign = TypeAlign > StackAlign; |
| 973 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 976 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 977 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 978 | if (!T) |
| 979 | T = Ty.getTypePtr(); |
| 980 | |
| 981 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 982 | BuiltinType::Kind K = BT->getKind(); |
| 983 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 984 | return Float; |
| 985 | } |
| 986 | return Integer; |
| 987 | } |
| 988 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 989 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 990 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 991 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 992 | Class C = classify(Ty); |
| 993 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 994 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 995 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 996 | unsigned Size = getContext().getTypeSize(Ty); |
| 997 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 998 | |
| 999 | if (SizeInRegs == 0) |
| 1000 | return false; |
| 1001 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1002 | if (SizeInRegs > State.FreeRegs) { |
| 1003 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1004 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1005 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1006 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1007 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1008 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1009 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1010 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1011 | if (Size > 32) |
| 1012 | return false; |
| 1013 | |
| 1014 | if (Ty->isIntegralOrEnumerationType()) |
| 1015 | return true; |
| 1016 | |
| 1017 | if (Ty->isPointerType()) |
| 1018 | return true; |
| 1019 | |
| 1020 | if (Ty->isReferenceType()) |
| 1021 | return true; |
| 1022 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1023 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1024 | NeedsPadding = true; |
| 1025 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
| 1028 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1029 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1032 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1033 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1034 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1035 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1036 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1037 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1038 | // Check with the C++ ABI first. |
| 1039 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1040 | if (RT) { |
| 1041 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1042 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1043 | return getIndirectResult(Ty, false, State); |
| 1044 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1045 | // The field index doesn't matter, we'll fix it up later. |
| 1046 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1051 | // to other targets. |
| 1052 | const Type *Base = nullptr; |
| 1053 | uint64_t NumElts = 0; |
| 1054 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1055 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1056 | if (State.FreeSSERegs >= NumElts) { |
| 1057 | State.FreeSSERegs -= NumElts; |
| 1058 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1059 | return ABIArgInfo::getDirect(); |
| 1060 | return ABIArgInfo::getExpand(); |
| 1061 | } |
| 1062 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1063 | } |
| 1064 | |
| 1065 | if (isAggregateTypeForABI(Ty)) { |
| 1066 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1067 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1068 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1069 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1070 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1071 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1072 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1073 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1074 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1075 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1076 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1077 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1078 | return ABIArgInfo::getIgnore(); |
| 1079 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1080 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1081 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 1082 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1083 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1084 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1085 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1086 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 1087 | return ABIArgInfo::getDirectInReg(Result); |
| 1088 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1089 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1090 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1091 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1092 | // of those arguments will match the struct. This is important because the |
| 1093 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1094 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1095 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 1096 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1097 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1098 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1099 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1100 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1101 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1102 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1105 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1106 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1107 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1108 | if (IsDarwinVectorABI) { |
| 1109 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1110 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1111 | (Size == 64 && VT->getNumElements() == 1)) |
| 1112 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1113 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1114 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1115 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1116 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1117 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1119 | return ABIArgInfo::getDirect(); |
| 1120 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1121 | |
| 1122 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1123 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1124 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1125 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1126 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1127 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1128 | |
| 1129 | if (Ty->isPromotableIntegerType()) { |
| 1130 | if (InReg) |
| 1131 | return ABIArgInfo::getExtendInReg(); |
| 1132 | return ABIArgInfo::getExtend(); |
| 1133 | } |
| 1134 | if (InReg) |
| 1135 | return ABIArgInfo::getDirectInReg(); |
| 1136 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1139 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1140 | CCState State(FI.getCallingConvention()); |
| 1141 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1142 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1143 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1144 | State.FreeRegs = 2; |
| 1145 | State.FreeSSERegs = 6; |
| 1146 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1147 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1148 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1149 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1150 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1151 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1152 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1153 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1154 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1155 | // return value was sret and put it in a register ourselves if appropriate. |
| 1156 | if (State.FreeRegs) { |
| 1157 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1158 | FI.getReturnInfo().setInReg(true); |
| 1159 | } |
| 1160 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1161 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1162 | // The chain argument effectively gives us another free register. |
| 1163 | if (FI.isChainCall()) |
| 1164 | ++State.FreeRegs; |
| 1165 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1166 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1167 | for (auto &I : FI.arguments()) { |
| 1168 | I.info = classifyArgumentType(I.type, State); |
| 1169 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1173 | // all the memory arguments to use inalloca. |
| 1174 | if (UsedInAlloca) |
| 1175 | rewriteWithInAlloca(FI); |
| 1176 | } |
| 1177 | |
| 1178 | void |
| 1179 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1180 | unsigned &StackOffset, |
| 1181 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1182 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 1183 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1184 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1185 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1186 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1187 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1188 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1189 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1190 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1191 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1192 | unsigned NumBytes = StackOffset - OldOffset; |
| 1193 | assert(NumBytes); |
| 1194 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1195 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1196 | FrameFields.push_back(Ty); |
| 1197 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1200 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1201 | // Leave ignored and inreg arguments alone. |
| 1202 | switch (Info.getKind()) { |
| 1203 | case ABIArgInfo::InAlloca: |
| 1204 | return true; |
| 1205 | case ABIArgInfo::Indirect: |
| 1206 | assert(Info.getIndirectByVal()); |
| 1207 | return true; |
| 1208 | case ABIArgInfo::Ignore: |
| 1209 | return false; |
| 1210 | case ABIArgInfo::Direct: |
| 1211 | case ABIArgInfo::Extend: |
| 1212 | case ABIArgInfo::Expand: |
| 1213 | if (Info.getInReg()) |
| 1214 | return false; |
| 1215 | return true; |
| 1216 | } |
| 1217 | llvm_unreachable("invalid enum"); |
| 1218 | } |
| 1219 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1220 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1221 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1222 | |
| 1223 | // Build a packed struct type for all of the arguments in memory. |
| 1224 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1225 | |
| 1226 | unsigned StackOffset = 0; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1227 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1228 | |
| 1229 | // Put 'this' into the struct before 'sret', if necessary. |
| 1230 | bool IsThisCall = |
| 1231 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1232 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1233 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1234 | isArgInAlloca(I->info)) { |
| 1235 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1236 | ++I; |
| 1237 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1238 | |
| 1239 | // 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] | 1240 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1241 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1242 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1243 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1244 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1248 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1249 | ++I; |
| 1250 | |
| 1251 | // Put arguments passed in memory into the struct. |
| 1252 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1253 | if (isArgInAlloca(I->info)) |
| 1254 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1258 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1261 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1262 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1263 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1264 | |
| 1265 | CGBuilderTy &Builder = CGF.Builder; |
| 1266 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1267 | "ap"); |
| 1268 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Compute if the address needs to be aligned |
| 1271 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1272 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1273 | Align = std::max(Align, 4U); |
| 1274 | if (Align > 4) { |
| 1275 | // addr = (addr + align - 1) & -align; |
| 1276 | llvm::Value *Offset = |
| 1277 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1278 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1279 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1280 | CGF.Int32Ty); |
| 1281 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1282 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1283 | Addr->getType(), |
| 1284 | "ap.cur.aligned"); |
| 1285 | } |
| 1286 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1287 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1288 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1289 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1290 | |
| 1291 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1292 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1293 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1294 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1295 | "ap.next"); |
| 1296 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1297 | |
| 1298 | return AddrTyped; |
| 1299 | } |
| 1300 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1301 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1302 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1303 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1304 | |
| 1305 | switch (Opts.getStructReturnConvention()) { |
| 1306 | case CodeGenOptions::SRCK_Default: |
| 1307 | break; |
| 1308 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1309 | return false; |
| 1310 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | if (Triple.isOSDarwin()) |
| 1315 | return true; |
| 1316 | |
| 1317 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1318 | case llvm::Triple::DragonFly: |
| 1319 | case llvm::Triple::FreeBSD: |
| 1320 | case llvm::Triple::OpenBSD: |
| 1321 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1322 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1323 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1324 | default: |
| 1325 | return false; |
| 1326 | } |
| 1327 | } |
| 1328 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1329 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1330 | llvm::GlobalValue *GV, |
| 1331 | CodeGen::CodeGenModule &CGM) const { |
| 1332 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1333 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1334 | // Get the LLVM function. |
| 1335 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1336 | |
| 1337 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1338 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1339 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1340 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1341 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1342 | llvm::AttributeSet::FunctionIndex, |
| 1343 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1348 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1349 | CodeGen::CodeGenFunction &CGF, |
| 1350 | llvm::Value *Address) const { |
| 1351 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1353 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1354 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1355 | // 0-7 are the eight integer registers; the order is different |
| 1356 | // on Darwin (for EH), but the range is the same. |
| 1357 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1358 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1359 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1360 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1361 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1362 | // These have size 16, which is sizeof(long double) on |
| 1363 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1364 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1365 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1366 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1367 | } else { |
| 1368 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1369 | // reason. |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 1370 | Builder.CreateStore( |
| 1371 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9)); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1372 | |
| 1373 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1374 | // These have size 12, which is sizeof(long double) on |
| 1375 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1376 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1377 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1378 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1379 | |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1383 | //===----------------------------------------------------------------------===// |
| 1384 | // X86-64 ABI Implementation |
| 1385 | //===----------------------------------------------------------------------===// |
| 1386 | |
| 1387 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1388 | namespace { |
| 1389 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1390 | class X86_64ABIInfo : public ABIInfo { |
| 1391 | enum Class { |
| 1392 | Integer = 0, |
| 1393 | SSE, |
| 1394 | SSEUp, |
| 1395 | X87, |
| 1396 | X87Up, |
| 1397 | ComplexX87, |
| 1398 | NoClass, |
| 1399 | Memory |
| 1400 | }; |
| 1401 | |
| 1402 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1403 | /// |
| 1404 | /// Merge an accumulating classification \arg Accum with a field |
| 1405 | /// classification \arg Field. |
| 1406 | /// |
| 1407 | /// \param Accum - The accumulating classification. This should |
| 1408 | /// always be either NoClass or the result of a previous merge |
| 1409 | /// call. In addition, this should never be Memory (the caller |
| 1410 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1411 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1412 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1413 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1414 | /// |
| 1415 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1416 | /// final MEMORY or SSE classes when necessary. |
| 1417 | /// |
| 1418 | /// \param AggregateSize - The size of the current aggregate in |
| 1419 | /// the classification process. |
| 1420 | /// |
| 1421 | /// \param Lo - The classification for the parts of the type |
| 1422 | /// residing in the low word of the containing object. |
| 1423 | /// |
| 1424 | /// \param Hi - The classification for the parts of the type |
| 1425 | /// residing in the higher words of the containing object. |
| 1426 | /// |
| 1427 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1428 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1429 | /// classify - Determine the x86_64 register classes in which the |
| 1430 | /// given type T should be passed. |
| 1431 | /// |
| 1432 | /// \param Lo - The classification for the parts of the type |
| 1433 | /// residing in the low word of the containing object. |
| 1434 | /// |
| 1435 | /// \param Hi - The classification for the parts of the type |
| 1436 | /// residing in the high word of the containing object. |
| 1437 | /// |
| 1438 | /// \param OffsetBase - The bit offset of this type in the |
| 1439 | /// containing object. Some parameters are classified different |
| 1440 | /// depending on whether they straddle an eightbyte boundary. |
| 1441 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1442 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1443 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1444 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1445 | /// If a word is unused its result will be NoClass; if a type should |
| 1446 | /// be passed in Memory then at least the classification of \arg Lo |
| 1447 | /// will be Memory. |
| 1448 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1449 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1450 | /// |
| 1451 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1452 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1453 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1454 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1455 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1456 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1457 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1458 | unsigned IROffset, QualType SourceTy, |
| 1459 | unsigned SourceOffset) const; |
| 1460 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1461 | unsigned IROffset, QualType SourceTy, |
| 1462 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1463 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1464 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1465 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1466 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1467 | |
| 1468 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1469 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1470 | /// |
| 1471 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1472 | /// available. |
| 1473 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1474 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1475 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1476 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1477 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1478 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1479 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1480 | unsigned &neededSSE, |
| 1481 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1482 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1483 | bool IsIllegalVectorType(QualType Ty) const; |
| 1484 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1485 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1486 | /// unfortunately in ways that were not always consistent with |
| 1487 | /// certain previous compilers. In particular, platforms which |
| 1488 | /// required strict binary compatibility with older versions of GCC |
| 1489 | /// may need to exempt themselves. |
| 1490 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1491 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1494 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1495 | // 64-bit hardware. |
| 1496 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1497 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1498 | public: |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1499 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 1500 | ABIInfo(CGT), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1501 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1502 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1503 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1504 | bool isPassedUsingAVXType(QualType type) const { |
| 1505 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1506 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1507 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1508 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1509 | if (info.isDirect()) { |
| 1510 | llvm::Type *ty = info.getCoerceToType(); |
| 1511 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1512 | return (vectorTy->getBitWidth() > 128); |
| 1513 | } |
| 1514 | return false; |
| 1515 | } |
| 1516 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1517 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1518 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1519 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1520 | CodeGenFunction &CGF) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1521 | |
| 1522 | bool has64BitPointers() const { |
| 1523 | return Has64BitPointers; |
| 1524 | } |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1525 | |
| 1526 | bool hasAVX() const { |
| 1527 | return getTarget().getABI() == "avx"; |
| 1528 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1529 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1530 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1531 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1532 | class WinX86_64ABIInfo : public ABIInfo { |
| 1533 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1534 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1535 | bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1537 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1538 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1539 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1540 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1541 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1542 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1543 | CodeGenFunction &CGF) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1544 | |
| 1545 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1546 | // FIXME: Assumes vectorcall is in use. |
| 1547 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1548 | } |
| 1549 | |
| 1550 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1551 | uint64_t NumMembers) const override { |
| 1552 | // FIXME: Assumes vectorcall is in use. |
| 1553 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1554 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1555 | }; |
| 1556 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1557 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1558 | public: |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1559 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1560 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1561 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1562 | const X86_64ABIInfo &getABIInfo() const { |
| 1563 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1564 | } |
| 1565 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1566 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1567 | return 7; |
| 1568 | } |
| 1569 | |
| 1570 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1571 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1572 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1573 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1574 | // 0-15 are the 16 integer registers. |
| 1575 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1576 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1577 | return false; |
| 1578 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1579 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1580 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1581 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1582 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1583 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1584 | } |
| 1585 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1586 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1587 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1588 | // The default CC on x86-64 sets %al to the number of SSA |
| 1589 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1590 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1591 | // that when AVX types are involved: the ABI explicitly states it is |
| 1592 | // undefined, and it doesn't work in practice because of how the ABI |
| 1593 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1594 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1595 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1596 | for (CallArgList::const_iterator |
| 1597 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1598 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1599 | HasAVXType = true; |
| 1600 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1601 | } |
| 1602 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1603 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1604 | if (!HasAVXType) |
| 1605 | return true; |
| 1606 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1607 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1608 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1611 | llvm::Constant * |
| 1612 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1613 | unsigned Sig; |
| 1614 | if (getABIInfo().has64BitPointers()) |
| 1615 | Sig = (0xeb << 0) | // jmp rel8 |
| 1616 | (0x0a << 8) | // .+0x0c |
| 1617 | ('F' << 16) | |
| 1618 | ('T' << 24); |
| 1619 | else |
| 1620 | Sig = (0xeb << 0) | // jmp rel8 |
| 1621 | (0x06 << 8) | // .+0x08 |
| 1622 | ('F' << 16) | |
| 1623 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1624 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1625 | } |
| 1626 | |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1627 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1628 | return getABIInfo().hasAVX() ? 32 : 16; |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1629 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1630 | }; |
| 1631 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1632 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1633 | public: |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1634 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1635 | : X86_64TargetCodeGenInfo(CGT) {} |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1636 | |
| 1637 | void getDependentLibraryOption(llvm::StringRef Lib, |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 1638 | llvm::SmallString<24> &Opt) const override { |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1639 | Opt = "\01"; |
| 1640 | Opt += Lib; |
| 1641 | } |
| 1642 | }; |
| 1643 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1644 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1645 | // If the argument does not end in .lib, automatically add the suffix. |
| 1646 | // If the argument contains a space, enclose it in quotes. |
| 1647 | // This matches the behavior of MSVC. |
| 1648 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1649 | std::string ArgStr = Quote ? "\"" : ""; |
| 1650 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1651 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1652 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1653 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1654 | return ArgStr; |
| 1655 | } |
| 1656 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1657 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1658 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1659 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1660 | bool d, bool p, bool w, unsigned RegParms) |
| 1661 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1662 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1663 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1664 | CodeGen::CodeGenModule &CGM) const override; |
| 1665 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1666 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1667 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1668 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1669 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1670 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1671 | |
| 1672 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1673 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1674 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1675 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1676 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1677 | }; |
| 1678 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1679 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1680 | llvm::GlobalValue *GV, |
| 1681 | CodeGen::CodeGenModule &CGM) { |
| 1682 | if (isa<FunctionDecl>(D)) { |
| 1683 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1684 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1685 | |
| 1686 | Fn->addFnAttr("stack-probe-size", llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | void WinX86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1692 | llvm::GlobalValue *GV, |
| 1693 | CodeGen::CodeGenModule &CGM) const { |
| 1694 | X86_32TargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 1695 | |
| 1696 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1697 | } |
| 1698 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1699 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1700 | bool hasAVX() const { return getABIInfo().getTarget().getABI() == "avx"; } |
| 1701 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1702 | public: |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1703 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1704 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1705 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1706 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1707 | CodeGen::CodeGenModule &CGM) const override; |
| 1708 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1709 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1710 | return 7; |
| 1711 | } |
| 1712 | |
| 1713 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1714 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1715 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1717 | // 0-15 are the 16 integer registers. |
| 1718 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1719 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1720 | return false; |
| 1721 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1722 | |
| 1723 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1724 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1725 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1726 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1727 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1728 | |
| 1729 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1730 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1731 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1732 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1733 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1734 | |
| 1735 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1736 | return hasAVX() ? 32 : 16; |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1737 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1738 | }; |
| 1739 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1740 | void WinX86_64TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1741 | llvm::GlobalValue *GV, |
| 1742 | CodeGen::CodeGenModule &CGM) const { |
| 1743 | TargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 1744 | |
| 1745 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1746 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1749 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1750 | Class &Hi) const { |
| 1751 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1752 | // |
| 1753 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1754 | // memory. |
| 1755 | // |
| 1756 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1757 | // memory. |
| 1758 | // |
| 1759 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1760 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1761 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1762 | // ABI working for processors that don't support the __m256 type. |
| 1763 | // |
| 1764 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1765 | // |
| 1766 | // Some of these are enforced by the merging logic. Others can arise |
| 1767 | // only with unions; for example: |
| 1768 | // union { _Complex double; unsigned; } |
| 1769 | // |
| 1770 | // Note that clauses (b) and (c) were added in 0.98. |
| 1771 | // |
| 1772 | if (Hi == Memory) |
| 1773 | Lo = Memory; |
| 1774 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1775 | Lo = Memory; |
| 1776 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1777 | Lo = Memory; |
| 1778 | if (Hi == SSEUp && Lo != SSE) |
| 1779 | Hi = SSE; |
| 1780 | } |
| 1781 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1782 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1783 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1784 | // classified recursively so that always two fields are |
| 1785 | // considered. The resulting class is calculated according to |
| 1786 | // the classes of the fields in the eightbyte: |
| 1787 | // |
| 1788 | // (a) If both classes are equal, this is the resulting class. |
| 1789 | // |
| 1790 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1791 | // the other class. |
| 1792 | // |
| 1793 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1794 | // class. |
| 1795 | // |
| 1796 | // (d) If one of the classes is INTEGER, the result is the |
| 1797 | // INTEGER. |
| 1798 | // |
| 1799 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1800 | // MEMORY is used as class. |
| 1801 | // |
| 1802 | // (f) Otherwise class SSE is used. |
| 1803 | |
| 1804 | // Accum should never be memory (we should have returned) or |
| 1805 | // ComplexX87 (because this cannot be passed in a structure). |
| 1806 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1807 | "Invalid accumulated classification during merge."); |
| 1808 | if (Accum == Field || Field == NoClass) |
| 1809 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1810 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1811 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1812 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1813 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1814 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1815 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1816 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1817 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1818 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1819 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1822 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1823 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1824 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1825 | // Class pairs with appropriate constructor methods for the various |
| 1826 | // situations. |
| 1827 | |
| 1828 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1829 | // shouldn't be passed in registers for example, so there is no chance they |
| 1830 | // can straddle an eightbyte. Verify & simplify. |
| 1831 | |
| 1832 | Lo = Hi = NoClass; |
| 1833 | |
| 1834 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1835 | Current = Memory; |
| 1836 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1837 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1838 | BuiltinType::Kind k = BT->getKind(); |
| 1839 | |
| 1840 | if (k == BuiltinType::Void) { |
| 1841 | Current = NoClass; |
| 1842 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1843 | Lo = Integer; |
| 1844 | Hi = Integer; |
| 1845 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1846 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1847 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1848 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1849 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1850 | Current = SSE; |
| 1851 | } else if (k == BuiltinType::LongDouble) { |
| 1852 | Lo = X87; |
| 1853 | Hi = X87Up; |
| 1854 | } |
| 1855 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1856 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1857 | return; |
| 1858 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1859 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1860 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1861 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1862 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1863 | return; |
| 1864 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1865 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1866 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1867 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1868 | return; |
| 1869 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1871 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1872 | if (Ty->isMemberFunctionPointerType()) { |
| 1873 | if (Has64BitPointers) { |
| 1874 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 1875 | // Lo and Hi now. |
| 1876 | Lo = Hi = Integer; |
| 1877 | } else { |
| 1878 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 1879 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 1880 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 1881 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 1882 | if (EB_FuncPtr != EB_ThisAdj) { |
| 1883 | Lo = Hi = Integer; |
| 1884 | } else { |
| 1885 | Current = Integer; |
| 1886 | } |
| 1887 | } |
| 1888 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1889 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1890 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1891 | return; |
| 1892 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1893 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1894 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1895 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1896 | if (Size == 32) { |
| 1897 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1898 | // float> as integer. |
| 1899 | Current = Integer; |
| 1900 | |
| 1901 | // If this type crosses an eightbyte boundary, it should be |
| 1902 | // split. |
| 1903 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1904 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1905 | if (EB_Real != EB_Imag) |
| 1906 | Hi = Lo; |
| 1907 | } else if (Size == 64) { |
| 1908 | // gcc passes <1 x double> in memory. :( |
| 1909 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1910 | return; |
| 1911 | |
| 1912 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1913 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1914 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1915 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1916 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1917 | Current = Integer; |
| 1918 | else |
| 1919 | Current = SSE; |
| 1920 | |
| 1921 | // If this type crosses an eightbyte boundary, it should be |
| 1922 | // split. |
| 1923 | if (OffsetBase && OffsetBase != 64) |
| 1924 | Hi = Lo; |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 1925 | } else if (Size == 128 || (hasAVX() && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1926 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1927 | // least significant one belongs to class SSE and all the others to class |
| 1928 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1929 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1930 | // This design isn't correct for 256-bits, but since there're no cases |
| 1931 | // where the upper parts would need to be inspected, avoid adding |
| 1932 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1933 | // |
| 1934 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1935 | // registers if they are "named", i.e. not part of the "..." of a |
| 1936 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1937 | Lo = SSE; |
| 1938 | Hi = SSEUp; |
| 1939 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1940 | return; |
| 1941 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1942 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1943 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1944 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1945 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1946 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1947 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1948 | if (Size <= 64) |
| 1949 | Current = Integer; |
| 1950 | else if (Size <= 128) |
| 1951 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1952 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1953 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1954 | else if (ET == getContext().DoubleTy || |
| 1955 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1956 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1957 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1958 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1959 | Current = ComplexX87; |
| 1960 | |
| 1961 | // If this complex type crosses an eightbyte boundary then it |
| 1962 | // should be split. |
| 1963 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1964 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1965 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1966 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1967 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1968 | return; |
| 1969 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1970 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1971 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1972 | // Arrays are treated like structures. |
| 1973 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1974 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1975 | |
| 1976 | // 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] | 1977 | // than four eightbytes, ..., it has class MEMORY. |
| 1978 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1979 | return; |
| 1980 | |
| 1981 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1982 | // fields, it has class MEMORY. |
| 1983 | // |
| 1984 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1985 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1986 | return; |
| 1987 | |
| 1988 | // Otherwise implement simplified merge. We could be smarter about |
| 1989 | // this, but it isn't worth it and would be harder to verify. |
| 1990 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1991 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1992 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1993 | |
| 1994 | // The only case a 256-bit wide vector could be used is when the array |
| 1995 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1996 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1997 | if (Size > 128 && EltSize != 256) |
| 1998 | return; |
| 1999 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2000 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2001 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2002 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2003 | Lo = merge(Lo, FieldLo); |
| 2004 | Hi = merge(Hi, FieldHi); |
| 2005 | if (Lo == Memory || Hi == Memory) |
| 2006 | break; |
| 2007 | } |
| 2008 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2009 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2010 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2011 | return; |
| 2012 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2014 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2015 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2016 | |
| 2017 | // 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] | 2018 | // than four eightbytes, ..., it has class MEMORY. |
| 2019 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2020 | return; |
| 2021 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2022 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2023 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2024 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2025 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2026 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2027 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2028 | const RecordDecl *RD = RT->getDecl(); |
| 2029 | |
| 2030 | // Assume variable sized types are passed in memory. |
| 2031 | if (RD->hasFlexibleArrayMember()) |
| 2032 | return; |
| 2033 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2034 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2035 | |
| 2036 | // Reset Lo class, this will be recomputed. |
| 2037 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2038 | |
| 2039 | // If this is a C++ record, classify the bases first. |
| 2040 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2041 | for (const auto &I : CXXRD->bases()) { |
| 2042 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2043 | "Unexpected base class!"); |
| 2044 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2045 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2046 | |
| 2047 | // Classify this field. |
| 2048 | // |
| 2049 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2050 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2051 | // initialized to class NO_CLASS. |
| 2052 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2053 | uint64_t Offset = |
| 2054 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2055 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2056 | Lo = merge(Lo, FieldLo); |
| 2057 | Hi = merge(Hi, FieldHi); |
| 2058 | if (Lo == Memory || Hi == Memory) |
| 2059 | break; |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2064 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2065 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2066 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2067 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2068 | bool BitField = i->isBitField(); |
| 2069 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2070 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2071 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2072 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2073 | // The only case a 256-bit wide vector could be used is when the struct |
| 2074 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2075 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2076 | // |
| 2077 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2078 | Lo = Memory; |
| 2079 | return; |
| 2080 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2081 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2082 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2083 | Lo = Memory; |
| 2084 | return; |
| 2085 | } |
| 2086 | |
| 2087 | // Classify this field. |
| 2088 | // |
| 2089 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2090 | // exceeds a single eightbyte, each is classified |
| 2091 | // separately. Each eightbyte gets initialized to class |
| 2092 | // NO_CLASS. |
| 2093 | Class FieldLo, FieldHi; |
| 2094 | |
| 2095 | // Bit-fields require special handling, they do not force the |
| 2096 | // structure to be passed in memory even if unaligned, and |
| 2097 | // therefore they can straddle an eightbyte. |
| 2098 | if (BitField) { |
| 2099 | // Ignore padding bit-fields. |
| 2100 | if (i->isUnnamedBitfield()) |
| 2101 | continue; |
| 2102 | |
| 2103 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2104 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2105 | |
| 2106 | uint64_t EB_Lo = Offset / 64; |
| 2107 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2108 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2109 | if (EB_Lo) { |
| 2110 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2111 | FieldLo = NoClass; |
| 2112 | FieldHi = Integer; |
| 2113 | } else { |
| 2114 | FieldLo = Integer; |
| 2115 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2116 | } |
| 2117 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2118 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2119 | Lo = merge(Lo, FieldLo); |
| 2120 | Hi = merge(Hi, FieldHi); |
| 2121 | if (Lo == Memory || Hi == Memory) |
| 2122 | break; |
| 2123 | } |
| 2124 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2125 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2129 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2130 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2131 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2132 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2133 | // Treat an enum type as its underlying type. |
| 2134 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2135 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2136 | |
| 2137 | return (Ty->isPromotableIntegerType() ? |
| 2138 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2139 | } |
| 2140 | |
| 2141 | return ABIArgInfo::getIndirect(0); |
| 2142 | } |
| 2143 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2144 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2145 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2146 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 2147 | unsigned LargestVector = hasAVX() ? 256 : 128; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2148 | if (Size <= 64 || Size > LargestVector) |
| 2149 | return true; |
| 2150 | } |
| 2151 | |
| 2152 | return false; |
| 2153 | } |
| 2154 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2155 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2156 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2157 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2158 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2159 | // |
| 2160 | // This assumption is optimistic, as there could be free registers available |
| 2161 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2162 | // the argument in the free register. This does not seem to happen currently, |
| 2163 | // but this code would be much safer if we could mark the argument with |
| 2164 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2165 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2166 | // Treat an enum type as its underlying type. |
| 2167 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2168 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2169 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2170 | return (Ty->isPromotableIntegerType() ? |
| 2171 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2172 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2173 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2174 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2175 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2177 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2178 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2179 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2180 | |
| 2181 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2182 | // is important for good codegen. |
| 2183 | // |
| 2184 | // We do this by coercing the value into a scalar type which the backend can |
| 2185 | // handle naturally (i.e., without using byval). |
| 2186 | // |
| 2187 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2188 | // free integer registers. Doing this when there are free integer registers |
| 2189 | // would require more care, as we would have to ensure that the coerced value |
| 2190 | // did not claim the unused register. That would require either reording the |
| 2191 | // arguments to the function (so that any subsequent inreg values came first), |
| 2192 | // or only doing this optimization when there were no following arguments that |
| 2193 | // might be inreg. |
| 2194 | // |
| 2195 | // We currently expect it to be rare (particularly in well written code) for |
| 2196 | // arguments to be passed on the stack when there are still free integer |
| 2197 | // registers available (this would typically imply large structs being passed |
| 2198 | // by value), so this seems like a fair tradeoff for now. |
| 2199 | // |
| 2200 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2201 | // attributes. See PR12193. |
| 2202 | if (freeIntRegs == 0) { |
| 2203 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2204 | |
| 2205 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2206 | // type, which will end up on the stack (with alignment 8). |
| 2207 | if (Align == 8 && Size <= 64) |
| 2208 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2209 | Size)); |
| 2210 | } |
| 2211 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2212 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2215 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2216 | /// 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] | 2217 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2218 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2219 | // vectors; strip them off if present. |
| 2220 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2221 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2222 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2223 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 2224 | assert(isa<llvm::VectorType>(IRType) && |
| 2225 | "Trying to return a non-vector type in a vector register!"); |
| 2226 | return IRType; |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2229 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2230 | /// is known to either be off the end of the specified type or being in |
| 2231 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2232 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2233 | /// classification that put one of the two halves in the INTEGER class. |
| 2234 | /// |
| 2235 | /// It is conservatively correct to return false. |
| 2236 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2237 | unsigned EndBit, ASTContext &Context) { |
| 2238 | // If the bytes being queried are off the end of the type, there is no user |
| 2239 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2240 | // types that don't contain interesting padding. |
| 2241 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2242 | if (TySize <= StartBit) |
| 2243 | return true; |
| 2244 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2245 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2246 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2247 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2248 | |
| 2249 | // Check each element to see if the element overlaps with the queried range. |
| 2250 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2251 | // If the element is after the span we care about, then we're done.. |
| 2252 | unsigned EltOffset = i*EltSize; |
| 2253 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2254 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2255 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2256 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2257 | EndBit-EltOffset, Context)) |
| 2258 | return false; |
| 2259 | } |
| 2260 | // If it overlaps no elements, then it is safe to process as padding. |
| 2261 | return true; |
| 2262 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2263 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2264 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2265 | const RecordDecl *RD = RT->getDecl(); |
| 2266 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2267 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2268 | // If this is a C++ record, check the bases first. |
| 2269 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2270 | for (const auto &I : CXXRD->bases()) { |
| 2271 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2272 | "Unexpected base class!"); |
| 2273 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2274 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2275 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2276 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2277 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2278 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2279 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2280 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2281 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2282 | EndBit-BaseOffset, Context)) |
| 2283 | return false; |
| 2284 | } |
| 2285 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2286 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2287 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2288 | // this could be sped up a lot by being smarter about queried fields, |
| 2289 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2290 | // much. |
| 2291 | unsigned idx = 0; |
| 2292 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2293 | i != e; ++i, ++idx) { |
| 2294 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2295 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2296 | // If we found a field after the region we care about, then we're done. |
| 2297 | if (FieldOffset >= EndBit) break; |
| 2298 | |
| 2299 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2300 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2301 | Context)) |
| 2302 | return false; |
| 2303 | } |
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 nothing in this record overlapped the area of interest, then we're |
| 2306 | // clean. |
| 2307 | return true; |
| 2308 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2309 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2310 | return false; |
| 2311 | } |
| 2312 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2313 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2314 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2315 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2316 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2317 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2318 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2319 | // Base case if we find a float. |
| 2320 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2321 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2322 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2323 | // 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] | 2324 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2325 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2326 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2327 | IROffset -= SL->getElementOffset(Elt); |
| 2328 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2329 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2330 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2331 | // 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] | 2332 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2333 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2334 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2335 | IROffset -= IROffset/EltSize*EltSize; |
| 2336 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2337 | } |
| 2338 | |
| 2339 | return false; |
| 2340 | } |
| 2341 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2342 | |
| 2343 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2344 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2345 | llvm::Type *X86_64ABIInfo:: |
| 2346 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2347 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2348 | // 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] | 2349 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2350 | // structs that contain 3 floats. |
| 2351 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2352 | SourceOffset*8+64, getContext())) |
| 2353 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2354 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2355 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2356 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2357 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2358 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2359 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2360 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2361 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2362 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2363 | } |
| 2364 | |
| 2365 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2366 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2367 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2368 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2369 | /// 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] | 2370 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2371 | /// etc). |
| 2372 | /// |
| 2373 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2374 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2375 | /// the 8-byte value references. PrefType may be null. |
| 2376 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2377 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2378 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2379 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2380 | llvm::Type *X86_64ABIInfo:: |
| 2381 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2382 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2383 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2384 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2385 | if (IROffset == 0) { |
| 2386 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2387 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2388 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2389 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2390 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2391 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2392 | // goodness in the source type is just tail padding. This is allowed to |
| 2393 | // kick in for struct {double,int} on the int, but not on |
| 2394 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2395 | // have to do this analysis on the source type because we can't depend on |
| 2396 | // unions being lowered a specific way etc. |
| 2397 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2398 | IRType->isIntegerTy(32) || |
| 2399 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2400 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2401 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2403 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2404 | SourceOffset*8+64, getContext())) |
| 2405 | return IRType; |
| 2406 | } |
| 2407 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2408 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2409 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2410 | // 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] | 2411 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2412 | if (IROffset < SL->getSizeInBytes()) { |
| 2413 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2414 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2415 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2416 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2417 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2418 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2419 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2420 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2421 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2422 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2423 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2424 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2425 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2426 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2427 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2429 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2430 | // 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] | 2431 | unsigned TySizeInBytes = |
| 2432 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2433 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2434 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2435 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2436 | // It is always safe to classify this as an integer type up to i64 that |
| 2437 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2438 | return llvm::IntegerType::get(getVMContext(), |
| 2439 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2442 | |
| 2443 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2444 | /// be used as elements of a two register pair to pass or return, return a |
| 2445 | /// first class aggregate to represent them. For example, if the low part of |
| 2446 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2447 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2448 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2449 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2450 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2451 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2452 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2453 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2454 | // the second element at offset 8. Check for this: |
| 2455 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2456 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2457 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2458 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2459 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2460 | // To handle this, we have to increase the size of the low part so that the |
| 2461 | // second element will start at an 8 byte offset. We can't increase the size |
| 2462 | // of the second element because it might make us access off the end of the |
| 2463 | // struct. |
| 2464 | if (HiStart != 8) { |
| 2465 | // There are only two sorts of types the ABI generation code can produce for |
| 2466 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2467 | // Promote these to a larger type. |
| 2468 | if (Lo->isFloatTy()) |
| 2469 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2470 | else { |
| 2471 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2472 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2473 | } |
| 2474 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2475 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2476 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2477 | |
| 2478 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2479 | // Verify that the second element is at an 8-byte offset. |
| 2480 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2481 | "Invalid x86-64 argument pair!"); |
| 2482 | return Result; |
| 2483 | } |
| 2484 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2485 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2486 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2487 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2488 | // classification algorithm. |
| 2489 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2490 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2491 | |
| 2492 | // Check some invariants. |
| 2493 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2494 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2495 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2496 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2497 | switch (Lo) { |
| 2498 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2499 | if (Hi == NoClass) |
| 2500 | return ABIArgInfo::getIgnore(); |
| 2501 | // If the low part is just padding, it takes no register, leave ResType |
| 2502 | // null. |
| 2503 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2504 | "Unknown missing lo part"); |
| 2505 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2506 | |
| 2507 | case SSEUp: |
| 2508 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2509 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2510 | |
| 2511 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2512 | // hidden argument. |
| 2513 | case Memory: |
| 2514 | return getIndirectReturnResult(RetTy); |
| 2515 | |
| 2516 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2517 | // available register of the sequence %rax, %rdx is used. |
| 2518 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2519 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2520 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2521 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2522 | // so that the parameter gets the right LLVM IR attributes. |
| 2523 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2524 | // Treat an enum type as its underlying type. |
| 2525 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2526 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2527 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2528 | if (RetTy->isIntegralOrEnumerationType() && |
| 2529 | RetTy->isPromotableIntegerType()) |
| 2530 | return ABIArgInfo::getExtend(); |
| 2531 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2532 | break; |
| 2533 | |
| 2534 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2535 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2536 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2537 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2538 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2539 | |
| 2540 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2541 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2542 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2543 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2544 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2545 | |
| 2546 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2547 | // part of the value is returned in %st0 and the imaginary part in |
| 2548 | // %st1. |
| 2549 | case ComplexX87: |
| 2550 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2551 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2552 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2553 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2554 | break; |
| 2555 | } |
| 2556 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2557 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2558 | switch (Hi) { |
| 2559 | // Memory was handled previously and X87 should |
| 2560 | // never occur as a hi class. |
| 2561 | case Memory: |
| 2562 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2563 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2564 | |
| 2565 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2566 | case NoClass: |
| 2567 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2568 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2569 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2570 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2571 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2572 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2573 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2574 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2575 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2576 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2577 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2578 | break; |
| 2579 | |
| 2580 | // 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] | 2581 | // is passed in the next available eightbyte chunk if the last used |
| 2582 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2583 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2584 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2585 | case SSEUp: |
| 2586 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2587 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2588 | break; |
| 2589 | |
| 2590 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2591 | // returned together with the previous X87 value in %st0. |
| 2592 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2593 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2594 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2595 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2596 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2597 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2598 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2599 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2600 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2601 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2602 | break; |
| 2603 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2605 | // 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] | 2606 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2607 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2608 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2609 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2610 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2611 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2614 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2615 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2616 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2617 | const |
| 2618 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2619 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2620 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2621 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2622 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2623 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2624 | // Check some invariants. |
| 2625 | // FIXME: Enforce these by construction. |
| 2626 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2627 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2628 | |
| 2629 | neededInt = 0; |
| 2630 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2631 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2632 | switch (Lo) { |
| 2633 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2634 | if (Hi == NoClass) |
| 2635 | return ABIArgInfo::getIgnore(); |
| 2636 | // If the low part is just padding, it takes no register, leave ResType |
| 2637 | // null. |
| 2638 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2639 | "Unknown missing lo part"); |
| 2640 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2641 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2642 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2643 | // on the stack. |
| 2644 | case Memory: |
| 2645 | |
| 2646 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2647 | // COMPLEX_X87, it is passed in memory. |
| 2648 | case X87: |
| 2649 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2650 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2651 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2652 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2653 | |
| 2654 | case SSEUp: |
| 2655 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2656 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2657 | |
| 2658 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2659 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2660 | // and %r9 is used. |
| 2661 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2662 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2663 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2664 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2665 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2666 | |
| 2667 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2668 | // so that the parameter gets the right LLVM IR attributes. |
| 2669 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2670 | // Treat an enum type as its underlying type. |
| 2671 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2672 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2673 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2674 | if (Ty->isIntegralOrEnumerationType() && |
| 2675 | Ty->isPromotableIntegerType()) |
| 2676 | return ABIArgInfo::getExtend(); |
| 2677 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2678 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2679 | break; |
| 2680 | |
| 2681 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2682 | // available SSE register is used, the registers are taken in the |
| 2683 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2684 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2685 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2686 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2687 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2688 | break; |
| 2689 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2690 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2691 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2692 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2693 | switch (Hi) { |
| 2694 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2695 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2696 | // which is passed in memory. |
| 2697 | case Memory: |
| 2698 | case X87: |
| 2699 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2700 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2701 | |
| 2702 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2703 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2704 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2705 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2706 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2707 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2708 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2709 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2710 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2711 | break; |
| 2712 | |
| 2713 | // X87Up generally doesn't occur here (long double is passed in |
| 2714 | // memory), except in situations involving unions. |
| 2715 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2716 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2717 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2718 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2719 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2720 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2721 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2722 | ++neededSSE; |
| 2723 | break; |
| 2724 | |
| 2725 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2726 | // 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] | 2727 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2728 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2729 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2730 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2731 | break; |
| 2732 | } |
| 2733 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2734 | // If a high part was specified, merge it together with the low part. It is |
| 2735 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2736 | // first class struct aggregate with the high and low part: {low, high} |
| 2737 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2738 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2739 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2740 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2743 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2744 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2745 | if (!getCXXABI().classifyReturnType(FI)) |
| 2746 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2747 | |
| 2748 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2749 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2750 | |
| 2751 | // If the return value is indirect, then the hidden argument is consuming one |
| 2752 | // integer register. |
| 2753 | if (FI.getReturnInfo().isIndirect()) |
| 2754 | --freeIntRegs; |
| 2755 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 2756 | // The chain argument effectively gives us another free register. |
| 2757 | if (FI.isChainCall()) |
| 2758 | ++freeIntRegs; |
| 2759 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2760 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2761 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2762 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2763 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2764 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2765 | it != ie; ++it, ++ArgNo) { |
| 2766 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2767 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2768 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2769 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2770 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2771 | |
| 2772 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2773 | // eightbyte of an argument, the whole argument is passed on the |
| 2774 | // stack. If registers have already been assigned for some |
| 2775 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2776 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2777 | freeIntRegs -= neededInt; |
| 2778 | freeSSERegs -= neededSSE; |
| 2779 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2780 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2781 | } |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2786 | QualType Ty, |
| 2787 | CodeGenFunction &CGF) { |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 2788 | llvm::Value *overflow_arg_area_p = CGF.Builder.CreateStructGEP( |
| 2789 | nullptr, VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2790 | llvm::Value *overflow_arg_area = |
| 2791 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2792 | |
| 2793 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2794 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2795 | // It isn't stated explicitly in the standard, but in practice we use |
| 2796 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2797 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2798 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2799 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2800 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2801 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2802 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2803 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2804 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2805 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2806 | overflow_arg_area = |
| 2807 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2808 | overflow_arg_area->getType(), |
| 2809 | "overflow_arg_area.align"); |
| 2810 | } |
| 2811 | |
| 2812 | // 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] | 2813 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2814 | llvm::Value *Res = |
| 2815 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2816 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2817 | |
| 2818 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2819 | // l->overflow_arg_area + sizeof(type). |
| 2820 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2821 | // an 8 byte boundary. |
| 2822 | |
| 2823 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2824 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2825 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2826 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2827 | "overflow_arg_area.next"); |
| 2828 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2829 | |
| 2830 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2831 | return Res; |
| 2832 | } |
| 2833 | |
| 2834 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2835 | CodeGenFunction &CGF) const { |
| 2836 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2837 | // struct { |
| 2838 | // i32 gp_offset; |
| 2839 | // i32 fp_offset; |
| 2840 | // i8* overflow_arg_area; |
| 2841 | // i8* reg_save_area; |
| 2842 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2843 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2844 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2845 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2846 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2847 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2848 | |
| 2849 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2850 | // in the registers. If not go to step 7. |
| 2851 | if (!neededInt && !neededSSE) |
| 2852 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2853 | |
| 2854 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2855 | // general purpose registers needed to pass type and num_fp to hold |
| 2856 | // the number of floating point registers needed. |
| 2857 | |
| 2858 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2859 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2860 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2861 | // |
| 2862 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2863 | // register save space). |
| 2864 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2865 | llvm::Value *InRegs = nullptr; |
| 2866 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2867 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2868 | if (neededInt) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2869 | gp_offset_p = |
| 2870 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2871 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2872 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2873 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | if (neededSSE) { |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2877 | fp_offset_p = |
| 2878 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2879 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2880 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2881 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2882 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2883 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2884 | } |
| 2885 | |
| 2886 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2887 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2888 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2889 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2890 | |
| 2891 | // Emit code to load the value if it was passed in registers. |
| 2892 | |
| 2893 | CGF.EmitBlock(InRegBlock); |
| 2894 | |
| 2895 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2896 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2897 | // copying to a temporary location in case the parameter is passed |
| 2898 | // in different register classes or requires an alignment greater |
| 2899 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2900 | // |
| 2901 | // FIXME: This really results in shameful code when we end up needing to |
| 2902 | // collect arguments from different places; often what should result in a |
| 2903 | // simple assembling of a structure from scattered addresses has many more |
| 2904 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2905 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2906 | llvm::Value *RegAddr = CGF.Builder.CreateLoad( |
| 2907 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3), "reg_save_area"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2908 | if (neededInt && neededSSE) { |
| 2909 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2910 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2911 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2912 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2913 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2914 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2915 | llvm::Type *TyLo = ST->getElementType(0); |
| 2916 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2917 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2918 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2919 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2920 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2921 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2922 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2923 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2924 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2925 | llvm::Value *V = |
| 2926 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2927 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2928 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2929 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2930 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2931 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2932 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2933 | } else if (neededInt) { |
| 2934 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2935 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2936 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2937 | |
| 2938 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2939 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2940 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2941 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2942 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2943 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2944 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2945 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2946 | RegAddr = Tmp; |
| 2947 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2948 | } else if (neededSSE == 1) { |
| 2949 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2950 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2951 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2952 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2953 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2954 | // SSE registers are spaced 16 bytes apart in the register save |
| 2955 | // area, we need to collect the two eightbytes together. |
| 2956 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2957 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2958 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2959 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2960 | llvm::PointerType::getUnqual(DoubleTy); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2961 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2962 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2963 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2964 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2965 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2966 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 0)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2967 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2968 | DblPtrTy)); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 2969 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(ST, Tmp, 1)); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2970 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2971 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2975 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2976 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2977 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2978 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2979 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2980 | gp_offset_p); |
| 2981 | } |
| 2982 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2983 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2984 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2985 | fp_offset_p); |
| 2986 | } |
| 2987 | CGF.EmitBranch(ContBlock); |
| 2988 | |
| 2989 | // Emit code to load the value if it was passed in memory. |
| 2990 | |
| 2991 | CGF.EmitBlock(InMemBlock); |
| 2992 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2993 | |
| 2994 | // Return the appropriate result. |
| 2995 | |
| 2996 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2997 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2998 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2999 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 3000 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3001 | return ResAddr; |
| 3002 | } |
| 3003 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3004 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 3005 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3006 | |
| 3007 | if (Ty->isVoidType()) |
| 3008 | return ABIArgInfo::getIgnore(); |
| 3009 | |
| 3010 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3011 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3012 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3013 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3014 | uint64_t Width = Info.Width; |
| 3015 | unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3016 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3017 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3018 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3019 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3020 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3021 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 3022 | } |
| 3023 | |
| 3024 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3025 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3026 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3027 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3028 | if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3029 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3030 | Width)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3031 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3032 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3033 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3034 | // other targets. |
| 3035 | const Type *Base = nullptr; |
| 3036 | uint64_t NumElts = 0; |
| 3037 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3038 | if (FreeSSERegs >= NumElts) { |
| 3039 | FreeSSERegs -= NumElts; |
| 3040 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3041 | return ABIArgInfo::getDirect(); |
| 3042 | return ABIArgInfo::getExpand(); |
| 3043 | } |
| 3044 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3045 | } |
| 3046 | |
| 3047 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3048 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3049 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3050 | // directly. |
| 3051 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3052 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3053 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3056 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3057 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3058 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3059 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3060 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3061 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3062 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3063 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3066 | // Bool type is always extended to the ABI, other builtin types are not |
| 3067 | // extended. |
| 3068 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3069 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3070 | return ABIArgInfo::getExtend(); |
| 3071 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3072 | return ABIArgInfo::getDirect(); |
| 3073 | } |
| 3074 | |
| 3075 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3076 | bool IsVectorCall = |
| 3077 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3078 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3079 | // We can use up to 4 SSE return registers with vectorcall. |
| 3080 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3081 | if (!getCXXABI().classifyReturnType(FI)) |
| 3082 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3083 | |
| 3084 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3085 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3086 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3087 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3088 | } |
| 3089 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3090 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3091 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3092 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3093 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3094 | CGBuilderTy &Builder = CGF.Builder; |
| 3095 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 3096 | "ap"); |
| 3097 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3098 | llvm::Type *PTy = |
| 3099 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3100 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3101 | |
| 3102 | uint64_t Offset = |
| 3103 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 3104 | llvm::Value *NextAddr = |
| 3105 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3106 | "ap.next"); |
| 3107 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3108 | |
| 3109 | return AddrTyped; |
| 3110 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3111 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3112 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3113 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3114 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3115 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3116 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3117 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3118 | |
| 3119 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3120 | CodeGenFunction &CGF) const override; |
| 3121 | }; |
| 3122 | |
| 3123 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3124 | public: |
| 3125 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3126 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3127 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3128 | // This is recovered from gcc output. |
| 3129 | return 1; // r1 is the dedicated stack pointer |
| 3130 | } |
| 3131 | |
| 3132 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3133 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3134 | |
| 3135 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3136 | return 16; // Natural alignment for Altivec vectors. |
| 3137 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3138 | }; |
| 3139 | |
| 3140 | } |
| 3141 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3142 | llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3143 | QualType Ty, |
| 3144 | CodeGenFunction &CGF) const { |
| 3145 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3146 | // TODO: Implement this. For now ignore. |
| 3147 | (void)CTy; |
| 3148 | return nullptr; |
| 3149 | } |
| 3150 | |
| 3151 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
| 3152 | bool isInt = Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
| 3153 | llvm::Type *CharPtr = CGF.Int8PtrTy; |
| 3154 | llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy; |
| 3155 | |
| 3156 | CGBuilderTy &Builder = CGF.Builder; |
| 3157 | llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr"); |
| 3158 | llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty); |
| 3159 | llvm::Value *FPRPtrAsInt = Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1)); |
| 3160 | llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr); |
| 3161 | llvm::Value *OverflowAreaPtrAsInt = Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3)); |
| 3162 | llvm::Value *OverflowAreaPtr = Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr); |
| 3163 | llvm::Value *RegsaveAreaPtrAsInt = Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4)); |
| 3164 | llvm::Value *RegsaveAreaPtr = Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr); |
| 3165 | llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr"); |
| 3166 | // Align GPR when TY is i64. |
| 3167 | if (isI64) { |
| 3168 | llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1)); |
| 3169 | llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1)); |
| 3170 | llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1)); |
| 3171 | GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR); |
| 3172 | } |
| 3173 | llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr"); |
| 3174 | llvm::Value *OverflowArea = Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area"); |
| 3175 | llvm::Value *OverflowAreaAsInt = Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty); |
| 3176 | llvm::Value *RegsaveArea = Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area"); |
| 3177 | llvm::Value *RegsaveAreaAsInt = Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty); |
| 3178 | |
| 3179 | llvm::Value *CC = Builder.CreateICmpULT(isInt ? GPR : FPR, |
| 3180 | Builder.getInt8(8), "cond"); |
| 3181 | |
| 3182 | llvm::Value *RegConstant = Builder.CreateMul(isInt ? GPR : FPR, |
| 3183 | Builder.getInt8(isInt ? 4 : 8)); |
| 3184 | |
| 3185 | llvm::Value *OurReg = Builder.CreateAdd(RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty)); |
| 3186 | |
| 3187 | if (Ty->isFloatingType()) |
| 3188 | OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32)); |
| 3189 | |
| 3190 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3191 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3192 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3193 | |
| 3194 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3195 | |
| 3196 | CGF.EmitBlock(UsingRegs); |
| 3197 | |
| 3198 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3199 | llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy); |
| 3200 | // Increase the GPR/FPR indexes. |
| 3201 | if (isInt) { |
| 3202 | GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1)); |
| 3203 | Builder.CreateStore(GPR, GPRPtr); |
| 3204 | } else { |
| 3205 | FPR = Builder.CreateAdd(FPR, Builder.getInt8(1)); |
| 3206 | Builder.CreateStore(FPR, FPRPtr); |
| 3207 | } |
| 3208 | CGF.EmitBranch(Cont); |
| 3209 | |
| 3210 | CGF.EmitBlock(UsingOverflow); |
| 3211 | |
| 3212 | // Increase the overflow area. |
| 3213 | llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy); |
| 3214 | OverflowAreaAsInt = Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8)); |
| 3215 | Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), OverflowAreaPtr); |
| 3216 | CGF.EmitBranch(Cont); |
| 3217 | |
| 3218 | CGF.EmitBlock(Cont); |
| 3219 | |
| 3220 | llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr"); |
| 3221 | Result->addIncoming(Result1, UsingRegs); |
| 3222 | Result->addIncoming(Result2, UsingOverflow); |
| 3223 | |
| 3224 | if (Ty->isAggregateType()) { |
| 3225 | llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr") ; |
| 3226 | return Builder.CreateLoad(AGGPtr, false, "aggr"); |
| 3227 | } |
| 3228 | |
| 3229 | return Result; |
| 3230 | } |
| 3231 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3232 | bool |
| 3233 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3234 | llvm::Value *Address) const { |
| 3235 | // This is calculated from the LLVM and GCC tables and verified |
| 3236 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3237 | |
| 3238 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3239 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3240 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3241 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3242 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3243 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3244 | |
| 3245 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3246 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3247 | |
| 3248 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3249 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3250 | |
| 3251 | // 64-76 are various 4-byte special-purpose registers: |
| 3252 | // 64: mq |
| 3253 | // 65: lr |
| 3254 | // 66: ctr |
| 3255 | // 67: ap |
| 3256 | // 68-75 cr0-7 |
| 3257 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3258 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3259 | |
| 3260 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3261 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3262 | |
| 3263 | // 109: vrsave |
| 3264 | // 110: vscr |
| 3265 | // 111: spe_acc |
| 3266 | // 112: spefscr |
| 3267 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3268 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3269 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3270 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3273 | // PowerPC-64 |
| 3274 | |
| 3275 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3276 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3277 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3278 | public: |
| 3279 | enum ABIKind { |
| 3280 | ELFv1 = 0, |
| 3281 | ELFv2 |
| 3282 | }; |
| 3283 | |
| 3284 | private: |
| 3285 | static const unsigned GPRBits = 64; |
| 3286 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3287 | bool HasQPX; |
| 3288 | |
| 3289 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3290 | // will be passed in a QPX register. |
| 3291 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3292 | if (!HasQPX) |
| 3293 | return false; |
| 3294 | |
| 3295 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3296 | unsigned NumElements = VT->getNumElements(); |
| 3297 | if (NumElements == 1) |
| 3298 | return false; |
| 3299 | |
| 3300 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3301 | if (getContext().getTypeSize(Ty) <= 256) |
| 3302 | return true; |
| 3303 | } else if (VT->getElementType()-> |
| 3304 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3305 | if (getContext().getTypeSize(Ty) <= 128) |
| 3306 | return true; |
| 3307 | } |
| 3308 | } |
| 3309 | |
| 3310 | return false; |
| 3311 | } |
| 3312 | |
| 3313 | bool IsQPXVectorTy(QualType Ty) const { |
| 3314 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3315 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3316 | |
| 3317 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3318 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3319 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3320 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3321 | bool isPromotableTypeForABI(QualType Ty) const; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3322 | bool isAlignedParamType(QualType Ty, bool &Align32) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3323 | |
| 3324 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3325 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3326 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3327 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3328 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3329 | uint64_t Members) const override; |
| 3330 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3331 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3332 | // Example: For aggregate arguments that fit in a register, we could |
| 3333 | // use getDirectInReg (as is done below for structs containing a single |
| 3334 | // floating-point value) to avoid pushing them to memory on function |
| 3335 | // entry. This would require changing the logic in PPCISelLowering |
| 3336 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3337 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3338 | if (!getCXXABI().classifyReturnType(FI)) |
| 3339 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3340 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3341 | // We rely on the default argument classification for the most part. |
| 3342 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3343 | // 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] | 3344 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3345 | if (T) { |
| 3346 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3347 | if (IsQPXVectorTy(T) || |
| 3348 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3349 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3350 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3351 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3352 | continue; |
| 3353 | } |
| 3354 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3355 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3356 | } |
| 3357 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3358 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3359 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3360 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3361 | }; |
| 3362 | |
| 3363 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3364 | bool HasQPX; |
| 3365 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3366 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3367 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3368 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
| 3369 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)), |
| 3370 | HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3371 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3372 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3373 | // This is recovered from gcc output. |
| 3374 | return 1; // r1 is the dedicated stack pointer |
| 3375 | } |
| 3376 | |
| 3377 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3378 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3379 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3380 | unsigned getOpenMPSimdDefaultAlignment(QualType QT) const override { |
| 3381 | if (HasQPX) |
| 3382 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 3383 | if (PT->getPointeeType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 3384 | return 32; // Natural alignment for QPX doubles. |
| 3385 | |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3386 | return 16; // Natural alignment for Altivec and VSX vectors. |
| 3387 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3388 | }; |
| 3389 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3390 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3391 | public: |
| 3392 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3393 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3394 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3395 | // This is recovered from gcc output. |
| 3396 | return 1; // r1 is the dedicated stack pointer |
| 3397 | } |
| 3398 | |
| 3399 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3400 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3401 | |
| 3402 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3403 | return 16; // Natural alignment for Altivec vectors. |
| 3404 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3405 | }; |
| 3406 | |
| 3407 | } |
| 3408 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3409 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3410 | // extended to 64 bits. |
| 3411 | bool |
| 3412 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3413 | // Treat an enum type as its underlying type. |
| 3414 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3415 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3416 | |
| 3417 | // Promotable integer types are required to be promoted by the ABI. |
| 3418 | if (Ty->isPromotableIntegerType()) |
| 3419 | return true; |
| 3420 | |
| 3421 | // In addition to the usual promotable integer types, we also need to |
| 3422 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3423 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3424 | switch (BT->getKind()) { |
| 3425 | case BuiltinType::Int: |
| 3426 | case BuiltinType::UInt: |
| 3427 | return true; |
| 3428 | default: |
| 3429 | break; |
| 3430 | } |
| 3431 | |
| 3432 | return false; |
| 3433 | } |
| 3434 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3435 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3436 | /// alignment in the parameter area. |
| 3437 | bool |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3438 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty, bool &Align32) const { |
| 3439 | Align32 = false; |
| 3440 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3441 | // Complex types are passed just like their elements. |
| 3442 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3443 | Ty = CTy->getElementType(); |
| 3444 | |
| 3445 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3446 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3447 | if (IsQPXVectorTy(Ty)) { |
| 3448 | if (getContext().getTypeSize(Ty) > 128) |
| 3449 | Align32 = true; |
| 3450 | |
| 3451 | return true; |
| 3452 | } else if (Ty->isVectorType()) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3453 | return getContext().getTypeSize(Ty) == 128; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3454 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3455 | |
| 3456 | // For single-element float/vector structs, we consider the whole type |
| 3457 | // to have the same alignment requirements as its single element. |
| 3458 | const Type *AlignAsType = nullptr; |
| 3459 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3460 | if (EltType) { |
| 3461 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3462 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3463 | getContext().getTypeSize(EltType) == 128) || |
| 3464 | (BT && BT->isFloatingPoint())) |
| 3465 | AlignAsType = EltType; |
| 3466 | } |
| 3467 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3468 | // Likewise for ELFv2 homogeneous aggregates. |
| 3469 | const Type *Base = nullptr; |
| 3470 | uint64_t Members = 0; |
| 3471 | if (!AlignAsType && Kind == ELFv2 && |
| 3472 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3473 | AlignAsType = Base; |
| 3474 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3475 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3476 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3477 | if (getContext().getTypeSize(AlignAsType) > 128) |
| 3478 | Align32 = true; |
| 3479 | |
| 3480 | return true; |
| 3481 | } else if (AlignAsType) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3482 | return AlignAsType->isVectorType(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3483 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3484 | |
| 3485 | // Otherwise, we only need alignment for any aggregate type that |
| 3486 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3487 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3488 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
| 3489 | Align32 = true; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3490 | return true; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3491 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3492 | |
| 3493 | return false; |
| 3494 | } |
| 3495 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3496 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3497 | /// aggregate. Base is set to the base element type, and Members is set |
| 3498 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3499 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3500 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3501 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3502 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3503 | if (NElements == 0) |
| 3504 | return false; |
| 3505 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3506 | return false; |
| 3507 | Members *= NElements; |
| 3508 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3509 | const RecordDecl *RD = RT->getDecl(); |
| 3510 | if (RD->hasFlexibleArrayMember()) |
| 3511 | return false; |
| 3512 | |
| 3513 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3514 | |
| 3515 | // If this is a C++ record, check the bases first. |
| 3516 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3517 | for (const auto &I : CXXRD->bases()) { |
| 3518 | // Ignore empty records. |
| 3519 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3520 | continue; |
| 3521 | |
| 3522 | uint64_t FldMembers; |
| 3523 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3524 | return false; |
| 3525 | |
| 3526 | Members += FldMembers; |
| 3527 | } |
| 3528 | } |
| 3529 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3530 | for (const auto *FD : RD->fields()) { |
| 3531 | // Ignore (non-zero arrays of) empty records. |
| 3532 | QualType FT = FD->getType(); |
| 3533 | while (const ConstantArrayType *AT = |
| 3534 | getContext().getAsConstantArrayType(FT)) { |
| 3535 | if (AT->getSize().getZExtValue() == 0) |
| 3536 | return false; |
| 3537 | FT = AT->getElementType(); |
| 3538 | } |
| 3539 | if (isEmptyRecord(getContext(), FT, true)) |
| 3540 | continue; |
| 3541 | |
| 3542 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3543 | if (getContext().getLangOpts().CPlusPlus && |
| 3544 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3545 | continue; |
| 3546 | |
| 3547 | uint64_t FldMembers; |
| 3548 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3549 | return false; |
| 3550 | |
| 3551 | Members = (RD->isUnion() ? |
| 3552 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3553 | } |
| 3554 | |
| 3555 | if (!Base) |
| 3556 | return false; |
| 3557 | |
| 3558 | // Ensure there is no padding. |
| 3559 | if (getContext().getTypeSize(Base) * Members != |
| 3560 | getContext().getTypeSize(Ty)) |
| 3561 | return false; |
| 3562 | } else { |
| 3563 | Members = 1; |
| 3564 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3565 | Members = 2; |
| 3566 | Ty = CT->getElementType(); |
| 3567 | } |
| 3568 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3569 | // Most ABIs only support float, double, and some vector type widths. |
| 3570 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3571 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3572 | |
| 3573 | // The base type must be the same for all members. Types that |
| 3574 | // agree in both total size and mode (float vs. vector) are |
| 3575 | // treated as being equivalent here. |
| 3576 | const Type *TyPtr = Ty.getTypePtr(); |
| 3577 | if (!Base) |
| 3578 | Base = TyPtr; |
| 3579 | |
| 3580 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3581 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3582 | return false; |
| 3583 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3584 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3585 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3586 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3587 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3588 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3589 | // double, long double, or 128-bit vectors. |
| 3590 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3591 | if (BT->getKind() == BuiltinType::Float || |
| 3592 | BT->getKind() == BuiltinType::Double || |
| 3593 | BT->getKind() == BuiltinType::LongDouble) |
| 3594 | return true; |
| 3595 | } |
| 3596 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3597 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3598 | return true; |
| 3599 | } |
| 3600 | return false; |
| 3601 | } |
| 3602 | |
| 3603 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3604 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3605 | // Vector types require one register, floating point types require one |
| 3606 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3607 | uint32_t NumRegs = |
| 3608 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3609 | |
| 3610 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3611 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3614 | ABIArgInfo |
| 3615 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3616 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3617 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3618 | if (Ty->isAnyComplexType()) |
| 3619 | return ABIArgInfo::getDirect(); |
| 3620 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3621 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3622 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3623 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3624 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3625 | if (Size > 128) |
| 3626 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3627 | else if (Size < 128) { |
| 3628 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3629 | return ABIArgInfo::getDirect(CoerceTy); |
| 3630 | } |
| 3631 | } |
| 3632 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3633 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3634 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3635 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3636 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3637 | bool Align32; |
| 3638 | uint64_t ABIAlign = isAlignedParamType(Ty, Align32) ? |
| 3639 | (Align32 ? 32 : 16) : 8; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3640 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3641 | |
| 3642 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3643 | const Type *Base = nullptr; |
| 3644 | uint64_t Members = 0; |
| 3645 | if (Kind == ELFv2 && |
| 3646 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3647 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3648 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3649 | return ABIArgInfo::getDirect(CoerceTy); |
| 3650 | } |
| 3651 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3652 | // If an aggregate may end up fully in registers, we do not |
| 3653 | // use the ByVal method, but pass the aggregate as array. |
| 3654 | // This is usually beneficial since we avoid forcing the |
| 3655 | // back-end to store the argument to memory. |
| 3656 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3657 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3658 | llvm::Type *CoerceTy; |
| 3659 | |
| 3660 | // Types up to 8 bytes are passed as integer type (which will be |
| 3661 | // properly aligned in the argument save area doubleword). |
| 3662 | if (Bits <= GPRBits) |
| 3663 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3664 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3665 | // Larger types are passed as arrays, with the base type selected |
| 3666 | // according to the required alignment in the save area. |
| 3667 | else { |
| 3668 | uint64_t RegBits = ABIAlign * 8; |
| 3669 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3670 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3671 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3672 | } |
| 3673 | |
| 3674 | return ABIArgInfo::getDirect(CoerceTy); |
| 3675 | } |
| 3676 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3677 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3678 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3679 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | return (isPromotableTypeForABI(Ty) ? |
| 3683 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3684 | } |
| 3685 | |
| 3686 | ABIArgInfo |
| 3687 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3688 | if (RetTy->isVoidType()) |
| 3689 | return ABIArgInfo::getIgnore(); |
| 3690 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3691 | if (RetTy->isAnyComplexType()) |
| 3692 | return ABIArgInfo::getDirect(); |
| 3693 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3694 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3695 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3696 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3697 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3698 | if (Size > 128) |
| 3699 | return ABIArgInfo::getIndirect(0); |
| 3700 | else if (Size < 128) { |
| 3701 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3702 | return ABIArgInfo::getDirect(CoerceTy); |
| 3703 | } |
| 3704 | } |
| 3705 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3706 | if (isAggregateTypeForABI(RetTy)) { |
| 3707 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3708 | const Type *Base = nullptr; |
| 3709 | uint64_t Members = 0; |
| 3710 | if (Kind == ELFv2 && |
| 3711 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3712 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3713 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3714 | return ABIArgInfo::getDirect(CoerceTy); |
| 3715 | } |
| 3716 | |
| 3717 | // ELFv2 small aggregates are returned in up to two registers. |
| 3718 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3719 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3720 | if (Bits == 0) |
| 3721 | return ABIArgInfo::getIgnore(); |
| 3722 | |
| 3723 | llvm::Type *CoerceTy; |
| 3724 | if (Bits > GPRBits) { |
| 3725 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3726 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3727 | } else |
| 3728 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3729 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3730 | return ABIArgInfo::getDirect(CoerceTy); |
| 3731 | } |
| 3732 | |
| 3733 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3734 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3735 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3736 | |
| 3737 | return (isPromotableTypeForABI(RetTy) ? |
| 3738 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3739 | } |
| 3740 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3741 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3742 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3743 | QualType Ty, |
| 3744 | CodeGenFunction &CGF) const { |
| 3745 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3746 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3747 | |
| 3748 | CGBuilderTy &Builder = CGF.Builder; |
| 3749 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3750 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3751 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3752 | // Handle types that require 16-byte alignment in the parameter save area. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3753 | bool Align32; |
| 3754 | if (isAlignedParamType(Ty, Align32)) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3755 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3756 | AddrAsInt = Builder.CreateAdd(AddrAsInt, |
| 3757 | Builder.getInt64(Align32 ? 31 : 15)); |
| 3758 | AddrAsInt = Builder.CreateAnd(AddrAsInt, |
| 3759 | Builder.getInt64(Align32 ? -32 : -16)); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3760 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3761 | } |
| 3762 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3763 | // Update the va_list pointer. The pointer should be bumped by the |
| 3764 | // size of the object. We can trust getTypeSize() except for a complex |
| 3765 | // type whose base type is smaller than a doubleword. For these, the |
| 3766 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3767 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3768 | QualType BaseTy; |
| 3769 | unsigned CplxBaseSize = 0; |
| 3770 | |
| 3771 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3772 | BaseTy = CTy->getElementType(); |
| 3773 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3774 | if (CplxBaseSize < 8) |
| 3775 | SizeInBytes = 16; |
| 3776 | } |
| 3777 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3778 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3779 | llvm::Value *NextAddr = |
| 3780 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3781 | "ap.next"); |
| 3782 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3783 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3784 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3785 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3786 | // in separate doublewords. However, Clang expects us to produce a |
| 3787 | // pointer to a structure with the two parts packed tightly. So generate |
| 3788 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3789 | // and store them to a temporary structure. |
| 3790 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3791 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3792 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3793 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 3794 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3795 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3796 | } else { |
| 3797 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3798 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3799 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3800 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3801 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3802 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3803 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 3804 | llvm::AllocaInst *Ptr = |
| 3805 | CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), "vacplx"); |
| 3806 | llvm::Value *RealPtr = |
| 3807 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 0, ".real"); |
| 3808 | llvm::Value *ImagPtr = |
| 3809 | Builder.CreateStructGEP(Ptr->getAllocatedType(), Ptr, 1, ".imag"); |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3810 | Builder.CreateStore(Real, RealPtr, false); |
| 3811 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3812 | return Ptr; |
| 3813 | } |
| 3814 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3815 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3816 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3817 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3818 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3819 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3820 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3821 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3822 | } |
| 3823 | |
| 3824 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3825 | return Builder.CreateBitCast(Addr, PTy); |
| 3826 | } |
| 3827 | |
| 3828 | static bool |
| 3829 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3830 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3831 | // This is calculated from the LLVM and GCC tables and verified |
| 3832 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3833 | |
| 3834 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3835 | |
| 3836 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3837 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3838 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3839 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3840 | |
| 3841 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3842 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3843 | |
| 3844 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3845 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3846 | |
| 3847 | // 64-76 are various 4-byte special-purpose registers: |
| 3848 | // 64: mq |
| 3849 | // 65: lr |
| 3850 | // 66: ctr |
| 3851 | // 67: ap |
| 3852 | // 68-75 cr0-7 |
| 3853 | // 76: xer |
| 3854 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3855 | |
| 3856 | // 77-108: v0-31, the 16-byte vector registers |
| 3857 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3858 | |
| 3859 | // 109: vrsave |
| 3860 | // 110: vscr |
| 3861 | // 111: spe_acc |
| 3862 | // 112: spefscr |
| 3863 | // 113: sfp |
| 3864 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3865 | |
| 3866 | return false; |
| 3867 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3868 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3869 | bool |
| 3870 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3871 | CodeGen::CodeGenFunction &CGF, |
| 3872 | llvm::Value *Address) const { |
| 3873 | |
| 3874 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3875 | } |
| 3876 | |
| 3877 | bool |
| 3878 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3879 | llvm::Value *Address) const { |
| 3880 | |
| 3881 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3882 | } |
| 3883 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3884 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3885 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3886 | //===----------------------------------------------------------------------===// |
| 3887 | |
| 3888 | namespace { |
| 3889 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3890 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3891 | public: |
| 3892 | enum ABIKind { |
| 3893 | AAPCS = 0, |
| 3894 | DarwinPCS |
| 3895 | }; |
| 3896 | |
| 3897 | private: |
| 3898 | ABIKind Kind; |
| 3899 | |
| 3900 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3901 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3902 | |
| 3903 | private: |
| 3904 | ABIKind getABIKind() const { return Kind; } |
| 3905 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3906 | |
| 3907 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3908 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3909 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3910 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3911 | uint64_t Members) const override; |
| 3912 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3913 | bool isIllegalVectorType(QualType Ty) const; |
| 3914 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 3915 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3916 | if (!getCXXABI().classifyReturnType(FI)) |
| 3917 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3918 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3919 | for (auto &it : FI.arguments()) |
| 3920 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3924 | CodeGenFunction &CGF) const; |
| 3925 | |
| 3926 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3927 | CodeGenFunction &CGF) const; |
| 3928 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3929 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3930 | CodeGenFunction &CGF) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3931 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3932 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3933 | } |
| 3934 | }; |
| 3935 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3936 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3937 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3938 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3939 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3940 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3941 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3942 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3943 | } |
| 3944 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3945 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 3946 | return 31; |
| 3947 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3948 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 3949 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3950 | }; |
| 3951 | } |
| 3952 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3953 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3954 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3955 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3956 | // Handle illegal vector types here. |
| 3957 | if (isIllegalVectorType(Ty)) { |
| 3958 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3959 | if (Size <= 32) { |
| 3960 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3961 | return ABIArgInfo::getDirect(ResType); |
| 3962 | } |
| 3963 | if (Size == 64) { |
| 3964 | llvm::Type *ResType = |
| 3965 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3966 | return ABIArgInfo::getDirect(ResType); |
| 3967 | } |
| 3968 | if (Size == 128) { |
| 3969 | llvm::Type *ResType = |
| 3970 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3971 | return ABIArgInfo::getDirect(ResType); |
| 3972 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3973 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3974 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3975 | |
| 3976 | if (!isAggregateTypeForABI(Ty)) { |
| 3977 | // Treat an enum type as its underlying type. |
| 3978 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3979 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3980 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3981 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 3982 | ? ABIArgInfo::getExtend() |
| 3983 | : ABIArgInfo::getDirect()); |
| 3984 | } |
| 3985 | |
| 3986 | // Structures with either a non-trivial destructor or a non-trivial |
| 3987 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3988 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3989 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3990 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 3994 | // elsewhere for GNU compatibility. |
| 3995 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3996 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 3997 | return ABIArgInfo::getIgnore(); |
| 3998 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3999 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 4000 | } |
| 4001 | |
| 4002 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4003 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4004 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4005 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4006 | return ABIArgInfo::getDirect( |
| 4007 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 4011 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4012 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4013 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4014 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4015 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4016 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4017 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4018 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4019 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4020 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4021 | } |
| 4022 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4023 | } |
| 4024 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4025 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4026 | } |
| 4027 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4028 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4029 | if (RetTy->isVoidType()) |
| 4030 | return ABIArgInfo::getIgnore(); |
| 4031 | |
| 4032 | // Large vector types should be returned via memory. |
| 4033 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 4034 | return ABIArgInfo::getIndirect(0); |
| 4035 | |
| 4036 | if (!isAggregateTypeForABI(RetTy)) { |
| 4037 | // Treat an enum type as its underlying type. |
| 4038 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4039 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4040 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4041 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4042 | ? ABIArgInfo::getExtend() |
| 4043 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4046 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4047 | return ABIArgInfo::getIgnore(); |
| 4048 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4049 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4050 | uint64_t Members = 0; |
| 4051 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4052 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4053 | return ABIArgInfo::getDirect(); |
| 4054 | |
| 4055 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4056 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4057 | if (Size <= 128) { |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4058 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
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 |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 4060 | |
| 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. |
| 4063 | if (Alignment < 128 && Size == 128) { |
| 4064 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4065 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4066 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4067 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4068 | } |
| 4069 | |
| 4070 | return ABIArgInfo::getIndirect(0); |
| 4071 | } |
| 4072 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4073 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4074 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4075 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4076 | // Check whether VT is legal. |
| 4077 | unsigned NumElements = VT->getNumElements(); |
| 4078 | uint64_t Size = getContext().getTypeSize(VT); |
| 4079 | // NumElements should be power of 2 between 1 and 16. |
| 4080 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4081 | return true; |
| 4082 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4083 | } |
| 4084 | return false; |
| 4085 | } |
| 4086 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4087 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4088 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4089 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4090 | // but with the difference that any floating-point type is allowed, |
| 4091 | // including __fp16. |
| 4092 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4093 | if (BT->isFloatingPoint()) |
| 4094 | return true; |
| 4095 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4096 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4097 | if (VecSize == 64 || VecSize == 128) |
| 4098 | return true; |
| 4099 | } |
| 4100 | return false; |
| 4101 | } |
| 4102 | |
| 4103 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4104 | uint64_t Members) const { |
| 4105 | return Members <= 4; |
| 4106 | } |
| 4107 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4108 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, |
| 4109 | QualType Ty, |
| 4110 | CodeGenFunction &CGF) const { |
| 4111 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4112 | bool IsIndirect = AI.isIndirect(); |
| 4113 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4114 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4115 | if (IsIndirect) |
| 4116 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4117 | else if (AI.getCoerceToType()) |
| 4118 | BaseTy = AI.getCoerceToType(); |
| 4119 | |
| 4120 | unsigned NumRegs = 1; |
| 4121 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4122 | BaseTy = ArrTy->getElementType(); |
| 4123 | NumRegs = ArrTy->getNumElements(); |
| 4124 | } |
| 4125 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4126 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4127 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4128 | // Standard, section B.4: |
| 4129 | // |
| 4130 | // struct { |
| 4131 | // void *__stack; |
| 4132 | // void *__gr_top; |
| 4133 | // void *__vr_top; |
| 4134 | // int __gr_offs; |
| 4135 | // int __vr_offs; |
| 4136 | // }; |
| 4137 | |
| 4138 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4139 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4140 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4141 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4142 | auto &Ctx = CGF.getContext(); |
| 4143 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4144 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4145 | int reg_top_index; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4146 | int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8; |
| 4147 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4148 | // 3 is the field number of __gr_offs |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4149 | reg_offs_p = |
| 4150 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4151 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4152 | reg_top_index = 1; // field number for __gr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4153 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4154 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4155 | // 4 is the field number of __vr_offs. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4156 | reg_offs_p = |
| 4157 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4158 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4159 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4160 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
| 4163 | //======================================= |
| 4164 | // Find out where argument was passed |
| 4165 | //======================================= |
| 4166 | |
| 4167 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4168 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4169 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4170 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4171 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4172 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4173 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4174 | |
| 4175 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4176 | |
| 4177 | // 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] | 4178 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4179 | CGF.EmitBlock(MaybeRegBlock); |
| 4180 | |
| 4181 | // Integer arguments may need to correct register alignment (for example a |
| 4182 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4183 | // align __gr_offs to calculate the potential address. |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4184 | if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4185 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4186 | |
| 4187 | reg_offs = CGF.Builder.CreateAdd( |
| 4188 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4189 | "align_regoffs"); |
| 4190 | reg_offs = CGF.Builder.CreateAnd( |
| 4191 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4192 | "aligned_regoffs"); |
| 4193 | } |
| 4194 | |
| 4195 | // 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] | 4196 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4197 | NewOffset = CGF.Builder.CreateAdd( |
| 4198 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4199 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4200 | |
| 4201 | // Now we're in a position to decide whether this argument really was in |
| 4202 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4203 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4204 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4205 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4206 | |
| 4207 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4208 | |
| 4209 | //======================================= |
| 4210 | // Argument was in registers |
| 4211 | //======================================= |
| 4212 | |
| 4213 | // Now we emit the code for if the argument was originally passed in |
| 4214 | // registers. First start the appropriate block: |
| 4215 | CGF.EmitBlock(InRegBlock); |
| 4216 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4217 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4218 | reg_top_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, reg_top_index, |
| 4219 | "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4220 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4221 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4222 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4223 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4224 | |
| 4225 | if (IsIndirect) { |
| 4226 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4227 | // stored registers or on the stack will actually be a struct **. |
| 4228 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4229 | } |
| 4230 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4231 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4232 | uint64_t NumMembers = 0; |
| 4233 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4234 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4235 | // Homogeneous aggregates passed in registers will have their elements split |
| 4236 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4237 | // qN+1, ...). We reload and store into a temporary local variable |
| 4238 | // contiguously. |
| 4239 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 4240 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4241 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4242 | llvm::AllocaInst *Tmp = CGF.CreateTempAlloca(HFATy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4243 | int Offset = 0; |
| 4244 | |
| 4245 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 4246 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 4247 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4248 | llvm::Value *BaseOffset = |
| 4249 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 4250 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4251 | LoadAddr = CGF.Builder.CreateBitCast( |
| 4252 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 4253 | llvm::Value *StoreAddr = |
| 4254 | CGF.Builder.CreateStructGEP(Tmp->getAllocatedType(), Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4255 | |
| 4256 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4257 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4258 | } |
| 4259 | |
| 4260 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4261 | } else { |
| 4262 | // Otherwise the object is contiguous in memory |
| 4263 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4264 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4265 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4266 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 4267 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 4268 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 4269 | |
| 4270 | BaseAddr = CGF.Builder.CreateAdd( |
| 4271 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4272 | |
| 4273 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 4274 | } |
| 4275 | |
| 4276 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4277 | } |
| 4278 | |
| 4279 | CGF.EmitBranch(ContBlock); |
| 4280 | |
| 4281 | //======================================= |
| 4282 | // Argument was on the stack |
| 4283 | //======================================= |
| 4284 | CGF.EmitBlock(OnStackBlock); |
| 4285 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4286 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 4287 | stack_p = CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 0, "stack_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4288 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4289 | |
| 4290 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4291 | // floating-point ones might be affected. |
| 4292 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 4293 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4294 | |
| 4295 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4296 | |
| 4297 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4298 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4299 | "align_stack"); |
| 4300 | OnStackAddr = CGF.Builder.CreateAnd( |
| 4301 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4302 | "align_stack"); |
| 4303 | |
| 4304 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4305 | } |
| 4306 | |
| 4307 | uint64_t StackSize; |
| 4308 | if (IsIndirect) |
| 4309 | StackSize = 8; |
| 4310 | else |
| 4311 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 4312 | |
| 4313 | // All stack slots are 8 bytes |
| 4314 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4315 | |
| 4316 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4317 | llvm::Value *NewStack = |
| 4318 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 4319 | |
| 4320 | // Write the new value of __stack for the next call to va_arg |
| 4321 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4322 | |
| 4323 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4324 | Ctx.getTypeSize(Ty) < 64) { |
| 4325 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 4326 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4327 | |
| 4328 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4329 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4330 | |
| 4331 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4332 | } |
| 4333 | |
| 4334 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4335 | |
| 4336 | CGF.EmitBranch(ContBlock); |
| 4337 | |
| 4338 | //======================================= |
| 4339 | // Tidy up |
| 4340 | //======================================= |
| 4341 | CGF.EmitBlock(ContBlock); |
| 4342 | |
| 4343 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4344 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4345 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4346 | |
| 4347 | if (IsIndirect) |
| 4348 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4349 | |
| 4350 | return ResAddr; |
| 4351 | } |
| 4352 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4353 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4354 | CodeGenFunction &CGF) const { |
| 4355 | // We do not support va_arg for aggregates or illegal vector types. |
| 4356 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 4357 | // other cases. |
| 4358 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4359 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4360 | |
| 4361 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 4362 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 4363 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4364 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4365 | uint64_t Members = 0; |
| 4366 | bool isHA = isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4367 | |
| 4368 | bool isIndirect = false; |
| 4369 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 4370 | // be passed indirectly. |
| 4371 | if (Size > 16 && !isHA) { |
| 4372 | isIndirect = true; |
| 4373 | Size = 8; |
| 4374 | Align = 8; |
| 4375 | } |
| 4376 | |
| 4377 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 4378 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 4379 | |
| 4380 | CGBuilderTy &Builder = CGF.Builder; |
| 4381 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4382 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4383 | |
| 4384 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4385 | // These are ignored for parameter passing purposes. |
| 4386 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4387 | return Builder.CreateBitCast(Addr, PTy); |
| 4388 | } |
| 4389 | |
| 4390 | const uint64_t MinABIAlign = 8; |
| 4391 | if (Align > MinABIAlign) { |
| 4392 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 4393 | Addr = Builder.CreateGEP(Addr, Offset); |
| 4394 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 4395 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 4396 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 4397 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 4398 | } |
| 4399 | |
| 4400 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 4401 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 4402 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 4403 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4404 | |
| 4405 | if (isIndirect) |
| 4406 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 4407 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4408 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4409 | |
| 4410 | return AddrTyped; |
| 4411 | } |
| 4412 | |
| 4413 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4414 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4415 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4416 | |
| 4417 | namespace { |
| 4418 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4419 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4420 | public: |
| 4421 | enum ABIKind { |
| 4422 | APCS = 0, |
| 4423 | AAPCS = 1, |
| 4424 | AAPCS_VFP |
| 4425 | }; |
| 4426 | |
| 4427 | private: |
| 4428 | ABIKind Kind; |
| 4429 | |
| 4430 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4431 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4432 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4433 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4434 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4435 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4436 | switch (getTarget().getTriple().getEnvironment()) { |
| 4437 | case llvm::Triple::Android: |
| 4438 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4439 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4440 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4441 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4442 | return true; |
| 4443 | default: |
| 4444 | return false; |
| 4445 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4448 | bool isEABIHF() const { |
| 4449 | switch (getTarget().getTriple().getEnvironment()) { |
| 4450 | case llvm::Triple::EABIHF: |
| 4451 | case llvm::Triple::GNUEABIHF: |
| 4452 | return true; |
| 4453 | default: |
| 4454 | return false; |
| 4455 | } |
| 4456 | } |
| 4457 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4458 | ABIKind getABIKind() const { return Kind; } |
| 4459 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4460 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4461 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4462 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4463 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4464 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4465 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4466 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4467 | uint64_t Members) const override; |
| 4468 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4469 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4470 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4471 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4472 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4473 | |
| 4474 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4475 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4476 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4477 | }; |
| 4478 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4479 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4480 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4481 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4482 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4483 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4484 | const ARMABIInfo &getABIInfo() const { |
| 4485 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4486 | } |
| 4487 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4488 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4489 | return 13; |
| 4490 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4491 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4492 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4493 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4494 | } |
| 4495 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4496 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4497 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4498 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4499 | |
| 4500 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4501 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4502 | return false; |
| 4503 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4504 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4505 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4506 | if (getABIInfo().isEABI()) return 88; |
| 4507 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4508 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4509 | |
| 4510 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4511 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4512 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4513 | if (!FD) |
| 4514 | return; |
| 4515 | |
| 4516 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4517 | if (!Attr) |
| 4518 | return; |
| 4519 | |
| 4520 | const char *Kind; |
| 4521 | switch (Attr->getInterrupt()) { |
| 4522 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4523 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4524 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4525 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4526 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4527 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4528 | } |
| 4529 | |
| 4530 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4531 | |
| 4532 | Fn->addFnAttr("interrupt", Kind); |
| 4533 | |
| 4534 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4535 | return; |
| 4536 | |
| 4537 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4538 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4539 | // the backend to perform a realignment as part of the function prologue. |
| 4540 | llvm::AttrBuilder B; |
| 4541 | B.addStackAlignmentAttr(8); |
| 4542 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4543 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4544 | llvm::AttributeSet::FunctionIndex, |
| 4545 | B)); |
| 4546 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4547 | }; |
| 4548 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4549 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4550 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4551 | CodeGen::CodeGenModule &CGM) const; |
| 4552 | |
| 4553 | public: |
| 4554 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4555 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4556 | |
| 4557 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4558 | CodeGen::CodeGenModule &CGM) const override; |
| 4559 | }; |
| 4560 | |
| 4561 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4562 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4563 | if (!isa<FunctionDecl>(D)) |
| 4564 | return; |
| 4565 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4566 | return; |
| 4567 | |
| 4568 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4569 | F->addFnAttr("stack-probe-size", |
| 4570 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4571 | } |
| 4572 | |
| 4573 | void WindowsARMTargetCodeGenInfo::SetTargetAttributes( |
| 4574 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4575 | ARMTargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 4576 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4577 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4580 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4581 | if (!getCXXABI().classifyReturnType(FI)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4582 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4583 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4584 | for (auto &I : FI.arguments()) |
| 4585 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4586 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4587 | // Always honor user-specified calling convention. |
| 4588 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4589 | return; |
| 4590 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4591 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4592 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4593 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4594 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4595 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4596 | /// Return the default calling convention that LLVM will use. |
| 4597 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4598 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4599 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4600 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4601 | else if (isEABI()) |
| 4602 | return llvm::CallingConv::ARM_AAPCS; |
| 4603 | else |
| 4604 | return llvm::CallingConv::ARM_APCS; |
| 4605 | } |
| 4606 | |
| 4607 | /// Return the calling convention that our ABI would like us to use |
| 4608 | /// as the C calling convention. |
| 4609 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4610 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4611 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4612 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4613 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4614 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4615 | llvm_unreachable("bad ABI kind"); |
| 4616 | } |
| 4617 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4618 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4619 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4620 | |
| 4621 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4622 | // they'd just match what LLVM will infer from the triple. |
| 4623 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4624 | if (abiCC != getLLVMDefaultCC()) |
| 4625 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4626 | |
| 4627 | BuiltinCC = (getABIKind() == APCS ? |
| 4628 | llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4631 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4632 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4633 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4634 | // A single-precision floating-point type (including promoted |
| 4635 | // half-precision types); A double-precision floating-point type; |
| 4636 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4637 | // with a Base Type of a single- or double-precision floating-point type, |
| 4638 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4639 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4640 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4641 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4642 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4643 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4644 | // Handle illegal vector types here. |
| 4645 | if (isIllegalVectorType(Ty)) { |
| 4646 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4647 | if (Size <= 32) { |
| 4648 | llvm::Type *ResType = |
| 4649 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4650 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4651 | } |
| 4652 | if (Size == 64) { |
| 4653 | llvm::Type *ResType = llvm::VectorType::get( |
| 4654 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4655 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4656 | } |
| 4657 | if (Size == 128) { |
| 4658 | llvm::Type *ResType = llvm::VectorType::get( |
| 4659 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4660 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4661 | } |
| 4662 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4663 | } |
| 4664 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4665 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4666 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4667 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4668 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4669 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4670 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4671 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4672 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4673 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4674 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4675 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4676 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4677 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4678 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4679 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4680 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4681 | return ABIArgInfo::getIgnore(); |
| 4682 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4683 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4684 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4685 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4686 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4687 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4688 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4689 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4690 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4691 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4692 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4695 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4696 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4697 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4698 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4699 | uint64_t ABIAlign = 4; |
| 4700 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4701 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4702 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4703 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4704 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4705 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4706 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4707 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4710 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4711 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4712 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4713 | // FIXME: Try to match the types of the arguments more accurately where |
| 4714 | // we can. |
| 4715 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4716 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4717 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4718 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4719 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4720 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4721 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4722 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4723 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4726 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4727 | llvm::LLVMContext &VMContext) { |
| 4728 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4729 | // is called integer-like if its size is less than or equal to one word, and |
| 4730 | // the offset of each of its addressable sub-fields is zero. |
| 4731 | |
| 4732 | uint64_t Size = Context.getTypeSize(Ty); |
| 4733 | |
| 4734 | // Check that the type fits in a word. |
| 4735 | if (Size > 32) |
| 4736 | return false; |
| 4737 | |
| 4738 | // FIXME: Handle vector types! |
| 4739 | if (Ty->isVectorType()) |
| 4740 | return false; |
| 4741 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4742 | // Float types are never treated as "integer like". |
| 4743 | if (Ty->isRealFloatingType()) |
| 4744 | return false; |
| 4745 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4746 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4747 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4748 | return true; |
| 4749 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4750 | // Small complex integer types are "integer like". |
| 4751 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4752 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4753 | |
| 4754 | // Single element and zero sized arrays should be allowed, by the definition |
| 4755 | // above, but they are not. |
| 4756 | |
| 4757 | // Otherwise, it must be a record type. |
| 4758 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4759 | if (!RT) return false; |
| 4760 | |
| 4761 | // Ignore records with flexible arrays. |
| 4762 | const RecordDecl *RD = RT->getDecl(); |
| 4763 | if (RD->hasFlexibleArrayMember()) |
| 4764 | return false; |
| 4765 | |
| 4766 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4767 | // like". |
| 4768 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4769 | |
| 4770 | bool HadField = false; |
| 4771 | unsigned idx = 0; |
| 4772 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4773 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4774 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4775 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4776 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4777 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4778 | // struct { int : 0; int x } |
| 4779 | // is non-integer like according to gcc. |
| 4780 | if (FD->isBitField()) { |
| 4781 | if (!RD->isUnion()) |
| 4782 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4783 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4784 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4785 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4786 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4787 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4790 | // Check if this field is at offset 0. |
| 4791 | if (Layout.getFieldOffset(idx) != 0) |
| 4792 | return false; |
| 4793 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4794 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4795 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4796 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4797 | // Only allow at most one field in a structure. This doesn't match the |
| 4798 | // wording above, but follows gcc in situations with a field following an |
| 4799 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4800 | if (!RD->isUnion()) { |
| 4801 | if (HadField) |
| 4802 | return false; |
| 4803 | |
| 4804 | HadField = true; |
| 4805 | } |
| 4806 | } |
| 4807 | |
| 4808 | return true; |
| 4809 | } |
| 4810 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4811 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4812 | bool isVariadic) const { |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4813 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4814 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4815 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4816 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4817 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4818 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4819 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4820 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4821 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4822 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4823 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4824 | // Treat an enum type as its underlying type. |
| 4825 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4826 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4827 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4828 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4829 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4830 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4831 | |
| 4832 | // Are we following APCS? |
| 4833 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4834 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4835 | return ABIArgInfo::getIgnore(); |
| 4836 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4837 | // Complex types are all returned as packed integers. |
| 4838 | // |
| 4839 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4840 | // correctly. |
| 4841 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4842 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 4843 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4844 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4845 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4846 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4847 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4848 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4849 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4850 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4851 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4852 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4853 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | // Otherwise return in memory. |
| 4857 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4858 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4859 | |
| 4860 | // Otherwise this is an AAPCS variant. |
| 4861 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4862 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4863 | return ABIArgInfo::getIgnore(); |
| 4864 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4865 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4866 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4867 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4868 | uint64_t Members; |
| 4869 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4870 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4871 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4872 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4873 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4876 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4877 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4878 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4879 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4880 | if (getDataLayout().isBigEndian()) |
| 4881 | // 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] | 4882 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4883 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4884 | // Return in the smallest viable integer type. |
| 4885 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4886 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4887 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4888 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4889 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4892 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4895 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4896 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4897 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4898 | // Check whether VT is legal. |
| 4899 | unsigned NumElements = VT->getNumElements(); |
| 4900 | uint64_t Size = getContext().getTypeSize(VT); |
| 4901 | // NumElements should be power of 2. |
| 4902 | if ((NumElements & (NumElements - 1)) != 0) |
| 4903 | return true; |
| 4904 | // Size should be greater than 32 bits. |
| 4905 | return Size <= 32; |
| 4906 | } |
| 4907 | return false; |
| 4908 | } |
| 4909 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4910 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4911 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4912 | // double, or 64-bit or 128-bit vectors. |
| 4913 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4914 | if (BT->getKind() == BuiltinType::Float || |
| 4915 | BT->getKind() == BuiltinType::Double || |
| 4916 | BT->getKind() == BuiltinType::LongDouble) |
| 4917 | return true; |
| 4918 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4919 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4920 | if (VecSize == 64 || VecSize == 128) |
| 4921 | return true; |
| 4922 | } |
| 4923 | return false; |
| 4924 | } |
| 4925 | |
| 4926 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4927 | uint64_t Members) const { |
| 4928 | return Members <= 4; |
| 4929 | } |
| 4930 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4931 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4932 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4933 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4934 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4935 | |
| 4936 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4937 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4938 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4939 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4940 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4941 | // These are ignored for parameter passing purposes. |
| 4942 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4943 | return Builder.CreateBitCast(Addr, PTy); |
| 4944 | } |
| 4945 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4946 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4947 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4948 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4949 | |
| 4950 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4951 | // 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] | 4952 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4953 | getABIKind() == ARMABIInfo::AAPCS) |
| 4954 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4955 | else |
| 4956 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4957 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4958 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4959 | IsIndirect = true; |
| 4960 | Size = 4; |
| 4961 | TyAlign = 4; |
| 4962 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4963 | |
| 4964 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4965 | if (TyAlign > 4) { |
| 4966 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 4967 | "Alignment is not power of 2!"); |
| 4968 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 4969 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 4970 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4971 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4972 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4973 | |
| 4974 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4975 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4976 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4977 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4978 | "ap.next"); |
| 4979 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4980 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4981 | if (IsIndirect) |
| 4982 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4983 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4984 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 4985 | // may not be correctly aligned for the vector type. We create an aligned |
| 4986 | // temporary space and copy the content over from ap.cur to the temporary |
| 4987 | // space. This is necessary if the natural alignment of the type is greater |
| 4988 | // than the ABI alignment. |
| 4989 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 4990 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 4991 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 4992 | "var.align"); |
| 4993 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 4994 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 4995 | Builder.CreateMemCpy(Dst, Src, |
| 4996 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 4997 | TyAlign, false); |
| 4998 | Addr = AlignedTemp; //The content is in aligned location. |
| 4999 | } |
| 5000 | llvm::Type *PTy = |
| 5001 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5002 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5003 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5004 | return AddrTyped; |
| 5005 | } |
| 5006 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5007 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5008 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5009 | //===----------------------------------------------------------------------===// |
| 5010 | |
| 5011 | namespace { |
| 5012 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5013 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5014 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5015 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5016 | |
| 5017 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5018 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 5019 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5020 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5021 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5022 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5023 | }; |
| 5024 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5025 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5026 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5027 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5028 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5029 | |
| 5030 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5031 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5032 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5033 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5034 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5035 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5036 | }; |
| 5037 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5038 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5039 | if (RetTy->isVoidType()) |
| 5040 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5041 | |
| 5042 | // note: this is different from default ABI |
| 5043 | if (!RetTy->isScalarType()) |
| 5044 | return ABIArgInfo::getDirect(); |
| 5045 | |
| 5046 | // Treat an enum type as its underlying type. |
| 5047 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5048 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5049 | |
| 5050 | return (RetTy->isPromotableIntegerType() ? |
| 5051 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5054 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5055 | // Treat an enum type as its underlying type. |
| 5056 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5057 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5058 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5059 | // Return aggregates type as indirect by value |
| 5060 | if (isAggregateTypeForABI(Ty)) |
| 5061 | return ABIArgInfo::getIndirect(0, /* byval */ true); |
| 5062 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5063 | return (Ty->isPromotableIntegerType() ? |
| 5064 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5067 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5068 | if (!getCXXABI().classifyReturnType(FI)) |
| 5069 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5070 | for (auto &I : FI.arguments()) |
| 5071 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5072 | |
| 5073 | // Always honor user-specified calling convention. |
| 5074 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5075 | return; |
| 5076 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5077 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5078 | } |
| 5079 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5080 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5081 | CodeGenFunction &CFG) const { |
| 5082 | llvm_unreachable("NVPTX does not support varargs"); |
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 | void NVPTXTargetCodeGenInfo:: |
| 5086 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5087 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5088 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5089 | if (!FD) return; |
| 5090 | |
| 5091 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5092 | |
| 5093 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5094 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5095 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5096 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5097 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5098 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5099 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5100 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5101 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5102 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5103 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5104 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5105 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5106 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5107 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5108 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5109 | // __global__ functions cannot be called from the device, we do not |
| 5110 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5111 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5112 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5113 | addNVVMMetadata(F, "kernel", 1); |
| 5114 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5115 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5116 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 5117 | llvm::APSInt MaxThreads(32); |
| 5118 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 5119 | if (MaxThreads > 0) |
| 5120 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 5121 | |
| 5122 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 5123 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 5124 | // we don't have to add a PTX directive. |
| 5125 | if (Attr->getMinBlocks()) { |
| 5126 | llvm::APSInt MinBlocks(32); |
| 5127 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 5128 | if (MinBlocks > 0) |
| 5129 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5130 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5131 | } |
| 5132 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5136 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5137 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5138 | llvm::Module *M = F->getParent(); |
| 5139 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5140 | |
| 5141 | // Get "nvvm.annotations" metadata node |
| 5142 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5143 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5144 | llvm::Metadata *MDVals[] = { |
| 5145 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5146 | llvm::ConstantAsMetadata::get( |
| 5147 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5148 | // Append metadata to nvvm.annotations |
| 5149 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5150 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5154 | // SystemZ ABI Implementation |
| 5155 | //===----------------------------------------------------------------------===// |
| 5156 | |
| 5157 | namespace { |
| 5158 | |
| 5159 | class SystemZABIInfo : public ABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5160 | bool HasVector; |
| 5161 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5162 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5163 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
| 5164 | : ABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5165 | |
| 5166 | bool isPromotableIntegerType(QualType Ty) const; |
| 5167 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5168 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5169 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5170 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5171 | |
| 5172 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5173 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5174 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5175 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5176 | if (!getCXXABI().classifyReturnType(FI)) |
| 5177 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5178 | for (auto &I : FI.arguments()) |
| 5179 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5180 | } |
| 5181 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5182 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5183 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5184 | }; |
| 5185 | |
| 5186 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5187 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5188 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 5189 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5190 | }; |
| 5191 | |
| 5192 | } |
| 5193 | |
| 5194 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5195 | // Treat an enum type as its underlying type. |
| 5196 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5197 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5198 | |
| 5199 | // Promotable integer types are required to be promoted by the ABI. |
| 5200 | if (Ty->isPromotableIntegerType()) |
| 5201 | return true; |
| 5202 | |
| 5203 | // 32-bit values must also be promoted. |
| 5204 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5205 | switch (BT->getKind()) { |
| 5206 | case BuiltinType::Int: |
| 5207 | case BuiltinType::UInt: |
| 5208 | return true; |
| 5209 | default: |
| 5210 | return false; |
| 5211 | } |
| 5212 | return false; |
| 5213 | } |
| 5214 | |
| 5215 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5216 | return (Ty->isAnyComplexType() || |
| 5217 | Ty->isVectorType() || |
| 5218 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5221 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 5222 | return (HasVector && |
| 5223 | Ty->isVectorType() && |
| 5224 | getContext().getTypeSize(Ty) <= 128); |
| 5225 | } |
| 5226 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5227 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5228 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5229 | switch (BT->getKind()) { |
| 5230 | case BuiltinType::Float: |
| 5231 | case BuiltinType::Double: |
| 5232 | return true; |
| 5233 | default: |
| 5234 | return false; |
| 5235 | } |
| 5236 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5237 | return false; |
| 5238 | } |
| 5239 | |
| 5240 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5241 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5242 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5243 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5244 | |
| 5245 | // If this is a C++ record, check the bases first. |
| 5246 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5247 | for (const auto &I : CXXRD->bases()) { |
| 5248 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5249 | |
| 5250 | // Empty bases don't affect things either way. |
| 5251 | if (isEmptyRecord(getContext(), Base, true)) |
| 5252 | continue; |
| 5253 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5254 | if (!Found.isNull()) |
| 5255 | return Ty; |
| 5256 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
| 5259 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5260 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5261 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5262 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5263 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5264 | if (getContext().getLangOpts().CPlusPlus && |
| 5265 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5266 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5267 | |
| 5268 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5269 | // Nested structures still do though. |
| 5270 | if (!Found.isNull()) |
| 5271 | return Ty; |
| 5272 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5273 | } |
| 5274 | |
| 5275 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5276 | // 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] | 5277 | if (!Found.isNull()) |
| 5278 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5281 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5282 | } |
| 5283 | |
| 5284 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5285 | CodeGenFunction &CGF) const { |
| 5286 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5287 | // struct { |
| 5288 | // i64 __gpr; |
| 5289 | // i64 __fpr; |
| 5290 | // i8 *__overflow_arg_area; |
| 5291 | // i8 *__reg_save_area; |
| 5292 | // }; |
| 5293 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5294 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 5295 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 5296 | // always passed on the stack. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5297 | Ty = CGF.getContext().getCanonicalType(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5298 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
| 5299 | llvm::Type *APTy = llvm::PointerType::getUnqual(ArgTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5300 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5301 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5302 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5303 | bool IsVector = false; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5304 | unsigned UnpaddedBitSize; |
| 5305 | if (IsIndirect) { |
| 5306 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5307 | UnpaddedBitSize = 64; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5308 | } else { |
| 5309 | if (AI.getCoerceToType()) |
| 5310 | ArgTy = AI.getCoerceToType(); |
| 5311 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5312 | IsVector = ArgTy->isVectorTy(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5313 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5314 | } |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5315 | unsigned PaddedBitSize = (IsVector && UnpaddedBitSize > 64) ? 128 : 64; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5316 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5317 | |
| 5318 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5319 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5320 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5321 | llvm::Type *IndexTy = CGF.Int64Ty; |
| 5322 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5323 | |
| 5324 | if (IsVector) { |
| 5325 | // Work out the address of a vector argument on the stack. |
| 5326 | // Vector arguments are always passed in the high bits of a |
| 5327 | // single (8 byte) or double (16 byte) stack slot. |
| 5328 | llvm::Value *OverflowArgAreaPtr = |
| 5329 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 2, |
| 5330 | "overflow_arg_area_ptr"); |
| 5331 | llvm::Value *OverflowArgArea = |
| 5332 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5333 | llvm::Value *MemAddr = |
| 5334 | CGF.Builder.CreateBitCast(OverflowArgArea, APTy, "mem_addr"); |
| 5335 | |
| 5336 | // Update overflow_arg_area_ptr pointer |
| 5337 | llvm::Value *NewOverflowArgArea = |
| 5338 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5339 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5340 | |
| 5341 | return MemAddr; |
| 5342 | } |
| 5343 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5344 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5345 | if (InFPRs) { |
| 5346 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5347 | RegCountField = 1; // __fpr |
| 5348 | RegSaveIndex = 16; // save offset for f0 |
| 5349 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5350 | } else { |
| 5351 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5352 | RegCountField = 0; // __gpr |
| 5353 | RegSaveIndex = 2; // save offset for r2 |
| 5354 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5355 | } |
| 5356 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5357 | llvm::Value *RegCountPtr = CGF.Builder.CreateStructGEP( |
| 5358 | nullptr, VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5359 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5360 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5361 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5362 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5363 | |
| 5364 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5365 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5366 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5367 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5368 | |
| 5369 | // Emit code to load the value if it was passed in registers. |
| 5370 | CGF.EmitBlock(InRegBlock); |
| 5371 | |
| 5372 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5373 | llvm::Value *ScaledRegCount = |
| 5374 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5375 | llvm::Value *RegBase = |
| 5376 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5377 | llvm::Value *RegOffset = |
| 5378 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5379 | llvm::Value *RegSaveAreaPtr = |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5380 | CGF.Builder.CreateStructGEP(nullptr, VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5381 | llvm::Value *RegSaveArea = |
| 5382 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5383 | llvm::Value *RawRegAddr = |
| 5384 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5385 | llvm::Value *RegAddr = |
| 5386 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5387 | |
| 5388 | // Update the register count |
| 5389 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5390 | llvm::Value *NewRegCount = |
| 5391 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5392 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5393 | CGF.EmitBranch(ContBlock); |
| 5394 | |
| 5395 | // Emit code to load the value if it was passed in memory. |
| 5396 | CGF.EmitBlock(InMemBlock); |
| 5397 | |
| 5398 | // Work out the address of a stack argument. |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 5399 | llvm::Value *OverflowArgAreaPtr = CGF.Builder.CreateStructGEP( |
| 5400 | nullptr, VAListAddr, 2, "overflow_arg_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5401 | llvm::Value *OverflowArgArea = |
| 5402 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5403 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5404 | llvm::Value *RawMemAddr = |
| 5405 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5406 | llvm::Value *MemAddr = |
| 5407 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5408 | |
| 5409 | // Update overflow_arg_area_ptr pointer |
| 5410 | llvm::Value *NewOverflowArgArea = |
| 5411 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5412 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5413 | CGF.EmitBranch(ContBlock); |
| 5414 | |
| 5415 | // Return the appropriate result. |
| 5416 | CGF.EmitBlock(ContBlock); |
| 5417 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5418 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5419 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5420 | |
| 5421 | if (IsIndirect) |
| 5422 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5423 | |
| 5424 | return ResAddr; |
| 5425 | } |
| 5426 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5427 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5428 | if (RetTy->isVoidType()) |
| 5429 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5430 | if (isVectorArgumentType(RetTy)) |
| 5431 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5432 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5433 | return ABIArgInfo::getIndirect(0); |
| 5434 | return (isPromotableIntegerType(RetTy) ? |
| 5435 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5436 | } |
| 5437 | |
| 5438 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5439 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5440 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5441 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5442 | |
| 5443 | // Integers and enums are extended to full register width. |
| 5444 | if (isPromotableIntegerType(Ty)) |
| 5445 | return ABIArgInfo::getExtend(); |
| 5446 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5447 | // Handle vector types and vector-like structure types. Note that |
| 5448 | // as opposed to float-like structure types, we do not allow any |
| 5449 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5450 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5451 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 5452 | if (isVectorArgumentType(SingleElementTy) && |
| 5453 | getContext().getTypeSize(SingleElementTy) == Size) |
| 5454 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 5455 | |
| 5456 | // 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] | 5457 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5458 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5459 | |
| 5460 | // Handle small structures. |
| 5461 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5462 | // Structures with flexible arrays have variable length, so really |
| 5463 | // fail the size test above. |
| 5464 | const RecordDecl *RD = RT->getDecl(); |
| 5465 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5466 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5467 | |
| 5468 | // The structure is passed as an unextended integer, a float, or a double. |
| 5469 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 5470 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5471 | assert(Size == 32 || Size == 64); |
| 5472 | if (Size == 32) |
| 5473 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5474 | else |
| 5475 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5476 | } else |
| 5477 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5478 | return ABIArgInfo::getDirect(PassTy); |
| 5479 | } |
| 5480 | |
| 5481 | // Non-structure compounds are passed indirectly. |
| 5482 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5483 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5484 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5485 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5489 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5490 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5491 | |
| 5492 | namespace { |
| 5493 | |
| 5494 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5495 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5496 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5497 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5498 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5499 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5500 | }; |
| 5501 | |
| 5502 | } |
| 5503 | |
| 5504 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5505 | llvm::GlobalValue *GV, |
| 5506 | CodeGen::CodeGenModule &M) const { |
| 5507 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5508 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5509 | // Handle 'interrupt' attribute: |
| 5510 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5511 | |
| 5512 | // Step 1: Set ISR calling convention. |
| 5513 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5514 | |
| 5515 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5516 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5517 | |
| 5518 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5519 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5520 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5521 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5522 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5523 | } |
| 5524 | } |
| 5525 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5526 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5527 | // MIPS ABI Implementation. This works for both little-endian and |
| 5528 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5529 | //===----------------------------------------------------------------------===// |
| 5530 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5531 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5532 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5533 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5534 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5535 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5536 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5537 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5538 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5539 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5540 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5541 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5542 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5543 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5544 | |
| 5545 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5546 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5547 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5548 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5549 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5550 | }; |
| 5551 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5552 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5553 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5554 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5555 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5556 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5557 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5558 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5559 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5560 | return 29; |
| 5561 | } |
| 5562 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5563 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5564 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5565 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5566 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5567 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5568 | if (FD->hasAttr<Mips16Attr>()) { |
| 5569 | Fn->addFnAttr("mips16"); |
| 5570 | } |
| 5571 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5572 | Fn->addFnAttr("nomips16"); |
| 5573 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5574 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5575 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5576 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5577 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5578 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5579 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5580 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5581 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5582 | }; |
| 5583 | } |
| 5584 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5585 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5586 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5587 | llvm::IntegerType *IntTy = |
| 5588 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5589 | |
| 5590 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5591 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5592 | ArgList.push_back(IntTy); |
| 5593 | |
| 5594 | // If necessary, add one more integer type to ArgList. |
| 5595 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5596 | |
| 5597 | if (R) |
| 5598 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5599 | } |
| 5600 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5601 | // In N32/64, an aligned double precision floating point field is passed in |
| 5602 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5603 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5604 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5605 | |
| 5606 | if (IsO32) { |
| 5607 | CoerceToIntArgs(TySize, ArgList); |
| 5608 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5609 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5610 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5611 | if (Ty->isComplexType()) |
| 5612 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5613 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5614 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5615 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5616 | // Unions/vectors are passed in integer registers. |
| 5617 | if (!RT || !RT->isStructureOrClassType()) { |
| 5618 | CoerceToIntArgs(TySize, ArgList); |
| 5619 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5620 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5621 | |
| 5622 | const RecordDecl *RD = RT->getDecl(); |
| 5623 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5624 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5625 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5626 | uint64_t LastOffset = 0; |
| 5627 | unsigned idx = 0; |
| 5628 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5629 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5630 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5631 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5632 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5633 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5634 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5635 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5636 | |
| 5637 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5638 | continue; |
| 5639 | |
| 5640 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5641 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5642 | continue; |
| 5643 | |
| 5644 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5645 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5646 | ArgList.push_back(I64); |
| 5647 | |
| 5648 | // Add double type. |
| 5649 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5650 | LastOffset = Offset + 64; |
| 5651 | } |
| 5652 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5653 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5654 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5655 | |
| 5656 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5657 | } |
| 5658 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5659 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5660 | uint64_t Offset) const { |
| 5661 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5662 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5663 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5664 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5665 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5666 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5667 | ABIArgInfo |
| 5668 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5669 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5670 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5671 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5672 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5673 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5674 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5675 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5676 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5677 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5678 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5679 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5680 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5681 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5682 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5683 | return ABIArgInfo::getIgnore(); |
| 5684 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5685 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5686 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5687 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5688 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5689 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5690 | // If we have reached here, aggregates are passed directly by coercing to |
| 5691 | // another structure type. Padding is inserted if the offset of the |
| 5692 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 5693 | ABIArgInfo ArgInfo = |
| 5694 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 5695 | getPaddingType(OrigOffset, CurrOffset)); |
| 5696 | ArgInfo.setInReg(true); |
| 5697 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
| 5700 | // Treat an enum type as its underlying type. |
| 5701 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5702 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5703 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 5704 | // All integral types are promoted to the GPR width. |
| 5705 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5706 | return ABIArgInfo::getExtend(); |
| 5707 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5708 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5709 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5710 | } |
| 5711 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5712 | llvm::Type* |
| 5713 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5714 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5715 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5716 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5717 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5718 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5719 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5720 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5721 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5722 | // N32/64 returns struct/classes in floating point registers if the |
| 5723 | // following conditions are met: |
| 5724 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5725 | // 2. The struct/class has one or two fields all of which are floating |
| 5726 | // point types. |
| 5727 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5728 | // |
| 5729 | // Any other composite results are returned in integer registers. |
| 5730 | // |
| 5731 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5732 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5733 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5734 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5735 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5736 | if (!BT || !BT->isFloatingPoint()) |
| 5737 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5738 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5739 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
| 5742 | if (b == e) |
| 5743 | return llvm::StructType::get(getVMContext(), RTList, |
| 5744 | RD->hasAttr<PackedAttr>()); |
| 5745 | |
| 5746 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5747 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5748 | } |
| 5749 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5750 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5751 | return llvm::StructType::get(getVMContext(), RTList); |
| 5752 | } |
| 5753 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5754 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5755 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5756 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5757 | if (RetTy->isVoidType()) |
| 5758 | return ABIArgInfo::getIgnore(); |
| 5759 | |
| 5760 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5761 | // However, N32/N64 ignores zero sized return values. |
| 5762 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5763 | return ABIArgInfo::getIgnore(); |
| 5764 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5765 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5766 | if (Size <= 128) { |
| 5767 | if (RetTy->isAnyComplexType()) |
| 5768 | return ABIArgInfo::getDirect(); |
| 5769 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5770 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 5771 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5772 | if (!IsO32 || |
| 5773 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 5774 | ABIArgInfo ArgInfo = |
| 5775 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5776 | ArgInfo.setInReg(true); |
| 5777 | return ArgInfo; |
| 5778 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5779 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5780 | |
| 5781 | return ABIArgInfo::getIndirect(0); |
| 5782 | } |
| 5783 | |
| 5784 | // Treat an enum type as its underlying type. |
| 5785 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5786 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5787 | |
| 5788 | return (RetTy->isPromotableIntegerType() ? |
| 5789 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5790 | } |
| 5791 | |
| 5792 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5793 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5794 | if (!getCXXABI().classifyReturnType(FI)) |
| 5795 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5796 | |
| 5797 | // 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] | 5798 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5799 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5800 | for (auto &I : FI.arguments()) |
| 5801 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
| 5804 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5805 | CodeGenFunction &CGF) const { |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5806 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5807 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5808 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5809 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 5810 | // 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] | 5811 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5812 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
| 5813 | if ((Ty->isIntegerType() && |
| 5814 | CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) || |
| 5815 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5816 | Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 5817 | Ty->isSignedIntegerType()); |
| 5818 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5819 | |
| 5820 | CGBuilderTy &Builder = CGF.Builder; |
| 5821 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5822 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Daniel Sanders | 8d36a61 | 2014-09-22 13:27:06 +0000 | [diff] [blame] | 5823 | int64_t TypeAlign = |
| 5824 | std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5825 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5826 | llvm::Value *AddrTyped; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5827 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
| 5828 | |
| 5829 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 5830 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5831 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5832 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5833 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
| 5834 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5835 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5836 | } |
| 5837 | else |
| 5838 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5839 | |
| 5840 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
| 5841 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5842 | unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty); |
| 5843 | uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5844 | llvm::Value *NextAddr = |
| 5845 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
| 5846 | "ap.next"); |
| 5847 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5848 | |
| 5849 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5852 | bool |
| 5853 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5854 | llvm::Value *Address) const { |
| 5855 | // This information comes from gcc's implementation, which seems to |
| 5856 | // as canonical as it gets. |
| 5857 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5858 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5859 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5860 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5861 | |
| 5862 | // 0-31 are the general purpose registers, $0 - $31. |
| 5863 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5864 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5865 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5866 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5867 | |
| 5868 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5869 | // They are one bit wide and ignored here. |
| 5870 | |
| 5871 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5872 | // (coprocessor 1 is the FP unit) |
| 5873 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5874 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5875 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5876 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5877 | return false; |
| 5878 | } |
| 5879 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5880 | //===----------------------------------------------------------------------===// |
| 5881 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5882 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5883 | // handling. |
| 5884 | //===----------------------------------------------------------------------===// |
| 5885 | |
| 5886 | namespace { |
| 5887 | |
| 5888 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5889 | public: |
| 5890 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5891 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5892 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5893 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5894 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5895 | }; |
| 5896 | |
| 5897 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5898 | llvm::GlobalValue *GV, |
| 5899 | CodeGen::CodeGenModule &M) const { |
| 5900 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5901 | if (!FD) return; |
| 5902 | |
| 5903 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5904 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5905 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5906 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5907 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5908 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5909 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5910 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5911 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5912 | llvm::LLVMContext &Context = F->getContext(); |
| 5913 | llvm::NamedMDNode *OpenCLMetadata = |
| 5914 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5915 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5916 | SmallVector<llvm::Metadata *, 5> Operands; |
| 5917 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5918 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5919 | Operands.push_back( |
| 5920 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5921 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 5922 | Operands.push_back( |
| 5923 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5924 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 5925 | Operands.push_back( |
| 5926 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5927 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5928 | |
| 5929 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5930 | // for implementing the work_group_size_hint attr later. Currently |
| 5931 | // always true as the hint is not yet implemented. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5932 | Operands.push_back( |
| 5933 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5934 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5935 | } |
| 5936 | } |
| 5937 | } |
| 5938 | } |
| 5939 | |
| 5940 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5941 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5942 | //===----------------------------------------------------------------------===// |
| 5943 | // Hexagon ABI Implementation |
| 5944 | //===----------------------------------------------------------------------===// |
| 5945 | |
| 5946 | namespace { |
| 5947 | |
| 5948 | class HexagonABIInfo : public ABIInfo { |
| 5949 | |
| 5950 | |
| 5951 | public: |
| 5952 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5953 | |
| 5954 | private: |
| 5955 | |
| 5956 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5957 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5958 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5959 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5960 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5961 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5962 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5963 | }; |
| 5964 | |
| 5965 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5966 | public: |
| 5967 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5968 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5969 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5970 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5971 | return 29; |
| 5972 | } |
| 5973 | }; |
| 5974 | |
| 5975 | } |
| 5976 | |
| 5977 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5978 | if (!getCXXABI().classifyReturnType(FI)) |
| 5979 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5980 | for (auto &I : FI.arguments()) |
| 5981 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5982 | } |
| 5983 | |
| 5984 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5985 | if (!isAggregateTypeForABI(Ty)) { |
| 5986 | // Treat an enum type as its underlying type. |
| 5987 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5988 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5989 | |
| 5990 | return (Ty->isPromotableIntegerType() ? |
| 5991 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5992 | } |
| 5993 | |
| 5994 | // Ignore empty records. |
| 5995 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5996 | return ABIArgInfo::getIgnore(); |
| 5997 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5998 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5999 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6000 | |
| 6001 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6002 | if (Size > 64) |
| 6003 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6004 | // Pass in the smallest viable integer type. |
| 6005 | else if (Size > 32) |
| 6006 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6007 | else if (Size > 16) |
| 6008 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6009 | else if (Size > 8) |
| 6010 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6011 | else |
| 6012 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6013 | } |
| 6014 | |
| 6015 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 6016 | if (RetTy->isVoidType()) |
| 6017 | return ABIArgInfo::getIgnore(); |
| 6018 | |
| 6019 | // Large vector types should be returned via memory. |
| 6020 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 6021 | return ABIArgInfo::getIndirect(0); |
| 6022 | |
| 6023 | if (!isAggregateTypeForABI(RetTy)) { |
| 6024 | // Treat an enum type as its underlying type. |
| 6025 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6026 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6027 | |
| 6028 | return (RetTy->isPromotableIntegerType() ? |
| 6029 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 6030 | } |
| 6031 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6032 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 6033 | return ABIArgInfo::getIgnore(); |
| 6034 | |
| 6035 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 6036 | // are returned indirectly. |
| 6037 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 6038 | if (Size <= 64) { |
| 6039 | // Return in the smallest viable integer type. |
| 6040 | if (Size <= 8) |
| 6041 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 6042 | if (Size <= 16) |
| 6043 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6044 | if (Size <= 32) |
| 6045 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 6046 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 6047 | } |
| 6048 | |
| 6049 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 6050 | } |
| 6051 | |
| 6052 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6053 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6054 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6055 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6056 | |
| 6057 | CGBuilderTy &Builder = CGF.Builder; |
| 6058 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 6059 | "ap"); |
| 6060 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6061 | llvm::Type *PTy = |
| 6062 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 6063 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 6064 | |
| 6065 | uint64_t Offset = |
| 6066 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 6067 | llvm::Value *NextAddr = |
| 6068 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 6069 | "ap.next"); |
| 6070 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 6071 | |
| 6072 | return AddrTyped; |
| 6073 | } |
| 6074 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6075 | //===----------------------------------------------------------------------===// |
| 6076 | // AMDGPU ABI Implementation |
| 6077 | //===----------------------------------------------------------------------===// |
| 6078 | |
| 6079 | namespace { |
| 6080 | |
| 6081 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6082 | public: |
| 6083 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6084 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| 6085 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 6086 | CodeGen::CodeGenModule &M) const override; |
| 6087 | }; |
| 6088 | |
| 6089 | } |
| 6090 | |
| 6091 | void AMDGPUTargetCodeGenInfo::SetTargetAttributes( |
| 6092 | const Decl *D, |
| 6093 | llvm::GlobalValue *GV, |
| 6094 | CodeGen::CodeGenModule &M) const { |
| 6095 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 6096 | if (!FD) |
| 6097 | return; |
| 6098 | |
| 6099 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6100 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6101 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6102 | if (NumVGPR != 0) |
| 6103 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6104 | } |
| 6105 | |
| 6106 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6107 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6108 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6109 | if (NumSGPR != 0) |
| 6110 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6111 | } |
| 6112 | } |
| 6113 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6114 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6115 | //===----------------------------------------------------------------------===// |
| 6116 | // SPARC v9 ABI Implementation. |
| 6117 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6118 | // |
| 6119 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6120 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6121 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6122 | // |
| 6123 | // One case requires special care: |
| 6124 | // |
| 6125 | // struct mixed { |
| 6126 | // int i; |
| 6127 | // float f; |
| 6128 | // }; |
| 6129 | // |
| 6130 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6131 | // parameter array, but the int is passed in an integer register, and the float |
| 6132 | // is passed in a floating point register. This is represented as two arguments |
| 6133 | // with the LLVM IR inreg attribute: |
| 6134 | // |
| 6135 | // declare void f(i32 inreg %i, float inreg %f) |
| 6136 | // |
| 6137 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6138 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6139 | // bytes. |
| 6140 | // |
| 6141 | namespace { |
| 6142 | class SparcV9ABIInfo : public ABIInfo { |
| 6143 | public: |
| 6144 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6145 | |
| 6146 | private: |
| 6147 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6148 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6149 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6150 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6151 | |
| 6152 | // Coercion type builder for structs passed in registers. The coercion type |
| 6153 | // serves two purposes: |
| 6154 | // |
| 6155 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6156 | // in registers. |
| 6157 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6158 | // code generator knows to pass them in floating point registers. |
| 6159 | // |
| 6160 | // We also compute the InReg flag which indicates that the struct contains |
| 6161 | // aligned 32-bit floats. |
| 6162 | // |
| 6163 | struct CoerceBuilder { |
| 6164 | llvm::LLVMContext &Context; |
| 6165 | const llvm::DataLayout &DL; |
| 6166 | SmallVector<llvm::Type*, 8> Elems; |
| 6167 | uint64_t Size; |
| 6168 | bool InReg; |
| 6169 | |
| 6170 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6171 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6172 | |
| 6173 | // Pad Elems with integers until Size is ToSize. |
| 6174 | void pad(uint64_t ToSize) { |
| 6175 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6176 | if (ToSize == Size) |
| 6177 | return; |
| 6178 | |
| 6179 | // Finish the current 64-bit word. |
| 6180 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6181 | if (Aligned > Size && Aligned <= ToSize) { |
| 6182 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6183 | Size = Aligned; |
| 6184 | } |
| 6185 | |
| 6186 | // Add whole 64-bit words. |
| 6187 | while (Size + 64 <= ToSize) { |
| 6188 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6189 | Size += 64; |
| 6190 | } |
| 6191 | |
| 6192 | // Final in-word padding. |
| 6193 | if (Size < ToSize) { |
| 6194 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6195 | Size = ToSize; |
| 6196 | } |
| 6197 | } |
| 6198 | |
| 6199 | // Add a floating point element at Offset. |
| 6200 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6201 | // Unaligned floats are treated as integers. |
| 6202 | if (Offset % Bits) |
| 6203 | return; |
| 6204 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6205 | if (Bits < 64) |
| 6206 | InReg = true; |
| 6207 | pad(Offset); |
| 6208 | Elems.push_back(Ty); |
| 6209 | Size = Offset + Bits; |
| 6210 | } |
| 6211 | |
| 6212 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6213 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6214 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6215 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6216 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6217 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6218 | switch (ElemTy->getTypeID()) { |
| 6219 | case llvm::Type::StructTyID: |
| 6220 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6221 | break; |
| 6222 | case llvm::Type::FloatTyID: |
| 6223 | addFloat(ElemOffset, ElemTy, 32); |
| 6224 | break; |
| 6225 | case llvm::Type::DoubleTyID: |
| 6226 | addFloat(ElemOffset, ElemTy, 64); |
| 6227 | break; |
| 6228 | case llvm::Type::FP128TyID: |
| 6229 | addFloat(ElemOffset, ElemTy, 128); |
| 6230 | break; |
| 6231 | case llvm::Type::PointerTyID: |
| 6232 | if (ElemOffset % 64 == 0) { |
| 6233 | pad(ElemOffset); |
| 6234 | Elems.push_back(ElemTy); |
| 6235 | Size += 64; |
| 6236 | } |
| 6237 | break; |
| 6238 | default: |
| 6239 | break; |
| 6240 | } |
| 6241 | } |
| 6242 | } |
| 6243 | |
| 6244 | // Check if Ty is a usable substitute for the coercion type. |
| 6245 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6246 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
| 6249 | // Get the coercion type as a literal struct type. |
| 6250 | llvm::Type *getType() const { |
| 6251 | if (Elems.size() == 1) |
| 6252 | return Elems.front(); |
| 6253 | else |
| 6254 | return llvm::StructType::get(Context, Elems); |
| 6255 | } |
| 6256 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6257 | }; |
| 6258 | } // end anonymous namespace |
| 6259 | |
| 6260 | ABIArgInfo |
| 6261 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6262 | if (Ty->isVoidType()) |
| 6263 | return ABIArgInfo::getIgnore(); |
| 6264 | |
| 6265 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6266 | |
| 6267 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6268 | // pointer / sret pointer. |
| 6269 | if (Size > SizeLimit) |
| 6270 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 6271 | |
| 6272 | // Treat an enum type as its underlying type. |
| 6273 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6274 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6275 | |
| 6276 | // Integer types smaller than a register are extended. |
| 6277 | if (Size < 64 && Ty->isIntegerType()) |
| 6278 | return ABIArgInfo::getExtend(); |
| 6279 | |
| 6280 | // Other non-aggregates go in registers. |
| 6281 | if (!isAggregateTypeForABI(Ty)) |
| 6282 | return ABIArgInfo::getDirect(); |
| 6283 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6284 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6285 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6286 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 6287 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 6288 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6289 | // 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] | 6290 | // Build a coercion type from the LLVM struct type. |
| 6291 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6292 | if (!StrTy) |
| 6293 | return ABIArgInfo::getDirect(); |
| 6294 | |
| 6295 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6296 | CB.addStruct(0, StrTy); |
| 6297 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6298 | |
| 6299 | // Try to use the original type for coercion. |
| 6300 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6301 | |
| 6302 | if (CB.InReg) |
| 6303 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6304 | else |
| 6305 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
| 6308 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6309 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6310 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6311 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6312 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6313 | AI.setCoerceToType(ArgTy); |
| 6314 | |
| 6315 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6316 | CGBuilderTy &Builder = CGF.Builder; |
| 6317 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6318 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6319 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6320 | llvm::Value *ArgAddr; |
| 6321 | unsigned Stride; |
| 6322 | |
| 6323 | switch (AI.getKind()) { |
| 6324 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6325 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6326 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6327 | |
| 6328 | case ABIArgInfo::Extend: |
| 6329 | Stride = 8; |
| 6330 | ArgAddr = Builder |
| 6331 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6332 | "extend"); |
| 6333 | break; |
| 6334 | |
| 6335 | case ABIArgInfo::Direct: |
| 6336 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6337 | ArgAddr = Addr; |
| 6338 | break; |
| 6339 | |
| 6340 | case ABIArgInfo::Indirect: |
| 6341 | Stride = 8; |
| 6342 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6343 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6344 | "indirect"); |
| 6345 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6346 | break; |
| 6347 | |
| 6348 | case ABIArgInfo::Ignore: |
| 6349 | return llvm::UndefValue::get(ArgPtrTy); |
| 6350 | } |
| 6351 | |
| 6352 | // Update VAList. |
| 6353 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6354 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6355 | |
| 6356 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6360 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6361 | for (auto &I : FI.arguments()) |
| 6362 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | namespace { |
| 6366 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6367 | public: |
| 6368 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6369 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6370 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6371 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6372 | return 14; |
| 6373 | } |
| 6374 | |
| 6375 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6376 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6377 | }; |
| 6378 | } // end anonymous namespace |
| 6379 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6380 | bool |
| 6381 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6382 | llvm::Value *Address) const { |
| 6383 | // This is calculated from the LLVM and GCC tables and verified |
| 6384 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6385 | |
| 6386 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6387 | |
| 6388 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6389 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6390 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6391 | |
| 6392 | // 0-31: the 8-byte general-purpose registers |
| 6393 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6394 | |
| 6395 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6396 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6397 | |
| 6398 | // Y = 64 |
| 6399 | // PSR = 65 |
| 6400 | // WIM = 66 |
| 6401 | // TBR = 67 |
| 6402 | // PC = 68 |
| 6403 | // NPC = 69 |
| 6404 | // FSR = 70 |
| 6405 | // CSR = 71 |
| 6406 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 6407 | |
| 6408 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6409 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6410 | |
| 6411 | return false; |
| 6412 | } |
| 6413 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6414 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6415 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6416 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6417 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6418 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6419 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6420 | |
| 6421 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6422 | /// it by reference between functions that append to it. |
| 6423 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6424 | |
| 6425 | /// TypeStringCache caches the meta encodings of Types. |
| 6426 | /// |
| 6427 | /// The reason for caching TypeStrings is two fold: |
| 6428 | /// 1. To cache a type's encoding for later uses; |
| 6429 | /// 2. As a means to break recursive member type inclusion. |
| 6430 | /// |
| 6431 | /// A cache Entry can have a Status of: |
| 6432 | /// NonRecursive: The type encoding is not recursive; |
| 6433 | /// Recursive: The type encoding is recursive; |
| 6434 | /// Incomplete: An incomplete TypeString; |
| 6435 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6436 | /// Recursive type encoding. |
| 6437 | /// |
| 6438 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6439 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6440 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6441 | /// the type is encountered. |
| 6442 | /// |
| 6443 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6444 | /// possible. The type itself is recursive and it may contain other types which |
| 6445 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6446 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6447 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6448 | /// |
| 6449 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6450 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6451 | /// are placed into the cache during type expansion as a means to identify and |
| 6452 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6453 | /// the entry becomes IncompleteUsed. |
| 6454 | /// |
| 6455 | /// During the expansion of a RecordType's members: |
| 6456 | /// |
| 6457 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6458 | /// cached encoding is used; |
| 6459 | /// |
| 6460 | /// If the cache contains a Recursive encoding for the member type, the |
| 6461 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6462 | /// |
| 6463 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6464 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6465 | /// |
| 6466 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6467 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6468 | /// it is swapped back in; |
| 6469 | /// |
| 6470 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6471 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6472 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6473 | /// |
| 6474 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6475 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6476 | /// Else the member is part of a recursive type and thus the recursion has |
| 6477 | /// been exited too soon for the encoding to be correct for the member. |
| 6478 | /// |
| 6479 | class TypeStringCache { |
| 6480 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6481 | struct Entry { |
| 6482 | std::string Str; // The encoded TypeString for the type. |
| 6483 | enum Status State; // Information about the encoding in 'Str'. |
| 6484 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6485 | // during the expansion of RecordType's members. |
| 6486 | }; |
| 6487 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6488 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6489 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6490 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6491 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6492 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6493 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6494 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6495 | bool IsRecursive); |
| 6496 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6497 | }; |
| 6498 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6499 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6500 | /// FieldEncoding is a helper for this ordering process. |
| 6501 | class FieldEncoding { |
| 6502 | bool HasName; |
| 6503 | std::string Enc; |
| 6504 | public: |
| 6505 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6506 | StringRef str() {return Enc.c_str();}; |
| 6507 | bool operator<(const FieldEncoding &rhs) const { |
| 6508 | if (HasName != rhs.HasName) return HasName; |
| 6509 | return Enc < rhs.Enc; |
| 6510 | } |
| 6511 | }; |
| 6512 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6513 | class XCoreABIInfo : public DefaultABIInfo { |
| 6514 | public: |
| 6515 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6516 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6517 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6518 | }; |
| 6519 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6520 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6521 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6522 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6523 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6524 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6525 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6526 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6527 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6528 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6529 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6530 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6531 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6532 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6533 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6534 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6535 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6536 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6537 | CGF.Int8PtrPtrTy); |
| 6538 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6539 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6540 | // Handle the argument. |
| 6541 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6542 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6543 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6544 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6545 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6546 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6547 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6548 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6549 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6550 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6551 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6552 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6553 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6554 | ArgSize = 0; |
| 6555 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6556 | case ABIArgInfo::Extend: |
| 6557 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6558 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6559 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6560 | if (ArgSize < 4) |
| 6561 | ArgSize = 4; |
| 6562 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6563 | case ABIArgInfo::Indirect: |
| 6564 | llvm::Value *ArgAddr; |
| 6565 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6566 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6567 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6568 | ArgSize = 4; |
| 6569 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6570 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6571 | |
| 6572 | // Increment the VAList. |
| 6573 | if (ArgSize) { |
| 6574 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6575 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6576 | } |
| 6577 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6578 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6579 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6580 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6581 | /// into the cache as a means to identify and break recursion. |
| 6582 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6583 | /// be reinserted by removeIncomplete(). |
| 6584 | /// All other types of encoding should have been used rather than arriving here. |
| 6585 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6586 | std::string StubEnc) { |
| 6587 | if (!ID) |
| 6588 | return; |
| 6589 | Entry &E = Map[ID]; |
| 6590 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6591 | "Incorrectly use of addIncomplete"); |
| 6592 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6593 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6594 | E.Str.swap(StubEnc); |
| 6595 | E.State = Incomplete; |
| 6596 | ++IncompleteCount; |
| 6597 | } |
| 6598 | |
| 6599 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6600 | /// must be removed from the cache. |
| 6601 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6602 | /// Returns true if the RecordType was defined recursively. |
| 6603 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6604 | if (!ID) |
| 6605 | return false; |
| 6606 | auto I = Map.find(ID); |
| 6607 | assert(I != Map.end() && "Entry not present"); |
| 6608 | Entry &E = I->second; |
| 6609 | assert( (E.State == Incomplete || |
| 6610 | E.State == IncompleteUsed) && |
| 6611 | "Entry must be an incomplete type"); |
| 6612 | bool IsRecursive = false; |
| 6613 | if (E.State == IncompleteUsed) { |
| 6614 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6615 | IsRecursive = true; |
| 6616 | --IncompleteUsedCount; |
| 6617 | } |
| 6618 | if (E.Swapped.empty()) |
| 6619 | Map.erase(I); |
| 6620 | else { |
| 6621 | // Swap the Recursive back. |
| 6622 | E.Swapped.swap(E.Str); |
| 6623 | E.Swapped.clear(); |
| 6624 | E.State = Recursive; |
| 6625 | } |
| 6626 | --IncompleteCount; |
| 6627 | return IsRecursive; |
| 6628 | } |
| 6629 | |
| 6630 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6631 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6632 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6633 | bool IsRecursive) { |
| 6634 | if (!ID || IncompleteUsedCount) |
| 6635 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6636 | Entry &E = Map[ID]; |
| 6637 | if (IsRecursive && !E.Str.empty()) { |
| 6638 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6639 | "This is not the same Recursive entry"); |
| 6640 | // The parent container was not recursive after all, so we could have used |
| 6641 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6642 | // we started viz: IncompleteCount!=0. |
| 6643 | return; |
| 6644 | } |
| 6645 | assert(E.Str.empty() && "Entry already present"); |
| 6646 | E.Str = Str.str(); |
| 6647 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6648 | } |
| 6649 | |
| 6650 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6651 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6652 | /// encoding is Recursive, return an empty StringRef. |
| 6653 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6654 | if (!ID) |
| 6655 | return StringRef(); // We have no key. |
| 6656 | auto I = Map.find(ID); |
| 6657 | if (I == Map.end()) |
| 6658 | return StringRef(); // We have no encoding. |
| 6659 | Entry &E = I->second; |
| 6660 | if (E.State == Recursive && IncompleteCount) |
| 6661 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6662 | |
| 6663 | if (E.State == Incomplete) { |
| 6664 | // The incomplete type is being used to break out of recursion. |
| 6665 | E.State = IncompleteUsed; |
| 6666 | ++IncompleteUsedCount; |
| 6667 | } |
| 6668 | return E.Str.c_str(); |
| 6669 | } |
| 6670 | |
| 6671 | /// The XCore ABI includes a type information section that communicates symbol |
| 6672 | /// type information to the linker. The linker uses this information to verify |
| 6673 | /// safety/correctness of things such as array bound and pointers et al. |
| 6674 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6675 | /// This type information (TypeString) is emitted into meta data for all global |
| 6676 | /// symbols: definitions, declarations, functions & variables. |
| 6677 | /// |
| 6678 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6679 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
| 6680 | /// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf> |
| 6681 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6682 | /// |
| 6683 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6684 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6685 | |
| 6686 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6687 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6688 | CodeGen::CodeGenModule &CGM) const { |
| 6689 | SmallStringEnc Enc; |
| 6690 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6691 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6692 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 6693 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6694 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6695 | llvm::NamedMDNode *MD = |
| 6696 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6697 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6698 | } |
| 6699 | } |
| 6700 | |
| 6701 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6702 | const CodeGen::CodeGenModule &CGM, |
| 6703 | TypeStringCache &TSC); |
| 6704 | |
| 6705 | /// Helper function for appendRecordType(). |
| 6706 | /// Builds a SmallVector containing the encoded field types in declaration order. |
| 6707 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6708 | const RecordDecl *RD, |
| 6709 | const CodeGen::CodeGenModule &CGM, |
| 6710 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6711 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6712 | SmallStringEnc Enc; |
| 6713 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6714 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6715 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6716 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6717 | Enc += "b("; |
| 6718 | llvm::raw_svector_ostream OS(Enc); |
| 6719 | OS.resync(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6720 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6721 | OS.flush(); |
| 6722 | Enc += ':'; |
| 6723 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6724 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6725 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6726 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6727 | Enc += ')'; |
| 6728 | Enc += '}'; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6729 | FE.push_back(FieldEncoding(!Field->getName().empty(), Enc)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6730 | } |
| 6731 | return true; |
| 6732 | } |
| 6733 | |
| 6734 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6735 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6736 | /// Union types have their fields ordered according to the ABI. |
| 6737 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6738 | const CodeGen::CodeGenModule &CGM, |
| 6739 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6740 | // Append the cached TypeString if we have one. |
| 6741 | StringRef TypeString = TSC.lookupStr(ID); |
| 6742 | if (!TypeString.empty()) { |
| 6743 | Enc += TypeString; |
| 6744 | return true; |
| 6745 | } |
| 6746 | |
| 6747 | // Start to emit an incomplete TypeString. |
| 6748 | size_t Start = Enc.size(); |
| 6749 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6750 | Enc += '('; |
| 6751 | if (ID) |
| 6752 | Enc += ID->getName(); |
| 6753 | Enc += "){"; |
| 6754 | |
| 6755 | // We collect all encoded fields and order as necessary. |
| 6756 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6757 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6758 | if (RD && !RD->field_empty()) { |
| 6759 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6760 | // so that recursive calls to this RecordType will use it whilst building a |
| 6761 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6762 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6763 | std::string StubEnc(Enc.substr(Start).str()); |
| 6764 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6765 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6766 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6767 | (void) TSC.removeIncomplete(ID); |
| 6768 | return false; |
| 6769 | } |
| 6770 | IsRecursive = TSC.removeIncomplete(ID); |
| 6771 | // The ABI requires unions to be sorted but not structures. |
| 6772 | // See FieldEncoding::operator< for sort algorithm. |
| 6773 | if (RT->isUnionType()) |
| 6774 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6775 | // We can now complete the TypeString. |
| 6776 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6777 | for (unsigned I = 0; I != E; ++I) { |
| 6778 | if (I) |
| 6779 | Enc += ','; |
| 6780 | Enc += FE[I].str(); |
| 6781 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6782 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6783 | Enc += '}'; |
| 6784 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6785 | return true; |
| 6786 | } |
| 6787 | |
| 6788 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6789 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6790 | TypeStringCache &TSC, |
| 6791 | const IdentifierInfo *ID) { |
| 6792 | // Append the cached TypeString if we have one. |
| 6793 | StringRef TypeString = TSC.lookupStr(ID); |
| 6794 | if (!TypeString.empty()) { |
| 6795 | Enc += TypeString; |
| 6796 | return true; |
| 6797 | } |
| 6798 | |
| 6799 | size_t Start = Enc.size(); |
| 6800 | Enc += "e("; |
| 6801 | if (ID) |
| 6802 | Enc += ID->getName(); |
| 6803 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6804 | |
| 6805 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6806 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6807 | SmallVector<FieldEncoding, 16> FE; |
| 6808 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6809 | ++I) { |
| 6810 | SmallStringEnc EnumEnc; |
| 6811 | EnumEnc += "m("; |
| 6812 | EnumEnc += I->getName(); |
| 6813 | EnumEnc += "){"; |
| 6814 | I->getInitVal().toString(EnumEnc); |
| 6815 | EnumEnc += '}'; |
| 6816 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6817 | } |
| 6818 | std::sort(FE.begin(), FE.end()); |
| 6819 | unsigned E = FE.size(); |
| 6820 | for (unsigned I = 0; I != E; ++I) { |
| 6821 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6822 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6823 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6824 | } |
| 6825 | } |
| 6826 | Enc += '}'; |
| 6827 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6828 | return true; |
| 6829 | } |
| 6830 | |
| 6831 | /// Appends type's qualifier to Enc. |
| 6832 | /// This is done prior to appending the type's encoding. |
| 6833 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6834 | // Qualifiers are emitted in alphabetical order. |
| 6835 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6836 | int Lookup = 0; |
| 6837 | if (QT.isConstQualified()) |
| 6838 | Lookup += 1<<0; |
| 6839 | if (QT.isRestrictQualified()) |
| 6840 | Lookup += 1<<1; |
| 6841 | if (QT.isVolatileQualified()) |
| 6842 | Lookup += 1<<2; |
| 6843 | Enc += Table[Lookup]; |
| 6844 | } |
| 6845 | |
| 6846 | /// Appends built-in types to Enc. |
| 6847 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6848 | const char *EncType; |
| 6849 | switch (BT->getKind()) { |
| 6850 | case BuiltinType::Void: |
| 6851 | EncType = "0"; |
| 6852 | break; |
| 6853 | case BuiltinType::Bool: |
| 6854 | EncType = "b"; |
| 6855 | break; |
| 6856 | case BuiltinType::Char_U: |
| 6857 | EncType = "uc"; |
| 6858 | break; |
| 6859 | case BuiltinType::UChar: |
| 6860 | EncType = "uc"; |
| 6861 | break; |
| 6862 | case BuiltinType::SChar: |
| 6863 | EncType = "sc"; |
| 6864 | break; |
| 6865 | case BuiltinType::UShort: |
| 6866 | EncType = "us"; |
| 6867 | break; |
| 6868 | case BuiltinType::Short: |
| 6869 | EncType = "ss"; |
| 6870 | break; |
| 6871 | case BuiltinType::UInt: |
| 6872 | EncType = "ui"; |
| 6873 | break; |
| 6874 | case BuiltinType::Int: |
| 6875 | EncType = "si"; |
| 6876 | break; |
| 6877 | case BuiltinType::ULong: |
| 6878 | EncType = "ul"; |
| 6879 | break; |
| 6880 | case BuiltinType::Long: |
| 6881 | EncType = "sl"; |
| 6882 | break; |
| 6883 | case BuiltinType::ULongLong: |
| 6884 | EncType = "ull"; |
| 6885 | break; |
| 6886 | case BuiltinType::LongLong: |
| 6887 | EncType = "sll"; |
| 6888 | break; |
| 6889 | case BuiltinType::Float: |
| 6890 | EncType = "ft"; |
| 6891 | break; |
| 6892 | case BuiltinType::Double: |
| 6893 | EncType = "d"; |
| 6894 | break; |
| 6895 | case BuiltinType::LongDouble: |
| 6896 | EncType = "ld"; |
| 6897 | break; |
| 6898 | default: |
| 6899 | return false; |
| 6900 | } |
| 6901 | Enc += EncType; |
| 6902 | return true; |
| 6903 | } |
| 6904 | |
| 6905 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6906 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6907 | const CodeGen::CodeGenModule &CGM, |
| 6908 | TypeStringCache &TSC) { |
| 6909 | Enc += "p("; |
| 6910 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6911 | return false; |
| 6912 | Enc += ')'; |
| 6913 | return true; |
| 6914 | } |
| 6915 | |
| 6916 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6917 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6918 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6919 | const CodeGen::CodeGenModule &CGM, |
| 6920 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6921 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6922 | return false; |
| 6923 | Enc += "a("; |
| 6924 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6925 | CAT->getSize().toStringUnsigned(Enc); |
| 6926 | else |
| 6927 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6928 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6929 | // The Qualifiers should be attached to the type rather than the array. |
| 6930 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6931 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6932 | return false; |
| 6933 | Enc += ')'; |
| 6934 | return true; |
| 6935 | } |
| 6936 | |
| 6937 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6938 | /// and the arguments. |
| 6939 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6940 | const CodeGen::CodeGenModule &CGM, |
| 6941 | TypeStringCache &TSC) { |
| 6942 | Enc += "f{"; |
| 6943 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 6944 | return false; |
| 6945 | Enc += "}("; |
| 6946 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 6947 | // N.B. we are only interested in the adjusted param types. |
| 6948 | auto I = FPT->param_type_begin(); |
| 6949 | auto E = FPT->param_type_end(); |
| 6950 | if (I != E) { |
| 6951 | do { |
| 6952 | if (!appendType(Enc, *I, CGM, TSC)) |
| 6953 | return false; |
| 6954 | ++I; |
| 6955 | if (I != E) |
| 6956 | Enc += ','; |
| 6957 | } while (I != E); |
| 6958 | if (FPT->isVariadic()) |
| 6959 | Enc += ",va"; |
| 6960 | } else { |
| 6961 | if (FPT->isVariadic()) |
| 6962 | Enc += "va"; |
| 6963 | else |
| 6964 | Enc += '0'; |
| 6965 | } |
| 6966 | } |
| 6967 | Enc += ')'; |
| 6968 | return true; |
| 6969 | } |
| 6970 | |
| 6971 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 6972 | /// type encodings. |
| 6973 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6974 | const CodeGen::CodeGenModule &CGM, |
| 6975 | TypeStringCache &TSC) { |
| 6976 | |
| 6977 | QualType QT = QType.getCanonicalType(); |
| 6978 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6979 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 6980 | // The Qualifiers should be attached to the type rather than the array. |
| 6981 | // Thus we don't call appendQualifier() here. |
| 6982 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 6983 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6984 | appendQualifier(Enc, QT); |
| 6985 | |
| 6986 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 6987 | return appendBuiltinType(Enc, BT); |
| 6988 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6989 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 6990 | return appendPointerType(Enc, PT, CGM, TSC); |
| 6991 | |
| 6992 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 6993 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 6994 | |
| 6995 | if (const RecordType *RT = QT->getAsStructureType()) |
| 6996 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6997 | |
| 6998 | if (const RecordType *RT = QT->getAsUnionType()) |
| 6999 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 7000 | |
| 7001 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 7002 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 7003 | |
| 7004 | return false; |
| 7005 | } |
| 7006 | |
| 7007 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 7008 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 7009 | if (!D) |
| 7010 | return false; |
| 7011 | |
| 7012 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7013 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 7014 | return false; |
| 7015 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 7016 | } |
| 7017 | |
| 7018 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7019 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 7020 | return false; |
| 7021 | QualType QT = VD->getType().getCanonicalType(); |
| 7022 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 7023 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 7024 | // The Qualifiers should be attached to the type rather than the array. |
| 7025 | // Thus we don't call appendQualifier() here. |
| 7026 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 7027 | } |
| 7028 | return appendType(Enc, QT, CGM, TSC); |
| 7029 | } |
| 7030 | return false; |
| 7031 | } |
| 7032 | |
| 7033 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7034 | //===----------------------------------------------------------------------===// |
| 7035 | // Driver code |
| 7036 | //===----------------------------------------------------------------------===// |
| 7037 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 7038 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 7039 | return getTarget().getTriple(); |
| 7040 | } |
| 7041 | |
| 7042 | bool CodeGenModule::supportsCOMDAT() const { |
| 7043 | return !getTriple().isOSBinFormatMachO(); |
| 7044 | } |
| 7045 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7046 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7047 | if (TheTargetCodeGenInfo) |
| 7048 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7049 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7050 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 7051 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7052 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7053 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7054 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 7055 | case llvm::Triple::le32: |
| 7056 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7057 | case llvm::Triple::mips: |
| 7058 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7059 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 7060 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 7061 | case llvm::Triple::mips64: |
| 7062 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 7063 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 7064 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 7065 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 7066 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7067 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7068 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7069 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7070 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 7071 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 7072 | } |
| 7073 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7074 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7075 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7076 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7077 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7078 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7079 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7080 | TheTargetCodeGenInfo = |
| 7081 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7082 | return *TheTargetCodeGenInfo; |
| 7083 | } |
| 7084 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7085 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7086 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7087 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7088 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7089 | (CodeGenOpts.FloatABI != "soft" && |
| 7090 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7091 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7092 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7093 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7094 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7095 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7096 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7097 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7098 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7099 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7100 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7101 | if (getTarget().getABI() == "elfv2") |
| 7102 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7103 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7104 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7105 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7106 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7107 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7108 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7109 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7110 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7111 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7112 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7113 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7114 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7115 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7116 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7117 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7118 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7119 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7120 | case llvm::Triple::nvptx: |
| 7121 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7122 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7123 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7124 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7125 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7126 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 7127 | case llvm::Triple::systemz: { |
| 7128 | bool HasVector = getTarget().getABI() == "vector"; |
| 7129 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types, |
| 7130 | HasVector)); |
| 7131 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7132 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7133 | case llvm::Triple::tce: |
| 7134 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7135 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7136 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7137 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 7138 | bool IsSmallStructInRegABI = |
| 7139 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7140 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7141 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7142 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 7143 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 7144 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7145 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7146 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 7147 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7148 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7149 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7150 | new X86_32TargetCodeGenInfo(Types, |
| 7151 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7152 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 7153 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7154 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7155 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7156 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7157 | case llvm::Triple::x86_64: { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7158 | switch (Triple.getOS()) { |
| 7159 | case llvm::Triple::Win32: |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 7160 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7161 | case llvm::Triple::PS4: |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 7162 | return *(TheTargetCodeGenInfo = new PS4TargetCodeGenInfo(Types)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7163 | default: |
Ahmed Bougacha | 1fca2ed | 2015-05-22 02:25:58 +0000 | [diff] [blame] | 7164 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7165 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7166 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7167 | case llvm::Triple::hexagon: |
| 7168 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7169 | case llvm::Triple::r600: |
| 7170 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7171 | case llvm::Triple::amdgcn: |
| 7172 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7173 | case llvm::Triple::sparcv9: |
| 7174 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7175 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7176 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7177 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7178 | } |