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" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecordLayout.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 20 | #include "clang/CodeGen/CGFunctionInfo.h" |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CodeGenOptions.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
| 24 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 26 | |
| 27 | #include <algorithm> // std::sort |
| 28 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
| 31 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 32 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 33 | llvm::Value *Array, |
| 34 | llvm::Value *Value, |
| 35 | unsigned FirstIndex, |
| 36 | unsigned LastIndex) { |
| 37 | // Alternatively, we could emit this as a loop in the source. |
| 38 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
| 39 | llvm::Value *Cell = Builder.CreateConstInBoundsGEP1_32(Array, I); |
| 40 | Builder.CreateStore(Value, Cell); |
| 41 | } |
| 42 | } |
| 43 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 44 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 45 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 46 | T->isMemberFunctionPointerType(); |
| 47 | } |
| 48 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 49 | ABIInfo::~ABIInfo() {} |
| 50 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 51 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 52 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 53 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 54 | if (!RD) |
| 55 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 56 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 60 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 61 | const RecordType *RT = T->getAs<RecordType>(); |
| 62 | if (!RT) |
| 63 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 64 | return getRecordArgABI(RT, CXXABI); |
| 65 | } |
| 66 | |
| 67 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 68 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 71 | ASTContext &ABIInfo::getContext() const { |
| 72 | return CGT.getContext(); |
| 73 | } |
| 74 | |
| 75 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 76 | return CGT.getLLVMContext(); |
| 77 | } |
| 78 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 79 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 80 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 83 | const TargetInfo &ABIInfo::getTarget() const { |
| 84 | return CGT.getTarget(); |
| 85 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 86 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 87 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 88 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 89 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 90 | switch (TheKind) { |
| 91 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 92 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 93 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 94 | Ty->print(OS); |
| 95 | else |
| 96 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 97 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 98 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 99 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 100 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 101 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 102 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 103 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 104 | case InAlloca: |
| 105 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 106 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 107 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 108 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 109 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 110 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 111 | break; |
| 112 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 113 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 116 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 119 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 120 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 121 | // If someone can figure out a general rule for this, that would be great. |
| 122 | // It's probably just doomed to be platform-dependent, though. |
| 123 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 124 | // Verified for: |
| 125 | // x86-64 FreeBSD, Linux, Darwin |
| 126 | // x86-32 FreeBSD, Linux, Darwin |
| 127 | // PowerPC Linux, Darwin |
| 128 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 129 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 130 | return 32; |
| 131 | } |
| 132 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 133 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 134 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 135 | // The following conventions are known to require this to be false: |
| 136 | // x86_stdcall |
| 137 | // MIPS |
| 138 | // For everything else, we just prefer false unless we opt out. |
| 139 | return false; |
| 140 | } |
| 141 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 142 | void |
| 143 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 144 | llvm::SmallString<24> &Opt) const { |
| 145 | // This assumes the user is passing a library name like "rt" instead of a |
| 146 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 147 | // dynamic. |
| 148 | Opt = "-l"; |
| 149 | Opt += Lib; |
| 150 | } |
| 151 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 152 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 153 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 154 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 155 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 156 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 157 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 158 | if (FD->isUnnamedBitfield()) |
| 159 | return true; |
| 160 | |
| 161 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 162 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 163 | // Constant arrays of empty records count as empty, strip them off. |
| 164 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 165 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 166 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 167 | if (AT->getSize() == 0) |
| 168 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 169 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 170 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 172 | const RecordType *RT = FT->getAs<RecordType>(); |
| 173 | if (!RT) |
| 174 | return false; |
| 175 | |
| 176 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 177 | // |
| 178 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 179 | // current ABI. |
| 180 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 181 | return false; |
| 182 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 183 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 186 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 187 | /// fields. Note that a structure with a flexible array member is not |
| 188 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 189 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 190 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 191 | if (!RT) |
| 192 | return 0; |
| 193 | const RecordDecl *RD = RT->getDecl(); |
| 194 | if (RD->hasFlexibleArrayMember()) |
| 195 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 196 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 197 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 198 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 199 | for (const auto &I : CXXRD->bases()) |
| 200 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 201 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 202 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 203 | for (const auto *I : RD->fields()) |
| 204 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 205 | return false; |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | /// isSingleElementStruct - Determine if a structure is a "single |
| 210 | /// element struct", i.e. it has exactly one non-empty field or |
| 211 | /// exactly one field which is itself a single element |
| 212 | /// struct. Structures with flexible array members are never |
| 213 | /// considered single element structs. |
| 214 | /// |
| 215 | /// \return The field declaration for the single non-empty field, if |
| 216 | /// it exists. |
| 217 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 218 | const RecordType *RT = T->getAsStructureType(); |
| 219 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 220 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 221 | |
| 222 | const RecordDecl *RD = RT->getDecl(); |
| 223 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 224 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 225 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 226 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 227 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 228 | // If this is a C++ record, check the bases first. |
| 229 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 230 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 231 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 232 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 233 | continue; |
| 234 | |
| 235 | // If we already found an element then this isn't a single-element struct. |
| 236 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 237 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 238 | |
| 239 | // If this is non-empty and not a single element struct, the composite |
| 240 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 241 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 242 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 243 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 248 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 249 | QualType FT = FD->getType(); |
| 250 | |
| 251 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 252 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 253 | continue; |
| 254 | |
| 255 | // If we already found an element then this isn't a single-element |
| 256 | // struct. |
| 257 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 258 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 259 | |
| 260 | // Treat single element arrays as the element. |
| 261 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 262 | if (AT->getSize().getZExtValue() != 1) |
| 263 | break; |
| 264 | FT = AT->getElementType(); |
| 265 | } |
| 266 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 267 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 268 | Found = FT.getTypePtr(); |
| 269 | } else { |
| 270 | Found = isSingleElementStruct(FT, Context); |
| 271 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 272 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 276 | // We don't consider a struct a single-element struct if it has |
| 277 | // padding beyond the element type. |
| 278 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 279 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 280 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 281 | return Found; |
| 282 | } |
| 283 | |
| 284 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 285 | // Treat complex types as the element type. |
| 286 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 287 | Ty = CTy->getElementType(); |
| 288 | |
| 289 | // Check for a type which we know has a simple scalar argument-passing |
| 290 | // convention without any padding. (We're specifically looking for 32 |
| 291 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 292 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 293 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 294 | return false; |
| 295 | |
| 296 | uint64_t Size = Context.getTypeSize(Ty); |
| 297 | return Size == 32 || Size == 64; |
| 298 | } |
| 299 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 300 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 301 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 302 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 303 | /// inhibiting optimizations. |
| 304 | /// |
| 305 | // FIXME: This predicate is missing many cases, currently it just follows |
| 306 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 307 | // should probably make this smarter, or better yet make the LLVM backend |
| 308 | // capable of handling it. |
| 309 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 310 | // We can only expand structure types. |
| 311 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 312 | if (!RT) |
| 313 | return false; |
| 314 | |
| 315 | // We can only expand (C) structures. |
| 316 | // |
| 317 | // FIXME: This needs to be generalized to handle classes as well. |
| 318 | const RecordDecl *RD = RT->getDecl(); |
| 319 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 320 | return false; |
| 321 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 322 | uint64_t Size = 0; |
| 323 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 324 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 325 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 326 | return false; |
| 327 | |
| 328 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 329 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 330 | // counts as "basic" is more complicated than what we were doing previously. |
| 331 | if (FD->isBitField()) |
| 332 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 333 | |
| 334 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 337 | // Make sure there are not any holes in the struct. |
| 338 | if (Size != Context.getTypeSize(Ty)) |
| 339 | return false; |
| 340 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
| 344 | namespace { |
| 345 | /// DefaultABIInfo - The default implementation for ABI specific |
| 346 | /// details. This implementation provides information which results in |
| 347 | /// self-consistent and sensible LLVM IR generation, but does not |
| 348 | /// conform to any particular ABI. |
| 349 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 350 | public: |
| 351 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 353 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 354 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 355 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 356 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 357 | if (!getCXXABI().classifyReturnType(FI)) |
| 358 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 359 | for (auto &I : FI.arguments()) |
| 360 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 363 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 364 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 365 | }; |
| 366 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 367 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 368 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 369 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 370 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 374 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 375 | return nullptr; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 378 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 379 | if (isAggregateTypeForABI(Ty)) |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 380 | return ABIArgInfo::getIndirect(0); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 381 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 382 | // Treat an enum type as its underlying type. |
| 383 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 384 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 386 | return (Ty->isPromotableIntegerType() ? |
| 387 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 390 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 391 | if (RetTy->isVoidType()) |
| 392 | return ABIArgInfo::getIgnore(); |
| 393 | |
| 394 | if (isAggregateTypeForABI(RetTy)) |
| 395 | return ABIArgInfo::getIndirect(0); |
| 396 | |
| 397 | // Treat an enum type as its underlying type. |
| 398 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 399 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 400 | |
| 401 | return (RetTy->isPromotableIntegerType() ? |
| 402 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 403 | } |
| 404 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 405 | //===----------------------------------------------------------------------===// |
| 406 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 407 | // |
| 408 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 409 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 410 | //===----------------------------------------------------------------------===// |
| 411 | |
| 412 | class PNaClABIInfo : public ABIInfo { |
| 413 | public: |
| 414 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 415 | |
| 416 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 417 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 418 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 419 | void computeInfo(CGFunctionInfo &FI) const override; |
| 420 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 421 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 425 | public: |
| 426 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 427 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 428 | }; |
| 429 | |
| 430 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 431 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 432 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 433 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 434 | for (auto &I : FI.arguments()) |
| 435 | I.info = classifyArgumentType(I.type); |
| 436 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 437 | |
| 438 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 439 | CodeGenFunction &CGF) const { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 440 | return nullptr; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 443 | /// \brief Classify argument of given type \p Ty. |
| 444 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 445 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 446 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 447 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 448 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 449 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 450 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 451 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 452 | } else if (Ty->isFloatingType()) { |
| 453 | // Floating-point types don't go inreg. |
| 454 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 455 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 456 | |
| 457 | return (Ty->isPromotableIntegerType() ? |
| 458 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 462 | if (RetTy->isVoidType()) |
| 463 | return ABIArgInfo::getIgnore(); |
| 464 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 465 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 466 | if (isAggregateTypeForABI(RetTy)) |
| 467 | return ABIArgInfo::getIndirect(0); |
| 468 | |
| 469 | // Treat an enum type as its underlying type. |
| 470 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 471 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 472 | |
| 473 | return (RetTy->isPromotableIntegerType() ? |
| 474 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 475 | } |
| 476 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 477 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 478 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 479 | // 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] | 480 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 481 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 482 | IRType->getScalarSizeInBits() != 64; |
| 483 | } |
| 484 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 485 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 486 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 487 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 488 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 489 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 490 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 491 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 494 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 498 | return Ty; |
| 499 | } |
| 500 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 501 | //===----------------------------------------------------------------------===// |
| 502 | // X86-32 ABI Implementation |
| 503 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 504 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 505 | /// \brief Similar to llvm::CCState, but for Clang. |
| 506 | struct CCState { |
| 507 | CCState(unsigned CC) : CC(CC), FreeRegs(0) {} |
| 508 | |
| 509 | unsigned CC; |
| 510 | unsigned FreeRegs; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 511 | unsigned StackOffset; |
| 512 | bool UseInAlloca; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 513 | }; |
| 514 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 515 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 516 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 517 | enum Class { |
| 518 | Integer, |
| 519 | Float |
| 520 | }; |
| 521 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 522 | static const unsigned MinABIStackAlignInBytes = 4; |
| 523 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 524 | bool IsDarwinVectorABI; |
| 525 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 526 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 527 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 528 | |
| 529 | static bool isRegisterSize(unsigned Size) { |
| 530 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 531 | } |
| 532 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 533 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 534 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 535 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 536 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 537 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 538 | |
| 539 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 540 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 541 | /// \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] | 542 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 543 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 544 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 545 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 546 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 547 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 548 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 549 | /// \brief Rewrite the function info so that all memory arguments use |
| 550 | /// inalloca. |
| 551 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 552 | |
| 553 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 554 | unsigned &StackOffset, ABIArgInfo &Info, |
| 555 | QualType Type) const; |
| 556 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 557 | public: |
| 558 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 559 | void computeInfo(CGFunctionInfo &FI) const override; |
| 560 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 561 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 562 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 563 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 564 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 565 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 566 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 567 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 568 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 569 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 570 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 571 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 572 | bool d, bool p, bool w, unsigned r) |
| 573 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 574 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 575 | static bool isStructReturnInRegABI( |
| 576 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 577 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 578 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 579 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 580 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 581 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 582 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 583 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 584 | return 4; |
| 585 | } |
| 586 | |
| 587 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 588 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 589 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 590 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 591 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 592 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 593 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 594 | } |
| 595 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 596 | llvm::Constant * |
| 597 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 598 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 599 | (0x06 << 8) | // .+0x08 |
| 600 | ('F' << 16) | |
| 601 | ('T' << 24); |
| 602 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 603 | } |
| 604 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 608 | |
| 609 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 610 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 611 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 612 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 613 | uint64_t Size = Context.getTypeSize(Ty); |
| 614 | |
| 615 | // Type must be register sized. |
| 616 | if (!isRegisterSize(Size)) |
| 617 | return false; |
| 618 | |
| 619 | if (Ty->isVectorType()) { |
| 620 | // 64- and 128- bit vectors inside structures are not returned in |
| 621 | // registers. |
| 622 | if (Size == 64 || Size == 128) |
| 623 | return false; |
| 624 | |
| 625 | return true; |
| 626 | } |
| 627 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 628 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 629 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 630 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 631 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 632 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 633 | return true; |
| 634 | |
| 635 | // Arrays are treated like records. |
| 636 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 637 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 638 | |
| 639 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 640 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 641 | if (!RT) return false; |
| 642 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 643 | // FIXME: Traverse bases here too. |
| 644 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 645 | // Structure types are passed in register if all fields would be |
| 646 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 647 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 648 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 649 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 650 | continue; |
| 651 | |
| 652 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 653 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 654 | return false; |
| 655 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 656 | return true; |
| 657 | } |
| 658 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 659 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 660 | // If the return value is indirect, then the hidden argument is consuming one |
| 661 | // integer register. |
| 662 | if (State.FreeRegs) { |
| 663 | --State.FreeRegs; |
| 664 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 665 | } |
| 666 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 667 | } |
| 668 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 669 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 670 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 671 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 673 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 674 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 675 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 676 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 677 | |
| 678 | // 128-bit vectors are a special case; they are returned in |
| 679 | // registers and we need to make sure to pick a type the LLVM |
| 680 | // backend will like. |
| 681 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 682 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 683 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 684 | |
| 685 | // Always return in register if it fits in a general purpose |
| 686 | // register, or if it is 64 bits and has a single element. |
| 687 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 688 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 689 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 690 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 691 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 692 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 696 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 697 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 698 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 699 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 700 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 701 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 702 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 703 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 704 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 705 | // If specified, structs and unions are always indirect. |
| 706 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 707 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 708 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 709 | // Small structures which are register sized are generally returned |
| 710 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 711 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 712 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 713 | |
| 714 | // As a special-case, if the struct is a "single-element" struct, and |
| 715 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 716 | // floating-point register. (MSVC does not apply this special case.) |
| 717 | // We apply a similar transformation for pointer types to improve the |
| 718 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 719 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 720 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 721 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 722 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 723 | |
| 724 | // FIXME: We should be able to narrow this integer in cases with dead |
| 725 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 726 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 729 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 730 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 732 | // Treat an enum type as its underlying type. |
| 733 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 734 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 735 | |
| 736 | return (RetTy->isPromotableIntegerType() ? |
| 737 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 740 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 741 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 742 | } |
| 743 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 744 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 745 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 746 | if (!RT) |
| 747 | return 0; |
| 748 | const RecordDecl *RD = RT->getDecl(); |
| 749 | |
| 750 | // If this is a C++ record, check the bases first. |
| 751 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 752 | for (const auto &I : CXXRD->bases()) |
| 753 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 754 | return false; |
| 755 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 756 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 757 | QualType FT = i->getType(); |
| 758 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 759 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 760 | return true; |
| 761 | |
| 762 | if (isRecordWithSSEVectorType(Context, FT)) |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | return false; |
| 767 | } |
| 768 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 769 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 770 | unsigned Align) const { |
| 771 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 772 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 773 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 774 | return 0; // Use default alignment. |
| 775 | |
| 776 | // On non-Darwin, the stack type alignment is always 4. |
| 777 | if (!IsDarwinVectorABI) { |
| 778 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 779 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 780 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 781 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 782 | // 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] | 783 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 784 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 785 | return 16; |
| 786 | |
| 787 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 790 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 791 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 792 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 793 | if (State.FreeRegs) { |
| 794 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 795 | return ABIArgInfo::getIndirectInReg(0, false); |
| 796 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 797 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 798 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 799 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 800 | // Compute the byval alignment. |
| 801 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 802 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 803 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 804 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 805 | |
| 806 | // If the stack alignment is less than the type alignment, realign the |
| 807 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 808 | bool Realign = TypeAlign > StackAlign; |
| 809 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 812 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 813 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 814 | if (!T) |
| 815 | T = Ty.getTypePtr(); |
| 816 | |
| 817 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 818 | BuiltinType::Kind K = BT->getKind(); |
| 819 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 820 | return Float; |
| 821 | } |
| 822 | return Integer; |
| 823 | } |
| 824 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 825 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 826 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 827 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 828 | Class C = classify(Ty); |
| 829 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 830 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 831 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 832 | unsigned Size = getContext().getTypeSize(Ty); |
| 833 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 834 | |
| 835 | if (SizeInRegs == 0) |
| 836 | return false; |
| 837 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 838 | if (SizeInRegs > State.FreeRegs) { |
| 839 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 840 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 841 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 842 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 843 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 844 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 845 | if (State.CC == llvm::CallingConv::X86_FastCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 846 | if (Size > 32) |
| 847 | return false; |
| 848 | |
| 849 | if (Ty->isIntegralOrEnumerationType()) |
| 850 | return true; |
| 851 | |
| 852 | if (Ty->isPointerType()) |
| 853 | return true; |
| 854 | |
| 855 | if (Ty->isReferenceType()) |
| 856 | return true; |
| 857 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 858 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 859 | NeedsPadding = true; |
| 860 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 861 | return false; |
| 862 | } |
| 863 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 864 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 867 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 868 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 869 | // FIXME: Set alignment on indirect arguments. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 870 | if (isAggregateTypeForABI(Ty)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 871 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 872 | // Check with the C++ ABI first. |
| 873 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 874 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 875 | return getIndirectResult(Ty, false, State); |
| 876 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 877 | // The field index doesn't matter, we'll fix it up later. |
| 878 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 879 | } |
| 880 | |
| 881 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 882 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 883 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 884 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 885 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 886 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 887 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 888 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 889 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 890 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 891 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 892 | return ABIArgInfo::getIgnore(); |
| 893 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 894 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 895 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 896 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 897 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 898 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 899 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 900 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 901 | return ABIArgInfo::getDirectInReg(Result); |
| 902 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 903 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 904 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 905 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 906 | // of those arguments will match the struct. This is important because the |
| 907 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 908 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 909 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 910 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 911 | return ABIArgInfo::getExpandWithPadding( |
| 912 | State.CC == llvm::CallingConv::X86_FastCall, PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 913 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 914 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 917 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 918 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 919 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 920 | if (IsDarwinVectorABI) { |
| 921 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 922 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 923 | (Size == 64 && VT->getNumElements() == 1)) |
| 924 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 925 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 926 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 927 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 928 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 929 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 930 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 931 | return ABIArgInfo::getDirect(); |
| 932 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 933 | |
| 934 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 935 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 936 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 937 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 938 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 939 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 940 | |
| 941 | if (Ty->isPromotableIntegerType()) { |
| 942 | if (InReg) |
| 943 | return ABIArgInfo::getExtendInReg(); |
| 944 | return ABIArgInfo::getExtend(); |
| 945 | } |
| 946 | if (InReg) |
| 947 | return ABIArgInfo::getDirectInReg(); |
| 948 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 951 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 952 | CCState State(FI.getCallingConvention()); |
| 953 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 954 | State.FreeRegs = 2; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 955 | else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 956 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 957 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 958 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 959 | |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 960 | if (!getCXXABI().classifyReturnType(FI)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 961 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 962 | } else if (FI.getReturnInfo().isIndirect()) { |
| 963 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 964 | // return value was sret and put it in a register ourselves if appropriate. |
| 965 | if (State.FreeRegs) { |
| 966 | --State.FreeRegs; // The sret parameter consumes a register. |
| 967 | FI.getReturnInfo().setInReg(true); |
| 968 | } |
| 969 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 970 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 971 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 972 | for (auto &I : FI.arguments()) { |
| 973 | I.info = classifyArgumentType(I.type, State); |
| 974 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 978 | // all the memory arguments to use inalloca. |
| 979 | if (UsedInAlloca) |
| 980 | rewriteWithInAlloca(FI); |
| 981 | } |
| 982 | |
| 983 | void |
| 984 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 985 | unsigned &StackOffset, |
| 986 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 987 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 988 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 989 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 990 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 991 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 992 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 993 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 994 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 995 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 996 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 997 | unsigned NumBytes = StackOffset - OldOffset; |
| 998 | assert(NumBytes); |
| 999 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1000 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1001 | FrameFields.push_back(Ty); |
| 1002 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1006 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1007 | |
| 1008 | // Build a packed struct type for all of the arguments in memory. |
| 1009 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1010 | |
| 1011 | unsigned StackOffset = 0; |
| 1012 | |
| 1013 | // Put the sret parameter into the inalloca struct if it's in memory. |
| 1014 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1015 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1016 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1017 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1018 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1019 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | // Skip the 'this' parameter in ecx. |
| 1023 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1024 | if (FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall) |
| 1025 | ++I; |
| 1026 | |
| 1027 | // Put arguments passed in memory into the struct. |
| 1028 | for (; I != E; ++I) { |
| 1029 | |
| 1030 | // Leave ignored and inreg arguments alone. |
| 1031 | switch (I->info.getKind()) { |
| 1032 | case ABIArgInfo::Indirect: |
| 1033 | assert(I->info.getIndirectByVal()); |
| 1034 | break; |
| 1035 | case ABIArgInfo::Ignore: |
| 1036 | continue; |
| 1037 | case ABIArgInfo::Direct: |
| 1038 | case ABIArgInfo::Extend: |
| 1039 | if (I->info.getInReg()) |
| 1040 | continue; |
| 1041 | break; |
| 1042 | default: |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1047 | } |
| 1048 | |
| 1049 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1050 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1053 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1054 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1055 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1056 | |
| 1057 | CGBuilderTy &Builder = CGF.Builder; |
| 1058 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1059 | "ap"); |
| 1060 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1061 | |
| 1062 | // Compute if the address needs to be aligned |
| 1063 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1064 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1065 | Align = std::max(Align, 4U); |
| 1066 | if (Align > 4) { |
| 1067 | // addr = (addr + align - 1) & -align; |
| 1068 | llvm::Value *Offset = |
| 1069 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1070 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1071 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1072 | CGF.Int32Ty); |
| 1073 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1074 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1075 | Addr->getType(), |
| 1076 | "ap.cur.aligned"); |
| 1077 | } |
| 1078 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1079 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1080 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1081 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1082 | |
| 1083 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1084 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1085 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1086 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1087 | "ap.next"); |
| 1088 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1089 | |
| 1090 | return AddrTyped; |
| 1091 | } |
| 1092 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1093 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1094 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1095 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1096 | |
| 1097 | switch (Opts.getStructReturnConvention()) { |
| 1098 | case CodeGenOptions::SRCK_Default: |
| 1099 | break; |
| 1100 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1101 | return false; |
| 1102 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
| 1106 | if (Triple.isOSDarwin()) |
| 1107 | return true; |
| 1108 | |
| 1109 | switch (Triple.getOS()) { |
| 1110 | case llvm::Triple::AuroraUX: |
| 1111 | case llvm::Triple::DragonFly: |
| 1112 | case llvm::Triple::FreeBSD: |
| 1113 | case llvm::Triple::OpenBSD: |
| 1114 | case llvm::Triple::Bitrig: |
| 1115 | return true; |
| 1116 | case llvm::Triple::Win32: |
| 1117 | switch (Triple.getEnvironment()) { |
| 1118 | case llvm::Triple::UnknownEnvironment: |
| 1119 | case llvm::Triple::Cygnus: |
| 1120 | case llvm::Triple::GNU: |
| 1121 | case llvm::Triple::MSVC: |
| 1122 | return true; |
| 1123 | default: |
| 1124 | return false; |
| 1125 | } |
| 1126 | default: |
| 1127 | return false; |
| 1128 | } |
| 1129 | } |
| 1130 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1131 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1132 | llvm::GlobalValue *GV, |
| 1133 | CodeGen::CodeGenModule &CGM) const { |
| 1134 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1135 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1136 | // Get the LLVM function. |
| 1137 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1138 | |
| 1139 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1140 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1141 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1142 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1143 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1144 | llvm::AttributeSet::FunctionIndex, |
| 1145 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1150 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1151 | CodeGen::CodeGenFunction &CGF, |
| 1152 | llvm::Value *Address) const { |
| 1153 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1155 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1156 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1157 | // 0-7 are the eight integer registers; the order is different |
| 1158 | // on Darwin (for EH), but the range is the same. |
| 1159 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1160 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1161 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1162 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1163 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1164 | // These have size 16, which is sizeof(long double) on |
| 1165 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1166 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1167 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1168 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1169 | } else { |
| 1170 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1171 | // reason. |
| 1172 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1173 | |
| 1174 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1175 | // These have size 12, which is sizeof(long double) on |
| 1176 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1177 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1178 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1179 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1180 | |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1184 | //===----------------------------------------------------------------------===// |
| 1185 | // X86-64 ABI Implementation |
| 1186 | //===----------------------------------------------------------------------===// |
| 1187 | |
| 1188 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1189 | namespace { |
| 1190 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1191 | class X86_64ABIInfo : public ABIInfo { |
| 1192 | enum Class { |
| 1193 | Integer = 0, |
| 1194 | SSE, |
| 1195 | SSEUp, |
| 1196 | X87, |
| 1197 | X87Up, |
| 1198 | ComplexX87, |
| 1199 | NoClass, |
| 1200 | Memory |
| 1201 | }; |
| 1202 | |
| 1203 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1204 | /// |
| 1205 | /// Merge an accumulating classification \arg Accum with a field |
| 1206 | /// classification \arg Field. |
| 1207 | /// |
| 1208 | /// \param Accum - The accumulating classification. This should |
| 1209 | /// always be either NoClass or the result of a previous merge |
| 1210 | /// call. In addition, this should never be Memory (the caller |
| 1211 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1212 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1213 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1214 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1215 | /// |
| 1216 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1217 | /// final MEMORY or SSE classes when necessary. |
| 1218 | /// |
| 1219 | /// \param AggregateSize - The size of the current aggregate in |
| 1220 | /// the classification process. |
| 1221 | /// |
| 1222 | /// \param Lo - The classification for the parts of the type |
| 1223 | /// residing in the low word of the containing object. |
| 1224 | /// |
| 1225 | /// \param Hi - The classification for the parts of the type |
| 1226 | /// residing in the higher words of the containing object. |
| 1227 | /// |
| 1228 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1229 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1230 | /// classify - Determine the x86_64 register classes in which the |
| 1231 | /// given type T should be passed. |
| 1232 | /// |
| 1233 | /// \param Lo - The classification for the parts of the type |
| 1234 | /// residing in the low word of the containing object. |
| 1235 | /// |
| 1236 | /// \param Hi - The classification for the parts of the type |
| 1237 | /// residing in the high word of the containing object. |
| 1238 | /// |
| 1239 | /// \param OffsetBase - The bit offset of this type in the |
| 1240 | /// containing object. Some parameters are classified different |
| 1241 | /// depending on whether they straddle an eightbyte boundary. |
| 1242 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1243 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1244 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1245 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1246 | /// If a word is unused its result will be NoClass; if a type should |
| 1247 | /// be passed in Memory then at least the classification of \arg Lo |
| 1248 | /// will be Memory. |
| 1249 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1250 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1251 | /// |
| 1252 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1253 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1254 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1255 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1256 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1257 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1258 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1259 | unsigned IROffset, QualType SourceTy, |
| 1260 | unsigned SourceOffset) const; |
| 1261 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1262 | unsigned IROffset, QualType SourceTy, |
| 1263 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1264 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1265 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1266 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1267 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1268 | |
| 1269 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1270 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1271 | /// |
| 1272 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1273 | /// available. |
| 1274 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1275 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1276 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1277 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1278 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1279 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1280 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1281 | unsigned &neededSSE, |
| 1282 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1283 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1284 | bool IsIllegalVectorType(QualType Ty) const; |
| 1285 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1286 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1287 | /// unfortunately in ways that were not always consistent with |
| 1288 | /// certain previous compilers. In particular, platforms which |
| 1289 | /// required strict binary compatibility with older versions of GCC |
| 1290 | /// may need to exempt themselves. |
| 1291 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1292 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1295 | bool HasAVX; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1296 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1297 | // 64-bit hardware. |
| 1298 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1299 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1300 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1301 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1302 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1303 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1304 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1305 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1306 | bool isPassedUsingAVXType(QualType type) const { |
| 1307 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1308 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1309 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1310 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1311 | if (info.isDirect()) { |
| 1312 | llvm::Type *ty = info.getCoerceToType(); |
| 1313 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1314 | return (vectorTy->getBitWidth() > 128); |
| 1315 | } |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1319 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1320 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1321 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1322 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1323 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1325 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1326 | class WinX86_64ABIInfo : public ABIInfo { |
| 1327 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1328 | ABIArgInfo classify(QualType Ty, bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1329 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1330 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1331 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1332 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1333 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1334 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1335 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1336 | CodeGenFunction &CGF) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1337 | }; |
| 1338 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1339 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1340 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1341 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1342 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1343 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1344 | const X86_64ABIInfo &getABIInfo() const { |
| 1345 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1346 | } |
| 1347 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1348 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1349 | return 7; |
| 1350 | } |
| 1351 | |
| 1352 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1353 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1354 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1355 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1356 | // 0-15 are the 16 integer registers. |
| 1357 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1358 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1359 | return false; |
| 1360 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1361 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1362 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1363 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1364 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1365 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1366 | } |
| 1367 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1368 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1369 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1370 | // The default CC on x86-64 sets %al to the number of SSA |
| 1371 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1372 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1373 | // that when AVX types are involved: the ABI explicitly states it is |
| 1374 | // undefined, and it doesn't work in practice because of how the ABI |
| 1375 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1376 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1377 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1378 | for (CallArgList::const_iterator |
| 1379 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1380 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1381 | HasAVXType = true; |
| 1382 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1383 | } |
| 1384 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1385 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1386 | if (!HasAVXType) |
| 1387 | return true; |
| 1388 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1389 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1390 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1393 | llvm::Constant * |
| 1394 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1395 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1396 | (0x0a << 8) | // .+0x0c |
| 1397 | ('F' << 16) | |
| 1398 | ('T' << 24); |
| 1399 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1400 | } |
| 1401 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1402 | }; |
| 1403 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1404 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| 1405 | // If the argument does not end in .lib, automatically add the suffix. This |
| 1406 | // matches the behavior of MSVC. |
| 1407 | std::string ArgStr = Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1408 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1409 | ArgStr += ".lib"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1410 | return ArgStr; |
| 1411 | } |
| 1412 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1413 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1414 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1415 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1416 | bool d, bool p, bool w, unsigned RegParms) |
| 1417 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1418 | |
| 1419 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1420 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1421 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1422 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1423 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1424 | |
| 1425 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1426 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1427 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1428 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1429 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1430 | }; |
| 1431 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1432 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1433 | public: |
| 1434 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1435 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 1436 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1437 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1438 | return 7; |
| 1439 | } |
| 1440 | |
| 1441 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1442 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1443 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1444 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1445 | // 0-15 are the 16 integer registers. |
| 1446 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1447 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1448 | return false; |
| 1449 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1450 | |
| 1451 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1452 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1453 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1454 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1455 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1456 | |
| 1457 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1458 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1459 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1460 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1461 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1462 | }; |
| 1463 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1466 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1467 | Class &Hi) const { |
| 1468 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1469 | // |
| 1470 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1471 | // memory. |
| 1472 | // |
| 1473 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1474 | // memory. |
| 1475 | // |
| 1476 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1477 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1478 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1479 | // ABI working for processors that don't support the __m256 type. |
| 1480 | // |
| 1481 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1482 | // |
| 1483 | // Some of these are enforced by the merging logic. Others can arise |
| 1484 | // only with unions; for example: |
| 1485 | // union { _Complex double; unsigned; } |
| 1486 | // |
| 1487 | // Note that clauses (b) and (c) were added in 0.98. |
| 1488 | // |
| 1489 | if (Hi == Memory) |
| 1490 | Lo = Memory; |
| 1491 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1492 | Lo = Memory; |
| 1493 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1494 | Lo = Memory; |
| 1495 | if (Hi == SSEUp && Lo != SSE) |
| 1496 | Hi = SSE; |
| 1497 | } |
| 1498 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1499 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1500 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1501 | // classified recursively so that always two fields are |
| 1502 | // considered. The resulting class is calculated according to |
| 1503 | // the classes of the fields in the eightbyte: |
| 1504 | // |
| 1505 | // (a) If both classes are equal, this is the resulting class. |
| 1506 | // |
| 1507 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1508 | // the other class. |
| 1509 | // |
| 1510 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1511 | // class. |
| 1512 | // |
| 1513 | // (d) If one of the classes is INTEGER, the result is the |
| 1514 | // INTEGER. |
| 1515 | // |
| 1516 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1517 | // MEMORY is used as class. |
| 1518 | // |
| 1519 | // (f) Otherwise class SSE is used. |
| 1520 | |
| 1521 | // Accum should never be memory (we should have returned) or |
| 1522 | // ComplexX87 (because this cannot be passed in a structure). |
| 1523 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1524 | "Invalid accumulated classification during merge."); |
| 1525 | if (Accum == Field || Field == NoClass) |
| 1526 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1527 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1528 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1529 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1530 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1531 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1532 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1533 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1534 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1535 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1536 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1539 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1540 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1541 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1542 | // Class pairs with appropriate constructor methods for the various |
| 1543 | // situations. |
| 1544 | |
| 1545 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1546 | // shouldn't be passed in registers for example, so there is no chance they |
| 1547 | // can straddle an eightbyte. Verify & simplify. |
| 1548 | |
| 1549 | Lo = Hi = NoClass; |
| 1550 | |
| 1551 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1552 | Current = Memory; |
| 1553 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1554 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1555 | BuiltinType::Kind k = BT->getKind(); |
| 1556 | |
| 1557 | if (k == BuiltinType::Void) { |
| 1558 | Current = NoClass; |
| 1559 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1560 | Lo = Integer; |
| 1561 | Hi = Integer; |
| 1562 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1563 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1564 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1565 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1566 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1567 | Current = SSE; |
| 1568 | } else if (k == BuiltinType::LongDouble) { |
| 1569 | Lo = X87; |
| 1570 | Hi = X87Up; |
| 1571 | } |
| 1572 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1573 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1574 | return; |
| 1575 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1577 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1578 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1579 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1580 | return; |
| 1581 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1582 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1583 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1584 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1585 | return; |
| 1586 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1587 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1588 | if (Ty->isMemberPointerType()) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1589 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1590 | Lo = Hi = Integer; |
| 1591 | else |
| 1592 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1593 | return; |
| 1594 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1595 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1596 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1597 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1598 | if (Size == 32) { |
| 1599 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1600 | // float> as integer. |
| 1601 | Current = Integer; |
| 1602 | |
| 1603 | // If this type crosses an eightbyte boundary, it should be |
| 1604 | // split. |
| 1605 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1606 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1607 | if (EB_Real != EB_Imag) |
| 1608 | Hi = Lo; |
| 1609 | } else if (Size == 64) { |
| 1610 | // gcc passes <1 x double> in memory. :( |
| 1611 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1612 | return; |
| 1613 | |
| 1614 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1615 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1616 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1617 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1618 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1619 | Current = Integer; |
| 1620 | else |
| 1621 | Current = SSE; |
| 1622 | |
| 1623 | // If this type crosses an eightbyte boundary, it should be |
| 1624 | // split. |
| 1625 | if (OffsetBase && OffsetBase != 64) |
| 1626 | Hi = Lo; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1627 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1628 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1629 | // least significant one belongs to class SSE and all the others to class |
| 1630 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1631 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1632 | // This design isn't correct for 256-bits, but since there're no cases |
| 1633 | // where the upper parts would need to be inspected, avoid adding |
| 1634 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1635 | // |
| 1636 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1637 | // registers if they are "named", i.e. not part of the "..." of a |
| 1638 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1639 | Lo = SSE; |
| 1640 | Hi = SSEUp; |
| 1641 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1642 | return; |
| 1643 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1645 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1646 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1648 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1649 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1650 | if (Size <= 64) |
| 1651 | Current = Integer; |
| 1652 | else if (Size <= 128) |
| 1653 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1654 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1655 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1656 | else if (ET == getContext().DoubleTy || |
| 1657 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1658 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1659 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1660 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1661 | Current = ComplexX87; |
| 1662 | |
| 1663 | // If this complex type crosses an eightbyte boundary then it |
| 1664 | // should be split. |
| 1665 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1666 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1667 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1668 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1669 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1670 | return; |
| 1671 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1673 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1674 | // Arrays are treated like structures. |
| 1675 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1676 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1677 | |
| 1678 | // 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] | 1679 | // than four eightbytes, ..., it has class MEMORY. |
| 1680 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1681 | return; |
| 1682 | |
| 1683 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1684 | // fields, it has class MEMORY. |
| 1685 | // |
| 1686 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1687 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1688 | return; |
| 1689 | |
| 1690 | // Otherwise implement simplified merge. We could be smarter about |
| 1691 | // this, but it isn't worth it and would be harder to verify. |
| 1692 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1693 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1694 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1695 | |
| 1696 | // The only case a 256-bit wide vector could be used is when the array |
| 1697 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1698 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1699 | if (Size > 128 && EltSize != 256) |
| 1700 | return; |
| 1701 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1702 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1703 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1704 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1705 | Lo = merge(Lo, FieldLo); |
| 1706 | Hi = merge(Hi, FieldHi); |
| 1707 | if (Lo == Memory || Hi == Memory) |
| 1708 | break; |
| 1709 | } |
| 1710 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1711 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1712 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1713 | return; |
| 1714 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1716 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1717 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1718 | |
| 1719 | // 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] | 1720 | // than four eightbytes, ..., it has class MEMORY. |
| 1721 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1722 | return; |
| 1723 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1724 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1725 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1726 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1727 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1728 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1729 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1730 | const RecordDecl *RD = RT->getDecl(); |
| 1731 | |
| 1732 | // Assume variable sized types are passed in memory. |
| 1733 | if (RD->hasFlexibleArrayMember()) |
| 1734 | return; |
| 1735 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1736 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1737 | |
| 1738 | // Reset Lo class, this will be recomputed. |
| 1739 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1740 | |
| 1741 | // If this is a C++ record, classify the bases first. |
| 1742 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1743 | for (const auto &I : CXXRD->bases()) { |
| 1744 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1745 | "Unexpected base class!"); |
| 1746 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1747 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Classify this field. |
| 1750 | // |
| 1751 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1752 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1753 | // initialized to class NO_CLASS. |
| 1754 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1755 | uint64_t Offset = |
| 1756 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1757 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1758 | Lo = merge(Lo, FieldLo); |
| 1759 | Hi = merge(Hi, FieldHi); |
| 1760 | if (Lo == Memory || Hi == Memory) |
| 1761 | break; |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1766 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1767 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1768 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1769 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1770 | bool BitField = i->isBitField(); |
| 1771 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1772 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1773 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1774 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1775 | // The only case a 256-bit wide vector could be used is when the struct |
| 1776 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1777 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1778 | // |
| 1779 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1780 | Lo = Memory; |
| 1781 | return; |
| 1782 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1783 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1784 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1785 | Lo = Memory; |
| 1786 | return; |
| 1787 | } |
| 1788 | |
| 1789 | // Classify this field. |
| 1790 | // |
| 1791 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1792 | // exceeds a single eightbyte, each is classified |
| 1793 | // separately. Each eightbyte gets initialized to class |
| 1794 | // NO_CLASS. |
| 1795 | Class FieldLo, FieldHi; |
| 1796 | |
| 1797 | // Bit-fields require special handling, they do not force the |
| 1798 | // structure to be passed in memory even if unaligned, and |
| 1799 | // therefore they can straddle an eightbyte. |
| 1800 | if (BitField) { |
| 1801 | // Ignore padding bit-fields. |
| 1802 | if (i->isUnnamedBitfield()) |
| 1803 | continue; |
| 1804 | |
| 1805 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1806 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1807 | |
| 1808 | uint64_t EB_Lo = Offset / 64; |
| 1809 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 1810 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1811 | if (EB_Lo) { |
| 1812 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1813 | FieldLo = NoClass; |
| 1814 | FieldHi = Integer; |
| 1815 | } else { |
| 1816 | FieldLo = Integer; |
| 1817 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1818 | } |
| 1819 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1820 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1821 | Lo = merge(Lo, FieldLo); |
| 1822 | Hi = merge(Hi, FieldHi); |
| 1823 | if (Lo == Memory || Hi == Memory) |
| 1824 | break; |
| 1825 | } |
| 1826 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1827 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1831 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1832 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1833 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1834 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1835 | // Treat an enum type as its underlying type. |
| 1836 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1837 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1838 | |
| 1839 | return (Ty->isPromotableIntegerType() ? |
| 1840 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1841 | } |
| 1842 | |
| 1843 | return ABIArgInfo::getIndirect(0); |
| 1844 | } |
| 1845 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1846 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1847 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1848 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1849 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1850 | if (Size <= 64 || Size > LargestVector) |
| 1851 | return true; |
| 1852 | } |
| 1853 | |
| 1854 | return false; |
| 1855 | } |
| 1856 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1857 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1858 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1859 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1860 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1861 | // |
| 1862 | // This assumption is optimistic, as there could be free registers available |
| 1863 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1864 | // the argument in the free register. This does not seem to happen currently, |
| 1865 | // but this code would be much safer if we could mark the argument with |
| 1866 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1867 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1868 | // Treat an enum type as its underlying type. |
| 1869 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1870 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1871 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1872 | return (Ty->isPromotableIntegerType() ? |
| 1873 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1874 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1875 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1876 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1877 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1878 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1879 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1880 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1881 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1884 | // is important for good codegen. |
| 1885 | // |
| 1886 | // We do this by coercing the value into a scalar type which the backend can |
| 1887 | // handle naturally (i.e., without using byval). |
| 1888 | // |
| 1889 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1890 | // free integer registers. Doing this when there are free integer registers |
| 1891 | // would require more care, as we would have to ensure that the coerced value |
| 1892 | // did not claim the unused register. That would require either reording the |
| 1893 | // arguments to the function (so that any subsequent inreg values came first), |
| 1894 | // or only doing this optimization when there were no following arguments that |
| 1895 | // might be inreg. |
| 1896 | // |
| 1897 | // We currently expect it to be rare (particularly in well written code) for |
| 1898 | // arguments to be passed on the stack when there are still free integer |
| 1899 | // registers available (this would typically imply large structs being passed |
| 1900 | // by value), so this seems like a fair tradeoff for now. |
| 1901 | // |
| 1902 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 1903 | // attributes. See PR12193. |
| 1904 | if (freeIntRegs == 0) { |
| 1905 | uint64_t Size = getContext().getTypeSize(Ty); |
| 1906 | |
| 1907 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 1908 | // type, which will end up on the stack (with alignment 8). |
| 1909 | if (Align == 8 && Size <= 64) |
| 1910 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1911 | Size)); |
| 1912 | } |
| 1913 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1914 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1917 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1918 | /// 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] | 1919 | /// vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1920 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1921 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1922 | |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1923 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1924 | // strip them off if present. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1925 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1926 | while (STy && STy->getNumElements() == 1) { |
| 1927 | IRType = STy->getElementType(0); |
| 1928 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1929 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1930 | |
Bruno Cardoso Lopes | 129b4cc | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1931 | // 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] | 1932 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1933 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1934 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | 71f1b2d | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 1935 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1936 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1937 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1938 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1939 | EltTy->isIntegerTy(128))) |
| 1940 | return VT; |
| 1941 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1942 | |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1943 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1944 | } |
| 1945 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1946 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1947 | /// is known to either be off the end of the specified type or being in |
| 1948 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1949 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1950 | /// classification that put one of the two halves in the INTEGER class. |
| 1951 | /// |
| 1952 | /// It is conservatively correct to return false. |
| 1953 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1954 | unsigned EndBit, ASTContext &Context) { |
| 1955 | // If the bytes being queried are off the end of the type, there is no user |
| 1956 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1957 | // types that don't contain interesting padding. |
| 1958 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1959 | if (TySize <= StartBit) |
| 1960 | return true; |
| 1961 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1962 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1963 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1964 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1965 | |
| 1966 | // Check each element to see if the element overlaps with the queried range. |
| 1967 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1968 | // If the element is after the span we care about, then we're done.. |
| 1969 | unsigned EltOffset = i*EltSize; |
| 1970 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1971 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1972 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1973 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1974 | EndBit-EltOffset, Context)) |
| 1975 | return false; |
| 1976 | } |
| 1977 | // If it overlaps no elements, then it is safe to process as padding. |
| 1978 | return true; |
| 1979 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1980 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1981 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1982 | const RecordDecl *RD = RT->getDecl(); |
| 1983 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1984 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1985 | // If this is a C++ record, check the bases first. |
| 1986 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1987 | for (const auto &I : CXXRD->bases()) { |
| 1988 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1989 | "Unexpected base class!"); |
| 1990 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1991 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1993 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1994 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1995 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1996 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1997 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1998 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1999 | EndBit-BaseOffset, Context)) |
| 2000 | return false; |
| 2001 | } |
| 2002 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2004 | // Verify that no field has data that overlaps the region of interest. Yes |
| 2005 | // this could be sped up a lot by being smarter about queried fields, |
| 2006 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2007 | // much. |
| 2008 | unsigned idx = 0; |
| 2009 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2010 | i != e; ++i, ++idx) { |
| 2011 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2012 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2013 | // If we found a field after the region we care about, then we're done. |
| 2014 | if (FieldOffset >= EndBit) break; |
| 2015 | |
| 2016 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2017 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2018 | Context)) |
| 2019 | return false; |
| 2020 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2022 | // If nothing in this record overlapped the area of interest, then we're |
| 2023 | // clean. |
| 2024 | return true; |
| 2025 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2026 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2027 | return false; |
| 2028 | } |
| 2029 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2030 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2031 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2032 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2033 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2034 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2035 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2036 | // Base case if we find a float. |
| 2037 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2038 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2039 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2040 | // 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] | 2041 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2042 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2043 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2044 | IROffset -= SL->getElementOffset(Elt); |
| 2045 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2046 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2048 | // 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] | 2049 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2050 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2051 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2052 | IROffset -= IROffset/EltSize*EltSize; |
| 2053 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2054 | } |
| 2055 | |
| 2056 | return false; |
| 2057 | } |
| 2058 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2059 | |
| 2060 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2061 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2062 | llvm::Type *X86_64ABIInfo:: |
| 2063 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2064 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2065 | // 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] | 2066 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2067 | // structs that contain 3 floats. |
| 2068 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2069 | SourceOffset*8+64, getContext())) |
| 2070 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2071 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2072 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2073 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2074 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2075 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2076 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2077 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2078 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2079 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2080 | } |
| 2081 | |
| 2082 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2083 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2084 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2085 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2086 | /// 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] | 2087 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2088 | /// etc). |
| 2089 | /// |
| 2090 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2091 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2092 | /// the 8-byte value references. PrefType may be null. |
| 2093 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 2094 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2095 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2096 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2097 | llvm::Type *X86_64ABIInfo:: |
| 2098 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2099 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2100 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2101 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2102 | if (IROffset == 0) { |
| 2103 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2104 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2105 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2106 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2107 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2108 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2109 | // goodness in the source type is just tail padding. This is allowed to |
| 2110 | // kick in for struct {double,int} on the int, but not on |
| 2111 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2112 | // have to do this analysis on the source type because we can't depend on |
| 2113 | // unions being lowered a specific way etc. |
| 2114 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2115 | IRType->isIntegerTy(32) || |
| 2116 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2117 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2118 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2119 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2120 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2121 | SourceOffset*8+64, getContext())) |
| 2122 | return IRType; |
| 2123 | } |
| 2124 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2125 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2126 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2127 | // 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] | 2128 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2129 | if (IROffset < SL->getSizeInBytes()) { |
| 2130 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2131 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2132 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2133 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2134 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2135 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2136 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2137 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2138 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2139 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2140 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2141 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2142 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2143 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2144 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2145 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2146 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2147 | // 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] | 2148 | unsigned TySizeInBytes = |
| 2149 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2150 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2151 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2152 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2153 | // It is always safe to classify this as an integer type up to i64 that |
| 2154 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2155 | return llvm::IntegerType::get(getVMContext(), |
| 2156 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2159 | |
| 2160 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2161 | /// be used as elements of a two register pair to pass or return, return a |
| 2162 | /// first class aggregate to represent them. For example, if the low part of |
| 2163 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2164 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2165 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2166 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2167 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2168 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2169 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2170 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2171 | // the second element at offset 8. Check for this: |
| 2172 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2173 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2174 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2175 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2177 | // To handle this, we have to increase the size of the low part so that the |
| 2178 | // second element will start at an 8 byte offset. We can't increase the size |
| 2179 | // of the second element because it might make us access off the end of the |
| 2180 | // struct. |
| 2181 | if (HiStart != 8) { |
| 2182 | // There are only two sorts of types the ABI generation code can produce for |
| 2183 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2184 | // Promote these to a larger type. |
| 2185 | if (Lo->isFloatTy()) |
| 2186 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2187 | else { |
| 2188 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2189 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2190 | } |
| 2191 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2192 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2193 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2194 | |
| 2195 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2196 | // Verify that the second element is at an 8-byte offset. |
| 2197 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2198 | "Invalid x86-64 argument pair!"); |
| 2199 | return Result; |
| 2200 | } |
| 2201 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2202 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2203 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2204 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2205 | // classification algorithm. |
| 2206 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2207 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2208 | |
| 2209 | // Check some invariants. |
| 2210 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2211 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2212 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2213 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2214 | switch (Lo) { |
| 2215 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2216 | if (Hi == NoClass) |
| 2217 | return ABIArgInfo::getIgnore(); |
| 2218 | // If the low part is just padding, it takes no register, leave ResType |
| 2219 | // null. |
| 2220 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2221 | "Unknown missing lo part"); |
| 2222 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2223 | |
| 2224 | case SSEUp: |
| 2225 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2226 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2227 | |
| 2228 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2229 | // hidden argument. |
| 2230 | case Memory: |
| 2231 | return getIndirectReturnResult(RetTy); |
| 2232 | |
| 2233 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2234 | // available register of the sequence %rax, %rdx is used. |
| 2235 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2236 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2237 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2238 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2239 | // so that the parameter gets the right LLVM IR attributes. |
| 2240 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2241 | // Treat an enum type as its underlying type. |
| 2242 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2243 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2244 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2245 | if (RetTy->isIntegralOrEnumerationType() && |
| 2246 | RetTy->isPromotableIntegerType()) |
| 2247 | return ABIArgInfo::getExtend(); |
| 2248 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2249 | break; |
| 2250 | |
| 2251 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2252 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2253 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2254 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2255 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2256 | |
| 2257 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2258 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2259 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2260 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2261 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2262 | |
| 2263 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 2264 | // part of the value is returned in %st0 and the imaginary part in |
| 2265 | // %st1. |
| 2266 | case ComplexX87: |
| 2267 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2268 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2269 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2270 | NULL); |
| 2271 | break; |
| 2272 | } |
| 2273 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2274 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2275 | switch (Hi) { |
| 2276 | // Memory was handled previously and X87 should |
| 2277 | // never occur as a hi class. |
| 2278 | case Memory: |
| 2279 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2280 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2281 | |
| 2282 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2283 | case NoClass: |
| 2284 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2285 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2286 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2287 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2288 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2289 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2290 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2291 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2292 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2293 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2294 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2295 | break; |
| 2296 | |
| 2297 | // 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] | 2298 | // is passed in the next available eightbyte chunk if the last used |
| 2299 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2300 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2301 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2302 | case SSEUp: |
| 2303 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2304 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2305 | break; |
| 2306 | |
| 2307 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2308 | // returned together with the previous X87 value in %st0. |
| 2309 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2310 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2311 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2312 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2313 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2314 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2315 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2316 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2317 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2318 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2319 | break; |
| 2320 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2321 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2322 | // 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] | 2323 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2324 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2325 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2326 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2327 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2328 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2331 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2332 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2333 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2334 | const |
| 2335 | { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2336 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2337 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2338 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2339 | // Check some invariants. |
| 2340 | // FIXME: Enforce these by construction. |
| 2341 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2342 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2343 | |
| 2344 | neededInt = 0; |
| 2345 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2346 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2347 | switch (Lo) { |
| 2348 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2349 | if (Hi == NoClass) |
| 2350 | return ABIArgInfo::getIgnore(); |
| 2351 | // If the low part is just padding, it takes no register, leave ResType |
| 2352 | // null. |
| 2353 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2354 | "Unknown missing lo part"); |
| 2355 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2356 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2357 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2358 | // on the stack. |
| 2359 | case Memory: |
| 2360 | |
| 2361 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2362 | // COMPLEX_X87, it is passed in memory. |
| 2363 | case X87: |
| 2364 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2365 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2366 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2367 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2368 | |
| 2369 | case SSEUp: |
| 2370 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2371 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2372 | |
| 2373 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2374 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2375 | // and %r9 is used. |
| 2376 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2377 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2378 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2379 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2380 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2381 | |
| 2382 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2383 | // so that the parameter gets the right LLVM IR attributes. |
| 2384 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2385 | // Treat an enum type as its underlying type. |
| 2386 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2387 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2388 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2389 | if (Ty->isIntegralOrEnumerationType() && |
| 2390 | Ty->isPromotableIntegerType()) |
| 2391 | return ABIArgInfo::getExtend(); |
| 2392 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2393 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2394 | break; |
| 2395 | |
| 2396 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2397 | // available SSE register is used, the registers are taken in the |
| 2398 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2399 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2400 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2401 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2402 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2403 | break; |
| 2404 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2405 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2406 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2407 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2408 | switch (Hi) { |
| 2409 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2410 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2411 | // which is passed in memory. |
| 2412 | case Memory: |
| 2413 | case X87: |
| 2414 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2415 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2416 | |
| 2417 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2418 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2419 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2420 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2421 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2422 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2423 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2424 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2425 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2426 | break; |
| 2427 | |
| 2428 | // X87Up generally doesn't occur here (long double is passed in |
| 2429 | // memory), except in situations involving unions. |
| 2430 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2431 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2432 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2433 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2434 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2435 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2436 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2437 | ++neededSSE; |
| 2438 | break; |
| 2439 | |
| 2440 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2441 | // 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] | 2442 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2443 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2444 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2445 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2446 | break; |
| 2447 | } |
| 2448 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2449 | // If a high part was specified, merge it together with the low part. It is |
| 2450 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2451 | // first class struct aggregate with the high and low part: {low, high} |
| 2452 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2453 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2455 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2458 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2459 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2460 | if (!getCXXABI().classifyReturnType(FI)) |
| 2461 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2462 | |
| 2463 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2464 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2465 | |
| 2466 | // If the return value is indirect, then the hidden argument is consuming one |
| 2467 | // integer register. |
| 2468 | if (FI.getReturnInfo().isIndirect()) |
| 2469 | --freeIntRegs; |
| 2470 | |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2471 | bool isVariadic = FI.isVariadic(); |
| 2472 | unsigned numRequiredArgs = 0; |
| 2473 | if (isVariadic) |
| 2474 | numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs(); |
| 2475 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2476 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2477 | // get assigned (in left-to-right order) for passing as follows... |
| 2478 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2479 | it != ie; ++it) { |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2480 | bool isNamedArg = true; |
| 2481 | if (isVariadic) |
Aaron Ballman | 6a30264 | 2013-06-12 15:03:45 +0000 | [diff] [blame] | 2482 | isNamedArg = (it - FI.arg_begin()) < |
| 2483 | static_cast<signed>(numRequiredArgs); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2484 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2485 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2486 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2487 | neededSSE, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2488 | |
| 2489 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2490 | // eightbyte of an argument, the whole argument is passed on the |
| 2491 | // stack. If registers have already been assigned for some |
| 2492 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2493 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2494 | freeIntRegs -= neededInt; |
| 2495 | freeSSERegs -= neededSSE; |
| 2496 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2497 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2498 | } |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2503 | QualType Ty, |
| 2504 | CodeGenFunction &CGF) { |
| 2505 | llvm::Value *overflow_arg_area_p = |
| 2506 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2507 | llvm::Value *overflow_arg_area = |
| 2508 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2509 | |
| 2510 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2511 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2512 | // It isn't stated explicitly in the standard, but in practice we use |
| 2513 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2514 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2515 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2516 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2517 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2518 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2519 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2520 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2521 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2522 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2523 | overflow_arg_area = |
| 2524 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2525 | overflow_arg_area->getType(), |
| 2526 | "overflow_arg_area.align"); |
| 2527 | } |
| 2528 | |
| 2529 | // 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] | 2530 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2531 | llvm::Value *Res = |
| 2532 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2533 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2534 | |
| 2535 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2536 | // l->overflow_arg_area + sizeof(type). |
| 2537 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2538 | // an 8 byte boundary. |
| 2539 | |
| 2540 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2541 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2542 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2543 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2544 | "overflow_arg_area.next"); |
| 2545 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2546 | |
| 2547 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2548 | return Res; |
| 2549 | } |
| 2550 | |
| 2551 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2552 | CodeGenFunction &CGF) const { |
| 2553 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2554 | // struct { |
| 2555 | // i32 gp_offset; |
| 2556 | // i32 fp_offset; |
| 2557 | // i8* overflow_arg_area; |
| 2558 | // i8* reg_save_area; |
| 2559 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2560 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2562 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2563 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2564 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2565 | |
| 2566 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2567 | // in the registers. If not go to step 7. |
| 2568 | if (!neededInt && !neededSSE) |
| 2569 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2570 | |
| 2571 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2572 | // general purpose registers needed to pass type and num_fp to hold |
| 2573 | // the number of floating point registers needed. |
| 2574 | |
| 2575 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2576 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2577 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2578 | // |
| 2579 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2580 | // register save space). |
| 2581 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2582 | llvm::Value *InRegs = nullptr; |
| 2583 | llvm::Value *gp_offset_p = nullptr, *gp_offset = nullptr; |
| 2584 | llvm::Value *fp_offset_p = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2585 | if (neededInt) { |
| 2586 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2587 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2588 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2589 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | if (neededSSE) { |
| 2593 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2594 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2595 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2596 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2597 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2598 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2599 | } |
| 2600 | |
| 2601 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2602 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2603 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2604 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2605 | |
| 2606 | // Emit code to load the value if it was passed in registers. |
| 2607 | |
| 2608 | CGF.EmitBlock(InRegBlock); |
| 2609 | |
| 2610 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2611 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2612 | // copying to a temporary location in case the parameter is passed |
| 2613 | // in different register classes or requires an alignment greater |
| 2614 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2615 | // |
| 2616 | // FIXME: This really results in shameful code when we end up needing to |
| 2617 | // collect arguments from different places; often what should result in a |
| 2618 | // simple assembling of a structure from scattered addresses has many more |
| 2619 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2620 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2621 | llvm::Value *RegAddr = |
| 2622 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2623 | "reg_save_area"); |
| 2624 | if (neededInt && neededSSE) { |
| 2625 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2626 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2627 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2628 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2629 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2630 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2631 | llvm::Type *TyLo = ST->getElementType(0); |
| 2632 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2633 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2634 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2635 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2636 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2637 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2638 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 2639 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 2640 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2641 | llvm::Value *V = |
| 2642 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2643 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2644 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2645 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2646 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2647 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2648 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2649 | } else if (neededInt) { |
| 2650 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2651 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2652 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2653 | |
| 2654 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2655 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2656 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2657 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2658 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2659 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2660 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2661 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2662 | RegAddr = Tmp; |
| 2663 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2664 | } else if (neededSSE == 1) { |
| 2665 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2666 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2667 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2668 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2669 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2670 | // SSE registers are spaced 16 bytes apart in the register save |
| 2671 | // area, we need to collect the two eightbytes together. |
| 2672 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2673 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2674 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2675 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2676 | llvm::PointerType::getUnqual(DoubleTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2677 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); |
| 2678 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2679 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2680 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2681 | DblPtrTy)); |
| 2682 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2683 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2684 | DblPtrTy)); |
| 2685 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2686 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2687 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2691 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2692 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2693 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2694 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2695 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2696 | gp_offset_p); |
| 2697 | } |
| 2698 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2699 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2700 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2701 | fp_offset_p); |
| 2702 | } |
| 2703 | CGF.EmitBranch(ContBlock); |
| 2704 | |
| 2705 | // Emit code to load the value if it was passed in memory. |
| 2706 | |
| 2707 | CGF.EmitBlock(InMemBlock); |
| 2708 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2709 | |
| 2710 | // Return the appropriate result. |
| 2711 | |
| 2712 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2713 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2714 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2715 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2716 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2717 | return ResAddr; |
| 2718 | } |
| 2719 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2720 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2721 | |
| 2722 | if (Ty->isVoidType()) |
| 2723 | return ABIArgInfo::getIgnore(); |
| 2724 | |
| 2725 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2726 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2727 | |
| 2728 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2729 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2730 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2731 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2732 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2733 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2734 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 2735 | } |
| 2736 | |
| 2737 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2738 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2739 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2740 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 2741 | if (Size == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2742 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2743 | Size)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2744 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2745 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 2746 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 2747 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 2748 | // directly. |
| 2749 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 2750 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 2751 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | if (RT || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2755 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2756 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2757 | if (Size > 64 || !llvm::isPowerOf2_64(Size)) |
| 2758 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2759 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2760 | // Otherwise, coerce it to a small integer. |
| 2761 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | if (Ty->isPromotableIntegerType()) |
| 2765 | return ABIArgInfo::getExtend(); |
| 2766 | |
| 2767 | return ABIArgInfo::getDirect(); |
| 2768 | } |
| 2769 | |
| 2770 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2771 | if (!getCXXABI().classifyReturnType(FI)) |
| 2772 | FI.getReturnInfo() = classify(FI.getReturnType(), true); |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2773 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2774 | for (auto &I : FI.arguments()) |
| 2775 | I.info = classify(I.type, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2778 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2779 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2780 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2781 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2782 | CGBuilderTy &Builder = CGF.Builder; |
| 2783 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2784 | "ap"); |
| 2785 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2786 | llvm::Type *PTy = |
| 2787 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2788 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2789 | |
| 2790 | uint64_t Offset = |
| 2791 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2792 | llvm::Value *NextAddr = |
| 2793 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2794 | "ap.next"); |
| 2795 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2796 | |
| 2797 | return AddrTyped; |
| 2798 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2799 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2800 | namespace { |
| 2801 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2802 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2803 | public: |
| 2804 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2805 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2806 | void computeInfo(CGFunctionInfo &FI) const override; |
| 2807 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2808 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2809 | private: |
| 2810 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2811 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2812 | }; |
| 2813 | |
| 2814 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2815 | public: |
| 2816 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2817 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} |
| 2818 | }; |
| 2819 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2822 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2823 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2824 | PInfo.computeInfo(FI); |
| 2825 | else |
| 2826 | NInfo.computeInfo(FI); |
| 2827 | } |
| 2828 | |
| 2829 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2830 | CodeGenFunction &CGF) const { |
| 2831 | // Always use the native convention; calling pnacl-style varargs functions |
| 2832 | // is unuspported. |
| 2833 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2834 | } |
| 2835 | |
| 2836 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2837 | // PowerPC-32 |
| 2838 | |
| 2839 | namespace { |
| 2840 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2841 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2842 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2843 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2844 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2845 | // This is recovered from gcc output. |
| 2846 | return 1; // r1 is the dedicated stack pointer |
| 2847 | } |
| 2848 | |
| 2849 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2850 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2851 | }; |
| 2852 | |
| 2853 | } |
| 2854 | |
| 2855 | bool |
| 2856 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2857 | llvm::Value *Address) const { |
| 2858 | // This is calculated from the LLVM and GCC tables and verified |
| 2859 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2860 | |
| 2861 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2862 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2863 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2864 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2865 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2866 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2867 | |
| 2868 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2869 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2870 | |
| 2871 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2872 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2873 | |
| 2874 | // 64-76 are various 4-byte special-purpose registers: |
| 2875 | // 64: mq |
| 2876 | // 65: lr |
| 2877 | // 66: ctr |
| 2878 | // 67: ap |
| 2879 | // 68-75 cr0-7 |
| 2880 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2881 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2882 | |
| 2883 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2884 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2885 | |
| 2886 | // 109: vrsave |
| 2887 | // 110: vscr |
| 2888 | // 111: spe_acc |
| 2889 | // 112: spefscr |
| 2890 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2891 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2892 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2893 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2896 | // PowerPC-64 |
| 2897 | |
| 2898 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2899 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 2900 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 2901 | public: |
| 2902 | enum ABIKind { |
| 2903 | ELFv1 = 0, |
| 2904 | ELFv2 |
| 2905 | }; |
| 2906 | |
| 2907 | private: |
| 2908 | static const unsigned GPRBits = 64; |
| 2909 | ABIKind Kind; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2910 | |
| 2911 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 2912 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind) |
| 2913 | : DefaultABIInfo(CGT), Kind(Kind) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2914 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2915 | bool isPromotableTypeForABI(QualType Ty) const; |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 2916 | bool isAlignedParamType(QualType Ty) const; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 2917 | bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 2918 | uint64_t &Members) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2919 | |
| 2920 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2921 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2922 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2923 | // TODO: We can add more logic to computeInfo to improve performance. |
| 2924 | // Example: For aggregate arguments that fit in a register, we could |
| 2925 | // use getDirectInReg (as is done below for structs containing a single |
| 2926 | // floating-point value) to avoid pushing them to memory on function |
| 2927 | // entry. This would require changing the logic in PPCISelLowering |
| 2928 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2929 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 2930 | if (!getCXXABI().classifyReturnType(FI)) |
| 2931 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2932 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2933 | // We rely on the default argument classification for the most part. |
| 2934 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2935 | // 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] | 2936 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2937 | if (T) { |
| 2938 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 2939 | if ((T->isVectorType() && getContext().getTypeSize(T) == 128) || |
| 2940 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2941 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2942 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2943 | continue; |
| 2944 | } |
| 2945 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2946 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2947 | } |
| 2948 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2949 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2950 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2951 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2952 | }; |
| 2953 | |
| 2954 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2955 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 2956 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
| 2957 | PPC64_SVR4_ABIInfo::ABIKind Kind) |
| 2958 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2959 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2960 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2961 | // This is recovered from gcc output. |
| 2962 | return 1; // r1 is the dedicated stack pointer |
| 2963 | } |
| 2964 | |
| 2965 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2966 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2967 | }; |
| 2968 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2969 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2970 | public: |
| 2971 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 2972 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2973 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2974 | // This is recovered from gcc output. |
| 2975 | return 1; // r1 is the dedicated stack pointer |
| 2976 | } |
| 2977 | |
| 2978 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2979 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2980 | }; |
| 2981 | |
| 2982 | } |
| 2983 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2984 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 2985 | // extended to 64 bits. |
| 2986 | bool |
| 2987 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 2988 | // Treat an enum type as its underlying type. |
| 2989 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2990 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2991 | |
| 2992 | // Promotable integer types are required to be promoted by the ABI. |
| 2993 | if (Ty->isPromotableIntegerType()) |
| 2994 | return true; |
| 2995 | |
| 2996 | // In addition to the usual promotable integer types, we also need to |
| 2997 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 2998 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2999 | switch (BT->getKind()) { |
| 3000 | case BuiltinType::Int: |
| 3001 | case BuiltinType::UInt: |
| 3002 | return true; |
| 3003 | default: |
| 3004 | break; |
| 3005 | } |
| 3006 | |
| 3007 | return false; |
| 3008 | } |
| 3009 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3010 | /// isAlignedParamType - Determine whether a type requires 16-byte |
| 3011 | /// alignment in the parameter area. |
| 3012 | bool |
| 3013 | PPC64_SVR4_ABIInfo::isAlignedParamType(QualType Ty) const { |
| 3014 | // Complex types are passed just like their elements. |
| 3015 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 3016 | Ty = CTy->getElementType(); |
| 3017 | |
| 3018 | // Only vector types of size 16 bytes need alignment (larger types are |
| 3019 | // passed via reference, smaller types are not aligned). |
| 3020 | if (Ty->isVectorType()) |
| 3021 | return getContext().getTypeSize(Ty) == 128; |
| 3022 | |
| 3023 | // For single-element float/vector structs, we consider the whole type |
| 3024 | // to have the same alignment requirements as its single element. |
| 3025 | const Type *AlignAsType = nullptr; |
| 3026 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 3027 | if (EltType) { |
| 3028 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 3029 | if ((EltType->isVectorType() && |
| 3030 | getContext().getTypeSize(EltType) == 128) || |
| 3031 | (BT && BT->isFloatingPoint())) |
| 3032 | AlignAsType = EltType; |
| 3033 | } |
| 3034 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3035 | // Likewise for ELFv2 homogeneous aggregates. |
| 3036 | const Type *Base = nullptr; |
| 3037 | uint64_t Members = 0; |
| 3038 | if (!AlignAsType && Kind == ELFv2 && |
| 3039 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 3040 | AlignAsType = Base; |
| 3041 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3042 | // With special case aggregates, only vector base types need alignment. |
| 3043 | if (AlignAsType) |
| 3044 | return AlignAsType->isVectorType(); |
| 3045 | |
| 3046 | // Otherwise, we only need alignment for any aggregate type that |
| 3047 | // has an alignment requirement of >= 16 bytes. |
| 3048 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) |
| 3049 | return true; |
| 3050 | |
| 3051 | return false; |
| 3052 | } |
| 3053 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3054 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 3055 | /// aggregate. Base is set to the base element type, and Members is set |
| 3056 | /// to the number of base elements. |
| 3057 | bool |
| 3058 | PPC64_SVR4_ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3059 | uint64_t &Members) const { |
| 3060 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 3061 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 3062 | if (NElements == 0) |
| 3063 | return false; |
| 3064 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 3065 | return false; |
| 3066 | Members *= NElements; |
| 3067 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3068 | const RecordDecl *RD = RT->getDecl(); |
| 3069 | if (RD->hasFlexibleArrayMember()) |
| 3070 | return false; |
| 3071 | |
| 3072 | Members = 0; |
| 3073 | for (const auto *FD : RD->fields()) { |
| 3074 | // Ignore (non-zero arrays of) empty records. |
| 3075 | QualType FT = FD->getType(); |
| 3076 | while (const ConstantArrayType *AT = |
| 3077 | getContext().getAsConstantArrayType(FT)) { |
| 3078 | if (AT->getSize().getZExtValue() == 0) |
| 3079 | return false; |
| 3080 | FT = AT->getElementType(); |
| 3081 | } |
| 3082 | if (isEmptyRecord(getContext(), FT, true)) |
| 3083 | continue; |
| 3084 | |
| 3085 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 3086 | if (getContext().getLangOpts().CPlusPlus && |
| 3087 | FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 3088 | continue; |
| 3089 | |
| 3090 | uint64_t FldMembers; |
| 3091 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 3092 | return false; |
| 3093 | |
| 3094 | Members = (RD->isUnion() ? |
| 3095 | std::max(Members, FldMembers) : Members + FldMembers); |
| 3096 | } |
| 3097 | |
| 3098 | if (!Base) |
| 3099 | return false; |
| 3100 | |
| 3101 | // Ensure there is no padding. |
| 3102 | if (getContext().getTypeSize(Base) * Members != |
| 3103 | getContext().getTypeSize(Ty)) |
| 3104 | return false; |
| 3105 | } else { |
| 3106 | Members = 1; |
| 3107 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 3108 | Members = 2; |
| 3109 | Ty = CT->getElementType(); |
| 3110 | } |
| 3111 | |
| 3112 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 3113 | // double, long double, or 128-bit vectors. |
| 3114 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3115 | if (BT->getKind() != BuiltinType::Float && |
| 3116 | BT->getKind() != BuiltinType::Double && |
| 3117 | BT->getKind() != BuiltinType::LongDouble) |
| 3118 | return false; |
| 3119 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3120 | if (getContext().getTypeSize(VT) != 128) |
| 3121 | return false; |
| 3122 | } else { |
| 3123 | return false; |
| 3124 | } |
| 3125 | |
| 3126 | // The base type must be the same for all members. Types that |
| 3127 | // agree in both total size and mode (float vs. vector) are |
| 3128 | // treated as being equivalent here. |
| 3129 | const Type *TyPtr = Ty.getTypePtr(); |
| 3130 | if (!Base) |
| 3131 | Base = TyPtr; |
| 3132 | |
| 3133 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 3134 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 3135 | return false; |
| 3136 | } |
| 3137 | |
| 3138 | // Vector types require one register, floating point types require one |
| 3139 | // or two registers depending on their size. |
| 3140 | uint32_t NumRegs = Base->isVectorType() ? 1 : |
| 3141 | (getContext().getTypeSize(Base) + 63) / 64; |
| 3142 | |
| 3143 | // Homogeneous Aggregates may occupy at most 8 registers. |
| 3144 | return (Members > 0 && Members * NumRegs <= 8); |
| 3145 | } |
| 3146 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3147 | ABIArgInfo |
| 3148 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3149 | if (Ty->isAnyComplexType()) |
| 3150 | return ABIArgInfo::getDirect(); |
| 3151 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3152 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 3153 | // or via reference (larger than 16 bytes). |
| 3154 | if (Ty->isVectorType()) { |
| 3155 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3156 | if (Size > 128) |
| 3157 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3158 | else if (Size < 128) { |
| 3159 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3160 | return ABIArgInfo::getDirect(CoerceTy); |
| 3161 | } |
| 3162 | } |
| 3163 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3164 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3165 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3166 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3167 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3168 | uint64_t ABIAlign = isAlignedParamType(Ty)? 16 : 8; |
| 3169 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3170 | |
| 3171 | // ELFv2 homogeneous aggregates are passed as array types. |
| 3172 | const Type *Base = nullptr; |
| 3173 | uint64_t Members = 0; |
| 3174 | if (Kind == ELFv2 && |
| 3175 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 3176 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3177 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3178 | return ABIArgInfo::getDirect(CoerceTy); |
| 3179 | } |
| 3180 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 3181 | // If an aggregate may end up fully in registers, we do not |
| 3182 | // use the ByVal method, but pass the aggregate as array. |
| 3183 | // This is usually beneficial since we avoid forcing the |
| 3184 | // back-end to store the argument to memory. |
| 3185 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 3186 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 3187 | llvm::Type *CoerceTy; |
| 3188 | |
| 3189 | // Types up to 8 bytes are passed as integer type (which will be |
| 3190 | // properly aligned in the argument save area doubleword). |
| 3191 | if (Bits <= GPRBits) |
| 3192 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3193 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3194 | // Larger types are passed as arrays, with the base type selected |
| 3195 | // according to the required alignment in the save area. |
| 3196 | else { |
| 3197 | uint64_t RegBits = ABIAlign * 8; |
| 3198 | uint64_t NumRegs = llvm::RoundUpToAlignment(Bits, RegBits) / RegBits; |
| 3199 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 3200 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 3201 | } |
| 3202 | |
| 3203 | return ABIArgInfo::getDirect(CoerceTy); |
| 3204 | } |
| 3205 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3206 | // All other aggregates are passed ByVal. |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3207 | return ABIArgInfo::getIndirect(ABIAlign, /*ByVal=*/true, |
| 3208 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | return (isPromotableTypeForABI(Ty) ? |
| 3212 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3213 | } |
| 3214 | |
| 3215 | ABIArgInfo |
| 3216 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3217 | if (RetTy->isVoidType()) |
| 3218 | return ABIArgInfo::getIgnore(); |
| 3219 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3220 | if (RetTy->isAnyComplexType()) |
| 3221 | return ABIArgInfo::getDirect(); |
| 3222 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 3223 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 3224 | // or via reference (larger than 16 bytes). |
| 3225 | if (RetTy->isVectorType()) { |
| 3226 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3227 | if (Size > 128) |
| 3228 | return ABIArgInfo::getIndirect(0); |
| 3229 | else if (Size < 128) { |
| 3230 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 3231 | return ABIArgInfo::getDirect(CoerceTy); |
| 3232 | } |
| 3233 | } |
| 3234 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3235 | if (isAggregateTypeForABI(RetTy)) { |
| 3236 | // ELFv2 homogeneous aggregates are returned as array types. |
| 3237 | const Type *Base = nullptr; |
| 3238 | uint64_t Members = 0; |
| 3239 | if (Kind == ELFv2 && |
| 3240 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 3241 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 3242 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 3243 | return ABIArgInfo::getDirect(CoerceTy); |
| 3244 | } |
| 3245 | |
| 3246 | // ELFv2 small aggregates are returned in up to two registers. |
| 3247 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 3248 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 3249 | if (Bits == 0) |
| 3250 | return ABIArgInfo::getIgnore(); |
| 3251 | |
| 3252 | llvm::Type *CoerceTy; |
| 3253 | if (Bits > GPRBits) { |
| 3254 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
| 3255 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy, NULL); |
| 3256 | } else |
| 3257 | CoerceTy = llvm::IntegerType::get(getVMContext(), |
| 3258 | llvm::RoundUpToAlignment(Bits, 8)); |
| 3259 | return ABIArgInfo::getDirect(CoerceTy); |
| 3260 | } |
| 3261 | |
| 3262 | // All other aggregates are returned indirectly. |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3263 | return ABIArgInfo::getIndirect(0); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 3264 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3265 | |
| 3266 | return (isPromotableTypeForABI(RetTy) ? |
| 3267 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3268 | } |
| 3269 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3270 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3271 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3272 | QualType Ty, |
| 3273 | CodeGenFunction &CGF) const { |
| 3274 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3275 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3276 | |
| 3277 | CGBuilderTy &Builder = CGF.Builder; |
| 3278 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3279 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3280 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 3281 | // Handle types that require 16-byte alignment in the parameter save area. |
| 3282 | if (isAlignedParamType(Ty)) { |
| 3283 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3284 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(15)); |
| 3285 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt64(-16)); |
| 3286 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
| 3287 | } |
| 3288 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3289 | // Update the va_list pointer. The pointer should be bumped by the |
| 3290 | // size of the object. We can trust getTypeSize() except for a complex |
| 3291 | // type whose base type is smaller than a doubleword. For these, the |
| 3292 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3293 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3294 | QualType BaseTy; |
| 3295 | unsigned CplxBaseSize = 0; |
| 3296 | |
| 3297 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3298 | BaseTy = CTy->getElementType(); |
| 3299 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3300 | if (CplxBaseSize < 8) |
| 3301 | SizeInBytes = 16; |
| 3302 | } |
| 3303 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3304 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3305 | llvm::Value *NextAddr = |
| 3306 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3307 | "ap.next"); |
| 3308 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3309 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3310 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3311 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3312 | // in separate doublewords. However, Clang expects us to produce a |
| 3313 | // pointer to a structure with the two parts packed tightly. So generate |
| 3314 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3315 | // and store them to a temporary structure. |
| 3316 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3317 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3318 | llvm::Value *ImagAddr = RealAddr; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3319 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 3320 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3321 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3322 | } else { |
| 3323 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(8)); |
| 3324 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3325 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3326 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3327 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3328 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3329 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 3330 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 3331 | "vacplx"); |
| 3332 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 3333 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 3334 | Builder.CreateStore(Real, RealPtr, false); |
| 3335 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3336 | return Ptr; |
| 3337 | } |
| 3338 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3339 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3340 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3341 | // correct offset. |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 3342 | if (SizeInBytes < 8 && CGF.CGM.getDataLayout().isBigEndian()) { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3343 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3344 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3345 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3346 | } |
| 3347 | |
| 3348 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3349 | return Builder.CreateBitCast(Addr, PTy); |
| 3350 | } |
| 3351 | |
| 3352 | static bool |
| 3353 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3354 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3355 | // This is calculated from the LLVM and GCC tables and verified |
| 3356 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3357 | |
| 3358 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3359 | |
| 3360 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3361 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3362 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3363 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3364 | |
| 3365 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3366 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3367 | |
| 3368 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3369 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3370 | |
| 3371 | // 64-76 are various 4-byte special-purpose registers: |
| 3372 | // 64: mq |
| 3373 | // 65: lr |
| 3374 | // 66: ctr |
| 3375 | // 67: ap |
| 3376 | // 68-75 cr0-7 |
| 3377 | // 76: xer |
| 3378 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3379 | |
| 3380 | // 77-108: v0-31, the 16-byte vector registers |
| 3381 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3382 | |
| 3383 | // 109: vrsave |
| 3384 | // 110: vscr |
| 3385 | // 111: spe_acc |
| 3386 | // 112: spefscr |
| 3387 | // 113: sfp |
| 3388 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3389 | |
| 3390 | return false; |
| 3391 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3392 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3393 | bool |
| 3394 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3395 | CodeGen::CodeGenFunction &CGF, |
| 3396 | llvm::Value *Address) const { |
| 3397 | |
| 3398 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3399 | } |
| 3400 | |
| 3401 | bool |
| 3402 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3403 | llvm::Value *Address) const { |
| 3404 | |
| 3405 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3406 | } |
| 3407 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3408 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3409 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3410 | //===----------------------------------------------------------------------===// |
| 3411 | |
| 3412 | namespace { |
| 3413 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3414 | class AArch64ABIInfo : public ABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3415 | public: |
| 3416 | enum ABIKind { |
| 3417 | AAPCS = 0, |
| 3418 | DarwinPCS |
| 3419 | }; |
| 3420 | |
| 3421 | private: |
| 3422 | ABIKind Kind; |
| 3423 | |
| 3424 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3425 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3426 | |
| 3427 | private: |
| 3428 | ABIKind getABIKind() const { return Kind; } |
| 3429 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3430 | |
| 3431 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3432 | ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &AllocatedVFP, |
| 3433 | bool &IsHA, unsigned &AllocatedGPR, |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3434 | bool &IsSmallAggr, bool IsNamedArg) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3435 | bool isIllegalVectorType(QualType Ty) const; |
| 3436 | |
| 3437 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 3438 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
| 3439 | // number of SIMD and Floating-point registers allocated so far. |
| 3440 | // If the argument is an HFA or an HVA and there are sufficient unallocated |
| 3441 | // SIMD and Floating-point registers, then the argument is allocated to SIMD |
| 3442 | // and Floating-point Registers (with one register per member of the HFA or |
| 3443 | // HVA). Otherwise, the NSRN is set to 8. |
| 3444 | unsigned AllocatedVFP = 0; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3445 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3446 | // To correctly handle small aggregates, we need to keep track of the number |
| 3447 | // of GPRs allocated so far. If the small aggregate can't all fit into |
| 3448 | // registers, it will be on stack. We don't allow the aggregate to be |
| 3449 | // partially in registers. |
| 3450 | unsigned AllocatedGPR = 0; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3451 | |
| 3452 | // Find the number of named arguments. Variadic arguments get special |
| 3453 | // treatment with the Darwin ABI. |
| 3454 | unsigned NumRequiredArgs = (FI.isVariadic() ? |
| 3455 | FI.getRequiredArgs().getNumRequiredArgs() : |
| 3456 | FI.arg_size()); |
| 3457 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3458 | if (!getCXXABI().classifyReturnType(FI)) |
| 3459 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3460 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3461 | it != ie; ++it) { |
| 3462 | unsigned PreAllocation = AllocatedVFP, PreGPR = AllocatedGPR; |
| 3463 | bool IsHA = false, IsSmallAggr = false; |
| 3464 | const unsigned NumVFPs = 8; |
| 3465 | const unsigned NumGPRs = 8; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3466 | bool IsNamedArg = ((it - FI.arg_begin()) < |
| 3467 | static_cast<signed>(NumRequiredArgs)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3468 | it->info = classifyArgumentType(it->type, AllocatedVFP, IsHA, |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3469 | AllocatedGPR, IsSmallAggr, IsNamedArg); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3470 | |
| 3471 | // Under AAPCS the 64-bit stack slot alignment means we can't pass HAs |
| 3472 | // as sequences of floats since they'll get "holes" inserted as |
| 3473 | // padding by the back end. |
Tim Northover | 07f1624 | 2014-04-18 10:47:44 +0000 | [diff] [blame] | 3474 | if (IsHA && AllocatedVFP > NumVFPs && !isDarwinPCS() && |
| 3475 | getContext().getTypeAlign(it->type) < 64) { |
| 3476 | uint32_t NumStackSlots = getContext().getTypeSize(it->type); |
| 3477 | NumStackSlots = llvm::RoundUpToAlignment(NumStackSlots, 64) / 64; |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3478 | |
Tim Northover | 07f1624 | 2014-04-18 10:47:44 +0000 | [diff] [blame] | 3479 | llvm::Type *CoerceTy = llvm::ArrayType::get( |
| 3480 | llvm::Type::getDoubleTy(getVMContext()), NumStackSlots); |
| 3481 | it->info = ABIArgInfo::getDirect(CoerceTy); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3484 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3485 | // that are unallocated are marked as unavailable. To achieve this, we add |
| 3486 | // padding of (NumVFPs - PreAllocation) floats. |
| 3487 | if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { |
| 3488 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3489 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3490 | it->info.setPaddingType(PaddingTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3491 | } |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3492 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3493 | // If we do not have enough GPRs for the small aggregate, any GPR regs |
| 3494 | // that are unallocated are marked as unavailable. |
| 3495 | if (IsSmallAggr && AllocatedGPR > NumGPRs && PreGPR < NumGPRs) { |
| 3496 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3497 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreGPR); |
| 3498 | it->info = |
| 3499 | ABIArgInfo::getDirect(it->info.getCoerceToType(), 0, PaddingTy); |
| 3500 | } |
| 3501 | } |
| 3502 | } |
| 3503 | |
| 3504 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3505 | CodeGenFunction &CGF) const; |
| 3506 | |
| 3507 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3508 | CodeGenFunction &CGF) const; |
| 3509 | |
| 3510 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3511 | CodeGenFunction &CGF) const { |
| 3512 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3513 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3514 | } |
| 3515 | }; |
| 3516 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3517 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3518 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3519 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 3520 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3521 | |
| 3522 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
| 3523 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3524 | } |
| 3525 | |
| 3526 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; } |
| 3527 | |
| 3528 | virtual bool doesReturnSlotInterfereWithArgs() const { return false; } |
| 3529 | }; |
| 3530 | } |
| 3531 | |
| 3532 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3533 | ASTContext &Context, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3534 | uint64_t *HAMembers = nullptr); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3535 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3536 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty, |
| 3537 | unsigned &AllocatedVFP, |
| 3538 | bool &IsHA, |
| 3539 | unsigned &AllocatedGPR, |
| 3540 | bool &IsSmallAggr, |
| 3541 | bool IsNamedArg) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3542 | // Handle illegal vector types here. |
| 3543 | if (isIllegalVectorType(Ty)) { |
| 3544 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3545 | if (Size <= 32) { |
| 3546 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
| 3547 | AllocatedGPR++; |
| 3548 | return ABIArgInfo::getDirect(ResType); |
| 3549 | } |
| 3550 | if (Size == 64) { |
| 3551 | llvm::Type *ResType = |
| 3552 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
| 3553 | AllocatedVFP++; |
| 3554 | return ABIArgInfo::getDirect(ResType); |
| 3555 | } |
| 3556 | if (Size == 128) { |
| 3557 | llvm::Type *ResType = |
| 3558 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
| 3559 | AllocatedVFP++; |
| 3560 | return ABIArgInfo::getDirect(ResType); |
| 3561 | } |
| 3562 | AllocatedGPR++; |
| 3563 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3564 | } |
| 3565 | if (Ty->isVectorType()) |
| 3566 | // Size of a legal vector should be either 64 or 128. |
| 3567 | AllocatedVFP++; |
| 3568 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3569 | if (BT->getKind() == BuiltinType::Half || |
| 3570 | BT->getKind() == BuiltinType::Float || |
| 3571 | BT->getKind() == BuiltinType::Double || |
| 3572 | BT->getKind() == BuiltinType::LongDouble) |
| 3573 | AllocatedVFP++; |
| 3574 | } |
| 3575 | |
| 3576 | if (!isAggregateTypeForABI(Ty)) { |
| 3577 | // Treat an enum type as its underlying type. |
| 3578 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3579 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3580 | |
| 3581 | if (!Ty->isFloatingType() && !Ty->isVectorType()) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3582 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3583 | if (!isDarwinPCS() && Alignment > 64) |
| 3584 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3585 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3586 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 3587 | AllocatedGPR += RegsNeeded; |
| 3588 | } |
| 3589 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 3590 | ? ABIArgInfo::getExtend() |
| 3591 | : ABIArgInfo::getDirect()); |
| 3592 | } |
| 3593 | |
| 3594 | // Structures with either a non-trivial destructor or a non-trivial |
| 3595 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3596 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3597 | AllocatedGPR++; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3598 | return ABIArgInfo::getIndirect(0, /*ByVal=*/RAA == |
| 3599 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
| 3602 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 3603 | // elsewhere for GNU compatibility. |
| 3604 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3605 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 3606 | return ABIArgInfo::getIgnore(); |
| 3607 | |
| 3608 | ++AllocatedGPR; |
| 3609 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 3610 | } |
| 3611 | |
| 3612 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3613 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3614 | uint64_t Members = 0; |
| 3615 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3616 | IsHA = true; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3617 | if (!IsNamedArg && isDarwinPCS()) { |
| 3618 | // With the Darwin ABI, variadic arguments are always passed on the stack |
| 3619 | // and should not be expanded. Treat variadic HFAs as arrays of doubles. |
| 3620 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3621 | llvm::Type *BaseTy = llvm::Type::getDoubleTy(getVMContext()); |
| 3622 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 3623 | } |
| 3624 | AllocatedVFP += Members; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3625 | return ABIArgInfo::getExpand(); |
| 3626 | } |
| 3627 | |
| 3628 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 3629 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3630 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3631 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3632 | if (!isDarwinPCS() && Alignment > 64) |
| 3633 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3634 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3635 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3636 | AllocatedGPR += Size / 64; |
| 3637 | IsSmallAggr = true; |
| 3638 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 3639 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3640 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3641 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3642 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 3643 | } |
| 3644 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3645 | } |
| 3646 | |
| 3647 | AllocatedGPR++; |
| 3648 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3649 | } |
| 3650 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3651 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3652 | if (RetTy->isVoidType()) |
| 3653 | return ABIArgInfo::getIgnore(); |
| 3654 | |
| 3655 | // Large vector types should be returned via memory. |
| 3656 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 3657 | return ABIArgInfo::getIndirect(0); |
| 3658 | |
| 3659 | if (!isAggregateTypeForABI(RetTy)) { |
| 3660 | // Treat an enum type as its underlying type. |
| 3661 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3662 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3663 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 3664 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 3665 | ? ABIArgInfo::getExtend() |
| 3666 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3669 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 3670 | return ABIArgInfo::getIgnore(); |
| 3671 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3672 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3673 | if (isHomogeneousAggregate(RetTy, Base, getContext())) |
| 3674 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 3675 | return ABIArgInfo::getDirect(); |
| 3676 | |
| 3677 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 3678 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3679 | if (Size <= 128) { |
| 3680 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3681 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3682 | } |
| 3683 | |
| 3684 | return ABIArgInfo::getIndirect(0); |
| 3685 | } |
| 3686 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3687 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 3688 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3689 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3690 | // Check whether VT is legal. |
| 3691 | unsigned NumElements = VT->getNumElements(); |
| 3692 | uint64_t Size = getContext().getTypeSize(VT); |
| 3693 | // NumElements should be power of 2 between 1 and 16. |
| 3694 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 3695 | return true; |
| 3696 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 3697 | } |
| 3698 | return false; |
| 3699 | } |
| 3700 | |
| 3701 | static llvm::Value *EmitAArch64VAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3702 | int AllocatedGPR, int AllocatedVFP, |
| 3703 | bool IsIndirect, CodeGenFunction &CGF) { |
| 3704 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 3705 | // Standard, section B.4: |
| 3706 | // |
| 3707 | // struct { |
| 3708 | // void *__stack; |
| 3709 | // void *__gr_top; |
| 3710 | // void *__vr_top; |
| 3711 | // int __gr_offs; |
| 3712 | // int __vr_offs; |
| 3713 | // }; |
| 3714 | |
| 3715 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 3716 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3717 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 3718 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3719 | auto &Ctx = CGF.getContext(); |
| 3720 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3721 | llvm::Value *reg_offs_p = nullptr, *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3722 | int reg_top_index; |
| 3723 | int RegSize; |
| 3724 | if (AllocatedGPR) { |
| 3725 | assert(!AllocatedVFP && "Arguments never split between int & VFP regs"); |
| 3726 | // 3 is the field number of __gr_offs |
| 3727 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 3728 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 3729 | reg_top_index = 1; // field number for __gr_top |
| 3730 | RegSize = 8 * AllocatedGPR; |
| 3731 | } else { |
| 3732 | assert(!AllocatedGPR && "Argument must go in VFP or int regs"); |
| 3733 | // 4 is the field number of __vr_offs. |
| 3734 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 3735 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 3736 | reg_top_index = 2; // field number for __vr_top |
| 3737 | RegSize = 16 * AllocatedVFP; |
| 3738 | } |
| 3739 | |
| 3740 | //======================================= |
| 3741 | // Find out where argument was passed |
| 3742 | //======================================= |
| 3743 | |
| 3744 | // If reg_offs >= 0 we're already using the stack for this type of |
| 3745 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 3746 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 3747 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3748 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3749 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 3750 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 3751 | |
| 3752 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 3753 | |
| 3754 | // 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] | 3755 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3756 | CGF.EmitBlock(MaybeRegBlock); |
| 3757 | |
| 3758 | // Integer arguments may need to correct register alignment (for example a |
| 3759 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 3760 | // align __gr_offs to calculate the potential address. |
| 3761 | if (AllocatedGPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3762 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3763 | |
| 3764 | reg_offs = CGF.Builder.CreateAdd( |
| 3765 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 3766 | "align_regoffs"); |
| 3767 | reg_offs = CGF.Builder.CreateAnd( |
| 3768 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 3769 | "aligned_regoffs"); |
| 3770 | } |
| 3771 | |
| 3772 | // 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] | 3773 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3774 | NewOffset = CGF.Builder.CreateAdd( |
| 3775 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 3776 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 3777 | |
| 3778 | // Now we're in a position to decide whether this argument really was in |
| 3779 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3780 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3781 | InRegs = CGF.Builder.CreateICmpSLE( |
| 3782 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 3783 | |
| 3784 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 3785 | |
| 3786 | //======================================= |
| 3787 | // Argument was in registers |
| 3788 | //======================================= |
| 3789 | |
| 3790 | // Now we emit the code for if the argument was originally passed in |
| 3791 | // registers. First start the appropriate block: |
| 3792 | CGF.EmitBlock(InRegBlock); |
| 3793 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3794 | llvm::Value *reg_top_p = nullptr, *reg_top = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3795 | reg_top_p = |
| 3796 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 3797 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 3798 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3799 | llvm::Value *RegAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3800 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 3801 | |
| 3802 | if (IsIndirect) { |
| 3803 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 3804 | // stored registers or on the stack will actually be a struct **. |
| 3805 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 3806 | } |
| 3807 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3808 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3809 | uint64_t NumMembers; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 3810 | bool IsHFA = isHomogeneousAggregate(Ty, Base, Ctx, &NumMembers); |
| 3811 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3812 | // Homogeneous aggregates passed in registers will have their elements split |
| 3813 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 3814 | // qN+1, ...). We reload and store into a temporary local variable |
| 3815 | // contiguously. |
| 3816 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 3817 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 3818 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 3819 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 3820 | int Offset = 0; |
| 3821 | |
| 3822 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 3823 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 3824 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 3825 | llvm::Value *BaseOffset = |
| 3826 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 3827 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 3828 | LoadAddr = CGF.Builder.CreateBitCast( |
| 3829 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
| 3830 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 3831 | |
| 3832 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 3833 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 3834 | } |
| 3835 | |
| 3836 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 3837 | } else { |
| 3838 | // Otherwise the object is contiguous in memory |
| 3839 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 3840 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 3841 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3842 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 3843 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 3844 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 3845 | |
| 3846 | BaseAddr = CGF.Builder.CreateAdd( |
| 3847 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 3848 | |
| 3849 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 3850 | } |
| 3851 | |
| 3852 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 3853 | } |
| 3854 | |
| 3855 | CGF.EmitBranch(ContBlock); |
| 3856 | |
| 3857 | //======================================= |
| 3858 | // Argument was on the stack |
| 3859 | //======================================= |
| 3860 | CGF.EmitBlock(OnStackBlock); |
| 3861 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3862 | llvm::Value *stack_p = nullptr, *OnStackAddr = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3863 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 3864 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 3865 | |
| 3866 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 3867 | // floating-point ones might be affected. |
| 3868 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3869 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3870 | |
| 3871 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3872 | |
| 3873 | OnStackAddr = CGF.Builder.CreateAdd( |
| 3874 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 3875 | "align_stack"); |
| 3876 | OnStackAddr = CGF.Builder.CreateAnd( |
| 3877 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 3878 | "align_stack"); |
| 3879 | |
| 3880 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3881 | } |
| 3882 | |
| 3883 | uint64_t StackSize; |
| 3884 | if (IsIndirect) |
| 3885 | StackSize = 8; |
| 3886 | else |
| 3887 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 3888 | |
| 3889 | // All stack slots are 8 bytes |
| 3890 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 3891 | |
| 3892 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 3893 | llvm::Value *NewStack = |
| 3894 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 3895 | |
| 3896 | // Write the new value of __stack for the next call to va_arg |
| 3897 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 3898 | |
| 3899 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 3900 | Ctx.getTypeSize(Ty) < 64) { |
| 3901 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 3902 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3903 | |
| 3904 | OnStackAddr = CGF.Builder.CreateAdd( |
| 3905 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 3906 | |
| 3907 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3908 | } |
| 3909 | |
| 3910 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 3911 | |
| 3912 | CGF.EmitBranch(ContBlock); |
| 3913 | |
| 3914 | //======================================= |
| 3915 | // Tidy up |
| 3916 | //======================================= |
| 3917 | CGF.EmitBlock(ContBlock); |
| 3918 | |
| 3919 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 3920 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 3921 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 3922 | |
| 3923 | if (IsIndirect) |
| 3924 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 3925 | |
| 3926 | return ResAddr; |
| 3927 | } |
| 3928 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3929 | llvm::Value *AArch64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3930 | CodeGenFunction &CGF) const { |
| 3931 | |
| 3932 | unsigned AllocatedGPR = 0, AllocatedVFP = 0; |
| 3933 | bool IsHA = false, IsSmallAggr = false; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3934 | ABIArgInfo AI = classifyArgumentType(Ty, AllocatedVFP, IsHA, AllocatedGPR, |
| 3935 | IsSmallAggr, false /*IsNamedArg*/); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3936 | |
| 3937 | return EmitAArch64VAArg(VAListAddr, Ty, AllocatedGPR, AllocatedVFP, |
| 3938 | AI.isIndirect(), CGF); |
| 3939 | } |
| 3940 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 3941 | llvm::Value *AArch64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3942 | CodeGenFunction &CGF) const { |
| 3943 | // We do not support va_arg for aggregates or illegal vector types. |
| 3944 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 3945 | // other cases. |
| 3946 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3947 | return nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3948 | |
| 3949 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 3950 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 3951 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3952 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3953 | bool isHA = isHomogeneousAggregate(Ty, Base, getContext()); |
| 3954 | |
| 3955 | bool isIndirect = false; |
| 3956 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 3957 | // be passed indirectly. |
| 3958 | if (Size > 16 && !isHA) { |
| 3959 | isIndirect = true; |
| 3960 | Size = 8; |
| 3961 | Align = 8; |
| 3962 | } |
| 3963 | |
| 3964 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 3965 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 3966 | |
| 3967 | CGBuilderTy &Builder = CGF.Builder; |
| 3968 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3969 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3970 | |
| 3971 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3972 | // These are ignored for parameter passing purposes. |
| 3973 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3974 | return Builder.CreateBitCast(Addr, PTy); |
| 3975 | } |
| 3976 | |
| 3977 | const uint64_t MinABIAlign = 8; |
| 3978 | if (Align > MinABIAlign) { |
| 3979 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 3980 | Addr = Builder.CreateGEP(Addr, Offset); |
| 3981 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3982 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 3983 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 3984 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 3985 | } |
| 3986 | |
| 3987 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 3988 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 3989 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 3990 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3991 | |
| 3992 | if (isIndirect) |
| 3993 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 3994 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3995 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3996 | |
| 3997 | return AddrTyped; |
| 3998 | } |
| 3999 | |
| 4000 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4001 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4002 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4003 | |
| 4004 | namespace { |
| 4005 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4006 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4007 | public: |
| 4008 | enum ABIKind { |
| 4009 | APCS = 0, |
| 4010 | AAPCS = 1, |
| 4011 | AAPCS_VFP |
| 4012 | }; |
| 4013 | |
| 4014 | private: |
| 4015 | ABIKind Kind; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4016 | mutable int VFPRegs[16]; |
| 4017 | const unsigned NumVFPs; |
| 4018 | const unsigned NumGPRs; |
| 4019 | mutable unsigned AllocatedGPRs; |
| 4020 | mutable unsigned AllocatedVFPs; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4021 | |
| 4022 | public: |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4023 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind), |
| 4024 | NumVFPs(16), NumGPRs(4) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4025 | setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4026 | resetAllocatedRegs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4027 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4028 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4029 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4030 | switch (getTarget().getTriple().getEnvironment()) { |
| 4031 | case llvm::Triple::Android: |
| 4032 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4033 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4034 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 4035 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 4036 | return true; |
| 4037 | default: |
| 4038 | return false; |
| 4039 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4042 | bool isEABIHF() const { |
| 4043 | switch (getTarget().getTriple().getEnvironment()) { |
| 4044 | case llvm::Triple::EABIHF: |
| 4045 | case llvm::Triple::GNUEABIHF: |
| 4046 | return true; |
| 4047 | default: |
| 4048 | return false; |
| 4049 | } |
| 4050 | } |
| 4051 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4052 | ABIKind getABIKind() const { return Kind; } |
| 4053 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4054 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4055 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4056 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4057 | bool &IsCPRC) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4058 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4059 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4060 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4061 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4062 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4063 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4064 | |
| 4065 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 4066 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 4067 | void setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4068 | |
| 4069 | void markAllocatedGPRs(unsigned Alignment, unsigned NumRequired) const; |
| 4070 | void markAllocatedVFPs(unsigned Alignment, unsigned NumRequired) const; |
| 4071 | void resetAllocatedRegs(void) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4072 | }; |
| 4073 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4074 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4075 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 4076 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 4077 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4078 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4079 | const ARMABIInfo &getABIInfo() const { |
| 4080 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4081 | } |
| 4082 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4083 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 4084 | return 13; |
| 4085 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4086 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4087 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4088 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 4089 | } |
| 4090 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4091 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4092 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4093 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4094 | |
| 4095 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4096 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 4097 | return false; |
| 4098 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4099 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4100 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 4101 | if (getABIInfo().isEABI()) return 88; |
| 4102 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 4103 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4104 | |
| 4105 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4106 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 4107 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4108 | if (!FD) |
| 4109 | return; |
| 4110 | |
| 4111 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 4112 | if (!Attr) |
| 4113 | return; |
| 4114 | |
| 4115 | const char *Kind; |
| 4116 | switch (Attr->getInterrupt()) { |
| 4117 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 4118 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 4119 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 4120 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 4121 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 4122 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 4123 | } |
| 4124 | |
| 4125 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 4126 | |
| 4127 | Fn->addFnAttr("interrupt", Kind); |
| 4128 | |
| 4129 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 4130 | return; |
| 4131 | |
| 4132 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 4133 | // however this is not necessarily true on taking any interrupt. Instruct |
| 4134 | // the backend to perform a realignment as part of the function prologue. |
| 4135 | llvm::AttrBuilder B; |
| 4136 | B.addStackAlignmentAttr(8); |
| 4137 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 4138 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 4139 | llvm::AttributeSet::FunctionIndex, |
| 4140 | B)); |
| 4141 | } |
| 4142 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4143 | }; |
| 4144 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 4147 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4148 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4149 | // VFP registers allocated so far. |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4150 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 4151 | // VFP registers of the appropriate type unallocated then the argument is |
| 4152 | // allocated to the lowest-numbered sequence of such registers. |
| 4153 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 4154 | // unallocated are marked as unavailable. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4155 | resetAllocatedRegs(); |
| 4156 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4157 | if (getCXXABI().classifyReturnType(FI)) { |
| 4158 | if (FI.getReturnInfo().isIndirect()) |
| 4159 | markAllocatedGPRs(1, 1); |
| 4160 | } else { |
| 4161 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
| 4162 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4163 | for (auto &I : FI.arguments()) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4164 | unsigned PreAllocationVFPs = AllocatedVFPs; |
| 4165 | unsigned PreAllocationGPRs = AllocatedGPRs; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4166 | bool IsCPRC = false; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4167 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 4168 | // s0-s15 (d0-d7) for passing arguments. |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4169 | I.info = classifyArgumentType(I.type, FI.isVariadic(), IsCPRC); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4170 | |
| 4171 | // If we have allocated some arguments onto the stack (due to running |
| 4172 | // out of VFP registers), we cannot split an argument between GPRs and |
| 4173 | // the stack. If this situation occurs, we add padding to prevent the |
Oliver Stannard | a3afc69 | 2014-05-19 13:10:05 +0000 | [diff] [blame] | 4174 | // GPRs from being used. In this situation, the current argument could |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4175 | // only be allocated by rule C.8, so rule C.6 would mark these GPRs as |
| 4176 | // unusable anyway. |
Oliver Stannard | e022851 | 2014-07-18 09:09:31 +0000 | [diff] [blame] | 4177 | // We do not have to do this if the argument is being passed ByVal, as the |
| 4178 | // backend can handle that situation correctly. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4179 | const bool StackUsed = PreAllocationGPRs > NumGPRs || PreAllocationVFPs > NumVFPs; |
Oliver Stannard | e022851 | 2014-07-18 09:09:31 +0000 | [diff] [blame] | 4180 | const bool IsByVal = I.info.isIndirect() && I.info.getIndirectByVal(); |
| 4181 | if (!IsCPRC && PreAllocationGPRs < NumGPRs && AllocatedGPRs > NumGPRs && |
| 4182 | StackUsed && !IsByVal) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4183 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 4184 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreAllocationGPRs); |
Oliver Stannard | a3afc69 | 2014-05-19 13:10:05 +0000 | [diff] [blame] | 4185 | if (I.info.canHaveCoerceToType()) { |
| 4186 | I.info = ABIArgInfo::getDirect(I.info.getCoerceToType() /* type */, 0 /* offset */, |
| 4187 | PaddingTy); |
| 4188 | } else { |
| 4189 | I.info = ABIArgInfo::getDirect(nullptr /* type */, 0 /* offset */, |
| 4190 | PaddingTy); |
| 4191 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4192 | } |
| 4193 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4194 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4195 | // Always honor user-specified calling convention. |
| 4196 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4197 | return; |
| 4198 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4199 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 4200 | if (cc != llvm::CallingConv::C) |
| 4201 | FI.setEffectiveCallingConvention(cc); |
| 4202 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 4203 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4204 | /// Return the default calling convention that LLVM will use. |
| 4205 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 4206 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 4207 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4208 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 4209 | else if (isEABI()) |
| 4210 | return llvm::CallingConv::ARM_AAPCS; |
| 4211 | else |
| 4212 | return llvm::CallingConv::ARM_APCS; |
| 4213 | } |
| 4214 | |
| 4215 | /// Return the calling convention that our ABI would like us to use |
| 4216 | /// as the C calling convention. |
| 4217 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4218 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4219 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 4220 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 4221 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 4222 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4223 | llvm_unreachable("bad ABI kind"); |
| 4224 | } |
| 4225 | |
| 4226 | void ARMABIInfo::setRuntimeCC() { |
| 4227 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 4228 | |
| 4229 | // Don't muddy up the IR with a ton of explicit annotations if |
| 4230 | // they'd just match what LLVM will infer from the triple. |
| 4231 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 4232 | if (abiCC != getLLVMDefaultCC()) |
| 4233 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4234 | } |
| 4235 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4236 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 4237 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 4238 | /// contained in the type is returned through it; this is used for the |
| 4239 | /// recursive calls that check aggregate component types. |
| 4240 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4241 | ASTContext &Context, uint64_t *HAMembers) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4242 | uint64_t Members = 0; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4243 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 4244 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 4245 | return false; |
| 4246 | Members *= AT->getSize().getZExtValue(); |
| 4247 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4248 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4249 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4250 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4251 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4252 | Members = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 4253 | for (const auto *FD : RD->fields()) { |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4254 | uint64_t FldMembers; |
| 4255 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 4256 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4257 | |
| 4258 | Members = (RD->isUnion() ? |
| 4259 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4260 | } |
| 4261 | } else { |
| 4262 | Members = 1; |
| 4263 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4264 | Members = 2; |
| 4265 | Ty = CT->getElementType(); |
| 4266 | } |
| 4267 | |
| 4268 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4269 | // double, or 64-bit or 128-bit vectors. |
| 4270 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4271 | if (BT->getKind() != BuiltinType::Float && |
Tim Northover | eb752d4 | 2012-07-20 22:29:29 +0000 | [diff] [blame] | 4272 | BT->getKind() != BuiltinType::Double && |
| 4273 | BT->getKind() != BuiltinType::LongDouble) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4274 | return false; |
| 4275 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4276 | unsigned VecSize = Context.getTypeSize(VT); |
| 4277 | if (VecSize != 64 && VecSize != 128) |
| 4278 | return false; |
| 4279 | } else { |
| 4280 | return false; |
| 4281 | } |
| 4282 | |
| 4283 | // The base type must be the same for all members. Vector types of the |
| 4284 | // same total size are treated as being equivalent here. |
| 4285 | const Type *TyPtr = Ty.getTypePtr(); |
| 4286 | if (!Base) |
| 4287 | Base = TyPtr; |
Oliver Stannard | 5e8558f | 2014-02-07 11:25:57 +0000 | [diff] [blame] | 4288 | |
| 4289 | if (Base != TyPtr) { |
| 4290 | // Homogeneous aggregates are defined as containing members with the |
| 4291 | // same machine type. There are two cases in which two members have |
| 4292 | // different TypePtrs but the same machine type: |
| 4293 | |
| 4294 | // 1) Vectors of the same length, regardless of the type and number |
| 4295 | // of their members. |
| 4296 | const bool SameLengthVectors = Base->isVectorType() && TyPtr->isVectorType() |
| 4297 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4298 | |
| 4299 | // 2) In the 32-bit AAPCS, `double' and `long double' have the same |
| 4300 | // machine type. This is not the case for the 64-bit AAPCS. |
| 4301 | const bool SameSizeDoubles = |
| 4302 | ( ( Base->isSpecificBuiltinType(BuiltinType::Double) |
| 4303 | && TyPtr->isSpecificBuiltinType(BuiltinType::LongDouble)) |
| 4304 | || ( Base->isSpecificBuiltinType(BuiltinType::LongDouble) |
| 4305 | && TyPtr->isSpecificBuiltinType(BuiltinType::Double))) |
| 4306 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4307 | |
| 4308 | if (!SameLengthVectors && !SameSizeDoubles) |
| 4309 | return false; |
| 4310 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 4314 | if (HAMembers) |
| 4315 | *HAMembers = Members; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4316 | |
| 4317 | return (Members > 0 && Members <= 4); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4318 | } |
| 4319 | |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4320 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 4321 | /// number of VFP registers (unit is S register) requested. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4322 | void ARMABIInfo::markAllocatedVFPs(unsigned Alignment, |
| 4323 | unsigned NumRequired) const { |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4324 | // Early Exit. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4325 | if (AllocatedVFPs >= 16) { |
| 4326 | // We use AllocatedVFP > 16 to signal that some CPRCs were allocated on |
| 4327 | // the stack. |
| 4328 | AllocatedVFPs = 17; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4329 | return; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4330 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4331 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 4332 | // VFP registers of the appropriate type unallocated then the argument is |
| 4333 | // allocated to the lowest-numbered sequence of such registers. |
| 4334 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 4335 | bool FoundSlot = true; |
| 4336 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4337 | if (J >= 16 || VFPRegs[J]) { |
| 4338 | FoundSlot = false; |
| 4339 | break; |
| 4340 | } |
| 4341 | if (FoundSlot) { |
| 4342 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4343 | VFPRegs[J] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4344 | AllocatedVFPs += NumRequired; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4345 | return; |
| 4346 | } |
| 4347 | } |
| 4348 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 4349 | // unallocated are marked as unavailable. |
| 4350 | for (unsigned I = 0; I < 16; I++) |
| 4351 | VFPRegs[I] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4352 | AllocatedVFPs = 17; // We do not have enough VFP registers. |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4355 | /// Update AllocatedGPRs to record the number of general purpose registers |
| 4356 | /// which have been allocated. It is valid for AllocatedGPRs to go above 4, |
| 4357 | /// this represents arguments being stored on the stack. |
| 4358 | void ARMABIInfo::markAllocatedGPRs(unsigned Alignment, |
Oliver Stannard | 3f32b9b | 2014-06-27 13:59:27 +0000 | [diff] [blame] | 4359 | unsigned NumRequired) const { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4360 | assert((Alignment == 1 || Alignment == 2) && "Alignment must be 4 or 8 bytes"); |
| 4361 | |
| 4362 | if (Alignment == 2 && AllocatedGPRs & 0x1) |
| 4363 | AllocatedGPRs += 1; |
| 4364 | |
| 4365 | AllocatedGPRs += NumRequired; |
| 4366 | } |
| 4367 | |
| 4368 | void ARMABIInfo::resetAllocatedRegs(void) const { |
| 4369 | AllocatedGPRs = 0; |
| 4370 | AllocatedVFPs = 0; |
| 4371 | for (unsigned i = 0; i < NumVFPs; ++i) |
| 4372 | VFPRegs[i] = 0; |
| 4373 | } |
| 4374 | |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4375 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4376 | bool &IsCPRC) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4377 | // We update number of allocated VFPs according to |
| 4378 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4379 | // A single-precision floating-point type (including promoted |
| 4380 | // half-precision types); A double-precision floating-point type; |
| 4381 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4382 | // with a Base Type of a single- or double-precision floating-point type, |
| 4383 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4384 | // to four Elements. |
| 4385 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4386 | // Handle illegal vector types here. |
| 4387 | if (isIllegalVectorType(Ty)) { |
| 4388 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4389 | if (Size <= 32) { |
| 4390 | llvm::Type *ResType = |
| 4391 | llvm::Type::getInt32Ty(getVMContext()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4392 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4393 | return ABIArgInfo::getDirect(ResType); |
| 4394 | } |
| 4395 | if (Size == 64) { |
| 4396 | llvm::Type *ResType = llvm::VectorType::get( |
| 4397 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4398 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic){ |
| 4399 | markAllocatedGPRs(2, 2); |
| 4400 | } else { |
| 4401 | markAllocatedVFPs(2, 2); |
| 4402 | IsCPRC = true; |
| 4403 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4404 | return ABIArgInfo::getDirect(ResType); |
| 4405 | } |
| 4406 | if (Size == 128) { |
| 4407 | llvm::Type *ResType = llvm::VectorType::get( |
| 4408 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4409 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic) { |
| 4410 | markAllocatedGPRs(2, 4); |
| 4411 | } else { |
| 4412 | markAllocatedVFPs(4, 4); |
| 4413 | IsCPRC = true; |
| 4414 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4415 | return ABIArgInfo::getDirect(ResType); |
| 4416 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4417 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4418 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4419 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4420 | // Update VFPRegs for legal vector types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4421 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4422 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4423 | uint64_t Size = getContext().getTypeSize(VT); |
| 4424 | // Size of a legal vector should be power of 2 and above 64. |
| 4425 | markAllocatedVFPs(Size >= 128 ? 4 : 2, Size / 32); |
| 4426 | IsCPRC = true; |
| 4427 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4428 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4429 | // Update VFPRegs for floating point types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4430 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4431 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4432 | if (BT->getKind() == BuiltinType::Half || |
| 4433 | BT->getKind() == BuiltinType::Float) { |
| 4434 | markAllocatedVFPs(1, 1); |
| 4435 | IsCPRC = true; |
| 4436 | } |
| 4437 | if (BT->getKind() == BuiltinType::Double || |
| 4438 | BT->getKind() == BuiltinType::LongDouble) { |
| 4439 | markAllocatedVFPs(2, 2); |
| 4440 | IsCPRC = true; |
| 4441 | } |
| 4442 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4443 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4444 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4445 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4446 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4447 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4448 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4449 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4450 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4451 | unsigned Size = getContext().getTypeSize(Ty); |
| 4452 | if (!IsCPRC) |
| 4453 | markAllocatedGPRs(Size > 32 ? 2 : 1, (Size + 31) / 32); |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 4454 | return (Ty->isPromotableIntegerType() ? |
| 4455 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4456 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4457 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4458 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 4459 | markAllocatedGPRs(1, 1); |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4460 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4461 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4462 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4463 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4464 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4465 | return ABIArgInfo::getIgnore(); |
| 4466 | |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4467 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4468 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4469 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4470 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4471 | uint64_t Members = 0; |
| 4472 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4473 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4474 | // Base can be a floating-point or a vector. |
| 4475 | if (Base->isVectorType()) { |
| 4476 | // ElementSize is in number of floats. |
| 4477 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4478 | markAllocatedVFPs(ElementSize, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4479 | Members * ElementSize); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4480 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4481 | markAllocatedVFPs(1, Members); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4482 | else { |
| 4483 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 4484 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4485 | markAllocatedVFPs(2, Members * 2); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4486 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4487 | IsCPRC = true; |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4488 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4489 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4492 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4493 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4494 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4495 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4496 | uint64_t ABIAlign = 4; |
| 4497 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4498 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4499 | getABIKind() == ARMABIInfo::AAPCS) |
| 4500 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4501 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Oliver Stannard | 3f32b9b | 2014-06-27 13:59:27 +0000 | [diff] [blame] | 4502 | // Update Allocated GPRs. Since this is only used when the size of the |
| 4503 | // argument is greater than 64 bytes, this will always use up any available |
| 4504 | // registers (of which there are 4). We also don't care about getting the |
| 4505 | // alignment right, because general-purpose registers cannot be back-filled. |
| 4506 | markAllocatedGPRs(1, 4); |
Oliver Stannard | 7c3c09e | 2014-03-12 14:02:50 +0000 | [diff] [blame] | 4507 | return ABIArgInfo::getIndirect(TyAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4508 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4511 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4512 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4513 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4514 | // FIXME: Try to match the types of the arguments more accurately where |
| 4515 | // we can. |
| 4516 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4517 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4518 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4519 | markAllocatedGPRs(1, SizeRegs); |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4520 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4521 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4522 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4523 | markAllocatedGPRs(2, SizeRegs * 2); |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4524 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4525 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4526 | llvm::Type *STy = |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 4527 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4528 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4531 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4532 | llvm::LLVMContext &VMContext) { |
| 4533 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4534 | // is called integer-like if its size is less than or equal to one word, and |
| 4535 | // the offset of each of its addressable sub-fields is zero. |
| 4536 | |
| 4537 | uint64_t Size = Context.getTypeSize(Ty); |
| 4538 | |
| 4539 | // Check that the type fits in a word. |
| 4540 | if (Size > 32) |
| 4541 | return false; |
| 4542 | |
| 4543 | // FIXME: Handle vector types! |
| 4544 | if (Ty->isVectorType()) |
| 4545 | return false; |
| 4546 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4547 | // Float types are never treated as "integer like". |
| 4548 | if (Ty->isRealFloatingType()) |
| 4549 | return false; |
| 4550 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4551 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4552 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4553 | return true; |
| 4554 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4555 | // Small complex integer types are "integer like". |
| 4556 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4557 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4558 | |
| 4559 | // Single element and zero sized arrays should be allowed, by the definition |
| 4560 | // above, but they are not. |
| 4561 | |
| 4562 | // Otherwise, it must be a record type. |
| 4563 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4564 | if (!RT) return false; |
| 4565 | |
| 4566 | // Ignore records with flexible arrays. |
| 4567 | const RecordDecl *RD = RT->getDecl(); |
| 4568 | if (RD->hasFlexibleArrayMember()) |
| 4569 | return false; |
| 4570 | |
| 4571 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4572 | // like". |
| 4573 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4574 | |
| 4575 | bool HadField = false; |
| 4576 | unsigned idx = 0; |
| 4577 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4578 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4579 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4580 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4581 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4582 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4583 | // struct { int : 0; int x } |
| 4584 | // is non-integer like according to gcc. |
| 4585 | if (FD->isBitField()) { |
| 4586 | if (!RD->isUnion()) |
| 4587 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4588 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4589 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4590 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4591 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4592 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4595 | // Check if this field is at offset 0. |
| 4596 | if (Layout.getFieldOffset(idx) != 0) |
| 4597 | return false; |
| 4598 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4599 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4600 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4601 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4602 | // Only allow at most one field in a structure. This doesn't match the |
| 4603 | // wording above, but follows gcc in situations with a field following an |
| 4604 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4605 | if (!RD->isUnion()) { |
| 4606 | if (HadField) |
| 4607 | return false; |
| 4608 | |
| 4609 | HadField = true; |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | return true; |
| 4614 | } |
| 4615 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4616 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4617 | bool isVariadic) const { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4618 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4619 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4620 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4621 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4622 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
| 4623 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4624 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4625 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4626 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4627 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4628 | // Treat an enum type as its underlying type. |
| 4629 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4630 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4631 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 4632 | return (RetTy->isPromotableIntegerType() ? |
| 4633 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4634 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4635 | |
| 4636 | // Are we following APCS? |
| 4637 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4638 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4639 | return ABIArgInfo::getIgnore(); |
| 4640 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4641 | // Complex types are all returned as packed integers. |
| 4642 | // |
| 4643 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4644 | // correctly. |
| 4645 | if (RetTy->isAnyComplexType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4646 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4647 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4648 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4649 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4650 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4651 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4652 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4653 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4654 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4655 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4656 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4657 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| 4660 | // Otherwise return in memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4661 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4662 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4663 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4664 | |
| 4665 | // Otherwise this is an AAPCS variant. |
| 4666 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4667 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4668 | return ABIArgInfo::getIgnore(); |
| 4669 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4670 | // Check for homogeneous aggregates with AAPCS-VFP. |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4671 | if (getABIKind() == AAPCS_VFP && !isVariadic) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4672 | const Type *Base = nullptr; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4673 | if (isHomogeneousAggregate(RetTy, Base, getContext())) { |
| 4674 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4675 | // Homogeneous Aggregates are returned directly. |
| 4676 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4677 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4680 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4681 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4682 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4683 | if (Size <= 32) { |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 4684 | if (getDataLayout().isBigEndian()) |
| 4685 | // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) |
| 4686 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 4687 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4688 | // Return in the smallest viable integer type. |
| 4689 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4690 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4691 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4692 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4693 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4696 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4697 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4700 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4701 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4702 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4703 | // Check whether VT is legal. |
| 4704 | unsigned NumElements = VT->getNumElements(); |
| 4705 | uint64_t Size = getContext().getTypeSize(VT); |
| 4706 | // NumElements should be power of 2. |
| 4707 | if ((NumElements & (NumElements - 1)) != 0) |
| 4708 | return true; |
| 4709 | // Size should be greater than 32 bits. |
| 4710 | return Size <= 32; |
| 4711 | } |
| 4712 | return false; |
| 4713 | } |
| 4714 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4715 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4716 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4717 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4718 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4719 | |
| 4720 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4721 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4722 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4723 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4724 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4725 | // These are ignored for parameter passing purposes. |
| 4726 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4727 | return Builder.CreateBitCast(Addr, PTy); |
| 4728 | } |
| 4729 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4730 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4731 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4732 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4733 | |
| 4734 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4735 | // 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] | 4736 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4737 | getABIKind() == ARMABIInfo::AAPCS) |
| 4738 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4739 | else |
| 4740 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4741 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4742 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4743 | IsIndirect = true; |
| 4744 | Size = 4; |
| 4745 | TyAlign = 4; |
| 4746 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4747 | |
| 4748 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4749 | if (TyAlign > 4) { |
| 4750 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 4751 | "Alignment is not power of 2!"); |
| 4752 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 4753 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 4754 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4755 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4756 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4757 | |
| 4758 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4759 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4760 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4761 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4762 | "ap.next"); |
| 4763 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4764 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4765 | if (IsIndirect) |
| 4766 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4767 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4768 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 4769 | // may not be correctly aligned for the vector type. We create an aligned |
| 4770 | // temporary space and copy the content over from ap.cur to the temporary |
| 4771 | // space. This is necessary if the natural alignment of the type is greater |
| 4772 | // than the ABI alignment. |
| 4773 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 4774 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 4775 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 4776 | "var.align"); |
| 4777 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 4778 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 4779 | Builder.CreateMemCpy(Dst, Src, |
| 4780 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 4781 | TyAlign, false); |
| 4782 | Addr = AlignedTemp; //The content is in aligned location. |
| 4783 | } |
| 4784 | llvm::Type *PTy = |
| 4785 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4786 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4787 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4788 | return AddrTyped; |
| 4789 | } |
| 4790 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4791 | namespace { |
| 4792 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4793 | class NaClARMABIInfo : public ABIInfo { |
| 4794 | public: |
| 4795 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4796 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4797 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4798 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4799 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4800 | private: |
| 4801 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 4802 | ARMABIInfo NInfo; // Used for everything else. |
| 4803 | }; |
| 4804 | |
| 4805 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4806 | public: |
| 4807 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4808 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 4809 | }; |
| 4810 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4813 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4814 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 4815 | PInfo.computeInfo(FI); |
| 4816 | else |
| 4817 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 4818 | } |
| 4819 | |
| 4820 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4821 | CodeGenFunction &CGF) const { |
| 4822 | // Always use the native convention; calling pnacl-style varargs functions |
| 4823 | // is unsupported. |
| 4824 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 4825 | } |
| 4826 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4827 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4828 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4829 | //===----------------------------------------------------------------------===// |
| 4830 | |
| 4831 | namespace { |
| 4832 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4833 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4834 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4835 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4836 | |
| 4837 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4838 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4839 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4840 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4841 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4842 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4843 | }; |
| 4844 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4845 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4846 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4847 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4848 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4849 | |
| 4850 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4851 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4852 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4853 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 4854 | // resulting MDNode to the nvvm.annotations MDNode. |
| 4855 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4856 | }; |
| 4857 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4858 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4859 | if (RetTy->isVoidType()) |
| 4860 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4861 | |
| 4862 | // note: this is different from default ABI |
| 4863 | if (!RetTy->isScalarType()) |
| 4864 | return ABIArgInfo::getDirect(); |
| 4865 | |
| 4866 | // Treat an enum type as its underlying type. |
| 4867 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4868 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4869 | |
| 4870 | return (RetTy->isPromotableIntegerType() ? |
| 4871 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4874 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4875 | // Treat an enum type as its underlying type. |
| 4876 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4877 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4878 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4879 | return (Ty->isPromotableIntegerType() ? |
| 4880 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4883 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4884 | if (!getCXXABI().classifyReturnType(FI)) |
| 4885 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4886 | for (auto &I : FI.arguments()) |
| 4887 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4888 | |
| 4889 | // Always honor user-specified calling convention. |
| 4890 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4891 | return; |
| 4892 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4893 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 4894 | } |
| 4895 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4896 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4897 | CodeGenFunction &CFG) const { |
| 4898 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4901 | void NVPTXTargetCodeGenInfo:: |
| 4902 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4903 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4904 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4905 | if (!FD) return; |
| 4906 | |
| 4907 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4908 | |
| 4909 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4910 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4911 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4912 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4913 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4914 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4915 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4916 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4917 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4918 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4919 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4920 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4921 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4922 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4923 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4924 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4925 | // __global__ functions cannot be called from the device, we do not |
| 4926 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4927 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 4928 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4929 | addNVVMMetadata(F, "kernel", 1); |
| 4930 | } |
| 4931 | if (FD->hasAttr<CUDALaunchBoundsAttr>()) { |
| 4932 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
| 4933 | addNVVMMetadata(F, "maxntidx", |
| 4934 | FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads()); |
| 4935 | // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a |
| 4936 | // zero value from getMinBlocks either means it was not specified in |
| 4937 | // __launch_bounds__ or the user specified a 0 value. In both cases, we |
| 4938 | // don't have to add a PTX directive. |
| 4939 | int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks(); |
| 4940 | if (MinCTASM > 0) { |
| 4941 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 4942 | addNVVMMetadata(F, "minctasm", MinCTASM); |
| 4943 | } |
| 4944 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4945 | } |
| 4946 | } |
| 4947 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4948 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 4949 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4950 | llvm::Module *M = F->getParent(); |
| 4951 | llvm::LLVMContext &Ctx = M->getContext(); |
| 4952 | |
| 4953 | // Get "nvvm.annotations" metadata node |
| 4954 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 4955 | |
Eli Bendersky | e1627b4 | 2014-04-15 17:19:26 +0000 | [diff] [blame] | 4956 | llvm::Value *MDVals[] = { |
| 4957 | F, llvm::MDString::get(Ctx, Name), |
| 4958 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand)}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4959 | // Append metadata to nvvm.annotations |
| 4960 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 4961 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4965 | // SystemZ ABI Implementation |
| 4966 | //===----------------------------------------------------------------------===// |
| 4967 | |
| 4968 | namespace { |
| 4969 | |
| 4970 | class SystemZABIInfo : public ABIInfo { |
| 4971 | public: |
| 4972 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4973 | |
| 4974 | bool isPromotableIntegerType(QualType Ty) const; |
| 4975 | bool isCompoundType(QualType Ty) const; |
| 4976 | bool isFPArgumentType(QualType Ty) const; |
| 4977 | |
| 4978 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4979 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 4980 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4981 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4982 | if (!getCXXABI().classifyReturnType(FI)) |
| 4983 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4984 | for (auto &I : FI.arguments()) |
| 4985 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4988 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4989 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4990 | }; |
| 4991 | |
| 4992 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4993 | public: |
| 4994 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4995 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 4996 | }; |
| 4997 | |
| 4998 | } |
| 4999 | |
| 5000 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 5001 | // Treat an enum type as its underlying type. |
| 5002 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5003 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5004 | |
| 5005 | // Promotable integer types are required to be promoted by the ABI. |
| 5006 | if (Ty->isPromotableIntegerType()) |
| 5007 | return true; |
| 5008 | |
| 5009 | // 32-bit values must also be promoted. |
| 5010 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5011 | switch (BT->getKind()) { |
| 5012 | case BuiltinType::Int: |
| 5013 | case BuiltinType::UInt: |
| 5014 | return true; |
| 5015 | default: |
| 5016 | return false; |
| 5017 | } |
| 5018 | return false; |
| 5019 | } |
| 5020 | |
| 5021 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 5022 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 5023 | } |
| 5024 | |
| 5025 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 5026 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 5027 | switch (BT->getKind()) { |
| 5028 | case BuiltinType::Float: |
| 5029 | case BuiltinType::Double: |
| 5030 | return true; |
| 5031 | default: |
| 5032 | return false; |
| 5033 | } |
| 5034 | |
| 5035 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 5036 | const RecordDecl *RD = RT->getDecl(); |
| 5037 | bool Found = false; |
| 5038 | |
| 5039 | // If this is a C++ record, check the bases first. |
| 5040 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5041 | for (const auto &I : CXXRD->bases()) { |
| 5042 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5043 | |
| 5044 | // Empty bases don't affect things either way. |
| 5045 | if (isEmptyRecord(getContext(), Base, true)) |
| 5046 | continue; |
| 5047 | |
| 5048 | if (Found) |
| 5049 | return false; |
| 5050 | Found = isFPArgumentType(Base); |
| 5051 | if (!Found) |
| 5052 | return false; |
| 5053 | } |
| 5054 | |
| 5055 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5056 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5057 | // Empty bitfields don't affect things either way. |
| 5058 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5059 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 5060 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5061 | return true; |
| 5062 | |
| 5063 | // Unlike isSingleElementStruct(), arrays do not count. |
| 5064 | // Nested isFPArgumentType structures still do though. |
| 5065 | if (Found) |
| 5066 | return false; |
| 5067 | Found = isFPArgumentType(FD->getType()); |
| 5068 | if (!Found) |
| 5069 | return false; |
| 5070 | } |
| 5071 | |
| 5072 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5073 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 5074 | return Found; |
| 5075 | } |
| 5076 | |
| 5077 | return false; |
| 5078 | } |
| 5079 | |
| 5080 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5081 | CodeGenFunction &CGF) const { |
| 5082 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5083 | // struct { |
| 5084 | // i64 __gpr; |
| 5085 | // i64 __fpr; |
| 5086 | // i8 *__overflow_arg_area; |
| 5087 | // i8 *__reg_save_area; |
| 5088 | // }; |
| 5089 | |
| 5090 | // Every argument occupies 8 bytes and is passed by preference in either |
| 5091 | // GPRs or FPRs. |
| 5092 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 5093 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 5094 | bool InFPRs = isFPArgumentType(Ty); |
| 5095 | |
| 5096 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 5097 | bool IsIndirect = AI.isIndirect(); |
| 5098 | unsigned UnpaddedBitSize; |
| 5099 | if (IsIndirect) { |
| 5100 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5101 | UnpaddedBitSize = 64; |
| 5102 | } else |
| 5103 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 5104 | unsigned PaddedBitSize = 64; |
| 5105 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5106 | |
| 5107 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5108 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5109 | |
| 5110 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5111 | if (InFPRs) { |
| 5112 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5113 | RegCountField = 1; // __fpr |
| 5114 | RegSaveIndex = 16; // save offset for f0 |
| 5115 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5116 | } else { |
| 5117 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5118 | RegCountField = 0; // __gpr |
| 5119 | RegSaveIndex = 2; // save offset for r2 |
| 5120 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5121 | } |
| 5122 | |
| 5123 | llvm::Value *RegCountPtr = |
| 5124 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 5125 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 5126 | llvm::Type *IndexTy = RegCount->getType(); |
| 5127 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5128 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5129 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5130 | |
| 5131 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5132 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5133 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5134 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5135 | |
| 5136 | // Emit code to load the value if it was passed in registers. |
| 5137 | CGF.EmitBlock(InRegBlock); |
| 5138 | |
| 5139 | // Work out the address of an argument register. |
| 5140 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5141 | llvm::Value *ScaledRegCount = |
| 5142 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5143 | llvm::Value *RegBase = |
| 5144 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5145 | llvm::Value *RegOffset = |
| 5146 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5147 | llvm::Value *RegSaveAreaPtr = |
| 5148 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 5149 | llvm::Value *RegSaveArea = |
| 5150 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5151 | llvm::Value *RawRegAddr = |
| 5152 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5153 | llvm::Value *RegAddr = |
| 5154 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5155 | |
| 5156 | // Update the register count |
| 5157 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5158 | llvm::Value *NewRegCount = |
| 5159 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5160 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5161 | CGF.EmitBranch(ContBlock); |
| 5162 | |
| 5163 | // Emit code to load the value if it was passed in memory. |
| 5164 | CGF.EmitBlock(InMemBlock); |
| 5165 | |
| 5166 | // Work out the address of a stack argument. |
| 5167 | llvm::Value *OverflowArgAreaPtr = |
| 5168 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 5169 | llvm::Value *OverflowArgArea = |
| 5170 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5171 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5172 | llvm::Value *RawMemAddr = |
| 5173 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5174 | llvm::Value *MemAddr = |
| 5175 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5176 | |
| 5177 | // Update overflow_arg_area_ptr pointer |
| 5178 | llvm::Value *NewOverflowArgArea = |
| 5179 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5180 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5181 | CGF.EmitBranch(ContBlock); |
| 5182 | |
| 5183 | // Return the appropriate result. |
| 5184 | CGF.EmitBlock(ContBlock); |
| 5185 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5186 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5187 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5188 | |
| 5189 | if (IsIndirect) |
| 5190 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5191 | |
| 5192 | return ResAddr; |
| 5193 | } |
| 5194 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5195 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5196 | if (RetTy->isVoidType()) |
| 5197 | return ABIArgInfo::getIgnore(); |
| 5198 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5199 | return ABIArgInfo::getIndirect(0); |
| 5200 | return (isPromotableIntegerType(RetTy) ? |
| 5201 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5202 | } |
| 5203 | |
| 5204 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5205 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5206 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5207 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5208 | |
| 5209 | // Integers and enums are extended to full register width. |
| 5210 | if (isPromotableIntegerType(Ty)) |
| 5211 | return ABIArgInfo::getExtend(); |
| 5212 | |
| 5213 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 5214 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5215 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5216 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5217 | |
| 5218 | // Handle small structures. |
| 5219 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5220 | // Structures with flexible arrays have variable length, so really |
| 5221 | // fail the size test above. |
| 5222 | const RecordDecl *RD = RT->getDecl(); |
| 5223 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5224 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5225 | |
| 5226 | // The structure is passed as an unextended integer, a float, or a double. |
| 5227 | llvm::Type *PassTy; |
| 5228 | if (isFPArgumentType(Ty)) { |
| 5229 | assert(Size == 32 || Size == 64); |
| 5230 | if (Size == 32) |
| 5231 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5232 | else |
| 5233 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5234 | } else |
| 5235 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5236 | return ABIArgInfo::getDirect(PassTy); |
| 5237 | } |
| 5238 | |
| 5239 | // Non-structure compounds are passed indirectly. |
| 5240 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5241 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5242 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5243 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5244 | } |
| 5245 | |
| 5246 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5247 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5248 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5249 | |
| 5250 | namespace { |
| 5251 | |
| 5252 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5253 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5254 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5255 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5256 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5257 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5258 | }; |
| 5259 | |
| 5260 | } |
| 5261 | |
| 5262 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5263 | llvm::GlobalValue *GV, |
| 5264 | CodeGen::CodeGenModule &M) const { |
| 5265 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5266 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5267 | // Handle 'interrupt' attribute: |
| 5268 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5269 | |
| 5270 | // Step 1: Set ISR calling convention. |
| 5271 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5272 | |
| 5273 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5274 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5275 | |
| 5276 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5277 | unsigned Num = attr->getNumber() / 2; |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 5278 | llvm::GlobalAlias::create(llvm::Function::ExternalLinkage, |
| 5279 | "__isr_" + Twine(Num), F); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5280 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5284 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5285 | // MIPS ABI Implementation. This works for both little-endian and |
| 5286 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5287 | //===----------------------------------------------------------------------===// |
| 5288 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5289 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5290 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5291 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5292 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5293 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5294 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5295 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5296 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5297 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5298 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5299 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5300 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5301 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5302 | |
| 5303 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5304 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5305 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5306 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5307 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5308 | }; |
| 5309 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5310 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5311 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5312 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5313 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5314 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5315 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5316 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5317 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5318 | return 29; |
| 5319 | } |
| 5320 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5321 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5322 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5323 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5324 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5325 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5326 | if (FD->hasAttr<Mips16Attr>()) { |
| 5327 | Fn->addFnAttr("mips16"); |
| 5328 | } |
| 5329 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5330 | Fn->addFnAttr("nomips16"); |
| 5331 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5332 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5333 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5334 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5335 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5336 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5337 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5338 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5339 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5340 | }; |
| 5341 | } |
| 5342 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5343 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5344 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5345 | llvm::IntegerType *IntTy = |
| 5346 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5347 | |
| 5348 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5349 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5350 | ArgList.push_back(IntTy); |
| 5351 | |
| 5352 | // If necessary, add one more integer type to ArgList. |
| 5353 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5354 | |
| 5355 | if (R) |
| 5356 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5359 | // In N32/64, an aligned double precision floating point field is passed in |
| 5360 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5361 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5362 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5363 | |
| 5364 | if (IsO32) { |
| 5365 | CoerceToIntArgs(TySize, ArgList); |
| 5366 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5367 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5368 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5369 | if (Ty->isComplexType()) |
| 5370 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5371 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5372 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5373 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5374 | // Unions/vectors are passed in integer registers. |
| 5375 | if (!RT || !RT->isStructureOrClassType()) { |
| 5376 | CoerceToIntArgs(TySize, ArgList); |
| 5377 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5378 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5379 | |
| 5380 | const RecordDecl *RD = RT->getDecl(); |
| 5381 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5382 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5383 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5384 | uint64_t LastOffset = 0; |
| 5385 | unsigned idx = 0; |
| 5386 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5387 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5388 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5389 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5390 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5391 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5392 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5393 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5394 | |
| 5395 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5396 | continue; |
| 5397 | |
| 5398 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5399 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5400 | continue; |
| 5401 | |
| 5402 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5403 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5404 | ArgList.push_back(I64); |
| 5405 | |
| 5406 | // Add double type. |
| 5407 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5408 | LastOffset = Offset + 64; |
| 5409 | } |
| 5410 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5411 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5412 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5413 | |
| 5414 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5415 | } |
| 5416 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5417 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5418 | uint64_t Offset) const { |
| 5419 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5420 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5421 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5422 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5423 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5424 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5425 | ABIArgInfo |
| 5426 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5427 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5428 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5429 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5430 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5431 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5432 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5433 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5434 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5435 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5436 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5437 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5438 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5439 | return ABIArgInfo::getIgnore(); |
| 5440 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5441 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5442 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5443 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5444 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5445 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5446 | // If we have reached here, aggregates are passed directly by coercing to |
| 5447 | // another structure type. Padding is inserted if the offset of the |
| 5448 | // aggregate is unaligned. |
| 5449 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5450 | getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
| 5453 | // Treat an enum type as its underlying type. |
| 5454 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5455 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5456 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5457 | if (Ty->isPromotableIntegerType()) |
| 5458 | return ABIArgInfo::getExtend(); |
| 5459 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5460 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5461 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5462 | } |
| 5463 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5464 | llvm::Type* |
| 5465 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5466 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5467 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5468 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5469 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5470 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5471 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5472 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5473 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5474 | // N32/64 returns struct/classes in floating point registers if the |
| 5475 | // following conditions are met: |
| 5476 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5477 | // 2. The struct/class has one or two fields all of which are floating |
| 5478 | // point types. |
| 5479 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5480 | // |
| 5481 | // Any other composite results are returned in integer registers. |
| 5482 | // |
| 5483 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5484 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5485 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5486 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5487 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5488 | if (!BT || !BT->isFloatingPoint()) |
| 5489 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5490 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5491 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | if (b == e) |
| 5495 | return llvm::StructType::get(getVMContext(), RTList, |
| 5496 | RD->hasAttr<PackedAttr>()); |
| 5497 | |
| 5498 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5499 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5502 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5503 | return llvm::StructType::get(getVMContext(), RTList); |
| 5504 | } |
| 5505 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5506 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5507 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5508 | |
| 5509 | if (RetTy->isVoidType() || Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5510 | return ABIArgInfo::getIgnore(); |
| 5511 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5512 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5513 | if (Size <= 128) { |
| 5514 | if (RetTy->isAnyComplexType()) |
| 5515 | return ABIArgInfo::getDirect(); |
| 5516 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5517 | // O32 returns integer vectors in registers. |
| 5518 | if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) |
| 5519 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5520 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5521 | if (!IsO32) |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5522 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5523 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5524 | |
| 5525 | return ABIArgInfo::getIndirect(0); |
| 5526 | } |
| 5527 | |
| 5528 | // Treat an enum type as its underlying type. |
| 5529 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5530 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5531 | |
| 5532 | return (RetTy->isPromotableIntegerType() ? |
| 5533 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5534 | } |
| 5535 | |
| 5536 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5537 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5538 | if (!getCXXABI().classifyReturnType(FI)) |
| 5539 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5540 | |
| 5541 | // 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] | 5542 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5543 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5544 | for (auto &I : FI.arguments()) |
| 5545 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5549 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5550 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5551 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5552 | |
| 5553 | CGBuilderTy &Builder = CGF.Builder; |
| 5554 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5555 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5556 | int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5557 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5558 | llvm::Value *AddrTyped; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5559 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5560 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5561 | |
| 5562 | if (TypeAlign > MinABIStackAlignInBytes) { |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5563 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5564 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5565 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5566 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5567 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5568 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5569 | } |
| 5570 | else |
| 5571 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5572 | |
| 5573 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5574 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5575 | uint64_t Offset = |
| 5576 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 5577 | llvm::Value *NextAddr = |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5578 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5579 | "ap.next"); |
| 5580 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5581 | |
| 5582 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5585 | bool |
| 5586 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5587 | llvm::Value *Address) const { |
| 5588 | // This information comes from gcc's implementation, which seems to |
| 5589 | // as canonical as it gets. |
| 5590 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5591 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5592 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5593 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5594 | |
| 5595 | // 0-31 are the general purpose registers, $0 - $31. |
| 5596 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5597 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5598 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5599 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5600 | |
| 5601 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5602 | // They are one bit wide and ignored here. |
| 5603 | |
| 5604 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5605 | // (coprocessor 1 is the FP unit) |
| 5606 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5607 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5608 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5609 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5610 | return false; |
| 5611 | } |
| 5612 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5613 | //===----------------------------------------------------------------------===// |
| 5614 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5615 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5616 | // handling. |
| 5617 | //===----------------------------------------------------------------------===// |
| 5618 | |
| 5619 | namespace { |
| 5620 | |
| 5621 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5622 | public: |
| 5623 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5624 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5625 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5626 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5627 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5628 | }; |
| 5629 | |
| 5630 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5631 | llvm::GlobalValue *GV, |
| 5632 | CodeGen::CodeGenModule &M) const { |
| 5633 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5634 | if (!FD) return; |
| 5635 | |
| 5636 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5637 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5638 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5639 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5640 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5641 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5642 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5643 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5644 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5645 | llvm::LLVMContext &Context = F->getContext(); |
| 5646 | llvm::NamedMDNode *OpenCLMetadata = |
| 5647 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5648 | |
| 5649 | SmallVector<llvm::Value*, 5> Operands; |
| 5650 | Operands.push_back(F); |
| 5651 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5652 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5653 | llvm::APInt(32, Attr->getXDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5654 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5655 | llvm::APInt(32, Attr->getYDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5656 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5657 | llvm::APInt(32, Attr->getZDim()))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5658 | |
| 5659 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5660 | // for implementing the work_group_size_hint attr later. Currently |
| 5661 | // always true as the hint is not yet implemented. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5662 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5663 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5664 | } |
| 5665 | } |
| 5666 | } |
| 5667 | } |
| 5668 | |
| 5669 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5670 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5671 | //===----------------------------------------------------------------------===// |
| 5672 | // Hexagon ABI Implementation |
| 5673 | //===----------------------------------------------------------------------===// |
| 5674 | |
| 5675 | namespace { |
| 5676 | |
| 5677 | class HexagonABIInfo : public ABIInfo { |
| 5678 | |
| 5679 | |
| 5680 | public: |
| 5681 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5682 | |
| 5683 | private: |
| 5684 | |
| 5685 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5686 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5687 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5688 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5689 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5690 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5691 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5692 | }; |
| 5693 | |
| 5694 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5695 | public: |
| 5696 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5697 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5698 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5699 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5700 | return 29; |
| 5701 | } |
| 5702 | }; |
| 5703 | |
| 5704 | } |
| 5705 | |
| 5706 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5707 | if (!getCXXABI().classifyReturnType(FI)) |
| 5708 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5709 | for (auto &I : FI.arguments()) |
| 5710 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
| 5713 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5714 | if (!isAggregateTypeForABI(Ty)) { |
| 5715 | // Treat an enum type as its underlying type. |
| 5716 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5717 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5718 | |
| 5719 | return (Ty->isPromotableIntegerType() ? |
| 5720 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5721 | } |
| 5722 | |
| 5723 | // Ignore empty records. |
| 5724 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5725 | return ABIArgInfo::getIgnore(); |
| 5726 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5727 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5728 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5729 | |
| 5730 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5731 | if (Size > 64) |
| 5732 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5733 | // Pass in the smallest viable integer type. |
| 5734 | else if (Size > 32) |
| 5735 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5736 | else if (Size > 16) |
| 5737 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5738 | else if (Size > 8) |
| 5739 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5740 | else |
| 5741 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5742 | } |
| 5743 | |
| 5744 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5745 | if (RetTy->isVoidType()) |
| 5746 | return ABIArgInfo::getIgnore(); |
| 5747 | |
| 5748 | // Large vector types should be returned via memory. |
| 5749 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5750 | return ABIArgInfo::getIndirect(0); |
| 5751 | |
| 5752 | if (!isAggregateTypeForABI(RetTy)) { |
| 5753 | // Treat an enum type as its underlying type. |
| 5754 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5755 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5756 | |
| 5757 | return (RetTy->isPromotableIntegerType() ? |
| 5758 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5759 | } |
| 5760 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5761 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5762 | return ABIArgInfo::getIgnore(); |
| 5763 | |
| 5764 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5765 | // are returned indirectly. |
| 5766 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5767 | if (Size <= 64) { |
| 5768 | // Return in the smallest viable integer type. |
| 5769 | if (Size <= 8) |
| 5770 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5771 | if (Size <= 16) |
| 5772 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5773 | if (Size <= 32) |
| 5774 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5775 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5776 | } |
| 5777 | |
| 5778 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5779 | } |
| 5780 | |
| 5781 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5782 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5783 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5784 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5785 | |
| 5786 | CGBuilderTy &Builder = CGF.Builder; |
| 5787 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5788 | "ap"); |
| 5789 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5790 | llvm::Type *PTy = |
| 5791 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5792 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5793 | |
| 5794 | uint64_t Offset = |
| 5795 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5796 | llvm::Value *NextAddr = |
| 5797 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5798 | "ap.next"); |
| 5799 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5800 | |
| 5801 | return AddrTyped; |
| 5802 | } |
| 5803 | |
| 5804 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5805 | //===----------------------------------------------------------------------===// |
| 5806 | // SPARC v9 ABI Implementation. |
| 5807 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 5808 | // |
| 5809 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 5810 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 5811 | // the array, structs larger than 16 bytes are passed indirectly. |
| 5812 | // |
| 5813 | // One case requires special care: |
| 5814 | // |
| 5815 | // struct mixed { |
| 5816 | // int i; |
| 5817 | // float f; |
| 5818 | // }; |
| 5819 | // |
| 5820 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 5821 | // parameter array, but the int is passed in an integer register, and the float |
| 5822 | // is passed in a floating point register. This is represented as two arguments |
| 5823 | // with the LLVM IR inreg attribute: |
| 5824 | // |
| 5825 | // declare void f(i32 inreg %i, float inreg %f) |
| 5826 | // |
| 5827 | // The code generator will only allocate 4 bytes from the parameter array for |
| 5828 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 5829 | // bytes. |
| 5830 | // |
| 5831 | namespace { |
| 5832 | class SparcV9ABIInfo : public ABIInfo { |
| 5833 | public: |
| 5834 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5835 | |
| 5836 | private: |
| 5837 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5838 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5839 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5840 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5841 | |
| 5842 | // Coercion type builder for structs passed in registers. The coercion type |
| 5843 | // serves two purposes: |
| 5844 | // |
| 5845 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 5846 | // in registers. |
| 5847 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 5848 | // code generator knows to pass them in floating point registers. |
| 5849 | // |
| 5850 | // We also compute the InReg flag which indicates that the struct contains |
| 5851 | // aligned 32-bit floats. |
| 5852 | // |
| 5853 | struct CoerceBuilder { |
| 5854 | llvm::LLVMContext &Context; |
| 5855 | const llvm::DataLayout &DL; |
| 5856 | SmallVector<llvm::Type*, 8> Elems; |
| 5857 | uint64_t Size; |
| 5858 | bool InReg; |
| 5859 | |
| 5860 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 5861 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 5862 | |
| 5863 | // Pad Elems with integers until Size is ToSize. |
| 5864 | void pad(uint64_t ToSize) { |
| 5865 | assert(ToSize >= Size && "Cannot remove elements"); |
| 5866 | if (ToSize == Size) |
| 5867 | return; |
| 5868 | |
| 5869 | // Finish the current 64-bit word. |
| 5870 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 5871 | if (Aligned > Size && Aligned <= ToSize) { |
| 5872 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 5873 | Size = Aligned; |
| 5874 | } |
| 5875 | |
| 5876 | // Add whole 64-bit words. |
| 5877 | while (Size + 64 <= ToSize) { |
| 5878 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 5879 | Size += 64; |
| 5880 | } |
| 5881 | |
| 5882 | // Final in-word padding. |
| 5883 | if (Size < ToSize) { |
| 5884 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 5885 | Size = ToSize; |
| 5886 | } |
| 5887 | } |
| 5888 | |
| 5889 | // Add a floating point element at Offset. |
| 5890 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 5891 | // Unaligned floats are treated as integers. |
| 5892 | if (Offset % Bits) |
| 5893 | return; |
| 5894 | // The InReg flag is only required if there are any floats < 64 bits. |
| 5895 | if (Bits < 64) |
| 5896 | InReg = true; |
| 5897 | pad(Offset); |
| 5898 | Elems.push_back(Ty); |
| 5899 | Size = Offset + Bits; |
| 5900 | } |
| 5901 | |
| 5902 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 5903 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 5904 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 5905 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 5906 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 5907 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 5908 | switch (ElemTy->getTypeID()) { |
| 5909 | case llvm::Type::StructTyID: |
| 5910 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 5911 | break; |
| 5912 | case llvm::Type::FloatTyID: |
| 5913 | addFloat(ElemOffset, ElemTy, 32); |
| 5914 | break; |
| 5915 | case llvm::Type::DoubleTyID: |
| 5916 | addFloat(ElemOffset, ElemTy, 64); |
| 5917 | break; |
| 5918 | case llvm::Type::FP128TyID: |
| 5919 | addFloat(ElemOffset, ElemTy, 128); |
| 5920 | break; |
| 5921 | case llvm::Type::PointerTyID: |
| 5922 | if (ElemOffset % 64 == 0) { |
| 5923 | pad(ElemOffset); |
| 5924 | Elems.push_back(ElemTy); |
| 5925 | Size += 64; |
| 5926 | } |
| 5927 | break; |
| 5928 | default: |
| 5929 | break; |
| 5930 | } |
| 5931 | } |
| 5932 | } |
| 5933 | |
| 5934 | // Check if Ty is a usable substitute for the coercion type. |
| 5935 | bool isUsableType(llvm::StructType *Ty) const { |
| 5936 | if (Ty->getNumElements() != Elems.size()) |
| 5937 | return false; |
| 5938 | for (unsigned i = 0, e = Elems.size(); i != e; ++i) |
| 5939 | if (Elems[i] != Ty->getElementType(i)) |
| 5940 | return false; |
| 5941 | return true; |
| 5942 | } |
| 5943 | |
| 5944 | // Get the coercion type as a literal struct type. |
| 5945 | llvm::Type *getType() const { |
| 5946 | if (Elems.size() == 1) |
| 5947 | return Elems.front(); |
| 5948 | else |
| 5949 | return llvm::StructType::get(Context, Elems); |
| 5950 | } |
| 5951 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5952 | }; |
| 5953 | } // end anonymous namespace |
| 5954 | |
| 5955 | ABIArgInfo |
| 5956 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 5957 | if (Ty->isVoidType()) |
| 5958 | return ABIArgInfo::getIgnore(); |
| 5959 | |
| 5960 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5961 | |
| 5962 | // Anything too big to fit in registers is passed with an explicit indirect |
| 5963 | // pointer / sret pointer. |
| 5964 | if (Size > SizeLimit) |
| 5965 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5966 | |
| 5967 | // Treat an enum type as its underlying type. |
| 5968 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5969 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5970 | |
| 5971 | // Integer types smaller than a register are extended. |
| 5972 | if (Size < 64 && Ty->isIntegerType()) |
| 5973 | return ABIArgInfo::getExtend(); |
| 5974 | |
| 5975 | // Other non-aggregates go in registers. |
| 5976 | if (!isAggregateTypeForABI(Ty)) |
| 5977 | return ABIArgInfo::getDirect(); |
| 5978 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 5979 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 5980 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 5981 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 5982 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5983 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5984 | // 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] | 5985 | // Build a coercion type from the LLVM struct type. |
| 5986 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 5987 | if (!StrTy) |
| 5988 | return ABIArgInfo::getDirect(); |
| 5989 | |
| 5990 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 5991 | CB.addStruct(0, StrTy); |
| 5992 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 5993 | |
| 5994 | // Try to use the original type for coercion. |
| 5995 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 5996 | |
| 5997 | if (CB.InReg) |
| 5998 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 5999 | else |
| 6000 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6004 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6005 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6006 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6007 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6008 | AI.setCoerceToType(ArgTy); |
| 6009 | |
| 6010 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6011 | CGBuilderTy &Builder = CGF.Builder; |
| 6012 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6013 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6014 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6015 | llvm::Value *ArgAddr; |
| 6016 | unsigned Stride; |
| 6017 | |
| 6018 | switch (AI.getKind()) { |
| 6019 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6020 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6021 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6022 | |
| 6023 | case ABIArgInfo::Extend: |
| 6024 | Stride = 8; |
| 6025 | ArgAddr = Builder |
| 6026 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6027 | "extend"); |
| 6028 | break; |
| 6029 | |
| 6030 | case ABIArgInfo::Direct: |
| 6031 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6032 | ArgAddr = Addr; |
| 6033 | break; |
| 6034 | |
| 6035 | case ABIArgInfo::Indirect: |
| 6036 | Stride = 8; |
| 6037 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6038 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6039 | "indirect"); |
| 6040 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6041 | break; |
| 6042 | |
| 6043 | case ABIArgInfo::Ignore: |
| 6044 | return llvm::UndefValue::get(ArgPtrTy); |
| 6045 | } |
| 6046 | |
| 6047 | // Update VAList. |
| 6048 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6049 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6050 | |
| 6051 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6052 | } |
| 6053 | |
| 6054 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6055 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6056 | for (auto &I : FI.arguments()) |
| 6057 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | namespace { |
| 6061 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6062 | public: |
| 6063 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6064 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6065 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6066 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6067 | return 14; |
| 6068 | } |
| 6069 | |
| 6070 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6071 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6072 | }; |
| 6073 | } // end anonymous namespace |
| 6074 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6075 | bool |
| 6076 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6077 | llvm::Value *Address) const { |
| 6078 | // This is calculated from the LLVM and GCC tables and verified |
| 6079 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6080 | |
| 6081 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6082 | |
| 6083 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6084 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6085 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6086 | |
| 6087 | // 0-31: the 8-byte general-purpose registers |
| 6088 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6089 | |
| 6090 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6091 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6092 | |
| 6093 | // Y = 64 |
| 6094 | // PSR = 65 |
| 6095 | // WIM = 66 |
| 6096 | // TBR = 67 |
| 6097 | // PC = 68 |
| 6098 | // NPC = 69 |
| 6099 | // FSR = 70 |
| 6100 | // CSR = 71 |
| 6101 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 6102 | |
| 6103 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6104 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6105 | |
| 6106 | return false; |
| 6107 | } |
| 6108 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6109 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6110 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6111 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6112 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6113 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6114 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6115 | |
| 6116 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6117 | /// it by reference between functions that append to it. |
| 6118 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6119 | |
| 6120 | /// TypeStringCache caches the meta encodings of Types. |
| 6121 | /// |
| 6122 | /// The reason for caching TypeStrings is two fold: |
| 6123 | /// 1. To cache a type's encoding for later uses; |
| 6124 | /// 2. As a means to break recursive member type inclusion. |
| 6125 | /// |
| 6126 | /// A cache Entry can have a Status of: |
| 6127 | /// NonRecursive: The type encoding is not recursive; |
| 6128 | /// Recursive: The type encoding is recursive; |
| 6129 | /// Incomplete: An incomplete TypeString; |
| 6130 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6131 | /// Recursive type encoding. |
| 6132 | /// |
| 6133 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6134 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6135 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6136 | /// the type is encountered. |
| 6137 | /// |
| 6138 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6139 | /// possible. The type itself is recursive and it may contain other types which |
| 6140 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6141 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6142 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6143 | /// |
| 6144 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6145 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6146 | /// are placed into the cache during type expansion as a means to identify and |
| 6147 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6148 | /// the entry becomes IncompleteUsed. |
| 6149 | /// |
| 6150 | /// During the expansion of a RecordType's members: |
| 6151 | /// |
| 6152 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6153 | /// cached encoding is used; |
| 6154 | /// |
| 6155 | /// If the cache contains a Recursive encoding for the member type, the |
| 6156 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6157 | /// |
| 6158 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6159 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6160 | /// |
| 6161 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6162 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6163 | /// it is swapped back in; |
| 6164 | /// |
| 6165 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6166 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6167 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6168 | /// |
| 6169 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6170 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6171 | /// Else the member is part of a recursive type and thus the recursion has |
| 6172 | /// been exited too soon for the encoding to be correct for the member. |
| 6173 | /// |
| 6174 | class TypeStringCache { |
| 6175 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6176 | struct Entry { |
| 6177 | std::string Str; // The encoded TypeString for the type. |
| 6178 | enum Status State; // Information about the encoding in 'Str'. |
| 6179 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6180 | // during the expansion of RecordType's members. |
| 6181 | }; |
| 6182 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6183 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6184 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6185 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6186 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6187 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6188 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6189 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6190 | bool IsRecursive); |
| 6191 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6192 | }; |
| 6193 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6194 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6195 | /// FieldEncoding is a helper for this ordering process. |
| 6196 | class FieldEncoding { |
| 6197 | bool HasName; |
| 6198 | std::string Enc; |
| 6199 | public: |
| 6200 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6201 | StringRef str() {return Enc.c_str();}; |
| 6202 | bool operator<(const FieldEncoding &rhs) const { |
| 6203 | if (HasName != rhs.HasName) return HasName; |
| 6204 | return Enc < rhs.Enc; |
| 6205 | } |
| 6206 | }; |
| 6207 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6208 | class XCoreABIInfo : public DefaultABIInfo { |
| 6209 | public: |
| 6210 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6211 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6212 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6213 | }; |
| 6214 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6215 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6216 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6217 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6218 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6219 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6220 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6221 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6222 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6223 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6224 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6225 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6226 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6227 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6228 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6229 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6230 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6231 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6232 | CGF.Int8PtrPtrTy); |
| 6233 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6234 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6235 | // Handle the argument. |
| 6236 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6237 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6238 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6239 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6240 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6241 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6242 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6243 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6244 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6245 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6246 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6247 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6248 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6249 | ArgSize = 0; |
| 6250 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6251 | case ABIArgInfo::Extend: |
| 6252 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6253 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6254 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6255 | if (ArgSize < 4) |
| 6256 | ArgSize = 4; |
| 6257 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6258 | case ABIArgInfo::Indirect: |
| 6259 | llvm::Value *ArgAddr; |
| 6260 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6261 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6262 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6263 | ArgSize = 4; |
| 6264 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6265 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6266 | |
| 6267 | // Increment the VAList. |
| 6268 | if (ArgSize) { |
| 6269 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6270 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6271 | } |
| 6272 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6273 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6274 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6275 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6276 | /// into the cache as a means to identify and break recursion. |
| 6277 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6278 | /// be reinserted by removeIncomplete(). |
| 6279 | /// All other types of encoding should have been used rather than arriving here. |
| 6280 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6281 | std::string StubEnc) { |
| 6282 | if (!ID) |
| 6283 | return; |
| 6284 | Entry &E = Map[ID]; |
| 6285 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6286 | "Incorrectly use of addIncomplete"); |
| 6287 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6288 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6289 | E.Str.swap(StubEnc); |
| 6290 | E.State = Incomplete; |
| 6291 | ++IncompleteCount; |
| 6292 | } |
| 6293 | |
| 6294 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6295 | /// must be removed from the cache. |
| 6296 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6297 | /// Returns true if the RecordType was defined recursively. |
| 6298 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6299 | if (!ID) |
| 6300 | return false; |
| 6301 | auto I = Map.find(ID); |
| 6302 | assert(I != Map.end() && "Entry not present"); |
| 6303 | Entry &E = I->second; |
| 6304 | assert( (E.State == Incomplete || |
| 6305 | E.State == IncompleteUsed) && |
| 6306 | "Entry must be an incomplete type"); |
| 6307 | bool IsRecursive = false; |
| 6308 | if (E.State == IncompleteUsed) { |
| 6309 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6310 | IsRecursive = true; |
| 6311 | --IncompleteUsedCount; |
| 6312 | } |
| 6313 | if (E.Swapped.empty()) |
| 6314 | Map.erase(I); |
| 6315 | else { |
| 6316 | // Swap the Recursive back. |
| 6317 | E.Swapped.swap(E.Str); |
| 6318 | E.Swapped.clear(); |
| 6319 | E.State = Recursive; |
| 6320 | } |
| 6321 | --IncompleteCount; |
| 6322 | return IsRecursive; |
| 6323 | } |
| 6324 | |
| 6325 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6326 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6327 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6328 | bool IsRecursive) { |
| 6329 | if (!ID || IncompleteUsedCount) |
| 6330 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6331 | Entry &E = Map[ID]; |
| 6332 | if (IsRecursive && !E.Str.empty()) { |
| 6333 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6334 | "This is not the same Recursive entry"); |
| 6335 | // The parent container was not recursive after all, so we could have used |
| 6336 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6337 | // we started viz: IncompleteCount!=0. |
| 6338 | return; |
| 6339 | } |
| 6340 | assert(E.Str.empty() && "Entry already present"); |
| 6341 | E.Str = Str.str(); |
| 6342 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6343 | } |
| 6344 | |
| 6345 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6346 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6347 | /// encoding is Recursive, return an empty StringRef. |
| 6348 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6349 | if (!ID) |
| 6350 | return StringRef(); // We have no key. |
| 6351 | auto I = Map.find(ID); |
| 6352 | if (I == Map.end()) |
| 6353 | return StringRef(); // We have no encoding. |
| 6354 | Entry &E = I->second; |
| 6355 | if (E.State == Recursive && IncompleteCount) |
| 6356 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6357 | |
| 6358 | if (E.State == Incomplete) { |
| 6359 | // The incomplete type is being used to break out of recursion. |
| 6360 | E.State = IncompleteUsed; |
| 6361 | ++IncompleteUsedCount; |
| 6362 | } |
| 6363 | return E.Str.c_str(); |
| 6364 | } |
| 6365 | |
| 6366 | /// The XCore ABI includes a type information section that communicates symbol |
| 6367 | /// type information to the linker. The linker uses this information to verify |
| 6368 | /// safety/correctness of things such as array bound and pointers et al. |
| 6369 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6370 | /// This type information (TypeString) is emitted into meta data for all global |
| 6371 | /// symbols: definitions, declarations, functions & variables. |
| 6372 | /// |
| 6373 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6374 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
| 6375 | /// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf> |
| 6376 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6377 | /// |
| 6378 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6379 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6380 | |
| 6381 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6382 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6383 | CodeGen::CodeGenModule &CGM) const { |
| 6384 | SmallStringEnc Enc; |
| 6385 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6386 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 6387 | llvm::SmallVector<llvm::Value *, 2> MDVals; |
| 6388 | MDVals.push_back(GV); |
| 6389 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6390 | llvm::NamedMDNode *MD = |
| 6391 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6392 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6393 | } |
| 6394 | } |
| 6395 | |
| 6396 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6397 | const CodeGen::CodeGenModule &CGM, |
| 6398 | TypeStringCache &TSC); |
| 6399 | |
| 6400 | /// Helper function for appendRecordType(). |
| 6401 | /// Builds a SmallVector containing the encoded field types in declaration order. |
| 6402 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6403 | const RecordDecl *RD, |
| 6404 | const CodeGen::CodeGenModule &CGM, |
| 6405 | TypeStringCache &TSC) { |
| 6406 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 6407 | I != E; ++I) { |
| 6408 | SmallStringEnc Enc; |
| 6409 | Enc += "m("; |
| 6410 | Enc += I->getName(); |
| 6411 | Enc += "){"; |
| 6412 | if (I->isBitField()) { |
| 6413 | Enc += "b("; |
| 6414 | llvm::raw_svector_ostream OS(Enc); |
| 6415 | OS.resync(); |
| 6416 | OS << I->getBitWidthValue(CGM.getContext()); |
| 6417 | OS.flush(); |
| 6418 | Enc += ':'; |
| 6419 | } |
| 6420 | if (!appendType(Enc, I->getType(), CGM, TSC)) |
| 6421 | return false; |
| 6422 | if (I->isBitField()) |
| 6423 | Enc += ')'; |
| 6424 | Enc += '}'; |
| 6425 | FE.push_back(FieldEncoding(!I->getName().empty(), Enc)); |
| 6426 | } |
| 6427 | return true; |
| 6428 | } |
| 6429 | |
| 6430 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6431 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6432 | /// Union types have their fields ordered according to the ABI. |
| 6433 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6434 | const CodeGen::CodeGenModule &CGM, |
| 6435 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6436 | // Append the cached TypeString if we have one. |
| 6437 | StringRef TypeString = TSC.lookupStr(ID); |
| 6438 | if (!TypeString.empty()) { |
| 6439 | Enc += TypeString; |
| 6440 | return true; |
| 6441 | } |
| 6442 | |
| 6443 | // Start to emit an incomplete TypeString. |
| 6444 | size_t Start = Enc.size(); |
| 6445 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6446 | Enc += '('; |
| 6447 | if (ID) |
| 6448 | Enc += ID->getName(); |
| 6449 | Enc += "){"; |
| 6450 | |
| 6451 | // We collect all encoded fields and order as necessary. |
| 6452 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6453 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6454 | if (RD && !RD->field_empty()) { |
| 6455 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6456 | // so that recursive calls to this RecordType will use it whilst building a |
| 6457 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6458 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6459 | std::string StubEnc(Enc.substr(Start).str()); |
| 6460 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6461 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6462 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6463 | (void) TSC.removeIncomplete(ID); |
| 6464 | return false; |
| 6465 | } |
| 6466 | IsRecursive = TSC.removeIncomplete(ID); |
| 6467 | // The ABI requires unions to be sorted but not structures. |
| 6468 | // See FieldEncoding::operator< for sort algorithm. |
| 6469 | if (RT->isUnionType()) |
| 6470 | std::sort(FE.begin(), FE.end()); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6471 | // We can now complete the TypeString. |
| 6472 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6473 | for (unsigned I = 0; I != E; ++I) { |
| 6474 | if (I) |
| 6475 | Enc += ','; |
| 6476 | Enc += FE[I].str(); |
| 6477 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6478 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6479 | Enc += '}'; |
| 6480 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6481 | return true; |
| 6482 | } |
| 6483 | |
| 6484 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6485 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6486 | TypeStringCache &TSC, |
| 6487 | const IdentifierInfo *ID) { |
| 6488 | // Append the cached TypeString if we have one. |
| 6489 | StringRef TypeString = TSC.lookupStr(ID); |
| 6490 | if (!TypeString.empty()) { |
| 6491 | Enc += TypeString; |
| 6492 | return true; |
| 6493 | } |
| 6494 | |
| 6495 | size_t Start = Enc.size(); |
| 6496 | Enc += "e("; |
| 6497 | if (ID) |
| 6498 | Enc += ID->getName(); |
| 6499 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6500 | |
| 6501 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6502 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6503 | SmallVector<FieldEncoding, 16> FE; |
| 6504 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 6505 | ++I) { |
| 6506 | SmallStringEnc EnumEnc; |
| 6507 | EnumEnc += "m("; |
| 6508 | EnumEnc += I->getName(); |
| 6509 | EnumEnc += "){"; |
| 6510 | I->getInitVal().toString(EnumEnc); |
| 6511 | EnumEnc += '}'; |
| 6512 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 6513 | } |
| 6514 | std::sort(FE.begin(), FE.end()); |
| 6515 | unsigned E = FE.size(); |
| 6516 | for (unsigned I = 0; I != E; ++I) { |
| 6517 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6518 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 6519 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6520 | } |
| 6521 | } |
| 6522 | Enc += '}'; |
| 6523 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6524 | return true; |
| 6525 | } |
| 6526 | |
| 6527 | /// Appends type's qualifier to Enc. |
| 6528 | /// This is done prior to appending the type's encoding. |
| 6529 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6530 | // Qualifiers are emitted in alphabetical order. |
| 6531 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6532 | int Lookup = 0; |
| 6533 | if (QT.isConstQualified()) |
| 6534 | Lookup += 1<<0; |
| 6535 | if (QT.isRestrictQualified()) |
| 6536 | Lookup += 1<<1; |
| 6537 | if (QT.isVolatileQualified()) |
| 6538 | Lookup += 1<<2; |
| 6539 | Enc += Table[Lookup]; |
| 6540 | } |
| 6541 | |
| 6542 | /// Appends built-in types to Enc. |
| 6543 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6544 | const char *EncType; |
| 6545 | switch (BT->getKind()) { |
| 6546 | case BuiltinType::Void: |
| 6547 | EncType = "0"; |
| 6548 | break; |
| 6549 | case BuiltinType::Bool: |
| 6550 | EncType = "b"; |
| 6551 | break; |
| 6552 | case BuiltinType::Char_U: |
| 6553 | EncType = "uc"; |
| 6554 | break; |
| 6555 | case BuiltinType::UChar: |
| 6556 | EncType = "uc"; |
| 6557 | break; |
| 6558 | case BuiltinType::SChar: |
| 6559 | EncType = "sc"; |
| 6560 | break; |
| 6561 | case BuiltinType::UShort: |
| 6562 | EncType = "us"; |
| 6563 | break; |
| 6564 | case BuiltinType::Short: |
| 6565 | EncType = "ss"; |
| 6566 | break; |
| 6567 | case BuiltinType::UInt: |
| 6568 | EncType = "ui"; |
| 6569 | break; |
| 6570 | case BuiltinType::Int: |
| 6571 | EncType = "si"; |
| 6572 | break; |
| 6573 | case BuiltinType::ULong: |
| 6574 | EncType = "ul"; |
| 6575 | break; |
| 6576 | case BuiltinType::Long: |
| 6577 | EncType = "sl"; |
| 6578 | break; |
| 6579 | case BuiltinType::ULongLong: |
| 6580 | EncType = "ull"; |
| 6581 | break; |
| 6582 | case BuiltinType::LongLong: |
| 6583 | EncType = "sll"; |
| 6584 | break; |
| 6585 | case BuiltinType::Float: |
| 6586 | EncType = "ft"; |
| 6587 | break; |
| 6588 | case BuiltinType::Double: |
| 6589 | EncType = "d"; |
| 6590 | break; |
| 6591 | case BuiltinType::LongDouble: |
| 6592 | EncType = "ld"; |
| 6593 | break; |
| 6594 | default: |
| 6595 | return false; |
| 6596 | } |
| 6597 | Enc += EncType; |
| 6598 | return true; |
| 6599 | } |
| 6600 | |
| 6601 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6602 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6603 | const CodeGen::CodeGenModule &CGM, |
| 6604 | TypeStringCache &TSC) { |
| 6605 | Enc += "p("; |
| 6606 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6607 | return false; |
| 6608 | Enc += ')'; |
| 6609 | return true; |
| 6610 | } |
| 6611 | |
| 6612 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6613 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 6614 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6615 | const CodeGen::CodeGenModule &CGM, |
| 6616 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6617 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6618 | return false; |
| 6619 | Enc += "a("; |
| 6620 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6621 | CAT->getSize().toStringUnsigned(Enc); |
| 6622 | else |
| 6623 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6624 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6625 | // The Qualifiers should be attached to the type rather than the array. |
| 6626 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6627 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6628 | return false; |
| 6629 | Enc += ')'; |
| 6630 | return true; |
| 6631 | } |
| 6632 | |
| 6633 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6634 | /// and the arguments. |
| 6635 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6636 | const CodeGen::CodeGenModule &CGM, |
| 6637 | TypeStringCache &TSC) { |
| 6638 | Enc += "f{"; |
| 6639 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 6640 | return false; |
| 6641 | Enc += "}("; |
| 6642 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 6643 | // N.B. we are only interested in the adjusted param types. |
| 6644 | auto I = FPT->param_type_begin(); |
| 6645 | auto E = FPT->param_type_end(); |
| 6646 | if (I != E) { |
| 6647 | do { |
| 6648 | if (!appendType(Enc, *I, CGM, TSC)) |
| 6649 | return false; |
| 6650 | ++I; |
| 6651 | if (I != E) |
| 6652 | Enc += ','; |
| 6653 | } while (I != E); |
| 6654 | if (FPT->isVariadic()) |
| 6655 | Enc += ",va"; |
| 6656 | } else { |
| 6657 | if (FPT->isVariadic()) |
| 6658 | Enc += "va"; |
| 6659 | else |
| 6660 | Enc += '0'; |
| 6661 | } |
| 6662 | } |
| 6663 | Enc += ')'; |
| 6664 | return true; |
| 6665 | } |
| 6666 | |
| 6667 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 6668 | /// type encodings. |
| 6669 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6670 | const CodeGen::CodeGenModule &CGM, |
| 6671 | TypeStringCache &TSC) { |
| 6672 | |
| 6673 | QualType QT = QType.getCanonicalType(); |
| 6674 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6675 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 6676 | // The Qualifiers should be attached to the type rather than the array. |
| 6677 | // Thus we don't call appendQualifier() here. |
| 6678 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 6679 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6680 | appendQualifier(Enc, QT); |
| 6681 | |
| 6682 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 6683 | return appendBuiltinType(Enc, BT); |
| 6684 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6685 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 6686 | return appendPointerType(Enc, PT, CGM, TSC); |
| 6687 | |
| 6688 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 6689 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 6690 | |
| 6691 | if (const RecordType *RT = QT->getAsStructureType()) |
| 6692 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6693 | |
| 6694 | if (const RecordType *RT = QT->getAsUnionType()) |
| 6695 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6696 | |
| 6697 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 6698 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 6699 | |
| 6700 | return false; |
| 6701 | } |
| 6702 | |
| 6703 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6704 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 6705 | if (!D) |
| 6706 | return false; |
| 6707 | |
| 6708 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6709 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 6710 | return false; |
| 6711 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 6712 | } |
| 6713 | |
| 6714 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6715 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 6716 | return false; |
| 6717 | QualType QT = VD->getType().getCanonicalType(); |
| 6718 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 6719 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 6720 | // The Qualifiers should be attached to the type rather than the array. |
| 6721 | // Thus we don't call appendQualifier() here. |
| 6722 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6723 | } |
| 6724 | return appendType(Enc, QT, CGM, TSC); |
| 6725 | } |
| 6726 | return false; |
| 6727 | } |
| 6728 | |
| 6729 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6730 | //===----------------------------------------------------------------------===// |
| 6731 | // Driver code |
| 6732 | //===----------------------------------------------------------------------===// |
| 6733 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6734 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6735 | if (TheTargetCodeGenInfo) |
| 6736 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6737 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6738 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 6739 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6740 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6741 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6742 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 6743 | case llvm::Triple::le32: |
| 6744 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6745 | case llvm::Triple::mips: |
| 6746 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6747 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 6748 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 6749 | case llvm::Triple::mips64: |
| 6750 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6751 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 6752 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 6753 | case llvm::Triple::aarch64: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame^] | 6754 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6755 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 6756 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6757 | Kind = AArch64ABIInfo::DarwinPCS; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6758 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 6759 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6762 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6763 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6764 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6765 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6766 | { |
| 6767 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 6768 | if (getTarget().getABI() == "apcs-gnu") |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6769 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 6770 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6771 | (CodeGenOpts.FloatABI != "soft" && |
| 6772 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6773 | Kind = ARMABIInfo::AAPCS_VFP; |
| 6774 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6775 | switch (Triple.getOS()) { |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 6776 | case llvm::Triple::NaCl: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6777 | return *(TheTargetCodeGenInfo = |
| 6778 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 6779 | default: |
| 6780 | return *(TheTargetCodeGenInfo = |
| 6781 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 6782 | } |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6783 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6784 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6785 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6786 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 6787 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6788 | if (Triple.isOSBinFormatELF()) { |
| 6789 | // FIXME: Should be switchable via command-line option. |
| 6790 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
| 6791 | return *(TheTargetCodeGenInfo = |
| 6792 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind)); |
| 6793 | } else |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 6794 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6795 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 6796 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 6797 | // FIXME: Should be switchable via command-line option. |
| 6798 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
| 6799 | return *(TheTargetCodeGenInfo = |
| 6800 | new PPC64_SVR4_TargetCodeGenInfo(Types, Kind)); |
| 6801 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6802 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 6803 | case llvm::Triple::nvptx: |
| 6804 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6805 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6806 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6807 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6808 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6809 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6810 | case llvm::Triple::systemz: |
| 6811 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 6812 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6813 | case llvm::Triple::tce: |
| 6814 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 6815 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6816 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6817 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 6818 | bool IsSmallStructInRegABI = |
| 6819 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 6820 | bool IsWin32FloatStructABI = Triple.isWindowsMSVCEnvironment(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 6821 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6822 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 6823 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6824 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6825 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6826 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6827 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6828 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6829 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6830 | new X86_32TargetCodeGenInfo(Types, |
| 6831 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6832 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 6833 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6834 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6835 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6836 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6837 | case llvm::Triple::x86_64: { |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 6838 | bool HasAVX = getTarget().getABI() == "avx"; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6839 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6840 | switch (Triple.getOS()) { |
| 6841 | case llvm::Triple::Win32: |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6842 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 6843 | case llvm::Triple::NaCl: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6844 | return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, |
| 6845 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6846 | default: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6847 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, |
| 6848 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6849 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6850 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6851 | case llvm::Triple::hexagon: |
| 6852 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6853 | case llvm::Triple::sparcv9: |
| 6854 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6855 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6856 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6857 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6858 | } |