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 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 51 | static bool isRecordReturnIndirect(const RecordType *RT, |
| 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 false; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 56 | return CXXABI.isReturnTypeIndirect(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 60 | static bool isRecordReturnIndirect(QualType T, 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 false; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 64 | return isRecordReturnIndirect(RT, CXXABI); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 68 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 69 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
| 70 | if (!RD) |
| 71 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 72 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 76 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 77 | const RecordType *RT = T->getAs<RecordType>(); |
| 78 | if (!RT) |
| 79 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 80 | return getRecordArgABI(RT, CXXABI); |
| 81 | } |
| 82 | |
| 83 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 84 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 87 | ASTContext &ABIInfo::getContext() const { |
| 88 | return CGT.getContext(); |
| 89 | } |
| 90 | |
| 91 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 92 | return CGT.getLLVMContext(); |
| 93 | } |
| 94 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 95 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 96 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 99 | const TargetInfo &ABIInfo::getTarget() const { |
| 100 | return CGT.getTarget(); |
| 101 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 102 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 103 | void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 104 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 105 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 106 | switch (TheKind) { |
| 107 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 108 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 109 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 110 | Ty->print(OS); |
| 111 | else |
| 112 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 113 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 114 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 115 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 116 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 117 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 118 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 119 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 120 | case InAlloca: |
| 121 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 122 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 123 | case Indirect: |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 124 | OS << "Indirect Align=" << getIndirectAlign() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 125 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 126 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 127 | break; |
| 128 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 129 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 130 | break; |
| 131 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 132 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 135 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 136 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 137 | // If someone can figure out a general rule for this, that would be great. |
| 138 | // It's probably just doomed to be platform-dependent, though. |
| 139 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 140 | // Verified for: |
| 141 | // x86-64 FreeBSD, Linux, Darwin |
| 142 | // x86-32 FreeBSD, Linux, Darwin |
| 143 | // PowerPC Linux, Darwin |
| 144 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 145 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 146 | return 32; |
| 147 | } |
| 148 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 149 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 150 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 151 | // The following conventions are known to require this to be false: |
| 152 | // x86_stdcall |
| 153 | // MIPS |
| 154 | // For everything else, we just prefer false unless we opt out. |
| 155 | return false; |
| 156 | } |
| 157 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 158 | void |
| 159 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 160 | llvm::SmallString<24> &Opt) const { |
| 161 | // This assumes the user is passing a library name like "rt" instead of a |
| 162 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 163 | // dynamic. |
| 164 | Opt = "-l"; |
| 165 | Opt += Lib; |
| 166 | } |
| 167 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 168 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 169 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 170 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 171 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 172 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 173 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 174 | if (FD->isUnnamedBitfield()) |
| 175 | return true; |
| 176 | |
| 177 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 178 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 179 | // Constant arrays of empty records count as empty, strip them off. |
| 180 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 181 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 182 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 183 | if (AT->getSize() == 0) |
| 184 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 185 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 186 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 188 | const RecordType *RT = FT->getAs<RecordType>(); |
| 189 | if (!RT) |
| 190 | return false; |
| 191 | |
| 192 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 193 | // |
| 194 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 195 | // current ABI. |
| 196 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 197 | return false; |
| 198 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 199 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 202 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 203 | /// fields. Note that a structure with a flexible array member is not |
| 204 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 205 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 206 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 207 | if (!RT) |
| 208 | return 0; |
| 209 | const RecordDecl *RD = RT->getDecl(); |
| 210 | if (RD->hasFlexibleArrayMember()) |
| 211 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 212 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 213 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 214 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 215 | for (const auto &I : CXXRD->bases()) |
| 216 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 217 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 218 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 219 | for (const auto *I : RD->fields()) |
| 220 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 221 | return false; |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | /// isSingleElementStruct - Determine if a structure is a "single |
| 226 | /// element struct", i.e. it has exactly one non-empty field or |
| 227 | /// exactly one field which is itself a single element |
| 228 | /// struct. Structures with flexible array members are never |
| 229 | /// considered single element structs. |
| 230 | /// |
| 231 | /// \return The field declaration for the single non-empty field, if |
| 232 | /// it exists. |
| 233 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
| 234 | const RecordType *RT = T->getAsStructureType(); |
| 235 | if (!RT) |
| 236 | return 0; |
| 237 | |
| 238 | const RecordDecl *RD = RT->getDecl(); |
| 239 | if (RD->hasFlexibleArrayMember()) |
| 240 | return 0; |
| 241 | |
| 242 | const Type *Found = 0; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 243 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 244 | // If this is a C++ record, check the bases first. |
| 245 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 246 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 247 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 248 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 249 | continue; |
| 250 | |
| 251 | // If we already found an element then this isn't a single-element struct. |
| 252 | if (Found) |
| 253 | return 0; |
| 254 | |
| 255 | // If this is non-empty and not a single element struct, the composite |
| 256 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 257 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 258 | if (!Found) |
| 259 | return 0; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 264 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 265 | QualType FT = FD->getType(); |
| 266 | |
| 267 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 268 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 269 | continue; |
| 270 | |
| 271 | // If we already found an element then this isn't a single-element |
| 272 | // struct. |
| 273 | if (Found) |
| 274 | return 0; |
| 275 | |
| 276 | // Treat single element arrays as the element. |
| 277 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 278 | if (AT->getSize().getZExtValue() != 1) |
| 279 | break; |
| 280 | FT = AT->getElementType(); |
| 281 | } |
| 282 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 283 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 284 | Found = FT.getTypePtr(); |
| 285 | } else { |
| 286 | Found = isSingleElementStruct(FT, Context); |
| 287 | if (!Found) |
| 288 | return 0; |
| 289 | } |
| 290 | } |
| 291 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 292 | // We don't consider a struct a single-element struct if it has |
| 293 | // padding beyond the element type. |
| 294 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
| 295 | return 0; |
| 296 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 297 | return Found; |
| 298 | } |
| 299 | |
| 300 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 301 | // Treat complex types as the element type. |
| 302 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 303 | Ty = CTy->getElementType(); |
| 304 | |
| 305 | // Check for a type which we know has a simple scalar argument-passing |
| 306 | // convention without any padding. (We're specifically looking for 32 |
| 307 | // and 64-bit integer and integer-equivalents, float, and double.) |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 308 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
Eli Friedman | a92db67 | 2012-11-29 23:21:04 +0000 | [diff] [blame] | 309 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 310 | return false; |
| 311 | |
| 312 | uint64_t Size = Context.getTypeSize(Ty); |
| 313 | return Size == 32 || Size == 64; |
| 314 | } |
| 315 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 316 | /// canExpandIndirectArgument - Test whether an argument type which is to be |
| 317 | /// passed indirectly (on the stack) would have the equivalent layout if it was |
| 318 | /// expanded into separate arguments. If so, we prefer to do the latter to avoid |
| 319 | /// inhibiting optimizations. |
| 320 | /// |
| 321 | // FIXME: This predicate is missing many cases, currently it just follows |
| 322 | // llvm-gcc (checks that all fields are 32-bit or 64-bit primitive types). We |
| 323 | // should probably make this smarter, or better yet make the LLVM backend |
| 324 | // capable of handling it. |
| 325 | static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { |
| 326 | // We can only expand structure types. |
| 327 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 328 | if (!RT) |
| 329 | return false; |
| 330 | |
| 331 | // We can only expand (C) structures. |
| 332 | // |
| 333 | // FIXME: This needs to be generalized to handle classes as well. |
| 334 | const RecordDecl *RD = RT->getDecl(); |
| 335 | if (!RD->isStruct() || isa<CXXRecordDecl>(RD)) |
| 336 | return false; |
| 337 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 338 | uint64_t Size = 0; |
| 339 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 340 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 341 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 342 | return false; |
| 343 | |
| 344 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 345 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 346 | // counts as "basic" is more complicated than what we were doing previously. |
| 347 | if (FD->isBitField()) |
| 348 | return false; |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 349 | |
| 350 | Size += Context.getTypeSize(FD->getType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Eli Friedman | e5c8562 | 2011-11-18 01:32:26 +0000 | [diff] [blame] | 353 | // Make sure there are not any holes in the struct. |
| 354 | if (Size != Context.getTypeSize(Ty)) |
| 355 | return false; |
| 356 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 357 | return true; |
| 358 | } |
| 359 | |
| 360 | namespace { |
| 361 | /// DefaultABIInfo - The default implementation for ABI specific |
| 362 | /// details. This implementation provides information which results in |
| 363 | /// self-consistent and sensible LLVM IR generation, but does not |
| 364 | /// conform to any particular ABI. |
| 365 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 366 | public: |
| 367 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 369 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 370 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 371 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 372 | void computeInfo(CGFunctionInfo &FI) const override { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 373 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 374 | for (auto &I : FI.arguments()) |
| 375 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 378 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 379 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 380 | }; |
| 381 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 382 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 383 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 384 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 385 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | llvm::Value *DefaultABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 389 | CodeGenFunction &CGF) const { |
| 390 | return 0; |
| 391 | } |
| 392 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 393 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 394 | if (isAggregateTypeForABI(Ty)) { |
Alp Toker | d473363 | 2013-12-05 04:47:09 +0000 | [diff] [blame] | 395 | // Records with non-trivial destructors/constructors should not be passed |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 396 | // by value. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 397 | if (isRecordReturnIndirect(Ty, getCXXABI())) |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 398 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 399 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 400 | return ABIArgInfo::getIndirect(0); |
Jan Wen Voung | 180319f | 2011-11-03 00:59:44 +0000 | [diff] [blame] | 401 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 403 | // Treat an enum type as its underlying type. |
| 404 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 405 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 407 | return (Ty->isPromotableIntegerType() ? |
| 408 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 411 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 412 | if (RetTy->isVoidType()) |
| 413 | return ABIArgInfo::getIgnore(); |
| 414 | |
| 415 | if (isAggregateTypeForABI(RetTy)) |
| 416 | return ABIArgInfo::getIndirect(0); |
| 417 | |
| 418 | // Treat an enum type as its underlying type. |
| 419 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 420 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 421 | |
| 422 | return (RetTy->isPromotableIntegerType() ? |
| 423 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 424 | } |
| 425 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 426 | //===----------------------------------------------------------------------===// |
| 427 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 428 | // |
| 429 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 430 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 431 | //===----------------------------------------------------------------------===// |
| 432 | |
| 433 | class PNaClABIInfo : public ABIInfo { |
| 434 | public: |
| 435 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 436 | |
| 437 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 438 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 439 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 440 | void computeInfo(CGFunctionInfo &FI) const override; |
| 441 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 442 | CodeGenFunction &CGF) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 443 | }; |
| 444 | |
| 445 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 446 | public: |
| 447 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 448 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 449 | }; |
| 450 | |
| 451 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 452 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 453 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 454 | for (auto &I : FI.arguments()) |
| 455 | I.info = classifyArgumentType(I.type); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | llvm::Value *PNaClABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 459 | CodeGenFunction &CGF) const { |
| 460 | return 0; |
| 461 | } |
| 462 | |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 463 | /// \brief Classify argument of given type \p Ty. |
| 464 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 465 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 466 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 467 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 468 | return ABIArgInfo::getIndirect(0); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 469 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 470 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 471 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 472 | } else if (Ty->isFloatingType()) { |
| 473 | // Floating-point types don't go inreg. |
| 474 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 475 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 476 | |
| 477 | return (Ty->isPromotableIntegerType() ? |
| 478 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 482 | if (RetTy->isVoidType()) |
| 483 | return ABIArgInfo::getIgnore(); |
| 484 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 485 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 486 | if (isAggregateTypeForABI(RetTy)) |
| 487 | return ABIArgInfo::getIndirect(0); |
| 488 | |
| 489 | // Treat an enum type as its underlying type. |
| 490 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 491 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 492 | |
| 493 | return (RetTy->isPromotableIntegerType() ? |
| 494 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 495 | } |
| 496 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 497 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 498 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 499 | // 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] | 500 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 501 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 502 | IRType->getScalarSizeInBits() != 64; |
| 503 | } |
| 504 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 505 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 506 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 507 | llvm::Type* Ty) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 508 | if ((Constraint == "y" || Constraint == "&y") && Ty->isVectorTy()) { |
| 509 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 510 | // Invalid MMX constraint |
| 511 | return 0; |
| 512 | } |
| 513 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 514 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 518 | return Ty; |
| 519 | } |
| 520 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 521 | //===----------------------------------------------------------------------===// |
| 522 | // X86-32 ABI Implementation |
| 523 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 524 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 525 | /// \brief Similar to llvm::CCState, but for Clang. |
| 526 | struct CCState { |
| 527 | CCState(unsigned CC) : CC(CC), FreeRegs(0) {} |
| 528 | |
| 529 | unsigned CC; |
| 530 | unsigned FreeRegs; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 531 | unsigned StackOffset; |
| 532 | bool UseInAlloca; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 533 | }; |
| 534 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 535 | /// X86_32ABIInfo - The X86-32 ABI information. |
| 536 | class X86_32ABIInfo : public ABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 537 | enum Class { |
| 538 | Integer, |
| 539 | Float |
| 540 | }; |
| 541 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 542 | static const unsigned MinABIStackAlignInBytes = 4; |
| 543 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 544 | bool IsDarwinVectorABI; |
| 545 | bool IsSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 546 | bool IsWin32StructABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 547 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 548 | |
| 549 | static bool isRegisterSize(unsigned Size) { |
| 550 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 551 | } |
| 552 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 553 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 554 | bool IsInstanceMethod) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 555 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 556 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 557 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 558 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 559 | |
| 560 | ABIArgInfo getIndirectReturnResult(CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 561 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 562 | /// \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] | 563 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 564 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 565 | Class classify(QualType Ty) const; |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 566 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State, |
| 567 | bool IsInstanceMethod) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 568 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 569 | bool shouldUseInReg(QualType Ty, CCState &State, bool &NeedsPadding) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 570 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 571 | /// \brief Rewrite the function info so that all memory arguments use |
| 572 | /// inalloca. |
| 573 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 574 | |
| 575 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 576 | unsigned &StackOffset, ABIArgInfo &Info, |
| 577 | QualType Type) const; |
| 578 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 579 | public: |
| 580 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 581 | void computeInfo(CGFunctionInfo &FI) const override; |
| 582 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 583 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 584 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 585 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool d, bool p, bool w, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 586 | unsigned r) |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 587 | : ABIInfo(CGT), IsDarwinVectorABI(d), IsSmallStructInRegABI(p), |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 588 | IsWin32StructABI(w), DefaultNumRegisterParameters(r) {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 589 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 590 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 591 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 592 | public: |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 593 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 594 | bool d, bool p, bool w, unsigned r) |
| 595 | :TargetCodeGenInfo(new X86_32ABIInfo(CGT, d, p, w, r)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 596 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 597 | static bool isStructReturnInRegABI( |
| 598 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 599 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 600 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 601 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 602 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 603 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 604 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 605 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 606 | return 4; |
| 607 | } |
| 608 | |
| 609 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 610 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 611 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 612 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 613 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 614 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 615 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 616 | } |
| 617 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 618 | llvm::Constant * |
| 619 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 620 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 621 | (0x06 << 8) | // .+0x08 |
| 622 | ('F' << 16) | |
| 623 | ('T' << 24); |
| 624 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 625 | } |
| 626 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
| 629 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 630 | |
| 631 | /// shouldReturnTypeInRegister - Determine if the given type should be |
| 632 | /// passed in a register (for the Darwin ABI). |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 633 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, ASTContext &Context, |
| 634 | bool IsInstanceMethod) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 635 | uint64_t Size = Context.getTypeSize(Ty); |
| 636 | |
| 637 | // Type must be register sized. |
| 638 | if (!isRegisterSize(Size)) |
| 639 | return false; |
| 640 | |
| 641 | if (Ty->isVectorType()) { |
| 642 | // 64- and 128- bit vectors inside structures are not returned in |
| 643 | // registers. |
| 644 | if (Size == 64 || Size == 128) |
| 645 | return false; |
| 646 | |
| 647 | return true; |
| 648 | } |
| 649 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 650 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 651 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 652 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 653 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 654 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 655 | return true; |
| 656 | |
| 657 | // Arrays are treated like records. |
| 658 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 659 | return shouldReturnTypeInRegister(AT->getElementType(), Context, |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 660 | IsInstanceMethod); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 661 | |
| 662 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 663 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 664 | if (!RT) return false; |
| 665 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 666 | // FIXME: Traverse bases here too. |
| 667 | |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 668 | // For thiscall conventions, structures will never be returned in |
| 669 | // a register. This is for compatibility with the MSVC ABI |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 670 | if (IsWin32StructABI && IsInstanceMethod && RT->isStructureType()) |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 671 | return false; |
Aaron Ballman | 3c42441 | 2012-02-22 03:04:13 +0000 | [diff] [blame] | 672 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 673 | // Structure types are passed in register if all fields would be |
| 674 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 675 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 676 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 677 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 678 | continue; |
| 679 | |
| 680 | // Check fields recursively. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 681 | if (!shouldReturnTypeInRegister(FD->getType(), Context, IsInstanceMethod)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 682 | return false; |
| 683 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 684 | return true; |
| 685 | } |
| 686 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 687 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(CCState &State) const { |
| 688 | // If the return value is indirect, then the hidden argument is consuming one |
| 689 | // integer register. |
| 690 | if (State.FreeRegs) { |
| 691 | --State.FreeRegs; |
| 692 | return ABIArgInfo::getIndirectInReg(/*Align=*/0, /*ByVal=*/false); |
| 693 | } |
| 694 | return ABIArgInfo::getIndirect(/*Align=*/0, /*ByVal=*/false); |
| 695 | } |
| 696 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 697 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, CCState &State, |
| 698 | bool IsInstanceMethod) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 699 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 700 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 702 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 703 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 704 | if (IsDarwinVectorABI) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 705 | uint64_t Size = getContext().getTypeSize(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 706 | |
| 707 | // 128-bit vectors are a special case; they are returned in |
| 708 | // registers and we need to make sure to pick a type the LLVM |
| 709 | // backend will like. |
| 710 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 711 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 712 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 713 | |
| 714 | // Always return in register if it fits in a general purpose |
| 715 | // register, or if it is 64 bits and has a single element. |
| 716 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 717 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 718 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 719 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 720 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 721 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 725 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 726 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 727 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 728 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 729 | if (isRecordReturnIndirect(RT, getCXXABI())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 730 | return getIndirectReturnResult(State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 731 | |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 732 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 733 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 734 | return getIndirectReturnResult(State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 735 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 736 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 737 | // If specified, structs and unions are always indirect. |
| 738 | if (!IsSmallStructInRegABI && !RetTy->isAnyComplexType()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 739 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 740 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 741 | // Small structures which are register sized are generally returned |
| 742 | // in a register. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 743 | if (shouldReturnTypeInRegister(RetTy, getContext(), IsInstanceMethod)) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 744 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 745 | |
| 746 | // As a special-case, if the struct is a "single-element" struct, and |
| 747 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 748 | // floating-point register. (MSVC does not apply this special case.) |
| 749 | // We apply a similar transformation for pointer types to improve the |
| 750 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 751 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 752 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 753 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 754 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 755 | |
| 756 | // FIXME: We should be able to narrow this integer in cases with dead |
| 757 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 758 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 761 | return getIndirectReturnResult(State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 762 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 764 | // Treat an enum type as its underlying type. |
| 765 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 766 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 767 | |
| 768 | return (RetTy->isPromotableIntegerType() ? |
| 769 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 772 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 773 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 774 | } |
| 775 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 776 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 777 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 778 | if (!RT) |
| 779 | return 0; |
| 780 | const RecordDecl *RD = RT->getDecl(); |
| 781 | |
| 782 | // If this is a C++ record, check the bases first. |
| 783 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 784 | for (const auto &I : CXXRD->bases()) |
| 785 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 786 | return false; |
| 787 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 788 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 789 | QualType FT = i->getType(); |
| 790 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 791 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 792 | return true; |
| 793 | |
| 794 | if (isRecordWithSSEVectorType(Context, FT)) |
| 795 | return true; |
| 796 | } |
| 797 | |
| 798 | return false; |
| 799 | } |
| 800 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 801 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 802 | unsigned Align) const { |
| 803 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 804 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 805 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 806 | return 0; // Use default alignment. |
| 807 | |
| 808 | // On non-Darwin, the stack type alignment is always 4. |
| 809 | if (!IsDarwinVectorABI) { |
| 810 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 811 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 812 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 813 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 814 | // 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] | 815 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 816 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 817 | return 16; |
| 818 | |
| 819 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 822 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 823 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 824 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 825 | if (State.FreeRegs) { |
| 826 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 827 | return ABIArgInfo::getIndirectInReg(0, false); |
| 828 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 829 | return ABIArgInfo::getIndirect(0, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 830 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 832 | // Compute the byval alignment. |
| 833 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 834 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 835 | if (StackAlign == 0) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 836 | return ABIArgInfo::getIndirect(4, /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 837 | |
| 838 | // If the stack alignment is less than the type alignment, realign the |
| 839 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 840 | bool Realign = TypeAlign > StackAlign; |
| 841 | return ABIArgInfo::getIndirect(StackAlign, /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 844 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 845 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 846 | if (!T) |
| 847 | T = Ty.getTypePtr(); |
| 848 | |
| 849 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 850 | BuiltinType::Kind K = BT->getKind(); |
| 851 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 852 | return Float; |
| 853 | } |
| 854 | return Integer; |
| 855 | } |
| 856 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 857 | bool X86_32ABIInfo::shouldUseInReg(QualType Ty, CCState &State, |
| 858 | bool &NeedsPadding) const { |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 859 | NeedsPadding = false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 860 | Class C = classify(Ty); |
| 861 | if (C == Float) |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 862 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 863 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 864 | unsigned Size = getContext().getTypeSize(Ty); |
| 865 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 866 | |
| 867 | if (SizeInRegs == 0) |
| 868 | return false; |
| 869 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 870 | if (SizeInRegs > State.FreeRegs) { |
| 871 | State.FreeRegs = 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 872 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 873 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 874 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 875 | State.FreeRegs -= SizeInRegs; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 876 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 877 | if (State.CC == llvm::CallingConv::X86_FastCall) { |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 878 | if (Size > 32) |
| 879 | return false; |
| 880 | |
| 881 | if (Ty->isIntegralOrEnumerationType()) |
| 882 | return true; |
| 883 | |
| 884 | if (Ty->isPointerType()) |
| 885 | return true; |
| 886 | |
| 887 | if (Ty->isReferenceType()) |
| 888 | return true; |
| 889 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 890 | if (State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 891 | NeedsPadding = true; |
| 892 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 893 | return false; |
| 894 | } |
| 895 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 896 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 899 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 900 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 901 | // FIXME: Set alignment on indirect arguments. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 902 | if (isAggregateTypeForABI(Ty)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 903 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 904 | // Check with the C++ ABI first. |
| 905 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 906 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 907 | return getIndirectResult(Ty, false, State); |
| 908 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 909 | // The field index doesn't matter, we'll fix it up later. |
| 910 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 911 | } |
| 912 | |
| 913 | // Structs are always byval on win32, regardless of what they contain. |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 914 | if (IsWin32StructABI) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 915 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 916 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 917 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 918 | if (RT->getDecl()->hasFlexibleArrayMember()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 919 | return getIndirectResult(Ty, true, State); |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 920 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 921 | |
Eli Friedman | 9f061a3 | 2011-11-18 00:28:11 +0000 | [diff] [blame] | 922 | // Ignore empty structs/unions. |
Eli Friedman | f22fa9e | 2011-11-18 04:01:36 +0000 | [diff] [blame] | 923 | if (isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 924 | return ABIArgInfo::getIgnore(); |
| 925 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 926 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 927 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 928 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 929 | if (shouldUseInReg(Ty, State, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 930 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 931 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 932 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 933 | return ABIArgInfo::getDirectInReg(Result); |
| 934 | } |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 935 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : 0; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 936 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 937 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 938 | // of those arguments will match the struct. This is important because the |
| 939 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 940 | // optimizations. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 941 | if (getContext().getTypeSize(Ty) <= 4*32 && |
| 942 | canExpandIndirectArgument(Ty, getContext())) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 943 | return ABIArgInfo::getExpandWithPadding( |
| 944 | State.CC == llvm::CallingConv::X86_FastCall, PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 945 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 946 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 949 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 950 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 951 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 952 | if (IsDarwinVectorABI) { |
| 953 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 954 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 955 | (Size == 64 && VT->getNumElements() == 1)) |
| 956 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 957 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 958 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 959 | |
Chad Rosier | 651c183 | 2013-03-25 21:00:27 +0000 | [diff] [blame] | 960 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 961 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 962 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 963 | return ABIArgInfo::getDirect(); |
| 964 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 965 | |
| 966 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 967 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 968 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 969 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 970 | bool NeedsPadding; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 971 | bool InReg = shouldUseInReg(Ty, State, NeedsPadding); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 972 | |
| 973 | if (Ty->isPromotableIntegerType()) { |
| 974 | if (InReg) |
| 975 | return ABIArgInfo::getExtendInReg(); |
| 976 | return ABIArgInfo::getExtend(); |
| 977 | } |
| 978 | if (InReg) |
| 979 | return ABIArgInfo::getDirectInReg(); |
| 980 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 983 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 984 | CCState State(FI.getCallingConvention()); |
| 985 | if (State.CC == llvm::CallingConv::X86_FastCall) |
| 986 | State.FreeRegs = 2; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 987 | else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 988 | State.FreeRegs = FI.getRegParm(); |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 989 | else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 990 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 991 | |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 992 | FI.getReturnInfo() = |
| 993 | classifyReturnType(FI.getReturnType(), State, FI.isInstanceMethod()); |
| 994 | |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 995 | // On win32, swap the order of the first two parameters for instance methods |
| 996 | // which are sret behind the scenes to match MSVC. |
Reid Kleckner | 4982b82 | 2014-01-31 22:54:50 +0000 | [diff] [blame] | 997 | if (IsWin32StructABI && FI.isInstanceMethod() && |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 998 | FI.getReturnInfo().isIndirect()) { |
| 999 | assert(FI.arg_size() >= 1 && "instance method should have this"); |
| 1000 | FI.getReturnInfo().setSRetAfterThis(true); |
| 1001 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1002 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1003 | bool UsedInAlloca = false; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 1004 | for (auto &I : FI.arguments()) { |
| 1005 | I.info = classifyArgumentType(I.type, State); |
| 1006 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1010 | // all the memory arguments to use inalloca. |
| 1011 | if (UsedInAlloca) |
| 1012 | rewriteWithInAlloca(FI); |
| 1013 | } |
| 1014 | |
| 1015 | void |
| 1016 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
| 1017 | unsigned &StackOffset, |
| 1018 | ABIArgInfo &Info, QualType Type) const { |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1019 | assert(StackOffset % 4U == 0 && "unaligned inalloca struct"); |
| 1020 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1021 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
| 1022 | StackOffset += getContext().getTypeSizeInChars(Type).getQuantity(); |
| 1023 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1024 | // Insert padding bytes to respect alignment. For x86_32, each argument is 4 |
| 1025 | // byte aligned. |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1026 | if (StackOffset % 4U) { |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1027 | unsigned OldOffset = StackOffset; |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1028 | StackOffset = llvm::RoundUpToAlignment(StackOffset, 4U); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1029 | unsigned NumBytes = StackOffset - OldOffset; |
| 1030 | assert(NumBytes); |
| 1031 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
| 1032 | Ty = llvm::ArrayType::get(Ty, NumBytes); |
| 1033 | FrameFields.push_back(Ty); |
| 1034 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1038 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1039 | |
| 1040 | // Build a packed struct type for all of the arguments in memory. |
| 1041 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1042 | |
| 1043 | unsigned StackOffset = 0; |
| 1044 | |
| 1045 | // Put the sret parameter into the inalloca struct if it's in memory. |
| 1046 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1047 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1048 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1049 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1050 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1051 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | // Skip the 'this' parameter in ecx. |
| 1055 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1056 | if (FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall) |
| 1057 | ++I; |
| 1058 | |
| 1059 | // Put arguments passed in memory into the struct. |
| 1060 | for (; I != E; ++I) { |
| 1061 | |
| 1062 | // Leave ignored and inreg arguments alone. |
| 1063 | switch (I->info.getKind()) { |
| 1064 | case ABIArgInfo::Indirect: |
| 1065 | assert(I->info.getIndirectByVal()); |
| 1066 | break; |
| 1067 | case ABIArgInfo::Ignore: |
| 1068 | continue; |
| 1069 | case ABIArgInfo::Direct: |
| 1070 | case ABIArgInfo::Extend: |
| 1071 | if (I->info.getInReg()) |
| 1072 | continue; |
| 1073 | break; |
| 1074 | default: |
| 1075 | break; |
| 1076 | } |
| 1077 | |
| 1078 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1079 | } |
| 1080 | |
| 1081 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
| 1082 | /*isPacked=*/true)); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1085 | llvm::Value *X86_32ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1086 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1087 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1088 | |
| 1089 | CGBuilderTy &Builder = CGF.Builder; |
| 1090 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 1091 | "ap"); |
| 1092 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1093 | |
| 1094 | // Compute if the address needs to be aligned |
| 1095 | unsigned Align = CGF.getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 1096 | Align = getTypeStackAlignInBytes(Ty, Align); |
| 1097 | Align = std::max(Align, 4U); |
| 1098 | if (Align > 4) { |
| 1099 | // addr = (addr + align - 1) & -align; |
| 1100 | llvm::Value *Offset = |
| 1101 | llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 1102 | Addr = CGF.Builder.CreateGEP(Addr, Offset); |
| 1103 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(Addr, |
| 1104 | CGF.Int32Ty); |
| 1105 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int32Ty, -Align); |
| 1106 | Addr = CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 1107 | Addr->getType(), |
| 1108 | "ap.cur.aligned"); |
| 1109 | } |
| 1110 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1111 | llvm::Type *PTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1112 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1113 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 1114 | |
| 1115 | uint64_t Offset = |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1116 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1117 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 1118 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1119 | "ap.next"); |
| 1120 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 1121 | |
| 1122 | return AddrTyped; |
| 1123 | } |
| 1124 | |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1125 | void X86_32TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 1126 | llvm::GlobalValue *GV, |
| 1127 | CodeGen::CodeGenModule &CGM) const { |
| 1128 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 1129 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
| 1130 | // Get the LLVM function. |
| 1131 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1132 | |
| 1133 | // Now add the 'alignstack' attribute with a value of 16. |
Bill Wendling | a514ebc | 2012-10-15 20:36:26 +0000 | [diff] [blame] | 1134 | llvm::AttrBuilder B; |
Bill Wendling | ccf94c9 | 2012-10-14 03:28:14 +0000 | [diff] [blame] | 1135 | B.addStackAlignmentAttr(16); |
Bill Wendling | 9a67792 | 2013-01-23 00:21:06 +0000 | [diff] [blame] | 1136 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 1137 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 1138 | llvm::AttributeSet::FunctionIndex, |
| 1139 | B)); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1144 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 1145 | CodeGen::CodeGenFunction &CGF, |
| 1146 | llvm::Value *Address) const { |
| 1147 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1148 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1149 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1150 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1151 | // 0-7 are the eight integer registers; the order is different |
| 1152 | // on Darwin (for EH), but the range is the same. |
| 1153 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1154 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1155 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1156 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1157 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 1158 | // These have size 16, which is sizeof(long double) on |
| 1159 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1160 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1161 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1162 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1163 | } else { |
| 1164 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 1165 | // reason. |
| 1166 | Builder.CreateStore(Four8, Builder.CreateConstInBoundsGEP1_32(Address, 9)); |
| 1167 | |
| 1168 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 1169 | // These have size 12, which is sizeof(long double) on |
| 1170 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1171 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1172 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 1173 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1174 | |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 1178 | //===----------------------------------------------------------------------===// |
| 1179 | // X86-64 ABI Implementation |
| 1180 | //===----------------------------------------------------------------------===// |
| 1181 | |
| 1182 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1183 | namespace { |
| 1184 | /// X86_64ABIInfo - The X86_64 ABI information. |
| 1185 | class X86_64ABIInfo : public ABIInfo { |
| 1186 | enum Class { |
| 1187 | Integer = 0, |
| 1188 | SSE, |
| 1189 | SSEUp, |
| 1190 | X87, |
| 1191 | X87Up, |
| 1192 | ComplexX87, |
| 1193 | NoClass, |
| 1194 | Memory |
| 1195 | }; |
| 1196 | |
| 1197 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 1198 | /// |
| 1199 | /// Merge an accumulating classification \arg Accum with a field |
| 1200 | /// classification \arg Field. |
| 1201 | /// |
| 1202 | /// \param Accum - The accumulating classification. This should |
| 1203 | /// always be either NoClass or the result of a previous merge |
| 1204 | /// call. In addition, this should never be Memory (the caller |
| 1205 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1206 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1207 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1208 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 1209 | /// |
| 1210 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 1211 | /// final MEMORY or SSE classes when necessary. |
| 1212 | /// |
| 1213 | /// \param AggregateSize - The size of the current aggregate in |
| 1214 | /// the classification process. |
| 1215 | /// |
| 1216 | /// \param Lo - The classification for the parts of the type |
| 1217 | /// residing in the low word of the containing object. |
| 1218 | /// |
| 1219 | /// \param Hi - The classification for the parts of the type |
| 1220 | /// residing in the higher words of the containing object. |
| 1221 | /// |
| 1222 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 1223 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1224 | /// classify - Determine the x86_64 register classes in which the |
| 1225 | /// given type T should be passed. |
| 1226 | /// |
| 1227 | /// \param Lo - The classification for the parts of the type |
| 1228 | /// residing in the low word of the containing object. |
| 1229 | /// |
| 1230 | /// \param Hi - The classification for the parts of the type |
| 1231 | /// residing in the high word of the containing object. |
| 1232 | /// |
| 1233 | /// \param OffsetBase - The bit offset of this type in the |
| 1234 | /// containing object. Some parameters are classified different |
| 1235 | /// depending on whether they straddle an eightbyte boundary. |
| 1236 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1237 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 1238 | /// argument, as used in AMD64-ABI 3.5.7. |
| 1239 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1240 | /// If a word is unused its result will be NoClass; if a type should |
| 1241 | /// be passed in Memory then at least the classification of \arg Lo |
| 1242 | /// will be Memory. |
| 1243 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1244 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1245 | /// |
| 1246 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 1247 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1248 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 1249 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1250 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1251 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1252 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 1253 | unsigned IROffset, QualType SourceTy, |
| 1254 | unsigned SourceOffset) const; |
| 1255 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 1256 | unsigned IROffset, QualType SourceTy, |
| 1257 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1258 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1259 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1260 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1261 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1262 | |
| 1263 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1264 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1265 | /// |
| 1266 | /// \param freeIntRegs - The number of free integer registers remaining |
| 1267 | /// available. |
| 1268 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1270 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1271 | |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1272 | ABIArgInfo classifyArgumentType(QualType Ty, |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1273 | unsigned freeIntRegs, |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1274 | unsigned &neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1275 | unsigned &neededSSE, |
| 1276 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1277 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1278 | bool IsIllegalVectorType(QualType Ty) const; |
| 1279 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1280 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 1281 | /// unfortunately in ways that were not always consistent with |
| 1282 | /// certain previous compilers. In particular, platforms which |
| 1283 | /// required strict binary compatibility with older versions of GCC |
| 1284 | /// may need to exempt themselves. |
| 1285 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1286 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1289 | bool HasAVX; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1290 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 1291 | // 64-bit hardware. |
| 1292 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1293 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1294 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1295 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool hasavx) : |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1296 | ABIInfo(CGT), HasAVX(hasavx), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 1297 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1298 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1299 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1300 | bool isPassedUsingAVXType(QualType type) const { |
| 1301 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1302 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1303 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 1304 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1305 | if (info.isDirect()) { |
| 1306 | llvm::Type *ty = info.getCoerceToType(); |
| 1307 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 1308 | return (vectorTy->getBitWidth() > 128); |
| 1309 | } |
| 1310 | return false; |
| 1311 | } |
| 1312 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1313 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1314 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1315 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1316 | CodeGenFunction &CGF) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1317 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1318 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1319 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1320 | class WinX86_64ABIInfo : public ABIInfo { |
| 1321 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1322 | ABIArgInfo classify(QualType Ty, bool IsReturnType) const; |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1323 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1324 | public: |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 1325 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 1326 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1327 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1328 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1329 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 1330 | CodeGenFunction &CGF) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1331 | }; |
| 1332 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1333 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1334 | public: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1335 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1336 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, HasAVX)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1337 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1338 | const X86_64ABIInfo &getABIInfo() const { |
| 1339 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 1340 | } |
| 1341 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1342 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1343 | return 7; |
| 1344 | } |
| 1345 | |
| 1346 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1347 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1348 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1349 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 1350 | // 0-15 are the 16 integer registers. |
| 1351 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1352 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1353 | return false; |
| 1354 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1355 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1356 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1357 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1358 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1359 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1360 | } |
| 1361 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1362 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1363 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1364 | // The default CC on x86-64 sets %al to the number of SSA |
| 1365 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1366 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 1367 | // that when AVX types are involved: the ABI explicitly states it is |
| 1368 | // undefined, and it doesn't work in practice because of how the ABI |
| 1369 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 1370 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1371 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1372 | for (CallArgList::const_iterator |
| 1373 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 1374 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 1375 | HasAVXType = true; |
| 1376 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1379 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 1380 | if (!HasAVXType) |
| 1381 | return true; |
| 1382 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1383 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1384 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1387 | llvm::Constant * |
| 1388 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1389 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1390 | (0x0a << 8) | // .+0x0c |
| 1391 | ('F' << 16) | |
| 1392 | ('T' << 24); |
| 1393 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1394 | } |
| 1395 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1396 | }; |
| 1397 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1398 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
| 1399 | // If the argument does not end in .lib, automatically add the suffix. This |
| 1400 | // matches the behavior of MSVC. |
| 1401 | std::string ArgStr = Lib; |
Rui Ueyama | 727025a | 2013-10-31 19:12:53 +0000 | [diff] [blame] | 1402 | if (!Lib.endswith_lower(".lib")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1403 | ArgStr += ".lib"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1404 | return ArgStr; |
| 1405 | } |
| 1406 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1407 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 1408 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1409 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 1410 | bool d, bool p, bool w, unsigned RegParms) |
| 1411 | : X86_32TargetCodeGenInfo(CGT, d, p, w, RegParms) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1412 | |
| 1413 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1414 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1415 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1416 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1417 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1418 | |
| 1419 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1420 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1421 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1422 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1423 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1424 | }; |
| 1425 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1426 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1427 | public: |
| 1428 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 1429 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT)) {} |
| 1430 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1431 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1432 | return 7; |
| 1433 | } |
| 1434 | |
| 1435 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1436 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1437 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1439 | // 0-15 are the 16 integer registers. |
| 1440 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1441 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1442 | return false; |
| 1443 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1444 | |
| 1445 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1446 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1447 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 1448 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 1449 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1450 | |
| 1451 | void getDetectMismatchOption(llvm::StringRef Name, |
| 1452 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1453 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 1454 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 1455 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 1456 | }; |
| 1457 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1460 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 1461 | Class &Hi) const { |
| 1462 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 1463 | // |
| 1464 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 1465 | // memory. |
| 1466 | // |
| 1467 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 1468 | // memory. |
| 1469 | // |
| 1470 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 1471 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 1472 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 1473 | // ABI working for processors that don't support the __m256 type. |
| 1474 | // |
| 1475 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 1476 | // |
| 1477 | // Some of these are enforced by the merging logic. Others can arise |
| 1478 | // only with unions; for example: |
| 1479 | // union { _Complex double; unsigned; } |
| 1480 | // |
| 1481 | // Note that clauses (b) and (c) were added in 0.98. |
| 1482 | // |
| 1483 | if (Hi == Memory) |
| 1484 | Lo = Memory; |
| 1485 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 1486 | Lo = Memory; |
| 1487 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 1488 | Lo = Memory; |
| 1489 | if (Hi == SSEUp && Lo != SSE) |
| 1490 | Hi = SSE; |
| 1491 | } |
| 1492 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1493 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1494 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 1495 | // classified recursively so that always two fields are |
| 1496 | // considered. The resulting class is calculated according to |
| 1497 | // the classes of the fields in the eightbyte: |
| 1498 | // |
| 1499 | // (a) If both classes are equal, this is the resulting class. |
| 1500 | // |
| 1501 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 1502 | // the other class. |
| 1503 | // |
| 1504 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 1505 | // class. |
| 1506 | // |
| 1507 | // (d) If one of the classes is INTEGER, the result is the |
| 1508 | // INTEGER. |
| 1509 | // |
| 1510 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 1511 | // MEMORY is used as class. |
| 1512 | // |
| 1513 | // (f) Otherwise class SSE is used. |
| 1514 | |
| 1515 | // Accum should never be memory (we should have returned) or |
| 1516 | // ComplexX87 (because this cannot be passed in a structure). |
| 1517 | assert((Accum != Memory && Accum != ComplexX87) && |
| 1518 | "Invalid accumulated classification during merge."); |
| 1519 | if (Accum == Field || Field == NoClass) |
| 1520 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1521 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1522 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1523 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1524 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1525 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1526 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1527 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 1528 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1529 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1530 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 1533 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1534 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1535 | // FIXME: This code can be simplified by introducing a simple value class for |
| 1536 | // Class pairs with appropriate constructor methods for the various |
| 1537 | // situations. |
| 1538 | |
| 1539 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 1540 | // shouldn't be passed in registers for example, so there is no chance they |
| 1541 | // can straddle an eightbyte. Verify & simplify. |
| 1542 | |
| 1543 | Lo = Hi = NoClass; |
| 1544 | |
| 1545 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 1546 | Current = Memory; |
| 1547 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1548 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1549 | BuiltinType::Kind k = BT->getKind(); |
| 1550 | |
| 1551 | if (k == BuiltinType::Void) { |
| 1552 | Current = NoClass; |
| 1553 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 1554 | Lo = Integer; |
| 1555 | Hi = Integer; |
| 1556 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 1557 | Current = Integer; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1558 | } else if ((k == BuiltinType::Float || k == BuiltinType::Double) || |
| 1559 | (k == BuiltinType::LongDouble && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1560 | getTarget().getTriple().isOSNaCl())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1561 | Current = SSE; |
| 1562 | } else if (k == BuiltinType::LongDouble) { |
| 1563 | Lo = X87; |
| 1564 | Hi = X87Up; |
| 1565 | } |
| 1566 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 1567 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1568 | return; |
| 1569 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1570 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1571 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1572 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1573 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
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 (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1578 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1579 | return; |
| 1580 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1581 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1582 | if (Ty->isMemberPointerType()) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 1583 | if (Ty->isMemberFunctionPointerType() && Has64BitPointers) |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 1584 | Lo = Hi = Integer; |
| 1585 | else |
| 1586 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1587 | return; |
| 1588 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1589 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1590 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1591 | uint64_t Size = getContext().getTypeSize(VT); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1592 | if (Size == 32) { |
| 1593 | // gcc passes all <4 x char>, <2 x short>, <1 x int>, <1 x |
| 1594 | // float> as integer. |
| 1595 | Current = Integer; |
| 1596 | |
| 1597 | // If this type crosses an eightbyte boundary, it should be |
| 1598 | // split. |
| 1599 | uint64_t EB_Real = (OffsetBase) / 64; |
| 1600 | uint64_t EB_Imag = (OffsetBase + Size - 1) / 64; |
| 1601 | if (EB_Real != EB_Imag) |
| 1602 | Hi = Lo; |
| 1603 | } else if (Size == 64) { |
| 1604 | // gcc passes <1 x double> in memory. :( |
| 1605 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) |
| 1606 | return; |
| 1607 | |
| 1608 | // gcc passes <1 x long long> as INTEGER. |
Chris Lattner | 46830f2 | 2010-08-26 18:03:20 +0000 | [diff] [blame] | 1609 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::LongLong) || |
Chris Lattner | 69e683f | 2010-08-26 18:13:50 +0000 | [diff] [blame] | 1610 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 1611 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::Long) || |
| 1612 | VT->getElementType()->isSpecificBuiltinType(BuiltinType::ULong)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1613 | Current = Integer; |
| 1614 | else |
| 1615 | Current = SSE; |
| 1616 | |
| 1617 | // If this type crosses an eightbyte boundary, it should be |
| 1618 | // split. |
| 1619 | if (OffsetBase && OffsetBase != 64) |
| 1620 | Hi = Lo; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1621 | } else if (Size == 128 || (HasAVX && isNamedArg && Size == 256)) { |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1622 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 1623 | // least significant one belongs to class SSE and all the others to class |
| 1624 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 1625 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 1626 | // This design isn't correct for 256-bits, but since there're no cases |
| 1627 | // where the upper parts would need to be inspected, avoid adding |
| 1628 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1629 | // |
| 1630 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 1631 | // registers if they are "named", i.e. not part of the "..." of a |
| 1632 | // variadic function. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1633 | Lo = SSE; |
| 1634 | Hi = SSEUp; |
| 1635 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1636 | return; |
| 1637 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1639 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1640 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1641 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1642 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1643 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1644 | if (Size <= 64) |
| 1645 | Current = Integer; |
| 1646 | else if (Size <= 128) |
| 1647 | Lo = Hi = Integer; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1648 | } else if (ET == getContext().FloatTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1649 | Current = SSE; |
Derek Schuff | 57b7e8f | 2012-10-11 16:55:58 +0000 | [diff] [blame] | 1650 | else if (ET == getContext().DoubleTy || |
| 1651 | (ET == getContext().LongDoubleTy && |
Cameron Esfahani | 556d91e | 2013-09-14 01:09:11 +0000 | [diff] [blame] | 1652 | getTarget().getTriple().isOSNaCl())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1653 | Lo = Hi = SSE; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1654 | else if (ET == getContext().LongDoubleTy) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1655 | Current = ComplexX87; |
| 1656 | |
| 1657 | // If this complex type crosses an eightbyte boundary then it |
| 1658 | // should be split. |
| 1659 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1660 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1661 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 1662 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1664 | return; |
| 1665 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1667 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1668 | // Arrays are treated like structures. |
| 1669 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1670 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1671 | |
| 1672 | // 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] | 1673 | // than four eightbytes, ..., it has class MEMORY. |
| 1674 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1675 | return; |
| 1676 | |
| 1677 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 1678 | // fields, it has class MEMORY. |
| 1679 | // |
| 1680 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1681 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1682 | return; |
| 1683 | |
| 1684 | // Otherwise implement simplified merge. We could be smarter about |
| 1685 | // this, but it isn't worth it and would be harder to verify. |
| 1686 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1687 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1688 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 1689 | |
| 1690 | // The only case a 256-bit wide vector could be used is when the array |
| 1691 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1692 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1693 | if (Size > 128 && EltSize != 256) |
| 1694 | return; |
| 1695 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1696 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 1697 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1698 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1699 | Lo = merge(Lo, FieldLo); |
| 1700 | Hi = merge(Hi, FieldHi); |
| 1701 | if (Lo == Memory || Hi == Memory) |
| 1702 | break; |
| 1703 | } |
| 1704 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1705 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1706 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1707 | return; |
| 1708 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1709 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 1710 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1711 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1712 | |
| 1713 | // 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] | 1714 | // than four eightbytes, ..., it has class MEMORY. |
| 1715 | if (Size > 256) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1716 | return; |
| 1717 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1718 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 1719 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 1720 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1721 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1722 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1723 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1724 | const RecordDecl *RD = RT->getDecl(); |
| 1725 | |
| 1726 | // Assume variable sized types are passed in memory. |
| 1727 | if (RD->hasFlexibleArrayMember()) |
| 1728 | return; |
| 1729 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1730 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Reset Lo class, this will be recomputed. |
| 1733 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1734 | |
| 1735 | // If this is a C++ record, classify the bases first. |
| 1736 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1737 | for (const auto &I : CXXRD->bases()) { |
| 1738 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1739 | "Unexpected base class!"); |
| 1740 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1741 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1742 | |
| 1743 | // Classify this field. |
| 1744 | // |
| 1745 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 1746 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 1747 | // initialized to class NO_CLASS. |
| 1748 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1749 | uint64_t Offset = |
| 1750 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1751 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 1752 | Lo = merge(Lo, FieldLo); |
| 1753 | Hi = merge(Hi, FieldHi); |
| 1754 | if (Lo == Memory || Hi == Memory) |
| 1755 | break; |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1760 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 1761 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1762 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1763 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 1764 | bool BitField = i->isBitField(); |
| 1765 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1766 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 1767 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1768 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 1769 | // The only case a 256-bit wide vector could be used is when the struct |
| 1770 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 1771 | // to work for sizes wider than 128, early check and fallback to memory. |
| 1772 | // |
| 1773 | if (Size > 128 && getContext().getTypeSize(i->getType()) != 256) { |
| 1774 | Lo = Memory; |
| 1775 | return; |
| 1776 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1777 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 1778 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1779 | Lo = Memory; |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | // Classify this field. |
| 1784 | // |
| 1785 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 1786 | // exceeds a single eightbyte, each is classified |
| 1787 | // separately. Each eightbyte gets initialized to class |
| 1788 | // NO_CLASS. |
| 1789 | Class FieldLo, FieldHi; |
| 1790 | |
| 1791 | // Bit-fields require special handling, they do not force the |
| 1792 | // structure to be passed in memory even if unaligned, and |
| 1793 | // therefore they can straddle an eightbyte. |
| 1794 | if (BitField) { |
| 1795 | // Ignore padding bit-fields. |
| 1796 | if (i->isUnnamedBitfield()) |
| 1797 | continue; |
| 1798 | |
| 1799 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1800 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1801 | |
| 1802 | uint64_t EB_Lo = Offset / 64; |
| 1803 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 1804 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1805 | if (EB_Lo) { |
| 1806 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 1807 | FieldLo = NoClass; |
| 1808 | FieldHi = Integer; |
| 1809 | } else { |
| 1810 | FieldLo = Integer; |
| 1811 | FieldHi = EB_Hi ? Integer : NoClass; |
| 1812 | } |
| 1813 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 1814 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1815 | Lo = merge(Lo, FieldLo); |
| 1816 | Hi = merge(Hi, FieldHi); |
| 1817 | if (Lo == Memory || Hi == Memory) |
| 1818 | break; |
| 1819 | } |
| 1820 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1821 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 1825 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1826 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1827 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1828 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1829 | // Treat an enum type as its underlying type. |
| 1830 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1831 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1832 | |
| 1833 | return (Ty->isPromotableIntegerType() ? |
| 1834 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 1835 | } |
| 1836 | |
| 1837 | return ABIArgInfo::getIndirect(0); |
| 1838 | } |
| 1839 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1840 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 1841 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 1842 | uint64_t Size = getContext().getTypeSize(VecTy); |
| 1843 | unsigned LargestVector = HasAVX ? 256 : 128; |
| 1844 | if (Size <= 64 || Size > LargestVector) |
| 1845 | return true; |
| 1846 | } |
| 1847 | |
| 1848 | return false; |
| 1849 | } |
| 1850 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1851 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 1852 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1853 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 1854 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1855 | // |
| 1856 | // This assumption is optimistic, as there could be free registers available |
| 1857 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 1858 | // the argument in the free register. This does not seem to happen currently, |
| 1859 | // but this code would be much safer if we could mark the argument with |
| 1860 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 1861 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1862 | // Treat an enum type as its underlying type. |
| 1863 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1864 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 1865 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 1866 | return (Ty->isPromotableIntegerType() ? |
| 1867 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1868 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1869 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 1870 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1871 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 1872 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1873 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 1874 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 1875 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 1876 | |
| 1877 | // Attempt to avoid passing indirect results using byval when possible. This |
| 1878 | // is important for good codegen. |
| 1879 | // |
| 1880 | // We do this by coercing the value into a scalar type which the backend can |
| 1881 | // handle naturally (i.e., without using byval). |
| 1882 | // |
| 1883 | // For simplicity, we currently only do this when we have exhausted all of the |
| 1884 | // free integer registers. Doing this when there are free integer registers |
| 1885 | // would require more care, as we would have to ensure that the coerced value |
| 1886 | // did not claim the unused register. That would require either reording the |
| 1887 | // arguments to the function (so that any subsequent inreg values came first), |
| 1888 | // or only doing this optimization when there were no following arguments that |
| 1889 | // might be inreg. |
| 1890 | // |
| 1891 | // We currently expect it to be rare (particularly in well written code) for |
| 1892 | // arguments to be passed on the stack when there are still free integer |
| 1893 | // registers available (this would typically imply large structs being passed |
| 1894 | // by value), so this seems like a fair tradeoff for now. |
| 1895 | // |
| 1896 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 1897 | // attributes. See PR12193. |
| 1898 | if (freeIntRegs == 0) { |
| 1899 | uint64_t Size = getContext().getTypeSize(Ty); |
| 1900 | |
| 1901 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 1902 | // type, which will end up on the stack (with alignment 8). |
| 1903 | if (Align == 8 && Size <= 64) |
| 1904 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1905 | Size)); |
| 1906 | } |
| 1907 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 1908 | return ABIArgInfo::getIndirect(Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1911 | /// GetByteVectorType - The ABI specifies that a value should be passed in an |
| 1912 | /// 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] | 1913 | /// vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1914 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1915 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1916 | |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1917 | // Wrapper structs that just contain vectors are passed just like vectors, |
| 1918 | // strip them off if present. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1919 | llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType); |
Chris Lattner | 9fa15c3 | 2010-07-29 05:02:29 +0000 | [diff] [blame] | 1920 | while (STy && STy->getNumElements() == 1) { |
| 1921 | IRType = STy->getElementType(0); |
| 1922 | STy = dyn_cast<llvm::StructType>(IRType); |
| 1923 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1924 | |
Bruno Cardoso Lopes | 129b4cc | 2011-07-08 22:57:35 +0000 | [diff] [blame] | 1925 | // 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] | 1926 | if (llvm::VectorType *VT = dyn_cast<llvm::VectorType>(IRType)){ |
| 1927 | llvm::Type *EltTy = VT->getElementType(); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 1928 | unsigned BitWidth = VT->getBitWidth(); |
Tanya Lattner | 71f1b2d | 2011-11-28 23:18:11 +0000 | [diff] [blame] | 1929 | if ((BitWidth >= 128 && BitWidth <= 256) && |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1930 | (EltTy->isFloatTy() || EltTy->isDoubleTy() || |
| 1931 | EltTy->isIntegerTy(8) || EltTy->isIntegerTy(16) || |
| 1932 | EltTy->isIntegerTy(32) || EltTy->isIntegerTy(64) || |
| 1933 | EltTy->isIntegerTy(128))) |
| 1934 | return VT; |
| 1935 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1936 | |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 1937 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), 2); |
| 1938 | } |
| 1939 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1940 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 1941 | /// is known to either be off the end of the specified type or being in |
| 1942 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 1943 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 1944 | /// classification that put one of the two halves in the INTEGER class. |
| 1945 | /// |
| 1946 | /// It is conservatively correct to return false. |
| 1947 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 1948 | unsigned EndBit, ASTContext &Context) { |
| 1949 | // If the bytes being queried are off the end of the type, there is no user |
| 1950 | // data hiding here. This handles analysis of builtins, vectors and other |
| 1951 | // types that don't contain interesting padding. |
| 1952 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 1953 | if (TySize <= StartBit) |
| 1954 | return true; |
| 1955 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1956 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 1957 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 1958 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 1959 | |
| 1960 | // Check each element to see if the element overlaps with the queried range. |
| 1961 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1962 | // If the element is after the span we care about, then we're done.. |
| 1963 | unsigned EltOffset = i*EltSize; |
| 1964 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1965 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 1966 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 1967 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 1968 | EndBit-EltOffset, Context)) |
| 1969 | return false; |
| 1970 | } |
| 1971 | // If it overlaps no elements, then it is safe to process as padding. |
| 1972 | return true; |
| 1973 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1974 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1975 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 1976 | const RecordDecl *RD = RT->getDecl(); |
| 1977 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1979 | // If this is a C++ record, check the bases first. |
| 1980 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1981 | for (const auto &I : CXXRD->bases()) { |
| 1982 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1983 | "Unexpected base class!"); |
| 1984 | const CXXRecordDecl *Base = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1985 | cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1987 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 1988 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1989 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1990 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1991 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1992 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1993 | EndBit-BaseOffset, Context)) |
| 1994 | return false; |
| 1995 | } |
| 1996 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1997 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 1998 | // Verify that no field has data that overlaps the region of interest. Yes |
| 1999 | // this could be sped up a lot by being smarter about queried fields, |
| 2000 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 2001 | // much. |
| 2002 | unsigned idx = 0; |
| 2003 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 2004 | i != e; ++i, ++idx) { |
| 2005 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2007 | // If we found a field after the region we care about, then we're done. |
| 2008 | if (FieldOffset >= EndBit) break; |
| 2009 | |
| 2010 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 2011 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 2012 | Context)) |
| 2013 | return false; |
| 2014 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2015 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2016 | // If nothing in this record overlapped the area of interest, then we're |
| 2017 | // clean. |
| 2018 | return true; |
| 2019 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2021 | return false; |
| 2022 | } |
| 2023 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2024 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 2025 | /// float member at the specified offset. For example, {int,{float}} has a |
| 2026 | /// float at offset 4. It is conservatively correct for this routine to return |
| 2027 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2028 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2029 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2030 | // Base case if we find a float. |
| 2031 | if (IROffset == 0 && IRType->isFloatTy()) |
| 2032 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2033 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2034 | // 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] | 2035 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2036 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 2037 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 2038 | IROffset -= SL->getElementOffset(Elt); |
| 2039 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 2040 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2041 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2042 | // 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] | 2043 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 2044 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2045 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 2046 | IROffset -= IROffset/EltSize*EltSize; |
| 2047 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 2048 | } |
| 2049 | |
| 2050 | return false; |
| 2051 | } |
| 2052 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2053 | |
| 2054 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 2055 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2056 | llvm::Type *X86_64ABIInfo:: |
| 2057 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2058 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 2059 | // 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] | 2060 | // pass as float if the last 4 bytes is just padding. This happens for |
| 2061 | // structs that contain 3 floats. |
| 2062 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 2063 | SourceOffset*8+64, getContext())) |
| 2064 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2065 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 2066 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 2067 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 2068 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2069 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 2070 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 2071 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2072 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 2073 | return llvm::Type::getDoubleTy(getVMContext()); |
| 2074 | } |
| 2075 | |
| 2076 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2077 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 2078 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 2079 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 2080 | /// 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] | 2081 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 2082 | /// etc). |
| 2083 | /// |
| 2084 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 2085 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 2086 | /// the 8-byte value references. PrefType may be null. |
| 2087 | /// |
| 2088 | /// SourceTy is the source level type for the entire argument. SourceOffset is |
| 2089 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 2090 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2091 | llvm::Type *X86_64ABIInfo:: |
| 2092 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2093 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2094 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 2095 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 2096 | if (IROffset == 0) { |
| 2097 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2098 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 2099 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2100 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2101 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2102 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 2103 | // goodness in the source type is just tail padding. This is allowed to |
| 2104 | // kick in for struct {double,int} on the int, but not on |
| 2105 | // struct{double,int,int} because we wouldn't return the second int. We |
| 2106 | // have to do this analysis on the source type because we can't depend on |
| 2107 | // unions being lowered a specific way etc. |
| 2108 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2109 | IRType->isIntegerTy(32) || |
| 2110 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 2111 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 2112 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2113 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 2114 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 2115 | SourceOffset*8+64, getContext())) |
| 2116 | return IRType; |
| 2117 | } |
| 2118 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2119 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2120 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2121 | // 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] | 2122 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2123 | if (IROffset < SL->getSizeInBytes()) { |
| 2124 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 2125 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2126 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2127 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 2128 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2129 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2130 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2131 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2132 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2133 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2134 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2135 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 2136 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 2137 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 2138 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2139 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2140 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 2141 | // 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] | 2142 | unsigned TySizeInBytes = |
| 2143 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2145 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2147 | // It is always safe to classify this as an integer type up to i64 that |
| 2148 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 2149 | return llvm::IntegerType::get(getVMContext(), |
| 2150 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2153 | |
| 2154 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 2155 | /// be used as elements of a two register pair to pass or return, return a |
| 2156 | /// first class aggregate to represent them. For example, if the low part of |
| 2157 | /// a by-value argument should be passed as i32* and the high part as float, |
| 2158 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2159 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2160 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2161 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2162 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 2163 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 2164 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 2165 | // the second element at offset 8. Check for this: |
| 2166 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 2167 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2168 | unsigned HiStart = llvm::DataLayout::RoundUpAlignment(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2169 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2170 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2171 | // To handle this, we have to increase the size of the low part so that the |
| 2172 | // second element will start at an 8 byte offset. We can't increase the size |
| 2173 | // of the second element because it might make us access off the end of the |
| 2174 | // struct. |
| 2175 | if (HiStart != 8) { |
| 2176 | // There are only two sorts of types the ABI generation code can produce for |
| 2177 | // the low part of a pair that aren't 8 bytes in size: float or i8/i16/i32. |
| 2178 | // Promote these to a larger type. |
| 2179 | if (Lo->isFloatTy()) |
| 2180 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 2181 | else { |
| 2182 | assert(Lo->isIntegerTy() && "Invalid/unknown lo type"); |
| 2183 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 2184 | } |
| 2185 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2186 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2187 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi, NULL); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2188 | |
| 2189 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2190 | // Verify that the second element is at an 8-byte offset. |
| 2191 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 2192 | "Invalid x86-64 argument pair!"); |
| 2193 | return Result; |
| 2194 | } |
| 2195 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2196 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2197 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2198 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 2199 | // classification algorithm. |
| 2200 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2201 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2202 | |
| 2203 | // Check some invariants. |
| 2204 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2205 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2206 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2207 | llvm::Type *ResType = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2208 | switch (Lo) { |
| 2209 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2210 | if (Hi == NoClass) |
| 2211 | return ABIArgInfo::getIgnore(); |
| 2212 | // If the low part is just padding, it takes no register, leave ResType |
| 2213 | // null. |
| 2214 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2215 | "Unknown missing lo part"); |
| 2216 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2217 | |
| 2218 | case SSEUp: |
| 2219 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2220 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2221 | |
| 2222 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 2223 | // hidden argument. |
| 2224 | case Memory: |
| 2225 | return getIndirectReturnResult(RetTy); |
| 2226 | |
| 2227 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 2228 | // available register of the sequence %rax, %rdx is used. |
| 2229 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2230 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2231 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2232 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2233 | // so that the parameter gets the right LLVM IR attributes. |
| 2234 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2235 | // Treat an enum type as its underlying type. |
| 2236 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 2237 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2239 | if (RetTy->isIntegralOrEnumerationType() && |
| 2240 | RetTy->isPromotableIntegerType()) |
| 2241 | return ABIArgInfo::getExtend(); |
| 2242 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2243 | break; |
| 2244 | |
| 2245 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 2246 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 2247 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2248 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2249 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2250 | |
| 2251 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 2252 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 2253 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2254 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
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 8. If the class is COMPLEX_X87, the real |
| 2258 | // part of the value is returned in %st0 and the imaginary part in |
| 2259 | // %st1. |
| 2260 | case ComplexX87: |
| 2261 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2262 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2263 | llvm::Type::getX86_FP80Ty(getVMContext()), |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2264 | NULL); |
| 2265 | break; |
| 2266 | } |
| 2267 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2268 | llvm::Type *HighPart = 0; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2269 | switch (Hi) { |
| 2270 | // Memory was handled previously and X87 should |
| 2271 | // never occur as a hi class. |
| 2272 | case Memory: |
| 2273 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2274 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2275 | |
| 2276 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 2277 | case NoClass: |
| 2278 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2279 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2280 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2281 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2282 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2283 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2284 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2285 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2286 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2287 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2288 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2289 | break; |
| 2290 | |
| 2291 | // 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] | 2292 | // is passed in the next available eightbyte chunk if the last used |
| 2293 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2294 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2295 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2296 | case SSEUp: |
| 2297 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2298 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2299 | break; |
| 2300 | |
| 2301 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 2302 | // returned together with the previous X87 value in %st0. |
| 2303 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2304 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2305 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2306 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2307 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2308 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2309 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2310 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 2311 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 2312 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2313 | break; |
| 2314 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2315 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 2316 | // 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] | 2317 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2318 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 2319 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2320 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2321 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2322 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2325 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2326 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 2327 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2328 | const |
| 2329 | { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2330 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2331 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2332 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2333 | // Check some invariants. |
| 2334 | // FIXME: Enforce these by construction. |
| 2335 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2336 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 2337 | |
| 2338 | neededInt = 0; |
| 2339 | neededSSE = 0; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2340 | llvm::Type *ResType = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2341 | switch (Lo) { |
| 2342 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2343 | if (Hi == NoClass) |
| 2344 | return ABIArgInfo::getIgnore(); |
| 2345 | // If the low part is just padding, it takes no register, leave ResType |
| 2346 | // null. |
| 2347 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 2348 | "Unknown missing lo part"); |
| 2349 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2350 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2351 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 2352 | // on the stack. |
| 2353 | case Memory: |
| 2354 | |
| 2355 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 2356 | // COMPLEX_X87, it is passed in memory. |
| 2357 | case X87: |
| 2358 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2359 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 2360 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2361 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2362 | |
| 2363 | case SSEUp: |
| 2364 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2365 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2366 | |
| 2367 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 2368 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 2369 | // and %r9 is used. |
| 2370 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2371 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2372 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2373 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2374 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2375 | |
| 2376 | // If we have a sign or zero extended integer, make sure to return Extend |
| 2377 | // so that the parameter gets the right LLVM IR attributes. |
| 2378 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 2379 | // Treat an enum type as its underlying type. |
| 2380 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2381 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2382 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2383 | if (Ty->isIntegralOrEnumerationType() && |
| 2384 | Ty->isPromotableIntegerType()) |
| 2385 | return ABIArgInfo::getExtend(); |
| 2386 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2387 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | |
| 2390 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 2391 | // available SSE register is used, the registers are taken in the |
| 2392 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2393 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2394 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 2395 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2396 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2397 | break; |
| 2398 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 2399 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2400 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2401 | llvm::Type *HighPart = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2402 | switch (Hi) { |
| 2403 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 2404 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2405 | // which is passed in memory. |
| 2406 | case Memory: |
| 2407 | case X87: |
| 2408 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2409 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2410 | |
| 2411 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2412 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2413 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2414 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 2415 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2416 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2417 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2418 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2419 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2420 | break; |
| 2421 | |
| 2422 | // X87Up generally doesn't occur here (long double is passed in |
| 2423 | // memory), except in situations involving unions. |
| 2424 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2425 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2426 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2427 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2428 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 2429 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 2430 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2431 | ++neededSSE; |
| 2432 | break; |
| 2433 | |
| 2434 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 2435 | // 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] | 2436 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2437 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 2438 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2439 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2440 | break; |
| 2441 | } |
| 2442 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 2443 | // If a high part was specified, merge it together with the low part. It is |
| 2444 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 2445 | // first class struct aggregate with the high and low part: {low, high} |
| 2446 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2447 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2448 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 2449 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 2452 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2453 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2454 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2455 | |
| 2456 | // Keep track of the number of assigned registers. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2457 | unsigned freeIntRegs = 6, freeSSERegs = 8; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2458 | |
| 2459 | // If the return value is indirect, then the hidden argument is consuming one |
| 2460 | // integer register. |
| 2461 | if (FI.getReturnInfo().isIndirect()) |
| 2462 | --freeIntRegs; |
| 2463 | |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2464 | bool isVariadic = FI.isVariadic(); |
| 2465 | unsigned numRequiredArgs = 0; |
| 2466 | if (isVariadic) |
| 2467 | numRequiredArgs = FI.getRequiredArgs().getNumRequiredArgs(); |
| 2468 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2469 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 2470 | // get assigned (in left-to-right order) for passing as follows... |
| 2471 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 2472 | it != ie; ++it) { |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2473 | bool isNamedArg = true; |
| 2474 | if (isVariadic) |
Aaron Ballman | 6a30264 | 2013-06-12 15:03:45 +0000 | [diff] [blame] | 2475 | isNamedArg = (it - FI.arg_begin()) < |
| 2476 | static_cast<signed>(numRequiredArgs); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2477 | |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2478 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2479 | it->info = classifyArgumentType(it->type, freeIntRegs, neededInt, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2480 | neededSSE, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2481 | |
| 2482 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 2483 | // eightbyte of an argument, the whole argument is passed on the |
| 2484 | // stack. If registers have already been assigned for some |
| 2485 | // eightbytes of such an argument, the assignments get reverted. |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2486 | if (freeIntRegs >= neededInt && freeSSERegs >= neededSSE) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2487 | freeIntRegs -= neededInt; |
| 2488 | freeSSERegs -= neededSSE; |
| 2489 | } else { |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2490 | it->info = getIndirectResult(it->type, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | static llvm::Value *EmitVAArgFromMemory(llvm::Value *VAListAddr, |
| 2496 | QualType Ty, |
| 2497 | CodeGenFunction &CGF) { |
| 2498 | llvm::Value *overflow_arg_area_p = |
| 2499 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
| 2500 | llvm::Value *overflow_arg_area = |
| 2501 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 2502 | |
| 2503 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 2504 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2505 | // It isn't stated explicitly in the standard, but in practice we use |
| 2506 | // alignment greater than 16 where necessary. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2507 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 2508 | if (Align > 8) { |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2509 | // overflow_arg_area = (overflow_arg_area + align - 1) & -align; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2510 | llvm::Value *Offset = |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2511 | llvm::ConstantInt::get(CGF.Int64Ty, Align - 1); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2512 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset); |
| 2513 | llvm::Value *AsInt = CGF.Builder.CreatePtrToInt(overflow_arg_area, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2514 | CGF.Int64Ty); |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 2515 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, -(uint64_t)Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2516 | overflow_arg_area = |
| 2517 | CGF.Builder.CreateIntToPtr(CGF.Builder.CreateAnd(AsInt, Mask), |
| 2518 | overflow_arg_area->getType(), |
| 2519 | "overflow_arg_area.align"); |
| 2520 | } |
| 2521 | |
| 2522 | // 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] | 2523 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2524 | llvm::Value *Res = |
| 2525 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2526 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2527 | |
| 2528 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 2529 | // l->overflow_arg_area + sizeof(type). |
| 2530 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 2531 | // an 8 byte boundary. |
| 2532 | |
| 2533 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2534 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2535 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2536 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 2537 | "overflow_arg_area.next"); |
| 2538 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 2539 | |
| 2540 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
| 2541 | return Res; |
| 2542 | } |
| 2543 | |
| 2544 | llvm::Value *X86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2545 | CodeGenFunction &CGF) const { |
| 2546 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 2547 | // struct { |
| 2548 | // i32 gp_offset; |
| 2549 | // i32 fp_offset; |
| 2550 | // i8* overflow_arg_area; |
| 2551 | // i8* reg_save_area; |
| 2552 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 2553 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2554 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 2555 | Ty = CGF.getContext().getCanonicalType(Ty); |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2556 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
| 2557 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2558 | |
| 2559 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 2560 | // in the registers. If not go to step 7. |
| 2561 | if (!neededInt && !neededSSE) |
| 2562 | return EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2563 | |
| 2564 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 2565 | // general purpose registers needed to pass type and num_fp to hold |
| 2566 | // the number of floating point registers needed. |
| 2567 | |
| 2568 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 2569 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 2570 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 2571 | // |
| 2572 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 2573 | // register save space). |
| 2574 | |
| 2575 | llvm::Value *InRegs = 0; |
| 2576 | llvm::Value *gp_offset_p = 0, *gp_offset = 0; |
| 2577 | llvm::Value *fp_offset_p = 0, *fp_offset = 0; |
| 2578 | if (neededInt) { |
| 2579 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
| 2580 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2581 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 2582 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | if (neededSSE) { |
| 2586 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
| 2587 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 2588 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2589 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 2590 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2591 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 2592 | } |
| 2593 | |
| 2594 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 2595 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 2596 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 2597 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 2598 | |
| 2599 | // Emit code to load the value if it was passed in registers. |
| 2600 | |
| 2601 | CGF.EmitBlock(InRegBlock); |
| 2602 | |
| 2603 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 2604 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 2605 | // copying to a temporary location in case the parameter is passed |
| 2606 | // in different register classes or requires an alignment greater |
| 2607 | // than 8 for general purpose registers and 16 for XMM registers. |
| 2608 | // |
| 2609 | // FIXME: This really results in shameful code when we end up needing to |
| 2610 | // collect arguments from different places; often what should result in a |
| 2611 | // simple assembling of a structure from scattered addresses has many more |
| 2612 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2613 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2614 | llvm::Value *RegAddr = |
| 2615 | CGF.Builder.CreateLoad(CGF.Builder.CreateStructGEP(VAListAddr, 3), |
| 2616 | "reg_save_area"); |
| 2617 | if (neededInt && neededSSE) { |
| 2618 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 2619 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2620 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2621 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2622 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2623 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2624 | llvm::Type *TyLo = ST->getElementType(0); |
| 2625 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 2626 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2627 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2628 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 2629 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2630 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2631 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Duncan Sands | 998f9d9 | 2010-02-15 16:14:01 +0000 | [diff] [blame] | 2632 | llvm::Value *RegLoAddr = TyLo->isFloatingPointTy() ? FPAddr : GPAddr; |
| 2633 | llvm::Value *RegHiAddr = TyLo->isFloatingPointTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2634 | llvm::Value *V = |
| 2635 | CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegLoAddr, PTyLo)); |
| 2636 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2637 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegHiAddr, PTyHi)); |
| 2638 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2639 | |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2640 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2641 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2642 | } else if (neededInt) { |
| 2643 | RegAddr = CGF.Builder.CreateGEP(RegAddr, gp_offset); |
| 2644 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2645 | llvm::PointerType::getUnqual(LTy)); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2646 | |
| 2647 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 2648 | std::pair<CharUnits, CharUnits> SizeAlign = |
| 2649 | CGF.getContext().getTypeInfoInChars(Ty); |
| 2650 | uint64_t TySize = SizeAlign.first.getQuantity(); |
| 2651 | unsigned TyAlign = SizeAlign.second.getQuantity(); |
| 2652 | if (TyAlign > 8) { |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2653 | llvm::Value *Tmp = CGF.CreateMemTemp(Ty); |
| 2654 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, 8, false); |
| 2655 | RegAddr = Tmp; |
| 2656 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2657 | } else if (neededSSE == 1) { |
| 2658 | RegAddr = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
| 2659 | RegAddr = CGF.Builder.CreateBitCast(RegAddr, |
| 2660 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2661 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2662 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 2663 | // SSE registers are spaced 16 bytes apart in the register save |
| 2664 | // area, we need to collect the two eightbytes together. |
| 2665 | llvm::Value *RegAddrLo = CGF.Builder.CreateGEP(RegAddr, fp_offset); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2666 | llvm::Value *RegAddrHi = CGF.Builder.CreateConstGEP1_32(RegAddrLo, 16); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2667 | llvm::Type *DoubleTy = CGF.DoubleTy; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2668 | llvm::Type *DblPtrTy = |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2669 | llvm::PointerType::getUnqual(DoubleTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 2670 | llvm::StructType *ST = llvm::StructType::get(DoubleTy, DoubleTy, NULL); |
| 2671 | llvm::Value *V, *Tmp = CGF.CreateMemTemp(Ty); |
| 2672 | Tmp = CGF.Builder.CreateBitCast(Tmp, ST->getPointerTo()); |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2673 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrLo, |
| 2674 | DblPtrTy)); |
| 2675 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
| 2676 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateBitCast(RegAddrHi, |
| 2677 | DblPtrTy)); |
| 2678 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
| 2679 | RegAddr = CGF.Builder.CreateBitCast(Tmp, |
| 2680 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 2684 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 2685 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 2686 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2687 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2688 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 2689 | gp_offset_p); |
| 2690 | } |
| 2691 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 2692 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2693 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 2694 | fp_offset_p); |
| 2695 | } |
| 2696 | CGF.EmitBranch(ContBlock); |
| 2697 | |
| 2698 | // Emit code to load the value if it was passed in memory. |
| 2699 | |
| 2700 | CGF.EmitBlock(InMemBlock); |
| 2701 | llvm::Value *MemAddr = EmitVAArgFromMemory(VAListAddr, Ty, CGF); |
| 2702 | |
| 2703 | // Return the appropriate result. |
| 2704 | |
| 2705 | CGF.EmitBlock(ContBlock); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2706 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(RegAddr->getType(), 2, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2707 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2708 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 2709 | ResAddr->addIncoming(MemAddr, InMemBlock); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2710 | return ResAddr; |
| 2711 | } |
| 2712 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2713 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, bool IsReturnType) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2714 | |
| 2715 | if (Ty->isVoidType()) |
| 2716 | return ABIArgInfo::getIgnore(); |
| 2717 | |
| 2718 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2719 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2720 | |
| 2721 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2722 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2723 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 2724 | if (RT) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2725 | if (IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2726 | if (isRecordReturnIndirect(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2727 | return ABIArgInfo::getIndirect(0, false); |
| 2728 | } else { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2729 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2730 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 2731 | } |
| 2732 | |
| 2733 | if (RT->getDecl()->hasFlexibleArrayMember()) |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2734 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 2735 | |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2736 | // FIXME: mingw-w64-gcc emits 128-bit struct as i128 |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 2737 | if (Size == 128 && getTarget().getTriple().isWindowsGNUEnvironment()) |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2738 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2739 | Size)); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2740 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2741 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 2742 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 2743 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 2744 | // directly. |
| 2745 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 2746 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 2747 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | if (RT || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 2751 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 2752 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2753 | if (Size > 64 || !llvm::isPowerOf2_64(Size)) |
| 2754 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2755 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 2756 | // Otherwise, coerce it to a small integer. |
| 2757 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | if (Ty->isPromotableIntegerType()) |
| 2761 | return ABIArgInfo::getExtend(); |
| 2762 | |
| 2763 | return ABIArgInfo::getDirect(); |
| 2764 | } |
| 2765 | |
| 2766 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2767 | |
| 2768 | QualType RetTy = FI.getReturnType(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 2769 | FI.getReturnInfo() = classify(RetTy, true); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2770 | |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 2771 | // On win64, swap the order of the first two parameters for instance methods |
| 2772 | // which are sret behind the scenes to match MSVC. |
| 2773 | if (FI.getReturnInfo().isIndirect() && FI.isInstanceMethod() && |
| 2774 | getCXXABI().isSRetParameterAfterThis()) { |
| 2775 | assert(FI.arg_size() >= 1 && "instance method should have this"); |
| 2776 | FI.getReturnInfo().setSRetAfterThis(true); |
| 2777 | } |
| 2778 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2779 | for (auto &I : FI.arguments()) |
| 2780 | I.info = classify(I.type, false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2783 | llvm::Value *WinX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2784 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2785 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2787 | CGBuilderTy &Builder = CGF.Builder; |
| 2788 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 2789 | "ap"); |
| 2790 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 2791 | llvm::Type *PTy = |
| 2792 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 2793 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 2794 | |
| 2795 | uint64_t Offset = |
| 2796 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 8); |
| 2797 | llvm::Value *NextAddr = |
| 2798 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 2799 | "ap.next"); |
| 2800 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 2801 | |
| 2802 | return AddrTyped; |
| 2803 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2804 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2805 | namespace { |
| 2806 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2807 | class NaClX86_64ABIInfo : public ABIInfo { |
| 2808 | public: |
| 2809 | NaClX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2810 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, HasAVX) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2811 | void computeInfo(CGFunctionInfo &FI) const override; |
| 2812 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2813 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2814 | private: |
| 2815 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 2816 | X86_64ABIInfo NInfo; // Used for everything else. |
| 2817 | }; |
| 2818 | |
| 2819 | class NaClX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2820 | public: |
| 2821 | NaClX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool HasAVX) |
| 2822 | : TargetCodeGenInfo(new NaClX86_64ABIInfo(CGT, HasAVX)) {} |
| 2823 | }; |
| 2824 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 2827 | void NaClX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 2828 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 2829 | PInfo.computeInfo(FI); |
| 2830 | else |
| 2831 | NInfo.computeInfo(FI); |
| 2832 | } |
| 2833 | |
| 2834 | llvm::Value *NaClX86_64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2835 | CodeGenFunction &CGF) const { |
| 2836 | // Always use the native convention; calling pnacl-style varargs functions |
| 2837 | // is unuspported. |
| 2838 | return NInfo.EmitVAArg(VAListAddr, Ty, CGF); |
| 2839 | } |
| 2840 | |
| 2841 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2842 | // PowerPC-32 |
| 2843 | |
| 2844 | namespace { |
| 2845 | class PPC32TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2846 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2847 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2848 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2849 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2850 | // This is recovered from gcc output. |
| 2851 | return 1; // r1 is the dedicated stack pointer |
| 2852 | } |
| 2853 | |
| 2854 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2855 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2856 | }; |
| 2857 | |
| 2858 | } |
| 2859 | |
| 2860 | bool |
| 2861 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 2862 | llvm::Value *Address) const { |
| 2863 | // This is calculated from the LLVM and GCC tables and verified |
| 2864 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 2865 | |
| 2866 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2867 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2868 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2869 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 2870 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 2871 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 2872 | |
| 2873 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2874 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2875 | |
| 2876 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2877 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2878 | |
| 2879 | // 64-76 are various 4-byte special-purpose registers: |
| 2880 | // 64: mq |
| 2881 | // 65: lr |
| 2882 | // 66: ctr |
| 2883 | // 67: ap |
| 2884 | // 68-75 cr0-7 |
| 2885 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2886 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2887 | |
| 2888 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2889 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2890 | |
| 2891 | // 109: vrsave |
| 2892 | // 110: vscr |
| 2893 | // 111: spe_acc |
| 2894 | // 112: spefscr |
| 2895 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2896 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2897 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2898 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2901 | // PowerPC-64 |
| 2902 | |
| 2903 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2904 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
| 2905 | class PPC64_SVR4_ABIInfo : public DefaultABIInfo { |
| 2906 | |
| 2907 | public: |
| 2908 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 2909 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2910 | bool isPromotableTypeForABI(QualType Ty) const; |
| 2911 | |
| 2912 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 2913 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 2914 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2915 | // TODO: We can add more logic to computeInfo to improve performance. |
| 2916 | // Example: For aggregate arguments that fit in a register, we could |
| 2917 | // use getDirectInReg (as is done below for structs containing a single |
| 2918 | // floating-point value) to avoid pushing them to memory on function |
| 2919 | // entry. This would require changing the logic in PPCISelLowering |
| 2920 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2921 | void computeInfo(CGFunctionInfo &FI) const override { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2922 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2923 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2924 | // We rely on the default argument classification for the most part. |
| 2925 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2926 | // 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] | 2927 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2928 | if (T) { |
| 2929 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 2930 | if (T->isVectorType() || (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2931 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2932 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2933 | continue; |
| 2934 | } |
| 2935 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 2936 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 2937 | } |
| 2938 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2939 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2940 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 2941 | CodeGenFunction &CGF) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2942 | }; |
| 2943 | |
| 2944 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2945 | public: |
| 2946 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT) |
| 2947 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT)) {} |
| 2948 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2949 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2950 | // This is recovered from gcc output. |
| 2951 | return 1; // r1 is the dedicated stack pointer |
| 2952 | } |
| 2953 | |
| 2954 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2955 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 2956 | }; |
| 2957 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2958 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 2959 | public: |
| 2960 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 2961 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2962 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2963 | // This is recovered from gcc output. |
| 2964 | return 1; // r1 is the dedicated stack pointer |
| 2965 | } |
| 2966 | |
| 2967 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2968 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 2969 | }; |
| 2970 | |
| 2971 | } |
| 2972 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 2973 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 2974 | // extended to 64 bits. |
| 2975 | bool |
| 2976 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 2977 | // Treat an enum type as its underlying type. |
| 2978 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2979 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2980 | |
| 2981 | // Promotable integer types are required to be promoted by the ABI. |
| 2982 | if (Ty->isPromotableIntegerType()) |
| 2983 | return true; |
| 2984 | |
| 2985 | // In addition to the usual promotable integer types, we also need to |
| 2986 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 2987 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 2988 | switch (BT->getKind()) { |
| 2989 | case BuiltinType::Int: |
| 2990 | case BuiltinType::UInt: |
| 2991 | return true; |
| 2992 | default: |
| 2993 | break; |
| 2994 | } |
| 2995 | |
| 2996 | return false; |
| 2997 | } |
| 2998 | |
| 2999 | ABIArgInfo |
| 3000 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 3001 | if (Ty->isAnyComplexType()) |
| 3002 | return ABIArgInfo::getDirect(); |
| 3003 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3004 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3005 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3006 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3007 | |
| 3008 | return ABIArgInfo::getIndirect(0); |
| 3009 | } |
| 3010 | |
| 3011 | return (isPromotableTypeForABI(Ty) ? |
| 3012 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3013 | } |
| 3014 | |
| 3015 | ABIArgInfo |
| 3016 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3017 | if (RetTy->isVoidType()) |
| 3018 | return ABIArgInfo::getIgnore(); |
| 3019 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 3020 | if (RetTy->isAnyComplexType()) |
| 3021 | return ABIArgInfo::getDirect(); |
| 3022 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 3023 | if (isAggregateTypeForABI(RetTy)) |
| 3024 | return ABIArgInfo::getIndirect(0); |
| 3025 | |
| 3026 | return (isPromotableTypeForABI(RetTy) ? |
| 3027 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 3028 | } |
| 3029 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3030 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
| 3031 | llvm::Value *PPC64_SVR4_ABIInfo::EmitVAArg(llvm::Value *VAListAddr, |
| 3032 | QualType Ty, |
| 3033 | CodeGenFunction &CGF) const { |
| 3034 | llvm::Type *BP = CGF.Int8PtrTy; |
| 3035 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 3036 | |
| 3037 | CGBuilderTy &Builder = CGF.Builder; |
| 3038 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3039 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3040 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3041 | // Update the va_list pointer. The pointer should be bumped by the |
| 3042 | // size of the object. We can trust getTypeSize() except for a complex |
| 3043 | // type whose base type is smaller than a doubleword. For these, the |
| 3044 | // size of the object is 16 bytes; see below for further explanation. |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3045 | unsigned SizeInBytes = CGF.getContext().getTypeSize(Ty) / 8; |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3046 | QualType BaseTy; |
| 3047 | unsigned CplxBaseSize = 0; |
| 3048 | |
| 3049 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 3050 | BaseTy = CTy->getElementType(); |
| 3051 | CplxBaseSize = CGF.getContext().getTypeSize(BaseTy) / 8; |
| 3052 | if (CplxBaseSize < 8) |
| 3053 | SizeInBytes = 16; |
| 3054 | } |
| 3055 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3056 | unsigned Offset = llvm::RoundUpToAlignment(SizeInBytes, 8); |
| 3057 | llvm::Value *NextAddr = |
| 3058 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), |
| 3059 | "ap.next"); |
| 3060 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3061 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 3062 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 3063 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 3064 | // in separate doublewords. However, Clang expects us to produce a |
| 3065 | // pointer to a structure with the two parts packed tightly. So generate |
| 3066 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 3067 | // and store them to a temporary structure. |
| 3068 | if (CplxBaseSize && CplxBaseSize < 8) { |
| 3069 | llvm::Value *RealAddr = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3070 | llvm::Value *ImagAddr = RealAddr; |
| 3071 | RealAddr = Builder.CreateAdd(RealAddr, Builder.getInt64(8 - CplxBaseSize)); |
| 3072 | ImagAddr = Builder.CreateAdd(ImagAddr, Builder.getInt64(16 - CplxBaseSize)); |
| 3073 | llvm::Type *PBaseTy = llvm::PointerType::getUnqual(CGF.ConvertType(BaseTy)); |
| 3074 | RealAddr = Builder.CreateIntToPtr(RealAddr, PBaseTy); |
| 3075 | ImagAddr = Builder.CreateIntToPtr(ImagAddr, PBaseTy); |
| 3076 | llvm::Value *Real = Builder.CreateLoad(RealAddr, false, ".vareal"); |
| 3077 | llvm::Value *Imag = Builder.CreateLoad(ImagAddr, false, ".vaimag"); |
| 3078 | llvm::Value *Ptr = CGF.CreateTempAlloca(CGT.ConvertTypeForMem(Ty), |
| 3079 | "vacplx"); |
| 3080 | llvm::Value *RealPtr = Builder.CreateStructGEP(Ptr, 0, ".real"); |
| 3081 | llvm::Value *ImagPtr = Builder.CreateStructGEP(Ptr, 1, ".imag"); |
| 3082 | Builder.CreateStore(Real, RealPtr, false); |
| 3083 | Builder.CreateStore(Imag, ImagPtr, false); |
| 3084 | return Ptr; |
| 3085 | } |
| 3086 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3087 | // If the argument is smaller than 8 bytes, it is right-adjusted in |
| 3088 | // its doubleword slot. Adjust the pointer to pick it up from the |
| 3089 | // correct offset. |
| 3090 | if (SizeInBytes < 8) { |
| 3091 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3092 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt64(8 - SizeInBytes)); |
| 3093 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP); |
| 3094 | } |
| 3095 | |
| 3096 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3097 | return Builder.CreateBitCast(Addr, PTy); |
| 3098 | } |
| 3099 | |
| 3100 | static bool |
| 3101 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3102 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 3103 | // This is calculated from the LLVM and GCC tables and verified |
| 3104 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 3105 | |
| 3106 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 3107 | |
| 3108 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 3109 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 3110 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 3111 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 3112 | |
| 3113 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 3114 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 3115 | |
| 3116 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 3117 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 3118 | |
| 3119 | // 64-76 are various 4-byte special-purpose registers: |
| 3120 | // 64: mq |
| 3121 | // 65: lr |
| 3122 | // 66: ctr |
| 3123 | // 67: ap |
| 3124 | // 68-75 cr0-7 |
| 3125 | // 76: xer |
| 3126 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
| 3127 | |
| 3128 | // 77-108: v0-31, the 16-byte vector registers |
| 3129 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 3130 | |
| 3131 | // 109: vrsave |
| 3132 | // 110: vscr |
| 3133 | // 111: spe_acc |
| 3134 | // 112: spefscr |
| 3135 | // 113: sfp |
| 3136 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
| 3137 | |
| 3138 | return false; |
| 3139 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 3140 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 3141 | bool |
| 3142 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 3143 | CodeGen::CodeGenFunction &CGF, |
| 3144 | llvm::Value *Address) const { |
| 3145 | |
| 3146 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3147 | } |
| 3148 | |
| 3149 | bool |
| 3150 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 3151 | llvm::Value *Address) const { |
| 3152 | |
| 3153 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 3154 | } |
| 3155 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3156 | //===----------------------------------------------------------------------===// |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3157 | // ARM64 ABI Implementation |
| 3158 | //===----------------------------------------------------------------------===// |
| 3159 | |
| 3160 | namespace { |
| 3161 | |
| 3162 | class ARM64ABIInfo : public ABIInfo { |
| 3163 | public: |
| 3164 | enum ABIKind { |
| 3165 | AAPCS = 0, |
| 3166 | DarwinPCS |
| 3167 | }; |
| 3168 | |
| 3169 | private: |
| 3170 | ABIKind Kind; |
| 3171 | |
| 3172 | public: |
| 3173 | ARM64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) : ABIInfo(CGT), Kind(Kind) {} |
| 3174 | |
| 3175 | private: |
| 3176 | ABIKind getABIKind() const { return Kind; } |
| 3177 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 3178 | |
| 3179 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 3180 | ABIArgInfo classifyArgumentType(QualType RetTy, unsigned &AllocatedVFP, |
| 3181 | bool &IsHA, unsigned &AllocatedGPR, |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3182 | bool &IsSmallAggr, bool IsNamedArg) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3183 | bool isIllegalVectorType(QualType Ty) const; |
| 3184 | |
| 3185 | virtual void computeInfo(CGFunctionInfo &FI) const { |
| 3186 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
| 3187 | // number of SIMD and Floating-point registers allocated so far. |
| 3188 | // If the argument is an HFA or an HVA and there are sufficient unallocated |
| 3189 | // SIMD and Floating-point registers, then the argument is allocated to SIMD |
| 3190 | // and Floating-point Registers (with one register per member of the HFA or |
| 3191 | // HVA). Otherwise, the NSRN is set to 8. |
| 3192 | unsigned AllocatedVFP = 0; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3193 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3194 | // To correctly handle small aggregates, we need to keep track of the number |
| 3195 | // of GPRs allocated so far. If the small aggregate can't all fit into |
| 3196 | // registers, it will be on stack. We don't allow the aggregate to be |
| 3197 | // partially in registers. |
| 3198 | unsigned AllocatedGPR = 0; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3199 | |
| 3200 | // Find the number of named arguments. Variadic arguments get special |
| 3201 | // treatment with the Darwin ABI. |
| 3202 | unsigned NumRequiredArgs = (FI.isVariadic() ? |
| 3203 | FI.getRequiredArgs().getNumRequiredArgs() : |
| 3204 | FI.arg_size()); |
| 3205 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3206 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3207 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
| 3208 | it != ie; ++it) { |
| 3209 | unsigned PreAllocation = AllocatedVFP, PreGPR = AllocatedGPR; |
| 3210 | bool IsHA = false, IsSmallAggr = false; |
| 3211 | const unsigned NumVFPs = 8; |
| 3212 | const unsigned NumGPRs = 8; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3213 | bool IsNamedArg = ((it - FI.arg_begin()) < |
| 3214 | static_cast<signed>(NumRequiredArgs)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3215 | it->info = classifyArgumentType(it->type, AllocatedVFP, IsHA, |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3216 | AllocatedGPR, IsSmallAggr, IsNamedArg); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3217 | |
| 3218 | // Under AAPCS the 64-bit stack slot alignment means we can't pass HAs |
| 3219 | // as sequences of floats since they'll get "holes" inserted as |
| 3220 | // padding by the back end. |
Tim Northover | 07f1624 | 2014-04-18 10:47:44 +0000 | [diff] [blame] | 3221 | if (IsHA && AllocatedVFP > NumVFPs && !isDarwinPCS() && |
| 3222 | getContext().getTypeAlign(it->type) < 64) { |
| 3223 | uint32_t NumStackSlots = getContext().getTypeSize(it->type); |
| 3224 | NumStackSlots = llvm::RoundUpToAlignment(NumStackSlots, 64) / 64; |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3225 | |
Tim Northover | 07f1624 | 2014-04-18 10:47:44 +0000 | [diff] [blame] | 3226 | llvm::Type *CoerceTy = llvm::ArrayType::get( |
| 3227 | llvm::Type::getDoubleTy(getVMContext()), NumStackSlots); |
| 3228 | it->info = ABIArgInfo::getDirect(CoerceTy); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3231 | // If we do not have enough VFP registers for the HA, any VFP registers |
| 3232 | // that are unallocated are marked as unavailable. To achieve this, we add |
| 3233 | // padding of (NumVFPs - PreAllocation) floats. |
| 3234 | if (IsHA && AllocatedVFP > NumVFPs && PreAllocation < NumVFPs) { |
| 3235 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3236 | llvm::Type::getFloatTy(getVMContext()), NumVFPs - PreAllocation); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3237 | it->info.setPaddingType(PaddingTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3238 | } |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 3239 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3240 | // If we do not have enough GPRs for the small aggregate, any GPR regs |
| 3241 | // that are unallocated are marked as unavailable. |
| 3242 | if (IsSmallAggr && AllocatedGPR > NumGPRs && PreGPR < NumGPRs) { |
| 3243 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3244 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreGPR); |
| 3245 | it->info = |
| 3246 | ABIArgInfo::getDirect(it->info.getCoerceToType(), 0, PaddingTy); |
| 3247 | } |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | llvm::Value *EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3252 | CodeGenFunction &CGF) const; |
| 3253 | |
| 3254 | llvm::Value *EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3255 | CodeGenFunction &CGF) const; |
| 3256 | |
| 3257 | virtual llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3258 | CodeGenFunction &CGF) const { |
| 3259 | return isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 3260 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
| 3261 | } |
| 3262 | }; |
| 3263 | |
| 3264 | class ARM64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 3265 | public: |
| 3266 | ARM64TargetCodeGenInfo(CodeGenTypes &CGT, ARM64ABIInfo::ABIKind Kind) |
| 3267 | : TargetCodeGenInfo(new ARM64ABIInfo(CGT, Kind)) {} |
| 3268 | |
| 3269 | StringRef getARCRetainAutoreleasedReturnValueMarker() const { |
| 3270 | return "mov\tfp, fp\t\t; marker for objc_retainAutoreleaseReturnValue"; |
| 3271 | } |
| 3272 | |
| 3273 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const { return 31; } |
| 3274 | |
| 3275 | virtual bool doesReturnSlotInterfereWithArgs() const { return false; } |
| 3276 | }; |
| 3277 | } |
| 3278 | |
| 3279 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 3280 | ASTContext &Context, |
| 3281 | uint64_t *HAMembers = 0); |
| 3282 | |
| 3283 | ABIArgInfo ARM64ABIInfo::classifyArgumentType(QualType Ty, |
| 3284 | unsigned &AllocatedVFP, |
| 3285 | bool &IsHA, |
| 3286 | unsigned &AllocatedGPR, |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3287 | bool &IsSmallAggr, |
| 3288 | bool IsNamedArg) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3289 | // Handle illegal vector types here. |
| 3290 | if (isIllegalVectorType(Ty)) { |
| 3291 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3292 | if (Size <= 32) { |
| 3293 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
| 3294 | AllocatedGPR++; |
| 3295 | return ABIArgInfo::getDirect(ResType); |
| 3296 | } |
| 3297 | if (Size == 64) { |
| 3298 | llvm::Type *ResType = |
| 3299 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
| 3300 | AllocatedVFP++; |
| 3301 | return ABIArgInfo::getDirect(ResType); |
| 3302 | } |
| 3303 | if (Size == 128) { |
| 3304 | llvm::Type *ResType = |
| 3305 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
| 3306 | AllocatedVFP++; |
| 3307 | return ABIArgInfo::getDirect(ResType); |
| 3308 | } |
| 3309 | AllocatedGPR++; |
| 3310 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3311 | } |
| 3312 | if (Ty->isVectorType()) |
| 3313 | // Size of a legal vector should be either 64 or 128. |
| 3314 | AllocatedVFP++; |
| 3315 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 3316 | if (BT->getKind() == BuiltinType::Half || |
| 3317 | BT->getKind() == BuiltinType::Float || |
| 3318 | BT->getKind() == BuiltinType::Double || |
| 3319 | BT->getKind() == BuiltinType::LongDouble) |
| 3320 | AllocatedVFP++; |
| 3321 | } |
| 3322 | |
| 3323 | if (!isAggregateTypeForABI(Ty)) { |
| 3324 | // Treat an enum type as its underlying type. |
| 3325 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3326 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3327 | |
| 3328 | if (!Ty->isFloatingType() && !Ty->isVectorType()) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3329 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3330 | if (!isDarwinPCS() && Alignment > 64) |
| 3331 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3332 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3333 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 3334 | AllocatedGPR += RegsNeeded; |
| 3335 | } |
| 3336 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
| 3337 | ? ABIArgInfo::getExtend() |
| 3338 | : ABIArgInfo::getDirect()); |
| 3339 | } |
| 3340 | |
| 3341 | // Structures with either a non-trivial destructor or a non-trivial |
| 3342 | // copy constructor are always indirect. |
| 3343 | if (isRecordReturnIndirect(Ty, getCXXABI())) { |
| 3344 | AllocatedGPR++; |
| 3345 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3346 | } |
| 3347 | |
| 3348 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 3349 | // elsewhere for GNU compatibility. |
| 3350 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3351 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 3352 | return ABIArgInfo::getIgnore(); |
| 3353 | |
| 3354 | ++AllocatedGPR; |
| 3355 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 3356 | } |
| 3357 | |
| 3358 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
| 3359 | const Type *Base = 0; |
| 3360 | uint64_t Members = 0; |
| 3361 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3362 | IsHA = true; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3363 | if (!IsNamedArg && isDarwinPCS()) { |
| 3364 | // With the Darwin ABI, variadic arguments are always passed on the stack |
| 3365 | // and should not be expanded. Treat variadic HFAs as arrays of doubles. |
| 3366 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3367 | llvm::Type *BaseTy = llvm::Type::getDoubleTy(getVMContext()); |
| 3368 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 3369 | } |
| 3370 | AllocatedVFP += Members; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3371 | return ABIArgInfo::getExpand(); |
| 3372 | } |
| 3373 | |
| 3374 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
| 3375 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3376 | if (Size <= 128) { |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3377 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 3378 | if (!isDarwinPCS() && Alignment > 64) |
| 3379 | AllocatedGPR = llvm::RoundUpToAlignment(AllocatedGPR, Alignment / 64); |
| 3380 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3381 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3382 | AllocatedGPR += Size / 64; |
| 3383 | IsSmallAggr = true; |
| 3384 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 3385 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | c801b4a | 2014-04-15 14:55:11 +0000 | [diff] [blame] | 3386 | if (Alignment < 128 && Size == 128) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3387 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 3388 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 3389 | } |
| 3390 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3391 | } |
| 3392 | |
| 3393 | AllocatedGPR++; |
| 3394 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3395 | } |
| 3396 | |
| 3397 | ABIArgInfo ARM64ABIInfo::classifyReturnType(QualType RetTy) const { |
| 3398 | if (RetTy->isVoidType()) |
| 3399 | return ABIArgInfo::getIgnore(); |
| 3400 | |
| 3401 | // Large vector types should be returned via memory. |
| 3402 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
| 3403 | return ABIArgInfo::getIndirect(0); |
| 3404 | |
| 3405 | if (!isAggregateTypeForABI(RetTy)) { |
| 3406 | // Treat an enum type as its underlying type. |
| 3407 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3408 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 3409 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 3410 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
| 3411 | ? ABIArgInfo::getExtend() |
| 3412 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | // Structures with either a non-trivial destructor or a non-trivial |
| 3416 | // copy constructor are always indirect. |
| 3417 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
| 3418 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 3419 | |
| 3420 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 3421 | return ABIArgInfo::getIgnore(); |
| 3422 | |
| 3423 | const Type *Base = 0; |
| 3424 | if (isHomogeneousAggregate(RetTy, Base, getContext())) |
| 3425 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 3426 | return ABIArgInfo::getDirect(); |
| 3427 | |
| 3428 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
| 3429 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 3430 | if (Size <= 128) { |
| 3431 | Size = 64 * ((Size + 63) / 64); // round up to multiple of 8 bytes |
| 3432 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 3433 | } |
| 3434 | |
| 3435 | return ABIArgInfo::getIndirect(0); |
| 3436 | } |
| 3437 | |
| 3438 | /// isIllegalVectorType - check whether the vector type is legal for ARM64. |
| 3439 | bool ARM64ABIInfo::isIllegalVectorType(QualType Ty) const { |
| 3440 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 3441 | // Check whether VT is legal. |
| 3442 | unsigned NumElements = VT->getNumElements(); |
| 3443 | uint64_t Size = getContext().getTypeSize(VT); |
| 3444 | // NumElements should be power of 2 between 1 and 16. |
| 3445 | if ((NumElements & (NumElements - 1)) != 0 || NumElements > 16) |
| 3446 | return true; |
| 3447 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 3448 | } |
| 3449 | return false; |
| 3450 | } |
| 3451 | |
| 3452 | static llvm::Value *EmitAArch64VAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3453 | int AllocatedGPR, int AllocatedVFP, |
| 3454 | bool IsIndirect, CodeGenFunction &CGF) { |
| 3455 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 3456 | // Standard, section B.4: |
| 3457 | // |
| 3458 | // struct { |
| 3459 | // void *__stack; |
| 3460 | // void *__gr_top; |
| 3461 | // void *__vr_top; |
| 3462 | // int __gr_offs; |
| 3463 | // int __vr_offs; |
| 3464 | // }; |
| 3465 | |
| 3466 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 3467 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3468 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 3469 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3470 | auto &Ctx = CGF.getContext(); |
| 3471 | |
| 3472 | llvm::Value *reg_offs_p = 0, *reg_offs = 0; |
| 3473 | int reg_top_index; |
| 3474 | int RegSize; |
| 3475 | if (AllocatedGPR) { |
| 3476 | assert(!AllocatedVFP && "Arguments never split between int & VFP regs"); |
| 3477 | // 3 is the field number of __gr_offs |
| 3478 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
| 3479 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 3480 | reg_top_index = 1; // field number for __gr_top |
| 3481 | RegSize = 8 * AllocatedGPR; |
| 3482 | } else { |
| 3483 | assert(!AllocatedGPR && "Argument must go in VFP or int regs"); |
| 3484 | // 4 is the field number of __vr_offs. |
| 3485 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
| 3486 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 3487 | reg_top_index = 2; // field number for __vr_top |
| 3488 | RegSize = 16 * AllocatedVFP; |
| 3489 | } |
| 3490 | |
| 3491 | //======================================= |
| 3492 | // Find out where argument was passed |
| 3493 | //======================================= |
| 3494 | |
| 3495 | // If reg_offs >= 0 we're already using the stack for this type of |
| 3496 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 3497 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 3498 | // whatever they get). |
| 3499 | llvm::Value *UsingStack = 0; |
| 3500 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 3501 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 3502 | |
| 3503 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 3504 | |
| 3505 | // 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] | 3506 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3507 | CGF.EmitBlock(MaybeRegBlock); |
| 3508 | |
| 3509 | // Integer arguments may need to correct register alignment (for example a |
| 3510 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 3511 | // align __gr_offs to calculate the potential address. |
| 3512 | if (AllocatedGPR && !IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3513 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3514 | |
| 3515 | reg_offs = CGF.Builder.CreateAdd( |
| 3516 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 3517 | "align_regoffs"); |
| 3518 | reg_offs = CGF.Builder.CreateAnd( |
| 3519 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 3520 | "aligned_regoffs"); |
| 3521 | } |
| 3522 | |
| 3523 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
| 3524 | llvm::Value *NewOffset = 0; |
| 3525 | NewOffset = CGF.Builder.CreateAdd( |
| 3526 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 3527 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 3528 | |
| 3529 | // Now we're in a position to decide whether this argument really was in |
| 3530 | // registers or not. |
| 3531 | llvm::Value *InRegs = 0; |
| 3532 | InRegs = CGF.Builder.CreateICmpSLE( |
| 3533 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 3534 | |
| 3535 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 3536 | |
| 3537 | //======================================= |
| 3538 | // Argument was in registers |
| 3539 | //======================================= |
| 3540 | |
| 3541 | // Now we emit the code for if the argument was originally passed in |
| 3542 | // registers. First start the appropriate block: |
| 3543 | CGF.EmitBlock(InRegBlock); |
| 3544 | |
| 3545 | llvm::Value *reg_top_p = 0, *reg_top = 0; |
| 3546 | reg_top_p = |
| 3547 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
| 3548 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
| 3549 | llvm::Value *BaseAddr = CGF.Builder.CreateGEP(reg_top, reg_offs); |
| 3550 | llvm::Value *RegAddr = 0; |
| 3551 | llvm::Type *MemTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 3552 | |
| 3553 | if (IsIndirect) { |
| 3554 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 3555 | // stored registers or on the stack will actually be a struct **. |
| 3556 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 3557 | } |
| 3558 | |
| 3559 | const Type *Base = 0; |
| 3560 | uint64_t NumMembers; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 3561 | bool IsHFA = isHomogeneousAggregate(Ty, Base, Ctx, &NumMembers); |
| 3562 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3563 | // Homogeneous aggregates passed in registers will have their elements split |
| 3564 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 3565 | // qN+1, ...). We reload and store into a temporary local variable |
| 3566 | // contiguously. |
| 3567 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
| 3568 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 3569 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
| 3570 | llvm::Value *Tmp = CGF.CreateTempAlloca(HFATy); |
| 3571 | int Offset = 0; |
| 3572 | |
| 3573 | if (CGF.CGM.getDataLayout().isBigEndian() && Ctx.getTypeSize(Base) < 128) |
| 3574 | Offset = 16 - Ctx.getTypeSize(Base) / 8; |
| 3575 | for (unsigned i = 0; i < NumMembers; ++i) { |
| 3576 | llvm::Value *BaseOffset = |
| 3577 | llvm::ConstantInt::get(CGF.Int32Ty, 16 * i + Offset); |
| 3578 | llvm::Value *LoadAddr = CGF.Builder.CreateGEP(BaseAddr, BaseOffset); |
| 3579 | LoadAddr = CGF.Builder.CreateBitCast( |
| 3580 | LoadAddr, llvm::PointerType::getUnqual(BaseTy)); |
| 3581 | llvm::Value *StoreAddr = CGF.Builder.CreateStructGEP(Tmp, i); |
| 3582 | |
| 3583 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 3584 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 3585 | } |
| 3586 | |
| 3587 | RegAddr = CGF.Builder.CreateBitCast(Tmp, MemTy); |
| 3588 | } else { |
| 3589 | // Otherwise the object is contiguous in memory |
| 3590 | unsigned BeAlign = reg_top_index == 2 ? 16 : 8; |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 3591 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 3592 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3593 | Ctx.getTypeSize(Ty) < (BeAlign * 8)) { |
| 3594 | int Offset = BeAlign - Ctx.getTypeSize(Ty) / 8; |
| 3595 | BaseAddr = CGF.Builder.CreatePtrToInt(BaseAddr, CGF.Int64Ty); |
| 3596 | |
| 3597 | BaseAddr = CGF.Builder.CreateAdd( |
| 3598 | BaseAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 3599 | |
| 3600 | BaseAddr = CGF.Builder.CreateIntToPtr(BaseAddr, CGF.Int8PtrTy); |
| 3601 | } |
| 3602 | |
| 3603 | RegAddr = CGF.Builder.CreateBitCast(BaseAddr, MemTy); |
| 3604 | } |
| 3605 | |
| 3606 | CGF.EmitBranch(ContBlock); |
| 3607 | |
| 3608 | //======================================= |
| 3609 | // Argument was on the stack |
| 3610 | //======================================= |
| 3611 | CGF.EmitBlock(OnStackBlock); |
| 3612 | |
| 3613 | llvm::Value *stack_p = 0, *OnStackAddr = 0; |
| 3614 | stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
| 3615 | OnStackAddr = CGF.Builder.CreateLoad(stack_p, "stack"); |
| 3616 | |
| 3617 | // Again, stack arguments may need realigmnent. In this case both integer and |
| 3618 | // floating-point ones might be affected. |
| 3619 | if (!IsIndirect && Ctx.getTypeAlign(Ty) > 64) { |
| 3620 | int Align = Ctx.getTypeAlign(Ty) / 8; |
| 3621 | |
| 3622 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3623 | |
| 3624 | OnStackAddr = CGF.Builder.CreateAdd( |
| 3625 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
| 3626 | "align_stack"); |
| 3627 | OnStackAddr = CGF.Builder.CreateAnd( |
| 3628 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
| 3629 | "align_stack"); |
| 3630 | |
| 3631 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3632 | } |
| 3633 | |
| 3634 | uint64_t StackSize; |
| 3635 | if (IsIndirect) |
| 3636 | StackSize = 8; |
| 3637 | else |
| 3638 | StackSize = Ctx.getTypeSize(Ty) / 8; |
| 3639 | |
| 3640 | // All stack slots are 8 bytes |
| 3641 | StackSize = llvm::RoundUpToAlignment(StackSize, 8); |
| 3642 | |
| 3643 | llvm::Value *StackSizeC = llvm::ConstantInt::get(CGF.Int32Ty, StackSize); |
| 3644 | llvm::Value *NewStack = |
| 3645 | CGF.Builder.CreateGEP(OnStackAddr, StackSizeC, "new_stack"); |
| 3646 | |
| 3647 | // Write the new value of __stack for the next call to va_arg |
| 3648 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 3649 | |
| 3650 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
| 3651 | Ctx.getTypeSize(Ty) < 64) { |
| 3652 | int Offset = 8 - Ctx.getTypeSize(Ty) / 8; |
| 3653 | OnStackAddr = CGF.Builder.CreatePtrToInt(OnStackAddr, CGF.Int64Ty); |
| 3654 | |
| 3655 | OnStackAddr = CGF.Builder.CreateAdd( |
| 3656 | OnStackAddr, llvm::ConstantInt::get(CGF.Int64Ty, Offset), "align_be"); |
| 3657 | |
| 3658 | OnStackAddr = CGF.Builder.CreateIntToPtr(OnStackAddr, CGF.Int8PtrTy); |
| 3659 | } |
| 3660 | |
| 3661 | OnStackAddr = CGF.Builder.CreateBitCast(OnStackAddr, MemTy); |
| 3662 | |
| 3663 | CGF.EmitBranch(ContBlock); |
| 3664 | |
| 3665 | //======================================= |
| 3666 | // Tidy up |
| 3667 | //======================================= |
| 3668 | CGF.EmitBlock(ContBlock); |
| 3669 | |
| 3670 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(MemTy, 2, "vaarg.addr"); |
| 3671 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 3672 | ResAddr->addIncoming(OnStackAddr, OnStackBlock); |
| 3673 | |
| 3674 | if (IsIndirect) |
| 3675 | return CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"); |
| 3676 | |
| 3677 | return ResAddr; |
| 3678 | } |
| 3679 | |
| 3680 | llvm::Value *ARM64ABIInfo::EmitAAPCSVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3681 | CodeGenFunction &CGF) const { |
| 3682 | |
| 3683 | unsigned AllocatedGPR = 0, AllocatedVFP = 0; |
| 3684 | bool IsHA = false, IsSmallAggr = false; |
Bob Wilson | 373af73 | 2014-04-21 01:23:39 +0000 | [diff] [blame] | 3685 | ABIArgInfo AI = classifyArgumentType(Ty, AllocatedVFP, IsHA, AllocatedGPR, |
| 3686 | IsSmallAggr, false /*IsNamedArg*/); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3687 | |
| 3688 | return EmitAArch64VAArg(VAListAddr, Ty, AllocatedGPR, AllocatedVFP, |
| 3689 | AI.isIndirect(), CGF); |
| 3690 | } |
| 3691 | |
| 3692 | llvm::Value *ARM64ABIInfo::EmitDarwinVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3693 | CodeGenFunction &CGF) const { |
| 3694 | // We do not support va_arg for aggregates or illegal vector types. |
| 3695 | // Lower VAArg here for these cases and use the LLVM va_arg instruction for |
| 3696 | // other cases. |
| 3697 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
| 3698 | return 0; |
| 3699 | |
| 3700 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
| 3701 | uint64_t Align = CGF.getContext().getTypeAlign(Ty) / 8; |
| 3702 | |
| 3703 | const Type *Base = 0; |
| 3704 | bool isHA = isHomogeneousAggregate(Ty, Base, getContext()); |
| 3705 | |
| 3706 | bool isIndirect = false; |
| 3707 | // Arguments bigger than 16 bytes which aren't homogeneous aggregates should |
| 3708 | // be passed indirectly. |
| 3709 | if (Size > 16 && !isHA) { |
| 3710 | isIndirect = true; |
| 3711 | Size = 8; |
| 3712 | Align = 8; |
| 3713 | } |
| 3714 | |
| 3715 | llvm::Type *BP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 3716 | llvm::Type *BPP = llvm::PointerType::getUnqual(BP); |
| 3717 | |
| 3718 | CGBuilderTy &Builder = CGF.Builder; |
| 3719 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 3720 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 3721 | |
| 3722 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 3723 | // These are ignored for parameter passing purposes. |
| 3724 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3725 | return Builder.CreateBitCast(Addr, PTy); |
| 3726 | } |
| 3727 | |
| 3728 | const uint64_t MinABIAlign = 8; |
| 3729 | if (Align > MinABIAlign) { |
| 3730 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, Align - 1); |
| 3731 | Addr = Builder.CreateGEP(Addr, Offset); |
| 3732 | llvm::Value *AsInt = Builder.CreatePtrToInt(Addr, CGF.Int64Ty); |
| 3733 | llvm::Value *Mask = llvm::ConstantInt::get(CGF.Int64Ty, ~(Align - 1)); |
| 3734 | llvm::Value *Aligned = Builder.CreateAnd(AsInt, Mask); |
| 3735 | Addr = Builder.CreateIntToPtr(Aligned, BP, "ap.align"); |
| 3736 | } |
| 3737 | |
| 3738 | uint64_t Offset = llvm::RoundUpToAlignment(Size, MinABIAlign); |
| 3739 | llvm::Value *NextAddr = Builder.CreateGEP( |
| 3740 | Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), "ap.next"); |
| 3741 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 3742 | |
| 3743 | if (isIndirect) |
| 3744 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
| 3745 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 3746 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 3747 | |
| 3748 | return AddrTyped; |
| 3749 | } |
| 3750 | |
| 3751 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3752 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3753 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3754 | |
| 3755 | namespace { |
| 3756 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3757 | class ARMABIInfo : public ABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3758 | public: |
| 3759 | enum ABIKind { |
| 3760 | APCS = 0, |
| 3761 | AAPCS = 1, |
| 3762 | AAPCS_VFP |
| 3763 | }; |
| 3764 | |
| 3765 | private: |
| 3766 | ABIKind Kind; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3767 | mutable int VFPRegs[16]; |
| 3768 | const unsigned NumVFPs; |
| 3769 | const unsigned NumGPRs; |
| 3770 | mutable unsigned AllocatedGPRs; |
| 3771 | mutable unsigned AllocatedVFPs; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3772 | |
| 3773 | public: |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3774 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) : ABIInfo(CGT), Kind(_Kind), |
| 3775 | NumVFPs(16), NumGPRs(4) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3776 | setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3777 | resetAllocatedRegs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3778 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3779 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3780 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3781 | switch (getTarget().getTriple().getEnvironment()) { |
| 3782 | case llvm::Triple::Android: |
| 3783 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3784 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3785 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 3786 | case llvm::Triple::GNUEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 3787 | return true; |
| 3788 | default: |
| 3789 | return false; |
| 3790 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3791 | } |
| 3792 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3793 | bool isEABIHF() const { |
| 3794 | switch (getTarget().getTriple().getEnvironment()) { |
| 3795 | case llvm::Triple::EABIHF: |
| 3796 | case llvm::Triple::GNUEABIHF: |
| 3797 | return true; |
| 3798 | default: |
| 3799 | return false; |
| 3800 | } |
| 3801 | } |
| 3802 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3803 | ABIKind getABIKind() const { return Kind; } |
| 3804 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3805 | private: |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3806 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic) const; |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 3807 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3808 | bool &IsCPRC) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 3809 | bool isIllegalVectorType(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3810 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3811 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3812 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3813 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 3814 | CodeGenFunction &CGF) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3815 | |
| 3816 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 3817 | llvm::CallingConv::ID getABIDefaultCC() const; |
| 3818 | void setRuntimeCC(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3819 | |
| 3820 | void markAllocatedGPRs(unsigned Alignment, unsigned NumRequired) const; |
| 3821 | void markAllocatedVFPs(unsigned Alignment, unsigned NumRequired) const; |
| 3822 | void resetAllocatedRegs(void) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3823 | }; |
| 3824 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3825 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 3826 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3827 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 3828 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3829 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3830 | const ARMABIInfo &getABIInfo() const { |
| 3831 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 3832 | } |
| 3833 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3834 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 3835 | return 13; |
| 3836 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3837 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3838 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3839 | return "mov\tr7, r7\t\t@ marker for objc_retainAutoreleaseReturnValue"; |
| 3840 | } |
| 3841 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3842 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3843 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3844 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3845 | |
| 3846 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 3847 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 3848 | return false; |
| 3849 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3850 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3851 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 3852 | if (getABIInfo().isEABI()) return 88; |
| 3853 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 3854 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3855 | |
| 3856 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 3857 | CodeGen::CodeGenModule &CGM) const override { |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 3858 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3859 | if (!FD) |
| 3860 | return; |
| 3861 | |
| 3862 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 3863 | if (!Attr) |
| 3864 | return; |
| 3865 | |
| 3866 | const char *Kind; |
| 3867 | switch (Attr->getInterrupt()) { |
| 3868 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 3869 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 3870 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 3871 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 3872 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 3873 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 3874 | } |
| 3875 | |
| 3876 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 3877 | |
| 3878 | Fn->addFnAttr("interrupt", Kind); |
| 3879 | |
| 3880 | if (cast<ARMABIInfo>(getABIInfo()).getABIKind() == ARMABIInfo::APCS) |
| 3881 | return; |
| 3882 | |
| 3883 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 3884 | // however this is not necessarily true on taking any interrupt. Instruct |
| 3885 | // the backend to perform a realignment as part of the function prologue. |
| 3886 | llvm::AttrBuilder B; |
| 3887 | B.addStackAlignmentAttr(8); |
| 3888 | Fn->addAttributes(llvm::AttributeSet::FunctionIndex, |
| 3889 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 3890 | llvm::AttributeSet::FunctionIndex, |
| 3891 | B)); |
| 3892 | } |
| 3893 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 3894 | }; |
| 3895 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3898 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3899 | // To correctly handle Homogeneous Aggregate, we need to keep track of the |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 3900 | // VFP registers allocated so far. |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3901 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 3902 | // VFP registers of the appropriate type unallocated then the argument is |
| 3903 | // allocated to the lowest-numbered sequence of such registers. |
| 3904 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 3905 | // unallocated are marked as unavailable. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3906 | resetAllocatedRegs(); |
| 3907 | |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 3908 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 3909 | for (auto &I : FI.arguments()) { |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3910 | unsigned PreAllocationVFPs = AllocatedVFPs; |
| 3911 | unsigned PreAllocationGPRs = AllocatedGPRs; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3912 | bool IsCPRC = false; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3913 | // 6.1.2.3 There is one VFP co-processor register class using registers |
| 3914 | // s0-s15 (d0-d7) for passing arguments. |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 3915 | I.info = classifyArgumentType(I.type, FI.isVariadic(), IsCPRC); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 3916 | |
| 3917 | // If we have allocated some arguments onto the stack (due to running |
| 3918 | // out of VFP registers), we cannot split an argument between GPRs and |
| 3919 | // the stack. If this situation occurs, we add padding to prevent the |
| 3920 | // GPRs from being used. In this situiation, the current argument could |
| 3921 | // only be allocated by rule C.8, so rule C.6 would mark these GPRs as |
| 3922 | // unusable anyway. |
| 3923 | const bool StackUsed = PreAllocationGPRs > NumGPRs || PreAllocationVFPs > NumVFPs; |
| 3924 | if (!IsCPRC && PreAllocationGPRs < NumGPRs && AllocatedGPRs > NumGPRs && StackUsed) { |
| 3925 | llvm::Type *PaddingTy = llvm::ArrayType::get( |
| 3926 | llvm::Type::getInt32Ty(getVMContext()), NumGPRs - PreAllocationGPRs); |
Oliver Stannard | 39d26c9 | 2014-05-07 10:39:12 +0000 | [diff] [blame] | 3927 | I.info = ABIArgInfo::getDirect(nullptr /* type */, 0 /* offset */, |
| 3928 | PaddingTy); |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 3929 | |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 3930 | } |
| 3931 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3932 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3933 | // Always honor user-specified calling convention. |
| 3934 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 3935 | return; |
| 3936 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3937 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 3938 | if (cc != llvm::CallingConv::C) |
| 3939 | FI.setEffectiveCallingConvention(cc); |
| 3940 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 3941 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3942 | /// Return the default calling convention that LLVM will use. |
| 3943 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 3944 | // The default calling convention that LLVM will infer. |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 3945 | if (isEABIHF()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3946 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 3947 | else if (isEABI()) |
| 3948 | return llvm::CallingConv::ARM_AAPCS; |
| 3949 | else |
| 3950 | return llvm::CallingConv::ARM_APCS; |
| 3951 | } |
| 3952 | |
| 3953 | /// Return the calling convention that our ABI would like us to use |
| 3954 | /// as the C calling convention. |
| 3955 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3956 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3957 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 3958 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 3959 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 3960 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3961 | llvm_unreachable("bad ABI kind"); |
| 3962 | } |
| 3963 | |
| 3964 | void ARMABIInfo::setRuntimeCC() { |
| 3965 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 3966 | |
| 3967 | // Don't muddy up the IR with a ton of explicit annotations if |
| 3968 | // they'd just match what LLVM will infer from the triple. |
| 3969 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 3970 | if (abiCC != getLLVMDefaultCC()) |
| 3971 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3974 | /// isHomogeneousAggregate - Return true if a type is an AAPCS-VFP homogeneous |
| 3975 | /// aggregate. If HAMembers is non-null, the number of base elements |
| 3976 | /// contained in the type is returned through it; this is used for the |
| 3977 | /// recursive calls that check aggregate component types. |
| 3978 | static bool isHomogeneousAggregate(QualType Ty, const Type *&Base, |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 3979 | ASTContext &Context, uint64_t *HAMembers) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3980 | uint64_t Members = 0; |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3981 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3982 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Context, &Members)) |
| 3983 | return false; |
| 3984 | Members *= AT->getSize().getZExtValue(); |
| 3985 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3986 | const RecordDecl *RD = RT->getDecl(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3987 | if (RD->hasFlexibleArrayMember()) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3988 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3989 | |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3990 | Members = 0; |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 3991 | for (const auto *FD : RD->fields()) { |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3992 | uint64_t FldMembers; |
| 3993 | if (!isHomogeneousAggregate(FD->getType(), Base, Context, &FldMembers)) |
| 3994 | return false; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 3995 | |
| 3996 | Members = (RD->isUnion() ? |
| 3997 | std::max(Members, FldMembers) : Members + FldMembers); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } else { |
| 4000 | Members = 1; |
| 4001 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4002 | Members = 2; |
| 4003 | Ty = CT->getElementType(); |
| 4004 | } |
| 4005 | |
| 4006 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 4007 | // double, or 64-bit or 128-bit vectors. |
| 4008 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4009 | if (BT->getKind() != BuiltinType::Float && |
Tim Northover | eb752d4 | 2012-07-20 22:29:29 +0000 | [diff] [blame] | 4010 | BT->getKind() != BuiltinType::Double && |
| 4011 | BT->getKind() != BuiltinType::LongDouble) |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4012 | return false; |
| 4013 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4014 | unsigned VecSize = Context.getTypeSize(VT); |
| 4015 | if (VecSize != 64 && VecSize != 128) |
| 4016 | return false; |
| 4017 | } else { |
| 4018 | return false; |
| 4019 | } |
| 4020 | |
| 4021 | // The base type must be the same for all members. Vector types of the |
| 4022 | // same total size are treated as being equivalent here. |
| 4023 | const Type *TyPtr = Ty.getTypePtr(); |
| 4024 | if (!Base) |
| 4025 | Base = TyPtr; |
Oliver Stannard | 5e8558f | 2014-02-07 11:25:57 +0000 | [diff] [blame] | 4026 | |
| 4027 | if (Base != TyPtr) { |
| 4028 | // Homogeneous aggregates are defined as containing members with the |
| 4029 | // same machine type. There are two cases in which two members have |
| 4030 | // different TypePtrs but the same machine type: |
| 4031 | |
| 4032 | // 1) Vectors of the same length, regardless of the type and number |
| 4033 | // of their members. |
| 4034 | const bool SameLengthVectors = Base->isVectorType() && TyPtr->isVectorType() |
| 4035 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4036 | |
| 4037 | // 2) In the 32-bit AAPCS, `double' and `long double' have the same |
| 4038 | // machine type. This is not the case for the 64-bit AAPCS. |
| 4039 | const bool SameSizeDoubles = |
| 4040 | ( ( Base->isSpecificBuiltinType(BuiltinType::Double) |
| 4041 | && TyPtr->isSpecificBuiltinType(BuiltinType::LongDouble)) |
| 4042 | || ( Base->isSpecificBuiltinType(BuiltinType::LongDouble) |
| 4043 | && TyPtr->isSpecificBuiltinType(BuiltinType::Double))) |
| 4044 | && (Context.getTypeSize(Base) == Context.getTypeSize(TyPtr)); |
| 4045 | |
| 4046 | if (!SameLengthVectors && !SameSizeDoubles) |
| 4047 | return false; |
| 4048 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | // Homogeneous Aggregates can have at most 4 members of the base type. |
| 4052 | if (HAMembers) |
| 4053 | *HAMembers = Members; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4054 | |
| 4055 | return (Members > 0 && Members <= 4); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4058 | /// markAllocatedVFPs - update VFPRegs according to the alignment and |
| 4059 | /// number of VFP registers (unit is S register) requested. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4060 | void ARMABIInfo::markAllocatedVFPs(unsigned Alignment, |
| 4061 | unsigned NumRequired) const { |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4062 | // Early Exit. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4063 | if (AllocatedVFPs >= 16) { |
| 4064 | // We use AllocatedVFP > 16 to signal that some CPRCs were allocated on |
| 4065 | // the stack. |
| 4066 | AllocatedVFPs = 17; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4067 | return; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4068 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4069 | // C.1.vfp If the argument is a VFP CPRC and there are sufficient consecutive |
| 4070 | // VFP registers of the appropriate type unallocated then the argument is |
| 4071 | // allocated to the lowest-numbered sequence of such registers. |
| 4072 | for (unsigned I = 0; I < 16; I += Alignment) { |
| 4073 | bool FoundSlot = true; |
| 4074 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4075 | if (J >= 16 || VFPRegs[J]) { |
| 4076 | FoundSlot = false; |
| 4077 | break; |
| 4078 | } |
| 4079 | if (FoundSlot) { |
| 4080 | for (unsigned J = I, JEnd = I + NumRequired; J < JEnd; J++) |
| 4081 | VFPRegs[J] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4082 | AllocatedVFPs += NumRequired; |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4083 | return; |
| 4084 | } |
| 4085 | } |
| 4086 | // C.2.vfp If the argument is a VFP CPRC then any VFP registers that are |
| 4087 | // unallocated are marked as unavailable. |
| 4088 | for (unsigned I = 0; I < 16; I++) |
| 4089 | VFPRegs[I] = 1; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4090 | AllocatedVFPs = 17; // We do not have enough VFP registers. |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4093 | /// Update AllocatedGPRs to record the number of general purpose registers |
| 4094 | /// which have been allocated. It is valid for AllocatedGPRs to go above 4, |
| 4095 | /// this represents arguments being stored on the stack. |
| 4096 | void ARMABIInfo::markAllocatedGPRs(unsigned Alignment, |
| 4097 | unsigned NumRequired) const { |
| 4098 | assert((Alignment == 1 || Alignment == 2) && "Alignment must be 4 or 8 bytes"); |
| 4099 | |
| 4100 | if (Alignment == 2 && AllocatedGPRs & 0x1) |
| 4101 | AllocatedGPRs += 1; |
| 4102 | |
| 4103 | AllocatedGPRs += NumRequired; |
| 4104 | } |
| 4105 | |
| 4106 | void ARMABIInfo::resetAllocatedRegs(void) const { |
| 4107 | AllocatedGPRs = 0; |
| 4108 | AllocatedVFPs = 0; |
| 4109 | for (unsigned i = 0; i < NumVFPs; ++i) |
| 4110 | VFPRegs[i] = 0; |
| 4111 | } |
| 4112 | |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4113 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4114 | bool &IsCPRC) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4115 | // We update number of allocated VFPs according to |
| 4116 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 4117 | // A single-precision floating-point type (including promoted |
| 4118 | // half-precision types); A double-precision floating-point type; |
| 4119 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 4120 | // with a Base Type of a single- or double-precision floating-point type, |
| 4121 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 4122 | // to four Elements. |
| 4123 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4124 | // Handle illegal vector types here. |
| 4125 | if (isIllegalVectorType(Ty)) { |
| 4126 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4127 | if (Size <= 32) { |
| 4128 | llvm::Type *ResType = |
| 4129 | llvm::Type::getInt32Ty(getVMContext()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4130 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4131 | return ABIArgInfo::getDirect(ResType); |
| 4132 | } |
| 4133 | if (Size == 64) { |
| 4134 | llvm::Type *ResType = llvm::VectorType::get( |
| 4135 | llvm::Type::getInt32Ty(getVMContext()), 2); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4136 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic){ |
| 4137 | markAllocatedGPRs(2, 2); |
| 4138 | } else { |
| 4139 | markAllocatedVFPs(2, 2); |
| 4140 | IsCPRC = true; |
| 4141 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4142 | return ABIArgInfo::getDirect(ResType); |
| 4143 | } |
| 4144 | if (Size == 128) { |
| 4145 | llvm::Type *ResType = llvm::VectorType::get( |
| 4146 | llvm::Type::getInt32Ty(getVMContext()), 4); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4147 | if (getABIKind() == ARMABIInfo::AAPCS || isVariadic) { |
| 4148 | markAllocatedGPRs(2, 4); |
| 4149 | } else { |
| 4150 | markAllocatedVFPs(4, 4); |
| 4151 | IsCPRC = true; |
| 4152 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4153 | return ABIArgInfo::getDirect(ResType); |
| 4154 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4155 | markAllocatedGPRs(1, 1); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4156 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4157 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4158 | // Update VFPRegs for legal vector types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4159 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4160 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4161 | uint64_t Size = getContext().getTypeSize(VT); |
| 4162 | // Size of a legal vector should be power of 2 and above 64. |
| 4163 | markAllocatedVFPs(Size >= 128 ? 4 : 2, Size / 32); |
| 4164 | IsCPRC = true; |
| 4165 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4166 | } |
Manman Ren | b505d33 | 2012-10-31 19:02:26 +0000 | [diff] [blame] | 4167 | // Update VFPRegs for floating point types. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4168 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
| 4169 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4170 | if (BT->getKind() == BuiltinType::Half || |
| 4171 | BT->getKind() == BuiltinType::Float) { |
| 4172 | markAllocatedVFPs(1, 1); |
| 4173 | IsCPRC = true; |
| 4174 | } |
| 4175 | if (BT->getKind() == BuiltinType::Double || |
| 4176 | BT->getKind() == BuiltinType::LongDouble) { |
| 4177 | markAllocatedVFPs(2, 2); |
| 4178 | IsCPRC = true; |
| 4179 | } |
| 4180 | } |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4181 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4182 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4183 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4184 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4185 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4186 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4187 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4188 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4189 | unsigned Size = getContext().getTypeSize(Ty); |
| 4190 | if (!IsCPRC) |
| 4191 | markAllocatedGPRs(Size > 32 ? 2 : 1, (Size + 31) / 32); |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 4192 | return (Ty->isPromotableIntegerType() ? |
| 4193 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4194 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4195 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4196 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 4197 | markAllocatedGPRs(1, 1); |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4198 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4199 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 4200 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4201 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4202 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 4203 | return ABIArgInfo::getIgnore(); |
| 4204 | |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4205 | if (getABIKind() == ARMABIInfo::AAPCS_VFP && !isVariadic) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4206 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 4207 | // into VFP registers. |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4208 | const Type *Base = 0; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4209 | uint64_t Members = 0; |
| 4210 | if (isHomogeneousAggregate(Ty, Base, getContext(), &Members)) { |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4211 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4212 | // Base can be a floating-point or a vector. |
| 4213 | if (Base->isVectorType()) { |
| 4214 | // ElementSize is in number of floats. |
| 4215 | unsigned ElementSize = getContext().getTypeSize(Base) == 64 ? 2 : 4; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4216 | markAllocatedVFPs(ElementSize, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4217 | Members * ElementSize); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4218 | } else if (Base->isSpecificBuiltinType(BuiltinType::Float)) |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4219 | markAllocatedVFPs(1, Members); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4220 | else { |
| 4221 | assert(Base->isSpecificBuiltinType(BuiltinType::Double) || |
| 4222 | Base->isSpecificBuiltinType(BuiltinType::LongDouble)); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4223 | markAllocatedVFPs(2, Members * 2); |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 4224 | } |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4225 | IsCPRC = true; |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4226 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4227 | } |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 4228 | } |
| 4229 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 4230 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4231 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 4232 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 4233 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 4234 | uint64_t ABIAlign = 4; |
| 4235 | uint64_t TyAlign = getContext().getTypeAlign(Ty) / 8; |
| 4236 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4237 | getABIKind() == ARMABIInfo::AAPCS) |
| 4238 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 4239 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
James Molloy | 6f244b6 | 2014-05-09 16:21:39 +0000 | [diff] [blame] | 4240 | // Update Allocated GPRs |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4241 | markAllocatedGPRs(1, 1); |
Oliver Stannard | 7c3c09e | 2014-03-12 14:02:50 +0000 | [diff] [blame] | 4242 | return ABIArgInfo::getIndirect(TyAlign, /*ByVal=*/true, |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 4243 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 4246 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4247 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4248 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 4249 | // FIXME: Try to match the types of the arguments more accurately where |
| 4250 | // we can. |
| 4251 | if (getContext().getTypeAlign(Ty) <= 32) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 4252 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 4253 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4254 | markAllocatedGPRs(1, SizeRegs); |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4255 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 4256 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4257 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4258 | markAllocatedGPRs(2, SizeRegs * 2); |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 4259 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4260 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4261 | llvm::Type *STy = |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 4262 | llvm::StructType::get(llvm::ArrayType::get(ElemTy, SizeRegs), NULL); |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 4263 | return ABIArgInfo::getDirect(STy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4266 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4267 | llvm::LLVMContext &VMContext) { |
| 4268 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 4269 | // is called integer-like if its size is less than or equal to one word, and |
| 4270 | // the offset of each of its addressable sub-fields is zero. |
| 4271 | |
| 4272 | uint64_t Size = Context.getTypeSize(Ty); |
| 4273 | |
| 4274 | // Check that the type fits in a word. |
| 4275 | if (Size > 32) |
| 4276 | return false; |
| 4277 | |
| 4278 | // FIXME: Handle vector types! |
| 4279 | if (Ty->isVectorType()) |
| 4280 | return false; |
| 4281 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 4282 | // Float types are never treated as "integer like". |
| 4283 | if (Ty->isRealFloatingType()) |
| 4284 | return false; |
| 4285 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4286 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4287 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4288 | return true; |
| 4289 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 4290 | // Small complex integer types are "integer like". |
| 4291 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 4292 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4293 | |
| 4294 | // Single element and zero sized arrays should be allowed, by the definition |
| 4295 | // above, but they are not. |
| 4296 | |
| 4297 | // Otherwise, it must be a record type. |
| 4298 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 4299 | if (!RT) return false; |
| 4300 | |
| 4301 | // Ignore records with flexible arrays. |
| 4302 | const RecordDecl *RD = RT->getDecl(); |
| 4303 | if (RD->hasFlexibleArrayMember()) |
| 4304 | return false; |
| 4305 | |
| 4306 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 4307 | // like". |
| 4308 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 4309 | |
| 4310 | bool HadField = false; |
| 4311 | unsigned idx = 0; |
| 4312 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 4313 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4314 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4315 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4316 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 4317 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 4318 | // struct { int : 0; int x } |
| 4319 | // is non-integer like according to gcc. |
| 4320 | if (FD->isBitField()) { |
| 4321 | if (!RD->isUnion()) |
| 4322 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4323 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4324 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4325 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4326 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4327 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4330 | // Check if this field is at offset 0. |
| 4331 | if (Layout.getFieldOffset(idx) != 0) |
| 4332 | return false; |
| 4333 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4334 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 4335 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4336 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 4337 | // Only allow at most one field in a structure. This doesn't match the |
| 4338 | // wording above, but follows gcc in situations with a field following an |
| 4339 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4340 | if (!RD->isUnion()) { |
| 4341 | if (HadField) |
| 4342 | return false; |
| 4343 | |
| 4344 | HadField = true; |
| 4345 | } |
| 4346 | } |
| 4347 | |
| 4348 | return true; |
| 4349 | } |
| 4350 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4351 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, |
| 4352 | bool isVariadic) const { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4353 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4354 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4355 | |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4356 | // Large vector types should be returned via memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4357 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) { |
| 4358 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4359 | return ABIArgInfo::getIndirect(0); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4360 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 4361 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 4362 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4363 | // Treat an enum type as its underlying type. |
| 4364 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4365 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4366 | |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 4367 | return (RetTy->isPromotableIntegerType() ? |
| 4368 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 4369 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4370 | |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 4371 | // Structures with either a non-trivial destructor or a non-trivial |
| 4372 | // copy constructor are always indirect. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4373 | if (isRecordReturnIndirect(RetTy, getCXXABI())) { |
| 4374 | markAllocatedGPRs(1, 1); |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 4375 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4376 | } |
Rafael Espindola | bbd44ef | 2010-06-08 02:42:08 +0000 | [diff] [blame] | 4377 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4378 | // Are we following APCS? |
| 4379 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4380 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4381 | return ABIArgInfo::getIgnore(); |
| 4382 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4383 | // Complex types are all returned as packed integers. |
| 4384 | // |
| 4385 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 4386 | // correctly. |
| 4387 | if (RetTy->isAnyComplexType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4388 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4389 | getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 4390 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4391 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4392 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4393 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4394 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4395 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4396 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4397 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4398 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4399 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
| 4402 | // Otherwise return in memory. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4403 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4404 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4405 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4406 | |
| 4407 | // Otherwise this is an AAPCS variant. |
| 4408 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4409 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4410 | return ABIArgInfo::getIgnore(); |
| 4411 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4412 | // Check for homogeneous aggregates with AAPCS-VFP. |
Amara Emerson | 9dc7878 | 2014-01-28 10:56:36 +0000 | [diff] [blame] | 4413 | if (getABIKind() == AAPCS_VFP && !isVariadic) { |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4414 | const Type *Base = 0; |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4415 | if (isHomogeneousAggregate(RetTy, Base, getContext())) { |
| 4416 | assert(Base && "Base class should be set for homogeneous aggregate"); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4417 | // Homogeneous Aggregates are returned directly. |
| 4418 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 4215ca7 | 2012-04-13 11:22:00 +0000 | [diff] [blame] | 4419 | } |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 4420 | } |
| 4421 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4422 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 4423 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 4424 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4425 | if (Size <= 32) { |
| 4426 | // Return in the smallest viable integer type. |
| 4427 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4428 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4429 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 4430 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 4431 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 4434 | markAllocatedGPRs(1, 1); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 4435 | return ABIArgInfo::getIndirect(0); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4438 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 4439 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
| 4440 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4441 | // Check whether VT is legal. |
| 4442 | unsigned NumElements = VT->getNumElements(); |
| 4443 | uint64_t Size = getContext().getTypeSize(VT); |
| 4444 | // NumElements should be power of 2. |
| 4445 | if ((NumElements & (NumElements - 1)) != 0) |
| 4446 | return true; |
| 4447 | // Size should be greater than 32 bits. |
| 4448 | return Size <= 32; |
| 4449 | } |
| 4450 | return false; |
| 4451 | } |
| 4452 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4453 | llvm::Value *ARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4454 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4455 | llvm::Type *BP = CGF.Int8PtrTy; |
| 4456 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4457 | |
| 4458 | CGBuilderTy &Builder = CGF.Builder; |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4459 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4460 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4461 | |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 4462 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4463 | // These are ignored for parameter passing purposes. |
| 4464 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4465 | return Builder.CreateBitCast(Addr, PTy); |
| 4466 | } |
| 4467 | |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4468 | uint64_t Size = CGF.getContext().getTypeSize(Ty) / 8; |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4469 | uint64_t TyAlign = CGF.getContext().getTypeAlign(Ty) / 8; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4470 | bool IsIndirect = false; |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4471 | |
| 4472 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 4473 | // 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] | 4474 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 4475 | getABIKind() == ARMABIInfo::AAPCS) |
| 4476 | TyAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
| 4477 | else |
| 4478 | TyAlign = 4; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4479 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 4480 | if (isIllegalVectorType(Ty) && Size > 16) { |
| 4481 | IsIndirect = true; |
| 4482 | Size = 4; |
| 4483 | TyAlign = 4; |
| 4484 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4485 | |
| 4486 | // Handle address alignment for ABI alignment > 4 bytes. |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4487 | if (TyAlign > 4) { |
| 4488 | assert((TyAlign & (TyAlign - 1)) == 0 && |
| 4489 | "Alignment is not power of 2!"); |
| 4490 | llvm::Value *AddrAsInt = Builder.CreatePtrToInt(Addr, CGF.Int32Ty); |
| 4491 | AddrAsInt = Builder.CreateAdd(AddrAsInt, Builder.getInt32(TyAlign - 1)); |
| 4492 | AddrAsInt = Builder.CreateAnd(AddrAsInt, Builder.getInt32(~(TyAlign - 1))); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4493 | Addr = Builder.CreateIntToPtr(AddrAsInt, BP, "ap.align"); |
Rafael Espindola | 11d994b | 2011-08-02 22:33:37 +0000 | [diff] [blame] | 4494 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4495 | |
| 4496 | uint64_t Offset = |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4497 | llvm::RoundUpToAlignment(Size, 4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4498 | llvm::Value *NextAddr = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 4499 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4500 | "ap.next"); |
| 4501 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 4502 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 4503 | if (IsIndirect) |
| 4504 | Addr = Builder.CreateLoad(Builder.CreateBitCast(Addr, BPP)); |
Manman Ren | 67effb9 | 2012-10-16 19:51:48 +0000 | [diff] [blame] | 4505 | else if (TyAlign < CGF.getContext().getTypeAlign(Ty) / 8) { |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 4506 | // We can't directly cast ap.cur to pointer to a vector type, since ap.cur |
| 4507 | // may not be correctly aligned for the vector type. We create an aligned |
| 4508 | // temporary space and copy the content over from ap.cur to the temporary |
| 4509 | // space. This is necessary if the natural alignment of the type is greater |
| 4510 | // than the ABI alignment. |
| 4511 | llvm::Type *I8PtrTy = Builder.getInt8PtrTy(); |
| 4512 | CharUnits CharSize = getContext().getTypeSizeInChars(Ty); |
| 4513 | llvm::Value *AlignedTemp = CGF.CreateTempAlloca(CGF.ConvertType(Ty), |
| 4514 | "var.align"); |
| 4515 | llvm::Value *Dst = Builder.CreateBitCast(AlignedTemp, I8PtrTy); |
| 4516 | llvm::Value *Src = Builder.CreateBitCast(Addr, I8PtrTy); |
| 4517 | Builder.CreateMemCpy(Dst, Src, |
| 4518 | llvm::ConstantInt::get(CGF.IntPtrTy, CharSize.getQuantity()), |
| 4519 | TyAlign, false); |
| 4520 | Addr = AlignedTemp; //The content is in aligned location. |
| 4521 | } |
| 4522 | llvm::Type *PTy = |
| 4523 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 4524 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 4525 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 4526 | return AddrTyped; |
| 4527 | } |
| 4528 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4529 | namespace { |
| 4530 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4531 | class NaClARMABIInfo : public ABIInfo { |
| 4532 | public: |
| 4533 | NaClARMABIInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4534 | : ABIInfo(CGT), PInfo(CGT), NInfo(CGT, Kind) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4535 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4536 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4537 | CodeGenFunction &CGF) const override; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4538 | private: |
| 4539 | PNaClABIInfo PInfo; // Used for generating calls with pnaclcall callingconv. |
| 4540 | ARMABIInfo NInfo; // Used for everything else. |
| 4541 | }; |
| 4542 | |
| 4543 | class NaClARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4544 | public: |
| 4545 | NaClARMTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, ARMABIInfo::ABIKind Kind) |
| 4546 | : TargetCodeGenInfo(new NaClARMABIInfo(CGT, Kind)) {} |
| 4547 | }; |
| 4548 | |
Benjamin Kramer | 1cdb23d | 2012-10-20 13:02:06 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4551 | void NaClARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4552 | if (FI.getASTCallingConvention() == CC_PnaclCall) |
| 4553 | PInfo.computeInfo(FI); |
| 4554 | else |
| 4555 | static_cast<const ABIInfo&>(NInfo).computeInfo(FI); |
| 4556 | } |
| 4557 | |
| 4558 | llvm::Value *NaClARMABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4559 | CodeGenFunction &CGF) const { |
| 4560 | // Always use the native convention; calling pnacl-style varargs functions |
| 4561 | // is unsupported. |
| 4562 | return static_cast<const ABIInfo&>(NInfo).EmitVAArg(VAListAddr, Ty, CGF); |
| 4563 | } |
| 4564 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4565 | //===----------------------------------------------------------------------===// |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4566 | // AArch64 ABI Implementation |
| 4567 | //===----------------------------------------------------------------------===// |
| 4568 | |
| 4569 | namespace { |
| 4570 | |
| 4571 | class AArch64ABIInfo : public ABIInfo { |
| 4572 | public: |
| 4573 | AArch64ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4574 | |
| 4575 | private: |
| 4576 | // The AArch64 PCS is explicit about return types and argument types being |
| 4577 | // handled identically, so we don't need to draw a distinction between |
| 4578 | // Argument and Return classification. |
| 4579 | ABIArgInfo classifyGenericType(QualType Ty, int &FreeIntRegs, |
| 4580 | int &FreeVFPRegs) const; |
| 4581 | |
| 4582 | ABIArgInfo tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, bool IsInt, |
| 4583 | llvm::Type *DirectTy = 0) const; |
| 4584 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4585 | void computeInfo(CGFunctionInfo &FI) const override; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4586 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4587 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4588 | CodeGenFunction &CGF) const override; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4589 | }; |
| 4590 | |
| 4591 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4592 | public: |
| 4593 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT) |
| 4594 | :TargetCodeGenInfo(new AArch64ABIInfo(CGT)) {} |
| 4595 | |
| 4596 | const AArch64ABIInfo &getABIInfo() const { |
| 4597 | return static_cast<const AArch64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 4598 | } |
| 4599 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4600 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4601 | return 31; |
| 4602 | } |
| 4603 | |
| 4604 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4605 | llvm::Value *Address) const override { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4606 | // 0-31 are x0-x30 and sp: 8 bytes each |
| 4607 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
| 4608 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 31); |
| 4609 | |
| 4610 | // 64-95 are v0-v31: 16 bytes each |
| 4611 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
| 4612 | AssignToArrayRange(CGF.Builder, Address, Sixteen8, 64, 95); |
| 4613 | |
| 4614 | return false; |
| 4615 | } |
| 4616 | |
| 4617 | }; |
| 4618 | |
| 4619 | } |
| 4620 | |
| 4621 | void AArch64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 4622 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 4623 | |
| 4624 | FI.getReturnInfo() = classifyGenericType(FI.getReturnType(), |
| 4625 | FreeIntRegs, FreeVFPRegs); |
| 4626 | |
| 4627 | FreeIntRegs = FreeVFPRegs = 8; |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4628 | for (auto &I : FI.arguments()) { |
| 4629 | I.info = classifyGenericType(I.type, FreeIntRegs, FreeVFPRegs); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4630 | |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | ABIArgInfo |
| 4635 | AArch64ABIInfo::tryUseRegs(QualType Ty, int &FreeRegs, int RegsNeeded, |
| 4636 | bool IsInt, llvm::Type *DirectTy) const { |
| 4637 | if (FreeRegs >= RegsNeeded) { |
| 4638 | FreeRegs -= RegsNeeded; |
| 4639 | return ABIArgInfo::getDirect(DirectTy); |
| 4640 | } |
| 4641 | |
| 4642 | llvm::Type *Padding = 0; |
| 4643 | |
| 4644 | // We need padding so that later arguments don't get filled in anyway. That |
| 4645 | // wouldn't happen if only ByVal arguments followed in the same category, but |
| 4646 | // a large structure will simply seem to be a pointer as far as LLVM is |
| 4647 | // concerned. |
| 4648 | if (FreeRegs > 0) { |
| 4649 | if (IsInt) |
| 4650 | Padding = llvm::Type::getInt64Ty(getVMContext()); |
| 4651 | else |
| 4652 | Padding = llvm::Type::getFloatTy(getVMContext()); |
| 4653 | |
| 4654 | // Either [N x i64] or [N x float]. |
| 4655 | Padding = llvm::ArrayType::get(Padding, FreeRegs); |
| 4656 | FreeRegs = 0; |
| 4657 | } |
| 4658 | |
| 4659 | return ABIArgInfo::getIndirect(getContext().getTypeAlign(Ty) / 8, |
| 4660 | /*IsByVal=*/ true, /*Realign=*/ false, |
| 4661 | Padding); |
| 4662 | } |
| 4663 | |
| 4664 | |
| 4665 | ABIArgInfo AArch64ABIInfo::classifyGenericType(QualType Ty, |
| 4666 | int &FreeIntRegs, |
| 4667 | int &FreeVFPRegs) const { |
| 4668 | // Can only occurs for return, but harmless otherwise. |
| 4669 | if (Ty->isVoidType()) |
| 4670 | return ABIArgInfo::getIgnore(); |
| 4671 | |
| 4672 | // Large vector types should be returned via memory. There's no such concept |
| 4673 | // in the ABI, but they'd be over 16 bytes anyway so no matter how they're |
| 4674 | // classified they'd go into memory (see B.3). |
| 4675 | if (Ty->isVectorType() && getContext().getTypeSize(Ty) > 128) { |
| 4676 | if (FreeIntRegs > 0) |
| 4677 | --FreeIntRegs; |
| 4678 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 4679 | } |
| 4680 | |
| 4681 | // All non-aggregate LLVM types have a concrete ABI representation so they can |
| 4682 | // be passed directly. After this block we're guaranteed to be in a |
| 4683 | // complicated case. |
| 4684 | if (!isAggregateTypeForABI(Ty)) { |
| 4685 | // Treat an enum type as its underlying type. |
| 4686 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4687 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4688 | |
| 4689 | if (Ty->isFloatingType() || Ty->isVectorType()) |
| 4690 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ false); |
| 4691 | |
| 4692 | assert(getContext().getTypeSize(Ty) <= 128 && |
| 4693 | "unexpectedly large scalar type"); |
| 4694 | |
| 4695 | int RegsNeeded = getContext().getTypeSize(Ty) > 64 ? 2 : 1; |
| 4696 | |
| 4697 | // If the type may need padding registers to ensure "alignment", we must be |
| 4698 | // careful when this is accounted for. Increasing the effective size covers |
| 4699 | // all cases. |
| 4700 | if (getContext().getTypeAlign(Ty) == 128) |
| 4701 | RegsNeeded += FreeIntRegs % 2 != 0; |
| 4702 | |
| 4703 | return tryUseRegs(Ty, FreeIntRegs, RegsNeeded, /*IsInt=*/ true); |
| 4704 | } |
| 4705 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4706 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4707 | if (FreeIntRegs > 0 && RAA == CGCXXABI::RAA_Indirect) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4708 | --FreeIntRegs; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 4709 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 4713 | if (!getContext().getLangOpts().CPlusPlus) { |
| 4714 | // Empty structs outside C++ mode are a GNU extension, so no ABI can |
| 4715 | // possibly tell us what to do. It turns out (I believe) that GCC ignores |
| 4716 | // the object for parameter-passsing purposes. |
| 4717 | return ABIArgInfo::getIgnore(); |
| 4718 | } |
| 4719 | |
| 4720 | // The combination of C++98 9p5 (sizeof(struct) != 0) and the pseudocode |
| 4721 | // description of va_arg in the PCS require that an empty struct does |
| 4722 | // actually occupy space for parameter-passing. I'm hoping for a |
| 4723 | // clarification giving an explicit paragraph to point to in future. |
| 4724 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ 1, /*IsInt=*/ true, |
| 4725 | llvm::Type::getInt8Ty(getVMContext())); |
| 4726 | } |
| 4727 | |
| 4728 | // Homogeneous vector aggregates get passed in registers or on the stack. |
| 4729 | const Type *Base = 0; |
| 4730 | uint64_t NumMembers = 0; |
| 4731 | if (isHomogeneousAggregate(Ty, Base, getContext(), &NumMembers)) { |
| 4732 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 4733 | // Homogeneous aggregates are passed and returned directly. |
| 4734 | return tryUseRegs(Ty, FreeVFPRegs, /*RegsNeeded=*/ NumMembers, |
| 4735 | /*IsInt=*/ false); |
| 4736 | } |
| 4737 | |
| 4738 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4739 | if (Size <= 128) { |
| 4740 | // Small structs can use the same direct type whether they're in registers |
| 4741 | // or on the stack. |
| 4742 | llvm::Type *BaseTy; |
| 4743 | unsigned NumBases; |
| 4744 | int SizeInRegs = (Size + 63) / 64; |
| 4745 | |
| 4746 | if (getContext().getTypeAlign(Ty) == 128) { |
| 4747 | BaseTy = llvm::Type::getIntNTy(getVMContext(), 128); |
| 4748 | NumBases = 1; |
| 4749 | |
| 4750 | // If the type may need padding registers to ensure "alignment", we must |
| 4751 | // be careful when this is accounted for. Increasing the effective size |
| 4752 | // covers all cases. |
| 4753 | SizeInRegs += FreeIntRegs % 2 != 0; |
| 4754 | } else { |
| 4755 | BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 4756 | NumBases = SizeInRegs; |
| 4757 | } |
| 4758 | llvm::Type *DirectTy = llvm::ArrayType::get(BaseTy, NumBases); |
| 4759 | |
| 4760 | return tryUseRegs(Ty, FreeIntRegs, /*RegsNeeded=*/ SizeInRegs, |
| 4761 | /*IsInt=*/ true, DirectTy); |
| 4762 | } |
| 4763 | |
| 4764 | // If the aggregate is > 16 bytes, it's passed and returned indirectly. In |
| 4765 | // LLVM terms the return uses an "sret" pointer, but that's handled elsewhere. |
| 4766 | --FreeIntRegs; |
| 4767 | return ABIArgInfo::getIndirect(0, /* byVal = */ false); |
| 4768 | } |
| 4769 | |
| 4770 | llvm::Value *AArch64ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4771 | CodeGenFunction &CGF) const { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4772 | int FreeIntRegs = 8, FreeVFPRegs = 8; |
| 4773 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 4774 | ABIArgInfo AI = classifyGenericType(Ty, FreeIntRegs, FreeVFPRegs); |
| 4775 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4776 | return EmitAArch64VAArg(VAListAddr, Ty, 8 - FreeIntRegs, 8 - FreeVFPRegs, |
| 4777 | AI.isIndirect(), CGF); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
| 4780 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4781 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4782 | //===----------------------------------------------------------------------===// |
| 4783 | |
| 4784 | namespace { |
| 4785 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4786 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4787 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4788 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4789 | |
| 4790 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4791 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4792 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4793 | void computeInfo(CGFunctionInfo &FI) const override; |
| 4794 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4795 | CodeGenFunction &CFG) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4796 | }; |
| 4797 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4798 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4799 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4800 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4801 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4802 | |
| 4803 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4804 | CodeGen::CodeGenModule &M) const override; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4805 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4806 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 4807 | // resulting MDNode to the nvvm.annotations MDNode. |
| 4808 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4809 | }; |
| 4810 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4811 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4812 | if (RetTy->isVoidType()) |
| 4813 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4814 | |
| 4815 | // note: this is different from default ABI |
| 4816 | if (!RetTy->isScalarType()) |
| 4817 | return ABIArgInfo::getDirect(); |
| 4818 | |
| 4819 | // Treat an enum type as its underlying type. |
| 4820 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 4821 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 4822 | |
| 4823 | return (RetTy->isPromotableIntegerType() ? |
| 4824 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4827 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4828 | // Treat an enum type as its underlying type. |
| 4829 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4830 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4831 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 4832 | return (Ty->isPromotableIntegerType() ? |
| 4833 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4836 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4837 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4838 | for (auto &I : FI.arguments()) |
| 4839 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4840 | |
| 4841 | // Always honor user-specified calling convention. |
| 4842 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 4843 | return; |
| 4844 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4845 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 4846 | } |
| 4847 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4848 | llvm::Value *NVPTXABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4849 | CodeGenFunction &CFG) const { |
| 4850 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 4853 | void NVPTXTargetCodeGenInfo:: |
| 4854 | SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 4855 | CodeGen::CodeGenModule &M) const{ |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4856 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 4857 | if (!FD) return; |
| 4858 | |
| 4859 | llvm::Function *F = cast<llvm::Function>(GV); |
| 4860 | |
| 4861 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4862 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4863 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4864 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4865 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4866 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4867 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4868 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4869 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 4870 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4871 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4872 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4873 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4874 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4875 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4876 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 4877 | // __global__ functions cannot be called from the device, we do not |
| 4878 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4879 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 4880 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 4881 | addNVVMMetadata(F, "kernel", 1); |
| 4882 | } |
| 4883 | if (FD->hasAttr<CUDALaunchBoundsAttr>()) { |
| 4884 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
| 4885 | addNVVMMetadata(F, "maxntidx", |
| 4886 | FD->getAttr<CUDALaunchBoundsAttr>()->getMaxThreads()); |
| 4887 | // min blocks is a default argument for CUDALaunchBoundsAttr, so getting a |
| 4888 | // zero value from getMinBlocks either means it was not specified in |
| 4889 | // __launch_bounds__ or the user specified a 0 value. In both cases, we |
| 4890 | // don't have to add a PTX directive. |
| 4891 | int MinCTASM = FD->getAttr<CUDALaunchBoundsAttr>()->getMinBlocks(); |
| 4892 | if (MinCTASM > 0) { |
| 4893 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 4894 | addNVVMMetadata(F, "minctasm", MinCTASM); |
| 4895 | } |
| 4896 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 4897 | } |
| 4898 | } |
| 4899 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 4900 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 4901 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4902 | llvm::Module *M = F->getParent(); |
| 4903 | llvm::LLVMContext &Ctx = M->getContext(); |
| 4904 | |
| 4905 | // Get "nvvm.annotations" metadata node |
| 4906 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 4907 | |
Eli Bendersky | e1627b4 | 2014-04-15 17:19:26 +0000 | [diff] [blame] | 4908 | llvm::Value *MDVals[] = { |
| 4909 | F, llvm::MDString::get(Ctx, Name), |
| 4910 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand)}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 4911 | // Append metadata to nvvm.annotations |
| 4912 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 4913 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4917 | // SystemZ ABI Implementation |
| 4918 | //===----------------------------------------------------------------------===// |
| 4919 | |
| 4920 | namespace { |
| 4921 | |
| 4922 | class SystemZABIInfo : public ABIInfo { |
| 4923 | public: |
| 4924 | SystemZABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 4925 | |
| 4926 | bool isPromotableIntegerType(QualType Ty) const; |
| 4927 | bool isCompoundType(QualType Ty) const; |
| 4928 | bool isFPArgumentType(QualType Ty) const; |
| 4929 | |
| 4930 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4931 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 4932 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4933 | void computeInfo(CGFunctionInfo &FI) const override { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4934 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4935 | for (auto &I : FI.arguments()) |
| 4936 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4939 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 4940 | CodeGenFunction &CGF) const override; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4941 | }; |
| 4942 | |
| 4943 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 4944 | public: |
| 4945 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT) |
| 4946 | : TargetCodeGenInfo(new SystemZABIInfo(CGT)) {} |
| 4947 | }; |
| 4948 | |
| 4949 | } |
| 4950 | |
| 4951 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 4952 | // Treat an enum type as its underlying type. |
| 4953 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4954 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4955 | |
| 4956 | // Promotable integer types are required to be promoted by the ABI. |
| 4957 | if (Ty->isPromotableIntegerType()) |
| 4958 | return true; |
| 4959 | |
| 4960 | // 32-bit values must also be promoted. |
| 4961 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4962 | switch (BT->getKind()) { |
| 4963 | case BuiltinType::Int: |
| 4964 | case BuiltinType::UInt: |
| 4965 | return true; |
| 4966 | default: |
| 4967 | return false; |
| 4968 | } |
| 4969 | return false; |
| 4970 | } |
| 4971 | |
| 4972 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
| 4973 | return Ty->isAnyComplexType() || isAggregateTypeForABI(Ty); |
| 4974 | } |
| 4975 | |
| 4976 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 4977 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4978 | switch (BT->getKind()) { |
| 4979 | case BuiltinType::Float: |
| 4980 | case BuiltinType::Double: |
| 4981 | return true; |
| 4982 | default: |
| 4983 | return false; |
| 4984 | } |
| 4985 | |
| 4986 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 4987 | const RecordDecl *RD = RT->getDecl(); |
| 4988 | bool Found = false; |
| 4989 | |
| 4990 | // If this is a C++ record, check the bases first. |
| 4991 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 4992 | for (const auto &I : CXXRD->bases()) { |
| 4993 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 4994 | |
| 4995 | // Empty bases don't affect things either way. |
| 4996 | if (isEmptyRecord(getContext(), Base, true)) |
| 4997 | continue; |
| 4998 | |
| 4999 | if (Found) |
| 5000 | return false; |
| 5001 | Found = isFPArgumentType(Base); |
| 5002 | if (!Found) |
| 5003 | return false; |
| 5004 | } |
| 5005 | |
| 5006 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5007 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5008 | // Empty bitfields don't affect things either way. |
| 5009 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 5010 | // do count. So do anonymous bitfields that aren't zero-sized. |
| 5011 | if (FD->isBitField() && FD->getBitWidthValue(getContext()) == 0) |
| 5012 | return true; |
| 5013 | |
| 5014 | // Unlike isSingleElementStruct(), arrays do not count. |
| 5015 | // Nested isFPArgumentType structures still do though. |
| 5016 | if (Found) |
| 5017 | return false; |
| 5018 | Found = isFPArgumentType(FD->getType()); |
| 5019 | if (!Found) |
| 5020 | return false; |
| 5021 | } |
| 5022 | |
| 5023 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 5024 | // An 8-byte aligned struct s { float f; } is passed as a double. |
| 5025 | return Found; |
| 5026 | } |
| 5027 | |
| 5028 | return false; |
| 5029 | } |
| 5030 | |
| 5031 | llvm::Value *SystemZABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5032 | CodeGenFunction &CGF) const { |
| 5033 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 5034 | // struct { |
| 5035 | // i64 __gpr; |
| 5036 | // i64 __fpr; |
| 5037 | // i8 *__overflow_arg_area; |
| 5038 | // i8 *__reg_save_area; |
| 5039 | // }; |
| 5040 | |
| 5041 | // Every argument occupies 8 bytes and is passed by preference in either |
| 5042 | // GPRs or FPRs. |
| 5043 | Ty = CGF.getContext().getCanonicalType(Ty); |
| 5044 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 5045 | bool InFPRs = isFPArgumentType(Ty); |
| 5046 | |
| 5047 | llvm::Type *APTy = llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 5048 | bool IsIndirect = AI.isIndirect(); |
| 5049 | unsigned UnpaddedBitSize; |
| 5050 | if (IsIndirect) { |
| 5051 | APTy = llvm::PointerType::getUnqual(APTy); |
| 5052 | UnpaddedBitSize = 64; |
| 5053 | } else |
| 5054 | UnpaddedBitSize = getContext().getTypeSize(Ty); |
| 5055 | unsigned PaddedBitSize = 64; |
| 5056 | assert((UnpaddedBitSize <= PaddedBitSize) && "Invalid argument size."); |
| 5057 | |
| 5058 | unsigned PaddedSize = PaddedBitSize / 8; |
| 5059 | unsigned Padding = (PaddedBitSize - UnpaddedBitSize) / 8; |
| 5060 | |
| 5061 | unsigned MaxRegs, RegCountField, RegSaveIndex, RegPadding; |
| 5062 | if (InFPRs) { |
| 5063 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 5064 | RegCountField = 1; // __fpr |
| 5065 | RegSaveIndex = 16; // save offset for f0 |
| 5066 | RegPadding = 0; // floats are passed in the high bits of an FPR |
| 5067 | } else { |
| 5068 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 5069 | RegCountField = 0; // __gpr |
| 5070 | RegSaveIndex = 2; // save offset for r2 |
| 5071 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 5072 | } |
| 5073 | |
| 5074 | llvm::Value *RegCountPtr = |
| 5075 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
| 5076 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
| 5077 | llvm::Type *IndexTy = RegCount->getType(); |
| 5078 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 5079 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5080 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5081 | |
| 5082 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5083 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 5084 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 5085 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 5086 | |
| 5087 | // Emit code to load the value if it was passed in registers. |
| 5088 | CGF.EmitBlock(InRegBlock); |
| 5089 | |
| 5090 | // Work out the address of an argument register. |
| 5091 | llvm::Value *PaddedSizeV = llvm::ConstantInt::get(IndexTy, PaddedSize); |
| 5092 | llvm::Value *ScaledRegCount = |
| 5093 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 5094 | llvm::Value *RegBase = |
| 5095 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize + RegPadding); |
| 5096 | llvm::Value *RegOffset = |
| 5097 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
| 5098 | llvm::Value *RegSaveAreaPtr = |
| 5099 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
| 5100 | llvm::Value *RegSaveArea = |
| 5101 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
| 5102 | llvm::Value *RawRegAddr = |
| 5103 | CGF.Builder.CreateGEP(RegSaveArea, RegOffset, "raw_reg_addr"); |
| 5104 | llvm::Value *RegAddr = |
| 5105 | CGF.Builder.CreateBitCast(RawRegAddr, APTy, "reg_addr"); |
| 5106 | |
| 5107 | // Update the register count |
| 5108 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 5109 | llvm::Value *NewRegCount = |
| 5110 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 5111 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 5112 | CGF.EmitBranch(ContBlock); |
| 5113 | |
| 5114 | // Emit code to load the value if it was passed in memory. |
| 5115 | CGF.EmitBlock(InMemBlock); |
| 5116 | |
| 5117 | // Work out the address of a stack argument. |
| 5118 | llvm::Value *OverflowArgAreaPtr = |
| 5119 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
| 5120 | llvm::Value *OverflowArgArea = |
| 5121 | CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"); |
| 5122 | llvm::Value *PaddingV = llvm::ConstantInt::get(IndexTy, Padding); |
| 5123 | llvm::Value *RawMemAddr = |
| 5124 | CGF.Builder.CreateGEP(OverflowArgArea, PaddingV, "raw_mem_addr"); |
| 5125 | llvm::Value *MemAddr = |
| 5126 | CGF.Builder.CreateBitCast(RawMemAddr, APTy, "mem_addr"); |
| 5127 | |
| 5128 | // Update overflow_arg_area_ptr pointer |
| 5129 | llvm::Value *NewOverflowArgArea = |
| 5130 | CGF.Builder.CreateGEP(OverflowArgArea, PaddedSizeV, "overflow_arg_area"); |
| 5131 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 5132 | CGF.EmitBranch(ContBlock); |
| 5133 | |
| 5134 | // Return the appropriate result. |
| 5135 | CGF.EmitBlock(ContBlock); |
| 5136 | llvm::PHINode *ResAddr = CGF.Builder.CreatePHI(APTy, 2, "va_arg.addr"); |
| 5137 | ResAddr->addIncoming(RegAddr, InRegBlock); |
| 5138 | ResAddr->addIncoming(MemAddr, InMemBlock); |
| 5139 | |
| 5140 | if (IsIndirect) |
| 5141 | return CGF.Builder.CreateLoad(ResAddr, "indirect_arg"); |
| 5142 | |
| 5143 | return ResAddr; |
| 5144 | } |
| 5145 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5146 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 5147 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 5148 | assert(Triple.getArch() == llvm::Triple::x86); |
| 5149 | |
| 5150 | switch (Opts.getStructReturnConvention()) { |
| 5151 | case CodeGenOptions::SRCK_Default: |
| 5152 | break; |
| 5153 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 5154 | return false; |
| 5155 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 5156 | return true; |
| 5157 | } |
| 5158 | |
| 5159 | if (Triple.isOSDarwin()) |
| 5160 | return true; |
| 5161 | |
| 5162 | switch (Triple.getOS()) { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5163 | case llvm::Triple::AuroraUX: |
| 5164 | case llvm::Triple::DragonFly: |
| 5165 | case llvm::Triple::FreeBSD: |
| 5166 | case llvm::Triple::OpenBSD: |
| 5167 | case llvm::Triple::Bitrig: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5168 | return true; |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 5169 | case llvm::Triple::Win32: |
| 5170 | switch (Triple.getEnvironment()) { |
| 5171 | case llvm::Triple::UnknownEnvironment: |
| 5172 | case llvm::Triple::Cygnus: |
| 5173 | case llvm::Triple::GNU: |
| 5174 | case llvm::Triple::MSVC: |
| 5175 | return true; |
| 5176 | default: |
| 5177 | return false; |
| 5178 | } |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 5179 | default: |
| 5180 | return false; |
| 5181 | } |
| 5182 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5183 | |
| 5184 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 5185 | if (RetTy->isVoidType()) |
| 5186 | return ABIArgInfo::getIgnore(); |
| 5187 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
| 5188 | return ABIArgInfo::getIndirect(0); |
| 5189 | return (isPromotableIntegerType(RetTy) ? |
| 5190 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5191 | } |
| 5192 | |
| 5193 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 5194 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5195 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5196 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5197 | |
| 5198 | // Integers and enums are extended to full register width. |
| 5199 | if (isPromotableIntegerType(Ty)) |
| 5200 | return ABIArgInfo::getExtend(); |
| 5201 | |
| 5202 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
| 5203 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5204 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5205 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5206 | |
| 5207 | // Handle small structures. |
| 5208 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 5209 | // Structures with flexible arrays have variable length, so really |
| 5210 | // fail the size test above. |
| 5211 | const RecordDecl *RD = RT->getDecl(); |
| 5212 | if (RD->hasFlexibleArrayMember()) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5213 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5214 | |
| 5215 | // The structure is passed as an unextended integer, a float, or a double. |
| 5216 | llvm::Type *PassTy; |
| 5217 | if (isFPArgumentType(Ty)) { |
| 5218 | assert(Size == 32 || Size == 64); |
| 5219 | if (Size == 32) |
| 5220 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 5221 | else |
| 5222 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 5223 | } else |
| 5224 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 5225 | return ABIArgInfo::getDirect(PassTy); |
| 5226 | } |
| 5227 | |
| 5228 | // Non-structure compounds are passed indirectly. |
| 5229 | if (isCompoundType(Ty)) |
Richard Sandiford | cdd8688 | 2013-12-04 09:59:57 +0000 | [diff] [blame] | 5230 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 5231 | |
| 5232 | return ABIArgInfo::getDirect(0); |
| 5233 | } |
| 5234 | |
| 5235 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5236 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5237 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5238 | |
| 5239 | namespace { |
| 5240 | |
| 5241 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 5242 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5243 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 5244 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5245 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5246 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5247 | }; |
| 5248 | |
| 5249 | } |
| 5250 | |
| 5251 | void MSP430TargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5252 | llvm::GlobalValue *GV, |
| 5253 | CodeGen::CodeGenModule &M) const { |
| 5254 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5255 | if (const MSP430InterruptAttr *attr = FD->getAttr<MSP430InterruptAttr>()) { |
| 5256 | // Handle 'interrupt' attribute: |
| 5257 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5258 | |
| 5259 | // Step 1: Set ISR calling convention. |
| 5260 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
| 5261 | |
| 5262 | // Step 2: Add attributes goodness. |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5263 | F->addFnAttr(llvm::Attribute::NoInline); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5264 | |
| 5265 | // Step 3: Emit ISR vector alias. |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5266 | unsigned Num = attr->getNumber() / 2; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5267 | new llvm::GlobalAlias(GV->getType(), llvm::Function::ExternalLinkage, |
Anton Korobeynikov | c5a7f92 | 2012-11-26 18:59:10 +0000 | [diff] [blame] | 5268 | "__isr_" + Twine(Num), |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5269 | GV, &M.getModule()); |
| 5270 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5271 | } |
| 5272 | } |
| 5273 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5274 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5275 | // MIPS ABI Implementation. This works for both little-endian and |
| 5276 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5277 | //===----------------------------------------------------------------------===// |
| 5278 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5279 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5280 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5281 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5282 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 5283 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5284 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5285 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5286 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5287 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5288 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5289 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5290 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5291 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5292 | |
| 5293 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5294 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5295 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5296 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5297 | CodeGenFunction &CGF) const override; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5298 | }; |
| 5299 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5300 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5301 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5302 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 5303 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 5304 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 5305 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5306 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5307 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5308 | return 29; |
| 5309 | } |
| 5310 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5311 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5312 | CodeGen::CodeGenModule &CGM) const override { |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5313 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5314 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 5315 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5316 | if (FD->hasAttr<Mips16Attr>()) { |
| 5317 | Fn->addFnAttr("mips16"); |
| 5318 | } |
| 5319 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 5320 | Fn->addFnAttr("nomips16"); |
| 5321 | } |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 5322 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 5323 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5324 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5325 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5326 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5327 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 5328 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5329 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5330 | }; |
| 5331 | } |
| 5332 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5333 | void MipsABIInfo::CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5334 | SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5335 | llvm::IntegerType *IntTy = |
| 5336 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5337 | |
| 5338 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 5339 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 5340 | ArgList.push_back(IntTy); |
| 5341 | |
| 5342 | // If necessary, add one more integer type to ArgList. |
| 5343 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 5344 | |
| 5345 | if (R) |
| 5346 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5347 | } |
| 5348 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5349 | // In N32/64, an aligned double precision floating point field is passed in |
| 5350 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5351 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5352 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 5353 | |
| 5354 | if (IsO32) { |
| 5355 | CoerceToIntArgs(TySize, ArgList); |
| 5356 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5357 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5358 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 5359 | if (Ty->isComplexType()) |
| 5360 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 5361 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5362 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5363 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5364 | // Unions/vectors are passed in integer registers. |
| 5365 | if (!RT || !RT->isStructureOrClassType()) { |
| 5366 | CoerceToIntArgs(TySize, ArgList); |
| 5367 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5368 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5369 | |
| 5370 | const RecordDecl *RD = RT->getDecl(); |
| 5371 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5372 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5373 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5374 | uint64_t LastOffset = 0; |
| 5375 | unsigned idx = 0; |
| 5376 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 5377 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 5378 | // Iterate over fields in the struct/class and check if there are any aligned |
| 5379 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5380 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 5381 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5382 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5383 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 5384 | |
| 5385 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 5386 | continue; |
| 5387 | |
| 5388 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 5389 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 5390 | continue; |
| 5391 | |
| 5392 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 5393 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 5394 | ArgList.push_back(I64); |
| 5395 | |
| 5396 | // Add double type. |
| 5397 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 5398 | LastOffset = Offset + 64; |
| 5399 | } |
| 5400 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5401 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 5402 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 5403 | |
| 5404 | return llvm::StructType::get(getVMContext(), ArgList); |
| 5405 | } |
| 5406 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5407 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 5408 | uint64_t Offset) const { |
| 5409 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
| 5410 | return 0; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5411 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5412 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5413 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 5414 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5415 | ABIArgInfo |
| 5416 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5417 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5418 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5419 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5420 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5421 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 5422 | (uint64_t)StackAlignInBytes); |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5423 | unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align); |
| 5424 | Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5425 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5426 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5427 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5428 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5429 | return ABIArgInfo::getIgnore(); |
| 5430 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5431 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5432 | Offset = OrigOffset + MinABIStackAlignInBytes; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5433 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 5434 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 5435 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 5436 | // If we have reached here, aggregates are passed directly by coercing to |
| 5437 | // another structure type. Padding is inserted if the offset of the |
| 5438 | // aggregate is unaligned. |
| 5439 | return ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5440 | getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5441 | } |
| 5442 | |
| 5443 | // Treat an enum type as its underlying type. |
| 5444 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5445 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5446 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 5447 | if (Ty->isPromotableIntegerType()) |
| 5448 | return ABIArgInfo::getExtend(); |
| 5449 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 5450 | return ABIArgInfo::getDirect( |
| 5451 | 0, 0, IsO32 ? 0 : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5454 | llvm::Type* |
| 5455 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5456 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5457 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5458 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5459 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5460 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5461 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 5462 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5463 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5464 | // N32/64 returns struct/classes in floating point registers if the |
| 5465 | // following conditions are met: |
| 5466 | // 1. The size of the struct/class is no larger than 128-bit. |
| 5467 | // 2. The struct/class has one or two fields all of which are floating |
| 5468 | // point types. |
| 5469 | // 3. The offset of the first field is zero (this follows what gcc does). |
| 5470 | // |
| 5471 | // Any other composite results are returned in integer registers. |
| 5472 | // |
| 5473 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 5474 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 5475 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5476 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5477 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5478 | if (!BT || !BT->isFloatingPoint()) |
| 5479 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5480 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 5481 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 5482 | } |
| 5483 | |
| 5484 | if (b == e) |
| 5485 | return llvm::StructType::get(getVMContext(), RTList, |
| 5486 | RD->hasAttr<PackedAttr>()); |
| 5487 | |
| 5488 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5489 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5492 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5493 | return llvm::StructType::get(getVMContext(), RTList); |
| 5494 | } |
| 5495 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5496 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 5497 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5498 | |
| 5499 | if (RetTy->isVoidType() || Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5500 | return ABIArgInfo::getIgnore(); |
| 5501 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 5502 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5503 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5504 | return ABIArgInfo::getIndirect(0); |
| 5505 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5506 | if (Size <= 128) { |
| 5507 | if (RetTy->isAnyComplexType()) |
| 5508 | return ABIArgInfo::getDirect(); |
| 5509 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 5510 | // O32 returns integer vectors in registers. |
| 5511 | if (IsO32 && RetTy->isVectorType() && !RetTy->hasFloatingRepresentation()) |
| 5512 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5513 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5514 | if (!IsO32) |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 5515 | return ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 5516 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5517 | |
| 5518 | return ABIArgInfo::getIndirect(0); |
| 5519 | } |
| 5520 | |
| 5521 | // Treat an enum type as its underlying type. |
| 5522 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5523 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5524 | |
| 5525 | return (RetTy->isPromotableIntegerType() ? |
| 5526 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5527 | } |
| 5528 | |
| 5529 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5530 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
| 5531 | RetInfo = classifyReturnType(FI.getReturnType()); |
| 5532 | |
| 5533 | // 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] | 5534 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 5535 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5536 | for (auto &I : FI.arguments()) |
| 5537 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | llvm::Value* MipsABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5541 | CodeGenFunction &CGF) const { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5542 | llvm::Type *BP = CGF.Int8PtrTy; |
| 5543 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5544 | |
| 5545 | CGBuilderTy &Builder = CGF.Builder; |
| 5546 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 5547 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5548 | int64_t TypeAlign = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5549 | llvm::Type *PTy = llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5550 | llvm::Value *AddrTyped; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 5551 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5552 | llvm::IntegerType *IntTy = (PtrWidth == 32) ? CGF.Int32Ty : CGF.Int64Ty; |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5553 | |
| 5554 | if (TypeAlign > MinABIStackAlignInBytes) { |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5555 | llvm::Value *AddrAsInt = CGF.Builder.CreatePtrToInt(Addr, IntTy); |
| 5556 | llvm::Value *Inc = llvm::ConstantInt::get(IntTy, TypeAlign - 1); |
| 5557 | llvm::Value *Mask = llvm::ConstantInt::get(IntTy, -TypeAlign); |
| 5558 | llvm::Value *Add = CGF.Builder.CreateAdd(AddrAsInt, Inc); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5559 | llvm::Value *And = CGF.Builder.CreateAnd(Add, Mask); |
| 5560 | AddrTyped = CGF.Builder.CreateIntToPtr(And, PTy); |
| 5561 | } |
| 5562 | else |
| 5563 | AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5564 | |
| 5565 | llvm::Value *AlignedAddr = Builder.CreateBitCast(AddrTyped, BP); |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5566 | TypeAlign = std::max((unsigned)TypeAlign, MinABIStackAlignInBytes); |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5567 | uint64_t Offset = |
| 5568 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, TypeAlign); |
| 5569 | llvm::Value *NextAddr = |
Akira Hatanaka | 3771528 | 2012-01-23 23:59:52 +0000 | [diff] [blame] | 5570 | Builder.CreateGEP(AlignedAddr, llvm::ConstantInt::get(IntTy, Offset), |
Akira Hatanaka | fb1d9f3 | 2011-08-01 20:48:01 +0000 | [diff] [blame] | 5571 | "ap.next"); |
| 5572 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5573 | |
| 5574 | return AddrTyped; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 5575 | } |
| 5576 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5577 | bool |
| 5578 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 5579 | llvm::Value *Address) const { |
| 5580 | // This information comes from gcc's implementation, which seems to |
| 5581 | // as canonical as it gets. |
| 5582 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5583 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 5584 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5585 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5586 | |
| 5587 | // 0-31 are the general purpose registers, $0 - $31. |
| 5588 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 5589 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 5590 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5591 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5592 | |
| 5593 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 5594 | // They are one bit wide and ignored here. |
| 5595 | |
| 5596 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 5597 | // (coprocessor 1 is the FP unit) |
| 5598 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 5599 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 5600 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5601 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5602 | return false; |
| 5603 | } |
| 5604 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5605 | //===----------------------------------------------------------------------===// |
| 5606 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
| 5607 | // Currently subclassed only to implement custom OpenCL C function attribute |
| 5608 | // handling. |
| 5609 | //===----------------------------------------------------------------------===// |
| 5610 | |
| 5611 | namespace { |
| 5612 | |
| 5613 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 5614 | public: |
| 5615 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 5616 | : DefaultTargetCodeGenInfo(CGT) {} |
| 5617 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5618 | void SetTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5619 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5620 | }; |
| 5621 | |
| 5622 | void TCETargetCodeGenInfo::SetTargetAttributes(const Decl *D, |
| 5623 | llvm::GlobalValue *GV, |
| 5624 | CodeGen::CodeGenModule &M) const { |
| 5625 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 5626 | if (!FD) return; |
| 5627 | |
| 5628 | llvm::Function *F = cast<llvm::Function>(GV); |
| 5629 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5630 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5631 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 5632 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 5633 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5634 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 5635 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5636 | // Convert the reqd_work_group_size() attributes to metadata. |
| 5637 | llvm::LLVMContext &Context = F->getContext(); |
| 5638 | llvm::NamedMDNode *OpenCLMetadata = |
| 5639 | M.getModule().getOrInsertNamedMetadata("opencl.kernel_wg_size_info"); |
| 5640 | |
| 5641 | SmallVector<llvm::Value*, 5> Operands; |
| 5642 | Operands.push_back(F); |
| 5643 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5644 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5645 | llvm::APInt(32, Attr->getXDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5646 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5647 | llvm::APInt(32, Attr->getYDim()))); |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5648 | Operands.push_back(llvm::Constant::getIntegerValue(M.Int32Ty, |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 5649 | llvm::APInt(32, Attr->getZDim()))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5650 | |
| 5651 | // Add a boolean constant operand for "required" (true) or "hint" (false) |
| 5652 | // for implementing the work_group_size_hint attr later. Currently |
| 5653 | // always true as the hint is not yet implemented. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5654 | Operands.push_back(llvm::ConstantInt::getTrue(Context)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 5655 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 5656 | } |
| 5657 | } |
| 5658 | } |
| 5659 | } |
| 5660 | |
| 5661 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 5662 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5663 | //===----------------------------------------------------------------------===// |
| 5664 | // Hexagon ABI Implementation |
| 5665 | //===----------------------------------------------------------------------===// |
| 5666 | |
| 5667 | namespace { |
| 5668 | |
| 5669 | class HexagonABIInfo : public ABIInfo { |
| 5670 | |
| 5671 | |
| 5672 | public: |
| 5673 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5674 | |
| 5675 | private: |
| 5676 | |
| 5677 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 5678 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 5679 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5680 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5681 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5682 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5683 | CodeGenFunction &CGF) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5684 | }; |
| 5685 | |
| 5686 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5687 | public: |
| 5688 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 5689 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 5690 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5691 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5692 | return 29; |
| 5693 | } |
| 5694 | }; |
| 5695 | |
| 5696 | } |
| 5697 | |
| 5698 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 5699 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 5700 | for (auto &I : FI.arguments()) |
| 5701 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 5705 | if (!isAggregateTypeForABI(Ty)) { |
| 5706 | // Treat an enum type as its underlying type. |
| 5707 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5708 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5709 | |
| 5710 | return (Ty->isPromotableIntegerType() ? |
| 5711 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5712 | } |
| 5713 | |
| 5714 | // Ignore empty records. |
| 5715 | if (isEmptyRecord(getContext(), Ty, true)) |
| 5716 | return ABIArgInfo::getIgnore(); |
| 5717 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5718 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 5719 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5720 | |
| 5721 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5722 | if (Size > 64) |
| 5723 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5724 | // Pass in the smallest viable integer type. |
| 5725 | else if (Size > 32) |
| 5726 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5727 | else if (Size > 16) |
| 5728 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5729 | else if (Size > 8) |
| 5730 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5731 | else |
| 5732 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5733 | } |
| 5734 | |
| 5735 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 5736 | if (RetTy->isVoidType()) |
| 5737 | return ABIArgInfo::getIgnore(); |
| 5738 | |
| 5739 | // Large vector types should be returned via memory. |
| 5740 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
| 5741 | return ABIArgInfo::getIndirect(0); |
| 5742 | |
| 5743 | if (!isAggregateTypeForABI(RetTy)) { |
| 5744 | // Treat an enum type as its underlying type. |
| 5745 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5746 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5747 | |
| 5748 | return (RetTy->isPromotableIntegerType() ? |
| 5749 | ABIArgInfo::getExtend() : ABIArgInfo::getDirect()); |
| 5750 | } |
| 5751 | |
| 5752 | // Structures with either a non-trivial destructor or a non-trivial |
| 5753 | // copy constructor are always indirect. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 5754 | if (isRecordReturnIndirect(RetTy, getCXXABI())) |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5755 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5756 | |
| 5757 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 5758 | return ABIArgInfo::getIgnore(); |
| 5759 | |
| 5760 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 5761 | // are returned indirectly. |
| 5762 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5763 | if (Size <= 64) { |
| 5764 | // Return in the smallest viable integer type. |
| 5765 | if (Size <= 8) |
| 5766 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5767 | if (Size <= 16) |
| 5768 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 5769 | if (Size <= 32) |
| 5770 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 5771 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 5772 | } |
| 5773 | |
| 5774 | return ABIArgInfo::getIndirect(0, /*ByVal=*/true); |
| 5775 | } |
| 5776 | |
| 5777 | llvm::Value *HexagonABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5778 | CodeGenFunction &CGF) const { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5779 | // FIXME: Need to handle alignment |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5780 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 5781 | |
| 5782 | CGBuilderTy &Builder = CGF.Builder; |
| 5783 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, |
| 5784 | "ap"); |
| 5785 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 5786 | llvm::Type *PTy = |
| 5787 | llvm::PointerType::getUnqual(CGF.ConvertType(Ty)); |
| 5788 | llvm::Value *AddrTyped = Builder.CreateBitCast(Addr, PTy); |
| 5789 | |
| 5790 | uint64_t Offset = |
| 5791 | llvm::RoundUpToAlignment(CGF.getContext().getTypeSize(Ty) / 8, 4); |
| 5792 | llvm::Value *NextAddr = |
| 5793 | Builder.CreateGEP(Addr, llvm::ConstantInt::get(CGF.Int32Ty, Offset), |
| 5794 | "ap.next"); |
| 5795 | Builder.CreateStore(NextAddr, VAListAddrAsBPP); |
| 5796 | |
| 5797 | return AddrTyped; |
| 5798 | } |
| 5799 | |
| 5800 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5801 | //===----------------------------------------------------------------------===// |
| 5802 | // SPARC v9 ABI Implementation. |
| 5803 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 5804 | // |
| 5805 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 5806 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 5807 | // the array, structs larger than 16 bytes are passed indirectly. |
| 5808 | // |
| 5809 | // One case requires special care: |
| 5810 | // |
| 5811 | // struct mixed { |
| 5812 | // int i; |
| 5813 | // float f; |
| 5814 | // }; |
| 5815 | // |
| 5816 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 5817 | // parameter array, but the int is passed in an integer register, and the float |
| 5818 | // is passed in a floating point register. This is represented as two arguments |
| 5819 | // with the LLVM IR inreg attribute: |
| 5820 | // |
| 5821 | // declare void f(i32 inreg %i, float inreg %f) |
| 5822 | // |
| 5823 | // The code generator will only allocate 4 bytes from the parameter array for |
| 5824 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 5825 | // bytes. |
| 5826 | // |
| 5827 | namespace { |
| 5828 | class SparcV9ABIInfo : public ABIInfo { |
| 5829 | public: |
| 5830 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 5831 | |
| 5832 | private: |
| 5833 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5834 | void computeInfo(CGFunctionInfo &FI) const override; |
| 5835 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 5836 | CodeGenFunction &CGF) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 5837 | |
| 5838 | // Coercion type builder for structs passed in registers. The coercion type |
| 5839 | // serves two purposes: |
| 5840 | // |
| 5841 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 5842 | // in registers. |
| 5843 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 5844 | // code generator knows to pass them in floating point registers. |
| 5845 | // |
| 5846 | // We also compute the InReg flag which indicates that the struct contains |
| 5847 | // aligned 32-bit floats. |
| 5848 | // |
| 5849 | struct CoerceBuilder { |
| 5850 | llvm::LLVMContext &Context; |
| 5851 | const llvm::DataLayout &DL; |
| 5852 | SmallVector<llvm::Type*, 8> Elems; |
| 5853 | uint64_t Size; |
| 5854 | bool InReg; |
| 5855 | |
| 5856 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 5857 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 5858 | |
| 5859 | // Pad Elems with integers until Size is ToSize. |
| 5860 | void pad(uint64_t ToSize) { |
| 5861 | assert(ToSize >= Size && "Cannot remove elements"); |
| 5862 | if (ToSize == Size) |
| 5863 | return; |
| 5864 | |
| 5865 | // Finish the current 64-bit word. |
| 5866 | uint64_t Aligned = llvm::RoundUpToAlignment(Size, 64); |
| 5867 | if (Aligned > Size && Aligned <= ToSize) { |
| 5868 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 5869 | Size = Aligned; |
| 5870 | } |
| 5871 | |
| 5872 | // Add whole 64-bit words. |
| 5873 | while (Size + 64 <= ToSize) { |
| 5874 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 5875 | Size += 64; |
| 5876 | } |
| 5877 | |
| 5878 | // Final in-word padding. |
| 5879 | if (Size < ToSize) { |
| 5880 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 5881 | Size = ToSize; |
| 5882 | } |
| 5883 | } |
| 5884 | |
| 5885 | // Add a floating point element at Offset. |
| 5886 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 5887 | // Unaligned floats are treated as integers. |
| 5888 | if (Offset % Bits) |
| 5889 | return; |
| 5890 | // The InReg flag is only required if there are any floats < 64 bits. |
| 5891 | if (Bits < 64) |
| 5892 | InReg = true; |
| 5893 | pad(Offset); |
| 5894 | Elems.push_back(Ty); |
| 5895 | Size = Offset + Bits; |
| 5896 | } |
| 5897 | |
| 5898 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 5899 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 5900 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 5901 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 5902 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 5903 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 5904 | switch (ElemTy->getTypeID()) { |
| 5905 | case llvm::Type::StructTyID: |
| 5906 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 5907 | break; |
| 5908 | case llvm::Type::FloatTyID: |
| 5909 | addFloat(ElemOffset, ElemTy, 32); |
| 5910 | break; |
| 5911 | case llvm::Type::DoubleTyID: |
| 5912 | addFloat(ElemOffset, ElemTy, 64); |
| 5913 | break; |
| 5914 | case llvm::Type::FP128TyID: |
| 5915 | addFloat(ElemOffset, ElemTy, 128); |
| 5916 | break; |
| 5917 | case llvm::Type::PointerTyID: |
| 5918 | if (ElemOffset % 64 == 0) { |
| 5919 | pad(ElemOffset); |
| 5920 | Elems.push_back(ElemTy); |
| 5921 | Size += 64; |
| 5922 | } |
| 5923 | break; |
| 5924 | default: |
| 5925 | break; |
| 5926 | } |
| 5927 | } |
| 5928 | } |
| 5929 | |
| 5930 | // Check if Ty is a usable substitute for the coercion type. |
| 5931 | bool isUsableType(llvm::StructType *Ty) const { |
| 5932 | if (Ty->getNumElements() != Elems.size()) |
| 5933 | return false; |
| 5934 | for (unsigned i = 0, e = Elems.size(); i != e; ++i) |
| 5935 | if (Elems[i] != Ty->getElementType(i)) |
| 5936 | return false; |
| 5937 | return true; |
| 5938 | } |
| 5939 | |
| 5940 | // Get the coercion type as a literal struct type. |
| 5941 | llvm::Type *getType() const { |
| 5942 | if (Elems.size() == 1) |
| 5943 | return Elems.front(); |
| 5944 | else |
| 5945 | return llvm::StructType::get(Context, Elems); |
| 5946 | } |
| 5947 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5948 | }; |
| 5949 | } // end anonymous namespace |
| 5950 | |
| 5951 | ABIArgInfo |
| 5952 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 5953 | if (Ty->isVoidType()) |
| 5954 | return ABIArgInfo::getIgnore(); |
| 5955 | |
| 5956 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5957 | |
| 5958 | // Anything too big to fit in registers is passed with an explicit indirect |
| 5959 | // pointer / sret pointer. |
| 5960 | if (Size > SizeLimit) |
| 5961 | return ABIArgInfo::getIndirect(0, /*ByVal=*/false); |
| 5962 | |
| 5963 | // Treat an enum type as its underlying type. |
| 5964 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5965 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5966 | |
| 5967 | // Integer types smaller than a register are extended. |
| 5968 | if (Size < 64 && Ty->isIntegerType()) |
| 5969 | return ABIArgInfo::getExtend(); |
| 5970 | |
| 5971 | // Other non-aggregates go in registers. |
| 5972 | if (!isAggregateTypeForABI(Ty)) |
| 5973 | return ABIArgInfo::getDirect(); |
| 5974 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 5975 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 5976 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 5977 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 5978 | return ABIArgInfo::getIndirect(0, RAA == CGCXXABI::RAA_DirectInMemory); |
| 5979 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5980 | // 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] | 5981 | // Build a coercion type from the LLVM struct type. |
| 5982 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 5983 | if (!StrTy) |
| 5984 | return ABIArgInfo::getDirect(); |
| 5985 | |
| 5986 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 5987 | CB.addStruct(0, StrTy); |
| 5988 | CB.pad(llvm::RoundUpToAlignment(CB.DL.getTypeSizeInBits(StrTy), 64)); |
| 5989 | |
| 5990 | // Try to use the original type for coercion. |
| 5991 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 5992 | |
| 5993 | if (CB.InReg) |
| 5994 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 5995 | else |
| 5996 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
| 5999 | llvm::Value *SparcV9ABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6000 | CodeGenFunction &CGF) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6001 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 6002 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6003 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6004 | AI.setCoerceToType(ArgTy); |
| 6005 | |
| 6006 | llvm::Type *BPP = CGF.Int8PtrPtrTy; |
| 6007 | CGBuilderTy &Builder = CGF.Builder; |
| 6008 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, BPP, "ap"); |
| 6009 | llvm::Value *Addr = Builder.CreateLoad(VAListAddrAsBPP, "ap.cur"); |
| 6010 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 6011 | llvm::Value *ArgAddr; |
| 6012 | unsigned Stride; |
| 6013 | |
| 6014 | switch (AI.getKind()) { |
| 6015 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6016 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 6017 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6018 | |
| 6019 | case ABIArgInfo::Extend: |
| 6020 | Stride = 8; |
| 6021 | ArgAddr = Builder |
| 6022 | .CreateConstGEP1_32(Addr, 8 - getDataLayout().getTypeAllocSize(ArgTy), |
| 6023 | "extend"); |
| 6024 | break; |
| 6025 | |
| 6026 | case ABIArgInfo::Direct: |
| 6027 | Stride = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6028 | ArgAddr = Addr; |
| 6029 | break; |
| 6030 | |
| 6031 | case ABIArgInfo::Indirect: |
| 6032 | Stride = 8; |
| 6033 | ArgAddr = Builder.CreateBitCast(Addr, |
| 6034 | llvm::PointerType::getUnqual(ArgPtrTy), |
| 6035 | "indirect"); |
| 6036 | ArgAddr = Builder.CreateLoad(ArgAddr, "indirect.arg"); |
| 6037 | break; |
| 6038 | |
| 6039 | case ABIArgInfo::Ignore: |
| 6040 | return llvm::UndefValue::get(ArgPtrTy); |
| 6041 | } |
| 6042 | |
| 6043 | // Update VAList. |
| 6044 | Addr = Builder.CreateConstGEP1_32(Addr, Stride, "ap.next"); |
| 6045 | Builder.CreateStore(Addr, VAListAddrAsBPP); |
| 6046 | |
| 6047 | return Builder.CreatePointerCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 6051 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6052 | for (auto &I : FI.arguments()) |
| 6053 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | namespace { |
| 6057 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6058 | public: |
| 6059 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6060 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6061 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6062 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6063 | return 14; |
| 6064 | } |
| 6065 | |
| 6066 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6067 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6068 | }; |
| 6069 | } // end anonymous namespace |
| 6070 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 6071 | bool |
| 6072 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 6073 | llvm::Value *Address) const { |
| 6074 | // This is calculated from the LLVM and GCC tables and verified |
| 6075 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 6076 | |
| 6077 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 6078 | |
| 6079 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 6080 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 6081 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 6082 | |
| 6083 | // 0-31: the 8-byte general-purpose registers |
| 6084 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 6085 | |
| 6086 | // 32-63: f0-31, the 4-byte floating-point registers |
| 6087 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 6088 | |
| 6089 | // Y = 64 |
| 6090 | // PSR = 65 |
| 6091 | // WIM = 66 |
| 6092 | // TBR = 67 |
| 6093 | // PC = 68 |
| 6094 | // NPC = 69 |
| 6095 | // FSR = 70 |
| 6096 | // CSR = 71 |
| 6097 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
| 6098 | |
| 6099 | // 72-87: d0-15, the 8-byte floating-point registers |
| 6100 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 6101 | |
| 6102 | return false; |
| 6103 | } |
| 6104 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6105 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6106 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6107 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6108 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6109 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6110 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6111 | |
| 6112 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 6113 | /// it by reference between functions that append to it. |
| 6114 | typedef llvm::SmallString<128> SmallStringEnc; |
| 6115 | |
| 6116 | /// TypeStringCache caches the meta encodings of Types. |
| 6117 | /// |
| 6118 | /// The reason for caching TypeStrings is two fold: |
| 6119 | /// 1. To cache a type's encoding for later uses; |
| 6120 | /// 2. As a means to break recursive member type inclusion. |
| 6121 | /// |
| 6122 | /// A cache Entry can have a Status of: |
| 6123 | /// NonRecursive: The type encoding is not recursive; |
| 6124 | /// Recursive: The type encoding is recursive; |
| 6125 | /// Incomplete: An incomplete TypeString; |
| 6126 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 6127 | /// Recursive type encoding. |
| 6128 | /// |
| 6129 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 6130 | /// as possible. Whilst it may contain types which are recursive, the type |
| 6131 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 6132 | /// the type is encountered. |
| 6133 | /// |
| 6134 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 6135 | /// possible. The type itself is recursive and it may contain other types which |
| 6136 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 6137 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 6138 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 6139 | /// |
| 6140 | /// An Incomplete entry is always a RecordType and only encodes its |
| 6141 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 6142 | /// are placed into the cache during type expansion as a means to identify and |
| 6143 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 6144 | /// the entry becomes IncompleteUsed. |
| 6145 | /// |
| 6146 | /// During the expansion of a RecordType's members: |
| 6147 | /// |
| 6148 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 6149 | /// cached encoding is used; |
| 6150 | /// |
| 6151 | /// If the cache contains a Recursive encoding for the member type, the |
| 6152 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 6153 | /// |
| 6154 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 6155 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 6156 | /// |
| 6157 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 6158 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 6159 | /// it is swapped back in; |
| 6160 | /// |
| 6161 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 6162 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 6163 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 6164 | /// |
| 6165 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 6166 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 6167 | /// Else the member is part of a recursive type and thus the recursion has |
| 6168 | /// been exited too soon for the encoding to be correct for the member. |
| 6169 | /// |
| 6170 | class TypeStringCache { |
| 6171 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 6172 | struct Entry { |
| 6173 | std::string Str; // The encoded TypeString for the type. |
| 6174 | enum Status State; // Information about the encoding in 'Str'. |
| 6175 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 6176 | // during the expansion of RecordType's members. |
| 6177 | }; |
| 6178 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 6179 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 6180 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 6181 | public: |
Robert Lytton | d263f14 | 2014-05-06 09:38:54 +0000 | [diff] [blame] | 6182 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6183 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 6184 | bool removeIncomplete(const IdentifierInfo *ID); |
| 6185 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6186 | bool IsRecursive); |
| 6187 | StringRef lookupStr(const IdentifierInfo *ID); |
| 6188 | }; |
| 6189 | |
| 6190 | /// TypeString encodings for union fields must be order. |
| 6191 | /// FieldEncoding is a helper for this ordering process. |
| 6192 | class FieldEncoding { |
| 6193 | bool HasName; |
| 6194 | std::string Enc; |
| 6195 | public: |
| 6196 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {}; |
| 6197 | StringRef str() {return Enc.c_str();}; |
| 6198 | bool operator<(const FieldEncoding &rhs) const { |
| 6199 | if (HasName != rhs.HasName) return HasName; |
| 6200 | return Enc < rhs.Enc; |
| 6201 | } |
| 6202 | }; |
| 6203 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6204 | class XCoreABIInfo : public DefaultABIInfo { |
| 6205 | public: |
| 6206 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6207 | llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6208 | CodeGenFunction &CGF) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6209 | }; |
| 6210 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6211 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6212 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6213 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6214 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6215 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 6216 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6217 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6218 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6219 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6220 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6221 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6222 | llvm::Value *XCoreABIInfo::EmitVAArg(llvm::Value *VAListAddr, QualType Ty, |
| 6223 | CodeGenFunction &CGF) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6224 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6225 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6226 | // Get the VAList. |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6227 | llvm::Value *VAListAddrAsBPP = Builder.CreateBitCast(VAListAddr, |
| 6228 | CGF.Int8PtrPtrTy); |
| 6229 | llvm::Value *AP = Builder.CreateLoad(VAListAddrAsBPP); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6230 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6231 | // Handle the argument. |
| 6232 | ABIArgInfo AI = classifyArgumentType(Ty); |
| 6233 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 6234 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 6235 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6236 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6237 | llvm::Value *Val; |
Andy Gibbs | d9ba472 | 2013-10-14 07:02:04 +0000 | [diff] [blame] | 6238 | uint64_t ArgSize = 0; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6239 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6240 | case ABIArgInfo::Expand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 6241 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6242 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 6243 | case ABIArgInfo::Ignore: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6244 | Val = llvm::UndefValue::get(ArgPtrTy); |
| 6245 | ArgSize = 0; |
| 6246 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6247 | case ABIArgInfo::Extend: |
| 6248 | case ABIArgInfo::Direct: |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6249 | Val = Builder.CreatePointerCast(AP, ArgPtrTy); |
| 6250 | ArgSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
| 6251 | if (ArgSize < 4) |
| 6252 | ArgSize = 4; |
| 6253 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6254 | case ABIArgInfo::Indirect: |
| 6255 | llvm::Value *ArgAddr; |
| 6256 | ArgAddr = Builder.CreateBitCast(AP, llvm::PointerType::getUnqual(ArgPtrTy)); |
| 6257 | ArgAddr = Builder.CreateLoad(ArgAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6258 | Val = Builder.CreatePointerCast(ArgAddr, ArgPtrTy); |
| 6259 | ArgSize = 4; |
| 6260 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6261 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Increment the VAList. |
| 6264 | if (ArgSize) { |
| 6265 | llvm::Value *APN = Builder.CreateConstGEP1_32(AP, ArgSize); |
| 6266 | Builder.CreateStore(APN, VAListAddrAsBPP); |
| 6267 | } |
| 6268 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 6269 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6270 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 6271 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 6272 | /// into the cache as a means to identify and break recursion. |
| 6273 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 6274 | /// be reinserted by removeIncomplete(). |
| 6275 | /// All other types of encoding should have been used rather than arriving here. |
| 6276 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 6277 | std::string StubEnc) { |
| 6278 | if (!ID) |
| 6279 | return; |
| 6280 | Entry &E = Map[ID]; |
| 6281 | assert( (E.Str.empty() || E.State == Recursive) && |
| 6282 | "Incorrectly use of addIncomplete"); |
| 6283 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 6284 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 6285 | E.Str.swap(StubEnc); |
| 6286 | E.State = Incomplete; |
| 6287 | ++IncompleteCount; |
| 6288 | } |
| 6289 | |
| 6290 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 6291 | /// must be removed from the cache. |
| 6292 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 6293 | /// Returns true if the RecordType was defined recursively. |
| 6294 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 6295 | if (!ID) |
| 6296 | return false; |
| 6297 | auto I = Map.find(ID); |
| 6298 | assert(I != Map.end() && "Entry not present"); |
| 6299 | Entry &E = I->second; |
| 6300 | assert( (E.State == Incomplete || |
| 6301 | E.State == IncompleteUsed) && |
| 6302 | "Entry must be an incomplete type"); |
| 6303 | bool IsRecursive = false; |
| 6304 | if (E.State == IncompleteUsed) { |
| 6305 | // We made use of our Incomplete encoding, thus we are recursive. |
| 6306 | IsRecursive = true; |
| 6307 | --IncompleteUsedCount; |
| 6308 | } |
| 6309 | if (E.Swapped.empty()) |
| 6310 | Map.erase(I); |
| 6311 | else { |
| 6312 | // Swap the Recursive back. |
| 6313 | E.Swapped.swap(E.Str); |
| 6314 | E.Swapped.clear(); |
| 6315 | E.State = Recursive; |
| 6316 | } |
| 6317 | --IncompleteCount; |
| 6318 | return IsRecursive; |
| 6319 | } |
| 6320 | |
| 6321 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 6322 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 6323 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 6324 | bool IsRecursive) { |
| 6325 | if (!ID || IncompleteUsedCount) |
| 6326 | return; // No key or it is is an incomplete sub-type so don't add. |
| 6327 | Entry &E = Map[ID]; |
| 6328 | if (IsRecursive && !E.Str.empty()) { |
| 6329 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 6330 | "This is not the same Recursive entry"); |
| 6331 | // The parent container was not recursive after all, so we could have used |
| 6332 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 6333 | // we started viz: IncompleteCount!=0. |
| 6334 | return; |
| 6335 | } |
| 6336 | assert(E.Str.empty() && "Entry already present"); |
| 6337 | E.Str = Str.str(); |
| 6338 | E.State = IsRecursive? Recursive : NonRecursive; |
| 6339 | } |
| 6340 | |
| 6341 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 6342 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 6343 | /// encoding is Recursive, return an empty StringRef. |
| 6344 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 6345 | if (!ID) |
| 6346 | return StringRef(); // We have no key. |
| 6347 | auto I = Map.find(ID); |
| 6348 | if (I == Map.end()) |
| 6349 | return StringRef(); // We have no encoding. |
| 6350 | Entry &E = I->second; |
| 6351 | if (E.State == Recursive && IncompleteCount) |
| 6352 | return StringRef(); // We don't use Recursive encodings for member types. |
| 6353 | |
| 6354 | if (E.State == Incomplete) { |
| 6355 | // The incomplete type is being used to break out of recursion. |
| 6356 | E.State = IncompleteUsed; |
| 6357 | ++IncompleteUsedCount; |
| 6358 | } |
| 6359 | return E.Str.c_str(); |
| 6360 | } |
| 6361 | |
| 6362 | /// The XCore ABI includes a type information section that communicates symbol |
| 6363 | /// type information to the linker. The linker uses this information to verify |
| 6364 | /// safety/correctness of things such as array bound and pointers et al. |
| 6365 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 6366 | /// This type information (TypeString) is emitted into meta data for all global |
| 6367 | /// symbols: definitions, declarations, functions & variables. |
| 6368 | /// |
| 6369 | /// The TypeString carries type, qualifier, name, size & value details. |
| 6370 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
| 6371 | /// <https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf> |
| 6372 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 6373 | /// |
| 6374 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6375 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 6376 | |
| 6377 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 6378 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 6379 | CodeGen::CodeGenModule &CGM) const { |
| 6380 | SmallStringEnc Enc; |
| 6381 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 6382 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
| 6383 | llvm::SmallVector<llvm::Value *, 2> MDVals; |
| 6384 | MDVals.push_back(GV); |
| 6385 | MDVals.push_back(llvm::MDString::get(Ctx, Enc.str())); |
| 6386 | llvm::NamedMDNode *MD = |
| 6387 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 6388 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6389 | } |
| 6390 | } |
| 6391 | |
| 6392 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6393 | const CodeGen::CodeGenModule &CGM, |
| 6394 | TypeStringCache &TSC); |
| 6395 | |
| 6396 | /// Helper function for appendRecordType(). |
| 6397 | /// Builds a SmallVector containing the encoded field types in declaration order. |
| 6398 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 6399 | const RecordDecl *RD, |
| 6400 | const CodeGen::CodeGenModule &CGM, |
| 6401 | TypeStringCache &TSC) { |
| 6402 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 6403 | I != E; ++I) { |
| 6404 | SmallStringEnc Enc; |
| 6405 | Enc += "m("; |
| 6406 | Enc += I->getName(); |
| 6407 | Enc += "){"; |
| 6408 | if (I->isBitField()) { |
| 6409 | Enc += "b("; |
| 6410 | llvm::raw_svector_ostream OS(Enc); |
| 6411 | OS.resync(); |
| 6412 | OS << I->getBitWidthValue(CGM.getContext()); |
| 6413 | OS.flush(); |
| 6414 | Enc += ':'; |
| 6415 | } |
| 6416 | if (!appendType(Enc, I->getType(), CGM, TSC)) |
| 6417 | return false; |
| 6418 | if (I->isBitField()) |
| 6419 | Enc += ')'; |
| 6420 | Enc += '}'; |
| 6421 | FE.push_back(FieldEncoding(!I->getName().empty(), Enc)); |
| 6422 | } |
| 6423 | return true; |
| 6424 | } |
| 6425 | |
| 6426 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 6427 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 6428 | /// Union types have their fields ordered according to the ABI. |
| 6429 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 6430 | const CodeGen::CodeGenModule &CGM, |
| 6431 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 6432 | // Append the cached TypeString if we have one. |
| 6433 | StringRef TypeString = TSC.lookupStr(ID); |
| 6434 | if (!TypeString.empty()) { |
| 6435 | Enc += TypeString; |
| 6436 | return true; |
| 6437 | } |
| 6438 | |
| 6439 | // Start to emit an incomplete TypeString. |
| 6440 | size_t Start = Enc.size(); |
| 6441 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 6442 | Enc += '('; |
| 6443 | if (ID) |
| 6444 | Enc += ID->getName(); |
| 6445 | Enc += "){"; |
| 6446 | |
| 6447 | // We collect all encoded fields and order as necessary. |
| 6448 | bool IsRecursive = false; |
| 6449 | SmallVector<FieldEncoding, 16> FE; |
| 6450 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 6451 | if (RD && !RD->field_empty()) { |
| 6452 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 6453 | // so that recursive calls to this RecordType will use it whilst building a |
| 6454 | // complete TypeString for this RecordType. |
| 6455 | std::string StubEnc(Enc.substr(Start).str()); |
| 6456 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 6457 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 6458 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 6459 | (void) TSC.removeIncomplete(ID); |
| 6460 | return false; |
| 6461 | } |
| 6462 | IsRecursive = TSC.removeIncomplete(ID); |
| 6463 | // The ABI requires unions to be sorted but not structures. |
| 6464 | // See FieldEncoding::operator< for sort algorithm. |
| 6465 | if (RT->isUnionType()) |
| 6466 | std::sort(FE.begin(), FE.end()); |
| 6467 | } |
| 6468 | |
| 6469 | // We can now complete the TypeString. |
| 6470 | if (unsigned E = FE.size()) |
| 6471 | for (unsigned I = 0; I != E; ++I) { |
| 6472 | if (I) |
| 6473 | Enc += ','; |
| 6474 | Enc += FE[I].str(); |
| 6475 | } |
| 6476 | Enc += '}'; |
| 6477 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 6478 | return true; |
| 6479 | } |
| 6480 | |
| 6481 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 6482 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 6483 | TypeStringCache &TSC, |
| 6484 | const IdentifierInfo *ID) { |
| 6485 | // Append the cached TypeString if we have one. |
| 6486 | StringRef TypeString = TSC.lookupStr(ID); |
| 6487 | if (!TypeString.empty()) { |
| 6488 | Enc += TypeString; |
| 6489 | return true; |
| 6490 | } |
| 6491 | |
| 6492 | size_t Start = Enc.size(); |
| 6493 | Enc += "e("; |
| 6494 | if (ID) |
| 6495 | Enc += ID->getName(); |
| 6496 | Enc += "){"; |
| 6497 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
| 6498 | auto I = ED->enumerator_begin(); |
| 6499 | auto E = ED->enumerator_end(); |
| 6500 | while (I != E) { |
| 6501 | Enc += "m("; |
| 6502 | Enc += I->getName(); |
| 6503 | Enc += "){"; |
| 6504 | I->getInitVal().toString(Enc); |
| 6505 | Enc += '}'; |
| 6506 | ++I; |
| 6507 | if (I != E) |
| 6508 | Enc += ','; |
| 6509 | } |
| 6510 | } |
| 6511 | Enc += '}'; |
| 6512 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 6513 | return true; |
| 6514 | } |
| 6515 | |
| 6516 | /// Appends type's qualifier to Enc. |
| 6517 | /// This is done prior to appending the type's encoding. |
| 6518 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 6519 | // Qualifiers are emitted in alphabetical order. |
| 6520 | static const char *Table[] = {"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
| 6521 | int Lookup = 0; |
| 6522 | if (QT.isConstQualified()) |
| 6523 | Lookup += 1<<0; |
| 6524 | if (QT.isRestrictQualified()) |
| 6525 | Lookup += 1<<1; |
| 6526 | if (QT.isVolatileQualified()) |
| 6527 | Lookup += 1<<2; |
| 6528 | Enc += Table[Lookup]; |
| 6529 | } |
| 6530 | |
| 6531 | /// Appends built-in types to Enc. |
| 6532 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 6533 | const char *EncType; |
| 6534 | switch (BT->getKind()) { |
| 6535 | case BuiltinType::Void: |
| 6536 | EncType = "0"; |
| 6537 | break; |
| 6538 | case BuiltinType::Bool: |
| 6539 | EncType = "b"; |
| 6540 | break; |
| 6541 | case BuiltinType::Char_U: |
| 6542 | EncType = "uc"; |
| 6543 | break; |
| 6544 | case BuiltinType::UChar: |
| 6545 | EncType = "uc"; |
| 6546 | break; |
| 6547 | case BuiltinType::SChar: |
| 6548 | EncType = "sc"; |
| 6549 | break; |
| 6550 | case BuiltinType::UShort: |
| 6551 | EncType = "us"; |
| 6552 | break; |
| 6553 | case BuiltinType::Short: |
| 6554 | EncType = "ss"; |
| 6555 | break; |
| 6556 | case BuiltinType::UInt: |
| 6557 | EncType = "ui"; |
| 6558 | break; |
| 6559 | case BuiltinType::Int: |
| 6560 | EncType = "si"; |
| 6561 | break; |
| 6562 | case BuiltinType::ULong: |
| 6563 | EncType = "ul"; |
| 6564 | break; |
| 6565 | case BuiltinType::Long: |
| 6566 | EncType = "sl"; |
| 6567 | break; |
| 6568 | case BuiltinType::ULongLong: |
| 6569 | EncType = "ull"; |
| 6570 | break; |
| 6571 | case BuiltinType::LongLong: |
| 6572 | EncType = "sll"; |
| 6573 | break; |
| 6574 | case BuiltinType::Float: |
| 6575 | EncType = "ft"; |
| 6576 | break; |
| 6577 | case BuiltinType::Double: |
| 6578 | EncType = "d"; |
| 6579 | break; |
| 6580 | case BuiltinType::LongDouble: |
| 6581 | EncType = "ld"; |
| 6582 | break; |
| 6583 | default: |
| 6584 | return false; |
| 6585 | } |
| 6586 | Enc += EncType; |
| 6587 | return true; |
| 6588 | } |
| 6589 | |
| 6590 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 6591 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 6592 | const CodeGen::CodeGenModule &CGM, |
| 6593 | TypeStringCache &TSC) { |
| 6594 | Enc += "p("; |
| 6595 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 6596 | return false; |
| 6597 | Enc += ')'; |
| 6598 | return true; |
| 6599 | } |
| 6600 | |
| 6601 | /// Appends array encoding to Enc before calling appendType for the element. |
| 6602 | static bool appendArrayType(SmallStringEnc &Enc, const ArrayType *AT, |
| 6603 | const CodeGen::CodeGenModule &CGM, |
| 6604 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 6605 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 6606 | return false; |
| 6607 | Enc += "a("; |
| 6608 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6609 | CAT->getSize().toStringUnsigned(Enc); |
| 6610 | else |
| 6611 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 6612 | Enc += ':'; |
| 6613 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 6614 | return false; |
| 6615 | Enc += ')'; |
| 6616 | return true; |
| 6617 | } |
| 6618 | |
| 6619 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 6620 | /// and the arguments. |
| 6621 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 6622 | const CodeGen::CodeGenModule &CGM, |
| 6623 | TypeStringCache &TSC) { |
| 6624 | Enc += "f{"; |
| 6625 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 6626 | return false; |
| 6627 | Enc += "}("; |
| 6628 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 6629 | // N.B. we are only interested in the adjusted param types. |
| 6630 | auto I = FPT->param_type_begin(); |
| 6631 | auto E = FPT->param_type_end(); |
| 6632 | if (I != E) { |
| 6633 | do { |
| 6634 | if (!appendType(Enc, *I, CGM, TSC)) |
| 6635 | return false; |
| 6636 | ++I; |
| 6637 | if (I != E) |
| 6638 | Enc += ','; |
| 6639 | } while (I != E); |
| 6640 | if (FPT->isVariadic()) |
| 6641 | Enc += ",va"; |
| 6642 | } else { |
| 6643 | if (FPT->isVariadic()) |
| 6644 | Enc += "va"; |
| 6645 | else |
| 6646 | Enc += '0'; |
| 6647 | } |
| 6648 | } |
| 6649 | Enc += ')'; |
| 6650 | return true; |
| 6651 | } |
| 6652 | |
| 6653 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 6654 | /// type encodings. |
| 6655 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 6656 | const CodeGen::CodeGenModule &CGM, |
| 6657 | TypeStringCache &TSC) { |
| 6658 | |
| 6659 | QualType QT = QType.getCanonicalType(); |
| 6660 | |
| 6661 | appendQualifier(Enc, QT); |
| 6662 | |
| 6663 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 6664 | return appendBuiltinType(Enc, BT); |
| 6665 | |
| 6666 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 6667 | return appendArrayType(Enc, AT, CGM, TSC, ""); |
| 6668 | |
| 6669 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 6670 | return appendPointerType(Enc, PT, CGM, TSC); |
| 6671 | |
| 6672 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 6673 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 6674 | |
| 6675 | if (const RecordType *RT = QT->getAsStructureType()) |
| 6676 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6677 | |
| 6678 | if (const RecordType *RT = QT->getAsUnionType()) |
| 6679 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 6680 | |
| 6681 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 6682 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 6683 | |
| 6684 | return false; |
| 6685 | } |
| 6686 | |
| 6687 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 6688 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 6689 | if (!D) |
| 6690 | return false; |
| 6691 | |
| 6692 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 6693 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 6694 | return false; |
| 6695 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 6696 | } |
| 6697 | |
| 6698 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 6699 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 6700 | return false; |
| 6701 | QualType QT = VD->getType().getCanonicalType(); |
| 6702 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 6703 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
| 6704 | appendQualifier(Enc, QT); |
| 6705 | return appendArrayType(Enc, AT, CGM, TSC, "*"); |
| 6706 | } |
| 6707 | return appendType(Enc, QT, CGM, TSC); |
| 6708 | } |
| 6709 | return false; |
| 6710 | } |
| 6711 | |
| 6712 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6713 | //===----------------------------------------------------------------------===// |
| 6714 | // Driver code |
| 6715 | //===----------------------------------------------------------------------===// |
| 6716 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6717 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6718 | if (TheTargetCodeGenInfo) |
| 6719 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6720 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6721 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 6722 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6723 | default: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6724 | return *(TheTargetCodeGenInfo = new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6725 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 6726 | case llvm::Triple::le32: |
| 6727 | return *(TheTargetCodeGenInfo = new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6728 | case llvm::Triple::mips: |
| 6729 | case llvm::Triple::mipsel: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6730 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, true)); |
| 6731 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 6732 | case llvm::Triple::mips64: |
| 6733 | case llvm::Triple::mips64el: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6734 | return *(TheTargetCodeGenInfo = new MIPSTargetCodeGenInfo(Types, false)); |
| 6735 | |
James Molloy | 7f4ba53 | 2014-04-23 10:26:08 +0000 | [diff] [blame] | 6736 | case llvm::Triple::arm64: |
| 6737 | case llvm::Triple::arm64_be: { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 6738 | ARM64ABIInfo::ABIKind Kind = ARM64ABIInfo::AAPCS; |
| 6739 | if (strcmp(getTarget().getABI(), "darwinpcs") == 0) |
| 6740 | Kind = ARM64ABIInfo::DarwinPCS; |
| 6741 | |
| 6742 | return *(TheTargetCodeGenInfo = new ARM64TargetCodeGenInfo(Types, Kind)); |
| 6743 | } |
| 6744 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6745 | case llvm::Triple::aarch64: |
Christian Pirker | 9b019ae | 2014-02-25 13:51:00 +0000 | [diff] [blame] | 6746 | case llvm::Triple::aarch64_be: |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6747 | return *(TheTargetCodeGenInfo = new AArch64TargetCodeGenInfo(Types)); |
| 6748 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6749 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6750 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6751 | case llvm::Triple::thumb: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 6752 | case llvm::Triple::thumbeb: |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6753 | { |
| 6754 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6755 | if (strcmp(getTarget().getABI(), "apcs-gnu") == 0) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6756 | Kind = ARMABIInfo::APCS; |
David Tweed | 8f67653 | 2012-10-25 13:33:01 +0000 | [diff] [blame] | 6757 | else if (CodeGenOpts.FloatABI == "hard" || |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6758 | (CodeGenOpts.FloatABI != "soft" && |
| 6759 | Triple.getEnvironment() == llvm::Triple::GNUEABIHF)) |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6760 | Kind = ARMABIInfo::AAPCS_VFP; |
| 6761 | |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6762 | switch (Triple.getOS()) { |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 6763 | case llvm::Triple::NaCl: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 6764 | return *(TheTargetCodeGenInfo = |
| 6765 | new NaClARMTargetCodeGenInfo(Types, Kind)); |
| 6766 | default: |
| 6767 | return *(TheTargetCodeGenInfo = |
| 6768 | new ARMTargetCodeGenInfo(Types, Kind)); |
| 6769 | } |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 6770 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6771 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6772 | case llvm::Triple::ppc: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6773 | return *(TheTargetCodeGenInfo = new PPC32TargetCodeGenInfo(Types)); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 6774 | case llvm::Triple::ppc64: |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 6775 | if (Triple.isOSBinFormatELF()) |
| 6776 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
| 6777 | else |
| 6778 | return *(TheTargetCodeGenInfo = new PPC64TargetCodeGenInfo(Types)); |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 6779 | case llvm::Triple::ppc64le: |
| 6780 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
| 6781 | return *(TheTargetCodeGenInfo = new PPC64_SVR4_TargetCodeGenInfo(Types)); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 6782 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 6783 | case llvm::Triple::nvptx: |
| 6784 | case llvm::Triple::nvptx64: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6785 | return *(TheTargetCodeGenInfo = new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6786 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6787 | case llvm::Triple::msp430: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6788 | return *(TheTargetCodeGenInfo = new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 6789 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6790 | case llvm::Triple::systemz: |
| 6791 | return *(TheTargetCodeGenInfo = new SystemZTargetCodeGenInfo(Types)); |
| 6792 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 6793 | case llvm::Triple::tce: |
| 6794 | return *(TheTargetCodeGenInfo = new TCETargetCodeGenInfo(Types)); |
| 6795 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6796 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6797 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
| 6798 | bool IsSmallStructInRegABI = |
| 6799 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | 377066a | 2014-03-27 22:50:18 +0000 | [diff] [blame] | 6800 | bool IsWin32FloatStructABI = Triple.isWindowsMSVCEnvironment(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 6801 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6802 | if (Triple.getOS() == llvm::Triple::Win32) { |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 6803 | return *(TheTargetCodeGenInfo = |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6804 | new WinX86_32TargetCodeGenInfo(Types, |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6805 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6806 | IsWin32FloatStructABI, |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 6807 | CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6808 | } else { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6809 | return *(TheTargetCodeGenInfo = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 6810 | new X86_32TargetCodeGenInfo(Types, |
| 6811 | IsDarwinVectorABI, IsSmallStructInRegABI, |
| 6812 | IsWin32FloatStructABI, |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 6813 | CodeGenOpts.NumRegisterParameters)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6814 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 6815 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6816 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6817 | case llvm::Triple::x86_64: { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6818 | bool HasAVX = strcmp(getTarget().getABI(), "avx") == 0; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6819 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6820 | switch (Triple.getOS()) { |
| 6821 | case llvm::Triple::Win32: |
NAKAMURA Takumi | 31ea2f1 | 2011-02-17 08:51:38 +0000 | [diff] [blame] | 6822 | case llvm::Triple::MinGW32: |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6823 | case llvm::Triple::Cygwin: |
| 6824 | return *(TheTargetCodeGenInfo = new WinX86_64TargetCodeGenInfo(Types)); |
Eli Bendersky | d7c9203 | 2012-12-04 18:38:10 +0000 | [diff] [blame] | 6825 | case llvm::Triple::NaCl: |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 6826 | return *(TheTargetCodeGenInfo = new NaClX86_64TargetCodeGenInfo(Types, |
| 6827 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6828 | default: |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6829 | return *(TheTargetCodeGenInfo = new X86_64TargetCodeGenInfo(Types, |
| 6830 | HasAVX)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 6831 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 6832 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 6833 | case llvm::Triple::hexagon: |
| 6834 | return *(TheTargetCodeGenInfo = new HexagonTargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 6835 | case llvm::Triple::sparcv9: |
| 6836 | return *(TheTargetCodeGenInfo = new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 6837 | case llvm::Triple::xcore: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 6838 | return *(TheTargetCodeGenInfo = new XCoreTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 6839 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6840 | } |