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 | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 409 | if (isAggregateTypeForABI(Ty)) |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 410 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 412 | // Treat an enum type as its underlying type. |
| 413 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 414 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 416 | return (Ty->isPromotableIntegerType() ? |
| 417 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 420 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 421 | if (RetTy->isVoidType()) |
| 422 | return ABIArgInfo::getIgnore(); |
| 423 | |
| 424 | if (isAggregateTypeForABI(RetTy)) |
| 425 | return ABIArgInfo::getIndirect(0); |
| 426 | |
| 427 | // Treat an enum type as its underlying type. |
| 428 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 429 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 430 | |
| 431 | return (RetTy->isPromotableIntegerType() ? |
| 432 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 433 | } |
| 434 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 435 | //===----------------------------------------------------------------------===// |
| 436 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 437 | // |
| 438 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 439 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 440 | //===----------------------------------------------------------------------===// |
| 441 | |
| 442 | class PNaClABIInfo : public ABIInfo { |
| 443 | public: |
| 444 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 445 | |
| 446 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 447 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 448 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 449 | void computeInfo(CGFunctionInfo &FI) const override; |
| 450 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 451 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 455 | public: |
| 456 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 457 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 458 | }; |
| 459 | |
| 460 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 461 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 462 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 463 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 464 | for (auto &I : FI.arguments()) |
| 465 | I.info = classifyArgumentType(I.type); |
| 466 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 467 | |
| 468 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 469 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 470 | return nullptr; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 473 | /// \brief Classify argument of given type \p Ty. |
| 474 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 475 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 476 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 477 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 478 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 479 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 480 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 481 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 482 | } else if (Ty->isFloatingType()) { |
| 483 | // Floating-point types don't go inreg. |
| 484 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 485 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 486 | |
| 487 | return (Ty->isPromotableIntegerType() ? |
| 488 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 492 | if (RetTy->isVoidType()) |
| 493 | return ABIArgInfo::getIgnore(); |
| 494 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 495 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 496 | if (isAggregateTypeForABI(RetTy)) |
| 497 | return ABIArgInfo::getIndirect(0); |
| 498 | |
| 499 | // Treat an enum type as its underlying type. |
| 500 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 501 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 502 | |
| 503 | return (RetTy->isPromotableIntegerType() ? |
| 504 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 505 | } |
| 506 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 507 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 508 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 509 | // 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] | 510 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 511 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 512 | IRType->getScalarSizeInBits() != 64; |
| 513 | } |
| 514 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 515 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 516 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 517 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 518 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 519 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 520 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 521 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 524 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 528 | return Ty; |
| 529 | } |
| 530 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 531 | /// Returns true if this type can be passed in SSE registers with the |
| 532 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 533 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 534 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 535 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) |
| 536 | return true; |
| 537 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 538 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 539 | // registers specially. |
| 540 | unsigned VecSize = Context.getTypeSize(VT); |
| 541 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 542 | return true; |
| 543 | } |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 548 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 549 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 550 | return NumMembers <= 4; |
| 551 | } |
| 552 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 553 | //===----------------------------------------------------------------------===// |
| 554 | // X86-32 ABI Implementation |
| 555 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 556 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 557 | /// \brief Similar to llvm::CCState, but for Clang. |
| 558 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 559 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 560 | |
| 561 | unsigned CC; |
| 562 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 563 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 564 | }; |
| 565 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 566 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 567 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 568 | enum Class { |
| 569 | Integer, |
| 570 | Float |
| 571 | }; |
| 572 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 573 | static const unsigned MinABIStackAlignInBytes = 4; |
| 574 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 575 | bool IsDarwinVectorABI; |
| 576 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 577 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 578 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 579 | |
| 580 | static bool isRegisterSize(unsigned Size) { |
| 581 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 582 | } |
| 583 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 584 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 585 | // FIXME: Assumes vectorcall is in use. |
| 586 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 587 | } |
| 588 | |
| 589 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 590 | uint64_t NumMembers) const override { |
| 591 | // FIXME: Assumes vectorcall is in use. |
| 592 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 593 | } |
| 594 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 595 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 596 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 597 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 598 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 599 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 600 | |
| 601 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 602 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 603 | /// \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] | 604 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 605 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 606 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 607 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 608 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 609 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 610 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 611 | /// \brief Rewrite the function info so that all memory arguments use |
| 612 | /// inalloca. |
| 613 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 614 | |
| 615 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 616 | unsigned &StackOffset, ABIArgInfo &Info, |
| 617 | QualType Type) const; |
| 618 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 619 | public: |
| 620 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 621 | void computeInfo(CGFunctionInfo &FI) const override; |
| 622 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 623 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 624 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 625 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 626 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 627 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 628 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 629 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 630 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 631 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 632 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 633 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 634 | bool d, bool p, bool w, unsigned r) |
| 635 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 636 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 637 | static bool isStructReturnInRegABI( |
| 638 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 639 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 640 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 641 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 642 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 643 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 644 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 645 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 646 | return 4; |
| 647 | } |
| 648 | |
| 649 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 650 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 651 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 652 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 653 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 654 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 655 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 656 | } |
| 657 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 658 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 659 | std::string &Constraints, |
| 660 | std::vector<llvm::Type *> &ResultRegTypes, |
| 661 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 662 | std::vector<LValue> &ResultRegDests, |
| 663 | std::string &AsmString, |
| 664 | unsigned NumOutputs) const override; |
| 665 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 666 | llvm::Constant * |
| 667 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 668 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 669 | (0x06 << 8) | // .+0x08 |
| 670 | ('F' << 16) | |
| 671 | ('T' << 24); |
| 672 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 673 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
| 676 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 677 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 678 | /// Rewrite input constraint references after adding some output constraints. |
| 679 | /// In the case where there is one output and one input and we add one output, |
| 680 | /// we need to replace all operand references greater than or equal to 1: |
| 681 | /// mov $0, $1 |
| 682 | /// mov eax, $1 |
| 683 | /// The result will be: |
| 684 | /// mov $0, $2 |
| 685 | /// mov eax, $2 |
| 686 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 687 | unsigned NumNewOuts, |
| 688 | std::string &AsmString) { |
| 689 | std::string Buf; |
| 690 | llvm::raw_string_ostream OS(Buf); |
| 691 | size_t Pos = 0; |
| 692 | while (Pos < AsmString.size()) { |
| 693 | size_t DollarStart = AsmString.find('$', Pos); |
| 694 | if (DollarStart == std::string::npos) |
| 695 | DollarStart = AsmString.size(); |
| 696 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 697 | if (DollarEnd == std::string::npos) |
| 698 | DollarEnd = AsmString.size(); |
| 699 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 700 | Pos = DollarEnd; |
| 701 | size_t NumDollars = DollarEnd - DollarStart; |
| 702 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 703 | // We have an operand reference. |
| 704 | size_t DigitStart = Pos; |
| 705 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 706 | if (DigitEnd == std::string::npos) |
| 707 | DigitEnd = AsmString.size(); |
| 708 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 709 | unsigned OperandIndex; |
| 710 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 711 | if (OperandIndex >= FirstIn) |
| 712 | OperandIndex += NumNewOuts; |
| 713 | OS << OperandIndex; |
| 714 | } else { |
| 715 | OS << OperandStr; |
| 716 | } |
| 717 | Pos = DigitEnd; |
| 718 | } |
| 719 | } |
| 720 | AsmString = std::move(OS.str()); |
| 721 | } |
| 722 | |
| 723 | /// Add output constraints for EAX:EDX because they are return registers. |
| 724 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 725 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 726 | std::vector<llvm::Type *> &ResultRegTypes, |
| 727 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 728 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 729 | unsigned NumOutputs) const { |
| 730 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 731 | |
| 732 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 733 | // larger. |
| 734 | if (!Constraints.empty()) |
| 735 | Constraints += ','; |
| 736 | if (RetWidth <= 32) { |
| 737 | Constraints += "={eax}"; |
| 738 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 739 | } else { |
| 740 | // Use the 'A' constraint for EAX:EDX. |
| 741 | Constraints += "=A"; |
| 742 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 743 | } |
| 744 | |
| 745 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 746 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 747 | ResultTruncRegTypes.push_back(CoerceTy); |
| 748 | |
| 749 | // Coerce the integer by bitcasting the return slot pointer. |
| 750 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 751 | CoerceTy->getPointerTo())); |
| 752 | ResultRegDests.push_back(ReturnSlot); |
| 753 | |
| 754 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 755 | } |
| 756 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 757 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 758 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 759 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 760 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 761 | uint64_t Size = Context.getTypeSize(Ty); |
| 762 | |
| 763 | // Type must be register sized. |
| 764 | if (!isRegisterSize(Size)) |
| 765 | return false; |
| 766 | |
| 767 | if (Ty->isVectorType()) { |
| 768 | // 64- and 128- bit vectors inside structures are not returned in |
| 769 | // registers. |
| 770 | if (Size == 64 || Size == 128) |
| 771 | return false; |
| 772 | |
| 773 | return true; |
| 774 | } |
| 775 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 776 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 777 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 778 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 779 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 780 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 781 | return true; |
| 782 | |
| 783 | // Arrays are treated like records. |
| 784 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 785 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 786 | |
| 787 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 788 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 789 | if (!RT) return false; |
| 790 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 791 | // FIXME: Traverse bases here too. |
| 792 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 793 | // Structure types are passed in register if all fields would be |
| 794 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 795 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 796 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 797 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 798 | continue; |
| 799 | |
| 800 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 801 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 802 | return false; |
| 803 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 804 | return true; |
| 805 | } |
| 806 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 807 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 808 | // If the return value is indirect, then the hidden argument is consuming one |
| 809 | // integer register. |
| 810 | if (State.FreeRegs) { |
| 811 | --State.FreeRegs; |
| 812 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 813 | } |
| 814 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 815 | } |
| 816 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 817 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 818 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 819 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 820 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 821 | const Type *Base = nullptr; |
| 822 | uint64_t NumElts = 0; |
| 823 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 824 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 825 | // The LLVM struct type for such an aggregate should lower properly. |
| 826 | return ABIArgInfo::getDirect(); |
| 827 | } |
| 828 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 829 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 830 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 831 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 832 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 833 | |
| 834 | // 128-bit vectors are a special case; they are returned in |
| 835 | // registers and we need to make sure to pick a type the LLVM |
| 836 | // backend will like. |
| 837 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 838 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 839 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 840 | |
| 841 | // Always return in register if it fits in a general purpose |
| 842 | // register, or if it is 64 bits and has a single element. |
| 843 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 844 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 845 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 846 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 847 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 848 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 852 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 853 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 854 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 855 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 856 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 857 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 858 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 859 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 860 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 861 | // If specified, structs and unions are always indirect. |
| 862 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 863 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 864 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 865 | // Small structures which are register sized are generally returned |
| 866 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 867 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 868 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 869 | |
| 870 | // As a special-case, if the struct is a "single-element" struct, and |
| 871 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 872 | // floating-point register. (MSVC does not apply this special case.) |
| 873 | // We apply a similar transformation for pointer types to improve the |
| 874 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 875 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 876 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 877 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 878 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 879 | |
| 880 | // FIXME: We should be able to narrow this integer in cases with dead |
| 881 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 882 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 885 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 886 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 888 | // Treat an enum type as its underlying type. |
| 889 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 890 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 891 | |
| 892 | return (RetTy->isPromotableIntegerType() ? |
| 893 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 896 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 897 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 898 | } |
| 899 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 900 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 901 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 902 | if (!RT) |
| 903 | return 0; |
| 904 | const RecordDecl *RD = RT->getDecl(); |
| 905 | |
| 906 | // If this is a C++ record, check the bases first. |
| 907 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 908 | for (const auto &I : CXXRD->bases()) |
| 909 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 910 | return false; |
| 911 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 912 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 913 | QualType FT = i->getType(); |
| 914 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 915 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 916 | return true; |
| 917 | |
| 918 | if (isRecordWithSSEVectorType(Context, FT)) |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | return false; |
| 923 | } |
| 924 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 925 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 926 | unsigned Align) const { |
| 927 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 928 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 929 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 930 | return 0; // Use default alignment. |
| 931 | |
| 932 | // On non-Darwin, the stack type alignment is always 4. |
| 933 | if (!IsDarwinVectorABI) { |
| 934 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 935 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 936 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 937 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 938 | // 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] | 939 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 940 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 941 | return 16; |
| 942 | |
| 943 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 946 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 947 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 948 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 949 | if (State.FreeRegs) { |
| 950 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 951 | return ABIArgInfo::getIndirectInReg(0, false); |
| 952 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 953 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 954 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 955 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 956 | // Compute the byval alignment. |
| 957 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 958 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 959 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 960 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 961 | |
| 962 | // If the stack alignment is less than the type alignment, realign the |
| 963 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 964 | bool Realign = TypeAlign > StackAlign; |
| 965 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 968 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 969 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 970 | if (!T) |
| 971 | T = Ty.getTypePtr(); |
| 972 | |
| 973 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 974 | BuiltinType::Kind K = BT->getKind(); |
| 975 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 976 | return Float; |
| 977 | } |
| 978 | return Integer; |
| 979 | } |
| 980 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 981 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 982 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 983 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 984 | Class C = classify(Ty); |
| 985 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 986 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 987 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 988 | unsigned Size = getContext().getTypeSize(Ty); |
| 989 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 990 | |
| 991 | if (SizeInRegs == 0) |
| 992 | return false; |
| 993 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 994 | if (SizeInRegs > State.FreeRegs) { |
| 995 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 996 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 997 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 998 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 999 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1000 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1001 | if (State.CC == llvm::CallingConv::X86_FastCall || |
| 1002 | State.CC == llvm::CallingConv::X86_VectorCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1003 | if (Size > 32) |
| 1004 | return false; |
| 1005 | |
| 1006 | if (Ty->isIntegralOrEnumerationType()) |
| 1007 | return true; |
| 1008 | |
| 1009 | if (Ty->isPointerType()) |
| 1010 | return true; |
| 1011 | |
| 1012 | if (Ty->isReferenceType()) |
| 1013 | return true; |
| 1014 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1015 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1016 | NeedsPadding = true; |
| 1017 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1018 | return false; |
| 1019 | } |
| 1020 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1021 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1024 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1025 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1026 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1027 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1028 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1029 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1030 | // Check with the C++ ABI first. |
| 1031 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1032 | if (RT) { |
| 1033 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1034 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1035 | return getIndirectResult(Ty, false, State); |
| 1036 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1037 | // The field index doesn't matter, we'll fix it up later. |
| 1038 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | // vectorcall adds the concept of a homogenous vector aggregate, similar |
| 1043 | // to other targets. |
| 1044 | const Type *Base = nullptr; |
| 1045 | uint64_t NumElts = 0; |
| 1046 | if (State.CC == llvm::CallingConv::X86_VectorCall && |
| 1047 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1048 | if (State.FreeSSERegs >= NumElts) { |
| 1049 | State.FreeSSERegs -= NumElts; |
| 1050 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
| 1051 | return ABIArgInfo::getDirect(); |
| 1052 | return ABIArgInfo::getExpand(); |
| 1053 | } |
| 1054 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1055 | } |
| 1056 | |
| 1057 | if (isAggregateTypeForABI(Ty)) { |
| 1058 | if (RT) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1059 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1060 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1061 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1062 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1063 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1064 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1065 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1066 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1067 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 1068 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 1069 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1070 | return ABIArgInfo::getIgnore(); |
| 1071 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1072 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1073 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 1074 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1075 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1076 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1077 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1078 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 1079 | return ABIArgInfo::getDirectInReg(Result); |
| 1080 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1081 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1082 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1083 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1084 | // of those arguments will match the struct. This is important because the |
| 1085 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1086 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1087 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 1088 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1089 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1090 | State.CC == llvm::CallingConv::X86_FastCall || |
| 1091 | State.CC == llvm::CallingConv::X86_VectorCall, |
| 1092 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1093 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1094 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1097 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1098 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1099 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1100 | if (IsDarwinVectorABI) { |
| 1101 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1102 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1103 | (Size == 64 && VT->getNumElements() == 1)) |
| 1104 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1105 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1106 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1107 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1108 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1109 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1110 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1111 | return ABIArgInfo::getDirect(); |
| 1112 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1113 | |
| 1114 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1115 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1116 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1117 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1118 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1119 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1120 | |
| 1121 | if (Ty->isPromotableIntegerType()) { |
| 1122 | if (InReg) |
| 1123 | return ABIArgInfo::getExtendInReg(); |
| 1124 | return ABIArgInfo::getExtend(); |
| 1125 | } |
| 1126 | if (InReg) |
| 1127 | return ABIArgInfo::getDirectInReg(); |
| 1128 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1131 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1132 | CCState State(FI.getCallingConvention()); |
| 1133 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1134 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1135 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1136 | State.FreeRegs = 2; |
| 1137 | State.FreeSSERegs = 6; |
| 1138 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1139 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1140 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1141 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1142 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1143 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1144 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1145 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1146 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1147 | // return value was sret and put it in a register ourselves if appropriate. |
| 1148 | if (State.FreeRegs) { |
| 1149 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1150 | FI.getReturnInfo().setInReg(true); |
| 1151 | } |
| 1152 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1153 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1154 | // The chain argument effectively gives us another free register. |
| 1155 | if (FI.isChainCall()) |
| 1156 | ++State.FreeRegs; |
| 1157 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1158 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1159 | for (auto &I : FI.arguments()) { |
| 1160 | I.info = classifyArgumentType(I.type, State); |
| 1161 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1165 | // all the memory arguments to use inalloca. |
| 1166 | if (UsedInAlloca) |
| 1167 | rewriteWithInAlloca(FI); |
| 1168 | } |
| 1169 | |
| 1170 | void |
| 1171 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1172 | unsigned &StackOffset, |
| 1173 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1174 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 1175 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1176 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1177 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1178 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1179 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1180 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1181 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1182 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1183 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1184 | unsigned NumBytes = StackOffset - OldOffset; |
| 1185 | assert(NumBytes); |
| 1186 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1187 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1188 | FrameFields.push_back(Ty); |
| 1189 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1192 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1193 | // Leave ignored and inreg arguments alone. |
| 1194 | switch (Info.getKind()) { |
| 1195 | case ABIArgInfo::InAlloca: |
| 1196 | return true; |
| 1197 | case ABIArgInfo::Indirect: |
| 1198 | assert(Info.getIndirectByVal()); |
| 1199 | return true; |
| 1200 | case ABIArgInfo::Ignore: |
| 1201 | return false; |
| 1202 | case ABIArgInfo::Direct: |
| 1203 | case ABIArgInfo::Extend: |
| 1204 | case ABIArgInfo::Expand: |
| 1205 | if (Info.getInReg()) |
| 1206 | return false; |
| 1207 | return true; |
| 1208 | } |
| 1209 | llvm_unreachable("invalid enum"); |
| 1210 | } |
| 1211 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1212 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1213 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1214 | |
| 1215 | // Build a packed struct type for all of the arguments in memory. |
| 1216 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1217 | |
| 1218 | unsigned StackOffset = 0; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1219 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1220 | |
| 1221 | // Put 'this' into the struct before 'sret', if necessary. |
| 1222 | bool IsThisCall = |
| 1223 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1224 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1225 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1226 | isArgInAlloca(I->info)) { |
| 1227 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1228 | ++I; |
| 1229 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1230 | |
| 1231 | // 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] | 1232 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1233 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1234 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1235 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1236 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1240 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1241 | ++I; |
| 1242 | |
| 1243 | // Put arguments passed in memory into the struct. |
| 1244 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1245 | if (isArgInAlloca(I->info)) |
| 1246 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1250 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1253 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1254 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1255 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1256 | |
| 1257 | CGBuilderTy &Builder = CGF.Builder; |
| 1258 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1259 | "ap"); |
| 1260 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Compute if the address needs to be aligned |
| 1263 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1264 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1265 | Align = std::max(Align, 4U); |
| 1266 | if (Align > 4) { |
| 1267 | // addr = (addr + align - 1) & -align; |
| 1268 | llvm::Value *Offset = |
| 1269 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1270 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1271 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1272 | CGF.Int32Ty); |
| 1273 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1274 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1275 | Addr->getType(), |
| 1276 | "ap.cur.aligned"); |
| 1277 | } |
| 1278 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1279 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1280 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1281 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1282 | |
| 1283 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1284 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1285 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1286 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1287 | "ap.next"); |
| 1288 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1289 | |
| 1290 | return AddrTyped; |
| 1291 | } |
| 1292 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1293 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1294 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1295 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1296 | |
| 1297 | switch (Opts.getStructReturnConvention()) { |
| 1298 | case CodeGenOptions::SRCK_Default: |
| 1299 | break; |
| 1300 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1301 | return false; |
| 1302 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1303 | return true; |
| 1304 | } |
| 1305 | |
| 1306 | if (Triple.isOSDarwin()) |
| 1307 | return true; |
| 1308 | |
| 1309 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1310 | case llvm::Triple::DragonFly: |
| 1311 | case llvm::Triple::FreeBSD: |
| 1312 | case llvm::Triple::OpenBSD: |
| 1313 | case llvm::Triple::Bitrig: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1314 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1315 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1316 | default: |
| 1317 | return false; |
| 1318 | } |
| 1319 | } |
| 1320 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1321 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1322 | llvm::GlobalValue *GV, |
| 1323 | CodeGen::CodeGenModule &CGM) const { |
| 1324 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1325 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1326 | // Get the LLVM function. |
| 1327 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1328 | |
| 1329 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1330 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1331 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1332 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1333 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1334 | llvm::AttributeSet::FunctionIndex, |
| 1335 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1340 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1341 | CodeGen::CodeGenFunction &CGF, |
| 1342 | llvm::Value *Address) const { |
| 1343 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1345 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1346 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1347 | // 0-7 are the eight integer registers; the order is different |
| 1348 | // on Darwin (for EH), but the range is the same. |
| 1349 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1350 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1351 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1352 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1353 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1354 | // These have size 16, which is sizeof(long double) on |
| 1355 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1356 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1357 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1358 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1359 | } else { |
| 1360 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1361 | // reason. |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame^] | 1362 | Builder.CreateStore( |
| 1363 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9)); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1364 | |
| 1365 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1366 | // These have size 12, which is sizeof(long double) on |
| 1367 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1368 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1369 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1370 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1371 | |
| 1372 | return false; |
| 1373 | } |
| 1374 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1375 | //===----------------------------------------------------------------------===// |
| 1376 | // X86-64 ABI Implementation |
| 1377 | //===----------------------------------------------------------------------===// |
| 1378 | |
| 1379 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1380 | namespace { |
| 1381 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1382 | class X86_64ABIInfo : public ABIInfo { |
| 1383 | enum Class { |
| 1384 | Integer = 0, |
| 1385 | SSE, |
| 1386 | SSEUp, |
| 1387 | X87, |
| 1388 | X87Up, |
| 1389 | ComplexX87, |
| 1390 | NoClass, |
| 1391 | Memory |
| 1392 | }; |
| 1393 | |
| 1394 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1395 | /// |
| 1396 | /// Merge an accumulating classification \arg Accum with a field |
| 1397 | /// classification \arg Field. |
| 1398 | /// |
| 1399 | /// \param Accum - The accumulating classification. This should |
| 1400 | /// always be either NoClass or the result of a previous merge |
| 1401 | /// call. In addition, this should never be Memory (the caller |
| 1402 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1403 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1404 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1405 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1406 | /// |
| 1407 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1408 | /// final MEMORY or SSE classes when necessary. |
| 1409 | /// |
| 1410 | /// \param AggregateSize - The size of the current aggregate in |
| 1411 | /// the classification process. |
| 1412 | /// |
| 1413 | /// \param Lo - The classification for the parts of the type |
| 1414 | /// residing in the low word of the containing object. |
| 1415 | /// |
| 1416 | /// \param Hi - The classification for the parts of the type |
| 1417 | /// residing in the higher words of the containing object. |
| 1418 | /// |
| 1419 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1420 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1421 | /// classify - Determine the x86_64 register classes in which the |
| 1422 | /// given type T should be passed. |
| 1423 | /// |
| 1424 | /// \param Lo - The classification for the parts of the type |
| 1425 | /// residing in the low word of the containing object. |
| 1426 | /// |
| 1427 | /// \param Hi - The classification for the parts of the type |
| 1428 | /// residing in the high word of the containing object. |
| 1429 | /// |
| 1430 | /// \param OffsetBase - The bit offset of this type in the |
| 1431 | /// containing object. Some parameters are classified different |
| 1432 | /// depending on whether they straddle an eightbyte boundary. |
| 1433 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1434 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1435 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1436 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1437 | /// If a word is unused its result will be NoClass; if a type should |
| 1438 | /// be passed in Memory then at least the classification of \arg Lo |
| 1439 | /// will be Memory. |
| 1440 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1441 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1442 | /// |
| 1443 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1444 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1445 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1446 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1447 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1448 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1449 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1450 | unsigned IROffset, QualType SourceTy, |
| 1451 | unsigned SourceOffset) const; |
| 1452 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1453 | unsigned IROffset, QualType SourceTy, |
| 1454 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1455 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1456 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1457 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1458 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1459 | |
| 1460 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1461 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1462 | /// |
| 1463 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1464 | /// available. |
| 1465 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1467 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1468 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1469 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1470 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1471 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1472 | unsigned &neededSSE, |
| 1473 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1474 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1475 | bool IsIllegalVectorType(QualType Ty) const; |
| 1476 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1477 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1478 | /// unfortunately in ways that were not always consistent with |
| 1479 | /// certain previous compilers. In particular, platforms which |
| 1480 | /// required strict binary compatibility with older versions of GCC |
| 1481 | /// may need to exempt themselves. |
| 1482 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1483 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1486 | bool HasAVX; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1487 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1488 | // 64-bit hardware. |
| 1489 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1490 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1491 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1492 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1493 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1494 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1495 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1496 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1497 | bool isPassedUsingAVXType(QualType type) const { |
| 1498 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1499 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1500 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1501 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1502 | if (info.isDirect()) { |
| 1503 | llvm::Type *ty = info.getCoerceToType(); |
| 1504 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1505 | return (vectorTy->getBitWidth() > 128); |
| 1506 | } |
| 1507 | return false; |
| 1508 | } |
| 1509 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1510 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1511 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1512 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1513 | CodeGenFunction &CGF) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1514 | |
| 1515 | bool has64BitPointers() const { |
| 1516 | return Has64BitPointers; |
| 1517 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1518 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1519 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1520 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1521 | class WinX86_64ABIInfo : public ABIInfo { |
| 1522 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1523 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, |
| 1524 | bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1525 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1526 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1527 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1528 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1529 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1530 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1531 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1532 | CodeGenFunction &CGF) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1533 | |
| 1534 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1535 | // FIXME: Assumes vectorcall is in use. |
| 1536 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1537 | } |
| 1538 | |
| 1539 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1540 | uint64_t NumMembers) const override { |
| 1541 | // FIXME: Assumes vectorcall is in use. |
| 1542 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1543 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1544 | }; |
| 1545 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1546 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1547 | bool HasAVX; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1548 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1549 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1550 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1551 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1552 | const X86_64ABIInfo &getABIInfo() const { |
| 1553 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1554 | } |
| 1555 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1556 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1557 | return 7; |
| 1558 | } |
| 1559 | |
| 1560 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1561 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1562 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1563 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1564 | // 0-15 are the 16 integer registers. |
| 1565 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1566 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1567 | return false; |
| 1568 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1569 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1570 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1571 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1572 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1573 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1574 | } |
| 1575 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1576 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1577 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1578 | // The default CC on x86-64 sets %al to the number of SSA |
| 1579 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1580 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1581 | // that when AVX types are involved: the ABI explicitly states it is |
| 1582 | // undefined, and it doesn't work in practice because of how the ABI |
| 1583 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1584 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1585 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1586 | for (CallArgList::const_iterator |
| 1587 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1588 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1589 | HasAVXType = true; |
| 1590 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1591 | } |
| 1592 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1593 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1594 | if (!HasAVXType) |
| 1595 | return true; |
| 1596 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1597 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1598 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1601 | llvm::Constant * |
| 1602 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 1603 | unsigned Sig; |
| 1604 | if (getABIInfo().has64BitPointers()) |
| 1605 | Sig = (0xeb << 0) | // jmp rel8 |
| 1606 | (0x0a << 8) | // .+0x0c |
| 1607 | ('F' << 16) | |
| 1608 | ('T' << 24); |
| 1609 | else |
| 1610 | Sig = (0xeb << 0) | // jmp rel8 |
| 1611 | (0x06 << 8) | // .+0x08 |
| 1612 | ('F' << 16) | |
| 1613 | ('T' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1614 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1615 | } |
| 1616 | |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1617 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 1618 | return HasAVX ? 32 : 16; |
| 1619 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1620 | }; |
| 1621 | |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 1622 | class PS4TargetCodeGenInfo : public X86_64TargetCodeGenInfo { |
| 1623 | public: |
| 1624 | PS4TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 1625 | : X86_64TargetCodeGenInfo(CGT, HasAVX) {} |
| 1626 | |
| 1627 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 1628 | llvm::SmallString<24> &Opt) const { |
| 1629 | Opt = "\01"; |
| 1630 | Opt += Lib; |
| 1631 | } |
| 1632 | }; |
| 1633 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1634 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1635 | // If the argument does not end in .lib, automatically add the suffix. |
| 1636 | // If the argument contains a space, enclose it in quotes. |
| 1637 | // This matches the behavior of MSVC. |
| 1638 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 1639 | std::string ArgStr = Quote ? "\"" : ""; |
| 1640 | ArgStr += Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1641 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1642 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 1643 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1644 | return ArgStr; |
| 1645 | } |
| 1646 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1647 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1648 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1649 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1650 | bool d, bool p, bool w, unsigned RegParms) |
| 1651 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1652 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1653 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1654 | CodeGen::CodeGenModule &CGM) const override; |
| 1655 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1656 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1657 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1658 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1659 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1660 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1661 | |
| 1662 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1663 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1664 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1665 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1666 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1667 | }; |
| 1668 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1669 | static void addStackProbeSizeTargetAttribute(const Decl *D, |
| 1670 | llvm::GlobalValue *GV, |
| 1671 | CodeGen::CodeGenModule &CGM) { |
| 1672 | if (isa<FunctionDecl>(D)) { |
| 1673 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) { |
| 1674 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1675 | |
| 1676 | Fn->addFnAttr("stack-probe-size", llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 1677 | } |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | void WinX86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1682 | llvm::GlobalValue *GV, |
| 1683 | CodeGen::CodeGenModule &CGM) const { |
| 1684 | X86_32TargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 1685 | |
| 1686 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1687 | } |
| 1688 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1689 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1690 | bool HasAVX; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1691 | public: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1692 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 1693 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)), HasAVX(HasAVX) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1694 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1695 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 1696 | CodeGen::CodeGenModule &CGM) const override; |
| 1697 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1698 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1699 | return 7; |
| 1700 | } |
| 1701 | |
| 1702 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1703 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1704 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1706 | // 0-15 are the 16 integer registers. |
| 1707 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1708 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1709 | return false; |
| 1710 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1711 | |
| 1712 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1713 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1714 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1715 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1716 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1717 | |
| 1718 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1719 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1720 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1721 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1722 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1723 | |
| 1724 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 1725 | return HasAVX ? 32 : 16; |
| 1726 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1727 | }; |
| 1728 | |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 1729 | void WinX86_64TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1730 | llvm::GlobalValue *GV, |
| 1731 | CodeGen::CodeGenModule &CGM) const { |
| 1732 | TargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 1733 | |
| 1734 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 1735 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1738 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1739 | Class &Hi) const { |
| 1740 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1741 | // |
| 1742 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1743 | // memory. |
| 1744 | // |
| 1745 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1746 | // memory. |
| 1747 | // |
| 1748 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1749 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1750 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1751 | // ABI working for processors that don't support the __m256 type. |
| 1752 | // |
| 1753 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1754 | // |
| 1755 | // Some of these are enforced by the merging logic. Others can arise |
| 1756 | // only with unions; for example: |
| 1757 | // union { _Complex double; unsigned; } |
| 1758 | // |
| 1759 | // Note that clauses (b) and (c) were added in 0.98. |
| 1760 | // |
| 1761 | if (Hi == Memory) |
| 1762 | Lo = Memory; |
| 1763 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1764 | Lo = Memory; |
| 1765 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1766 | Lo = Memory; |
| 1767 | if (Hi == SSEUp && Lo != SSE) |
| 1768 | Hi = SSE; |
| 1769 | } |
| 1770 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1771 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1772 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1773 | // classified recursively so that always two fields are |
| 1774 | // considered. The resulting class is calculated according to |
| 1775 | // the classes of the fields in the eightbyte: |
| 1776 | // |
| 1777 | // (a) If both classes are equal, this is the resulting class. |
| 1778 | // |
| 1779 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1780 | // the other class. |
| 1781 | // |
| 1782 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1783 | // class. |
| 1784 | // |
| 1785 | // (d) If one of the classes is INTEGER, the result is the |
| 1786 | // INTEGER. |
| 1787 | // |
| 1788 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1789 | // MEMORY is used as class. |
| 1790 | // |
| 1791 | // (f) Otherwise class SSE is used. |
| 1792 | |
| 1793 | // Accum should never be memory (we should have returned) or |
| 1794 | // ComplexX87 (because this cannot be passed in a structure). |
| 1795 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1796 | "Invalid accumulated classification during merge."); |
| 1797 | if (Accum == Field || Field == NoClass) |
| 1798 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1799 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1800 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1801 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1802 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1803 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1804 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1805 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1806 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1807 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1808 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1811 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1812 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1813 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1814 | // Class pairs with appropriate constructor methods for the various |
| 1815 | // situations. |
| 1816 | |
| 1817 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1818 | // shouldn't be passed in registers for example, so there is no chance they |
| 1819 | // can straddle an eightbyte. Verify & simplify. |
| 1820 | |
| 1821 | Lo = Hi = NoClass; |
| 1822 | |
| 1823 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1824 | Current = Memory; |
| 1825 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1826 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1827 | BuiltinType::Kind k = BT->getKind(); |
| 1828 | |
| 1829 | if (k == BuiltinType::Void) { |
| 1830 | Current = NoClass; |
| 1831 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1832 | Lo = Integer; |
| 1833 | Hi = Integer; |
| 1834 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1835 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1836 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1837 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1838 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1839 | Current = SSE; |
| 1840 | } else if (k == BuiltinType::LongDouble) { |
| 1841 | Lo = X87; |
| 1842 | Hi = X87Up; |
| 1843 | } |
| 1844 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1845 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1846 | return; |
| 1847 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1849 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1850 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1851 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1852 | return; |
| 1853 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1854 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1855 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1856 | Current = Integer; |
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 (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1861 | if (Ty->isMemberFunctionPointerType()) { |
| 1862 | if (Has64BitPointers) { |
| 1863 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 1864 | // Lo and Hi now. |
| 1865 | Lo = Hi = Integer; |
| 1866 | } else { |
| 1867 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 1868 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 1869 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 1870 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 1871 | if (EB_FuncPtr != EB_ThisAdj) { |
| 1872 | Lo = Hi = Integer; |
| 1873 | } else { |
| 1874 | Current = Integer; |
| 1875 | } |
| 1876 | } |
| 1877 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1878 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 1879 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1880 | return; |
| 1881 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1882 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1883 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1884 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1885 | if (Size == 32) { |
| 1886 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1887 | // float> as integer. |
| 1888 | Current = Integer; |
| 1889 | |
| 1890 | // If this type crosses an eightbyte boundary, it should be |
| 1891 | // split. |
| 1892 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1893 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1894 | if (EB_Real != EB_Imag) |
| 1895 | Hi = Lo; |
| 1896 | } else if (Size == 64) { |
| 1897 | // gcc passes <1 x double> in memory. :( |
| 1898 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1899 | return; |
| 1900 | |
| 1901 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1902 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1903 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1904 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1905 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1906 | Current = Integer; |
| 1907 | else |
| 1908 | Current = SSE; |
| 1909 | |
| 1910 | // If this type crosses an eightbyte boundary, it should be |
| 1911 | // split. |
| 1912 | if (OffsetBase && OffsetBase != 64) |
| 1913 | Hi = Lo; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1914 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1915 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1916 | // least significant one belongs to class SSE and all the others to class |
| 1917 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1918 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1919 | // This design isn't correct for 256-bits, but since there're no cases |
| 1920 | // where the upper parts would need to be inspected, avoid adding |
| 1921 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1922 | // |
| 1923 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1924 | // registers if they are "named", i.e. not part of the "..." of a |
| 1925 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1926 | Lo = SSE; |
| 1927 | Hi = SSEUp; |
| 1928 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1929 | return; |
| 1930 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1931 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1932 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1933 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1934 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1935 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1936 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1937 | if (Size <= 64) |
| 1938 | Current = Integer; |
| 1939 | else if (Size <= 128) |
| 1940 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1941 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1942 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1943 | else if (ET == getContext().DoubleTy || |
| 1944 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1945 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1946 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1947 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1948 | Current = ComplexX87; |
| 1949 | |
| 1950 | // If this complex type crosses an eightbyte boundary then it |
| 1951 | // should be split. |
| 1952 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1953 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1954 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1955 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1956 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1957 | return; |
| 1958 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1959 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1960 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1961 | // Arrays are treated like structures. |
| 1962 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1963 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1964 | |
| 1965 | // 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] | 1966 | // than four eightbytes, ..., it has class MEMORY. |
| 1967 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1968 | return; |
| 1969 | |
| 1970 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1971 | // fields, it has class MEMORY. |
| 1972 | // |
| 1973 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1974 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1975 | return; |
| 1976 | |
| 1977 | // Otherwise implement simplified merge. We could be smarter about |
| 1978 | // this, but it isn't worth it and would be harder to verify. |
| 1979 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1980 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1981 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1982 | |
| 1983 | // The only case a 256-bit wide vector could be used is when the array |
| 1984 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1985 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1986 | if (Size > 128 && EltSize != 256) |
| 1987 | return; |
| 1988 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1989 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1990 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1991 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1992 | Lo = merge(Lo, FieldLo); |
| 1993 | Hi = merge(Hi, FieldHi); |
| 1994 | if (Lo == Memory || Hi == Memory) |
| 1995 | break; |
| 1996 | } |
| 1997 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1998 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1999 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2000 | return; |
| 2001 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2002 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2003 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2004 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2005 | |
| 2006 | // 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] | 2007 | // than four eightbytes, ..., it has class MEMORY. |
| 2008 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2009 | return; |
| 2010 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2011 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2012 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2013 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2014 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2015 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2016 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2017 | const RecordDecl *RD = RT->getDecl(); |
| 2018 | |
| 2019 | // Assume variable sized types are passed in memory. |
| 2020 | if (RD->hasFlexibleArrayMember()) |
| 2021 | return; |
| 2022 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2023 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Reset Lo class, this will be recomputed. |
| 2026 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2027 | |
| 2028 | // If this is a C++ record, classify the bases first. |
| 2029 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2030 | for (const auto &I : CXXRD->bases()) { |
| 2031 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2032 | "Unexpected base class!"); |
| 2033 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2034 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2035 | |
| 2036 | // Classify this field. |
| 2037 | // |
| 2038 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2039 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2040 | // initialized to class NO_CLASS. |
| 2041 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2042 | uint64_t Offset = |
| 2043 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2044 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2045 | Lo = merge(Lo, FieldLo); |
| 2046 | Hi = merge(Hi, FieldHi); |
| 2047 | if (Lo == Memory || Hi == Memory) |
| 2048 | break; |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2053 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2054 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2055 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2056 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2057 | bool BitField = i->isBitField(); |
| 2058 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2059 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2060 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2061 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2062 | // The only case a 256-bit wide vector could be used is when the struct |
| 2063 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2064 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2065 | // |
| 2066 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 2067 | Lo = Memory; |
| 2068 | return; |
| 2069 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2070 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2071 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2072 | Lo = Memory; |
| 2073 | return; |
| 2074 | } |
| 2075 | |
| 2076 | // Classify this field. |
| 2077 | // |
| 2078 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2079 | // exceeds a single eightbyte, each is classified |
| 2080 | // separately. Each eightbyte gets initialized to class |
| 2081 | // NO_CLASS. |
| 2082 | Class FieldLo, FieldHi; |
| 2083 | |
| 2084 | // Bit-fields require special handling, they do not force the |
| 2085 | // structure to be passed in memory even if unaligned, and |
| 2086 | // therefore they can straddle an eightbyte. |
| 2087 | if (BitField) { |
| 2088 | // Ignore padding bit-fields. |
| 2089 | if (i->isUnnamedBitfield()) |
| 2090 | continue; |
| 2091 | |
| 2092 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2093 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2094 | |
| 2095 | uint64_t EB_Lo = Offset / 64; |
| 2096 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2097 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2098 | if (EB_Lo) { |
| 2099 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2100 | FieldLo = NoClass; |
| 2101 | FieldHi = Integer; |
| 2102 | } else { |
| 2103 | FieldLo = Integer; |
| 2104 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2105 | } |
| 2106 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2107 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2108 | Lo = merge(Lo, FieldLo); |
| 2109 | Hi = merge(Hi, FieldHi); |
| 2110 | if (Lo == Memory || Hi == Memory) |
| 2111 | break; |
| 2112 | } |
| 2113 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2114 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2118 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2119 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2120 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2121 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2122 | // Treat an enum type as its underlying type. |
| 2123 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2124 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2125 | |
| 2126 | return (Ty->isPromotableIntegerType() ? |
| 2127 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 2128 | } |
| 2129 | |
| 2130 | return ABIArgInfo::getIndirect(0); |
| 2131 | } |
| 2132 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2133 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2134 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2135 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 2136 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 2137 | if (Size <= 64 || Size > LargestVector) |
| 2138 | return true; |
| 2139 | } |
| 2140 | |
| 2141 | return false; |
| 2142 | } |
| 2143 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2144 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2145 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2146 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2147 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2148 | // |
| 2149 | // This assumption is optimistic, as there could be free registers available |
| 2150 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2151 | // the argument in the free register. This does not seem to happen currently, |
| 2152 | // but this code would be much safer if we could mark the argument with |
| 2153 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2154 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2155 | // Treat an enum type as its underlying type. |
| 2156 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2157 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2158 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 2159 | return (Ty->isPromotableIntegerType() ? |
| 2160 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2161 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2162 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2163 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2164 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2165 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2166 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2167 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2168 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2169 | |
| 2170 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2171 | // is important for good codegen. |
| 2172 | // |
| 2173 | // We do this by coercing the value into a scalar type which the backend can |
| 2174 | // handle naturally (i.e., without using byval). |
| 2175 | // |
| 2176 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2177 | // free integer registers. Doing this when there are free integer registers |
| 2178 | // would require more care, as we would have to ensure that the coerced value |
| 2179 | // did not claim the unused register. That would require either reording the |
| 2180 | // arguments to the function (so that any subsequent inreg values came first), |
| 2181 | // or only doing this optimization when there were no following arguments that |
| 2182 | // might be inreg. |
| 2183 | // |
| 2184 | // We currently expect it to be rare (particularly in well written code) for |
| 2185 | // arguments to be passed on the stack when there are still free integer |
| 2186 | // registers available (this would typically imply large structs being passed |
| 2187 | // by value), so this seems like a fair tradeoff for now. |
| 2188 | // |
| 2189 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2190 | // attributes. See PR12193. |
| 2191 | if (freeIntRegs == 0) { |
| 2192 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2193 | |
| 2194 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2195 | // type, which will end up on the stack (with alignment 8). |
| 2196 | if (Align == 8 && Size <= 64) |
| 2197 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2198 | Size)); |
| 2199 | } |
| 2200 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2201 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2204 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 2205 | /// 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] | 2206 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2207 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 2208 | // vectors; strip them off if present. |
| 2209 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 2210 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2211 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 2212 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 2213 | assert(isa<llvm::VectorType>(IRType) && |
| 2214 | "Trying to return a non-vector type in a vector register!"); |
| 2215 | return IRType; |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2218 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2219 | /// is known to either be off the end of the specified type or being in |
| 2220 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2221 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2222 | /// classification that put one of the two halves in the INTEGER class. |
| 2223 | /// |
| 2224 | /// It is conservatively correct to return false. |
| 2225 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2226 | unsigned EndBit, ASTContext &Context) { |
| 2227 | // If the bytes being queried are off the end of the type, there is no user |
| 2228 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2229 | // types that don't contain interesting padding. |
| 2230 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2231 | if (TySize <= StartBit) |
| 2232 | return true; |
| 2233 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2234 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2235 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2236 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2237 | |
| 2238 | // Check each element to see if the element overlaps with the queried range. |
| 2239 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2240 | // If the element is after the span we care about, then we're done.. |
| 2241 | unsigned EltOffset = i*EltSize; |
| 2242 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2243 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2244 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2245 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2246 | EndBit-EltOffset, Context)) |
| 2247 | return false; |
| 2248 | } |
| 2249 | // If it overlaps no elements, then it is safe to process as padding. |
| 2250 | return true; |
| 2251 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2252 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2253 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2254 | const RecordDecl *RD = RT->getDecl(); |
| 2255 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2256 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2257 | // If this is a C++ record, check the bases first. |
| 2258 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2259 | for (const auto &I : CXXRD->bases()) { |
| 2260 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2261 | "Unexpected base class!"); |
| 2262 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2263 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2264 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2265 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2266 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2267 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2268 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2269 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2270 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2271 | EndBit-BaseOffset, Context)) |
| 2272 | return false; |
| 2273 | } |
| 2274 | } |
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 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2277 | // this could be sped up a lot by being smarter about queried fields, |
| 2278 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2279 | // much. |
| 2280 | unsigned idx = 0; |
| 2281 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2282 | i != e; ++i, ++idx) { |
| 2283 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2284 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2285 | // If we found a field after the region we care about, then we're done. |
| 2286 | if (FieldOffset >= EndBit) break; |
| 2287 | |
| 2288 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2289 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2290 | Context)) |
| 2291 | return false; |
| 2292 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2293 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2294 | // If nothing in this record overlapped the area of interest, then we're |
| 2295 | // clean. |
| 2296 | return true; |
| 2297 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2298 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2299 | return false; |
| 2300 | } |
| 2301 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2302 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2303 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2304 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2305 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2306 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2307 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2308 | // Base case if we find a float. |
| 2309 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2310 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2311 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2312 | // 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] | 2313 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2314 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2315 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2316 | IROffset -= SL->getElementOffset(Elt); |
| 2317 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2318 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2319 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2320 | // 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] | 2321 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2322 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2323 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2324 | IROffset -= IROffset/EltSize*EltSize; |
| 2325 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2326 | } |
| 2327 | |
| 2328 | return false; |
| 2329 | } |
| 2330 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2331 | |
| 2332 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2333 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2334 | llvm::Type *X86_64ABIInfo:: |
| 2335 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2336 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2337 | // 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] | 2338 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2339 | // structs that contain 3 floats. |
| 2340 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2341 | SourceOffset*8+64, getContext())) |
| 2342 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2343 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2344 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2345 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2346 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2347 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2348 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2349 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2351 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2352 | } |
| 2353 | |
| 2354 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2355 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2356 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2357 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2358 | /// 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] | 2359 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2360 | /// etc). |
| 2361 | /// |
| 2362 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2363 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2364 | /// the 8-byte value references. PrefType may be null. |
| 2365 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2366 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2367 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2368 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2369 | llvm::Type *X86_64ABIInfo:: |
| 2370 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2371 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2372 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2373 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2374 | if (IROffset == 0) { |
| 2375 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2376 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2377 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2378 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2379 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2380 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2381 | // goodness in the source type is just tail padding. This is allowed to |
| 2382 | // kick in for struct {double,int} on the int, but not on |
| 2383 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2384 | // have to do this analysis on the source type because we can't depend on |
| 2385 | // unions being lowered a specific way etc. |
| 2386 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2387 | IRType->isIntegerTy(32) || |
| 2388 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2389 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2390 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2391 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2392 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2393 | SourceOffset*8+64, getContext())) |
| 2394 | return IRType; |
| 2395 | } |
| 2396 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2397 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2398 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2399 | // 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] | 2400 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2401 | if (IROffset < SL->getSizeInBytes()) { |
| 2402 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2403 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2404 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2405 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2406 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2407 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2408 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2409 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2410 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2411 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2412 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2413 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2414 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2415 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2416 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2417 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2418 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2419 | // 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] | 2420 | unsigned TySizeInBytes = |
| 2421 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2422 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2423 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2425 | // It is always safe to classify this as an integer type up to i64 that |
| 2426 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2427 | return llvm::IntegerType::get(getVMContext(), |
| 2428 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2431 | |
| 2432 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2433 | /// be used as elements of a two register pair to pass or return, return a |
| 2434 | /// first class aggregate to represent them. For example, if the low part of |
| 2435 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2436 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2437 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2438 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2439 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2440 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2441 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2442 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2443 | // the second element at offset 8. Check for this: |
| 2444 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2445 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
David Majnemer | ed68407 | 2014-10-20 06:13:36 +0000 | [diff] [blame] | 2446 | unsigned HiStart = llvm::RoundUpToAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2447 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2448 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2449 | // To handle this, we have to increase the size of the low part so that the |
| 2450 | // second element will start at an 8 byte offset. We can't increase the size |
| 2451 | // of the second element because it might make us access off the end of the |
| 2452 | // struct. |
| 2453 | if (HiStart != 8) { |
| 2454 | // There are only two sorts of types the ABI generation code can produce for |
| 2455 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2456 | // Promote these to a larger type. |
| 2457 | if (Lo->isFloatTy()) |
| 2458 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2459 | else { |
| 2460 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2461 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2462 | } |
| 2463 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2464 | |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2465 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, nullptr); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2466 | |
| 2467 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2468 | // Verify that the second element is at an 8-byte offset. |
| 2469 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2470 | "Invalid x86-64 argument pair!"); |
| 2471 | return Result; |
| 2472 | } |
| 2473 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2474 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2475 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2476 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2477 | // classification algorithm. |
| 2478 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2479 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2480 | |
| 2481 | // Check some invariants. |
| 2482 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2483 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2484 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2485 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2486 | switch (Lo) { |
| 2487 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2488 | if (Hi == NoClass) |
| 2489 | return ABIArgInfo::getIgnore(); |
| 2490 | // If the low part is just padding, it takes no register, leave ResType |
| 2491 | // null. |
| 2492 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2493 | "Unknown missing lo part"); |
| 2494 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2495 | |
| 2496 | case SSEUp: |
| 2497 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2498 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2499 | |
| 2500 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2501 | // hidden argument. |
| 2502 | case Memory: |
| 2503 | return getIndirectReturnResult(RetTy); |
| 2504 | |
| 2505 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2506 | // available register of the sequence %rax, %rdx is used. |
| 2507 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2508 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2509 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2510 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2511 | // so that the parameter gets the right LLVM IR attributes. |
| 2512 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2513 | // Treat an enum type as its underlying type. |
| 2514 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2515 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2516 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2517 | if (RetTy->isIntegralOrEnumerationType() && |
| 2518 | RetTy->isPromotableIntegerType()) |
| 2519 | return ABIArgInfo::getExtend(); |
| 2520 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2521 | break; |
| 2522 | |
| 2523 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2524 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2525 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2526 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2527 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2528 | |
| 2529 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2530 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2531 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2532 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2533 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2534 | |
| 2535 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2536 | // part of the value is returned in %st0 and the imaginary part in |
| 2537 | // %st1. |
| 2538 | case ComplexX87: |
| 2539 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2540 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2541 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2542 | nullptr); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2543 | break; |
| 2544 | } |
| 2545 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2546 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2547 | switch (Hi) { |
| 2548 | // Memory was handled previously and X87 should |
| 2549 | // never occur as a hi class. |
| 2550 | case Memory: |
| 2551 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2552 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2553 | |
| 2554 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2555 | case NoClass: |
| 2556 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2557 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2558 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2559 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2560 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2561 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2562 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2563 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2564 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2565 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2566 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2567 | break; |
| 2568 | |
| 2569 | // 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] | 2570 | // is passed in the next available eightbyte chunk if the last used |
| 2571 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2572 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2573 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2574 | case SSEUp: |
| 2575 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2576 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2577 | break; |
| 2578 | |
| 2579 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2580 | // returned together with the previous X87 value in %st0. |
| 2581 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2582 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2583 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2584 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2585 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2586 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2587 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2588 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2589 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2590 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2591 | break; |
| 2592 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2593 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2594 | // 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] | 2595 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2596 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2597 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2598 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2599 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2600 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2603 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2604 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2605 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2606 | const |
| 2607 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 2608 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 2609 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2610 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2611 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2612 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2613 | // Check some invariants. |
| 2614 | // FIXME: Enforce these by construction. |
| 2615 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2616 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2617 | |
| 2618 | neededInt = 0; |
| 2619 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2620 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2621 | switch (Lo) { |
| 2622 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2623 | if (Hi == NoClass) |
| 2624 | return ABIArgInfo::getIgnore(); |
| 2625 | // If the low part is just padding, it takes no register, leave ResType |
| 2626 | // null. |
| 2627 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2628 | "Unknown missing lo part"); |
| 2629 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2630 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2631 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2632 | // on the stack. |
| 2633 | case Memory: |
| 2634 | |
| 2635 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2636 | // COMPLEX_X87, it is passed in memory. |
| 2637 | case X87: |
| 2638 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2639 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2640 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2641 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2642 | |
| 2643 | case SSEUp: |
| 2644 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2645 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2646 | |
| 2647 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2648 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2649 | // and %r9 is used. |
| 2650 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2651 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2653 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2654 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2655 | |
| 2656 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2657 | // so that the parameter gets the right LLVM IR attributes. |
| 2658 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2659 | // Treat an enum type as its underlying type. |
| 2660 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2661 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2663 | if (Ty->isIntegralOrEnumerationType() && |
| 2664 | Ty->isPromotableIntegerType()) |
| 2665 | return ABIArgInfo::getExtend(); |
| 2666 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2667 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2668 | break; |
| 2669 | |
| 2670 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2671 | // available SSE register is used, the registers are taken in the |
| 2672 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2673 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2674 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2675 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2676 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2677 | break; |
| 2678 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2679 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2680 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2681 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2682 | switch (Hi) { |
| 2683 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2684 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2685 | // which is passed in memory. |
| 2686 | case Memory: |
| 2687 | case X87: |
| 2688 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2689 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2690 | |
| 2691 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2693 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2694 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2695 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2696 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2698 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2699 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2700 | break; |
| 2701 | |
| 2702 | // X87Up generally doesn't occur here (long double is passed in |
| 2703 | // memory), except in situations involving unions. |
| 2704 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2705 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2706 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2707 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2708 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2709 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2710 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2711 | ++neededSSE; |
| 2712 | break; |
| 2713 | |
| 2714 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2715 | // 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] | 2716 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2717 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2718 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2719 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2720 | break; |
| 2721 | } |
| 2722 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2723 | // If a high part was specified, merge it together with the low part. It is |
| 2724 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2725 | // first class struct aggregate with the high and low part: {low, high} |
| 2726 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2727 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2728 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2729 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2732 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2733 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2734 | if (!getCXXABI().classifyReturnType(FI)) |
| 2735 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2738 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2739 | |
| 2740 | // If the return value is indirect, then the hidden argument is consuming one |
| 2741 | // integer register. |
| 2742 | if (FI.getReturnInfo().isIndirect()) |
| 2743 | --freeIntRegs; |
| 2744 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 2745 | // The chain argument effectively gives us another free register. |
| 2746 | if (FI.isChainCall()) |
| 2747 | ++freeIntRegs; |
| 2748 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2749 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2750 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2751 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2752 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2753 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2754 | it != ie; ++it, ++ArgNo) { |
| 2755 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2756 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2757 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2758 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2759 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2760 | |
| 2761 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2762 | // eightbyte of an argument, the whole argument is passed on the |
| 2763 | // stack. If registers have already been assigned for some |
| 2764 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2765 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2766 | freeIntRegs -= neededInt; |
| 2767 | freeSSERegs -= neededSSE; |
| 2768 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2769 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2775 | QualType Ty, |
| 2776 | CodeGenFunction &CGF) { |
| 2777 | llvm::Value *overflow_arg_area_p = |
| 2778 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2779 | llvm::Value *overflow_arg_area = |
| 2780 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2781 | |
| 2782 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2783 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2784 | // It isn't stated explicitly in the standard, but in practice we use |
| 2785 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2786 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2787 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2788 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2789 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2790 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2791 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2792 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2793 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2794 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2795 | overflow_arg_area = |
| 2796 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2797 | overflow_arg_area->getType(), |
| 2798 | "overflow_arg_area.align"); |
| 2799 | } |
| 2800 | |
| 2801 | // 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] | 2802 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2803 | llvm::Value *Res = |
| 2804 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2805 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2806 | |
| 2807 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2808 | // l->overflow_arg_area + sizeof(type). |
| 2809 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2810 | // an 8 byte boundary. |
| 2811 | |
| 2812 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2813 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2814 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2815 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2816 | "overflow_arg_area.next"); |
| 2817 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2818 | |
| 2819 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2820 | return Res; |
| 2821 | } |
| 2822 | |
| 2823 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2824 | CodeGenFunction &CGF) const { |
| 2825 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2826 | // struct { |
| 2827 | // i32 gp_offset; |
| 2828 | // i32 fp_offset; |
| 2829 | // i8* overflow_arg_area; |
| 2830 | // i8* reg_save_area; |
| 2831 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2832 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2833 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2834 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2835 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2836 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2837 | |
| 2838 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2839 | // in the registers. If not go to step 7. |
| 2840 | if (!neededInt && !neededSSE) |
| 2841 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2842 | |
| 2843 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2844 | // general purpose registers needed to pass type and num_fp to hold |
| 2845 | // the number of floating point registers needed. |
| 2846 | |
| 2847 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2848 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2849 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2850 | // |
| 2851 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2852 | // register save space). |
| 2853 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2854 | llvm::Value *InRegs = nullptr; |
| 2855 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2856 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2857 | if (neededInt) { |
| 2858 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2859 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2860 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2861 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2862 | } |
| 2863 | |
| 2864 | if (neededSSE) { |
| 2865 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2866 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2867 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2868 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2869 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2870 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2871 | } |
| 2872 | |
| 2873 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2874 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2875 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2876 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2877 | |
| 2878 | // Emit code to load the value if it was passed in registers. |
| 2879 | |
| 2880 | CGF.EmitBlock(InRegBlock); |
| 2881 | |
| 2882 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2883 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2884 | // copying to a temporary location in case the parameter is passed |
| 2885 | // in different register classes or requires an alignment greater |
| 2886 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2887 | // |
| 2888 | // FIXME: This really results in shameful code when we end up needing to |
| 2889 | // collect arguments from different places; often what should result in a |
| 2890 | // simple assembling of a structure from scattered addresses has many more |
| 2891 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2892 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2893 | llvm::Value *RegAddr = |
| 2894 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2895 | "reg_save_area"); |
| 2896 | if (neededInt && neededSSE) { |
| 2897 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2898 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2899 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2900 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2901 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2902 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2903 | llvm::Type *TyLo = ST->getElementType(0); |
| 2904 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2905 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2906 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2907 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2908 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2909 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2910 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2911 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2912 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2913 | llvm::Value *V = |
| 2914 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2915 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2916 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2917 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2918 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2919 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2920 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2921 | } else if (neededInt) { |
| 2922 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2923 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2924 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2925 | |
| 2926 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2927 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2928 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2929 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2930 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2931 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2932 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2933 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2934 | RegAddr = Tmp; |
| 2935 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2936 | } else if (neededSSE == 1) { |
| 2937 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2938 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2939 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2940 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2941 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2942 | // SSE registers are spaced 16 bytes apart in the register save |
| 2943 | // area, we need to collect the two eightbytes together. |
| 2944 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2945 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2946 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2947 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2948 | llvm::PointerType::getUnqual(DoubleTy); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 2949 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, nullptr); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2950 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2951 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2952 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2953 | DblPtrTy)); |
| 2954 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2955 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2956 | DblPtrTy)); |
| 2957 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2958 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2959 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2963 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2964 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2965 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2966 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2967 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2968 | gp_offset_p); |
| 2969 | } |
| 2970 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2971 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2972 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2973 | fp_offset_p); |
| 2974 | } |
| 2975 | CGF.EmitBranch(ContBlock); |
| 2976 | |
| 2977 | // Emit code to load the value if it was passed in memory. |
| 2978 | |
| 2979 | CGF.EmitBlock(InMemBlock); |
| 2980 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2981 | |
| 2982 | // Return the appropriate result. |
| 2983 | |
| 2984 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2985 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2986 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2987 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2988 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2989 | return ResAddr; |
| 2990 | } |
| 2991 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2992 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
| 2993 | bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2994 | |
| 2995 | if (Ty->isVoidType()) |
| 2996 | return ABIArgInfo::getIgnore(); |
| 2997 | |
| 2998 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2999 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3000 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3001 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3002 | uint64_t Width = Info.Width; |
| 3003 | unsigned Align = getContext().toCharUnitsFromBits(Info.Align).getQuantity(); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3004 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3005 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3006 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3007 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3008 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3009 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 3010 | } |
| 3011 | |
| 3012 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3013 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3014 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3015 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3016 | if (Width == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3017 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3018 | Width)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3019 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3020 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3021 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3022 | // other targets. |
| 3023 | const Type *Base = nullptr; |
| 3024 | uint64_t NumElts = 0; |
| 3025 | if (FreeSSERegs && isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3026 | if (FreeSSERegs >= NumElts) { |
| 3027 | FreeSSERegs -= NumElts; |
| 3028 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3029 | return ABIArgInfo::getDirect(); |
| 3030 | return ABIArgInfo::getExpand(); |
| 3031 | } |
| 3032 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3033 | } |
| 3034 | |
| 3035 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3036 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3037 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3038 | // directly. |
| 3039 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3040 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3041 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3044 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3045 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3046 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3047 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3048 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3049 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3050 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3051 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 3054 | // Bool type is always extended to the ABI, other builtin types are not |
| 3055 | // extended. |
| 3056 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 3057 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 3058 | return ABIArgInfo::getExtend(); |
| 3059 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3060 | return ABIArgInfo::getDirect(); |
| 3061 | } |
| 3062 | |
| 3063 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3064 | bool IsVectorCall = |
| 3065 | FI.getCallingConvention() == llvm::CallingConv::X86_VectorCall; |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 3066 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3067 | // We can use up to 4 SSE return registers with vectorcall. |
| 3068 | unsigned FreeSSERegs = IsVectorCall ? 4 : 0; |
| 3069 | if (!getCXXABI().classifyReturnType(FI)) |
| 3070 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true); |
| 3071 | |
| 3072 | // We can use up to 6 SSE register parameters with vectorcall. |
| 3073 | FreeSSERegs = IsVectorCall ? 6 : 0; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3074 | for (auto &I : FI.arguments()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3075 | I.info = classify(I.type, FreeSSERegs, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3076 | } |
| 3077 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3078 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3079 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3080 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3081 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 3082 | CGBuilderTy &Builder = CGF.Builder; |
| 3083 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 3084 | "ap"); |
| 3085 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3086 | llvm::Type *PTy = |
| 3087 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3088 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3089 | |
| 3090 | uint64_t Offset = |
| 3091 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 3092 | llvm::Value *NextAddr = |
| 3093 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 3094 | "ap.next"); |
| 3095 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3096 | |
| 3097 | return AddrTyped; |
| 3098 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3099 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3100 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3101 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3102 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 3103 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3104 | public: |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3105 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 3106 | |
| 3107 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3108 | CodeGenFunction &CGF) const override; |
| 3109 | }; |
| 3110 | |
| 3111 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3112 | public: |
| 3113 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3114 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3115 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3116 | // This is recovered from gcc output. |
| 3117 | return 1; // r1 is the dedicated stack pointer |
| 3118 | } |
| 3119 | |
| 3120 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3121 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3122 | |
| 3123 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3124 | return 16; // Natural alignment for Altivec vectors. |
| 3125 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3126 | }; |
| 3127 | |
| 3128 | } |
| 3129 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 3130 | llvm::Value *PPC32_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3131 | QualType Ty, |
| 3132 | CodeGenFunction &CGF) const { |
| 3133 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3134 | // TODO: Implement this. For now ignore. |
| 3135 | (void)CTy; |
| 3136 | return nullptr; |
| 3137 | } |
| 3138 | |
| 3139 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
| 3140 | bool isInt = Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
| 3141 | llvm::Type *CharPtr = CGF.Int8PtrTy; |
| 3142 | llvm::Type *CharPtrPtr = CGF.Int8PtrPtrTy; |
| 3143 | |
| 3144 | CGBuilderTy &Builder = CGF.Builder; |
| 3145 | llvm::Value *GPRPtr = Builder.CreateBitCast(VAListAddr, CharPtr, "gprptr"); |
| 3146 | llvm::Value *GPRPtrAsInt = Builder.CreatePtrToInt(GPRPtr, CGF.Int32Ty); |
| 3147 | llvm::Value *FPRPtrAsInt = Builder.CreateAdd(GPRPtrAsInt, Builder.getInt32(1)); |
| 3148 | llvm::Value *FPRPtr = Builder.CreateIntToPtr(FPRPtrAsInt, CharPtr); |
| 3149 | llvm::Value *OverflowAreaPtrAsInt = Builder.CreateAdd(FPRPtrAsInt, Builder.getInt32(3)); |
| 3150 | llvm::Value *OverflowAreaPtr = Builder.CreateIntToPtr(OverflowAreaPtrAsInt, CharPtrPtr); |
| 3151 | llvm::Value *RegsaveAreaPtrAsInt = Builder.CreateAdd(OverflowAreaPtrAsInt, Builder.getInt32(4)); |
| 3152 | llvm::Value *RegsaveAreaPtr = Builder.CreateIntToPtr(RegsaveAreaPtrAsInt, CharPtrPtr); |
| 3153 | llvm::Value *GPR = Builder.CreateLoad(GPRPtr, false, "gpr"); |
| 3154 | // Align GPR when TY is i64. |
| 3155 | if (isI64) { |
| 3156 | llvm::Value *GPRAnd = Builder.CreateAnd(GPR, Builder.getInt8(1)); |
| 3157 | llvm::Value *CC64 = Builder.CreateICmpEQ(GPRAnd, Builder.getInt8(1)); |
| 3158 | llvm::Value *GPRPlusOne = Builder.CreateAdd(GPR, Builder.getInt8(1)); |
| 3159 | GPR = Builder.CreateSelect(CC64, GPRPlusOne, GPR); |
| 3160 | } |
| 3161 | llvm::Value *FPR = Builder.CreateLoad(FPRPtr, false, "fpr"); |
| 3162 | llvm::Value *OverflowArea = Builder.CreateLoad(OverflowAreaPtr, false, "overflow_area"); |
| 3163 | llvm::Value *OverflowAreaAsInt = Builder.CreatePtrToInt(OverflowArea, CGF.Int32Ty); |
| 3164 | llvm::Value *RegsaveArea = Builder.CreateLoad(RegsaveAreaPtr, false, "regsave_area"); |
| 3165 | llvm::Value *RegsaveAreaAsInt = Builder.CreatePtrToInt(RegsaveArea, CGF.Int32Ty); |
| 3166 | |
| 3167 | llvm::Value *CC = Builder.CreateICmpULT(isInt ? GPR : FPR, |
| 3168 | Builder.getInt8(8), "cond"); |
| 3169 | |
| 3170 | llvm::Value *RegConstant = Builder.CreateMul(isInt ? GPR : FPR, |
| 3171 | Builder.getInt8(isInt ? 4 : 8)); |
| 3172 | |
| 3173 | llvm::Value *OurReg = Builder.CreateAdd(RegsaveAreaAsInt, Builder.CreateSExt(RegConstant, CGF.Int32Ty)); |
| 3174 | |
| 3175 | if (Ty->isFloatingType()) |
| 3176 | OurReg = Builder.CreateAdd(OurReg, Builder.getInt32(32)); |
| 3177 | |
| 3178 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 3179 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 3180 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 3181 | |
| 3182 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 3183 | |
| 3184 | CGF.EmitBlock(UsingRegs); |
| 3185 | |
| 3186 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3187 | llvm::Value *Result1 = Builder.CreateIntToPtr(OurReg, PTy); |
| 3188 | // Increase the GPR/FPR indexes. |
| 3189 | if (isInt) { |
| 3190 | GPR = Builder.CreateAdd(GPR, Builder.getInt8(isI64 ? 2 : 1)); |
| 3191 | Builder.CreateStore(GPR, GPRPtr); |
| 3192 | } else { |
| 3193 | FPR = Builder.CreateAdd(FPR, Builder.getInt8(1)); |
| 3194 | Builder.CreateStore(FPR, FPRPtr); |
| 3195 | } |
| 3196 | CGF.EmitBranch(Cont); |
| 3197 | |
| 3198 | CGF.EmitBlock(UsingOverflow); |
| 3199 | |
| 3200 | // Increase the overflow area. |
| 3201 | llvm::Value *Result2 = Builder.CreateIntToPtr(OverflowAreaAsInt, PTy); |
| 3202 | OverflowAreaAsInt = Builder.CreateAdd(OverflowAreaAsInt, Builder.getInt32(isInt ? 4 : 8)); |
| 3203 | Builder.CreateStore(Builder.CreateIntToPtr(OverflowAreaAsInt, CharPtr), OverflowAreaPtr); |
| 3204 | CGF.EmitBranch(Cont); |
| 3205 | |
| 3206 | CGF.EmitBlock(Cont); |
| 3207 | |
| 3208 | llvm::PHINode *Result = CGF.Builder.CreatePHI(PTy, 2, "vaarg.addr"); |
| 3209 | Result->addIncoming(Result1, UsingRegs); |
| 3210 | Result->addIncoming(Result2, UsingOverflow); |
| 3211 | |
| 3212 | if (Ty->isAggregateType()) { |
| 3213 | llvm::Value *AGGPtr = Builder.CreateBitCast(Result, CharPtrPtr, "aggrptr") ; |
| 3214 | return Builder.CreateLoad(AGGPtr, false, "aggr"); |
| 3215 | } |
| 3216 | |
| 3217 | return Result; |
| 3218 | } |
| 3219 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3220 | bool |
| 3221 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3222 | llvm::Value *Address) const { |
| 3223 | // This is calculated from the LLVM and GCC tables and verified |
| 3224 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3225 | |
| 3226 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3227 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3228 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3229 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3230 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3231 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3232 | |
| 3233 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3234 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3235 | |
| 3236 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3237 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3238 | |
| 3239 | // 64-76 are various 4-byte special-purpose registers: |
| 3240 | // 64: mq |
| 3241 | // 65: lr |
| 3242 | // 66: ctr |
| 3243 | // 67: ap |
| 3244 | // 68-75 cr0-7 |
| 3245 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3246 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3247 | |
| 3248 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3249 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3250 | |
| 3251 | // 109: vrsave |
| 3252 | // 110: vscr |
| 3253 | // 111: spe_acc |
| 3254 | // 112: spefscr |
| 3255 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3256 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3257 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3258 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3261 | // PowerPC-64 |
| 3262 | |
| 3263 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3264 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3265 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3266 | public: |
| 3267 | enum ABIKind { |
| 3268 | ELFv1 = 0, |
| 3269 | ELFv2 |
| 3270 | }; |
| 3271 | |
| 3272 | private: |
| 3273 | static const unsigned GPRBits = 64; |
| 3274 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3275 | bool HasQPX; |
| 3276 | |
| 3277 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 3278 | // will be passed in a QPX register. |
| 3279 | bool IsQPXVectorTy(const Type *Ty) const { |
| 3280 | if (!HasQPX) |
| 3281 | return false; |
| 3282 | |
| 3283 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3284 | unsigned NumElements = VT->getNumElements(); |
| 3285 | if (NumElements == 1) |
| 3286 | return false; |
| 3287 | |
| 3288 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 3289 | if (getContext().getTypeSize(Ty) <= 256) |
| 3290 | return true; |
| 3291 | } else if (VT->getElementType()-> |
| 3292 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 3293 | if (getContext().getTypeSize(Ty) <= 128) |
| 3294 | return true; |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | return false; |
| 3299 | } |
| 3300 | |
| 3301 | bool IsQPXVectorTy(QualType Ty) const { |
| 3302 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 3303 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3304 | |
| 3305 | public: |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3306 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX) |
| 3307 | : DefaultABIInfo(CGT), Kind(Kind), HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3308 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3309 | bool isPromotableTypeForABI(QualType Ty) const; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3310 | bool isAlignedParamType(QualType Ty, bool &Align32) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3311 | |
| 3312 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3313 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3314 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3315 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3316 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3317 | uint64_t Members) const override; |
| 3318 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3319 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3320 | // Example: For aggregate arguments that fit in a register, we could |
| 3321 | // use getDirectInReg (as is done below for structs containing a single |
| 3322 | // floating-point value) to avoid pushing them to memory on function |
| 3323 | // entry. This would require changing the logic in PPCISelLowering |
| 3324 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3325 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3326 | if (!getCXXABI().classifyReturnType(FI)) |
| 3327 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3328 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3329 | // We rely on the default argument classification for the most part. |
| 3330 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3331 | // 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] | 3332 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3333 | if (T) { |
| 3334 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3335 | if (IsQPXVectorTy(T) || |
| 3336 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3337 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3338 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3339 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3340 | continue; |
| 3341 | } |
| 3342 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3343 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3344 | } |
| 3345 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3346 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3347 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3348 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3349 | }; |
| 3350 | |
| 3351 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3352 | bool HasQPX; |
| 3353 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3354 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3355 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3356 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX) |
| 3357 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX)), |
| 3358 | HasQPX(HasQPX) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3359 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3360 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3361 | // This is recovered from gcc output. |
| 3362 | return 1; // r1 is the dedicated stack pointer |
| 3363 | } |
| 3364 | |
| 3365 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3366 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3367 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3368 | unsigned getOpenMPSimdDefaultAlignment(QualType QT) const override { |
| 3369 | if (HasQPX) |
| 3370 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 3371 | if (PT->getPointeeType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 3372 | return 32; // Natural alignment for QPX doubles. |
| 3373 | |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3374 | return 16; // Natural alignment for Altivec and VSX vectors. |
| 3375 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3376 | }; |
| 3377 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3378 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3379 | public: |
| 3380 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3381 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3382 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3383 | // This is recovered from gcc output. |
| 3384 | return 1; // r1 is the dedicated stack pointer |
| 3385 | } |
| 3386 | |
| 3387 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3388 | llvm::Value *Address) const override; |
Hal Finkel | 92e31a5 | 2014-10-03 17:45:20 +0000 | [diff] [blame] | 3389 | |
| 3390 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 3391 | return 16; // Natural alignment for Altivec vectors. |
| 3392 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3393 | }; |
| 3394 | |
| 3395 | } |
| 3396 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3397 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3398 | // extended to 64 bits. |
| 3399 | bool |
| 3400 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3401 | // Treat an enum type as its underlying type. |
| 3402 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3403 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3404 | |
| 3405 | // Promotable integer types are required to be promoted by the ABI. |
| 3406 | if (Ty->isPromotableIntegerType()) |
| 3407 | return true; |
| 3408 | |
| 3409 | // In addition to the usual promotable integer types, we also need to |
| 3410 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3411 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3412 | switch (BT->getKind()) { |
| 3413 | case BuiltinType::Int: |
| 3414 | case BuiltinType::UInt: |
| 3415 | return true; |
| 3416 | default: |
| 3417 | break; |
| 3418 | } |
| 3419 | |
| 3420 | return false; |
| 3421 | } |
| 3422 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3423 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3424 | /// alignment in the parameter area. |
| 3425 | bool |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3426 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty, bool &Align32) const { |
| 3427 | Align32 = false; |
| 3428 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3429 | // Complex types are passed just like their elements. |
| 3430 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3431 | Ty = CTy->getElementType(); |
| 3432 | |
| 3433 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3434 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3435 | if (IsQPXVectorTy(Ty)) { |
| 3436 | if (getContext().getTypeSize(Ty) > 128) |
| 3437 | Align32 = true; |
| 3438 | |
| 3439 | return true; |
| 3440 | } else if (Ty->isVectorType()) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3441 | return getContext().getTypeSize(Ty) == 128; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3442 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3443 | |
| 3444 | // For single-element float/vector structs, we consider the whole type |
| 3445 | // to have the same alignment requirements as its single element. |
| 3446 | const Type *AlignAsType = nullptr; |
| 3447 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3448 | if (EltType) { |
| 3449 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3450 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3451 | getContext().getTypeSize(EltType) == 128) || |
| 3452 | (BT && BT->isFloatingPoint())) |
| 3453 | AlignAsType = EltType; |
| 3454 | } |
| 3455 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3456 | // Likewise for ELFv2 homogeneous aggregates. |
| 3457 | const Type *Base = nullptr; |
| 3458 | uint64_t Members = 0; |
| 3459 | if (!AlignAsType && Kind == ELFv2 && |
| 3460 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3461 | AlignAsType = Base; |
| 3462 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3463 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3464 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 3465 | if (getContext().getTypeSize(AlignAsType) > 128) |
| 3466 | Align32 = true; |
| 3467 | |
| 3468 | return true; |
| 3469 | } else if (AlignAsType) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3470 | return AlignAsType->isVectorType(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3471 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3472 | |
| 3473 | // Otherwise, we only need alignment for any aggregate type that |
| 3474 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3475 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 3476 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
| 3477 | Align32 = true; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3478 | return true; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3479 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3480 | |
| 3481 | return false; |
| 3482 | } |
| 3483 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3484 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3485 | /// aggregate. Base is set to the base element type, and Members is set |
| 3486 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3487 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3488 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3489 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3490 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3491 | if (NElements == 0) |
| 3492 | return false; |
| 3493 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3494 | return false; |
| 3495 | Members *= NElements; |
| 3496 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3497 | const RecordDecl *RD = RT->getDecl(); |
| 3498 | if (RD->hasFlexibleArrayMember()) |
| 3499 | return false; |
| 3500 | |
| 3501 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 3502 | |
| 3503 | // If this is a C++ record, check the bases first. |
| 3504 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 3505 | for (const auto &I : CXXRD->bases()) { |
| 3506 | // Ignore empty records. |
| 3507 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 3508 | continue; |
| 3509 | |
| 3510 | uint64_t FldMembers; |
| 3511 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 3512 | return false; |
| 3513 | |
| 3514 | Members += FldMembers; |
| 3515 | } |
| 3516 | } |
| 3517 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3518 | for (const auto *FD : RD->fields()) { |
| 3519 | // Ignore (non-zero arrays of) empty records. |
| 3520 | QualType FT = FD->getType(); |
| 3521 | while (const ConstantArrayType *AT = |
| 3522 | getContext().getAsConstantArrayType(FT)) { |
| 3523 | if (AT->getSize().getZExtValue() == 0) |
| 3524 | return false; |
| 3525 | FT = AT->getElementType(); |
| 3526 | } |
| 3527 | if (isEmptyRecord(getContext(), FT, true)) |
| 3528 | continue; |
| 3529 | |
| 3530 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3531 | if (getContext().getLangOpts().CPlusPlus && |
| 3532 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3533 | continue; |
| 3534 | |
| 3535 | uint64_t FldMembers; |
| 3536 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3537 | return false; |
| 3538 | |
| 3539 | Members = (RD->isUnion() ? |
| 3540 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3541 | } |
| 3542 | |
| 3543 | if (!Base) |
| 3544 | return false; |
| 3545 | |
| 3546 | // Ensure there is no padding. |
| 3547 | if (getContext().getTypeSize(Base) * Members != |
| 3548 | getContext().getTypeSize(Ty)) |
| 3549 | return false; |
| 3550 | } else { |
| 3551 | Members = 1; |
| 3552 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3553 | Members = 2; |
| 3554 | Ty = CT->getElementType(); |
| 3555 | } |
| 3556 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3557 | // Most ABIs only support float, double, and some vector type widths. |
| 3558 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3559 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3560 | |
| 3561 | // The base type must be the same for all members. Types that |
| 3562 | // agree in both total size and mode (float vs. vector) are |
| 3563 | // treated as being equivalent here. |
| 3564 | const Type *TyPtr = Ty.getTypePtr(); |
| 3565 | if (!Base) |
| 3566 | Base = TyPtr; |
| 3567 | |
| 3568 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3569 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3570 | return false; |
| 3571 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3572 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 3573 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3574 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3575 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 3576 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3577 | // double, long double, or 128-bit vectors. |
| 3578 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3579 | if (BT->getKind() == BuiltinType::Float || |
| 3580 | BT->getKind() == BuiltinType::Double || |
| 3581 | BT->getKind() == BuiltinType::LongDouble) |
| 3582 | return true; |
| 3583 | } |
| 3584 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3585 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3586 | return true; |
| 3587 | } |
| 3588 | return false; |
| 3589 | } |
| 3590 | |
| 3591 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 3592 | const Type *Base, uint64_t Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3593 | // Vector types require one register, floating point types require one |
| 3594 | // or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3595 | uint32_t NumRegs = |
| 3596 | Base->isVectorType() ? 1 : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3597 | |
| 3598 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3599 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3602 | ABIArgInfo |
| 3603 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3604 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3605 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3606 | if (Ty->isAnyComplexType()) |
| 3607 | return ABIArgInfo::getDirect(); |
| 3608 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3609 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3610 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3611 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3612 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3613 | if (Size > 128) |
| 3614 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3615 | else if (Size < 128) { |
| 3616 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3617 | return ABIArgInfo::getDirect(CoerceTy); |
| 3618 | } |
| 3619 | } |
| 3620 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3621 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3622 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3623 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3624 | |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3625 | bool Align32; |
| 3626 | uint64_t ABIAlign = isAlignedParamType(Ty, Align32) ? |
| 3627 | (Align32 ? 32 : 16) : 8; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3628 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3629 | |
| 3630 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3631 | const Type *Base = nullptr; |
| 3632 | uint64_t Members = 0; |
| 3633 | if (Kind == ELFv2 && |
| 3634 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3635 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3636 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3637 | return ABIArgInfo::getDirect(CoerceTy); |
| 3638 | } |
| 3639 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3640 | // If an aggregate may end up fully in registers, we do not |
| 3641 | // use the ByVal method, but pass the aggregate as array. |
| 3642 | // This is usually beneficial since we avoid forcing the |
| 3643 | // back-end to store the argument to memory. |
| 3644 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3645 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3646 | llvm::Type *CoerceTy; |
| 3647 | |
| 3648 | // Types up to 8 bytes are passed as integer type (which will be |
| 3649 | // properly aligned in the argument save area doubleword). |
| 3650 | if (Bits <= GPRBits) |
| 3651 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3652 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3653 | // Larger types are passed as arrays, with the base type selected |
| 3654 | // according to the required alignment in the save area. |
| 3655 | else { |
| 3656 | uint64_t RegBits = ABIAlign * 8; |
| 3657 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3658 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3659 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3660 | } |
| 3661 | |
| 3662 | return ABIArgInfo::getDirect(CoerceTy); |
| 3663 | } |
| 3664 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3665 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3666 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3667 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | return (isPromotableTypeForABI(Ty) ? |
| 3671 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3672 | } |
| 3673 | |
| 3674 | ABIArgInfo |
| 3675 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3676 | if (RetTy->isVoidType()) |
| 3677 | return ABIArgInfo::getIgnore(); |
| 3678 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3679 | if (RetTy->isAnyComplexType()) |
| 3680 | return ABIArgInfo::getDirect(); |
| 3681 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3682 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3683 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3684 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3685 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3686 | if (Size > 128) |
| 3687 | return ABIArgInfo::getIndirect(0); |
| 3688 | else if (Size < 128) { |
| 3689 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3690 | return ABIArgInfo::getDirect(CoerceTy); |
| 3691 | } |
| 3692 | } |
| 3693 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3694 | if (isAggregateTypeForABI(RetTy)) { |
| 3695 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3696 | const Type *Base = nullptr; |
| 3697 | uint64_t Members = 0; |
| 3698 | if (Kind == ELFv2 && |
| 3699 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3700 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3701 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3702 | return ABIArgInfo::getDirect(CoerceTy); |
| 3703 | } |
| 3704 | |
| 3705 | // ELFv2 small aggregates are returned in up to two registers. |
| 3706 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3707 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3708 | if (Bits == 0) |
| 3709 | return ABIArgInfo::getIgnore(); |
| 3710 | |
| 3711 | llvm::Type *CoerceTy; |
| 3712 | if (Bits > GPRBits) { |
| 3713 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Reid Kleckner | ee7cf84 | 2014-12-01 22:02:27 +0000 | [diff] [blame] | 3714 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, nullptr); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3715 | } else |
| 3716 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3717 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3718 | return ABIArgInfo::getDirect(CoerceTy); |
| 3719 | } |
| 3720 | |
| 3721 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3722 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3723 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3724 | |
| 3725 | return (isPromotableTypeForABI(RetTy) ? |
| 3726 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3727 | } |
| 3728 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3729 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3730 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3731 | QualType Ty, |
| 3732 | CodeGenFunction &CGF) const { |
| 3733 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3734 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3735 | |
| 3736 | CGBuilderTy &Builder = CGF.Builder; |
| 3737 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3738 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3739 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3740 | // Handle types that require 16-byte alignment in the parameter save area. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3741 | bool Align32; |
| 3742 | if (isAlignedParamType(Ty, Align32)) { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3743 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 3744 | AddrAsInt = Builder.CreateAdd(AddrAsInt, |
| 3745 | Builder.getInt64(Align32 ? 31 : 15)); |
| 3746 | AddrAsInt = Builder.CreateAnd(AddrAsInt, |
| 3747 | Builder.getInt64(Align32 ? -32 : -16)); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3748 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3749 | } |
| 3750 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3751 | // Update the va_list pointer. The pointer should be bumped by the |
| 3752 | // size of the object. We can trust getTypeSize() except for a complex |
| 3753 | // type whose base type is smaller than a doubleword. For these, the |
| 3754 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3755 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3756 | QualType BaseTy; |
| 3757 | unsigned CplxBaseSize = 0; |
| 3758 | |
| 3759 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3760 | BaseTy = CTy->getElementType(); |
| 3761 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3762 | if (CplxBaseSize < 8) |
| 3763 | SizeInBytes = 16; |
| 3764 | } |
| 3765 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3766 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3767 | llvm::Value *NextAddr = |
| 3768 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3769 | "ap.next"); |
| 3770 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3771 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3772 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3773 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3774 | // in separate doublewords. However, Clang expects us to produce a |
| 3775 | // pointer to a structure with the two parts packed tightly. So generate |
| 3776 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3777 | // and store them to a temporary structure. |
| 3778 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3779 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3780 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3781 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 3782 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3783 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3784 | } else { |
| 3785 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3786 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3787 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3788 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3789 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3790 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3791 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 3792 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 3793 | "vacplx"); |
| 3794 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 3795 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 3796 | Builder.CreateStore(Real, RealPtr, false); |
| 3797 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3798 | return Ptr; |
| 3799 | } |
| 3800 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3801 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3802 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3803 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3804 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3805 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3806 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3807 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3808 | } |
| 3809 | |
| 3810 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3811 | return Builder.CreateBitCast(Addr, PTy); |
| 3812 | } |
| 3813 | |
| 3814 | static bool |
| 3815 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3816 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3817 | // This is calculated from the LLVM and GCC tables and verified |
| 3818 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3819 | |
| 3820 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3821 | |
| 3822 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3823 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3824 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3825 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3826 | |
| 3827 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3828 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3829 | |
| 3830 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3831 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3832 | |
| 3833 | // 64-76 are various 4-byte special-purpose registers: |
| 3834 | // 64: mq |
| 3835 | // 65: lr |
| 3836 | // 66: ctr |
| 3837 | // 67: ap |
| 3838 | // 68-75 cr0-7 |
| 3839 | // 76: xer |
| 3840 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3841 | |
| 3842 | // 77-108: v0-31, the 16-byte vector registers |
| 3843 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3844 | |
| 3845 | // 109: vrsave |
| 3846 | // 110: vscr |
| 3847 | // 111: spe_acc |
| 3848 | // 112: spefscr |
| 3849 | // 113: sfp |
| 3850 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3851 | |
| 3852 | return false; |
| 3853 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3854 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3855 | bool |
| 3856 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3857 | CodeGen::CodeGenFunction &CGF, |
| 3858 | llvm::Value *Address) const { |
| 3859 | |
| 3860 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3861 | } |
| 3862 | |
| 3863 | bool |
| 3864 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3865 | llvm::Value *Address) const { |
| 3866 | |
| 3867 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3868 | } |
| 3869 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3870 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3871 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3872 | //===----------------------------------------------------------------------===// |
| 3873 | |
| 3874 | namespace { |
| 3875 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3876 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3877 | public: |
| 3878 | enum ABIKind { |
| 3879 | AAPCS = 0, |
| 3880 | DarwinPCS |
| 3881 | }; |
| 3882 | |
| 3883 | private: |
| 3884 | ABIKind Kind; |
| 3885 | |
| 3886 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3887 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3888 | |
| 3889 | private: |
| 3890 | ABIKind getABIKind() const { return Kind; } |
| 3891 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3892 | |
| 3893 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3894 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3895 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 3896 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 3897 | uint64_t Members) const override; |
| 3898 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3899 | bool isIllegalVectorType(QualType Ty) const; |
| 3900 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 3901 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3902 | if (!getCXXABI().classifyReturnType(FI)) |
| 3903 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3904 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3905 | for (auto &it : FI.arguments()) |
| 3906 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3910 | CodeGenFunction &CGF) const; |
| 3911 | |
| 3912 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3913 | CodeGenFunction &CGF) const; |
| 3914 | |
| 3915 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
NAKAMURA Takumi | 8c89496 | 2014-11-01 01:32:27 +0000 | [diff] [blame] | 3916 | CodeGenFunction &CGF) const override { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3917 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3918 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3919 | } |
| 3920 | }; |
| 3921 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3922 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3923 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3924 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3925 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3926 | |
| 3927 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
| 3928 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3929 | } |
| 3930 | |
| 3931 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; } |
| 3932 | |
| 3933 | virtual bool doesReturnSlotInterfereWithArgs() const { return false; } |
| 3934 | }; |
| 3935 | } |
| 3936 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3937 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3938 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3939 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3940 | // Handle illegal vector types here. |
| 3941 | if (isIllegalVectorType(Ty)) { |
| 3942 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3943 | if (Size <= 32) { |
| 3944 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3945 | return ABIArgInfo::getDirect(ResType); |
| 3946 | } |
| 3947 | if (Size == 64) { |
| 3948 | llvm::Type *ResType = |
| 3949 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3950 | return ABIArgInfo::getDirect(ResType); |
| 3951 | } |
| 3952 | if (Size == 128) { |
| 3953 | llvm::Type *ResType = |
| 3954 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3955 | return ABIArgInfo::getDirect(ResType); |
| 3956 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3957 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3958 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3959 | |
| 3960 | if (!isAggregateTypeForABI(Ty)) { |
| 3961 | // Treat an enum type as its underlying type. |
| 3962 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3963 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3964 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3965 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 3966 | ? ABIArgInfo::getExtend() |
| 3967 | : ABIArgInfo::getDirect()); |
| 3968 | } |
| 3969 | |
| 3970 | // Structures with either a non-trivial destructor or a non-trivial |
| 3971 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3972 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3973 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3974 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 3978 | // elsewhere for GNU compatibility. |
| 3979 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3980 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 3981 | return ABIArgInfo::getIgnore(); |
| 3982 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3983 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 3984 | } |
| 3985 | |
| 3986 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3987 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3988 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 3989 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3990 | return ABIArgInfo::getDirect( |
| 3991 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 3995 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3996 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3997 | unsigned Alignment = getContext().getTypeAlign(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3998 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 3999 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4000 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 4001 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 4002 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4003 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4004 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 4005 | } |
| 4006 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4007 | } |
| 4008 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4009 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4010 | } |
| 4011 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4012 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4013 | if (RetTy->isVoidType()) |
| 4014 | return ABIArgInfo::getIgnore(); |
| 4015 | |
| 4016 | // Large vector types should be returned via memory. |
| 4017 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 4018 | return ABIArgInfo::getIndirect(0); |
| 4019 | |
| 4020 | if (!isAggregateTypeForABI(RetTy)) { |
| 4021 | // Treat an enum type as its underlying type. |
| 4022 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4023 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4024 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 4025 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 4026 | ? ABIArgInfo::getExtend() |
| 4027 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4030 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 4031 | return ABIArgInfo::getIgnore(); |
| 4032 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4033 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4034 | uint64_t Members = 0; |
| 4035 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4036 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 4037 | return ABIArgInfo::getDirect(); |
| 4038 | |
| 4039 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 4040 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4041 | if (Size <= 128) { |
| 4042 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 4043 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 4044 | } |
| 4045 | |
| 4046 | return ABIArgInfo::getIndirect(0); |
| 4047 | } |
| 4048 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4049 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 4050 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4051 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4052 | // Check whether VT is legal. |
| 4053 | unsigned NumElements = VT->getNumElements(); |
| 4054 | uint64_t Size = getContext().getTypeSize(VT); |
| 4055 | // NumElements should be power of 2 between 1 and 16. |
| 4056 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 4057 | return true; |
| 4058 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 4059 | } |
| 4060 | return false; |
| 4061 | } |
| 4062 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4063 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4064 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4065 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4066 | // but with the difference that any floating-point type is allowed, |
| 4067 | // including __fp16. |
| 4068 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4069 | if (BT->isFloatingPoint()) |
| 4070 | return true; |
| 4071 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4072 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4073 | if (VecSize == 64 || VecSize == 128) |
| 4074 | return true; |
| 4075 | } |
| 4076 | return false; |
| 4077 | } |
| 4078 | |
| 4079 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4080 | uint64_t Members) const { |
| 4081 | return Members <= 4; |
| 4082 | } |
| 4083 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4084 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, |
| 4085 | QualType Ty, |
| 4086 | CodeGenFunction &CGF) const { |
| 4087 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4088 | bool IsIndirect = AI.isIndirect(); |
| 4089 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4090 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 4091 | if (IsIndirect) |
| 4092 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 4093 | else if (AI.getCoerceToType()) |
| 4094 | BaseTy = AI.getCoerceToType(); |
| 4095 | |
| 4096 | unsigned NumRegs = 1; |
| 4097 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 4098 | BaseTy = ArrTy->getElementType(); |
| 4099 | NumRegs = ArrTy->getNumElements(); |
| 4100 | } |
| 4101 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 4102 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4103 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 4104 | // Standard, section B.4: |
| 4105 | // |
| 4106 | // struct { |
| 4107 | // void *__stack; |
| 4108 | // void *__gr_top; |
| 4109 | // void *__vr_top; |
| 4110 | // int __gr_offs; |
| 4111 | // int __vr_offs; |
| 4112 | // }; |
| 4113 | |
| 4114 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 4115 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 4116 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 4117 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 4118 | auto &Ctx = CGF.getContext(); |
| 4119 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4120 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4121 | int reg_top_index; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4122 | int RegSize = IsIndirect ? 8 : getContext().getTypeSize(Ty) / 8; |
| 4123 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4124 | // 3 is the field number of __gr_offs |
| 4125 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 4126 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 4127 | reg_top_index = 1; // field number for __gr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4128 | RegSize = llvm::RoundUpToAlignment(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4129 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4130 | // 4 is the field number of __vr_offs. |
| 4131 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 4132 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 4133 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4134 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | //======================================= |
| 4138 | // Find out where argument was passed |
| 4139 | //======================================= |
| 4140 | |
| 4141 | // If reg_offs >= 0 we're already using the stack for this type of |
| 4142 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 4143 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 4144 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4145 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4146 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 4147 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 4148 | |
| 4149 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 4150 | |
| 4151 | // 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] | 4152 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4153 | CGF.EmitBlock(MaybeRegBlock); |
| 4154 | |
| 4155 | // Integer arguments may need to correct register alignment (for example a |
| 4156 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 4157 | // align __gr_offs to calculate the potential address. |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 4158 | if (!IsFPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4159 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4160 | |
| 4161 | reg_offs = CGF.Builder.CreateAdd( |
| 4162 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 4163 | "align_regoffs"); |
| 4164 | reg_offs = CGF.Builder.CreateAnd( |
| 4165 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 4166 | "aligned_regoffs"); |
| 4167 | } |
| 4168 | |
| 4169 | // 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] | 4170 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4171 | NewOffset = CGF.Builder.CreateAdd( |
| 4172 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 4173 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 4174 | |
| 4175 | // Now we're in a position to decide whether this argument really was in |
| 4176 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4177 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4178 | InRegs = CGF.Builder.CreateICmpSLE( |
| 4179 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 4180 | |
| 4181 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 4182 | |
| 4183 | //======================================= |
| 4184 | // Argument was in registers |
| 4185 | //======================================= |
| 4186 | |
| 4187 | // Now we emit the code for if the argument was originally passed in |
| 4188 | // registers. First start the appropriate block: |
| 4189 | CGF.EmitBlock(InRegBlock); |
| 4190 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4191 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4192 | reg_top_p = |
| 4193 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 4194 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 4195 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4196 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4197 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 4198 | |
| 4199 | if (IsIndirect) { |
| 4200 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 4201 | // stored registers or on the stack will actually be a struct **. |
| 4202 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 4203 | } |
| 4204 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4205 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4206 | uint64_t NumMembers = 0; |
| 4207 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4208 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4209 | // Homogeneous aggregates passed in registers will have their elements split |
| 4210 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 4211 | // qN+1, ...). We reload and store into a temporary local variable |
| 4212 | // contiguously. |
| 4213 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 4214 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 4215 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 4216 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 4217 | int Offset = 0; |
| 4218 | |
| 4219 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 4220 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 4221 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 4222 | llvm::Value *BaseOffset = |
| 4223 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 4224 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 4225 | LoadAddr = CGF.Builder.CreateBitCast( |
| 4226 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
| 4227 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 4228 | |
| 4229 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 4230 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 4231 | } |
| 4232 | |
| 4233 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 4234 | } else { |
| 4235 | // Otherwise the object is contiguous in memory |
| 4236 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 4237 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 4238 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4239 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 4240 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 4241 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 4242 | |
| 4243 | BaseAddr = CGF.Builder.CreateAdd( |
| 4244 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4245 | |
| 4246 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 4247 | } |
| 4248 | |
| 4249 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 4250 | } |
| 4251 | |
| 4252 | CGF.EmitBranch(ContBlock); |
| 4253 | |
| 4254 | //======================================= |
| 4255 | // Argument was on the stack |
| 4256 | //======================================= |
| 4257 | CGF.EmitBlock(OnStackBlock); |
| 4258 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4259 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4260 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 4261 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 4262 | |
| 4263 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 4264 | // floating-point ones might be affected. |
| 4265 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 4266 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 4267 | |
| 4268 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4269 | |
| 4270 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4271 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 4272 | "align_stack"); |
| 4273 | OnStackAddr = CGF.Builder.CreateAnd( |
| 4274 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 4275 | "align_stack"); |
| 4276 | |
| 4277 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4278 | } |
| 4279 | |
| 4280 | uint64_t StackSize; |
| 4281 | if (IsIndirect) |
| 4282 | StackSize = 8; |
| 4283 | else |
| 4284 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 4285 | |
| 4286 | // All stack slots are 8 bytes |
| 4287 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4288 | |
| 4289 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4290 | llvm::Value *NewStack = |
| 4291 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 4292 | |
| 4293 | // Write the new value of __stack for the next call to va_arg |
| 4294 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4295 | |
| 4296 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4297 | Ctx.getTypeSize(Ty) < 64) { |
| 4298 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 4299 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4300 | |
| 4301 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4302 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4303 | |
| 4304 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4305 | } |
| 4306 | |
| 4307 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4308 | |
| 4309 | CGF.EmitBranch(ContBlock); |
| 4310 | |
| 4311 | //======================================= |
| 4312 | // Tidy up |
| 4313 | //======================================= |
| 4314 | CGF.EmitBlock(ContBlock); |
| 4315 | |
| 4316 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4317 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4318 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4319 | |
| 4320 | if (IsIndirect) |
| 4321 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4322 | |
| 4323 | return ResAddr; |
| 4324 | } |
| 4325 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4326 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4327 | CodeGenFunction &CGF) const { |
| 4328 | // We do not support va_arg for aggregates or illegal vector types. |
| 4329 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 4330 | // other cases. |
| 4331 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4332 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4333 | |
| 4334 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 4335 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 4336 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4337 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4338 | uint64_t Members = 0; |
| 4339 | bool isHA = isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4340 | |
| 4341 | bool isIndirect = false; |
| 4342 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 4343 | // be passed indirectly. |
| 4344 | if (Size > 16 && !isHA) { |
| 4345 | isIndirect = true; |
| 4346 | Size = 8; |
| 4347 | Align = 8; |
| 4348 | } |
| 4349 | |
| 4350 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 4351 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 4352 | |
| 4353 | CGBuilderTy &Builder = CGF.Builder; |
| 4354 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4355 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4356 | |
| 4357 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4358 | // These are ignored for parameter passing purposes. |
| 4359 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4360 | return Builder.CreateBitCast(Addr, PTy); |
| 4361 | } |
| 4362 | |
| 4363 | const uint64_t MinABIAlign = 8; |
| 4364 | if (Align > MinABIAlign) { |
| 4365 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 4366 | Addr = Builder.CreateGEP(Addr, Offset); |
| 4367 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 4368 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 4369 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 4370 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 4371 | } |
| 4372 | |
| 4373 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 4374 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 4375 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 4376 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4377 | |
| 4378 | if (isIndirect) |
| 4379 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 4380 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4381 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4382 | |
| 4383 | return AddrTyped; |
| 4384 | } |
| 4385 | |
| 4386 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4387 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4388 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4389 | |
| 4390 | namespace { |
| 4391 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4392 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4393 | public: |
| 4394 | enum ABIKind { |
| 4395 | APCS = 0, |
| 4396 | AAPCS = 1, |
| 4397 | AAPCS_VFP |
| 4398 | }; |
| 4399 | |
| 4400 | private: |
| 4401 | ABIKind Kind; |
| 4402 | |
| 4403 | public: |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4404 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4405 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4406 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4407 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4408 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4409 | switch (getTarget().getTriple().getEnvironment()) { |
| 4410 | case llvm::Triple::Android: |
| 4411 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4412 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4413 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4414 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4415 | return true; |
| 4416 | default: |
| 4417 | return false; |
| 4418 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4421 | bool isEABIHF() const { |
| 4422 | switch (getTarget().getTriple().getEnvironment()) { |
| 4423 | case llvm::Triple::EABIHF: |
| 4424 | case llvm::Triple::GNUEABIHF: |
| 4425 | return true; |
| 4426 | default: |
| 4427 | return false; |
| 4428 | } |
| 4429 | } |
| 4430 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4431 | ABIKind getABIKind() const { return Kind; } |
| 4432 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4433 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4434 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4435 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4436 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4437 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4438 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4439 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4440 | uint64_t Members) const override; |
| 4441 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4442 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4443 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4444 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4445 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4446 | |
| 4447 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4448 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4449 | void setCCs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4450 | }; |
| 4451 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4452 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4453 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4454 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4455 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4456 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4457 | const ARMABIInfo &getABIInfo() const { |
| 4458 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4459 | } |
| 4460 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4461 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4462 | return 13; |
| 4463 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4464 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4465 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4466 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4467 | } |
| 4468 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4469 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4470 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4471 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4472 | |
| 4473 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4474 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4475 | return false; |
| 4476 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4477 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4478 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4479 | if (getABIInfo().isEABI()) return 88; |
| 4480 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4481 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4482 | |
| 4483 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4484 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4485 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4486 | if (!FD) |
| 4487 | return; |
| 4488 | |
| 4489 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4490 | if (!Attr) |
| 4491 | return; |
| 4492 | |
| 4493 | const char *Kind; |
| 4494 | switch (Attr->getInterrupt()) { |
| 4495 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4496 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4497 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4498 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4499 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4500 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4501 | } |
| 4502 | |
| 4503 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4504 | |
| 4505 | Fn->addFnAttr("interrupt", Kind); |
| 4506 | |
| 4507 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4508 | return; |
| 4509 | |
| 4510 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4511 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4512 | // the backend to perform a realignment as part of the function prologue. |
| 4513 | llvm::AttrBuilder B; |
| 4514 | B.addStackAlignmentAttr(8); |
| 4515 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4516 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4517 | llvm::AttributeSet::FunctionIndex, |
| 4518 | B)); |
| 4519 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4520 | }; |
| 4521 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 4522 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
| 4523 | void addStackProbeSizeTargetAttribute(const Decl *D, llvm::GlobalValue *GV, |
| 4524 | CodeGen::CodeGenModule &CGM) const; |
| 4525 | |
| 4526 | public: |
| 4527 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4528 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 4529 | |
| 4530 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4531 | CodeGen::CodeGenModule &CGM) const override; |
| 4532 | }; |
| 4533 | |
| 4534 | void WindowsARMTargetCodeGenInfo::addStackProbeSizeTargetAttribute( |
| 4535 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4536 | if (!isa<FunctionDecl>(D)) |
| 4537 | return; |
| 4538 | if (CGM.getCodeGenOpts().StackProbeSize == 4096) |
| 4539 | return; |
| 4540 | |
| 4541 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4542 | F->addFnAttr("stack-probe-size", |
| 4543 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
| 4544 | } |
| 4545 | |
| 4546 | void WindowsARMTargetCodeGenInfo::SetTargetAttributes( |
| 4547 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 4548 | ARMTargetCodeGenInfo::SetTargetAttributes(D, GV, CGM); |
| 4549 | addStackProbeSizeTargetAttribute(D, GV, CGM); |
| 4550 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4553 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4554 | if (!getCXXABI().classifyReturnType(FI)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4555 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4556 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4557 | for (auto &I : FI.arguments()) |
| 4558 | I.info = classifyArgumentType(I.type, FI.isVariadic()); |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4559 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4560 | // Always honor user-specified calling convention. |
| 4561 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4562 | return; |
| 4563 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4564 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4565 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4566 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4567 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4568 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4569 | /// Return the default calling convention that LLVM will use. |
| 4570 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4571 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4572 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4573 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4574 | else if (isEABI()) |
| 4575 | return llvm::CallingConv::ARM_AAPCS; |
| 4576 | else |
| 4577 | return llvm::CallingConv::ARM_APCS; |
| 4578 | } |
| 4579 | |
| 4580 | /// Return the calling convention that our ABI would like us to use |
| 4581 | /// as the C calling convention. |
| 4582 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4583 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4584 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4585 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4586 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4587 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4588 | llvm_unreachable("bad ABI kind"); |
| 4589 | } |
| 4590 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4591 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4592 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4593 | |
| 4594 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4595 | // they'd just match what LLVM will infer from the triple. |
| 4596 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4597 | if (abiCC != getLLVMDefaultCC()) |
| 4598 | RuntimeCC = abiCC; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 4599 | |
| 4600 | BuiltinCC = (getABIKind() == APCS ? |
| 4601 | llvm::CallingConv::ARM_APCS : llvm::CallingConv::ARM_AAPCS); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 4604 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, |
| 4605 | bool isVariadic) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4606 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4607 | // A single-precision floating-point type (including promoted |
| 4608 | // half-precision types); A double-precision floating-point type; |
| 4609 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4610 | // with a Base Type of a single- or double-precision floating-point type, |
| 4611 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4612 | // to four Elements. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4613 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4614 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4615 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4616 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4617 | // Handle illegal vector types here. |
| 4618 | if (isIllegalVectorType(Ty)) { |
| 4619 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4620 | if (Size <= 32) { |
| 4621 | llvm::Type *ResType = |
| 4622 | llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4623 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4624 | } |
| 4625 | if (Size == 64) { |
| 4626 | llvm::Type *ResType = llvm::VectorType::get( |
| 4627 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4628 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4629 | } |
| 4630 | if (Size == 128) { |
| 4631 | llvm::Type *ResType = llvm::VectorType::get( |
| 4632 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4633 | return ABIArgInfo::getDirect(ResType); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4634 | } |
| 4635 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4636 | } |
| 4637 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4638 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4639 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4640 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4641 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4642 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4643 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4644 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4645 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4646 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4647 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4648 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4649 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4650 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4651 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4652 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4653 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4654 | return ABIArgInfo::getIgnore(); |
| 4655 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4656 | if (IsEffectivelyAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4657 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4658 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4659 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4660 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4661 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4662 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4663 | // Base can be a floating-point or a vector. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4664 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4665 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4668 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4669 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4670 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4671 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4672 | uint64_t ABIAlign = 4; |
| 4673 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4674 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4675 | getABIKind() == ARMABIInfo::AAPCS) |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4676 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4677 | |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4678 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | d157e19 | 2015-03-09 21:40:42 +0000 | [diff] [blame] | 4679 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4680 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4683 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4684 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4685 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4686 | // FIXME: Try to match the types of the arguments more accurately where |
| 4687 | // we can. |
| 4688 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4689 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4690 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4691 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4692 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4693 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4694 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4695 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4696 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4697 | } |
| 4698 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4699 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4700 | llvm::LLVMContext &VMContext) { |
| 4701 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4702 | // is called integer-like if its size is less than or equal to one word, and |
| 4703 | // the offset of each of its addressable sub-fields is zero. |
| 4704 | |
| 4705 | uint64_t Size = Context.getTypeSize(Ty); |
| 4706 | |
| 4707 | // Check that the type fits in a word. |
| 4708 | if (Size > 32) |
| 4709 | return false; |
| 4710 | |
| 4711 | // FIXME: Handle vector types! |
| 4712 | if (Ty->isVectorType()) |
| 4713 | return false; |
| 4714 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4715 | // Float types are never treated as "integer like". |
| 4716 | if (Ty->isRealFloatingType()) |
| 4717 | return false; |
| 4718 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4719 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4720 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4721 | return true; |
| 4722 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4723 | // Small complex integer types are "integer like". |
| 4724 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4725 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4726 | |
| 4727 | // Single element and zero sized arrays should be allowed, by the definition |
| 4728 | // above, but they are not. |
| 4729 | |
| 4730 | // Otherwise, it must be a record type. |
| 4731 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4732 | if (!RT) return false; |
| 4733 | |
| 4734 | // Ignore records with flexible arrays. |
| 4735 | const RecordDecl *RD = RT->getDecl(); |
| 4736 | if (RD->hasFlexibleArrayMember()) |
| 4737 | return false; |
| 4738 | |
| 4739 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4740 | // like". |
| 4741 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4742 | |
| 4743 | bool HadField = false; |
| 4744 | unsigned idx = 0; |
| 4745 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4746 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4747 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4748 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4749 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4750 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4751 | // struct { int : 0; int x } |
| 4752 | // is non-integer like according to gcc. |
| 4753 | if (FD->isBitField()) { |
| 4754 | if (!RD->isUnion()) |
| 4755 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4756 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4757 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4758 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4759 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4760 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4761 | } |
| 4762 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4763 | // Check if this field is at offset 0. |
| 4764 | if (Layout.getFieldOffset(idx) != 0) |
| 4765 | return false; |
| 4766 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4767 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4768 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4769 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4770 | // Only allow at most one field in a structure. This doesn't match the |
| 4771 | // wording above, but follows gcc in situations with a field following an |
| 4772 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4773 | if (!RD->isUnion()) { |
| 4774 | if (HadField) |
| 4775 | return false; |
| 4776 | |
| 4777 | HadField = true; |
| 4778 | } |
| 4779 | } |
| 4780 | |
| 4781 | return true; |
| 4782 | } |
| 4783 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4784 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4785 | bool isVariadic) const { |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4786 | bool IsEffectivelyAAPCS_VFP = getABIKind() == AAPCS_VFP && !isVariadic; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4787 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4788 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4789 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4790 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4791 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4792 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4793 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4794 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4795 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4796 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4797 | // Treat an enum type as its underlying type. |
| 4798 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4799 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4800 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4801 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend() |
| 4802 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4803 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4804 | |
| 4805 | // Are we following APCS? |
| 4806 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4807 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4808 | return ABIArgInfo::getIgnore(); |
| 4809 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4810 | // Complex types are all returned as packed integers. |
| 4811 | // |
| 4812 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4813 | // correctly. |
| 4814 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4815 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 4816 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4817 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4818 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4819 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4820 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4821 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4822 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4823 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4824 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4825 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4826 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | // Otherwise return in memory. |
| 4830 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4831 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4832 | |
| 4833 | // Otherwise this is an AAPCS variant. |
| 4834 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4835 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4836 | return ABIArgInfo::getIgnore(); |
| 4837 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4838 | // Check for homogeneous aggregates with AAPCS-VFP. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4839 | if (IsEffectivelyAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4840 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4841 | uint64_t Members; |
| 4842 | if (isHomogeneousAggregate(RetTy, Base, Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4843 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4844 | // Homogeneous Aggregates are returned directly. |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4845 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4846 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4849 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4850 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4851 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4852 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4853 | if (getDataLayout().isBigEndian()) |
| 4854 | // 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] | 4855 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4856 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4857 | // Return in the smallest viable integer type. |
| 4858 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4859 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4860 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 4861 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4862 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4865 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4866 | } |
| 4867 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4868 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4869 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4870 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4871 | // Check whether VT is legal. |
| 4872 | unsigned NumElements = VT->getNumElements(); |
| 4873 | uint64_t Size = getContext().getTypeSize(VT); |
| 4874 | // NumElements should be power of 2. |
| 4875 | if ((NumElements & (NumElements - 1)) != 0) |
| 4876 | return true; |
| 4877 | // Size should be greater than 32 bits. |
| 4878 | return Size <= 32; |
| 4879 | } |
| 4880 | return false; |
| 4881 | } |
| 4882 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4883 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4884 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4885 | // double, or 64-bit or 128-bit vectors. |
| 4886 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4887 | if (BT->getKind() == BuiltinType::Float || |
| 4888 | BT->getKind() == BuiltinType::Double || |
| 4889 | BT->getKind() == BuiltinType::LongDouble) |
| 4890 | return true; |
| 4891 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4892 | unsigned VecSize = getContext().getTypeSize(VT); |
| 4893 | if (VecSize == 64 || VecSize == 128) |
| 4894 | return true; |
| 4895 | } |
| 4896 | return false; |
| 4897 | } |
| 4898 | |
| 4899 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 4900 | uint64_t Members) const { |
| 4901 | return Members <= 4; |
| 4902 | } |
| 4903 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4904 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4905 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4906 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4907 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4908 | |
| 4909 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4910 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4911 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4912 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4913 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4914 | // These are ignored for parameter passing purposes. |
| 4915 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4916 | return Builder.CreateBitCast(Addr, PTy); |
| 4917 | } |
| 4918 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4919 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4920 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4921 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4922 | |
| 4923 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4924 | // 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] | 4925 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4926 | getABIKind() == ARMABIInfo::AAPCS) |
| 4927 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4928 | else |
| 4929 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4930 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4931 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4932 | IsIndirect = true; |
| 4933 | Size = 4; |
| 4934 | TyAlign = 4; |
| 4935 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4936 | |
| 4937 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4938 | if (TyAlign > 4) { |
| 4939 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 4940 | "Alignment is not power of 2!"); |
| 4941 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 4942 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 4943 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4944 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4945 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4946 | |
| 4947 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4948 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4949 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4950 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4951 | "ap.next"); |
| 4952 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4953 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4954 | if (IsIndirect) |
| 4955 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4956 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4957 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 4958 | // may not be correctly aligned for the vector type. We create an aligned |
| 4959 | // temporary space and copy the content over from ap.cur to the temporary |
| 4960 | // space. This is necessary if the natural alignment of the type is greater |
| 4961 | // than the ABI alignment. |
| 4962 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 4963 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 4964 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 4965 | "var.align"); |
| 4966 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 4967 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 4968 | Builder.CreateMemCpy(Dst, Src, |
| 4969 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 4970 | TyAlign, false); |
| 4971 | Addr = AlignedTemp; //The content is in aligned location. |
| 4972 | } |
| 4973 | llvm::Type *PTy = |
| 4974 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4975 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4976 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4977 | return AddrTyped; |
| 4978 | } |
| 4979 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4980 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4981 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4982 | //===----------------------------------------------------------------------===// |
| 4983 | |
| 4984 | namespace { |
| 4985 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4986 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4987 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4988 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4989 | |
| 4990 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4991 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4992 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4993 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4994 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4995 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4996 | }; |
| 4997 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4998 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4999 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5000 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5001 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5002 | |
| 5003 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5004 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5005 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5006 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 5007 | // resulting MDNode to the nvvm.annotations MDNode. |
| 5008 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5009 | }; |
| 5010 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5011 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5012 | if (RetTy->isVoidType()) |
| 5013 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5014 | |
| 5015 | // note: this is different from default ABI |
| 5016 | if (!RetTy->isScalarType()) |
| 5017 | return ABIArgInfo::getDirect(); |
| 5018 | |
| 5019 | // Treat an enum type as its underlying type. |
| 5020 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5021 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5022 | |
| 5023 | return (RetTy->isPromotableIntegerType() ? |
| 5024 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5027 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5028 | // Treat an enum type as its underlying type. |
| 5029 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5030 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5031 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 5032 | // Return aggregates type as indirect by value |
| 5033 | if (isAggregateTypeForABI(Ty)) |
| 5034 | return ABIArgInfo::getIndirect(0, /* byval */ true); |
| 5035 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5036 | return (Ty->isPromotableIntegerType() ? |
| 5037 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5040 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5041 | if (!getCXXABI().classifyReturnType(FI)) |
| 5042 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5043 | for (auto &I : FI.arguments()) |
| 5044 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5045 | |
| 5046 | // Always honor user-specified calling convention. |
| 5047 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5048 | return; |
| 5049 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5050 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5051 | } |
| 5052 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5053 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5054 | CodeGenFunction &CFG) const { |
| 5055 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5058 | void NVPTXTargetCodeGenInfo:: |
| 5059 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5060 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5061 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5062 | if (!FD) return; |
| 5063 | |
| 5064 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5065 | |
| 5066 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5067 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5068 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5069 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5070 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5071 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5072 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5073 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5074 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5075 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5076 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5077 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5078 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5079 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5080 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5081 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5082 | // __global__ functions cannot be called from the device, we do not |
| 5083 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5084 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5085 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5086 | addNVVMMetadata(F, "kernel", 1); |
| 5087 | } |
| 5088 | if (FD->hasAttr<CUDALaunchBoundsAttr>()) { |
| 5089 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
| 5090 | addNVVMMetadata(F, "maxntidx", |
| 5091 | FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads()); |
| 5092 | // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a |
| 5093 | // zero value from getMinBlocks either means it was not specified in |
| 5094 | // __launch_bounds__ or the user specified a 0 value. In both cases, we |
| 5095 | // don't have to add a PTX directive. |
| 5096 | int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks(); |
| 5097 | if (MinCTASM > 0) { |
| 5098 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5099 | addNVVMMetadata(F, "minctasm", MinCTASM); |
| 5100 | } |
| 5101 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5102 | } |
| 5103 | } |
| 5104 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5105 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5106 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5107 | llvm::Module *M = F->getParent(); |
| 5108 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5109 | |
| 5110 | // Get "nvvm.annotations" metadata node |
| 5111 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5112 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5113 | llvm::Metadata *MDVals[] = { |
| 5114 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 5115 | llvm::ConstantAsMetadata::get( |
| 5116 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5117 | // Append metadata to nvvm.annotations |
| 5118 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5119 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5123 | // SystemZ ABI Implementation |
| 5124 | //===----------------------------------------------------------------------===// |
| 5125 | |
| 5126 | namespace { |
| 5127 | |
| 5128 | class SystemZABIInfo : public ABIInfo { |
| 5129 | public: |
| 5130 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5131 | |
| 5132 | bool isPromotableIntegerType(QualType Ty) const; |
| 5133 | bool isCompoundType(QualType Ty) const; |
| 5134 | bool isFPArgumentType(QualType Ty) const; |
| 5135 | |
| 5136 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5137 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5138 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5139 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5140 | if (!getCXXABI().classifyReturnType(FI)) |
| 5141 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5142 | for (auto &I : FI.arguments()) |
| 5143 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5144 | } |
| 5145 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5146 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5147 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5148 | }; |
| 5149 | |
| 5150 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5151 | public: |
| 5152 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5153 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 5154 | }; |
| 5155 | |
| 5156 | } |
| 5157 | |
| 5158 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5159 | // Treat an enum type as its underlying type. |
| 5160 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5161 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5162 | |
| 5163 | // Promotable integer types are required to be promoted by the ABI. |
| 5164 | if (Ty->isPromotableIntegerType()) |
| 5165 | return true; |
| 5166 | |
| 5167 | // 32-bit values must also be promoted. |
| 5168 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5169 | switch (BT->getKind()) { |
| 5170 | case BuiltinType::Int: |
| 5171 | case BuiltinType::UInt: |
| 5172 | return true; |
| 5173 | default: |
| 5174 | return false; |
| 5175 | } |
| 5176 | return false; |
| 5177 | } |
| 5178 | |
| 5179 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5180 | return (Ty->isAnyComplexType() || |
| 5181 | Ty->isVectorType() || |
| 5182 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5186 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5187 | switch (BT->getKind()) { |
| 5188 | case BuiltinType::Float: |
| 5189 | case BuiltinType::Double: |
| 5190 | return true; |
| 5191 | default: |
| 5192 | return false; |
| 5193 | } |
| 5194 | |
| 5195 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5196 | const RecordDecl *RD = RT->getDecl(); |
| 5197 | bool Found = false; |
| 5198 | |
| 5199 | // If this is a C++ record, check the bases first. |
| 5200 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5201 | for (const auto &I : CXXRD->bases()) { |
| 5202 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5203 | |
| 5204 | // Empty bases don't affect things either way. |
| 5205 | if (isEmptyRecord(getContext(), Base, true)) |
| 5206 | continue; |
| 5207 | |
| 5208 | if (Found) |
| 5209 | return false; |
| 5210 | Found = isFPArgumentType(Base); |
| 5211 | if (!Found) |
| 5212 | return false; |
| 5213 | } |
| 5214 | |
| 5215 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5216 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5217 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5218 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5219 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5220 | if (getContext().getLangOpts().CPlusPlus && |
| 5221 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5222 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5223 | |
| 5224 | // Unlike isSingleElementStruct(), arrays do not count. |
| 5225 | // Nested isFPArgumentType structures still do though. |
| 5226 | if (Found) |
| 5227 | return false; |
| 5228 | Found = isFPArgumentType(FD->getType()); |
| 5229 | if (!Found) |
| 5230 | return false; |
| 5231 | } |
| 5232 | |
| 5233 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5234 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 5235 | return Found; |
| 5236 | } |
| 5237 | |
| 5238 | return false; |
| 5239 | } |
| 5240 | |
| 5241 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5242 | CodeGenFunction &CGF) const { |
| 5243 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5244 | // struct { |
| 5245 | // i64 __gpr; |
| 5246 | // i64 __fpr; |
| 5247 | // i8 *__overflow_arg_area; |
| 5248 | // i8 *__reg_save_area; |
| 5249 | // }; |
| 5250 | |
| 5251 | // Every argument occupies 8 bytes and is passed by preference in either |
| 5252 | // GPRs or FPRs. |
| 5253 | Ty = CGF.getContext().getCanonicalType(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5254 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
| 5255 | llvm::Type *APTy = llvm::PointerType::getUnqual(ArgTy); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5256 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5257 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5258 | bool InFPRs = false; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5259 | unsigned UnpaddedBitSize; |
| 5260 | if (IsIndirect) { |
| 5261 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5262 | UnpaddedBitSize = 64; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5263 | } else { |
| 5264 | if (AI.getCoerceToType()) |
| 5265 | ArgTy = AI.getCoerceToType(); |
| 5266 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5267 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 5268 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5269 | unsigned PaddedBitSize = 64; |
| 5270 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5271 | |
| 5272 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5273 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5274 | |
| 5275 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5276 | if (InFPRs) { |
| 5277 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5278 | RegCountField = 1; // __fpr |
| 5279 | RegSaveIndex = 16; // save offset for f0 |
| 5280 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5281 | } else { |
| 5282 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5283 | RegCountField = 0; // __gpr |
| 5284 | RegSaveIndex = 2; // save offset for r2 |
| 5285 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5286 | } |
| 5287 | |
| 5288 | llvm::Value *RegCountPtr = |
| 5289 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 5290 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 5291 | llvm::Type *IndexTy = RegCount->getType(); |
| 5292 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5293 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5294 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5295 | |
| 5296 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5297 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5298 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5299 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5300 | |
| 5301 | // Emit code to load the value if it was passed in registers. |
| 5302 | CGF.EmitBlock(InRegBlock); |
| 5303 | |
| 5304 | // Work out the address of an argument register. |
| 5305 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5306 | llvm::Value *ScaledRegCount = |
| 5307 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5308 | llvm::Value *RegBase = |
| 5309 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5310 | llvm::Value *RegOffset = |
| 5311 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5312 | llvm::Value *RegSaveAreaPtr = |
| 5313 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 5314 | llvm::Value *RegSaveArea = |
| 5315 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5316 | llvm::Value *RawRegAddr = |
| 5317 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5318 | llvm::Value *RegAddr = |
| 5319 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5320 | |
| 5321 | // Update the register count |
| 5322 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5323 | llvm::Value *NewRegCount = |
| 5324 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5325 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5326 | CGF.EmitBranch(ContBlock); |
| 5327 | |
| 5328 | // Emit code to load the value if it was passed in memory. |
| 5329 | CGF.EmitBlock(InMemBlock); |
| 5330 | |
| 5331 | // Work out the address of a stack argument. |
| 5332 | llvm::Value *OverflowArgAreaPtr = |
| 5333 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 5334 | llvm::Value *OverflowArgArea = |
| 5335 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5336 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5337 | llvm::Value *RawMemAddr = |
| 5338 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5339 | llvm::Value *MemAddr = |
| 5340 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5341 | |
| 5342 | // Update overflow_arg_area_ptr pointer |
| 5343 | llvm::Value *NewOverflowArgArea = |
| 5344 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5345 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5346 | CGF.EmitBranch(ContBlock); |
| 5347 | |
| 5348 | // Return the appropriate result. |
| 5349 | CGF.EmitBlock(ContBlock); |
| 5350 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5351 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5352 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5353 | |
| 5354 | if (IsIndirect) |
| 5355 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5356 | |
| 5357 | return ResAddr; |
| 5358 | } |
| 5359 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5360 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5361 | if (RetTy->isVoidType()) |
| 5362 | return ABIArgInfo::getIgnore(); |
| 5363 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5364 | return ABIArgInfo::getIndirect(0); |
| 5365 | return (isPromotableIntegerType(RetTy) ? |
| 5366 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5367 | } |
| 5368 | |
| 5369 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5370 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5371 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5372 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5373 | |
| 5374 | // Integers and enums are extended to full register width. |
| 5375 | if (isPromotableIntegerType(Ty)) |
| 5376 | return ABIArgInfo::getExtend(); |
| 5377 | |
| 5378 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 5379 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5380 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5381 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5382 | |
| 5383 | // Handle small structures. |
| 5384 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5385 | // Structures with flexible arrays have variable length, so really |
| 5386 | // fail the size test above. |
| 5387 | const RecordDecl *RD = RT->getDecl(); |
| 5388 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5389 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5390 | |
| 5391 | // The structure is passed as an unextended integer, a float, or a double. |
| 5392 | llvm::Type *PassTy; |
| 5393 | if (isFPArgumentType(Ty)) { |
| 5394 | assert(Size == 32 || Size == 64); |
| 5395 | if (Size == 32) |
| 5396 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5397 | else |
| 5398 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5399 | } else |
| 5400 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5401 | return ABIArgInfo::getDirect(PassTy); |
| 5402 | } |
| 5403 | |
| 5404 | // Non-structure compounds are passed indirectly. |
| 5405 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5406 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5407 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5408 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5409 | } |
| 5410 | |
| 5411 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5412 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5413 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5414 | |
| 5415 | namespace { |
| 5416 | |
| 5417 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5418 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5419 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5420 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5421 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5422 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5423 | }; |
| 5424 | |
| 5425 | } |
| 5426 | |
| 5427 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5428 | llvm::GlobalValue *GV, |
| 5429 | CodeGen::CodeGenModule &M) const { |
| 5430 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5431 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5432 | // Handle 'interrupt' attribute: |
| 5433 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5434 | |
| 5435 | // Step 1: Set ISR calling convention. |
| 5436 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5437 | |
| 5438 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5439 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5440 | |
| 5441 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5442 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5443 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5444 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5445 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5449 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5450 | // MIPS ABI Implementation. This works for both little-endian and |
| 5451 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5452 | //===----------------------------------------------------------------------===// |
| 5453 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5454 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5455 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5456 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5457 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5458 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5459 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5460 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5461 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5462 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5463 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5464 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5465 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5466 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5467 | |
| 5468 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5469 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5470 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5471 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5472 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5473 | }; |
| 5474 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5475 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5476 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5477 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5478 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5479 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5480 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5481 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5482 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5483 | return 29; |
| 5484 | } |
| 5485 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5486 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5487 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5488 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5489 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5490 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5491 | if (FD->hasAttr<Mips16Attr>()) { |
| 5492 | Fn->addFnAttr("mips16"); |
| 5493 | } |
| 5494 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5495 | Fn->addFnAttr("nomips16"); |
| 5496 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5497 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5498 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5499 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5500 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5501 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5502 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5503 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5504 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5505 | }; |
| 5506 | } |
| 5507 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5508 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5509 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5510 | llvm::IntegerType *IntTy = |
| 5511 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5512 | |
| 5513 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5514 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5515 | ArgList.push_back(IntTy); |
| 5516 | |
| 5517 | // If necessary, add one more integer type to ArgList. |
| 5518 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5519 | |
| 5520 | if (R) |
| 5521 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5524 | // In N32/64, an aligned double precision floating point field is passed in |
| 5525 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5526 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5527 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5528 | |
| 5529 | if (IsO32) { |
| 5530 | CoerceToIntArgs(TySize, ArgList); |
| 5531 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5532 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5533 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5534 | if (Ty->isComplexType()) |
| 5535 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5536 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5537 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5538 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5539 | // Unions/vectors are passed in integer registers. |
| 5540 | if (!RT || !RT->isStructureOrClassType()) { |
| 5541 | CoerceToIntArgs(TySize, ArgList); |
| 5542 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5543 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5544 | |
| 5545 | const RecordDecl *RD = RT->getDecl(); |
| 5546 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5547 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5548 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5549 | uint64_t LastOffset = 0; |
| 5550 | unsigned idx = 0; |
| 5551 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5552 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5553 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5554 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5555 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5556 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5557 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5558 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5559 | |
| 5560 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5561 | continue; |
| 5562 | |
| 5563 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5564 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5565 | continue; |
| 5566 | |
| 5567 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5568 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5569 | ArgList.push_back(I64); |
| 5570 | |
| 5571 | // Add double type. |
| 5572 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5573 | LastOffset = Offset + 64; |
| 5574 | } |
| 5575 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5576 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5577 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5578 | |
| 5579 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5580 | } |
| 5581 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5582 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5583 | uint64_t Offset) const { |
| 5584 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5585 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5586 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5587 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5588 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5589 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5590 | ABIArgInfo |
| 5591 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 5592 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5593 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5594 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5595 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5596 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5597 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5598 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5599 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5600 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5601 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5602 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5603 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5604 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5605 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5606 | return ABIArgInfo::getIgnore(); |
| 5607 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5608 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5609 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5610 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5611 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5612 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5613 | // If we have reached here, aggregates are passed directly by coercing to |
| 5614 | // another structure type. Padding is inserted if the offset of the |
| 5615 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 5616 | ABIArgInfo ArgInfo = |
| 5617 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 5618 | getPaddingType(OrigOffset, CurrOffset)); |
| 5619 | ArgInfo.setInReg(true); |
| 5620 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
| 5623 | // Treat an enum type as its underlying type. |
| 5624 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5625 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5626 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 5627 | // All integral types are promoted to the GPR width. |
| 5628 | if (Ty->isIntegralOrEnumerationType()) |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5629 | return ABIArgInfo::getExtend(); |
| 5630 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5631 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5632 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5635 | llvm::Type* |
| 5636 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5637 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5638 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5639 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5640 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5641 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5642 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5643 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5644 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5645 | // N32/64 returns struct/classes in floating point registers if the |
| 5646 | // following conditions are met: |
| 5647 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5648 | // 2. The struct/class has one or two fields all of which are floating |
| 5649 | // point types. |
| 5650 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5651 | // |
| 5652 | // Any other composite results are returned in integer registers. |
| 5653 | // |
| 5654 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5655 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5656 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5657 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5658 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5659 | if (!BT || !BT->isFloatingPoint()) |
| 5660 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5661 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5662 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | if (b == e) |
| 5666 | return llvm::StructType::get(getVMContext(), RTList, |
| 5667 | RD->hasAttr<PackedAttr>()); |
| 5668 | |
| 5669 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5670 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5673 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5674 | return llvm::StructType::get(getVMContext(), RTList); |
| 5675 | } |
| 5676 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5677 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5678 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5679 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5680 | if (RetTy->isVoidType()) |
| 5681 | return ABIArgInfo::getIgnore(); |
| 5682 | |
| 5683 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5684 | // However, N32/N64 ignores zero sized return values. |
| 5685 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5686 | return ABIArgInfo::getIgnore(); |
| 5687 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5688 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5689 | if (Size <= 128) { |
| 5690 | if (RetTy->isAnyComplexType()) |
| 5691 | return ABIArgInfo::getDirect(); |
| 5692 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5693 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 5694 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5695 | if (!IsO32 || |
| 5696 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 5697 | ABIArgInfo ArgInfo = |
| 5698 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5699 | ArgInfo.setInReg(true); |
| 5700 | return ArgInfo; |
| 5701 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5702 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5703 | |
| 5704 | return ABIArgInfo::getIndirect(0); |
| 5705 | } |
| 5706 | |
| 5707 | // Treat an enum type as its underlying type. |
| 5708 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5709 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5710 | |
| 5711 | return (RetTy->isPromotableIntegerType() ? |
| 5712 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5713 | } |
| 5714 | |
| 5715 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5716 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5717 | if (!getCXXABI().classifyReturnType(FI)) |
| 5718 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5719 | |
| 5720 | // 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] | 5721 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5722 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5723 | for (auto &I : FI.arguments()) |
| 5724 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5725 | } |
| 5726 | |
| 5727 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5728 | CodeGenFunction &CGF) const { |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5729 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5730 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5731 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5732 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 5733 | // 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] | 5734 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 5735 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
| 5736 | if ((Ty->isIntegerType() && |
| 5737 | CGF.getContext().getIntWidth(Ty) < SlotSizeInBits) || |
| 5738 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5739 | Ty = CGF.getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 5740 | Ty->isSignedIntegerType()); |
| 5741 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5742 | |
| 5743 | CGBuilderTy &Builder = CGF.Builder; |
| 5744 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5745 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Daniel Sanders | 8d36a61 | 2014-09-22 13:27:06 +0000 | [diff] [blame] | 5746 | int64_t TypeAlign = |
| 5747 | std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5748 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5749 | llvm::Value *AddrTyped; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5750 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
| 5751 | |
| 5752 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 5753 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5754 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5755 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5756 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
| 5757 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5758 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5759 | } |
| 5760 | else |
| 5761 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5762 | |
| 5763 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
| 5764 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 5765 | unsigned ArgSizeInBits = CGF.getContext().getTypeSize(Ty); |
| 5766 | uint64_t Offset = llvm::RoundUpToAlignment(ArgSizeInBits / 8, TypeAlign); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5767 | llvm::Value *NextAddr = |
| 5768 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
| 5769 | "ap.next"); |
| 5770 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5771 | |
| 5772 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5775 | bool |
| 5776 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5777 | llvm::Value *Address) const { |
| 5778 | // This information comes from gcc's implementation, which seems to |
| 5779 | // as canonical as it gets. |
| 5780 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5781 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5782 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5783 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5784 | |
| 5785 | // 0-31 are the general purpose registers, $0 - $31. |
| 5786 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5787 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5788 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5789 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5790 | |
| 5791 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5792 | // They are one bit wide and ignored here. |
| 5793 | |
| 5794 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5795 | // (coprocessor 1 is the FP unit) |
| 5796 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5797 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5798 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5799 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5800 | return false; |
| 5801 | } |
| 5802 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5803 | //===----------------------------------------------------------------------===// |
| 5804 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5805 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5806 | // handling. |
| 5807 | //===----------------------------------------------------------------------===// |
| 5808 | |
| 5809 | namespace { |
| 5810 | |
| 5811 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5812 | public: |
| 5813 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5814 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5815 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5816 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5817 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5818 | }; |
| 5819 | |
| 5820 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5821 | llvm::GlobalValue *GV, |
| 5822 | CodeGen::CodeGenModule &M) const { |
| 5823 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5824 | if (!FD) return; |
| 5825 | |
| 5826 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5827 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5828 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5829 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5830 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5831 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5832 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5833 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5834 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5835 | llvm::LLVMContext &Context = F->getContext(); |
| 5836 | llvm::NamedMDNode *OpenCLMetadata = |
| 5837 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5838 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5839 | SmallVector<llvm::Metadata *, 5> Operands; |
| 5840 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5841 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5842 | Operands.push_back( |
| 5843 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5844 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 5845 | Operands.push_back( |
| 5846 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5847 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 5848 | Operands.push_back( |
| 5849 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 5850 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5851 | |
| 5852 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5853 | // for implementing the work_group_size_hint attr later. Currently |
| 5854 | // always true as the hint is not yet implemented. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 5855 | Operands.push_back( |
| 5856 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5857 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | } |
| 5862 | |
| 5863 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5864 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5865 | //===----------------------------------------------------------------------===// |
| 5866 | // Hexagon ABI Implementation |
| 5867 | //===----------------------------------------------------------------------===// |
| 5868 | |
| 5869 | namespace { |
| 5870 | |
| 5871 | class HexagonABIInfo : public ABIInfo { |
| 5872 | |
| 5873 | |
| 5874 | public: |
| 5875 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5876 | |
| 5877 | private: |
| 5878 | |
| 5879 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5880 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5881 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5882 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5883 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5884 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5885 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5886 | }; |
| 5887 | |
| 5888 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5889 | public: |
| 5890 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5891 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5892 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5893 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5894 | return 29; |
| 5895 | } |
| 5896 | }; |
| 5897 | |
| 5898 | } |
| 5899 | |
| 5900 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5901 | if (!getCXXABI().classifyReturnType(FI)) |
| 5902 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5903 | for (auto &I : FI.arguments()) |
| 5904 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5908 | if (!isAggregateTypeForABI(Ty)) { |
| 5909 | // Treat an enum type as its underlying type. |
| 5910 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5911 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5912 | |
| 5913 | return (Ty->isPromotableIntegerType() ? |
| 5914 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5915 | } |
| 5916 | |
| 5917 | // Ignore empty records. |
| 5918 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5919 | return ABIArgInfo::getIgnore(); |
| 5920 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5921 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5922 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5923 | |
| 5924 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5925 | if (Size > 64) |
| 5926 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5927 | // Pass in the smallest viable integer type. |
| 5928 | else if (Size > 32) |
| 5929 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5930 | else if (Size > 16) |
| 5931 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5932 | else if (Size > 8) |
| 5933 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5934 | else |
| 5935 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5936 | } |
| 5937 | |
| 5938 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5939 | if (RetTy->isVoidType()) |
| 5940 | return ABIArgInfo::getIgnore(); |
| 5941 | |
| 5942 | // Large vector types should be returned via memory. |
| 5943 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5944 | return ABIArgInfo::getIndirect(0); |
| 5945 | |
| 5946 | if (!isAggregateTypeForABI(RetTy)) { |
| 5947 | // Treat an enum type as its underlying type. |
| 5948 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5949 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5950 | |
| 5951 | return (RetTy->isPromotableIntegerType() ? |
| 5952 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5953 | } |
| 5954 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5955 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5956 | return ABIArgInfo::getIgnore(); |
| 5957 | |
| 5958 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5959 | // are returned indirectly. |
| 5960 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5961 | if (Size <= 64) { |
| 5962 | // Return in the smallest viable integer type. |
| 5963 | if (Size <= 8) |
| 5964 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5965 | if (Size <= 16) |
| 5966 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5967 | if (Size <= 32) |
| 5968 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5969 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5970 | } |
| 5971 | |
| 5972 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5973 | } |
| 5974 | |
| 5975 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5976 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5977 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5978 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5979 | |
| 5980 | CGBuilderTy &Builder = CGF.Builder; |
| 5981 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5982 | "ap"); |
| 5983 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5984 | llvm::Type *PTy = |
| 5985 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5986 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5987 | |
| 5988 | uint64_t Offset = |
| 5989 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5990 | llvm::Value *NextAddr = |
| 5991 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5992 | "ap.next"); |
| 5993 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5994 | |
| 5995 | return AddrTyped; |
| 5996 | } |
| 5997 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5998 | //===----------------------------------------------------------------------===// |
| 5999 | // AMDGPU ABI Implementation |
| 6000 | //===----------------------------------------------------------------------===// |
| 6001 | |
| 6002 | namespace { |
| 6003 | |
| 6004 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6005 | public: |
| 6006 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6007 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
| 6008 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 6009 | CodeGen::CodeGenModule &M) const override; |
| 6010 | }; |
| 6011 | |
| 6012 | } |
| 6013 | |
| 6014 | void AMDGPUTargetCodeGenInfo::SetTargetAttributes( |
| 6015 | const Decl *D, |
| 6016 | llvm::GlobalValue *GV, |
| 6017 | CodeGen::CodeGenModule &M) const { |
| 6018 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 6019 | if (!FD) |
| 6020 | return; |
| 6021 | |
| 6022 | if (const auto Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6023 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6024 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 6025 | if (NumVGPR != 0) |
| 6026 | F->addFnAttr("amdgpu_num_vgpr", llvm::utostr(NumVGPR)); |
| 6027 | } |
| 6028 | |
| 6029 | if (const auto Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6030 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6031 | unsigned NumSGPR = Attr->getNumSGPR(); |
| 6032 | if (NumSGPR != 0) |
| 6033 | F->addFnAttr("amdgpu_num_sgpr", llvm::utostr(NumSGPR)); |
| 6034 | } |
| 6035 | } |
| 6036 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6037 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6038 | //===----------------------------------------------------------------------===// |
| 6039 | // SPARC v9 ABI Implementation. |
| 6040 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 6041 | // |
| 6042 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 6043 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 6044 | // the array, structs larger than 16 bytes are passed indirectly. |
| 6045 | // |
| 6046 | // One case requires special care: |
| 6047 | // |
| 6048 | // struct mixed { |
| 6049 | // int i; |
| 6050 | // float f; |
| 6051 | // }; |
| 6052 | // |
| 6053 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 6054 | // parameter array, but the int is passed in an integer register, and the float |
| 6055 | // is passed in a floating point register. This is represented as two arguments |
| 6056 | // with the LLVM IR inreg attribute: |
| 6057 | // |
| 6058 | // declare void f(i32 inreg %i, float inreg %f) |
| 6059 | // |
| 6060 | // The code generator will only allocate 4 bytes from the parameter array for |
| 6061 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 6062 | // bytes. |
| 6063 | // |
| 6064 | namespace { |
| 6065 | class SparcV9ABIInfo : public ABIInfo { |
| 6066 | public: |
| 6067 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 6068 | |
| 6069 | private: |
| 6070 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6071 | void computeInfo(CGFunctionInfo &FI) const override; |
| 6072 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6073 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6074 | |
| 6075 | // Coercion type builder for structs passed in registers. The coercion type |
| 6076 | // serves two purposes: |
| 6077 | // |
| 6078 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 6079 | // in registers. |
| 6080 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 6081 | // code generator knows to pass them in floating point registers. |
| 6082 | // |
| 6083 | // We also compute the InReg flag which indicates that the struct contains |
| 6084 | // aligned 32-bit floats. |
| 6085 | // |
| 6086 | struct CoerceBuilder { |
| 6087 | llvm::LLVMContext &Context; |
| 6088 | const llvm::DataLayout &DL; |
| 6089 | SmallVector<llvm::Type*, 8> Elems; |
| 6090 | uint64_t Size; |
| 6091 | bool InReg; |
| 6092 | |
| 6093 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6094 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6095 | |
| 6096 | // Pad Elems with integers until Size is ToSize. |
| 6097 | void pad(uint64_t ToSize) { |
| 6098 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6099 | if (ToSize == Size) |
| 6100 | return; |
| 6101 | |
| 6102 | // Finish the current 64-bit word. |
| 6103 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6104 | if (Aligned > Size && Aligned <= ToSize) { |
| 6105 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6106 | Size = Aligned; |
| 6107 | } |
| 6108 | |
| 6109 | // Add whole 64-bit words. |
| 6110 | while (Size + 64 <= ToSize) { |
| 6111 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6112 | Size += 64; |
| 6113 | } |
| 6114 | |
| 6115 | // Final in-word padding. |
| 6116 | if (Size < ToSize) { |
| 6117 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6118 | Size = ToSize; |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | // Add a floating point element at Offset. |
| 6123 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6124 | // Unaligned floats are treated as integers. |
| 6125 | if (Offset % Bits) |
| 6126 | return; |
| 6127 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6128 | if (Bits < 64) |
| 6129 | InReg = true; |
| 6130 | pad(Offset); |
| 6131 | Elems.push_back(Ty); |
| 6132 | Size = Offset + Bits; |
| 6133 | } |
| 6134 | |
| 6135 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6136 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6137 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6138 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6139 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6140 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6141 | switch (ElemTy->getTypeID()) { |
| 6142 | case llvm::Type::StructTyID: |
| 6143 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6144 | break; |
| 6145 | case llvm::Type::FloatTyID: |
| 6146 | addFloat(ElemOffset, ElemTy, 32); |
| 6147 | break; |
| 6148 | case llvm::Type::DoubleTyID: |
| 6149 | addFloat(ElemOffset, ElemTy, 64); |
| 6150 | break; |
| 6151 | case llvm::Type::FP128TyID: |
| 6152 | addFloat(ElemOffset, ElemTy, 128); |
| 6153 | break; |
| 6154 | case llvm::Type::PointerTyID: |
| 6155 | if (ElemOffset % 64 == 0) { |
| 6156 | pad(ElemOffset); |
| 6157 | Elems.push_back(ElemTy); |
| 6158 | Size += 64; |
| 6159 | } |
| 6160 | break; |
| 6161 | default: |
| 6162 | break; |
| 6163 | } |
| 6164 | } |
| 6165 | } |
| 6166 | |
| 6167 | // Check if Ty is a usable substitute for the coercion type. |
| 6168 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 6169 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 6170 | } |
| 6171 | |
| 6172 | // Get the coercion type as a literal struct type. |
| 6173 | llvm::Type *getType() const { |
| 6174 | if (Elems.size() == 1) |
| 6175 | return Elems.front(); |
| 6176 | else |
| 6177 | return llvm::StructType::get(Context, Elems); |
| 6178 | } |
| 6179 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6180 | }; |
| 6181 | } // end anonymous namespace |
| 6182 | |
| 6183 | ABIArgInfo |
| 6184 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6185 | if (Ty->isVoidType()) |
| 6186 | return ABIArgInfo::getIgnore(); |
| 6187 | |
| 6188 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6189 | |
| 6190 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6191 | // pointer / sret pointer. |
| 6192 | if (Size > SizeLimit) |
| 6193 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 6194 | |
| 6195 | // Treat an enum type as its underlying type. |
| 6196 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6197 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6198 | |
| 6199 | // Integer types smaller than a register are extended. |
| 6200 | if (Size < 64 && Ty->isIntegerType()) |
| 6201 | return ABIArgInfo::getExtend(); |
| 6202 | |
| 6203 | // Other non-aggregates go in registers. |
| 6204 | if (!isAggregateTypeForABI(Ty)) |
| 6205 | return ABIArgInfo::getDirect(); |
| 6206 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6207 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6208 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6209 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 6210 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 6211 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6212 | // 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] | 6213 | // Build a coercion type from the LLVM struct type. |
| 6214 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6215 | if (!StrTy) |
| 6216 | return ABIArgInfo::getDirect(); |
| 6217 | |
| 6218 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6219 | CB.addStruct(0, StrTy); |
| 6220 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6221 | |
| 6222 | // Try to use the original type for coercion. |
| 6223 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6224 | |
| 6225 | if (CB.InReg) |
| 6226 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6227 | else |
| 6228 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6229 | } |
| 6230 | |
| 6231 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6232 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6233 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6234 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6235 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6236 | AI.setCoerceToType(ArgTy); |
| 6237 | |
| 6238 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6239 | CGBuilderTy &Builder = CGF.Builder; |
| 6240 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6241 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6242 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6243 | llvm::Value *ArgAddr; |
| 6244 | unsigned Stride; |
| 6245 | |
| 6246 | switch (AI.getKind()) { |
| 6247 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6248 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6249 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6250 | |
| 6251 | case ABIArgInfo::Extend: |
| 6252 | Stride = 8; |
| 6253 | ArgAddr = Builder |
| 6254 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6255 | "extend"); |
| 6256 | break; |
| 6257 | |
| 6258 | case ABIArgInfo::Direct: |
| 6259 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6260 | ArgAddr = Addr; |
| 6261 | break; |
| 6262 | |
| 6263 | case ABIArgInfo::Indirect: |
| 6264 | Stride = 8; |
| 6265 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6266 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6267 | "indirect"); |
| 6268 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6269 | break; |
| 6270 | |
| 6271 | case ABIArgInfo::Ignore: |
| 6272 | return llvm::UndefValue::get(ArgPtrTy); |
| 6273 | } |
| 6274 | |
| 6275 | // Update VAList. |
| 6276 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6277 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6278 | |
| 6279 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6280 | } |
| 6281 | |
| 6282 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6283 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6284 | for (auto &I : FI.arguments()) |
| 6285 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | namespace { |
| 6289 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6290 | public: |
| 6291 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6292 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6293 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6294 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6295 | return 14; |
| 6296 | } |
| 6297 | |
| 6298 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6299 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6300 | }; |
| 6301 | } // end anonymous namespace |
| 6302 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6303 | bool |
| 6304 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6305 | llvm::Value *Address) const { |
| 6306 | // This is calculated from the LLVM and GCC tables and verified |
| 6307 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6308 | |
| 6309 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6310 | |
| 6311 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6312 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6313 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6314 | |
| 6315 | // 0-31: the 8-byte general-purpose registers |
| 6316 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6317 | |
| 6318 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6319 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6320 | |
| 6321 | // Y = 64 |
| 6322 | // PSR = 65 |
| 6323 | // WIM = 66 |
| 6324 | // TBR = 67 |
| 6325 | // PC = 68 |
| 6326 | // NPC = 69 |
| 6327 | // FSR = 70 |
| 6328 | // CSR = 71 |
| 6329 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 6330 | |
| 6331 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6332 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6333 | |
| 6334 | return false; |
| 6335 | } |
| 6336 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6337 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6338 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6339 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6340 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6341 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6342 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6343 | |
| 6344 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6345 | /// it by reference between functions that append to it. |
| 6346 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6347 | |
| 6348 | /// TypeStringCache caches the meta encodings of Types. |
| 6349 | /// |
| 6350 | /// The reason for caching TypeStrings is two fold: |
| 6351 | /// 1. To cache a type's encoding for later uses; |
| 6352 | /// 2. As a means to break recursive member type inclusion. |
| 6353 | /// |
| 6354 | /// A cache Entry can have a Status of: |
| 6355 | /// NonRecursive: The type encoding is not recursive; |
| 6356 | /// Recursive: The type encoding is recursive; |
| 6357 | /// Incomplete: An incomplete TypeString; |
| 6358 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6359 | /// Recursive type encoding. |
| 6360 | /// |
| 6361 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6362 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6363 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6364 | /// the type is encountered. |
| 6365 | /// |
| 6366 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6367 | /// possible. The type itself is recursive and it may contain other types which |
| 6368 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6369 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6370 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6371 | /// |
| 6372 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6373 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6374 | /// are placed into the cache during type expansion as a means to identify and |
| 6375 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6376 | /// the entry becomes IncompleteUsed. |
| 6377 | /// |
| 6378 | /// During the expansion of a RecordType's members: |
| 6379 | /// |
| 6380 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6381 | /// cached encoding is used; |
| 6382 | /// |
| 6383 | /// If the cache contains a Recursive encoding for the member type, the |
| 6384 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6385 | /// |
| 6386 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6387 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6388 | /// |
| 6389 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6390 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6391 | /// it is swapped back in; |
| 6392 | /// |
| 6393 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6394 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6395 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6396 | /// |
| 6397 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6398 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6399 | /// Else the member is part of a recursive type and thus the recursion has |
| 6400 | /// been exited too soon for the encoding to be correct for the member. |
| 6401 | /// |
| 6402 | class TypeStringCache { |
| 6403 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6404 | struct Entry { |
| 6405 | std::string Str; // The encoded TypeString for the type. |
| 6406 | enum Status State; // Information about the encoding in 'Str'. |
| 6407 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6408 | // during the expansion of RecordType's members. |
| 6409 | }; |
| 6410 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6411 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6412 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6413 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6414 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6415 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6416 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6417 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6418 | bool IsRecursive); |
| 6419 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6420 | }; |
| 6421 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6422 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6423 | /// FieldEncoding is a helper for this ordering process. |
| 6424 | class FieldEncoding { |
| 6425 | bool HasName; |
| 6426 | std::string Enc; |
| 6427 | public: |
| 6428 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6429 | StringRef str() {return Enc.c_str();}; |
| 6430 | bool operator<(const FieldEncoding &rhs) const { |
| 6431 | if (HasName != rhs.HasName) return HasName; |
| 6432 | return Enc < rhs.Enc; |
| 6433 | } |
| 6434 | }; |
| 6435 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6436 | class XCoreABIInfo : public DefaultABIInfo { |
| 6437 | public: |
| 6438 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6439 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6440 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6441 | }; |
| 6442 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6443 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6444 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6445 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6446 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6447 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6448 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6449 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6450 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6451 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6452 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6453 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6454 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6455 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6456 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6457 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6458 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6459 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6460 | CGF.Int8PtrPtrTy); |
| 6461 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6462 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6463 | // Handle the argument. |
| 6464 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6465 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6466 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6467 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6468 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6469 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6470 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6471 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6472 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6473 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6474 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6475 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6476 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6477 | ArgSize = 0; |
| 6478 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6479 | case ABIArgInfo::Extend: |
| 6480 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6481 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6482 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6483 | if (ArgSize < 4) |
| 6484 | ArgSize = 4; |
| 6485 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6486 | case ABIArgInfo::Indirect: |
| 6487 | llvm::Value *ArgAddr; |
| 6488 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6489 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6490 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6491 | ArgSize = 4; |
| 6492 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6493 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6494 | |
| 6495 | // Increment the VAList. |
| 6496 | if (ArgSize) { |
| 6497 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6498 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6499 | } |
| 6500 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6501 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6502 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6503 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6504 | /// into the cache as a means to identify and break recursion. |
| 6505 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6506 | /// be reinserted by removeIncomplete(). |
| 6507 | /// All other types of encoding should have been used rather than arriving here. |
| 6508 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6509 | std::string StubEnc) { |
| 6510 | if (!ID) |
| 6511 | return; |
| 6512 | Entry &E = Map[ID]; |
| 6513 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6514 | "Incorrectly use of addIncomplete"); |
| 6515 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6516 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6517 | E.Str.swap(StubEnc); |
| 6518 | E.State = Incomplete; |
| 6519 | ++IncompleteCount; |
| 6520 | } |
| 6521 | |
| 6522 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6523 | /// must be removed from the cache. |
| 6524 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6525 | /// Returns true if the RecordType was defined recursively. |
| 6526 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6527 | if (!ID) |
| 6528 | return false; |
| 6529 | auto I = Map.find(ID); |
| 6530 | assert(I != Map.end() && "Entry not present"); |
| 6531 | Entry &E = I->second; |
| 6532 | assert( (E.State == Incomplete || |
| 6533 | E.State == IncompleteUsed) && |
| 6534 | "Entry must be an incomplete type"); |
| 6535 | bool IsRecursive = false; |
| 6536 | if (E.State == IncompleteUsed) { |
| 6537 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6538 | IsRecursive = true; |
| 6539 | --IncompleteUsedCount; |
| 6540 | } |
| 6541 | if (E.Swapped.empty()) |
| 6542 | Map.erase(I); |
| 6543 | else { |
| 6544 | // Swap the Recursive back. |
| 6545 | E.Swapped.swap(E.Str); |
| 6546 | E.Swapped.clear(); |
| 6547 | E.State = Recursive; |
| 6548 | } |
| 6549 | --IncompleteCount; |
| 6550 | return IsRecursive; |
| 6551 | } |
| 6552 | |
| 6553 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6554 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6555 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6556 | bool IsRecursive) { |
| 6557 | if (!ID || IncompleteUsedCount) |
| 6558 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6559 | Entry &E = Map[ID]; |
| 6560 | if (IsRecursive && !E.Str.empty()) { |
| 6561 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6562 | "This is not the same Recursive entry"); |
| 6563 | // The parent container was not recursive after all, so we could have used |
| 6564 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6565 | // we started viz: IncompleteCount!=0. |
| 6566 | return; |
| 6567 | } |
| 6568 | assert(E.Str.empty() && "Entry already present"); |
| 6569 | E.Str = Str.str(); |
| 6570 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6571 | } |
| 6572 | |
| 6573 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6574 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6575 | /// encoding is Recursive, return an empty StringRef. |
| 6576 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6577 | if (!ID) |
| 6578 | return StringRef(); // We have no key. |
| 6579 | auto I = Map.find(ID); |
| 6580 | if (I == Map.end()) |
| 6581 | return StringRef(); // We have no encoding. |
| 6582 | Entry &E = I->second; |
| 6583 | if (E.State == Recursive && IncompleteCount) |
| 6584 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6585 | |
| 6586 | if (E.State == Incomplete) { |
| 6587 | // The incomplete type is being used to break out of recursion. |
| 6588 | E.State = IncompleteUsed; |
| 6589 | ++IncompleteUsedCount; |
| 6590 | } |
| 6591 | return E.Str.c_str(); |
| 6592 | } |
| 6593 | |
| 6594 | /// The XCore ABI includes a type information section that communicates symbol |
| 6595 | /// type information to the linker. The linker uses this information to verify |
| 6596 | /// safety/correctness of things such as array bound and pointers et al. |
| 6597 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6598 | /// This type information (TypeString) is emitted into meta data for all global |
| 6599 | /// symbols: definitions, declarations, functions & variables. |
| 6600 | /// |
| 6601 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6602 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
| 6603 | /// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf> |
| 6604 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6605 | /// |
| 6606 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6607 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6608 | |
| 6609 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6610 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6611 | CodeGen::CodeGenModule &CGM) const { |
| 6612 | SmallStringEnc Enc; |
| 6613 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6614 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6615 | llvm::SmallVector<llvm::Metadata *, 2> MDVals; |
| 6616 | MDVals.push_back(llvm::ConstantAsMetadata::get(GV)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6617 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6618 | llvm::NamedMDNode *MD = |
| 6619 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6620 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6621 | } |
| 6622 | } |
| 6623 | |
| 6624 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6625 | const CodeGen::CodeGenModule &CGM, |
| 6626 | TypeStringCache &TSC); |
| 6627 | |
| 6628 | /// Helper function for appendRecordType(). |
| 6629 | /// Builds a SmallVector containing the encoded field types in declaration order. |
| 6630 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6631 | const RecordDecl *RD, |
| 6632 | const CodeGen::CodeGenModule &CGM, |
| 6633 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6634 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6635 | SmallStringEnc Enc; |
| 6636 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6637 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6638 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6639 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6640 | Enc += "b("; |
| 6641 | llvm::raw_svector_ostream OS(Enc); |
| 6642 | OS.resync(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6643 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6644 | OS.flush(); |
| 6645 | Enc += ':'; |
| 6646 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6647 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6648 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6649 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6650 | Enc += ')'; |
| 6651 | Enc += '}'; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6652 | FE.push_back(FieldEncoding(!Field->getName().empty(), Enc)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6653 | } |
| 6654 | return true; |
| 6655 | } |
| 6656 | |
| 6657 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6658 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6659 | /// Union types have their fields ordered according to the ABI. |
| 6660 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6661 | const CodeGen::CodeGenModule &CGM, |
| 6662 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6663 | // Append the cached TypeString if we have one. |
| 6664 | StringRef TypeString = TSC.lookupStr(ID); |
| 6665 | if (!TypeString.empty()) { |
| 6666 | Enc += TypeString; |
| 6667 | return true; |
| 6668 | } |
| 6669 | |
| 6670 | // Start to emit an incomplete TypeString. |
| 6671 | size_t Start = Enc.size(); |
| 6672 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6673 | Enc += '('; |
| 6674 | if (ID) |
| 6675 | Enc += ID->getName(); |
| 6676 | Enc += "){"; |
| 6677 | |
| 6678 | // We collect all encoded fields and order as necessary. |
| 6679 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6680 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6681 | if (RD && !RD->field_empty()) { |
| 6682 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6683 | // so that recursive calls to this RecordType will use it whilst building a |
| 6684 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6685 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6686 | std::string StubEnc(Enc.substr(Start).str()); |
| 6687 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6688 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6689 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6690 | (void) TSC.removeIncomplete(ID); |
| 6691 | return false; |
| 6692 | } |
| 6693 | IsRecursive = TSC.removeIncomplete(ID); |
| 6694 | // The ABI requires unions to be sorted but not structures. |
| 6695 | // See FieldEncoding::operator< for sort algorithm. |
| 6696 | if (RT->isUnionType()) |
| 6697 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6698 | // We can now complete the TypeString. |
| 6699 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6700 | for (unsigned I = 0; I != E; ++I) { |
| 6701 | if (I) |
| 6702 | Enc += ','; |
| 6703 | Enc += FE[I].str(); |
| 6704 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6705 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6706 | Enc += '}'; |
| 6707 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6708 | return true; |
| 6709 | } |
| 6710 | |
| 6711 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6712 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6713 | TypeStringCache &TSC, |
| 6714 | const IdentifierInfo *ID) { |
| 6715 | // Append the cached TypeString if we have one. |
| 6716 | StringRef TypeString = TSC.lookupStr(ID); |
| 6717 | if (!TypeString.empty()) { |
| 6718 | Enc += TypeString; |
| 6719 | return true; |
| 6720 | } |
| 6721 | |
| 6722 | size_t Start = Enc.size(); |
| 6723 | Enc += "e("; |
| 6724 | if (ID) |
| 6725 | Enc += ID->getName(); |
| 6726 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6727 | |
| 6728 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6729 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6730 | SmallVector<FieldEncoding, 16> FE; |
| 6731 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6732 | ++I) { |
| 6733 | SmallStringEnc EnumEnc; |
| 6734 | EnumEnc += "m("; |
| 6735 | EnumEnc += I->getName(); |
| 6736 | EnumEnc += "){"; |
| 6737 | I->getInitVal().toString(EnumEnc); |
| 6738 | EnumEnc += '}'; |
| 6739 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6740 | } |
| 6741 | std::sort(FE.begin(), FE.end()); |
| 6742 | unsigned E = FE.size(); |
| 6743 | for (unsigned I = 0; I != E; ++I) { |
| 6744 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6745 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6746 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6747 | } |
| 6748 | } |
| 6749 | Enc += '}'; |
| 6750 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6751 | return true; |
| 6752 | } |
| 6753 | |
| 6754 | /// Appends type's qualifier to Enc. |
| 6755 | /// This is done prior to appending the type's encoding. |
| 6756 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6757 | // Qualifiers are emitted in alphabetical order. |
| 6758 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6759 | int Lookup = 0; |
| 6760 | if (QT.isConstQualified()) |
| 6761 | Lookup += 1<<0; |
| 6762 | if (QT.isRestrictQualified()) |
| 6763 | Lookup += 1<<1; |
| 6764 | if (QT.isVolatileQualified()) |
| 6765 | Lookup += 1<<2; |
| 6766 | Enc += Table[Lookup]; |
| 6767 | } |
| 6768 | |
| 6769 | /// Appends built-in types to Enc. |
| 6770 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6771 | const char *EncType; |
| 6772 | switch (BT->getKind()) { |
| 6773 | case BuiltinType::Void: |
| 6774 | EncType = "0"; |
| 6775 | break; |
| 6776 | case BuiltinType::Bool: |
| 6777 | EncType = "b"; |
| 6778 | break; |
| 6779 | case BuiltinType::Char_U: |
| 6780 | EncType = "uc"; |
| 6781 | break; |
| 6782 | case BuiltinType::UChar: |
| 6783 | EncType = "uc"; |
| 6784 | break; |
| 6785 | case BuiltinType::SChar: |
| 6786 | EncType = "sc"; |
| 6787 | break; |
| 6788 | case BuiltinType::UShort: |
| 6789 | EncType = "us"; |
| 6790 | break; |
| 6791 | case BuiltinType::Short: |
| 6792 | EncType = "ss"; |
| 6793 | break; |
| 6794 | case BuiltinType::UInt: |
| 6795 | EncType = "ui"; |
| 6796 | break; |
| 6797 | case BuiltinType::Int: |
| 6798 | EncType = "si"; |
| 6799 | break; |
| 6800 | case BuiltinType::ULong: |
| 6801 | EncType = "ul"; |
| 6802 | break; |
| 6803 | case BuiltinType::Long: |
| 6804 | EncType = "sl"; |
| 6805 | break; |
| 6806 | case BuiltinType::ULongLong: |
| 6807 | EncType = "ull"; |
| 6808 | break; |
| 6809 | case BuiltinType::LongLong: |
| 6810 | EncType = "sll"; |
| 6811 | break; |
| 6812 | case BuiltinType::Float: |
| 6813 | EncType = "ft"; |
| 6814 | break; |
| 6815 | case BuiltinType::Double: |
| 6816 | EncType = "d"; |
| 6817 | break; |
| 6818 | case BuiltinType::LongDouble: |
| 6819 | EncType = "ld"; |
| 6820 | break; |
| 6821 | default: |
| 6822 | return false; |
| 6823 | } |
| 6824 | Enc += EncType; |
| 6825 | return true; |
| 6826 | } |
| 6827 | |
| 6828 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6829 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6830 | const CodeGen::CodeGenModule &CGM, |
| 6831 | TypeStringCache &TSC) { |
| 6832 | Enc += "p("; |
| 6833 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6834 | return false; |
| 6835 | Enc += ')'; |
| 6836 | return true; |
| 6837 | } |
| 6838 | |
| 6839 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6840 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6841 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6842 | const CodeGen::CodeGenModule &CGM, |
| 6843 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6844 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6845 | return false; |
| 6846 | Enc += "a("; |
| 6847 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6848 | CAT->getSize().toStringUnsigned(Enc); |
| 6849 | else |
| 6850 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6851 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6852 | // The Qualifiers should be attached to the type rather than the array. |
| 6853 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6854 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6855 | return false; |
| 6856 | Enc += ')'; |
| 6857 | return true; |
| 6858 | } |
| 6859 | |
| 6860 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6861 | /// and the arguments. |
| 6862 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6863 | const CodeGen::CodeGenModule &CGM, |
| 6864 | TypeStringCache &TSC) { |
| 6865 | Enc += "f{"; |
| 6866 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 6867 | return false; |
| 6868 | Enc += "}("; |
| 6869 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 6870 | // N.B. we are only interested in the adjusted param types. |
| 6871 | auto I = FPT->param_type_begin(); |
| 6872 | auto E = FPT->param_type_end(); |
| 6873 | if (I != E) { |
| 6874 | do { |
| 6875 | if (!appendType(Enc, *I, CGM, TSC)) |
| 6876 | return false; |
| 6877 | ++I; |
| 6878 | if (I != E) |
| 6879 | Enc += ','; |
| 6880 | } while (I != E); |
| 6881 | if (FPT->isVariadic()) |
| 6882 | Enc += ",va"; |
| 6883 | } else { |
| 6884 | if (FPT->isVariadic()) |
| 6885 | Enc += "va"; |
| 6886 | else |
| 6887 | Enc += '0'; |
| 6888 | } |
| 6889 | } |
| 6890 | Enc += ')'; |
| 6891 | return true; |
| 6892 | } |
| 6893 | |
| 6894 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 6895 | /// type encodings. |
| 6896 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6897 | const CodeGen::CodeGenModule &CGM, |
| 6898 | TypeStringCache &TSC) { |
| 6899 | |
| 6900 | QualType QT = QType.getCanonicalType(); |
| 6901 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6902 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 6903 | // The Qualifiers should be attached to the type rather than the array. |
| 6904 | // Thus we don't call appendQualifier() here. |
| 6905 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 6906 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6907 | appendQualifier(Enc, QT); |
| 6908 | |
| 6909 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 6910 | return appendBuiltinType(Enc, BT); |
| 6911 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6912 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 6913 | return appendPointerType(Enc, PT, CGM, TSC); |
| 6914 | |
| 6915 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 6916 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 6917 | |
| 6918 | if (const RecordType *RT = QT->getAsStructureType()) |
| 6919 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6920 | |
| 6921 | if (const RecordType *RT = QT->getAsUnionType()) |
| 6922 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6923 | |
| 6924 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 6925 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 6926 | |
| 6927 | return false; |
| 6928 | } |
| 6929 | |
| 6930 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6931 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 6932 | if (!D) |
| 6933 | return false; |
| 6934 | |
| 6935 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6936 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 6937 | return false; |
| 6938 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 6939 | } |
| 6940 | |
| 6941 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6942 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 6943 | return false; |
| 6944 | QualType QT = VD->getType().getCanonicalType(); |
| 6945 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 6946 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6947 | // The Qualifiers should be attached to the type rather than the array. |
| 6948 | // Thus we don't call appendQualifier() here. |
| 6949 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6950 | } |
| 6951 | return appendType(Enc, QT, CGM, TSC); |
| 6952 | } |
| 6953 | return false; |
| 6954 | } |
| 6955 | |
| 6956 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6957 | //===----------------------------------------------------------------------===// |
| 6958 | // Driver code |
| 6959 | //===----------------------------------------------------------------------===// |
| 6960 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 6961 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 6962 | return getTarget().getTriple(); |
| 6963 | } |
| 6964 | |
| 6965 | bool CodeGenModule::supportsCOMDAT() const { |
| 6966 | return !getTriple().isOSBinFormatMachO(); |
| 6967 | } |
| 6968 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6969 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6970 | if (TheTargetCodeGenInfo) |
| 6971 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6972 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6973 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 6974 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6975 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6976 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6977 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 6978 | case llvm::Triple::le32: |
| 6979 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6980 | case llvm::Triple::mips: |
| 6981 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6982 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 6983 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 6984 | case llvm::Triple::mips64: |
| 6985 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6986 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 6987 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 6988 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 6989 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6990 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 6991 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6992 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6993 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6994 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6997 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6998 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6999 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 7000 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7001 | { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 7002 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 7003 | TheTargetCodeGenInfo = |
| 7004 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP); |
| 7005 | return *TheTargetCodeGenInfo; |
| 7006 | } |
| 7007 | |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7008 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7009 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7010 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 7011 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 7012 | (CodeGenOpts.FloatABI != "soft" && |
| 7013 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7014 | Kind = ARMABIInfo::AAPCS_VFP; |
| 7015 | |
Derek Schuff | 71658bd | 2015-01-29 00:47:04 +0000 | [diff] [blame] | 7016 | return *(TheTargetCodeGenInfo = new ARMTargetCodeGenInfo(Types, Kind)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 7017 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7018 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7019 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7020 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 7021 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7022 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7023 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7024 | if (getTarget().getABI() == "elfv2") |
| 7025 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7026 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7027 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7028 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7029 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7030 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 7031 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7032 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 7033 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7034 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7035 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7036 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7037 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 7038 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7039 | return *(TheTargetCodeGenInfo = |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 7040 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 7041 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 7042 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 7043 | case llvm::Triple::nvptx: |
| 7044 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 7045 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 7046 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7047 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 7048 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 7049 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7050 | case llvm::Triple::systemz: |
| 7051 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 7052 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7053 | case llvm::Triple::tce: |
| 7054 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 7055 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7056 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7057 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 7058 | bool IsSmallStructInRegABI = |
| 7059 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 7060 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 7061 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7062 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 7063 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 7064 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7065 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7066 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 7067 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7068 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 7069 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 7070 | new X86_32TargetCodeGenInfo(Types, |
| 7071 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 7072 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 7073 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7074 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 7075 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7076 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7077 | case llvm::Triple::x86_64: { |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 7078 | bool HasAVX = getTarget().getABI() == "avx"; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7079 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7080 | switch (Triple.getOS()) { |
| 7081 | case llvm::Triple::Win32: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 7082 | return *(TheTargetCodeGenInfo = |
| 7083 | new WinX86_64TargetCodeGenInfo(Types, HasAVX)); |
Alex Rosenberg | 12207fa | 2015-01-27 14:47:44 +0000 | [diff] [blame] | 7084 | case llvm::Triple::PS4: |
| 7085 | return *(TheTargetCodeGenInfo = new PS4TargetCodeGenInfo(Types, HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7086 | default: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 7087 | return *(TheTargetCodeGenInfo = |
| 7088 | new X86_64TargetCodeGenInfo(Types, HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7089 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7090 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7091 | case llvm::Triple::hexagon: |
| 7092 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7093 | case llvm::Triple::r600: |
| 7094 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 7095 | case llvm::Triple::amdgcn: |
| 7096 | return *(TheTargetCodeGenInfo = new AMDGPUTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7097 | case llvm::Triple::sparcv9: |
| 7098 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7099 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7100 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7101 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7102 | } |