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" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
| 25 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 27 | |
| 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) { |
| 40 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 41 | Builder.CreateStore(Value, Cell); |
| 42 | } |
| 43 | } |
| 44 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 45 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 46 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 47 | T->isMemberFunctionPointerType(); |
| 48 | } |
| 49 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 50 | ABIInfo::~ABIInfo() {} |
| 51 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 52 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 53 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 54 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 55 | if (!RD) |
| 56 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 57 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 61 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 62 | const RecordType *RT = T->getAs<RecordType>(); |
| 63 | if (!RT) |
| 64 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 65 | return getRecordArgABI(RT, CXXABI); |
| 66 | } |
| 67 | |
| 68 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 69 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 72 | ASTContext &ABIInfo::getContext() const { |
| 73 | return CGT.getContext(); |
| 74 | } |
| 75 | |
| 76 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 77 | return CGT.getLLVMContext(); |
| 78 | } |
| 79 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 80 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 81 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 84 | const TargetInfo &ABIInfo::getTarget() const { |
| 85 | return CGT.getTarget(); |
| 86 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 87 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 88 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 89 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 90 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 91 | switch (TheKind) { |
| 92 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 93 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 94 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 95 | Ty->print(OS); |
| 96 | else |
| 97 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 98 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 99 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 100 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 101 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 102 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 103 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 104 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 105 | case InAlloca: |
| 106 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 107 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 108 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 109 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 110 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 111 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 112 | break; |
| 113 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 114 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 115 | break; |
| 116 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 117 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 120 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 121 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 122 | // If someone can figure out a general rule for this, that would be great. |
| 123 | // It's probably just doomed to be platform-dependent, though. |
| 124 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 125 | // Verified for: |
| 126 | // x86-64 FreeBSD, Linux, Darwin |
| 127 | // x86-32 FreeBSD, Linux, Darwin |
| 128 | // PowerPC Linux, Darwin |
| 129 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 130 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 131 | return 32; |
| 132 | } |
| 133 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 134 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 135 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 136 | // The following conventions are known to require this to be false: |
| 137 | // x86_stdcall |
| 138 | // MIPS |
| 139 | // For everything else, we just prefer false unless we opt out. |
| 140 | return false; |
| 141 | } |
| 142 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 143 | void |
| 144 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 145 | llvm::SmallString<24> &Opt) const { |
| 146 | // This assumes the user is passing a library name like "rt" instead of a |
| 147 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 148 | // dynamic. |
| 149 | Opt = "-l"; |
| 150 | Opt += Lib; |
| 151 | } |
| 152 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 153 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 154 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 155 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 156 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 157 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 158 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 159 | if (FD->isUnnamedBitfield()) |
| 160 | return true; |
| 161 | |
| 162 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 163 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 164 | // Constant arrays of empty records count as empty, strip them off. |
| 165 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 166 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 167 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 168 | if (AT->getSize() == 0) |
| 169 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 170 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 171 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 172 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 173 | const RecordType *RT = FT->getAs<RecordType>(); |
| 174 | if (!RT) |
| 175 | return false; |
| 176 | |
| 177 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 178 | // |
| 179 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 180 | // current ABI. |
| 181 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 182 | return false; |
| 183 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 184 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 187 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 188 | /// fields. Note that a structure with a flexible array member is not |
| 189 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 190 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 191 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 192 | if (!RT) |
| 193 | return 0; |
| 194 | const RecordDecl *RD = RT->getDecl(); |
| 195 | if (RD->hasFlexibleArrayMember()) |
| 196 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 197 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 198 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 199 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 200 | for (const auto &I : CXXRD->bases()) |
| 201 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 202 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 203 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 204 | for (const auto *I : RD->fields()) |
| 205 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 206 | return false; |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | /// isSingleElementStruct - Determine if a structure is a "single |
| 211 | /// element struct", i.e. it has exactly one non-empty field or |
| 212 | /// exactly one field which is itself a single element |
| 213 | /// struct. Structures with flexible array members are never |
| 214 | /// considered single element structs. |
| 215 | /// |
| 216 | /// \return The field declaration for the single non-empty field, if |
| 217 | /// it exists. |
| 218 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 219 | const RecordType *RT = T->getAsStructureType(); |
| 220 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 221 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 222 | |
| 223 | const RecordDecl *RD = RT->getDecl(); |
| 224 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 225 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 226 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 227 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 228 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 229 | // If this is a C++ record, check the bases first. |
| 230 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 231 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 232 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 233 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 234 | continue; |
| 235 | |
| 236 | // If we already found an element then this isn't a single-element struct. |
| 237 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 238 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 239 | |
| 240 | // If this is non-empty and not a single element struct, the composite |
| 241 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 242 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 243 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 244 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 249 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 250 | QualType FT = FD->getType(); |
| 251 | |
| 252 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 253 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 254 | continue; |
| 255 | |
| 256 | // If we already found an element then this isn't a single-element |
| 257 | // struct. |
| 258 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 259 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 260 | |
| 261 | // Treat single element arrays as the element. |
| 262 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 263 | if (AT->getSize().getZExtValue() != 1) |
| 264 | break; |
| 265 | FT = AT->getElementType(); |
| 266 | } |
| 267 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 268 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 269 | Found = FT.getTypePtr(); |
| 270 | } else { |
| 271 | Found = isSingleElementStruct(FT, Context); |
| 272 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 273 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 277 | // We don't consider a struct a single-element struct if it has |
| 278 | // padding beyond the element type. |
| 279 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 280 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 281 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 282 | return Found; |
| 283 | } |
| 284 | |
| 285 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 286 | // Treat complex types as the element type. |
| 287 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 288 | Ty = CTy->getElementType(); |
| 289 | |
| 290 | // Check for a type which we know has a simple scalar argument-passing |
| 291 | // convention without any padding. (We're specifically looking for 32 |
| 292 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 293 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 294 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 295 | return false; |
| 296 | |
| 297 | uint64_t Size = Context.getTypeSize(Ty); |
| 298 | return Size == 32 || Size == 64; |
| 299 | } |
| 300 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 301 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 302 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 303 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 304 | /// inhibiting optimizations. |
| 305 | /// |
| 306 | // FIXME: This predicate is missing many cases, currently it just follows |
| 307 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 308 | // should probably make this smarter, or better yet make the LLVM backend |
| 309 | // capable of handling it. |
| 310 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 311 | // We can only expand structure types. |
| 312 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 313 | if (!RT) |
| 314 | return false; |
| 315 | |
| 316 | // We can only expand (C) structures. |
| 317 | // |
| 318 | // FIXME: This needs to be generalized to handle classes as well. |
| 319 | const RecordDecl *RD = RT->getDecl(); |
| 320 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 321 | return false; |
| 322 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 323 | uint64_t Size = 0; |
| 324 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 325 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 326 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 327 | return false; |
| 328 | |
| 329 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 330 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 331 | // counts as "basic" is more complicated than what we were doing previously. |
| 332 | if (FD->isBitField()) |
| 333 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 334 | |
| 335 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 338 | // Make sure there are not any holes in the struct. |
| 339 | if (Size != Context.getTypeSize(Ty)) |
| 340 | return false; |
| 341 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 342 | return true; |
| 343 | } |
| 344 | |
| 345 | namespace { |
| 346 | /// DefaultABIInfo - The default implementation for ABI specific |
| 347 | /// details. This implementation provides information which results in |
| 348 | /// self-consistent and sensible LLVM IR generation, but does not |
| 349 | /// conform to any particular ABI. |
| 350 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 351 | public: |
| 352 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 353 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 354 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 355 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 356 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 357 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 358 | if (!getCXXABI().classifyReturnType(FI)) |
| 359 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 360 | for (auto &I : FI.arguments()) |
| 361 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 364 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 365 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 368 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 369 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 370 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 371 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 375 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 376 | return nullptr; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 379 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 380 | if (isAggregateTypeForABI(Ty)) |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 381 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 383 | // Treat an enum type as its underlying type. |
| 384 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 385 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 387 | return (Ty->isPromotableIntegerType() ? |
| 388 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 391 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 392 | if (RetTy->isVoidType()) |
| 393 | return ABIArgInfo::getIgnore(); |
| 394 | |
| 395 | if (isAggregateTypeForABI(RetTy)) |
| 396 | return ABIArgInfo::getIndirect(0); |
| 397 | |
| 398 | // Treat an enum type as its underlying type. |
| 399 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 400 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 401 | |
| 402 | return (RetTy->isPromotableIntegerType() ? |
| 403 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 404 | } |
| 405 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 406 | //===----------------------------------------------------------------------===// |
| 407 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 408 | // |
| 409 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 410 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 411 | //===----------------------------------------------------------------------===// |
| 412 | |
| 413 | class PNaClABIInfo : public ABIInfo { |
| 414 | public: |
| 415 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 416 | |
| 417 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 418 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 419 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 420 | void computeInfo(CGFunctionInfo &FI) const override; |
| 421 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 422 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 426 | public: |
| 427 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 428 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 429 | }; |
| 430 | |
| 431 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 432 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 433 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 434 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 435 | for (auto &I : FI.arguments()) |
| 436 | I.info = classifyArgumentType(I.type); |
| 437 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 438 | |
| 439 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 440 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 441 | return nullptr; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 444 | /// \brief Classify argument of given type \p Ty. |
| 445 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 446 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 447 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 448 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 449 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 450 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 451 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 452 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 453 | } else if (Ty->isFloatingType()) { |
| 454 | // Floating-point types don't go inreg. |
| 455 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 456 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 457 | |
| 458 | return (Ty->isPromotableIntegerType() ? |
| 459 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 463 | if (RetTy->isVoidType()) |
| 464 | return ABIArgInfo::getIgnore(); |
| 465 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 466 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 467 | if (isAggregateTypeForABI(RetTy)) |
| 468 | return ABIArgInfo::getIndirect(0); |
| 469 | |
| 470 | // Treat an enum type as its underlying type. |
| 471 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 472 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 473 | |
| 474 | return (RetTy->isPromotableIntegerType() ? |
| 475 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 476 | } |
| 477 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 478 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 479 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 480 | // 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] | 481 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 482 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 483 | IRType->getScalarSizeInBits() != 64; |
| 484 | } |
| 485 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 486 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 487 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 488 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 489 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 490 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 491 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 492 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 495 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 499 | return Ty; |
| 500 | } |
| 501 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 502 | //===----------------------------------------------------------------------===// |
| 503 | // X86-32 ABI Implementation |
| 504 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 505 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 506 | /// \brief Similar to llvm::CCState, but for Clang. |
| 507 | struct CCState { |
| 508 | CCState(unsigned CC) : CC(CC), FreeRegs(0) {} |
| 509 | |
| 510 | unsigned CC; |
| 511 | unsigned FreeRegs; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 512 | unsigned StackOffset; |
| 513 | bool UseInAlloca; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 514 | }; |
| 515 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 516 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 517 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 518 | enum Class { |
| 519 | Integer, |
| 520 | Float |
| 521 | }; |
| 522 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 523 | static const unsigned MinABIStackAlignInBytes = 4; |
| 524 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 525 | bool IsDarwinVectorABI; |
| 526 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 527 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 528 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 529 | |
| 530 | static bool isRegisterSize(unsigned Size) { |
| 531 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 532 | } |
| 533 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 534 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 535 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 536 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 537 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 538 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 539 | |
| 540 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 541 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 542 | /// \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] | 543 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 544 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 545 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 546 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 547 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 548 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 549 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 550 | /// \brief Rewrite the function info so that all memory arguments use |
| 551 | /// inalloca. |
| 552 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 553 | |
| 554 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 555 | unsigned &StackOffset, ABIArgInfo &Info, |
| 556 | QualType Type) const; |
| 557 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 558 | public: |
| 559 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 560 | void computeInfo(CGFunctionInfo &FI) const override; |
| 561 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 562 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 563 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 564 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 565 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 566 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 567 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 568 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 569 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 570 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 571 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 572 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 573 | bool d, bool p, bool w, unsigned r) |
| 574 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 575 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 576 | static bool isStructReturnInRegABI( |
| 577 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 578 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 579 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 580 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 581 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 582 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 583 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 584 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 585 | return 4; |
| 586 | } |
| 587 | |
| 588 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 589 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 590 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 591 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 592 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 593 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 594 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 595 | } |
| 596 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 597 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 598 | std::string &Constraints, |
| 599 | std::vector<llvm::Type *> &ResultRegTypes, |
| 600 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 601 | std::vector<LValue> &ResultRegDests, |
| 602 | std::string &AsmString, |
| 603 | unsigned NumOutputs) const override; |
| 604 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 605 | llvm::Constant * |
| 606 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 607 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 608 | (0x06 << 8) | // .+0x08 |
| 609 | ('F' << 16) | |
| 610 | ('T' << 24); |
| 611 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 612 | } |
| 613 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 614 | }; |
| 615 | |
| 616 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 617 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 618 | /// Rewrite input constraint references after adding some output constraints. |
| 619 | /// In the case where there is one output and one input and we add one output, |
| 620 | /// we need to replace all operand references greater than or equal to 1: |
| 621 | /// mov $0, $1 |
| 622 | /// mov eax, $1 |
| 623 | /// The result will be: |
| 624 | /// mov $0, $2 |
| 625 | /// mov eax, $2 |
| 626 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 627 | unsigned NumNewOuts, |
| 628 | std::string &AsmString) { |
| 629 | std::string Buf; |
| 630 | llvm::raw_string_ostream OS(Buf); |
| 631 | size_t Pos = 0; |
| 632 | while (Pos < AsmString.size()) { |
| 633 | size_t DollarStart = AsmString.find('$', Pos); |
| 634 | if (DollarStart == std::string::npos) |
| 635 | DollarStart = AsmString.size(); |
| 636 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 637 | if (DollarEnd == std::string::npos) |
| 638 | DollarEnd = AsmString.size(); |
| 639 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 640 | Pos = DollarEnd; |
| 641 | size_t NumDollars = DollarEnd - DollarStart; |
| 642 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 643 | // We have an operand reference. |
| 644 | size_t DigitStart = Pos; |
| 645 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 646 | if (DigitEnd == std::string::npos) |
| 647 | DigitEnd = AsmString.size(); |
| 648 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 649 | unsigned OperandIndex; |
| 650 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 651 | if (OperandIndex >= FirstIn) |
| 652 | OperandIndex += NumNewOuts; |
| 653 | OS << OperandIndex; |
| 654 | } else { |
| 655 | OS << OperandStr; |
| 656 | } |
| 657 | Pos = DigitEnd; |
| 658 | } |
| 659 | } |
| 660 | AsmString = std::move(OS.str()); |
| 661 | } |
| 662 | |
| 663 | /// Add output constraints for EAX:EDX because they are return registers. |
| 664 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 665 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 666 | std::vector<llvm::Type *> &ResultRegTypes, |
| 667 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 668 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 669 | unsigned NumOutputs) const { |
| 670 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 671 | |
| 672 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 673 | // larger. |
| 674 | if (!Constraints.empty()) |
| 675 | Constraints += ','; |
| 676 | if (RetWidth <= 32) { |
| 677 | Constraints += "={eax}"; |
| 678 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 679 | } else { |
| 680 | // Use the 'A' constraint for EAX:EDX. |
| 681 | Constraints += "=A"; |
| 682 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 683 | } |
| 684 | |
| 685 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 686 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 687 | ResultTruncRegTypes.push_back(CoerceTy); |
| 688 | |
| 689 | // Coerce the integer by bitcasting the return slot pointer. |
| 690 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(), |
| 691 | CoerceTy->getPointerTo())); |
| 692 | ResultRegDests.push_back(ReturnSlot); |
| 693 | |
| 694 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 695 | } |
| 696 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 697 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 698 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 699 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 700 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 701 | uint64_t Size = Context.getTypeSize(Ty); |
| 702 | |
| 703 | // Type must be register sized. |
| 704 | if (!isRegisterSize(Size)) |
| 705 | return false; |
| 706 | |
| 707 | if (Ty->isVectorType()) { |
| 708 | // 64- and 128- bit vectors inside structures are not returned in |
| 709 | // registers. |
| 710 | if (Size == 64 || Size == 128) |
| 711 | return false; |
| 712 | |
| 713 | return true; |
| 714 | } |
| 715 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 716 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 717 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 718 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 719 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 720 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 721 | return true; |
| 722 | |
| 723 | // Arrays are treated like records. |
| 724 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 725 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 726 | |
| 727 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 728 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 729 | if (!RT) return false; |
| 730 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 731 | // FIXME: Traverse bases here too. |
| 732 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 733 | // Structure types are passed in register if all fields would be |
| 734 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 735 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 736 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 737 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 738 | continue; |
| 739 | |
| 740 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 741 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 742 | return false; |
| 743 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 744 | return true; |
| 745 | } |
| 746 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 747 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 748 | // If the return value is indirect, then the hidden argument is consuming one |
| 749 | // integer register. |
| 750 | if (State.FreeRegs) { |
| 751 | --State.FreeRegs; |
| 752 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 753 | } |
| 754 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 755 | } |
| 756 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 757 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 758 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 759 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 760 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 761 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 762 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 763 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 764 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 765 | |
| 766 | // 128-bit vectors are a special case; they are returned in |
| 767 | // registers and we need to make sure to pick a type the LLVM |
| 768 | // backend will like. |
| 769 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 770 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 771 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 772 | |
| 773 | // Always return in register if it fits in a general purpose |
| 774 | // register, or if it is 64 bits and has a single element. |
| 775 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 776 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 777 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 778 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 779 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 780 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 784 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 785 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 786 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 787 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 788 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 789 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 790 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 791 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 792 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 793 | // If specified, structs and unions are always indirect. |
| 794 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 795 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 796 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 797 | // Small structures which are register sized are generally returned |
| 798 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 799 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 800 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 801 | |
| 802 | // As a special-case, if the struct is a "single-element" struct, and |
| 803 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 804 | // floating-point register. (MSVC does not apply this special case.) |
| 805 | // We apply a similar transformation for pointer types to improve the |
| 806 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 807 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 808 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 809 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 810 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 811 | |
| 812 | // FIXME: We should be able to narrow this integer in cases with dead |
| 813 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 814 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 817 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 818 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 819 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 820 | // Treat an enum type as its underlying type. |
| 821 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 822 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 823 | |
| 824 | return (RetTy->isPromotableIntegerType() ? |
| 825 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 828 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 829 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 830 | } |
| 831 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 832 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 833 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 834 | if (!RT) |
| 835 | return 0; |
| 836 | const RecordDecl *RD = RT->getDecl(); |
| 837 | |
| 838 | // If this is a C++ record, check the bases first. |
| 839 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 840 | for (const auto &I : CXXRD->bases()) |
| 841 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 842 | return false; |
| 843 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 844 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 845 | QualType FT = i->getType(); |
| 846 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 847 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 848 | return true; |
| 849 | |
| 850 | if (isRecordWithSSEVectorType(Context, FT)) |
| 851 | return true; |
| 852 | } |
| 853 | |
| 854 | return false; |
| 855 | } |
| 856 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 857 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 858 | unsigned Align) const { |
| 859 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 860 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 861 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 862 | return 0; // Use default alignment. |
| 863 | |
| 864 | // On non-Darwin, the stack type alignment is always 4. |
| 865 | if (!IsDarwinVectorABI) { |
| 866 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 867 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 868 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 869 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 870 | // 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] | 871 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 872 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 873 | return 16; |
| 874 | |
| 875 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 878 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 879 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 880 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 881 | if (State.FreeRegs) { |
| 882 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 883 | return ABIArgInfo::getIndirectInReg(0, false); |
| 884 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 885 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 886 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 887 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 888 | // Compute the byval alignment. |
| 889 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 890 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 891 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 892 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 893 | |
| 894 | // If the stack alignment is less than the type alignment, realign the |
| 895 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 896 | bool Realign = TypeAlign > StackAlign; |
| 897 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 900 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 901 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 902 | if (!T) |
| 903 | T = Ty.getTypePtr(); |
| 904 | |
| 905 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 906 | BuiltinType::Kind K = BT->getKind(); |
| 907 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 908 | return Float; |
| 909 | } |
| 910 | return Integer; |
| 911 | } |
| 912 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 913 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 914 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 915 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 916 | Class C = classify(Ty); |
| 917 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 918 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 919 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 920 | unsigned Size = getContext().getTypeSize(Ty); |
| 921 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 922 | |
| 923 | if (SizeInRegs == 0) |
| 924 | return false; |
| 925 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 926 | if (SizeInRegs > State.FreeRegs) { |
| 927 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 928 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 929 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 930 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 931 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 932 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 933 | if (State.CC == llvm::CallingConv::X86_FastCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 934 | if (Size > 32) |
| 935 | return false; |
| 936 | |
| 937 | if (Ty->isIntegralOrEnumerationType()) |
| 938 | return true; |
| 939 | |
| 940 | if (Ty->isPointerType()) |
| 941 | return true; |
| 942 | |
| 943 | if (Ty->isReferenceType()) |
| 944 | return true; |
| 945 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 946 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 947 | NeedsPadding = true; |
| 948 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 949 | return false; |
| 950 | } |
| 951 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 952 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 955 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 956 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 957 | // FIXME: Set alignment on indirect arguments. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 958 | if (isAggregateTypeForABI(Ty)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 959 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 960 | // Check with the C++ ABI first. |
| 961 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 962 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 963 | return getIndirectResult(Ty, false, State); |
| 964 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 965 | // The field index doesn't matter, we'll fix it up later. |
| 966 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 967 | } |
| 968 | |
| 969 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 970 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 971 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 972 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 973 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 974 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 975 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 976 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 977 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 978 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 979 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 980 | return ABIArgInfo::getIgnore(); |
| 981 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 982 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 983 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 984 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 985 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 986 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 987 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 988 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 989 | return ABIArgInfo::getDirectInReg(Result); |
| 990 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 991 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 992 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 993 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 994 | // of those arguments will match the struct. This is important because the |
| 995 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 996 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 997 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 998 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 999 | return ABIArgInfo::getExpandWithPadding( |
| 1000 | State.CC == llvm::CallingConv::X86_FastCall, PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1001 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1002 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1005 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1006 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1007 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1008 | if (IsDarwinVectorABI) { |
| 1009 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1010 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1011 | (Size == 64 && VT->getNumElements() == 1)) |
| 1012 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1013 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1014 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1015 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 1016 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1017 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1019 | return ABIArgInfo::getDirect(); |
| 1020 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1021 | |
| 1022 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1023 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1024 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1025 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1026 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1027 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (Ty->isPromotableIntegerType()) { |
| 1030 | if (InReg) |
| 1031 | return ABIArgInfo::getExtendInReg(); |
| 1032 | return ABIArgInfo::getExtend(); |
| 1033 | } |
| 1034 | if (InReg) |
| 1035 | return ABIArgInfo::getDirectInReg(); |
| 1036 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1039 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1040 | CCState State(FI.getCallingConvention()); |
| 1041 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 1042 | State.FreeRegs = 2; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1043 | else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1044 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1045 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1046 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1047 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1048 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1049 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1050 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1051 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1052 | // return value was sret and put it in a register ourselves if appropriate. |
| 1053 | if (State.FreeRegs) { |
| 1054 | --State.FreeRegs; // The sret parameter consumes a register. |
| 1055 | FI.getReturnInfo().setInReg(true); |
| 1056 | } |
| 1057 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1058 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1059 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1060 | for (auto &I : FI.arguments()) { |
| 1061 | I.info = classifyArgumentType(I.type, State); |
| 1062 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1066 | // all the memory arguments to use inalloca. |
| 1067 | if (UsedInAlloca) |
| 1068 | rewriteWithInAlloca(FI); |
| 1069 | } |
| 1070 | |
| 1071 | void |
| 1072 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1073 | unsigned &StackOffset, |
| 1074 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1075 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 1076 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1077 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1078 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1079 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1080 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1081 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1082 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1083 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1084 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1085 | unsigned NumBytes = StackOffset - OldOffset; |
| 1086 | assert(NumBytes); |
| 1087 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1088 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1089 | FrameFields.push_back(Ty); |
| 1090 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1093 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1094 | // Leave ignored and inreg arguments alone. |
| 1095 | switch (Info.getKind()) { |
| 1096 | case ABIArgInfo::InAlloca: |
| 1097 | return true; |
| 1098 | case ABIArgInfo::Indirect: |
| 1099 | assert(Info.getIndirectByVal()); |
| 1100 | return true; |
| 1101 | case ABIArgInfo::Ignore: |
| 1102 | return false; |
| 1103 | case ABIArgInfo::Direct: |
| 1104 | case ABIArgInfo::Extend: |
| 1105 | case ABIArgInfo::Expand: |
| 1106 | if (Info.getInReg()) |
| 1107 | return false; |
| 1108 | return true; |
| 1109 | } |
| 1110 | llvm_unreachable("invalid enum"); |
| 1111 | } |
| 1112 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1113 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1114 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1115 | |
| 1116 | // Build a packed struct type for all of the arguments in memory. |
| 1117 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1118 | |
| 1119 | unsigned StackOffset = 0; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1120 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1121 | |
| 1122 | // Put 'this' into the struct before 'sret', if necessary. |
| 1123 | bool IsThisCall = |
| 1124 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1125 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1126 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1127 | isArgInAlloca(I->info)) { |
| 1128 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1129 | ++I; |
| 1130 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1131 | |
| 1132 | // 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] | 1133 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1134 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1135 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1136 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1137 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1141 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1142 | ++I; |
| 1143 | |
| 1144 | // Put arguments passed in memory into the struct. |
| 1145 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1146 | if (isArgInAlloca(I->info)) |
| 1147 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1151 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1154 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1155 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1156 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1157 | |
| 1158 | CGBuilderTy &Builder = CGF.Builder; |
| 1159 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1160 | "ap"); |
| 1161 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1162 | |
| 1163 | // Compute if the address needs to be aligned |
| 1164 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1165 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1166 | Align = std::max(Align, 4U); |
| 1167 | if (Align > 4) { |
| 1168 | // addr = (addr + align - 1) & -align; |
| 1169 | llvm::Value *Offset = |
| 1170 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1171 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1172 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1173 | CGF.Int32Ty); |
| 1174 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1175 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1176 | Addr->getType(), |
| 1177 | "ap.cur.aligned"); |
| 1178 | } |
| 1179 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1180 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1181 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1182 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1183 | |
| 1184 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1185 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1186 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1187 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1188 | "ap.next"); |
| 1189 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1190 | |
| 1191 | return AddrTyped; |
| 1192 | } |
| 1193 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1194 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1195 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1196 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1197 | |
| 1198 | switch (Opts.getStructReturnConvention()) { |
| 1199 | case CodeGenOptions::SRCK_Default: |
| 1200 | break; |
| 1201 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1202 | return false; |
| 1203 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
| 1207 | if (Triple.isOSDarwin()) |
| 1208 | return true; |
| 1209 | |
| 1210 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1211 | case llvm::Triple::DragonFly: |
| 1212 | case llvm::Triple::FreeBSD: |
| 1213 | case llvm::Triple::OpenBSD: |
| 1214 | case llvm::Triple::Bitrig: |
| 1215 | return true; |
| 1216 | case llvm::Triple::Win32: |
| 1217 | switch (Triple.getEnvironment()) { |
| 1218 | case llvm::Triple::UnknownEnvironment: |
| 1219 | case llvm::Triple::Cygnus: |
| 1220 | case llvm::Triple::GNU: |
| 1221 | case llvm::Triple::MSVC: |
| 1222 | return true; |
| 1223 | default: |
| 1224 | return false; |
| 1225 | } |
| 1226 | default: |
| 1227 | return false; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1231 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1232 | llvm::GlobalValue *GV, |
| 1233 | CodeGen::CodeGenModule &CGM) const { |
| 1234 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1235 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1236 | // Get the LLVM function. |
| 1237 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1238 | |
| 1239 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1240 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1241 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1242 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1243 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1244 | llvm::AttributeSet::FunctionIndex, |
| 1245 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | } |
| 1249 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1250 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1251 | CodeGen::CodeGenFunction &CGF, |
| 1252 | llvm::Value *Address) const { |
| 1253 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1255 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1256 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1257 | // 0-7 are the eight integer registers; the order is different |
| 1258 | // on Darwin (for EH), but the range is the same. |
| 1259 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1260 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1261 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1262 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1263 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1264 | // These have size 16, which is sizeof(long double) on |
| 1265 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1266 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1267 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1268 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1269 | } else { |
| 1270 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1271 | // reason. |
| 1272 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1273 | |
| 1274 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1275 | // These have size 12, which is sizeof(long double) on |
| 1276 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1277 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1278 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1279 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1280 | |
| 1281 | return false; |
| 1282 | } |
| 1283 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1284 | //===----------------------------------------------------------------------===// |
| 1285 | // X86-64 ABI Implementation |
| 1286 | //===----------------------------------------------------------------------===// |
| 1287 | |
| 1288 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1289 | namespace { |
| 1290 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1291 | class X86_64ABIInfo : public ABIInfo { |
| 1292 | enum Class { |
| 1293 | Integer = 0, |
| 1294 | SSE, |
| 1295 | SSEUp, |
| 1296 | X87, |
| 1297 | X87Up, |
| 1298 | ComplexX87, |
| 1299 | NoClass, |
| 1300 | Memory |
| 1301 | }; |
| 1302 | |
| 1303 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1304 | /// |
| 1305 | /// Merge an accumulating classification \arg Accum with a field |
| 1306 | /// classification \arg Field. |
| 1307 | /// |
| 1308 | /// \param Accum - The accumulating classification. This should |
| 1309 | /// always be either NoClass or the result of a previous merge |
| 1310 | /// call. In addition, this should never be Memory (the caller |
| 1311 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1312 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1313 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1314 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1315 | /// |
| 1316 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1317 | /// final MEMORY or SSE classes when necessary. |
| 1318 | /// |
| 1319 | /// \param AggregateSize - The size of the current aggregate in |
| 1320 | /// the classification process. |
| 1321 | /// |
| 1322 | /// \param Lo - The classification for the parts of the type |
| 1323 | /// residing in the low word of the containing object. |
| 1324 | /// |
| 1325 | /// \param Hi - The classification for the parts of the type |
| 1326 | /// residing in the higher words of the containing object. |
| 1327 | /// |
| 1328 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1329 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1330 | /// classify - Determine the x86_64 register classes in which the |
| 1331 | /// given type T should be passed. |
| 1332 | /// |
| 1333 | /// \param Lo - The classification for the parts of the type |
| 1334 | /// residing in the low word of the containing object. |
| 1335 | /// |
| 1336 | /// \param Hi - The classification for the parts of the type |
| 1337 | /// residing in the high word of the containing object. |
| 1338 | /// |
| 1339 | /// \param OffsetBase - The bit offset of this type in the |
| 1340 | /// containing object. Some parameters are classified different |
| 1341 | /// depending on whether they straddle an eightbyte boundary. |
| 1342 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1343 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1344 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1345 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1346 | /// If a word is unused its result will be NoClass; if a type should |
| 1347 | /// be passed in Memory then at least the classification of \arg Lo |
| 1348 | /// will be Memory. |
| 1349 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1350 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1351 | /// |
| 1352 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1353 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1354 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1355 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1356 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1357 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1358 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1359 | unsigned IROffset, QualType SourceTy, |
| 1360 | unsigned SourceOffset) const; |
| 1361 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1362 | unsigned IROffset, QualType SourceTy, |
| 1363 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1364 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1365 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1366 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1367 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1368 | |
| 1369 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1370 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1371 | /// |
| 1372 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1373 | /// available. |
| 1374 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1376 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1377 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1378 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1379 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1380 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1381 | unsigned &neededSSE, |
| 1382 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1383 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1384 | bool IsIllegalVectorType(QualType Ty) const; |
| 1385 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1386 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1387 | /// unfortunately in ways that were not always consistent with |
| 1388 | /// certain previous compilers. In particular, platforms which |
| 1389 | /// required strict binary compatibility with older versions of GCC |
| 1390 | /// may need to exempt themselves. |
| 1391 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1392 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1395 | bool HasAVX; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1396 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1397 | // 64-bit hardware. |
| 1398 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1399 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1400 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1401 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1402 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1403 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1404 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1405 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1406 | bool isPassedUsingAVXType(QualType type) const { |
| 1407 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1408 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1409 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1410 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1411 | if (info.isDirect()) { |
| 1412 | llvm::Type *ty = info.getCoerceToType(); |
| 1413 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1414 | return (vectorTy->getBitWidth() > 128); |
| 1415 | } |
| 1416 | return false; |
| 1417 | } |
| 1418 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1419 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1420 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1421 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1422 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1423 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1424 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1425 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1426 | class WinX86_64ABIInfo : public ABIInfo { |
| 1427 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1428 | ABIArgInfo classify(QualType Ty, bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1429 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1430 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1431 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1432 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1433 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1434 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1435 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1436 | CodeGenFunction &CGF) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1437 | }; |
| 1438 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1439 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1440 | bool HasAVX; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1441 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1442 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1443 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1444 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1445 | const X86_64ABIInfo &getABIInfo() const { |
| 1446 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1447 | } |
| 1448 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1449 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1450 | return 7; |
| 1451 | } |
| 1452 | |
| 1453 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1454 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1455 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1456 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1457 | // 0-15 are the 16 integer registers. |
| 1458 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1459 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1460 | return false; |
| 1461 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1462 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1463 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1464 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1465 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1466 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1467 | } |
| 1468 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1469 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1470 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1471 | // The default CC on x86-64 sets %al to the number of SSA |
| 1472 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1473 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1474 | // that when AVX types are involved: the ABI explicitly states it is |
| 1475 | // undefined, and it doesn't work in practice because of how the ABI |
| 1476 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1477 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1478 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1479 | for (CallArgList::const_iterator |
| 1480 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1481 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1482 | HasAVXType = true; |
| 1483 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1486 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1487 | if (!HasAVXType) |
| 1488 | return true; |
| 1489 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1490 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1491 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1494 | llvm::Constant * |
| 1495 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1496 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1497 | (0x0a << 8) | // .+0x0c |
| 1498 | ('F' << 16) | |
| 1499 | ('T' << 24); |
| 1500 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1501 | } |
| 1502 | |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1503 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 1504 | return HasAVX ? 32 : 16; |
| 1505 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1506 | }; |
| 1507 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1508 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| 1509 | // If the argument does not end in .lib, automatically add the suffix. This |
| 1510 | // matches the behavior of MSVC. |
| 1511 | std::string ArgStr = Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1512 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1513 | ArgStr += ".lib"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1514 | return ArgStr; |
| 1515 | } |
| 1516 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1517 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1518 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1519 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1520 | bool d, bool p, bool w, unsigned RegParms) |
| 1521 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1522 | |
| 1523 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1524 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1525 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1526 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1527 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1528 | |
| 1529 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1530 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1531 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1532 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1533 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1534 | }; |
| 1535 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1536 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1537 | bool HasAVX; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1538 | public: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1539 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 1540 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)), HasAVX(HasAVX) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1541 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1542 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1543 | return 7; |
| 1544 | } |
| 1545 | |
| 1546 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1547 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1548 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1549 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1550 | // 0-15 are the 16 integer registers. |
| 1551 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1552 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1553 | return false; |
| 1554 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1555 | |
| 1556 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1557 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1558 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1559 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1560 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1561 | |
| 1562 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1563 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1564 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1565 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1566 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1567 | |
| 1568 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 1569 | return HasAVX ? 32 : 16; |
| 1570 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1571 | }; |
| 1572 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1575 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1576 | Class &Hi) const { |
| 1577 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1578 | // |
| 1579 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1580 | // memory. |
| 1581 | // |
| 1582 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1583 | // memory. |
| 1584 | // |
| 1585 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1586 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1587 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1588 | // ABI working for processors that don't support the __m256 type. |
| 1589 | // |
| 1590 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1591 | // |
| 1592 | // Some of these are enforced by the merging logic. Others can arise |
| 1593 | // only with unions; for example: |
| 1594 | // union { _Complex double; unsigned; } |
| 1595 | // |
| 1596 | // Note that clauses (b) and (c) were added in 0.98. |
| 1597 | // |
| 1598 | if (Hi == Memory) |
| 1599 | Lo = Memory; |
| 1600 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1601 | Lo = Memory; |
| 1602 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1603 | Lo = Memory; |
| 1604 | if (Hi == SSEUp && Lo != SSE) |
| 1605 | Hi = SSE; |
| 1606 | } |
| 1607 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1608 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1609 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1610 | // classified recursively so that always two fields are |
| 1611 | // considered. The resulting class is calculated according to |
| 1612 | // the classes of the fields in the eightbyte: |
| 1613 | // |
| 1614 | // (a) If both classes are equal, this is the resulting class. |
| 1615 | // |
| 1616 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1617 | // the other class. |
| 1618 | // |
| 1619 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1620 | // class. |
| 1621 | // |
| 1622 | // (d) If one of the classes is INTEGER, the result is the |
| 1623 | // INTEGER. |
| 1624 | // |
| 1625 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1626 | // MEMORY is used as class. |
| 1627 | // |
| 1628 | // (f) Otherwise class SSE is used. |
| 1629 | |
| 1630 | // Accum should never be memory (we should have returned) or |
| 1631 | // ComplexX87 (because this cannot be passed in a structure). |
| 1632 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1633 | "Invalid accumulated classification during merge."); |
| 1634 | if (Accum == Field || Field == NoClass) |
| 1635 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1636 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1637 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1638 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1639 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1640 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1641 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1642 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1643 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1644 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1645 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1648 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1649 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1650 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1651 | // Class pairs with appropriate constructor methods for the various |
| 1652 | // situations. |
| 1653 | |
| 1654 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1655 | // shouldn't be passed in registers for example, so there is no chance they |
| 1656 | // can straddle an eightbyte. Verify & simplify. |
| 1657 | |
| 1658 | Lo = Hi = NoClass; |
| 1659 | |
| 1660 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1661 | Current = Memory; |
| 1662 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1663 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1664 | BuiltinType::Kind k = BT->getKind(); |
| 1665 | |
| 1666 | if (k == BuiltinType::Void) { |
| 1667 | Current = NoClass; |
| 1668 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1669 | Lo = Integer; |
| 1670 | Hi = Integer; |
| 1671 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1672 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1673 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1674 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1675 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1676 | Current = SSE; |
| 1677 | } else if (k == BuiltinType::LongDouble) { |
| 1678 | Lo = X87; |
| 1679 | Hi = X87Up; |
| 1680 | } |
| 1681 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1682 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1683 | return; |
| 1684 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1685 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1686 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1687 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1688 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1689 | return; |
| 1690 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1692 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1693 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1694 | return; |
| 1695 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1696 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1697 | if (Ty->isMemberPointerType()) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1698 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1699 | Lo = Hi = Integer; |
| 1700 | else |
| 1701 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1702 | return; |
| 1703 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1704 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1705 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1706 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1707 | if (Size == 32) { |
| 1708 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1709 | // float> as integer. |
| 1710 | Current = Integer; |
| 1711 | |
| 1712 | // If this type crosses an eightbyte boundary, it should be |
| 1713 | // split. |
| 1714 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1715 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1716 | if (EB_Real != EB_Imag) |
| 1717 | Hi = Lo; |
| 1718 | } else if (Size == 64) { |
| 1719 | // gcc passes <1 x double> in memory. :( |
| 1720 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1721 | return; |
| 1722 | |
| 1723 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1724 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1725 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1726 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1727 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1728 | Current = Integer; |
| 1729 | else |
| 1730 | Current = SSE; |
| 1731 | |
| 1732 | // If this type crosses an eightbyte boundary, it should be |
| 1733 | // split. |
| 1734 | if (OffsetBase && OffsetBase != 64) |
| 1735 | Hi = Lo; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1736 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1737 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1738 | // least significant one belongs to class SSE and all the others to class |
| 1739 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1740 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1741 | // This design isn't correct for 256-bits, but since there're no cases |
| 1742 | // where the upper parts would need to be inspected, avoid adding |
| 1743 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1744 | // |
| 1745 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1746 | // registers if they are "named", i.e. not part of the "..." of a |
| 1747 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1748 | Lo = SSE; |
| 1749 | Hi = SSEUp; |
| 1750 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1751 | return; |
| 1752 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1753 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1754 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1755 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1756 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1757 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1758 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1759 | if (Size <= 64) |
| 1760 | Current = Integer; |
| 1761 | else if (Size <= 128) |
| 1762 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1763 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1764 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1765 | else if (ET == getContext().DoubleTy || |
| 1766 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1767 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1768 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1769 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1770 | Current = ComplexX87; |
| 1771 | |
| 1772 | // If this complex type crosses an eightbyte boundary then it |
| 1773 | // should be split. |
| 1774 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1775 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1776 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1777 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1778 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1779 | return; |
| 1780 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1781 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1782 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1783 | // Arrays are treated like structures. |
| 1784 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1785 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1786 | |
| 1787 | // 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] | 1788 | // than four eightbytes, ..., it has class MEMORY. |
| 1789 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1790 | return; |
| 1791 | |
| 1792 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1793 | // fields, it has class MEMORY. |
| 1794 | // |
| 1795 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1796 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1797 | return; |
| 1798 | |
| 1799 | // Otherwise implement simplified merge. We could be smarter about |
| 1800 | // this, but it isn't worth it and would be harder to verify. |
| 1801 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1802 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1803 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1804 | |
| 1805 | // The only case a 256-bit wide vector could be used is when the array |
| 1806 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1807 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1808 | if (Size > 128 && EltSize != 256) |
| 1809 | return; |
| 1810 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1811 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1812 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1813 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1814 | Lo = merge(Lo, FieldLo); |
| 1815 | Hi = merge(Hi, FieldHi); |
| 1816 | if (Lo == Memory || Hi == Memory) |
| 1817 | break; |
| 1818 | } |
| 1819 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1820 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1821 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1822 | return; |
| 1823 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1824 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1825 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1826 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1827 | |
| 1828 | // 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] | 1829 | // than four eightbytes, ..., it has class MEMORY. |
| 1830 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1831 | return; |
| 1832 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1833 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1834 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1835 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1836 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1837 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1838 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1839 | const RecordDecl *RD = RT->getDecl(); |
| 1840 | |
| 1841 | // Assume variable sized types are passed in memory. |
| 1842 | if (RD->hasFlexibleArrayMember()) |
| 1843 | return; |
| 1844 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1845 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1846 | |
| 1847 | // Reset Lo class, this will be recomputed. |
| 1848 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1849 | |
| 1850 | // If this is a C++ record, classify the bases first. |
| 1851 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1852 | for (const auto &I : CXXRD->bases()) { |
| 1853 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1854 | "Unexpected base class!"); |
| 1855 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1856 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1857 | |
| 1858 | // Classify this field. |
| 1859 | // |
| 1860 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1861 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1862 | // initialized to class NO_CLASS. |
| 1863 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1864 | uint64_t Offset = |
| 1865 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1866 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1867 | Lo = merge(Lo, FieldLo); |
| 1868 | Hi = merge(Hi, FieldHi); |
| 1869 | if (Lo == Memory || Hi == Memory) |
| 1870 | break; |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1875 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1876 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1877 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1878 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1879 | bool BitField = i->isBitField(); |
| 1880 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1881 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1882 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1883 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1884 | // The only case a 256-bit wide vector could be used is when the struct |
| 1885 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1886 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1887 | // |
| 1888 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1889 | Lo = Memory; |
| 1890 | return; |
| 1891 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1892 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1893 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1894 | Lo = Memory; |
| 1895 | return; |
| 1896 | } |
| 1897 | |
| 1898 | // Classify this field. |
| 1899 | // |
| 1900 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1901 | // exceeds a single eightbyte, each is classified |
| 1902 | // separately. Each eightbyte gets initialized to class |
| 1903 | // NO_CLASS. |
| 1904 | Class FieldLo, FieldHi; |
| 1905 | |
| 1906 | // Bit-fields require special handling, they do not force the |
| 1907 | // structure to be passed in memory even if unaligned, and |
| 1908 | // therefore they can straddle an eightbyte. |
| 1909 | if (BitField) { |
| 1910 | // Ignore padding bit-fields. |
| 1911 | if (i->isUnnamedBitfield()) |
| 1912 | continue; |
| 1913 | |
| 1914 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1915 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1916 | |
| 1917 | uint64_t EB_Lo = Offset / 64; |
| 1918 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 1919 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1920 | if (EB_Lo) { |
| 1921 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1922 | FieldLo = NoClass; |
| 1923 | FieldHi = Integer; |
| 1924 | } else { |
| 1925 | FieldLo = Integer; |
| 1926 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1927 | } |
| 1928 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1929 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1930 | Lo = merge(Lo, FieldLo); |
| 1931 | Hi = merge(Hi, FieldHi); |
| 1932 | if (Lo == Memory || Hi == Memory) |
| 1933 | break; |
| 1934 | } |
| 1935 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1936 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1940 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1941 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1942 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1943 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1944 | // Treat an enum type as its underlying type. |
| 1945 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1946 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1947 | |
| 1948 | return (Ty->isPromotableIntegerType() ? |
| 1949 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1950 | } |
| 1951 | |
| 1952 | return ABIArgInfo::getIndirect(0); |
| 1953 | } |
| 1954 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1955 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1956 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1957 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1958 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1959 | if (Size <= 64 || Size > LargestVector) |
| 1960 | return true; |
| 1961 | } |
| 1962 | |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1966 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1967 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1968 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1969 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1970 | // |
| 1971 | // This assumption is optimistic, as there could be free registers available |
| 1972 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1973 | // the argument in the free register. This does not seem to happen currently, |
| 1974 | // but this code would be much safer if we could mark the argument with |
| 1975 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1976 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1977 | // Treat an enum type as its underlying type. |
| 1978 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1979 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1980 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1981 | return (Ty->isPromotableIntegerType() ? |
| 1982 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1983 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1984 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1985 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1986 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1987 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1988 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1989 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1990 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1991 | |
| 1992 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1993 | // is important for good codegen. |
| 1994 | // |
| 1995 | // We do this by coercing the value into a scalar type which the backend can |
| 1996 | // handle naturally (i.e., without using byval). |
| 1997 | // |
| 1998 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1999 | // free integer registers. Doing this when there are free integer registers |
| 2000 | // would require more care, as we would have to ensure that the coerced value |
| 2001 | // did not claim the unused register. That would require either reording the |
| 2002 | // arguments to the function (so that any subsequent inreg values came first), |
| 2003 | // or only doing this optimization when there were no following arguments that |
| 2004 | // might be inreg. |
| 2005 | // |
| 2006 | // We currently expect it to be rare (particularly in well written code) for |
| 2007 | // arguments to be passed on the stack when there are still free integer |
| 2008 | // registers available (this would typically imply large structs being passed |
| 2009 | // by value), so this seems like a fair tradeoff for now. |
| 2010 | // |
| 2011 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2012 | // attributes. See PR12193. |
| 2013 | if (freeIntRegs == 0) { |
| 2014 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2015 | |
| 2016 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2017 | // type, which will end up on the stack (with alignment 8). |
| 2018 | if (Align == 8 && Size <= 64) |
| 2019 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2020 | Size)); |
| 2021 | } |
| 2022 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2023 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2026 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 2027 | /// full vector XMM/YMM register. Pick an LLVM IR type that will be passed as a |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2028 | /// vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2029 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2030 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 2032 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 2033 | // strip them off if present. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2034 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 2035 | while (STy && STy->getNumElements() == 1) { |
| 2036 | IRType = STy->getElementType(0); |
| 2037 | STy = dyn_cast<llvm::StructType>(IRType); |
| 2038 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2039 | |
Bruno Cardoso Lopes | 129b4cc | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 2040 | // If the preferred type is a 16-byte vector, prefer to pass it. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2041 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 2042 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2043 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | 71f1b2d | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 2044 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2045 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 2046 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 2047 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 2048 | EltTy->isIntegerTy(128))) |
| 2049 | return VT; |
| 2050 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2051 | |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 2052 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 2053 | } |
| 2054 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2055 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 2056 | /// is known to either be off the end of the specified type or being in |
| 2057 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 2058 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 2059 | /// classification that put one of the two halves in the INTEGER class. |
| 2060 | /// |
| 2061 | /// It is conservatively correct to return false. |
| 2062 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 2063 | unsigned EndBit, ASTContext &Context) { |
| 2064 | // If the bytes being queried are off the end of the type, there is no user |
| 2065 | // data hiding here. This handles analysis of builtins, vectors and other |
| 2066 | // types that don't contain interesting padding. |
| 2067 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 2068 | if (TySize <= StartBit) |
| 2069 | return true; |
| 2070 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2071 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 2072 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 2073 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 2074 | |
| 2075 | // Check each element to see if the element overlaps with the queried range. |
| 2076 | for (unsigned i = 0; i != NumElts; ++i) { |
| 2077 | // If the element is after the span we care about, then we're done.. |
| 2078 | unsigned EltOffset = i*EltSize; |
| 2079 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2080 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2081 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 2082 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 2083 | EndBit-EltOffset, Context)) |
| 2084 | return false; |
| 2085 | } |
| 2086 | // If it overlaps no elements, then it is safe to process as padding. |
| 2087 | return true; |
| 2088 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2089 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2090 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 2091 | const RecordDecl *RD = RT->getDecl(); |
| 2092 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2093 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2094 | // If this is a C++ record, check the bases first. |
| 2095 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2096 | for (const auto &I : CXXRD->bases()) { |
| 2097 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2098 | "Unexpected base class!"); |
| 2099 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2100 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2101 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2102 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2103 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2104 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2105 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2106 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2107 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2108 | EndBit-BaseOffset, Context)) |
| 2109 | return false; |
| 2110 | } |
| 2111 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2112 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2113 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2114 | // this could be sped up a lot by being smarter about queried fields, |
| 2115 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2116 | // much. |
| 2117 | unsigned idx = 0; |
| 2118 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2119 | i != e; ++i, ++idx) { |
| 2120 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2122 | // If we found a field after the region we care about, then we're done. |
| 2123 | if (FieldOffset >= EndBit) break; |
| 2124 | |
| 2125 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2126 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2127 | Context)) |
| 2128 | return false; |
| 2129 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2130 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2131 | // If nothing in this record overlapped the area of interest, then we're |
| 2132 | // clean. |
| 2133 | return true; |
| 2134 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2135 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2136 | return false; |
| 2137 | } |
| 2138 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2139 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2140 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2141 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2142 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2143 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2144 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2145 | // Base case if we find a float. |
| 2146 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2147 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2148 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2149 | // 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] | 2150 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2151 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2152 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2153 | IROffset -= SL->getElementOffset(Elt); |
| 2154 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2155 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2156 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2157 | // 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] | 2158 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2159 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2160 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2161 | IROffset -= IROffset/EltSize*EltSize; |
| 2162 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2163 | } |
| 2164 | |
| 2165 | return false; |
| 2166 | } |
| 2167 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2168 | |
| 2169 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2170 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2171 | llvm::Type *X86_64ABIInfo:: |
| 2172 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2173 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2174 | // 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] | 2175 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2176 | // structs that contain 3 floats. |
| 2177 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2178 | SourceOffset*8+64, getContext())) |
| 2179 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2180 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2181 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2182 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2183 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2184 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2185 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2186 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2187 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2188 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2189 | } |
| 2190 | |
| 2191 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2192 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2193 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2194 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2195 | /// 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] | 2196 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2197 | /// etc). |
| 2198 | /// |
| 2199 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2200 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2201 | /// the 8-byte value references. PrefType may be null. |
| 2202 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2203 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2204 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2205 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2206 | llvm::Type *X86_64ABIInfo:: |
| 2207 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2208 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2209 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2210 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2211 | if (IROffset == 0) { |
| 2212 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2213 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2214 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2215 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2216 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2217 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2218 | // goodness in the source type is just tail padding. This is allowed to |
| 2219 | // kick in for struct {double,int} on the int, but not on |
| 2220 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2221 | // have to do this analysis on the source type because we can't depend on |
| 2222 | // unions being lowered a specific way etc. |
| 2223 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2224 | IRType->isIntegerTy(32) || |
| 2225 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2226 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2227 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2228 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2229 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2230 | SourceOffset*8+64, getContext())) |
| 2231 | return IRType; |
| 2232 | } |
| 2233 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2234 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2235 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2236 | // 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] | 2237 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2238 | if (IROffset < SL->getSizeInBytes()) { |
| 2239 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2240 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2241 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2242 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2243 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2244 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2245 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2246 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2247 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2248 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2249 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2250 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2251 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2252 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2253 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2254 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2255 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2256 | // 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] | 2257 | unsigned TySizeInBytes = |
| 2258 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2259 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2260 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2261 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2262 | // It is always safe to classify this as an integer type up to i64 that |
| 2263 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2264 | return llvm::IntegerType::get(getVMContext(), |
| 2265 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2268 | |
| 2269 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2270 | /// be used as elements of a two register pair to pass or return, return a |
| 2271 | /// first class aggregate to represent them. For example, if the low part of |
| 2272 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2273 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2274 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2275 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2276 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2277 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2278 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2279 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2280 | // the second element at offset 8. Check for this: |
| 2281 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2282 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2283 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2284 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2285 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2286 | // To handle this, we have to increase the size of the low part so that the |
| 2287 | // second element will start at an 8 byte offset. We can't increase the size |
| 2288 | // of the second element because it might make us access off the end of the |
| 2289 | // struct. |
| 2290 | if (HiStart != 8) { |
| 2291 | // There are only two sorts of types the ABI generation code can produce for |
| 2292 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2293 | // Promote these to a larger type. |
| 2294 | if (Lo->isFloatTy()) |
| 2295 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2296 | else { |
| 2297 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2298 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2299 | } |
| 2300 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2301 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2302 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2303 | |
| 2304 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2305 | // Verify that the second element is at an 8-byte offset. |
| 2306 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2307 | "Invalid x86-64 argument pair!"); |
| 2308 | return Result; |
| 2309 | } |
| 2310 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2311 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2312 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2313 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2314 | // classification algorithm. |
| 2315 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2316 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2317 | |
| 2318 | // Check some invariants. |
| 2319 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2320 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2321 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2322 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2323 | switch (Lo) { |
| 2324 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2325 | if (Hi == NoClass) |
| 2326 | return ABIArgInfo::getIgnore(); |
| 2327 | // If the low part is just padding, it takes no register, leave ResType |
| 2328 | // null. |
| 2329 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2330 | "Unknown missing lo part"); |
| 2331 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2332 | |
| 2333 | case SSEUp: |
| 2334 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2335 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2336 | |
| 2337 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2338 | // hidden argument. |
| 2339 | case Memory: |
| 2340 | return getIndirectReturnResult(RetTy); |
| 2341 | |
| 2342 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2343 | // available register of the sequence %rax, %rdx is used. |
| 2344 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2345 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2346 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2347 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2348 | // so that the parameter gets the right LLVM IR attributes. |
| 2349 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2350 | // Treat an enum type as its underlying type. |
| 2351 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2352 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2353 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2354 | if (RetTy->isIntegralOrEnumerationType() && |
| 2355 | RetTy->isPromotableIntegerType()) |
| 2356 | return ABIArgInfo::getExtend(); |
| 2357 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2358 | break; |
| 2359 | |
| 2360 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2361 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2362 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2363 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2364 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2365 | |
| 2366 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2367 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2368 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2369 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2370 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2371 | |
| 2372 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2373 | // part of the value is returned in %st0 and the imaginary part in |
| 2374 | // %st1. |
| 2375 | case ComplexX87: |
| 2376 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2377 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2378 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2379 | NULL); |
| 2380 | break; |
| 2381 | } |
| 2382 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2383 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2384 | switch (Hi) { |
| 2385 | // Memory was handled previously and X87 should |
| 2386 | // never occur as a hi class. |
| 2387 | case Memory: |
| 2388 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2389 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2390 | |
| 2391 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2392 | case NoClass: |
| 2393 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2394 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2395 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2396 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2397 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2398 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2399 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2400 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2401 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2402 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2403 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2404 | break; |
| 2405 | |
| 2406 | // 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] | 2407 | // is passed in the next available eightbyte chunk if the last used |
| 2408 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2409 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2410 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2411 | case SSEUp: |
| 2412 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2413 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2414 | break; |
| 2415 | |
| 2416 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2417 | // returned together with the previous X87 value in %st0. |
| 2418 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2419 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2420 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2421 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2422 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2423 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2424 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2425 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2426 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2427 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2428 | break; |
| 2429 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2430 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2431 | // 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] | 2432 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2433 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2434 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2435 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2436 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2437 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2440 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2441 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2442 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2443 | const |
| 2444 | { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2445 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2446 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2447 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2448 | // Check some invariants. |
| 2449 | // FIXME: Enforce these by construction. |
| 2450 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2451 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2452 | |
| 2453 | neededInt = 0; |
| 2454 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2455 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2456 | switch (Lo) { |
| 2457 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2458 | if (Hi == NoClass) |
| 2459 | return ABIArgInfo::getIgnore(); |
| 2460 | // If the low part is just padding, it takes no register, leave ResType |
| 2461 | // null. |
| 2462 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2463 | "Unknown missing lo part"); |
| 2464 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2465 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2466 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2467 | // on the stack. |
| 2468 | case Memory: |
| 2469 | |
| 2470 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2471 | // COMPLEX_X87, it is passed in memory. |
| 2472 | case X87: |
| 2473 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2474 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2475 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2476 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2477 | |
| 2478 | case SSEUp: |
| 2479 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2480 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2481 | |
| 2482 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2483 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2484 | // and %r9 is used. |
| 2485 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2486 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2487 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2488 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2489 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2490 | |
| 2491 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2492 | // so that the parameter gets the right LLVM IR attributes. |
| 2493 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2494 | // Treat an enum type as its underlying type. |
| 2495 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2496 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2497 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2498 | if (Ty->isIntegralOrEnumerationType() && |
| 2499 | Ty->isPromotableIntegerType()) |
| 2500 | return ABIArgInfo::getExtend(); |
| 2501 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2502 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2503 | break; |
| 2504 | |
| 2505 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2506 | // available SSE register is used, the registers are taken in the |
| 2507 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2508 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2509 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2510 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2511 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2512 | break; |
| 2513 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2514 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2515 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2516 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2517 | switch (Hi) { |
| 2518 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2519 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2520 | // which is passed in memory. |
| 2521 | case Memory: |
| 2522 | case X87: |
| 2523 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2524 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2525 | |
| 2526 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2527 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2528 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2529 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2530 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2531 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2532 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2533 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2534 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2535 | break; |
| 2536 | |
| 2537 | // X87Up generally doesn't occur here (long double is passed in |
| 2538 | // memory), except in situations involving unions. |
| 2539 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2540 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2541 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2542 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2543 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2544 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2545 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2546 | ++neededSSE; |
| 2547 | break; |
| 2548 | |
| 2549 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2550 | // 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] | 2551 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2552 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2553 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2554 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2555 | break; |
| 2556 | } |
| 2557 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2558 | // If a high part was specified, merge it together with the low part. It is |
| 2559 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2560 | // first class struct aggregate with the high and low part: {low, high} |
| 2561 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2562 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2563 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2564 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2567 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2568 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2569 | if (!getCXXABI().classifyReturnType(FI)) |
| 2570 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2571 | |
| 2572 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2573 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2574 | |
| 2575 | // If the return value is indirect, then the hidden argument is consuming one |
| 2576 | // integer register. |
| 2577 | if (FI.getReturnInfo().isIndirect()) |
| 2578 | --freeIntRegs; |
| 2579 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2580 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2581 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2582 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2583 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2584 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2585 | it != ie; ++it, ++ArgNo) { |
| 2586 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2587 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2588 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2589 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 2590 | neededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2591 | |
| 2592 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2593 | // eightbyte of an argument, the whole argument is passed on the |
| 2594 | // stack. If registers have already been assigned for some |
| 2595 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2596 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2597 | freeIntRegs -= neededInt; |
| 2598 | freeSSERegs -= neededSSE; |
| 2599 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2600 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2601 | } |
| 2602 | } |
| 2603 | } |
| 2604 | |
| 2605 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2606 | QualType Ty, |
| 2607 | CodeGenFunction &CGF) { |
| 2608 | llvm::Value *overflow_arg_area_p = |
| 2609 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2610 | llvm::Value *overflow_arg_area = |
| 2611 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2612 | |
| 2613 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2614 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2615 | // It isn't stated explicitly in the standard, but in practice we use |
| 2616 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2617 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2618 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2619 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2620 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2621 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2622 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2623 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2624 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2625 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2626 | overflow_arg_area = |
| 2627 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2628 | overflow_arg_area->getType(), |
| 2629 | "overflow_arg_area.align"); |
| 2630 | } |
| 2631 | |
| 2632 | // 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] | 2633 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2634 | llvm::Value *Res = |
| 2635 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2636 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2637 | |
| 2638 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2639 | // l->overflow_arg_area + sizeof(type). |
| 2640 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2641 | // an 8 byte boundary. |
| 2642 | |
| 2643 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2644 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2645 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2646 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2647 | "overflow_arg_area.next"); |
| 2648 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2649 | |
| 2650 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2651 | return Res; |
| 2652 | } |
| 2653 | |
| 2654 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2655 | CodeGenFunction &CGF) const { |
| 2656 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2657 | // struct { |
| 2658 | // i32 gp_offset; |
| 2659 | // i32 fp_offset; |
| 2660 | // i8* overflow_arg_area; |
| 2661 | // i8* reg_save_area; |
| 2662 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2663 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2664 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2665 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2666 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2667 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2668 | |
| 2669 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2670 | // in the registers. If not go to step 7. |
| 2671 | if (!neededInt && !neededSSE) |
| 2672 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2673 | |
| 2674 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2675 | // general purpose registers needed to pass type and num_fp to hold |
| 2676 | // the number of floating point registers needed. |
| 2677 | |
| 2678 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2679 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2680 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2681 | // |
| 2682 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2683 | // register save space). |
| 2684 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2685 | llvm::Value *InRegs = nullptr; |
| 2686 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2687 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2688 | if (neededInt) { |
| 2689 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2690 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2691 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2692 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | if (neededSSE) { |
| 2696 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2697 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2698 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2699 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2700 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2701 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2702 | } |
| 2703 | |
| 2704 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2705 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2706 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2707 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2708 | |
| 2709 | // Emit code to load the value if it was passed in registers. |
| 2710 | |
| 2711 | CGF.EmitBlock(InRegBlock); |
| 2712 | |
| 2713 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2714 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2715 | // copying to a temporary location in case the parameter is passed |
| 2716 | // in different register classes or requires an alignment greater |
| 2717 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2718 | // |
| 2719 | // FIXME: This really results in shameful code when we end up needing to |
| 2720 | // collect arguments from different places; often what should result in a |
| 2721 | // simple assembling of a structure from scattered addresses has many more |
| 2722 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2723 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2724 | llvm::Value *RegAddr = |
| 2725 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2726 | "reg_save_area"); |
| 2727 | if (neededInt && neededSSE) { |
| 2728 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2729 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2730 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2731 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2732 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2733 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2734 | llvm::Type *TyLo = ST->getElementType(0); |
| 2735 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2736 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2737 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2738 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2739 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2740 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2741 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2742 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2743 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2744 | llvm::Value *V = |
| 2745 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2746 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2747 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2748 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2749 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2750 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2751 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2752 | } else if (neededInt) { |
| 2753 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2754 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2755 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2756 | |
| 2757 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2758 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2759 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2760 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2761 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2762 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2763 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2764 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2765 | RegAddr = Tmp; |
| 2766 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2767 | } else if (neededSSE == 1) { |
| 2768 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2769 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2770 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2771 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2772 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2773 | // SSE registers are spaced 16 bytes apart in the register save |
| 2774 | // area, we need to collect the two eightbytes together. |
| 2775 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2776 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2777 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2778 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2779 | llvm::PointerType::getUnqual(DoubleTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2780 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); |
| 2781 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2782 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2783 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2784 | DblPtrTy)); |
| 2785 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2786 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2787 | DblPtrTy)); |
| 2788 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2789 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2790 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2794 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2795 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2796 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2797 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2798 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2799 | gp_offset_p); |
| 2800 | } |
| 2801 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2802 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2803 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2804 | fp_offset_p); |
| 2805 | } |
| 2806 | CGF.EmitBranch(ContBlock); |
| 2807 | |
| 2808 | // Emit code to load the value if it was passed in memory. |
| 2809 | |
| 2810 | CGF.EmitBlock(InMemBlock); |
| 2811 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2812 | |
| 2813 | // Return the appropriate result. |
| 2814 | |
| 2815 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2816 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2817 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2818 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2819 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2820 | return ResAddr; |
| 2821 | } |
| 2822 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2823 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2824 | |
| 2825 | if (Ty->isVoidType()) |
| 2826 | return ABIArgInfo::getIgnore(); |
| 2827 | |
| 2828 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2829 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2830 | |
| 2831 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2832 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2833 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2834 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2835 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2836 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2837 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 2838 | } |
| 2839 | |
| 2840 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2841 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2842 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2843 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 2844 | if (Size == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2845 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2846 | Size)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2847 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2848 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 2849 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 2850 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 2851 | // directly. |
| 2852 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 2853 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 2854 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | if (RT || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2858 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2859 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2860 | if (Size > 64 || !llvm::isPowerOf2_64(Size)) |
| 2861 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2862 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2863 | // Otherwise, coerce it to a small integer. |
| 2864 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Julien Lerouge | 10dcff8 | 2014-08-27 00:36:55 +0000 | [diff] [blame] | 2867 | // Bool type is always extended to the ABI, other builtin types are not |
| 2868 | // extended. |
| 2869 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 2870 | if (BT && BT->getKind() == BuiltinType::Bool) |
Julien Lerouge | e8d34fa | 2014-08-26 22:11:53 +0000 | [diff] [blame] | 2871 | return ABIArgInfo::getExtend(); |
| 2872 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2873 | return ABIArgInfo::getDirect(); |
| 2874 | } |
| 2875 | |
| 2876 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2877 | if (!getCXXABI().classifyReturnType(FI)) |
| 2878 | FI.getReturnInfo() = classify(FI.getReturnType(), true); |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2879 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2880 | for (auto &I : FI.arguments()) |
| 2881 | I.info = classify(I.type, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2884 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2885 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2886 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2887 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2888 | CGBuilderTy &Builder = CGF.Builder; |
| 2889 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2890 | "ap"); |
| 2891 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2892 | llvm::Type *PTy = |
| 2893 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2894 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2895 | |
| 2896 | uint64_t Offset = |
| 2897 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2898 | llvm::Value *NextAddr = |
| 2899 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2900 | "ap.next"); |
| 2901 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2902 | |
| 2903 | return AddrTyped; |
| 2904 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2905 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2906 | namespace { |
| 2907 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2908 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2909 | public: |
| 2910 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2911 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2912 | void computeInfo(CGFunctionInfo &FI) const override; |
| 2913 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2914 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2915 | private: |
| 2916 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2917 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2918 | }; |
| 2919 | |
| 2920 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 2921 | bool HasAVX; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2922 | public: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 2923 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2924 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)), HasAVX(HasAVX) { |
| 2925 | } |
| 2926 | unsigned getOpenMPSimdDefaultAlignment(QualType) const override { |
| 2927 | return HasAVX ? 32 : 16; |
| 2928 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2929 | }; |
| 2930 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2933 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2934 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2935 | PInfo.computeInfo(FI); |
| 2936 | else |
| 2937 | NInfo.computeInfo(FI); |
| 2938 | } |
| 2939 | |
| 2940 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2941 | CodeGenFunction &CGF) const { |
| 2942 | // Always use the native convention; calling pnacl-style varargs functions |
| 2943 | // is unuspported. |
| 2944 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2945 | } |
| 2946 | |
| 2947 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2948 | // PowerPC-32 |
| 2949 | |
| 2950 | namespace { |
| 2951 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2952 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2953 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2954 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2955 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2956 | // This is recovered from gcc output. |
| 2957 | return 1; // r1 is the dedicated stack pointer |
| 2958 | } |
| 2959 | |
| 2960 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2961 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2962 | }; |
| 2963 | |
| 2964 | } |
| 2965 | |
| 2966 | bool |
| 2967 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2968 | llvm::Value *Address) const { |
| 2969 | // This is calculated from the LLVM and GCC tables and verified |
| 2970 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2971 | |
| 2972 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2973 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2974 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2975 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2976 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2977 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2978 | |
| 2979 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2980 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2981 | |
| 2982 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2983 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2984 | |
| 2985 | // 64-76 are various 4-byte special-purpose registers: |
| 2986 | // 64: mq |
| 2987 | // 65: lr |
| 2988 | // 66: ctr |
| 2989 | // 67: ap |
| 2990 | // 68-75 cr0-7 |
| 2991 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2992 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2993 | |
| 2994 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2995 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2996 | |
| 2997 | // 109: vrsave |
| 2998 | // 110: vscr |
| 2999 | // 111: spe_acc |
| 3000 | // 112: spefscr |
| 3001 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 3002 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3003 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3004 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3007 | // PowerPC-64 |
| 3008 | |
| 3009 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3010 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 3011 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3012 | public: |
| 3013 | enum ABIKind { |
| 3014 | ELFv1 = 0, |
| 3015 | ELFv2 |
| 3016 | }; |
| 3017 | |
| 3018 | private: |
| 3019 | static const unsigned GPRBits = 64; |
| 3020 | ABIKind Kind; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3021 | |
| 3022 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3023 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind) |
| 3024 | : DefaultABIInfo(CGT), Kind(Kind) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3025 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3026 | bool isPromotableTypeForABI(QualType Ty) const; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3027 | bool isAlignedParamType(QualType Ty) const; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3028 | bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3029 | uint64_t &Members) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3030 | |
| 3031 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3032 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 3033 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3034 | // TODO: We can add more logic to computeInfo to improve performance. |
| 3035 | // Example: For aggregate arguments that fit in a register, we could |
| 3036 | // use getDirectInReg (as is done below for structs containing a single |
| 3037 | // floating-point value) to avoid pushing them to memory on function |
| 3038 | // entry. This would require changing the logic in PPCISelLowering |
| 3039 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3040 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3041 | if (!getCXXABI().classifyReturnType(FI)) |
| 3042 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3043 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3044 | // We rely on the default argument classification for the most part. |
| 3045 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 3046 | // 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] | 3047 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3048 | if (T) { |
| 3049 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3050 | if ((T->isVectorType() && getContext().getTypeSize(T) == 128) || |
| 3051 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3052 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3053 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3054 | continue; |
| 3055 | } |
| 3056 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3057 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 3058 | } |
| 3059 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3060 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3061 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3062 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3063 | }; |
| 3064 | |
| 3065 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3066 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3067 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
| 3068 | PPC64_SVR4_ABIInfo::ABIKind Kind) |
| 3069 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3070 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3071 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3072 | // This is recovered from gcc output. |
| 3073 | return 1; // r1 is the dedicated stack pointer |
| 3074 | } |
| 3075 | |
| 3076 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3077 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3078 | }; |
| 3079 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3080 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 3081 | public: |
| 3082 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 3083 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3084 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3085 | // This is recovered from gcc output. |
| 3086 | return 1; // r1 is the dedicated stack pointer |
| 3087 | } |
| 3088 | |
| 3089 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3090 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3091 | }; |
| 3092 | |
| 3093 | } |
| 3094 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3095 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 3096 | // extended to 64 bits. |
| 3097 | bool |
| 3098 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 3099 | // Treat an enum type as its underlying type. |
| 3100 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3101 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3102 | |
| 3103 | // Promotable integer types are required to be promoted by the ABI. |
| 3104 | if (Ty->isPromotableIntegerType()) |
| 3105 | return true; |
| 3106 | |
| 3107 | // In addition to the usual promotable integer types, we also need to |
| 3108 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 3109 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 3110 | switch (BT->getKind()) { |
| 3111 | case BuiltinType::Int: |
| 3112 | case BuiltinType::UInt: |
| 3113 | return true; |
| 3114 | default: |
| 3115 | break; |
| 3116 | } |
| 3117 | |
| 3118 | return false; |
| 3119 | } |
| 3120 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3121 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3122 | /// alignment in the parameter area. |
| 3123 | bool |
| 3124 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty) const { |
| 3125 | // Complex types are passed just like their elements. |
| 3126 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3127 | Ty = CTy->getElementType(); |
| 3128 | |
| 3129 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3130 | // passed via reference, smaller types are not aligned). |
| 3131 | if (Ty->isVectorType()) |
| 3132 | return getContext().getTypeSize(Ty) == 128; |
| 3133 | |
| 3134 | // For single-element float/vector structs, we consider the whole type |
| 3135 | // to have the same alignment requirements as its single element. |
| 3136 | const Type *AlignAsType = nullptr; |
| 3137 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3138 | if (EltType) { |
| 3139 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 3140 | if ((EltType->isVectorType() && |
| 3141 | getContext().getTypeSize(EltType) == 128) || |
| 3142 | (BT && BT->isFloatingPoint())) |
| 3143 | AlignAsType = EltType; |
| 3144 | } |
| 3145 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3146 | // Likewise for ELFv2 homogeneous aggregates. |
| 3147 | const Type *Base = nullptr; |
| 3148 | uint64_t Members = 0; |
| 3149 | if (!AlignAsType && Kind == ELFv2 && |
| 3150 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3151 | AlignAsType = Base; |
| 3152 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3153 | // With special case aggregates, only vector base types need alignment. |
| 3154 | if (AlignAsType) |
| 3155 | return AlignAsType->isVectorType(); |
| 3156 | |
| 3157 | // Otherwise, we only need alignment for any aggregate type that |
| 3158 | // has an alignment requirement of >= 16 bytes. |
| 3159 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) |
| 3160 | return true; |
| 3161 | |
| 3162 | return false; |
| 3163 | } |
| 3164 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3165 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3166 | /// aggregate. Base is set to the base element type, and Members is set |
| 3167 | /// to the number of base elements. |
| 3168 | bool |
| 3169 | PPC64_SVR4_ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3170 | uint64_t &Members) const { |
| 3171 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3172 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3173 | if (NElements == 0) |
| 3174 | return false; |
| 3175 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3176 | return false; |
| 3177 | Members *= NElements; |
| 3178 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3179 | const RecordDecl *RD = RT->getDecl(); |
| 3180 | if (RD->hasFlexibleArrayMember()) |
| 3181 | return false; |
| 3182 | |
| 3183 | Members = 0; |
| 3184 | for (const auto *FD : RD->fields()) { |
| 3185 | // Ignore (non-zero arrays of) empty records. |
| 3186 | QualType FT = FD->getType(); |
| 3187 | while (const ConstantArrayType *AT = |
| 3188 | getContext().getAsConstantArrayType(FT)) { |
| 3189 | if (AT->getSize().getZExtValue() == 0) |
| 3190 | return false; |
| 3191 | FT = AT->getElementType(); |
| 3192 | } |
| 3193 | if (isEmptyRecord(getContext(), FT, true)) |
| 3194 | continue; |
| 3195 | |
| 3196 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3197 | if (getContext().getLangOpts().CPlusPlus && |
| 3198 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3199 | continue; |
| 3200 | |
| 3201 | uint64_t FldMembers; |
| 3202 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3203 | return false; |
| 3204 | |
| 3205 | Members = (RD->isUnion() ? |
| 3206 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3207 | } |
| 3208 | |
| 3209 | if (!Base) |
| 3210 | return false; |
| 3211 | |
| 3212 | // Ensure there is no padding. |
| 3213 | if (getContext().getTypeSize(Base) * Members != |
| 3214 | getContext().getTypeSize(Ty)) |
| 3215 | return false; |
| 3216 | } else { |
| 3217 | Members = 1; |
| 3218 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3219 | Members = 2; |
| 3220 | Ty = CT->getElementType(); |
| 3221 | } |
| 3222 | |
| 3223 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3224 | // double, long double, or 128-bit vectors. |
| 3225 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3226 | if (BT->getKind() != BuiltinType::Float && |
| 3227 | BT->getKind() != BuiltinType::Double && |
| 3228 | BT->getKind() != BuiltinType::LongDouble) |
| 3229 | return false; |
| 3230 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3231 | if (getContext().getTypeSize(VT) != 128) |
| 3232 | return false; |
| 3233 | } else { |
| 3234 | return false; |
| 3235 | } |
| 3236 | |
| 3237 | // The base type must be the same for all members. Types that |
| 3238 | // agree in both total size and mode (float vs. vector) are |
| 3239 | // treated as being equivalent here. |
| 3240 | const Type *TyPtr = Ty.getTypePtr(); |
| 3241 | if (!Base) |
| 3242 | Base = TyPtr; |
| 3243 | |
| 3244 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3245 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3246 | return false; |
| 3247 | } |
| 3248 | |
| 3249 | // Vector types require one register, floating point types require one |
| 3250 | // or two registers depending on their size. |
| 3251 | uint32_t NumRegs = Base->isVectorType() ? 1 : |
| 3252 | (getContext().getTypeSize(Base) + 63) / 64; |
| 3253 | |
| 3254 | // Homogeneous Aggregates may occupy at most 8 registers. |
| 3255 | return (Members > 0 && Members * NumRegs <= 8); |
| 3256 | } |
| 3257 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3258 | ABIArgInfo |
| 3259 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3260 | if (Ty->isAnyComplexType()) |
| 3261 | return ABIArgInfo::getDirect(); |
| 3262 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3263 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3264 | // or via reference (larger than 16 bytes). |
| 3265 | if (Ty->isVectorType()) { |
| 3266 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3267 | if (Size > 128) |
| 3268 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3269 | else if (Size < 128) { |
| 3270 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3271 | return ABIArgInfo::getDirect(CoerceTy); |
| 3272 | } |
| 3273 | } |
| 3274 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3275 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3276 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3277 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3278 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3279 | uint64_t ABIAlign = isAlignedParamType(Ty)? 16 : 8; |
| 3280 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3281 | |
| 3282 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3283 | const Type *Base = nullptr; |
| 3284 | uint64_t Members = 0; |
| 3285 | if (Kind == ELFv2 && |
| 3286 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3287 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3288 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3289 | return ABIArgInfo::getDirect(CoerceTy); |
| 3290 | } |
| 3291 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3292 | // If an aggregate may end up fully in registers, we do not |
| 3293 | // use the ByVal method, but pass the aggregate as array. |
| 3294 | // This is usually beneficial since we avoid forcing the |
| 3295 | // back-end to store the argument to memory. |
| 3296 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3297 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3298 | llvm::Type *CoerceTy; |
| 3299 | |
| 3300 | // Types up to 8 bytes are passed as integer type (which will be |
| 3301 | // properly aligned in the argument save area doubleword). |
| 3302 | if (Bits <= GPRBits) |
| 3303 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3304 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3305 | // Larger types are passed as arrays, with the base type selected |
| 3306 | // according to the required alignment in the save area. |
| 3307 | else { |
| 3308 | uint64_t RegBits = ABIAlign * 8; |
| 3309 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3310 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3311 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3312 | } |
| 3313 | |
| 3314 | return ABIArgInfo::getDirect(CoerceTy); |
| 3315 | } |
| 3316 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3317 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3318 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3319 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | return (isPromotableTypeForABI(Ty) ? |
| 3323 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3324 | } |
| 3325 | |
| 3326 | ABIArgInfo |
| 3327 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3328 | if (RetTy->isVoidType()) |
| 3329 | return ABIArgInfo::getIgnore(); |
| 3330 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3331 | if (RetTy->isAnyComplexType()) |
| 3332 | return ABIArgInfo::getDirect(); |
| 3333 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3334 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3335 | // or via reference (larger than 16 bytes). |
| 3336 | if (RetTy->isVectorType()) { |
| 3337 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3338 | if (Size > 128) |
| 3339 | return ABIArgInfo::getIndirect(0); |
| 3340 | else if (Size < 128) { |
| 3341 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3342 | return ABIArgInfo::getDirect(CoerceTy); |
| 3343 | } |
| 3344 | } |
| 3345 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3346 | if (isAggregateTypeForABI(RetTy)) { |
| 3347 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3348 | const Type *Base = nullptr; |
| 3349 | uint64_t Members = 0; |
| 3350 | if (Kind == ELFv2 && |
| 3351 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3352 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3353 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3354 | return ABIArgInfo::getDirect(CoerceTy); |
| 3355 | } |
| 3356 | |
| 3357 | // ELFv2 small aggregates are returned in up to two registers. |
| 3358 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3359 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3360 | if (Bits == 0) |
| 3361 | return ABIArgInfo::getIgnore(); |
| 3362 | |
| 3363 | llvm::Type *CoerceTy; |
| 3364 | if (Bits > GPRBits) { |
| 3365 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
| 3366 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, NULL); |
| 3367 | } else |
| 3368 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3369 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3370 | return ABIArgInfo::getDirect(CoerceTy); |
| 3371 | } |
| 3372 | |
| 3373 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3374 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3375 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3376 | |
| 3377 | return (isPromotableTypeForABI(RetTy) ? |
| 3378 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3379 | } |
| 3380 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3381 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3382 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3383 | QualType Ty, |
| 3384 | CodeGenFunction &CGF) const { |
| 3385 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3386 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3387 | |
| 3388 | CGBuilderTy &Builder = CGF.Builder; |
| 3389 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3390 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3391 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3392 | // Handle types that require 16-byte alignment in the parameter save area. |
| 3393 | if (isAlignedParamType(Ty)) { |
| 3394 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3395 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(15)); |
| 3396 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(-16)); |
| 3397 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3398 | } |
| 3399 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3400 | // Update the va_list pointer. The pointer should be bumped by the |
| 3401 | // size of the object. We can trust getTypeSize() except for a complex |
| 3402 | // type whose base type is smaller than a doubleword. For these, the |
| 3403 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3404 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3405 | QualType BaseTy; |
| 3406 | unsigned CplxBaseSize = 0; |
| 3407 | |
| 3408 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3409 | BaseTy = CTy->getElementType(); |
| 3410 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3411 | if (CplxBaseSize < 8) |
| 3412 | SizeInBytes = 16; |
| 3413 | } |
| 3414 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3415 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3416 | llvm::Value *NextAddr = |
| 3417 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3418 | "ap.next"); |
| 3419 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3420 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3421 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3422 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3423 | // in separate doublewords. However, Clang expects us to produce a |
| 3424 | // pointer to a structure with the two parts packed tightly. So generate |
| 3425 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3426 | // and store them to a temporary structure. |
| 3427 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3428 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3429 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3430 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 3431 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3432 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3433 | } else { |
| 3434 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3435 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3436 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3437 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3438 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3439 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3440 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 3441 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 3442 | "vacplx"); |
| 3443 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 3444 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 3445 | Builder.CreateStore(Real, RealPtr, false); |
| 3446 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3447 | return Ptr; |
| 3448 | } |
| 3449 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3450 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3451 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3452 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3453 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3454 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3455 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3456 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3457 | } |
| 3458 | |
| 3459 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3460 | return Builder.CreateBitCast(Addr, PTy); |
| 3461 | } |
| 3462 | |
| 3463 | static bool |
| 3464 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3465 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3466 | // This is calculated from the LLVM and GCC tables and verified |
| 3467 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3468 | |
| 3469 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3470 | |
| 3471 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3472 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3473 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3474 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3475 | |
| 3476 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3477 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3478 | |
| 3479 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3480 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3481 | |
| 3482 | // 64-76 are various 4-byte special-purpose registers: |
| 3483 | // 64: mq |
| 3484 | // 65: lr |
| 3485 | // 66: ctr |
| 3486 | // 67: ap |
| 3487 | // 68-75 cr0-7 |
| 3488 | // 76: xer |
| 3489 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3490 | |
| 3491 | // 77-108: v0-31, the 16-byte vector registers |
| 3492 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3493 | |
| 3494 | // 109: vrsave |
| 3495 | // 110: vscr |
| 3496 | // 111: spe_acc |
| 3497 | // 112: spefscr |
| 3498 | // 113: sfp |
| 3499 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3500 | |
| 3501 | return false; |
| 3502 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3503 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3504 | bool |
| 3505 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3506 | CodeGen::CodeGenFunction &CGF, |
| 3507 | llvm::Value *Address) const { |
| 3508 | |
| 3509 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3510 | } |
| 3511 | |
| 3512 | bool |
| 3513 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3514 | llvm::Value *Address) const { |
| 3515 | |
| 3516 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3517 | } |
| 3518 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3519 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3520 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3521 | //===----------------------------------------------------------------------===// |
| 3522 | |
| 3523 | namespace { |
| 3524 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3525 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3526 | public: |
| 3527 | enum ABIKind { |
| 3528 | AAPCS = 0, |
| 3529 | DarwinPCS |
| 3530 | }; |
| 3531 | |
| 3532 | private: |
| 3533 | ABIKind Kind; |
| 3534 | |
| 3535 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3536 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3537 | |
| 3538 | private: |
| 3539 | ABIKind getABIKind() const { return Kind; } |
| 3540 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3541 | |
| 3542 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3543 | ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &AllocatedVFP, |
| 3544 | bool &IsHA, unsigned &AllocatedGPR, |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3545 | bool &IsSmallAggr, bool IsNamedArg) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3546 | bool isIllegalVectorType(QualType Ty) const; |
| 3547 | |
| 3548 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 3549 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
| 3550 | // number of SIMD and Floating-point registers allocated so far. |
| 3551 | // If the argument is an HFA or an HVA and there are sufficient unallocated |
| 3552 | // SIMD and Floating-point registers, then the argument is allocated to SIMD |
| 3553 | // and Floating-point Registers (with one register per member of the HFA or |
| 3554 | // HVA). Otherwise, the NSRN is set to 8. |
| 3555 | unsigned AllocatedVFP = 0; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3556 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3557 | // To correctly handle small aggregates, we need to keep track of the number |
| 3558 | // of GPRs allocated so far. If the small aggregate can't all fit into |
| 3559 | // registers, it will be on stack. We don't allow the aggregate to be |
| 3560 | // partially in registers. |
| 3561 | unsigned AllocatedGPR = 0; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3562 | |
| 3563 | // Find the number of named arguments. Variadic arguments get special |
| 3564 | // treatment with the Darwin ABI. |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3565 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3566 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3567 | if (!getCXXABI().classifyReturnType(FI)) |
| 3568 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3569 | unsigned ArgNo = 0; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3570 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3571 | it != ie; ++it, ++ArgNo) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3572 | unsigned PreAllocation = AllocatedVFP, PreGPR = AllocatedGPR; |
| 3573 | bool IsHA = false, IsSmallAggr = false; |
| 3574 | const unsigned NumVFPs = 8; |
| 3575 | const unsigned NumGPRs = 8; |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3576 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3577 | it->info = classifyArgumentType(it->type, AllocatedVFP, IsHA, |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3578 | AllocatedGPR, IsSmallAggr, IsNamedArg); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3579 | |
| 3580 | // Under AAPCS the 64-bit stack slot alignment means we can't pass HAs |
| 3581 | // as sequences of floats since they'll get "holes" inserted as |
| 3582 | // padding by the back end. |
Tim Northover | 07f1624 | 2014-04-18 10:47:44 +0000 | [diff] [blame] | 3583 | if (IsHA && AllocatedVFP > NumVFPs && !isDarwinPCS() && |
| 3584 | getContext().getTypeAlign(it->type) < 64) { |
| 3585 | uint32_t NumStackSlots = getContext().getTypeSize(it->type); |
| 3586 | NumStackSlots = llvm::RoundUpToAlignment(NumStackSlots, 64) / 64; |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3587 | |
Tim Northover | 07f1624 | 2014-04-18 10:47:44 +0000 | [diff] [blame] | 3588 | llvm::Type *CoerceTy = llvm::ArrayType::get( |
| 3589 | llvm::Type::getDoubleTy(getVMContext()), NumStackSlots); |
| 3590 | it->info = ABIArgInfo::getDirect(CoerceTy); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3593 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3594 | // that are unallocated are marked as unavailable. To achieve this, we add |
| 3595 | // padding of (NumVFPs - PreAllocation) floats. |
| 3596 | if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { |
| 3597 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3598 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3599 | it->info.setPaddingType(PaddingTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3600 | } |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3601 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3602 | // If we do not have enough GPRs for the small aggregate, any GPR regs |
| 3603 | // that are unallocated are marked as unavailable. |
| 3604 | if (IsSmallAggr && AllocatedGPR > NumGPRs && PreGPR < NumGPRs) { |
| 3605 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3606 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreGPR); |
| 3607 | it->info = |
| 3608 | ABIArgInfo::getDirect(it->info.getCoerceToType(), 0, PaddingTy); |
| 3609 | } |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3614 | CodeGenFunction &CGF) const; |
| 3615 | |
| 3616 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3617 | CodeGenFunction &CGF) const; |
| 3618 | |
| 3619 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3620 | CodeGenFunction &CGF) const { |
| 3621 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3622 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3623 | } |
| 3624 | }; |
| 3625 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3626 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3627 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3628 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3629 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3630 | |
| 3631 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
| 3632 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3633 | } |
| 3634 | |
| 3635 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; } |
| 3636 | |
| 3637 | virtual bool doesReturnSlotInterfereWithArgs() const { return false; } |
| 3638 | }; |
| 3639 | } |
| 3640 | |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 3641 | static bool isARMHomogeneousAggregate(QualType Ty, const Type *&Base, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3642 | ASTContext &Context, |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 3643 | bool isAArch64, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3644 | uint64_t *HAMembers = nullptr); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3645 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3646 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty, |
| 3647 | unsigned &AllocatedVFP, |
| 3648 | bool &IsHA, |
| 3649 | unsigned &AllocatedGPR, |
| 3650 | bool &IsSmallAggr, |
| 3651 | bool IsNamedArg) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3652 | // Handle illegal vector types here. |
| 3653 | if (isIllegalVectorType(Ty)) { |
| 3654 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3655 | if (Size <= 32) { |
| 3656 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
| 3657 | AllocatedGPR++; |
| 3658 | return ABIArgInfo::getDirect(ResType); |
| 3659 | } |
| 3660 | if (Size == 64) { |
| 3661 | llvm::Type *ResType = |
| 3662 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
| 3663 | AllocatedVFP++; |
| 3664 | return ABIArgInfo::getDirect(ResType); |
| 3665 | } |
| 3666 | if (Size == 128) { |
| 3667 | llvm::Type *ResType = |
| 3668 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
| 3669 | AllocatedVFP++; |
| 3670 | return ABIArgInfo::getDirect(ResType); |
| 3671 | } |
| 3672 | AllocatedGPR++; |
| 3673 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3674 | } |
| 3675 | if (Ty->isVectorType()) |
| 3676 | // Size of a legal vector should be either 64 or 128. |
| 3677 | AllocatedVFP++; |
| 3678 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3679 | if (BT->getKind() == BuiltinType::Half || |
| 3680 | BT->getKind() == BuiltinType::Float || |
| 3681 | BT->getKind() == BuiltinType::Double || |
| 3682 | BT->getKind() == BuiltinType::LongDouble) |
| 3683 | AllocatedVFP++; |
| 3684 | } |
| 3685 | |
| 3686 | if (!isAggregateTypeForABI(Ty)) { |
| 3687 | // Treat an enum type as its underlying type. |
| 3688 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3689 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3690 | |
| 3691 | if (!Ty->isFloatingType() && !Ty->isVectorType()) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3692 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3693 | if (!isDarwinPCS() && Alignment > 64) |
| 3694 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3695 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3696 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 3697 | AllocatedGPR += RegsNeeded; |
| 3698 | } |
| 3699 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 3700 | ? ABIArgInfo::getExtend() |
| 3701 | : ABIArgInfo::getDirect()); |
| 3702 | } |
| 3703 | |
| 3704 | // Structures with either a non-trivial destructor or a non-trivial |
| 3705 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3706 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3707 | AllocatedGPR++; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3708 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
| 3709 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 3713 | // elsewhere for GNU compatibility. |
| 3714 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3715 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 3716 | return ABIArgInfo::getIgnore(); |
| 3717 | |
| 3718 | ++AllocatedGPR; |
| 3719 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 3720 | } |
| 3721 | |
| 3722 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3723 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3724 | uint64_t Members = 0; |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 3725 | if (isARMHomogeneousAggregate(Ty, Base, getContext(), true, &Members)) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3726 | IsHA = true; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3727 | if (!IsNamedArg && isDarwinPCS()) { |
| 3728 | // With the Darwin ABI, variadic arguments are always passed on the stack |
| 3729 | // and should not be expanded. Treat variadic HFAs as arrays of doubles. |
| 3730 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3731 | llvm::Type *BaseTy = llvm::Type::getDoubleTy(getVMContext()); |
| 3732 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 3733 | } |
| 3734 | AllocatedVFP += Members; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3735 | return ABIArgInfo::getExpand(); |
| 3736 | } |
| 3737 | |
| 3738 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 3739 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3740 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3741 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3742 | if (!isDarwinPCS() && Alignment > 64) |
| 3743 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3744 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3745 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3746 | AllocatedGPR += Size / 64; |
| 3747 | IsSmallAggr = true; |
| 3748 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 3749 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3750 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3751 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3752 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 3753 | } |
| 3754 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3755 | } |
| 3756 | |
| 3757 | AllocatedGPR++; |
| 3758 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3759 | } |
| 3760 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3761 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3762 | if (RetTy->isVoidType()) |
| 3763 | return ABIArgInfo::getIgnore(); |
| 3764 | |
| 3765 | // Large vector types should be returned via memory. |
| 3766 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 3767 | return ABIArgInfo::getIndirect(0); |
| 3768 | |
| 3769 | if (!isAggregateTypeForABI(RetTy)) { |
| 3770 | // Treat an enum type as its underlying type. |
| 3771 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3772 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3773 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 3774 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 3775 | ? ABIArgInfo::getExtend() |
| 3776 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3779 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 3780 | return ABIArgInfo::getIgnore(); |
| 3781 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3782 | const Type *Base = nullptr; |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 3783 | if (isARMHomogeneousAggregate(RetTy, Base, getContext(), true)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3784 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 3785 | return ABIArgInfo::getDirect(); |
| 3786 | |
| 3787 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 3788 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3789 | if (Size <= 128) { |
| 3790 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3791 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3792 | } |
| 3793 | |
| 3794 | return ABIArgInfo::getIndirect(0); |
| 3795 | } |
| 3796 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3797 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 3798 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3799 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3800 | // Check whether VT is legal. |
| 3801 | unsigned NumElements = VT->getNumElements(); |
| 3802 | uint64_t Size = getContext().getTypeSize(VT); |
| 3803 | // NumElements should be power of 2 between 1 and 16. |
| 3804 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 3805 | return true; |
| 3806 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 3807 | } |
| 3808 | return false; |
| 3809 | } |
| 3810 | |
| 3811 | static llvm::Value *EmitAArch64VAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3812 | int AllocatedGPR, int AllocatedVFP, |
| 3813 | bool IsIndirect, CodeGenFunction &CGF) { |
| 3814 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 3815 | // Standard, section B.4: |
| 3816 | // |
| 3817 | // struct { |
| 3818 | // void *__stack; |
| 3819 | // void *__gr_top; |
| 3820 | // void *__vr_top; |
| 3821 | // int __gr_offs; |
| 3822 | // int __vr_offs; |
| 3823 | // }; |
| 3824 | |
| 3825 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 3826 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3827 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 3828 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3829 | auto &Ctx = CGF.getContext(); |
| 3830 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3831 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3832 | int reg_top_index; |
| 3833 | int RegSize; |
| 3834 | if (AllocatedGPR) { |
| 3835 | assert(!AllocatedVFP && "Arguments never split between int & VFP regs"); |
| 3836 | // 3 is the field number of __gr_offs |
| 3837 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 3838 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 3839 | reg_top_index = 1; // field number for __gr_top |
| 3840 | RegSize = 8 * AllocatedGPR; |
| 3841 | } else { |
| 3842 | assert(!AllocatedGPR && "Argument must go in VFP or int regs"); |
| 3843 | // 4 is the field number of __vr_offs. |
| 3844 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 3845 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 3846 | reg_top_index = 2; // field number for __vr_top |
| 3847 | RegSize = 16 * AllocatedVFP; |
| 3848 | } |
| 3849 | |
| 3850 | //======================================= |
| 3851 | // Find out where argument was passed |
| 3852 | //======================================= |
| 3853 | |
| 3854 | // If reg_offs >= 0 we're already using the stack for this type of |
| 3855 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 3856 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 3857 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3858 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3859 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 3860 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 3861 | |
| 3862 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 3863 | |
| 3864 | // 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] | 3865 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3866 | CGF.EmitBlock(MaybeRegBlock); |
| 3867 | |
| 3868 | // Integer arguments may need to correct register alignment (for example a |
| 3869 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 3870 | // align __gr_offs to calculate the potential address. |
| 3871 | if (AllocatedGPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3872 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3873 | |
| 3874 | reg_offs = CGF.Builder.CreateAdd( |
| 3875 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 3876 | "align_regoffs"); |
| 3877 | reg_offs = CGF.Builder.CreateAnd( |
| 3878 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 3879 | "aligned_regoffs"); |
| 3880 | } |
| 3881 | |
| 3882 | // 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] | 3883 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3884 | NewOffset = CGF.Builder.CreateAdd( |
| 3885 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 3886 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 3887 | |
| 3888 | // Now we're in a position to decide whether this argument really was in |
| 3889 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3890 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3891 | InRegs = CGF.Builder.CreateICmpSLE( |
| 3892 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 3893 | |
| 3894 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 3895 | |
| 3896 | //======================================= |
| 3897 | // Argument was in registers |
| 3898 | //======================================= |
| 3899 | |
| 3900 | // Now we emit the code for if the argument was originally passed in |
| 3901 | // registers. First start the appropriate block: |
| 3902 | CGF.EmitBlock(InRegBlock); |
| 3903 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3904 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3905 | reg_top_p = |
| 3906 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 3907 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 3908 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3909 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3910 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 3911 | |
| 3912 | if (IsIndirect) { |
| 3913 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 3914 | // stored registers or on the stack will actually be a struct **. |
| 3915 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 3916 | } |
| 3917 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3918 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3919 | uint64_t NumMembers; |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 3920 | bool IsHFA = isARMHomogeneousAggregate(Ty, Base, Ctx, true, &NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 3921 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3922 | // Homogeneous aggregates passed in registers will have their elements split |
| 3923 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 3924 | // qN+1, ...). We reload and store into a temporary local variable |
| 3925 | // contiguously. |
| 3926 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 3927 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 3928 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 3929 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 3930 | int Offset = 0; |
| 3931 | |
| 3932 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 3933 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 3934 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 3935 | llvm::Value *BaseOffset = |
| 3936 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 3937 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 3938 | LoadAddr = CGF.Builder.CreateBitCast( |
| 3939 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
| 3940 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 3941 | |
| 3942 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 3943 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 3944 | } |
| 3945 | |
| 3946 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 3947 | } else { |
| 3948 | // Otherwise the object is contiguous in memory |
| 3949 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 3950 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 3951 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3952 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 3953 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 3954 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 3955 | |
| 3956 | BaseAddr = CGF.Builder.CreateAdd( |
| 3957 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 3958 | |
| 3959 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 3960 | } |
| 3961 | |
| 3962 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 3963 | } |
| 3964 | |
| 3965 | CGF.EmitBranch(ContBlock); |
| 3966 | |
| 3967 | //======================================= |
| 3968 | // Argument was on the stack |
| 3969 | //======================================= |
| 3970 | CGF.EmitBlock(OnStackBlock); |
| 3971 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3972 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3973 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 3974 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 3975 | |
| 3976 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 3977 | // floating-point ones might be affected. |
| 3978 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3979 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3980 | |
| 3981 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3982 | |
| 3983 | OnStackAddr = CGF.Builder.CreateAdd( |
| 3984 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 3985 | "align_stack"); |
| 3986 | OnStackAddr = CGF.Builder.CreateAnd( |
| 3987 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 3988 | "align_stack"); |
| 3989 | |
| 3990 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3991 | } |
| 3992 | |
| 3993 | uint64_t StackSize; |
| 3994 | if (IsIndirect) |
| 3995 | StackSize = 8; |
| 3996 | else |
| 3997 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 3998 | |
| 3999 | // All stack slots are 8 bytes |
| 4000 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 4001 | |
| 4002 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 4003 | llvm::Value *NewStack = |
| 4004 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 4005 | |
| 4006 | // Write the new value of __stack for the next call to va_arg |
| 4007 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 4008 | |
| 4009 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 4010 | Ctx.getTypeSize(Ty) < 64) { |
| 4011 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 4012 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 4013 | |
| 4014 | OnStackAddr = CGF.Builder.CreateAdd( |
| 4015 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 4016 | |
| 4017 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 4018 | } |
| 4019 | |
| 4020 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 4021 | |
| 4022 | CGF.EmitBranch(ContBlock); |
| 4023 | |
| 4024 | //======================================= |
| 4025 | // Tidy up |
| 4026 | //======================================= |
| 4027 | CGF.EmitBlock(ContBlock); |
| 4028 | |
| 4029 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 4030 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 4031 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 4032 | |
| 4033 | if (IsIndirect) |
| 4034 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 4035 | |
| 4036 | return ResAddr; |
| 4037 | } |
| 4038 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4039 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4040 | CodeGenFunction &CGF) const { |
| 4041 | |
| 4042 | unsigned AllocatedGPR = 0, AllocatedVFP = 0; |
| 4043 | bool IsHA = false, IsSmallAggr = false; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 4044 | ABIArgInfo AI = classifyArgumentType(Ty, AllocatedVFP, IsHA, AllocatedGPR, |
| 4045 | IsSmallAggr, false /*IsNamedArg*/); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4046 | |
| 4047 | return EmitAArch64VAArg(VAListAddr, Ty, AllocatedGPR, AllocatedVFP, |
| 4048 | AI.isIndirect(), CGF); |
| 4049 | } |
| 4050 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4051 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4052 | CodeGenFunction &CGF) const { |
| 4053 | // We do not support va_arg for aggregates or illegal vector types. |
| 4054 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 4055 | // other cases. |
| 4056 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4057 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4058 | |
| 4059 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 4060 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 4061 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4062 | const Type *Base = nullptr; |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4063 | bool isHA = isARMHomogeneousAggregate(Ty, Base, getContext(), true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4064 | |
| 4065 | bool isIndirect = false; |
| 4066 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 4067 | // be passed indirectly. |
| 4068 | if (Size > 16 && !isHA) { |
| 4069 | isIndirect = true; |
| 4070 | Size = 8; |
| 4071 | Align = 8; |
| 4072 | } |
| 4073 | |
| 4074 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 4075 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 4076 | |
| 4077 | CGBuilderTy &Builder = CGF.Builder; |
| 4078 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 4079 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 4080 | |
| 4081 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4082 | // These are ignored for parameter passing purposes. |
| 4083 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4084 | return Builder.CreateBitCast(Addr, PTy); |
| 4085 | } |
| 4086 | |
| 4087 | const uint64_t MinABIAlign = 8; |
| 4088 | if (Align > MinABIAlign) { |
| 4089 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 4090 | Addr = Builder.CreateGEP(Addr, Offset); |
| 4091 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 4092 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 4093 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 4094 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 4095 | } |
| 4096 | |
| 4097 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 4098 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 4099 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 4100 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4101 | |
| 4102 | if (isIndirect) |
| 4103 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 4104 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4105 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4106 | |
| 4107 | return AddrTyped; |
| 4108 | } |
| 4109 | |
| 4110 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4111 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4112 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4113 | |
| 4114 | namespace { |
| 4115 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4116 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4117 | public: |
| 4118 | enum ABIKind { |
| 4119 | APCS = 0, |
| 4120 | AAPCS = 1, |
| 4121 | AAPCS_VFP |
| 4122 | }; |
| 4123 | |
| 4124 | private: |
| 4125 | ABIKind Kind; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4126 | mutable int VFPRegs[16]; |
| 4127 | const unsigned NumVFPs; |
| 4128 | const unsigned NumGPRs; |
| 4129 | mutable unsigned AllocatedGPRs; |
| 4130 | mutable unsigned AllocatedVFPs; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4131 | |
| 4132 | public: |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4133 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind), |
| 4134 | NumVFPs(16), NumGPRs(4) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4135 | setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4136 | resetAllocatedRegs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4137 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4138 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4139 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4140 | switch (getTarget().getTriple().getEnvironment()) { |
| 4141 | case llvm::Triple::Android: |
| 4142 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4143 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4144 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4145 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4146 | return true; |
| 4147 | default: |
| 4148 | return false; |
| 4149 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4152 | bool isEABIHF() const { |
| 4153 | switch (getTarget().getTriple().getEnvironment()) { |
| 4154 | case llvm::Triple::EABIHF: |
| 4155 | case llvm::Triple::GNUEABIHF: |
| 4156 | return true; |
| 4157 | default: |
| 4158 | return false; |
| 4159 | } |
| 4160 | } |
| 4161 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4162 | ABIKind getABIKind() const { return Kind; } |
| 4163 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4164 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4165 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4166 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4167 | bool &IsCPRC) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4168 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4169 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4170 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4171 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4172 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4173 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4174 | |
| 4175 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4176 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 4177 | void setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4178 | |
| 4179 | void markAllocatedGPRs(unsigned Alignment, unsigned NumRequired) const; |
| 4180 | void markAllocatedVFPs(unsigned Alignment, unsigned NumRequired) const; |
| 4181 | void resetAllocatedRegs(void) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4182 | }; |
| 4183 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4184 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4185 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4186 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4187 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4188 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4189 | const ARMABIInfo &getABIInfo() const { |
| 4190 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4191 | } |
| 4192 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4193 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4194 | return 13; |
| 4195 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4196 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4197 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4198 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4199 | } |
| 4200 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4201 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4202 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4203 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4204 | |
| 4205 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4206 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4207 | return false; |
| 4208 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4209 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4210 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4211 | if (getABIInfo().isEABI()) return 88; |
| 4212 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4213 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4214 | |
| 4215 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4216 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4217 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4218 | if (!FD) |
| 4219 | return; |
| 4220 | |
| 4221 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4222 | if (!Attr) |
| 4223 | return; |
| 4224 | |
| 4225 | const char *Kind; |
| 4226 | switch (Attr->getInterrupt()) { |
| 4227 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4228 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4229 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4230 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4231 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4232 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4233 | } |
| 4234 | |
| 4235 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4236 | |
| 4237 | Fn->addFnAttr("interrupt", Kind); |
| 4238 | |
| 4239 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4240 | return; |
| 4241 | |
| 4242 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4243 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4244 | // the backend to perform a realignment as part of the function prologue. |
| 4245 | llvm::AttrBuilder B; |
| 4246 | B.addStackAlignmentAttr(8); |
| 4247 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4248 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4249 | llvm::AttributeSet::FunctionIndex, |
| 4250 | B)); |
| 4251 | } |
| 4252 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4253 | }; |
| 4254 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4257 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4258 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4259 | // VFP registers allocated so far. |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4260 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 4261 | // VFP registers of the appropriate type unallocated then the argument is |
| 4262 | // allocated to the lowest-numbered sequence of such registers. |
| 4263 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 4264 | // unallocated are marked as unavailable. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4265 | resetAllocatedRegs(); |
| 4266 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4267 | const bool isAAPCS_VFP = |
| 4268 | getABIKind() == ARMABIInfo::AAPCS_VFP && !FI.isVariadic(); |
| 4269 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4270 | if (getCXXABI().classifyReturnType(FI)) { |
| 4271 | if (FI.getReturnInfo().isIndirect()) |
| 4272 | markAllocatedGPRs(1, 1); |
| 4273 | } else { |
| 4274 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
| 4275 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4276 | for (auto &I : FI.arguments()) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4277 | unsigned PreAllocationVFPs = AllocatedVFPs; |
| 4278 | unsigned PreAllocationGPRs = AllocatedGPRs; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4279 | bool IsCPRC = false; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4280 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 4281 | // s0-s15 (d0-d7) for passing arguments. |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4282 | I.info = classifyArgumentType(I.type, FI.isVariadic(), IsCPRC); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4283 | |
| 4284 | // If we have allocated some arguments onto the stack (due to running |
| 4285 | // out of VFP registers), we cannot split an argument between GPRs and |
| 4286 | // the stack. If this situation occurs, we add padding to prevent the |
Oliver Stannard | a3afc69 | 2014-05-19 13:10:05 +0000 | [diff] [blame] | 4287 | // GPRs from being used. In this situation, the current argument could |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4288 | // only be allocated by rule C.8, so rule C.6 would mark these GPRs as |
| 4289 | // unusable anyway. |
Oliver Stannard | e022851 | 2014-07-18 09:09:31 +0000 | [diff] [blame] | 4290 | // We do not have to do this if the argument is being passed ByVal, as the |
| 4291 | // backend can handle that situation correctly. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4292 | const bool StackUsed = PreAllocationGPRs > NumGPRs || PreAllocationVFPs > NumVFPs; |
Oliver Stannard | e022851 | 2014-07-18 09:09:31 +0000 | [diff] [blame] | 4293 | const bool IsByVal = I.info.isIndirect() && I.info.getIndirectByVal(); |
| 4294 | if (!IsCPRC && PreAllocationGPRs < NumGPRs && AllocatedGPRs > NumGPRs && |
| 4295 | StackUsed && !IsByVal) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4296 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 4297 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreAllocationGPRs); |
Oliver Stannard | a3afc69 | 2014-05-19 13:10:05 +0000 | [diff] [blame] | 4298 | if (I.info.canHaveCoerceToType()) { |
| 4299 | I.info = ABIArgInfo::getDirect(I.info.getCoerceToType() /* type */, 0 /* offset */, |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4300 | PaddingTy, !isAAPCS_VFP); |
Oliver Stannard | a3afc69 | 2014-05-19 13:10:05 +0000 | [diff] [blame] | 4301 | } else { |
| 4302 | I.info = ABIArgInfo::getDirect(nullptr /* type */, 0 /* offset */, |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4303 | PaddingTy, !isAAPCS_VFP); |
Oliver Stannard | a3afc69 | 2014-05-19 13:10:05 +0000 | [diff] [blame] | 4304 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4305 | } |
| 4306 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4307 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4308 | // Always honor user-specified calling convention. |
| 4309 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4310 | return; |
| 4311 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4312 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4313 | if (cc != llvm::CallingConv::C) |
| 4314 | FI.setEffectiveCallingConvention(cc); |
| 4315 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4316 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4317 | /// Return the default calling convention that LLVM will use. |
| 4318 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4319 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4320 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4321 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4322 | else if (isEABI()) |
| 4323 | return llvm::CallingConv::ARM_AAPCS; |
| 4324 | else |
| 4325 | return llvm::CallingConv::ARM_APCS; |
| 4326 | } |
| 4327 | |
| 4328 | /// Return the calling convention that our ABI would like us to use |
| 4329 | /// as the C calling convention. |
| 4330 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4331 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4332 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4333 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4334 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4335 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4336 | llvm_unreachable("bad ABI kind"); |
| 4337 | } |
| 4338 | |
| 4339 | void ARMABIInfo::setRuntimeCC() { |
| 4340 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4341 | |
| 4342 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4343 | // they'd just match what LLVM will infer from the triple. |
| 4344 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4345 | if (abiCC != getLLVMDefaultCC()) |
| 4346 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4347 | } |
| 4348 | |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4349 | /// isARMHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4350 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 4351 | /// contained in the type is returned through it; this is used for the |
| 4352 | /// recursive calls that check aggregate component types. |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4353 | static bool isARMHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4354 | ASTContext &Context, bool isAArch64, |
| 4355 | uint64_t *HAMembers) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4356 | uint64_t Members = 0; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4357 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4358 | if (!isARMHomogeneousAggregate(AT->getElementType(), Base, Context, isAArch64, &Members)) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4359 | return false; |
| 4360 | Members *= AT->getSize().getZExtValue(); |
| 4361 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4362 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4363 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4364 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4365 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4366 | Members = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4367 | for (const auto *FD : RD->fields()) { |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4368 | uint64_t FldMembers; |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4369 | if (!isARMHomogeneousAggregate(FD->getType(), Base, Context, isAArch64, &FldMembers)) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4370 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4371 | |
| 4372 | Members = (RD->isUnion() ? |
| 4373 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4374 | } |
| 4375 | } else { |
| 4376 | Members = 1; |
| 4377 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4378 | Members = 2; |
| 4379 | Ty = CT->getElementType(); |
| 4380 | } |
| 4381 | |
| 4382 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4383 | // double, or 64-bit or 128-bit vectors. "long double" has the same machine |
| 4384 | // type as double, so it is also allowed as a base type. |
| 4385 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 4386 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 4387 | // but with the difference that any floating-point type is allowed, |
| 4388 | // including __fp16. |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4389 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4390 | if (isAArch64) { |
| 4391 | if (!BT->isFloatingPoint()) |
| 4392 | return false; |
| 4393 | } else { |
| 4394 | if (BT->getKind() != BuiltinType::Float && |
| 4395 | BT->getKind() != BuiltinType::Double && |
| 4396 | BT->getKind() != BuiltinType::LongDouble) |
| 4397 | return false; |
| 4398 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4399 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4400 | unsigned VecSize = Context.getTypeSize(VT); |
| 4401 | if (VecSize != 64 && VecSize != 128) |
| 4402 | return false; |
| 4403 | } else { |
| 4404 | return false; |
| 4405 | } |
| 4406 | |
| 4407 | // The base type must be the same for all members. Vector types of the |
| 4408 | // same total size are treated as being equivalent here. |
| 4409 | const Type *TyPtr = Ty.getTypePtr(); |
| 4410 | if (!Base) |
| 4411 | Base = TyPtr; |
Oliver Stannard | 5e8558f | 2014-02-07 11:25:57 +0000 | [diff] [blame] | 4412 | |
| 4413 | if (Base != TyPtr) { |
| 4414 | // Homogeneous aggregates are defined as containing members with the |
| 4415 | // same machine type. There are two cases in which two members have |
| 4416 | // different TypePtrs but the same machine type: |
| 4417 | |
| 4418 | // 1) Vectors of the same length, regardless of the type and number |
| 4419 | // of their members. |
| 4420 | const bool SameLengthVectors = Base->isVectorType() && TyPtr->isVectorType() |
| 4421 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4422 | |
| 4423 | // 2) In the 32-bit AAPCS, `double' and `long double' have the same |
| 4424 | // machine type. This is not the case for the 64-bit AAPCS. |
| 4425 | const bool SameSizeDoubles = |
| 4426 | ( ( Base->isSpecificBuiltinType(BuiltinType::Double) |
| 4427 | && TyPtr->isSpecificBuiltinType(BuiltinType::LongDouble)) |
| 4428 | || ( Base->isSpecificBuiltinType(BuiltinType::LongDouble) |
| 4429 | && TyPtr->isSpecificBuiltinType(BuiltinType::Double))) |
| 4430 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4431 | |
| 4432 | if (!SameLengthVectors && !SameSizeDoubles) |
| 4433 | return false; |
| 4434 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 4438 | if (HAMembers) |
| 4439 | *HAMembers = Members; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4440 | |
| 4441 | return (Members > 0 && Members <= 4); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4444 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 4445 | /// number of VFP registers (unit is S register) requested. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4446 | void ARMABIInfo::markAllocatedVFPs(unsigned Alignment, |
| 4447 | unsigned NumRequired) const { |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4448 | // Early Exit. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4449 | if (AllocatedVFPs >= 16) { |
| 4450 | // We use AllocatedVFP > 16 to signal that some CPRCs were allocated on |
| 4451 | // the stack. |
| 4452 | AllocatedVFPs = 17; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4453 | return; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4454 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4455 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 4456 | // VFP registers of the appropriate type unallocated then the argument is |
| 4457 | // allocated to the lowest-numbered sequence of such registers. |
| 4458 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 4459 | bool FoundSlot = true; |
| 4460 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4461 | if (J >= 16 || VFPRegs[J]) { |
| 4462 | FoundSlot = false; |
| 4463 | break; |
| 4464 | } |
| 4465 | if (FoundSlot) { |
| 4466 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4467 | VFPRegs[J] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4468 | AllocatedVFPs += NumRequired; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4469 | return; |
| 4470 | } |
| 4471 | } |
| 4472 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 4473 | // unallocated are marked as unavailable. |
| 4474 | for (unsigned I = 0; I < 16; I++) |
| 4475 | VFPRegs[I] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4476 | AllocatedVFPs = 17; // We do not have enough VFP registers. |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4479 | /// Update AllocatedGPRs to record the number of general purpose registers |
| 4480 | /// which have been allocated. It is valid for AllocatedGPRs to go above 4, |
| 4481 | /// this represents arguments being stored on the stack. |
| 4482 | void ARMABIInfo::markAllocatedGPRs(unsigned Alignment, |
Oliver Stannard | 3f32b9b | 2014-06-27 13:59:27 +0000 | [diff] [blame] | 4483 | unsigned NumRequired) const { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4484 | assert((Alignment == 1 || Alignment == 2) && "Alignment must be 4 or 8 bytes"); |
| 4485 | |
| 4486 | if (Alignment == 2 && AllocatedGPRs & 0x1) |
| 4487 | AllocatedGPRs += 1; |
| 4488 | |
| 4489 | AllocatedGPRs += NumRequired; |
| 4490 | } |
| 4491 | |
| 4492 | void ARMABIInfo::resetAllocatedRegs(void) const { |
| 4493 | AllocatedGPRs = 0; |
| 4494 | AllocatedVFPs = 0; |
| 4495 | for (unsigned i = 0; i < NumVFPs; ++i) |
| 4496 | VFPRegs[i] = 0; |
| 4497 | } |
| 4498 | |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4499 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4500 | bool &IsCPRC) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4501 | // We update number of allocated VFPs according to |
| 4502 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4503 | // A single-precision floating-point type (including promoted |
| 4504 | // half-precision types); A double-precision floating-point type; |
| 4505 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4506 | // with a Base Type of a single- or double-precision floating-point type, |
| 4507 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4508 | // to four Elements. |
| 4509 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4510 | const bool isAAPCS_VFP = |
| 4511 | getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic; |
| 4512 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4513 | // Handle illegal vector types here. |
| 4514 | if (isIllegalVectorType(Ty)) { |
| 4515 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4516 | if (Size <= 32) { |
| 4517 | llvm::Type *ResType = |
| 4518 | llvm::Type::getInt32Ty(getVMContext()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4519 | markAllocatedGPRs(1, 1); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4520 | return ABIArgInfo::getDirect(ResType, 0, nullptr, !isAAPCS_VFP); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4521 | } |
| 4522 | if (Size == 64) { |
| 4523 | llvm::Type *ResType = llvm::VectorType::get( |
| 4524 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4525 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic){ |
| 4526 | markAllocatedGPRs(2, 2); |
| 4527 | } else { |
| 4528 | markAllocatedVFPs(2, 2); |
| 4529 | IsCPRC = true; |
| 4530 | } |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4531 | return ABIArgInfo::getDirect(ResType, 0, nullptr, !isAAPCS_VFP); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4532 | } |
| 4533 | if (Size == 128) { |
| 4534 | llvm::Type *ResType = llvm::VectorType::get( |
| 4535 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4536 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic) { |
| 4537 | markAllocatedGPRs(2, 4); |
| 4538 | } else { |
| 4539 | markAllocatedVFPs(4, 4); |
| 4540 | IsCPRC = true; |
| 4541 | } |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4542 | return ABIArgInfo::getDirect(ResType, 0, nullptr, !isAAPCS_VFP); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4543 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4544 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4545 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4546 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4547 | // Update VFPRegs for legal vector types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4548 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4549 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4550 | uint64_t Size = getContext().getTypeSize(VT); |
| 4551 | // Size of a legal vector should be power of 2 and above 64. |
| 4552 | markAllocatedVFPs(Size >= 128 ? 4 : 2, Size / 32); |
| 4553 | IsCPRC = true; |
| 4554 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4555 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4556 | // Update VFPRegs for floating point types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4557 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4558 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4559 | if (BT->getKind() == BuiltinType::Half || |
| 4560 | BT->getKind() == BuiltinType::Float) { |
| 4561 | markAllocatedVFPs(1, 1); |
| 4562 | IsCPRC = true; |
| 4563 | } |
| 4564 | if (BT->getKind() == BuiltinType::Double || |
| 4565 | BT->getKind() == BuiltinType::LongDouble) { |
| 4566 | markAllocatedVFPs(2, 2); |
| 4567 | IsCPRC = true; |
| 4568 | } |
| 4569 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4570 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4571 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4572 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4573 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4574 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4575 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4576 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4577 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4578 | unsigned Size = getContext().getTypeSize(Ty); |
| 4579 | if (!IsCPRC) |
| 4580 | markAllocatedGPRs(Size > 32 ? 2 : 1, (Size + 31) / 32); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4581 | return (Ty->isPromotableIntegerType() |
| 4582 | ? ABIArgInfo::getExtend() |
| 4583 | : ABIArgInfo::getDirect(nullptr, 0, nullptr, !isAAPCS_VFP)); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4584 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4585 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4586 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 4587 | markAllocatedGPRs(1, 1); |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4588 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4589 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4590 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4591 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4592 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4593 | return ABIArgInfo::getIgnore(); |
| 4594 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4595 | if (isAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4596 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4597 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4598 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4599 | uint64_t Members = 0; |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4600 | if (isARMHomogeneousAggregate(Ty, Base, getContext(), false, &Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4601 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4602 | // Base can be a floating-point or a vector. |
| 4603 | if (Base->isVectorType()) { |
| 4604 | // ElementSize is in number of floats. |
| 4605 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4606 | markAllocatedVFPs(ElementSize, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4607 | Members * ElementSize); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4608 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4609 | markAllocatedVFPs(1, Members); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4610 | else { |
| 4611 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 4612 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4613 | markAllocatedVFPs(2, Members * 2); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4614 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4615 | IsCPRC = true; |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4616 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, !isAAPCS_VFP); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4617 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4618 | } |
| 4619 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4620 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4621 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4622 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4623 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4624 | uint64_t ABIAlign = 4; |
| 4625 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4626 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4627 | getABIKind() == ARMABIInfo::AAPCS) |
| 4628 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4629 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Oliver Stannard | 3f32b9b | 2014-06-27 13:59:27 +0000 | [diff] [blame] | 4630 | // Update Allocated GPRs. Since this is only used when the size of the |
| 4631 | // argument is greater than 64 bytes, this will always use up any available |
| 4632 | // registers (of which there are 4). We also don't care about getting the |
| 4633 | // alignment right, because general-purpose registers cannot be back-filled. |
| 4634 | markAllocatedGPRs(1, 4); |
Oliver Stannard | 7c3c09e | 2014-03-12 14:02:50 +0000 | [diff] [blame] | 4635 | return ABIArgInfo::getIndirect(TyAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4636 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4639 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4640 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4641 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4642 | // FIXME: Try to match the types of the arguments more accurately where |
| 4643 | // we can. |
| 4644 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4645 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4646 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4647 | markAllocatedGPRs(1, SizeRegs); |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4648 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4649 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4650 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4651 | markAllocatedGPRs(2, SizeRegs * 2); |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4652 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4653 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4654 | llvm::Type *STy = |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 4655 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4656 | return ABIArgInfo::getDirect(STy, 0, nullptr, !isAAPCS_VFP); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4659 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4660 | llvm::LLVMContext &VMContext) { |
| 4661 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4662 | // is called integer-like if its size is less than or equal to one word, and |
| 4663 | // the offset of each of its addressable sub-fields is zero. |
| 4664 | |
| 4665 | uint64_t Size = Context.getTypeSize(Ty); |
| 4666 | |
| 4667 | // Check that the type fits in a word. |
| 4668 | if (Size > 32) |
| 4669 | return false; |
| 4670 | |
| 4671 | // FIXME: Handle vector types! |
| 4672 | if (Ty->isVectorType()) |
| 4673 | return false; |
| 4674 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4675 | // Float types are never treated as "integer like". |
| 4676 | if (Ty->isRealFloatingType()) |
| 4677 | return false; |
| 4678 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4679 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4680 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4681 | return true; |
| 4682 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4683 | // Small complex integer types are "integer like". |
| 4684 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4685 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4686 | |
| 4687 | // Single element and zero sized arrays should be allowed, by the definition |
| 4688 | // above, but they are not. |
| 4689 | |
| 4690 | // Otherwise, it must be a record type. |
| 4691 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4692 | if (!RT) return false; |
| 4693 | |
| 4694 | // Ignore records with flexible arrays. |
| 4695 | const RecordDecl *RD = RT->getDecl(); |
| 4696 | if (RD->hasFlexibleArrayMember()) |
| 4697 | return false; |
| 4698 | |
| 4699 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4700 | // like". |
| 4701 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4702 | |
| 4703 | bool HadField = false; |
| 4704 | unsigned idx = 0; |
| 4705 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4706 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4707 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4708 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4709 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4710 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4711 | // struct { int : 0; int x } |
| 4712 | // is non-integer like according to gcc. |
| 4713 | if (FD->isBitField()) { |
| 4714 | if (!RD->isUnion()) |
| 4715 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4716 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4717 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4718 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4719 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4720 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4723 | // Check if this field is at offset 0. |
| 4724 | if (Layout.getFieldOffset(idx) != 0) |
| 4725 | return false; |
| 4726 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4727 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4728 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4729 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4730 | // Only allow at most one field in a structure. This doesn't match the |
| 4731 | // wording above, but follows gcc in situations with a field following an |
| 4732 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4733 | if (!RD->isUnion()) { |
| 4734 | if (HadField) |
| 4735 | return false; |
| 4736 | |
| 4737 | HadField = true; |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | return true; |
| 4742 | } |
| 4743 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4744 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4745 | bool isVariadic) const { |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4746 | const bool isAAPCS_VFP = |
| 4747 | getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic; |
| 4748 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4749 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4750 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4751 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4752 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4753 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
| 4754 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4755 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4756 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4757 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4758 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4759 | // Treat an enum type as its underlying type. |
| 4760 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4761 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4762 | |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4763 | return (RetTy->isPromotableIntegerType() |
| 4764 | ? ABIArgInfo::getExtend() |
| 4765 | : ABIArgInfo::getDirect(nullptr, 0, nullptr, !isAAPCS_VFP)); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4766 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4767 | |
| 4768 | // Are we following APCS? |
| 4769 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4770 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4771 | return ABIArgInfo::getIgnore(); |
| 4772 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4773 | // Complex types are all returned as packed integers. |
| 4774 | // |
| 4775 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4776 | // correctly. |
| 4777 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4778 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 4779 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4780 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4781 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4782 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4783 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4784 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4785 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4786 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4787 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4788 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4789 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | // Otherwise return in memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4793 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4794 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4795 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4796 | |
| 4797 | // Otherwise this is an AAPCS variant. |
| 4798 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4799 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4800 | return ABIArgInfo::getIgnore(); |
| 4801 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4802 | // Check for homogeneous aggregates with AAPCS-VFP. |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4803 | if (getABIKind() == AAPCS_VFP && !isVariadic) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4804 | const Type *Base = nullptr; |
Oliver Stannard | ed8ecc8 | 2014-08-27 16:31:57 +0000 | [diff] [blame] | 4805 | if (isARMHomogeneousAggregate(RetTy, Base, getContext(), false)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4806 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4807 | // Homogeneous Aggregates are returned directly. |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4808 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, !isAAPCS_VFP); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4809 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4812 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4813 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4814 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4815 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4816 | if (getDataLayout().isBigEndian()) |
| 4817 | // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4818 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()), 0, |
| 4819 | nullptr, !isAAPCS_VFP); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4820 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4821 | // Return in the smallest viable integer type. |
| 4822 | if (Size <= 8) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4823 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext()), 0, |
| 4824 | nullptr, !isAAPCS_VFP); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4825 | if (Size <= 16) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 4826 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()), 0, |
| 4827 | nullptr, !isAAPCS_VFP); |
| 4828 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()), 0, |
| 4829 | nullptr, !isAAPCS_VFP); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4832 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4833 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4836 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4837 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4838 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4839 | // Check whether VT is legal. |
| 4840 | unsigned NumElements = VT->getNumElements(); |
| 4841 | uint64_t Size = getContext().getTypeSize(VT); |
| 4842 | // NumElements should be power of 2. |
| 4843 | if ((NumElements & (NumElements - 1)) != 0) |
| 4844 | return true; |
| 4845 | // Size should be greater than 32 bits. |
| 4846 | return Size <= 32; |
| 4847 | } |
| 4848 | return false; |
| 4849 | } |
| 4850 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4851 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4852 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4853 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4854 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4855 | |
| 4856 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4857 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4858 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4859 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4860 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4861 | // These are ignored for parameter passing purposes. |
| 4862 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4863 | return Builder.CreateBitCast(Addr, PTy); |
| 4864 | } |
| 4865 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4866 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4867 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4868 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4869 | |
| 4870 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4871 | // 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] | 4872 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4873 | getABIKind() == ARMABIInfo::AAPCS) |
| 4874 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4875 | else |
| 4876 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4877 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4878 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4879 | IsIndirect = true; |
| 4880 | Size = 4; |
| 4881 | TyAlign = 4; |
| 4882 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4883 | |
| 4884 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4885 | if (TyAlign > 4) { |
| 4886 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 4887 | "Alignment is not power of 2!"); |
| 4888 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 4889 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 4890 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4891 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4892 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4893 | |
| 4894 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4895 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4896 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4897 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4898 | "ap.next"); |
| 4899 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4900 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4901 | if (IsIndirect) |
| 4902 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4903 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4904 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 4905 | // may not be correctly aligned for the vector type. We create an aligned |
| 4906 | // temporary space and copy the content over from ap.cur to the temporary |
| 4907 | // space. This is necessary if the natural alignment of the type is greater |
| 4908 | // than the ABI alignment. |
| 4909 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 4910 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 4911 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 4912 | "var.align"); |
| 4913 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 4914 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 4915 | Builder.CreateMemCpy(Dst, Src, |
| 4916 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 4917 | TyAlign, false); |
| 4918 | Addr = AlignedTemp; //The content is in aligned location. |
| 4919 | } |
| 4920 | llvm::Type *PTy = |
| 4921 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4922 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4923 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4924 | return AddrTyped; |
| 4925 | } |
| 4926 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4927 | namespace { |
| 4928 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4929 | class NaClARMABIInfo : public ABIInfo { |
| 4930 | public: |
| 4931 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4932 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4933 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4934 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4935 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4936 | private: |
| 4937 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 4938 | ARMABIInfo NInfo; // Used for everything else. |
| 4939 | }; |
| 4940 | |
| 4941 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4942 | public: |
| 4943 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4944 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 4945 | }; |
| 4946 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4949 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4950 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 4951 | PInfo.computeInfo(FI); |
| 4952 | else |
| 4953 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 4954 | } |
| 4955 | |
| 4956 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4957 | CodeGenFunction &CGF) const { |
| 4958 | // Always use the native convention; calling pnacl-style varargs functions |
| 4959 | // is unsupported. |
| 4960 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 4961 | } |
| 4962 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4963 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4964 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4965 | //===----------------------------------------------------------------------===// |
| 4966 | |
| 4967 | namespace { |
| 4968 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4969 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4970 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4971 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4972 | |
| 4973 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4974 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4975 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4976 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4977 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4978 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4979 | }; |
| 4980 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4981 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4982 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4983 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4984 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4985 | |
| 4986 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4987 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4988 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4989 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 4990 | // resulting MDNode to the nvvm.annotations MDNode. |
| 4991 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4992 | }; |
| 4993 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4994 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4995 | if (RetTy->isVoidType()) |
| 4996 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4997 | |
| 4998 | // note: this is different from default ABI |
| 4999 | if (!RetTy->isScalarType()) |
| 5000 | return ABIArgInfo::getDirect(); |
| 5001 | |
| 5002 | // Treat an enum type as its underlying type. |
| 5003 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5004 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5005 | |
| 5006 | return (RetTy->isPromotableIntegerType() ? |
| 5007 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5010 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5011 | // Treat an enum type as its underlying type. |
| 5012 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5013 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5014 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 5015 | return (Ty->isPromotableIntegerType() ? |
| 5016 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5019 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5020 | if (!getCXXABI().classifyReturnType(FI)) |
| 5021 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5022 | for (auto &I : FI.arguments()) |
| 5023 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5024 | |
| 5025 | // Always honor user-specified calling convention. |
| 5026 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5027 | return; |
| 5028 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5029 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 5030 | } |
| 5031 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5032 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5033 | CodeGenFunction &CFG) const { |
| 5034 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 5037 | void NVPTXTargetCodeGenInfo:: |
| 5038 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5039 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5040 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5041 | if (!FD) return; |
| 5042 | |
| 5043 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5044 | |
| 5045 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5046 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5047 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5048 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5049 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5050 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5051 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5052 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5053 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5054 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5055 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5056 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5057 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5058 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5059 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5060 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 5061 | // __global__ functions cannot be called from the device, we do not |
| 5062 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5063 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 5064 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 5065 | addNVVMMetadata(F, "kernel", 1); |
| 5066 | } |
| 5067 | if (FD->hasAttr<CUDALaunchBoundsAttr>()) { |
| 5068 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
| 5069 | addNVVMMetadata(F, "maxntidx", |
| 5070 | FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads()); |
| 5071 | // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a |
| 5072 | // zero value from getMinBlocks either means it was not specified in |
| 5073 | // __launch_bounds__ or the user specified a 0 value. In both cases, we |
| 5074 | // don't have to add a PTX directive. |
| 5075 | int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks(); |
| 5076 | if (MinCTASM > 0) { |
| 5077 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 5078 | addNVVMMetadata(F, "minctasm", MinCTASM); |
| 5079 | } |
| 5080 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 5081 | } |
| 5082 | } |
| 5083 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 5084 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 5085 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5086 | llvm::Module *M = F->getParent(); |
| 5087 | llvm::LLVMContext &Ctx = M->getContext(); |
| 5088 | |
| 5089 | // Get "nvvm.annotations" metadata node |
| 5090 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 5091 | |
Eli Bendersky | e1627b4 | 2014-04-15 17:19:26 +0000 | [diff] [blame] | 5092 | llvm::Value *MDVals[] = { |
| 5093 | F, llvm::MDString::get(Ctx, Name), |
| 5094 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand)}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 5095 | // Append metadata to nvvm.annotations |
| 5096 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 5097 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
| 5100 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5101 | // SystemZ ABI Implementation |
| 5102 | //===----------------------------------------------------------------------===// |
| 5103 | |
| 5104 | namespace { |
| 5105 | |
| 5106 | class SystemZABIInfo : public ABIInfo { |
| 5107 | public: |
| 5108 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5109 | |
| 5110 | bool isPromotableIntegerType(QualType Ty) const; |
| 5111 | bool isCompoundType(QualType Ty) const; |
| 5112 | bool isFPArgumentType(QualType Ty) const; |
| 5113 | |
| 5114 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5115 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 5116 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5117 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5118 | if (!getCXXABI().classifyReturnType(FI)) |
| 5119 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5120 | for (auto &I : FI.arguments()) |
| 5121 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5122 | } |
| 5123 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5124 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5125 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5126 | }; |
| 5127 | |
| 5128 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5129 | public: |
| 5130 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5131 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 5132 | }; |
| 5133 | |
| 5134 | } |
| 5135 | |
| 5136 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5137 | // Treat an enum type as its underlying type. |
| 5138 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5139 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5140 | |
| 5141 | // Promotable integer types are required to be promoted by the ABI. |
| 5142 | if (Ty->isPromotableIntegerType()) |
| 5143 | return true; |
| 5144 | |
| 5145 | // 32-bit values must also be promoted. |
| 5146 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5147 | switch (BT->getKind()) { |
| 5148 | case BuiltinType::Int: |
| 5149 | case BuiltinType::UInt: |
| 5150 | return true; |
| 5151 | default: |
| 5152 | return false; |
| 5153 | } |
| 5154 | return false; |
| 5155 | } |
| 5156 | |
| 5157 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 5158 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 5159 | } |
| 5160 | |
| 5161 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5162 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5163 | switch (BT->getKind()) { |
| 5164 | case BuiltinType::Float: |
| 5165 | case BuiltinType::Double: |
| 5166 | return true; |
| 5167 | default: |
| 5168 | return false; |
| 5169 | } |
| 5170 | |
| 5171 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5172 | const RecordDecl *RD = RT->getDecl(); |
| 5173 | bool Found = false; |
| 5174 | |
| 5175 | // If this is a C++ record, check the bases first. |
| 5176 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5177 | for (const auto &I : CXXRD->bases()) { |
| 5178 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5179 | |
| 5180 | // Empty bases don't affect things either way. |
| 5181 | if (isEmptyRecord(getContext(), Base, true)) |
| 5182 | continue; |
| 5183 | |
| 5184 | if (Found) |
| 5185 | return false; |
| 5186 | Found = isFPArgumentType(Base); |
| 5187 | if (!Found) |
| 5188 | return false; |
| 5189 | } |
| 5190 | |
| 5191 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5192 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5193 | // Empty bitfields don't affect things either way. |
| 5194 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5195 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 5196 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5197 | return true; |
| 5198 | |
| 5199 | // Unlike isSingleElementStruct(), arrays do not count. |
| 5200 | // Nested isFPArgumentType structures still do though. |
| 5201 | if (Found) |
| 5202 | return false; |
| 5203 | Found = isFPArgumentType(FD->getType()); |
| 5204 | if (!Found) |
| 5205 | return false; |
| 5206 | } |
| 5207 | |
| 5208 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5209 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 5210 | return Found; |
| 5211 | } |
| 5212 | |
| 5213 | return false; |
| 5214 | } |
| 5215 | |
| 5216 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5217 | CodeGenFunction &CGF) const { |
| 5218 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5219 | // struct { |
| 5220 | // i64 __gpr; |
| 5221 | // i64 __fpr; |
| 5222 | // i8 *__overflow_arg_area; |
| 5223 | // i8 *__reg_save_area; |
| 5224 | // }; |
| 5225 | |
| 5226 | // Every argument occupies 8 bytes and is passed by preference in either |
| 5227 | // GPRs or FPRs. |
| 5228 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 5229 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 5230 | bool InFPRs = isFPArgumentType(Ty); |
| 5231 | |
| 5232 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 5233 | bool IsIndirect = AI.isIndirect(); |
| 5234 | unsigned UnpaddedBitSize; |
| 5235 | if (IsIndirect) { |
| 5236 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5237 | UnpaddedBitSize = 64; |
| 5238 | } else |
| 5239 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 5240 | unsigned PaddedBitSize = 64; |
| 5241 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5242 | |
| 5243 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5244 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5245 | |
| 5246 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5247 | if (InFPRs) { |
| 5248 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5249 | RegCountField = 1; // __fpr |
| 5250 | RegSaveIndex = 16; // save offset for f0 |
| 5251 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5252 | } else { |
| 5253 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5254 | RegCountField = 0; // __gpr |
| 5255 | RegSaveIndex = 2; // save offset for r2 |
| 5256 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5257 | } |
| 5258 | |
| 5259 | llvm::Value *RegCountPtr = |
| 5260 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 5261 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 5262 | llvm::Type *IndexTy = RegCount->getType(); |
| 5263 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5264 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5265 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5266 | |
| 5267 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5268 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5269 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5270 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5271 | |
| 5272 | // Emit code to load the value if it was passed in registers. |
| 5273 | CGF.EmitBlock(InRegBlock); |
| 5274 | |
| 5275 | // Work out the address of an argument register. |
| 5276 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5277 | llvm::Value *ScaledRegCount = |
| 5278 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5279 | llvm::Value *RegBase = |
| 5280 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5281 | llvm::Value *RegOffset = |
| 5282 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5283 | llvm::Value *RegSaveAreaPtr = |
| 5284 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 5285 | llvm::Value *RegSaveArea = |
| 5286 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5287 | llvm::Value *RawRegAddr = |
| 5288 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5289 | llvm::Value *RegAddr = |
| 5290 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5291 | |
| 5292 | // Update the register count |
| 5293 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5294 | llvm::Value *NewRegCount = |
| 5295 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5296 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5297 | CGF.EmitBranch(ContBlock); |
| 5298 | |
| 5299 | // Emit code to load the value if it was passed in memory. |
| 5300 | CGF.EmitBlock(InMemBlock); |
| 5301 | |
| 5302 | // Work out the address of a stack argument. |
| 5303 | llvm::Value *OverflowArgAreaPtr = |
| 5304 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 5305 | llvm::Value *OverflowArgArea = |
| 5306 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5307 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5308 | llvm::Value *RawMemAddr = |
| 5309 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5310 | llvm::Value *MemAddr = |
| 5311 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5312 | |
| 5313 | // Update overflow_arg_area_ptr pointer |
| 5314 | llvm::Value *NewOverflowArgArea = |
| 5315 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5316 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5317 | CGF.EmitBranch(ContBlock); |
| 5318 | |
| 5319 | // Return the appropriate result. |
| 5320 | CGF.EmitBlock(ContBlock); |
| 5321 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5322 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5323 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5324 | |
| 5325 | if (IsIndirect) |
| 5326 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5327 | |
| 5328 | return ResAddr; |
| 5329 | } |
| 5330 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5331 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5332 | if (RetTy->isVoidType()) |
| 5333 | return ABIArgInfo::getIgnore(); |
| 5334 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5335 | return ABIArgInfo::getIndirect(0); |
| 5336 | return (isPromotableIntegerType(RetTy) ? |
| 5337 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5338 | } |
| 5339 | |
| 5340 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5341 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5342 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5343 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5344 | |
| 5345 | // Integers and enums are extended to full register width. |
| 5346 | if (isPromotableIntegerType(Ty)) |
| 5347 | return ABIArgInfo::getExtend(); |
| 5348 | |
| 5349 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 5350 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5351 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5352 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5353 | |
| 5354 | // Handle small structures. |
| 5355 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5356 | // Structures with flexible arrays have variable length, so really |
| 5357 | // fail the size test above. |
| 5358 | const RecordDecl *RD = RT->getDecl(); |
| 5359 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5360 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5361 | |
| 5362 | // The structure is passed as an unextended integer, a float, or a double. |
| 5363 | llvm::Type *PassTy; |
| 5364 | if (isFPArgumentType(Ty)) { |
| 5365 | assert(Size == 32 || Size == 64); |
| 5366 | if (Size == 32) |
| 5367 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5368 | else |
| 5369 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5370 | } else |
| 5371 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5372 | return ABIArgInfo::getDirect(PassTy); |
| 5373 | } |
| 5374 | |
| 5375 | // Non-structure compounds are passed indirectly. |
| 5376 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5377 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5378 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5379 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5383 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5384 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5385 | |
| 5386 | namespace { |
| 5387 | |
| 5388 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5389 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5390 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5391 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5392 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5393 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5394 | }; |
| 5395 | |
| 5396 | } |
| 5397 | |
| 5398 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5399 | llvm::GlobalValue *GV, |
| 5400 | CodeGen::CodeGenModule &M) const { |
| 5401 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5402 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5403 | // Handle 'interrupt' attribute: |
| 5404 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5405 | |
| 5406 | // Step 1: Set ISR calling convention. |
| 5407 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5408 | |
| 5409 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5410 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5411 | |
| 5412 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5413 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5414 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5415 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5416 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5417 | } |
| 5418 | } |
| 5419 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5420 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5421 | // MIPS ABI Implementation. This works for both little-endian and |
| 5422 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5423 | //===----------------------------------------------------------------------===// |
| 5424 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5425 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5426 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5427 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5428 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5429 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5430 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5431 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5432 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5433 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5434 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5435 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5436 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5437 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5438 | |
| 5439 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5440 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5441 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5442 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5443 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5444 | }; |
| 5445 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5446 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5447 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5448 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5449 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5450 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5451 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5452 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5453 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5454 | return 29; |
| 5455 | } |
| 5456 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5457 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5458 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5459 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5460 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5461 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5462 | if (FD->hasAttr<Mips16Attr>()) { |
| 5463 | Fn->addFnAttr("mips16"); |
| 5464 | } |
| 5465 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5466 | Fn->addFnAttr("nomips16"); |
| 5467 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5468 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5469 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5470 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5471 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5472 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5473 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5474 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5475 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5476 | }; |
| 5477 | } |
| 5478 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5479 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5480 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5481 | llvm::IntegerType *IntTy = |
| 5482 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5483 | |
| 5484 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5485 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5486 | ArgList.push_back(IntTy); |
| 5487 | |
| 5488 | // If necessary, add one more integer type to ArgList. |
| 5489 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5490 | |
| 5491 | if (R) |
| 5492 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5493 | } |
| 5494 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5495 | // In N32/64, an aligned double precision floating point field is passed in |
| 5496 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5497 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5498 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5499 | |
| 5500 | if (IsO32) { |
| 5501 | CoerceToIntArgs(TySize, ArgList); |
| 5502 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5503 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5504 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5505 | if (Ty->isComplexType()) |
| 5506 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5507 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5508 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5509 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5510 | // Unions/vectors are passed in integer registers. |
| 5511 | if (!RT || !RT->isStructureOrClassType()) { |
| 5512 | CoerceToIntArgs(TySize, ArgList); |
| 5513 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5514 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5515 | |
| 5516 | const RecordDecl *RD = RT->getDecl(); |
| 5517 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5518 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5519 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5520 | uint64_t LastOffset = 0; |
| 5521 | unsigned idx = 0; |
| 5522 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5523 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5524 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5525 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5526 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5527 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5528 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5529 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5530 | |
| 5531 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5532 | continue; |
| 5533 | |
| 5534 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5535 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5536 | continue; |
| 5537 | |
| 5538 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5539 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5540 | ArgList.push_back(I64); |
| 5541 | |
| 5542 | // Add double type. |
| 5543 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5544 | LastOffset = Offset + 64; |
| 5545 | } |
| 5546 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5547 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5548 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5549 | |
| 5550 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5551 | } |
| 5552 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5553 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5554 | uint64_t Offset) const { |
| 5555 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5556 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5557 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5558 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5559 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5560 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5561 | ABIArgInfo |
| 5562 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5563 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5564 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5565 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5566 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5567 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5568 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5569 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5570 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5571 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5572 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5573 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5574 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5575 | return ABIArgInfo::getIgnore(); |
| 5576 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5577 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5578 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5579 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5580 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5581 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5582 | // If we have reached here, aggregates are passed directly by coercing to |
| 5583 | // another structure type. Padding is inserted if the offset of the |
| 5584 | // aggregate is unaligned. |
| 5585 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5586 | getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | // Treat an enum type as its underlying type. |
| 5590 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5591 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5592 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5593 | if (Ty->isPromotableIntegerType()) |
| 5594 | return ABIArgInfo::getExtend(); |
| 5595 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5596 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5597 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5598 | } |
| 5599 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5600 | llvm::Type* |
| 5601 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5602 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5603 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5604 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5605 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5606 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5607 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5608 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5609 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5610 | // N32/64 returns struct/classes in floating point registers if the |
| 5611 | // following conditions are met: |
| 5612 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5613 | // 2. The struct/class has one or two fields all of which are floating |
| 5614 | // point types. |
| 5615 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5616 | // |
| 5617 | // Any other composite results are returned in integer registers. |
| 5618 | // |
| 5619 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5620 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5621 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5622 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5623 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5624 | if (!BT || !BT->isFloatingPoint()) |
| 5625 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5626 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5627 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | if (b == e) |
| 5631 | return llvm::StructType::get(getVMContext(), RTList, |
| 5632 | RD->hasAttr<PackedAttr>()); |
| 5633 | |
| 5634 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5635 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5636 | } |
| 5637 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5638 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5639 | return llvm::StructType::get(getVMContext(), RTList); |
| 5640 | } |
| 5641 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5642 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5643 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5644 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 5645 | if (RetTy->isVoidType()) |
| 5646 | return ABIArgInfo::getIgnore(); |
| 5647 | |
| 5648 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 5649 | // However, N32/N64 ignores zero sized return values. |
| 5650 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5651 | return ABIArgInfo::getIgnore(); |
| 5652 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5653 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5654 | if (Size <= 128) { |
| 5655 | if (RetTy->isAnyComplexType()) |
| 5656 | return ABIArgInfo::getDirect(); |
| 5657 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5658 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 5659 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 5660 | if (!IsO32 || |
| 5661 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 5662 | ABIArgInfo ArgInfo = |
| 5663 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5664 | ArgInfo.setInReg(true); |
| 5665 | return ArgInfo; |
| 5666 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5667 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5668 | |
| 5669 | return ABIArgInfo::getIndirect(0); |
| 5670 | } |
| 5671 | |
| 5672 | // Treat an enum type as its underlying type. |
| 5673 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5674 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5675 | |
| 5676 | return (RetTy->isPromotableIntegerType() ? |
| 5677 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5678 | } |
| 5679 | |
| 5680 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5681 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5682 | if (!getCXXABI().classifyReturnType(FI)) |
| 5683 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5684 | |
| 5685 | // 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] | 5686 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5687 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5688 | for (auto &I : FI.arguments()) |
| 5689 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
| 5692 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5693 | CodeGenFunction &CGF) const { |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5694 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5695 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 5696 | |
| 5697 | CGBuilderTy &Builder = CGF.Builder; |
| 5698 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5699 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Daniel Sanders | 8d36a61 | 2014-09-22 13:27:06 +0000 | [diff] [blame] | 5700 | int64_t TypeAlign = |
| 5701 | std::min(getContext().getTypeAlign(Ty) / 8, StackAlignInBytes); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 5702 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5703 | llvm::Value *AddrTyped; |
| 5704 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
| 5705 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
| 5706 | |
| 5707 | if (TypeAlign > MinABIStackAlignInBytes) { |
| 5708 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5709 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5710 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5711 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
| 5712 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5713 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5714 | } |
| 5715 | else |
| 5716 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5717 | |
| 5718 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
| 5719 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
| 5720 | uint64_t Offset = |
| 5721 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 5722 | llvm::Value *NextAddr = |
| 5723 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
| 5724 | "ap.next"); |
| 5725 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5726 | |
| 5727 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5730 | bool |
| 5731 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5732 | llvm::Value *Address) const { |
| 5733 | // This information comes from gcc's implementation, which seems to |
| 5734 | // as canonical as it gets. |
| 5735 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5736 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5737 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5738 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5739 | |
| 5740 | // 0-31 are the general purpose registers, $0 - $31. |
| 5741 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5742 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5743 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5744 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5745 | |
| 5746 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5747 | // They are one bit wide and ignored here. |
| 5748 | |
| 5749 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5750 | // (coprocessor 1 is the FP unit) |
| 5751 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5752 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5753 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5754 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5755 | return false; |
| 5756 | } |
| 5757 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5758 | //===----------------------------------------------------------------------===// |
| 5759 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5760 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5761 | // handling. |
| 5762 | //===----------------------------------------------------------------------===// |
| 5763 | |
| 5764 | namespace { |
| 5765 | |
| 5766 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5767 | public: |
| 5768 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5769 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5770 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5771 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5772 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5773 | }; |
| 5774 | |
| 5775 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5776 | llvm::GlobalValue *GV, |
| 5777 | CodeGen::CodeGenModule &M) const { |
| 5778 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5779 | if (!FD) return; |
| 5780 | |
| 5781 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5782 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5783 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5784 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5785 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5786 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5787 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5788 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5789 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5790 | llvm::LLVMContext &Context = F->getContext(); |
| 5791 | llvm::NamedMDNode *OpenCLMetadata = |
| 5792 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5793 | |
| 5794 | SmallVector<llvm::Value*, 5> Operands; |
| 5795 | Operands.push_back(F); |
| 5796 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5797 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5798 | llvm::APInt(32, Attr->getXDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5799 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5800 | llvm::APInt(32, Attr->getYDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5801 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5802 | llvm::APInt(32, Attr->getZDim()))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5803 | |
| 5804 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5805 | // for implementing the work_group_size_hint attr later. Currently |
| 5806 | // always true as the hint is not yet implemented. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5807 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5808 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5809 | } |
| 5810 | } |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5815 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5816 | //===----------------------------------------------------------------------===// |
| 5817 | // Hexagon ABI Implementation |
| 5818 | //===----------------------------------------------------------------------===// |
| 5819 | |
| 5820 | namespace { |
| 5821 | |
| 5822 | class HexagonABIInfo : public ABIInfo { |
| 5823 | |
| 5824 | |
| 5825 | public: |
| 5826 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5827 | |
| 5828 | private: |
| 5829 | |
| 5830 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5831 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5832 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5833 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5834 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5835 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5836 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5837 | }; |
| 5838 | |
| 5839 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5840 | public: |
| 5841 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5842 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5843 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5844 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5845 | return 29; |
| 5846 | } |
| 5847 | }; |
| 5848 | |
| 5849 | } |
| 5850 | |
| 5851 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5852 | if (!getCXXABI().classifyReturnType(FI)) |
| 5853 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5854 | for (auto &I : FI.arguments()) |
| 5855 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5859 | if (!isAggregateTypeForABI(Ty)) { |
| 5860 | // Treat an enum type as its underlying type. |
| 5861 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5862 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5863 | |
| 5864 | return (Ty->isPromotableIntegerType() ? |
| 5865 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5866 | } |
| 5867 | |
| 5868 | // Ignore empty records. |
| 5869 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5870 | return ABIArgInfo::getIgnore(); |
| 5871 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5872 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5873 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5874 | |
| 5875 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5876 | if (Size > 64) |
| 5877 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5878 | // Pass in the smallest viable integer type. |
| 5879 | else if (Size > 32) |
| 5880 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5881 | else if (Size > 16) |
| 5882 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5883 | else if (Size > 8) |
| 5884 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5885 | else |
| 5886 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5887 | } |
| 5888 | |
| 5889 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5890 | if (RetTy->isVoidType()) |
| 5891 | return ABIArgInfo::getIgnore(); |
| 5892 | |
| 5893 | // Large vector types should be returned via memory. |
| 5894 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5895 | return ABIArgInfo::getIndirect(0); |
| 5896 | |
| 5897 | if (!isAggregateTypeForABI(RetTy)) { |
| 5898 | // Treat an enum type as its underlying type. |
| 5899 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5900 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5901 | |
| 5902 | return (RetTy->isPromotableIntegerType() ? |
| 5903 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5904 | } |
| 5905 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5906 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5907 | return ABIArgInfo::getIgnore(); |
| 5908 | |
| 5909 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5910 | // are returned indirectly. |
| 5911 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5912 | if (Size <= 64) { |
| 5913 | // Return in the smallest viable integer type. |
| 5914 | if (Size <= 8) |
| 5915 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5916 | if (Size <= 16) |
| 5917 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5918 | if (Size <= 32) |
| 5919 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5920 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5921 | } |
| 5922 | |
| 5923 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5924 | } |
| 5925 | |
| 5926 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5927 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5928 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5929 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5930 | |
| 5931 | CGBuilderTy &Builder = CGF.Builder; |
| 5932 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5933 | "ap"); |
| 5934 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5935 | llvm::Type *PTy = |
| 5936 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5937 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5938 | |
| 5939 | uint64_t Offset = |
| 5940 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5941 | llvm::Value *NextAddr = |
| 5942 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5943 | "ap.next"); |
| 5944 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5945 | |
| 5946 | return AddrTyped; |
| 5947 | } |
| 5948 | |
| 5949 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5950 | //===----------------------------------------------------------------------===// |
| 5951 | // SPARC v9 ABI Implementation. |
| 5952 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 5953 | // |
| 5954 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 5955 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 5956 | // the array, structs larger than 16 bytes are passed indirectly. |
| 5957 | // |
| 5958 | // One case requires special care: |
| 5959 | // |
| 5960 | // struct mixed { |
| 5961 | // int i; |
| 5962 | // float f; |
| 5963 | // }; |
| 5964 | // |
| 5965 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 5966 | // parameter array, but the int is passed in an integer register, and the float |
| 5967 | // is passed in a floating point register. This is represented as two arguments |
| 5968 | // with the LLVM IR inreg attribute: |
| 5969 | // |
| 5970 | // declare void f(i32 inreg %i, float inreg %f) |
| 5971 | // |
| 5972 | // The code generator will only allocate 4 bytes from the parameter array for |
| 5973 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 5974 | // bytes. |
| 5975 | // |
| 5976 | namespace { |
| 5977 | class SparcV9ABIInfo : public ABIInfo { |
| 5978 | public: |
| 5979 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5980 | |
| 5981 | private: |
| 5982 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5983 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5984 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5985 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5986 | |
| 5987 | // Coercion type builder for structs passed in registers. The coercion type |
| 5988 | // serves two purposes: |
| 5989 | // |
| 5990 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 5991 | // in registers. |
| 5992 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 5993 | // code generator knows to pass them in floating point registers. |
| 5994 | // |
| 5995 | // We also compute the InReg flag which indicates that the struct contains |
| 5996 | // aligned 32-bit floats. |
| 5997 | // |
| 5998 | struct CoerceBuilder { |
| 5999 | llvm::LLVMContext &Context; |
| 6000 | const llvm::DataLayout &DL; |
| 6001 | SmallVector<llvm::Type*, 8> Elems; |
| 6002 | uint64_t Size; |
| 6003 | bool InReg; |
| 6004 | |
| 6005 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 6006 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 6007 | |
| 6008 | // Pad Elems with integers until Size is ToSize. |
| 6009 | void pad(uint64_t ToSize) { |
| 6010 | assert(ToSize >= Size && "Cannot remove elements"); |
| 6011 | if (ToSize == Size) |
| 6012 | return; |
| 6013 | |
| 6014 | // Finish the current 64-bit word. |
| 6015 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 6016 | if (Aligned > Size && Aligned <= ToSize) { |
| 6017 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 6018 | Size = Aligned; |
| 6019 | } |
| 6020 | |
| 6021 | // Add whole 64-bit words. |
| 6022 | while (Size + 64 <= ToSize) { |
| 6023 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 6024 | Size += 64; |
| 6025 | } |
| 6026 | |
| 6027 | // Final in-word padding. |
| 6028 | if (Size < ToSize) { |
| 6029 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 6030 | Size = ToSize; |
| 6031 | } |
| 6032 | } |
| 6033 | |
| 6034 | // Add a floating point element at Offset. |
| 6035 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 6036 | // Unaligned floats are treated as integers. |
| 6037 | if (Offset % Bits) |
| 6038 | return; |
| 6039 | // The InReg flag is only required if there are any floats < 64 bits. |
| 6040 | if (Bits < 64) |
| 6041 | InReg = true; |
| 6042 | pad(Offset); |
| 6043 | Elems.push_back(Ty); |
| 6044 | Size = Offset + Bits; |
| 6045 | } |
| 6046 | |
| 6047 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 6048 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 6049 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 6050 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 6051 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 6052 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 6053 | switch (ElemTy->getTypeID()) { |
| 6054 | case llvm::Type::StructTyID: |
| 6055 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 6056 | break; |
| 6057 | case llvm::Type::FloatTyID: |
| 6058 | addFloat(ElemOffset, ElemTy, 32); |
| 6059 | break; |
| 6060 | case llvm::Type::DoubleTyID: |
| 6061 | addFloat(ElemOffset, ElemTy, 64); |
| 6062 | break; |
| 6063 | case llvm::Type::FP128TyID: |
| 6064 | addFloat(ElemOffset, ElemTy, 128); |
| 6065 | break; |
| 6066 | case llvm::Type::PointerTyID: |
| 6067 | if (ElemOffset % 64 == 0) { |
| 6068 | pad(ElemOffset); |
| 6069 | Elems.push_back(ElemTy); |
| 6070 | Size += 64; |
| 6071 | } |
| 6072 | break; |
| 6073 | default: |
| 6074 | break; |
| 6075 | } |
| 6076 | } |
| 6077 | } |
| 6078 | |
| 6079 | // Check if Ty is a usable substitute for the coercion type. |
| 6080 | bool isUsableType(llvm::StructType *Ty) const { |
| 6081 | if (Ty->getNumElements() != Elems.size()) |
| 6082 | return false; |
| 6083 | for (unsigned i = 0, e = Elems.size(); i != e; ++i) |
| 6084 | if (Elems[i] != Ty->getElementType(i)) |
| 6085 | return false; |
| 6086 | return true; |
| 6087 | } |
| 6088 | |
| 6089 | // Get the coercion type as a literal struct type. |
| 6090 | llvm::Type *getType() const { |
| 6091 | if (Elems.size() == 1) |
| 6092 | return Elems.front(); |
| 6093 | else |
| 6094 | return llvm::StructType::get(Context, Elems); |
| 6095 | } |
| 6096 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6097 | }; |
| 6098 | } // end anonymous namespace |
| 6099 | |
| 6100 | ABIArgInfo |
| 6101 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 6102 | if (Ty->isVoidType()) |
| 6103 | return ABIArgInfo::getIgnore(); |
| 6104 | |
| 6105 | uint64_t Size = getContext().getTypeSize(Ty); |
| 6106 | |
| 6107 | // Anything too big to fit in registers is passed with an explicit indirect |
| 6108 | // pointer / sret pointer. |
| 6109 | if (Size > SizeLimit) |
| 6110 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 6111 | |
| 6112 | // Treat an enum type as its underlying type. |
| 6113 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6114 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6115 | |
| 6116 | // Integer types smaller than a register are extended. |
| 6117 | if (Size < 64 && Ty->isIntegerType()) |
| 6118 | return ABIArgInfo::getExtend(); |
| 6119 | |
| 6120 | // Other non-aggregates go in registers. |
| 6121 | if (!isAggregateTypeForABI(Ty)) |
| 6122 | return ABIArgInfo::getDirect(); |
| 6123 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 6124 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 6125 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 6126 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 6127 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 6128 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6129 | // 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] | 6130 | // Build a coercion type from the LLVM struct type. |
| 6131 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 6132 | if (!StrTy) |
| 6133 | return ABIArgInfo::getDirect(); |
| 6134 | |
| 6135 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 6136 | CB.addStruct(0, StrTy); |
| 6137 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 6138 | |
| 6139 | // Try to use the original type for coercion. |
| 6140 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 6141 | |
| 6142 | if (CB.InReg) |
| 6143 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 6144 | else |
| 6145 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6146 | } |
| 6147 | |
| 6148 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6149 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6150 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6151 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6152 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6153 | AI.setCoerceToType(ArgTy); |
| 6154 | |
| 6155 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6156 | CGBuilderTy &Builder = CGF.Builder; |
| 6157 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6158 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6159 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6160 | llvm::Value *ArgAddr; |
| 6161 | unsigned Stride; |
| 6162 | |
| 6163 | switch (AI.getKind()) { |
| 6164 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6165 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6166 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6167 | |
| 6168 | case ABIArgInfo::Extend: |
| 6169 | Stride = 8; |
| 6170 | ArgAddr = Builder |
| 6171 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6172 | "extend"); |
| 6173 | break; |
| 6174 | |
| 6175 | case ABIArgInfo::Direct: |
| 6176 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6177 | ArgAddr = Addr; |
| 6178 | break; |
| 6179 | |
| 6180 | case ABIArgInfo::Indirect: |
| 6181 | Stride = 8; |
| 6182 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6183 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6184 | "indirect"); |
| 6185 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6186 | break; |
| 6187 | |
| 6188 | case ABIArgInfo::Ignore: |
| 6189 | return llvm::UndefValue::get(ArgPtrTy); |
| 6190 | } |
| 6191 | |
| 6192 | // Update VAList. |
| 6193 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6194 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6195 | |
| 6196 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6200 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6201 | for (auto &I : FI.arguments()) |
| 6202 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | namespace { |
| 6206 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6207 | public: |
| 6208 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6209 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6210 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6211 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6212 | return 14; |
| 6213 | } |
| 6214 | |
| 6215 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6216 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6217 | }; |
| 6218 | } // end anonymous namespace |
| 6219 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6220 | bool |
| 6221 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6222 | llvm::Value *Address) const { |
| 6223 | // This is calculated from the LLVM and GCC tables and verified |
| 6224 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6225 | |
| 6226 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6227 | |
| 6228 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6229 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6230 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6231 | |
| 6232 | // 0-31: the 8-byte general-purpose registers |
| 6233 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6234 | |
| 6235 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6236 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6237 | |
| 6238 | // Y = 64 |
| 6239 | // PSR = 65 |
| 6240 | // WIM = 66 |
| 6241 | // TBR = 67 |
| 6242 | // PC = 68 |
| 6243 | // NPC = 69 |
| 6244 | // FSR = 70 |
| 6245 | // CSR = 71 |
| 6246 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 6247 | |
| 6248 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6249 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6250 | |
| 6251 | return false; |
| 6252 | } |
| 6253 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6254 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6255 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6256 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6257 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6258 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6259 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6260 | |
| 6261 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6262 | /// it by reference between functions that append to it. |
| 6263 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6264 | |
| 6265 | /// TypeStringCache caches the meta encodings of Types. |
| 6266 | /// |
| 6267 | /// The reason for caching TypeStrings is two fold: |
| 6268 | /// 1. To cache a type's encoding for later uses; |
| 6269 | /// 2. As a means to break recursive member type inclusion. |
| 6270 | /// |
| 6271 | /// A cache Entry can have a Status of: |
| 6272 | /// NonRecursive: The type encoding is not recursive; |
| 6273 | /// Recursive: The type encoding is recursive; |
| 6274 | /// Incomplete: An incomplete TypeString; |
| 6275 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6276 | /// Recursive type encoding. |
| 6277 | /// |
| 6278 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6279 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6280 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6281 | /// the type is encountered. |
| 6282 | /// |
| 6283 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6284 | /// possible. The type itself is recursive and it may contain other types which |
| 6285 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6286 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6287 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6288 | /// |
| 6289 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6290 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6291 | /// are placed into the cache during type expansion as a means to identify and |
| 6292 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6293 | /// the entry becomes IncompleteUsed. |
| 6294 | /// |
| 6295 | /// During the expansion of a RecordType's members: |
| 6296 | /// |
| 6297 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6298 | /// cached encoding is used; |
| 6299 | /// |
| 6300 | /// If the cache contains a Recursive encoding for the member type, the |
| 6301 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6302 | /// |
| 6303 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6304 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6305 | /// |
| 6306 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6307 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6308 | /// it is swapped back in; |
| 6309 | /// |
| 6310 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6311 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6312 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6313 | /// |
| 6314 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6315 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6316 | /// Else the member is part of a recursive type and thus the recursion has |
| 6317 | /// been exited too soon for the encoding to be correct for the member. |
| 6318 | /// |
| 6319 | class TypeStringCache { |
| 6320 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6321 | struct Entry { |
| 6322 | std::string Str; // The encoded TypeString for the type. |
| 6323 | enum Status State; // Information about the encoding in 'Str'. |
| 6324 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6325 | // during the expansion of RecordType's members. |
| 6326 | }; |
| 6327 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6328 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6329 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6330 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6331 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6332 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6333 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6334 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6335 | bool IsRecursive); |
| 6336 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6337 | }; |
| 6338 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6339 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6340 | /// FieldEncoding is a helper for this ordering process. |
| 6341 | class FieldEncoding { |
| 6342 | bool HasName; |
| 6343 | std::string Enc; |
| 6344 | public: |
| 6345 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6346 | StringRef str() {return Enc.c_str();}; |
| 6347 | bool operator<(const FieldEncoding &rhs) const { |
| 6348 | if (HasName != rhs.HasName) return HasName; |
| 6349 | return Enc < rhs.Enc; |
| 6350 | } |
| 6351 | }; |
| 6352 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6353 | class XCoreABIInfo : public DefaultABIInfo { |
| 6354 | public: |
| 6355 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6356 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6357 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6358 | }; |
| 6359 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6360 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6361 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6362 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6363 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6364 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6365 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6366 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6367 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6368 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6369 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6370 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6371 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6372 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6373 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6374 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6375 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6376 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6377 | CGF.Int8PtrPtrTy); |
| 6378 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6379 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6380 | // Handle the argument. |
| 6381 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6382 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6383 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6384 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6385 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6386 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6387 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6388 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6389 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6390 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6391 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6392 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6393 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6394 | ArgSize = 0; |
| 6395 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6396 | case ABIArgInfo::Extend: |
| 6397 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6398 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6399 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6400 | if (ArgSize < 4) |
| 6401 | ArgSize = 4; |
| 6402 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6403 | case ABIArgInfo::Indirect: |
| 6404 | llvm::Value *ArgAddr; |
| 6405 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6406 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6407 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6408 | ArgSize = 4; |
| 6409 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6410 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6411 | |
| 6412 | // Increment the VAList. |
| 6413 | if (ArgSize) { |
| 6414 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6415 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6416 | } |
| 6417 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6418 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6419 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6420 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6421 | /// into the cache as a means to identify and break recursion. |
| 6422 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6423 | /// be reinserted by removeIncomplete(). |
| 6424 | /// All other types of encoding should have been used rather than arriving here. |
| 6425 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6426 | std::string StubEnc) { |
| 6427 | if (!ID) |
| 6428 | return; |
| 6429 | Entry &E = Map[ID]; |
| 6430 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6431 | "Incorrectly use of addIncomplete"); |
| 6432 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6433 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6434 | E.Str.swap(StubEnc); |
| 6435 | E.State = Incomplete; |
| 6436 | ++IncompleteCount; |
| 6437 | } |
| 6438 | |
| 6439 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6440 | /// must be removed from the cache. |
| 6441 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6442 | /// Returns true if the RecordType was defined recursively. |
| 6443 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6444 | if (!ID) |
| 6445 | return false; |
| 6446 | auto I = Map.find(ID); |
| 6447 | assert(I != Map.end() && "Entry not present"); |
| 6448 | Entry &E = I->second; |
| 6449 | assert( (E.State == Incomplete || |
| 6450 | E.State == IncompleteUsed) && |
| 6451 | "Entry must be an incomplete type"); |
| 6452 | bool IsRecursive = false; |
| 6453 | if (E.State == IncompleteUsed) { |
| 6454 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6455 | IsRecursive = true; |
| 6456 | --IncompleteUsedCount; |
| 6457 | } |
| 6458 | if (E.Swapped.empty()) |
| 6459 | Map.erase(I); |
| 6460 | else { |
| 6461 | // Swap the Recursive back. |
| 6462 | E.Swapped.swap(E.Str); |
| 6463 | E.Swapped.clear(); |
| 6464 | E.State = Recursive; |
| 6465 | } |
| 6466 | --IncompleteCount; |
| 6467 | return IsRecursive; |
| 6468 | } |
| 6469 | |
| 6470 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6471 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6472 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6473 | bool IsRecursive) { |
| 6474 | if (!ID || IncompleteUsedCount) |
| 6475 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6476 | Entry &E = Map[ID]; |
| 6477 | if (IsRecursive && !E.Str.empty()) { |
| 6478 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6479 | "This is not the same Recursive entry"); |
| 6480 | // The parent container was not recursive after all, so we could have used |
| 6481 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6482 | // we started viz: IncompleteCount!=0. |
| 6483 | return; |
| 6484 | } |
| 6485 | assert(E.Str.empty() && "Entry already present"); |
| 6486 | E.Str = Str.str(); |
| 6487 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6488 | } |
| 6489 | |
| 6490 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6491 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6492 | /// encoding is Recursive, return an empty StringRef. |
| 6493 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6494 | if (!ID) |
| 6495 | return StringRef(); // We have no key. |
| 6496 | auto I = Map.find(ID); |
| 6497 | if (I == Map.end()) |
| 6498 | return StringRef(); // We have no encoding. |
| 6499 | Entry &E = I->second; |
| 6500 | if (E.State == Recursive && IncompleteCount) |
| 6501 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6502 | |
| 6503 | if (E.State == Incomplete) { |
| 6504 | // The incomplete type is being used to break out of recursion. |
| 6505 | E.State = IncompleteUsed; |
| 6506 | ++IncompleteUsedCount; |
| 6507 | } |
| 6508 | return E.Str.c_str(); |
| 6509 | } |
| 6510 | |
| 6511 | /// The XCore ABI includes a type information section that communicates symbol |
| 6512 | /// type information to the linker. The linker uses this information to verify |
| 6513 | /// safety/correctness of things such as array bound and pointers et al. |
| 6514 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6515 | /// This type information (TypeString) is emitted into meta data for all global |
| 6516 | /// symbols: definitions, declarations, functions & variables. |
| 6517 | /// |
| 6518 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6519 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
| 6520 | /// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf> |
| 6521 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6522 | /// |
| 6523 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6524 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6525 | |
| 6526 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6527 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6528 | CodeGen::CodeGenModule &CGM) const { |
| 6529 | SmallStringEnc Enc; |
| 6530 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6531 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 6532 | llvm::SmallVector<llvm::Value *, 2> MDVals; |
| 6533 | MDVals.push_back(GV); |
| 6534 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6535 | llvm::NamedMDNode *MD = |
| 6536 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6537 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6538 | } |
| 6539 | } |
| 6540 | |
| 6541 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6542 | const CodeGen::CodeGenModule &CGM, |
| 6543 | TypeStringCache &TSC); |
| 6544 | |
| 6545 | /// Helper function for appendRecordType(). |
| 6546 | /// Builds a SmallVector containing the encoded field types in declaration order. |
| 6547 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6548 | const RecordDecl *RD, |
| 6549 | const CodeGen::CodeGenModule &CGM, |
| 6550 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6551 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6552 | SmallStringEnc Enc; |
| 6553 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6554 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6555 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6556 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6557 | Enc += "b("; |
| 6558 | llvm::raw_svector_ostream OS(Enc); |
| 6559 | OS.resync(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6560 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6561 | OS.flush(); |
| 6562 | Enc += ':'; |
| 6563 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6564 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6565 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6566 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6567 | Enc += ')'; |
| 6568 | Enc += '}'; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6569 | FE.push_back(FieldEncoding(!Field->getName().empty(), Enc)); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6570 | } |
| 6571 | return true; |
| 6572 | } |
| 6573 | |
| 6574 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6575 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6576 | /// Union types have their fields ordered according to the ABI. |
| 6577 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6578 | const CodeGen::CodeGenModule &CGM, |
| 6579 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6580 | // Append the cached TypeString if we have one. |
| 6581 | StringRef TypeString = TSC.lookupStr(ID); |
| 6582 | if (!TypeString.empty()) { |
| 6583 | Enc += TypeString; |
| 6584 | return true; |
| 6585 | } |
| 6586 | |
| 6587 | // Start to emit an incomplete TypeString. |
| 6588 | size_t Start = Enc.size(); |
| 6589 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6590 | Enc += '('; |
| 6591 | if (ID) |
| 6592 | Enc += ID->getName(); |
| 6593 | Enc += "){"; |
| 6594 | |
| 6595 | // We collect all encoded fields and order as necessary. |
| 6596 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6597 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6598 | if (RD && !RD->field_empty()) { |
| 6599 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6600 | // so that recursive calls to this RecordType will use it whilst building a |
| 6601 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6602 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6603 | std::string StubEnc(Enc.substr(Start).str()); |
| 6604 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6605 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6606 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6607 | (void) TSC.removeIncomplete(ID); |
| 6608 | return false; |
| 6609 | } |
| 6610 | IsRecursive = TSC.removeIncomplete(ID); |
| 6611 | // The ABI requires unions to be sorted but not structures. |
| 6612 | // See FieldEncoding::operator< for sort algorithm. |
| 6613 | if (RT->isUnionType()) |
| 6614 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6615 | // We can now complete the TypeString. |
| 6616 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6617 | for (unsigned I = 0; I != E; ++I) { |
| 6618 | if (I) |
| 6619 | Enc += ','; |
| 6620 | Enc += FE[I].str(); |
| 6621 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6622 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6623 | Enc += '}'; |
| 6624 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6625 | return true; |
| 6626 | } |
| 6627 | |
| 6628 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6629 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6630 | TypeStringCache &TSC, |
| 6631 | const IdentifierInfo *ID) { |
| 6632 | // Append the cached TypeString if we have one. |
| 6633 | StringRef TypeString = TSC.lookupStr(ID); |
| 6634 | if (!TypeString.empty()) { |
| 6635 | Enc += TypeString; |
| 6636 | return true; |
| 6637 | } |
| 6638 | |
| 6639 | size_t Start = Enc.size(); |
| 6640 | Enc += "e("; |
| 6641 | if (ID) |
| 6642 | Enc += ID->getName(); |
| 6643 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6644 | |
| 6645 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6646 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6647 | SmallVector<FieldEncoding, 16> FE; |
| 6648 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6649 | ++I) { |
| 6650 | SmallStringEnc EnumEnc; |
| 6651 | EnumEnc += "m("; |
| 6652 | EnumEnc += I->getName(); |
| 6653 | EnumEnc += "){"; |
| 6654 | I->getInitVal().toString(EnumEnc); |
| 6655 | EnumEnc += '}'; |
| 6656 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6657 | } |
| 6658 | std::sort(FE.begin(), FE.end()); |
| 6659 | unsigned E = FE.size(); |
| 6660 | for (unsigned I = 0; I != E; ++I) { |
| 6661 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6662 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6663 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6664 | } |
| 6665 | } |
| 6666 | Enc += '}'; |
| 6667 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6668 | return true; |
| 6669 | } |
| 6670 | |
| 6671 | /// Appends type's qualifier to Enc. |
| 6672 | /// This is done prior to appending the type's encoding. |
| 6673 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6674 | // Qualifiers are emitted in alphabetical order. |
| 6675 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6676 | int Lookup = 0; |
| 6677 | if (QT.isConstQualified()) |
| 6678 | Lookup += 1<<0; |
| 6679 | if (QT.isRestrictQualified()) |
| 6680 | Lookup += 1<<1; |
| 6681 | if (QT.isVolatileQualified()) |
| 6682 | Lookup += 1<<2; |
| 6683 | Enc += Table[Lookup]; |
| 6684 | } |
| 6685 | |
| 6686 | /// Appends built-in types to Enc. |
| 6687 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6688 | const char *EncType; |
| 6689 | switch (BT->getKind()) { |
| 6690 | case BuiltinType::Void: |
| 6691 | EncType = "0"; |
| 6692 | break; |
| 6693 | case BuiltinType::Bool: |
| 6694 | EncType = "b"; |
| 6695 | break; |
| 6696 | case BuiltinType::Char_U: |
| 6697 | EncType = "uc"; |
| 6698 | break; |
| 6699 | case BuiltinType::UChar: |
| 6700 | EncType = "uc"; |
| 6701 | break; |
| 6702 | case BuiltinType::SChar: |
| 6703 | EncType = "sc"; |
| 6704 | break; |
| 6705 | case BuiltinType::UShort: |
| 6706 | EncType = "us"; |
| 6707 | break; |
| 6708 | case BuiltinType::Short: |
| 6709 | EncType = "ss"; |
| 6710 | break; |
| 6711 | case BuiltinType::UInt: |
| 6712 | EncType = "ui"; |
| 6713 | break; |
| 6714 | case BuiltinType::Int: |
| 6715 | EncType = "si"; |
| 6716 | break; |
| 6717 | case BuiltinType::ULong: |
| 6718 | EncType = "ul"; |
| 6719 | break; |
| 6720 | case BuiltinType::Long: |
| 6721 | EncType = "sl"; |
| 6722 | break; |
| 6723 | case BuiltinType::ULongLong: |
| 6724 | EncType = "ull"; |
| 6725 | break; |
| 6726 | case BuiltinType::LongLong: |
| 6727 | EncType = "sll"; |
| 6728 | break; |
| 6729 | case BuiltinType::Float: |
| 6730 | EncType = "ft"; |
| 6731 | break; |
| 6732 | case BuiltinType::Double: |
| 6733 | EncType = "d"; |
| 6734 | break; |
| 6735 | case BuiltinType::LongDouble: |
| 6736 | EncType = "ld"; |
| 6737 | break; |
| 6738 | default: |
| 6739 | return false; |
| 6740 | } |
| 6741 | Enc += EncType; |
| 6742 | return true; |
| 6743 | } |
| 6744 | |
| 6745 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6746 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6747 | const CodeGen::CodeGenModule &CGM, |
| 6748 | TypeStringCache &TSC) { |
| 6749 | Enc += "p("; |
| 6750 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6751 | return false; |
| 6752 | Enc += ')'; |
| 6753 | return true; |
| 6754 | } |
| 6755 | |
| 6756 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6757 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6758 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6759 | const CodeGen::CodeGenModule &CGM, |
| 6760 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6761 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6762 | return false; |
| 6763 | Enc += "a("; |
| 6764 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6765 | CAT->getSize().toStringUnsigned(Enc); |
| 6766 | else |
| 6767 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6768 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6769 | // The Qualifiers should be attached to the type rather than the array. |
| 6770 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6771 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6772 | return false; |
| 6773 | Enc += ')'; |
| 6774 | return true; |
| 6775 | } |
| 6776 | |
| 6777 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6778 | /// and the arguments. |
| 6779 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6780 | const CodeGen::CodeGenModule &CGM, |
| 6781 | TypeStringCache &TSC) { |
| 6782 | Enc += "f{"; |
| 6783 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 6784 | return false; |
| 6785 | Enc += "}("; |
| 6786 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 6787 | // N.B. we are only interested in the adjusted param types. |
| 6788 | auto I = FPT->param_type_begin(); |
| 6789 | auto E = FPT->param_type_end(); |
| 6790 | if (I != E) { |
| 6791 | do { |
| 6792 | if (!appendType(Enc, *I, CGM, TSC)) |
| 6793 | return false; |
| 6794 | ++I; |
| 6795 | if (I != E) |
| 6796 | Enc += ','; |
| 6797 | } while (I != E); |
| 6798 | if (FPT->isVariadic()) |
| 6799 | Enc += ",va"; |
| 6800 | } else { |
| 6801 | if (FPT->isVariadic()) |
| 6802 | Enc += "va"; |
| 6803 | else |
| 6804 | Enc += '0'; |
| 6805 | } |
| 6806 | } |
| 6807 | Enc += ')'; |
| 6808 | return true; |
| 6809 | } |
| 6810 | |
| 6811 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 6812 | /// type encodings. |
| 6813 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6814 | const CodeGen::CodeGenModule &CGM, |
| 6815 | TypeStringCache &TSC) { |
| 6816 | |
| 6817 | QualType QT = QType.getCanonicalType(); |
| 6818 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6819 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 6820 | // The Qualifiers should be attached to the type rather than the array. |
| 6821 | // Thus we don't call appendQualifier() here. |
| 6822 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 6823 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6824 | appendQualifier(Enc, QT); |
| 6825 | |
| 6826 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 6827 | return appendBuiltinType(Enc, BT); |
| 6828 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6829 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 6830 | return appendPointerType(Enc, PT, CGM, TSC); |
| 6831 | |
| 6832 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 6833 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 6834 | |
| 6835 | if (const RecordType *RT = QT->getAsStructureType()) |
| 6836 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6837 | |
| 6838 | if (const RecordType *RT = QT->getAsUnionType()) |
| 6839 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6840 | |
| 6841 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 6842 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 6843 | |
| 6844 | return false; |
| 6845 | } |
| 6846 | |
| 6847 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6848 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 6849 | if (!D) |
| 6850 | return false; |
| 6851 | |
| 6852 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6853 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 6854 | return false; |
| 6855 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 6856 | } |
| 6857 | |
| 6858 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6859 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 6860 | return false; |
| 6861 | QualType QT = VD->getType().getCanonicalType(); |
| 6862 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 6863 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6864 | // The Qualifiers should be attached to the type rather than the array. |
| 6865 | // Thus we don't call appendQualifier() here. |
| 6866 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6867 | } |
| 6868 | return appendType(Enc, QT, CGM, TSC); |
| 6869 | } |
| 6870 | return false; |
| 6871 | } |
| 6872 | |
| 6873 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6874 | //===----------------------------------------------------------------------===// |
| 6875 | // Driver code |
| 6876 | //===----------------------------------------------------------------------===// |
| 6877 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 6878 | const llvm::Triple &CodeGenModule::getTriple() const { |
| 6879 | return getTarget().getTriple(); |
| 6880 | } |
| 6881 | |
| 6882 | bool CodeGenModule::supportsCOMDAT() const { |
| 6883 | return !getTriple().isOSBinFormatMachO(); |
| 6884 | } |
| 6885 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6886 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6887 | if (TheTargetCodeGenInfo) |
| 6888 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6889 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6890 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 6891 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6892 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6893 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6894 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 6895 | case llvm::Triple::le32: |
| 6896 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6897 | case llvm::Triple::mips: |
| 6898 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6899 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 6900 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 6901 | case llvm::Triple::mips64: |
| 6902 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6903 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 6904 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 6905 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 6906 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6907 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 6908 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6909 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6910 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6911 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6912 | } |
| 6913 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6914 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6915 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6916 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6917 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6918 | { |
| 6919 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 6920 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6921 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 6922 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6923 | (CodeGenOpts.FloatABI != "soft" && |
| 6924 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6925 | Kind = ARMABIInfo::AAPCS_VFP; |
| 6926 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6927 | switch (Triple.getOS()) { |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 6928 | case llvm::Triple::NaCl: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6929 | return *(TheTargetCodeGenInfo = |
| 6930 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 6931 | default: |
| 6932 | return *(TheTargetCodeGenInfo = |
| 6933 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 6934 | } |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6935 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6936 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6937 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6938 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 6939 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6940 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6941 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 6942 | if (getTarget().getABI() == "elfv2") |
| 6943 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
| 6944 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6945 | return *(TheTargetCodeGenInfo = |
| 6946 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind)); |
| 6947 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 6948 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6949 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 6950 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6951 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 6952 | if (getTarget().getABI() == "elfv1") |
| 6953 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
| 6954 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6955 | return *(TheTargetCodeGenInfo = |
| 6956 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind)); |
| 6957 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6958 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 6959 | case llvm::Triple::nvptx: |
| 6960 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6961 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6962 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6963 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6964 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6965 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6966 | case llvm::Triple::systemz: |
| 6967 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 6968 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6969 | case llvm::Triple::tce: |
| 6970 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 6971 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6972 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6973 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 6974 | bool IsSmallStructInRegABI = |
| 6975 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 6976 | bool IsWin32FloatStructABI = Triple.isWindowsMSVCEnvironment(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 6977 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6978 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 6979 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6980 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6981 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6982 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6983 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6984 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6985 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6986 | new X86_32TargetCodeGenInfo(Types, |
| 6987 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6988 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 6989 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6990 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6991 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6992 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6993 | case llvm::Triple::x86_64: { |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 6994 | bool HasAVX = getTarget().getABI() == "avx"; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6995 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6996 | switch (Triple.getOS()) { |
| 6997 | case llvm::Triple::Win32: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 6998 | return *(TheTargetCodeGenInfo = |
| 6999 | new WinX86_64TargetCodeGenInfo(Types, HasAVX)); |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 7000 | case llvm::Triple::NaCl: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 7001 | return *(TheTargetCodeGenInfo = |
| 7002 | new NaClX86_64TargetCodeGenInfo(Types, HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7003 | default: |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 7004 | return *(TheTargetCodeGenInfo = |
| 7005 | new X86_64TargetCodeGenInfo(Types, HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 7006 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 7007 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7008 | case llvm::Triple::hexagon: |
| 7009 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 7010 | case llvm::Triple::sparcv9: |
| 7011 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 7012 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 7013 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 7014 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 7015 | } |