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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // These classes wrap the information about a call or function |
| 10 | // definition used to handle ABI compliancy. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 14 | #include "TargetInfo.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 15 | #include "ABIInfo.h" |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 16 | #include "CGBlocks.h" |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 17 | #include "CGCXXABI.h" |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 18 | #include "CGValue.h" |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 20 | #include "clang/AST/Attr.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Richard Trieu | 6368818 | 2018-12-11 03:18:39 +0000 | [diff] [blame] | 22 | #include "clang/Basic/CodeGenOptions.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 23 | #include "clang/CodeGen/CGFunctionInfo.h" |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 24 | #include "clang/CodeGen/SwiftCallingConv.h" |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Triple.h" |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Twine.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/Type.h" |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 32 | #include <algorithm> // std::sort |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 33 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace CodeGen; |
| 36 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 37 | // Helper for coercing an aggregate argument or return value into an integer |
| 38 | // array of the same size (including padding) and alignment. This alternate |
| 39 | // coercion happens only for the RenderScript ABI and can be removed after |
| 40 | // runtimes that rely on it are no longer supported. |
| 41 | // |
| 42 | // RenderScript assumes that the size of the argument / return value in the IR |
| 43 | // is the same as the size of the corresponding qualified type. This helper |
| 44 | // coerces the aggregate type into an array of the same size (including |
| 45 | // padding). This coercion is used in lieu of expansion of struct members or |
| 46 | // other canonical coercions that return a coerced-type of larger size. |
| 47 | // |
| 48 | // Ty - The argument / return value type |
| 49 | // Context - The associated ASTContext |
| 50 | // LLVMContext - The associated LLVMContext |
| 51 | static ABIArgInfo coerceToIntArray(QualType Ty, |
| 52 | ASTContext &Context, |
| 53 | llvm::LLVMContext &LLVMContext) { |
| 54 | // Alignment and Size are measured in bits. |
| 55 | const uint64_t Size = Context.getTypeSize(Ty); |
| 56 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 57 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Alignment); |
| 58 | const uint64_t NumElements = (Size + Alignment - 1) / Alignment; |
| 59 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 60 | } |
| 61 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 62 | static void AssignToArrayRange(CodeGen::CGBuilderTy &Builder, |
| 63 | llvm::Value *Array, |
| 64 | llvm::Value *Value, |
| 65 | unsigned FirstIndex, |
| 66 | unsigned LastIndex) { |
| 67 | // Alternatively, we could emit this as a loop in the source. |
| 68 | for (unsigned I = FirstIndex; I <= LastIndex; ++I) { |
David Blaikie | fb901c7a | 2015-04-04 15:12:29 +0000 | [diff] [blame] | 69 | llvm::Value *Cell = |
| 70 | Builder.CreateConstInBoundsGEP1_32(Builder.getInt8Ty(), Array, I); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 71 | Builder.CreateAlignedStore(Value, Cell, CharUnits::One()); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 75 | static bool isAggregateTypeForABI(QualType T) { |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 76 | return !CodeGenFunction::hasScalarEvaluationKind(T) || |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 77 | T->isMemberFunctionPointerType(); |
| 78 | } |
| 79 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 80 | ABIArgInfo |
| 81 | ABIInfo::getNaturalAlignIndirect(QualType Ty, bool ByRef, bool Realign, |
| 82 | llvm::Type *Padding) const { |
| 83 | return ABIArgInfo::getIndirect(getContext().getTypeAlignInChars(Ty), |
| 84 | ByRef, Realign, Padding); |
| 85 | } |
| 86 | |
| 87 | ABIArgInfo |
| 88 | ABIInfo::getNaturalAlignIndirectInReg(QualType Ty, bool Realign) const { |
| 89 | return ABIArgInfo::getIndirectInReg(getContext().getTypeAlignInChars(Ty), |
| 90 | /*ByRef*/ false, Realign); |
| 91 | } |
| 92 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 93 | Address ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 94 | QualType Ty) const { |
| 95 | return Address::invalid(); |
| 96 | } |
| 97 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 98 | ABIInfo::~ABIInfo() {} |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 99 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 100 | /// Does the given lowering require more than the given number of |
| 101 | /// registers when expanded? |
| 102 | /// |
| 103 | /// This is intended to be the basis of a reasonable basic implementation |
| 104 | /// of should{Pass,Return}IndirectlyForSwift. |
| 105 | /// |
| 106 | /// For most targets, a limit of four total registers is reasonable; this |
| 107 | /// limits the amount of code required in order to move around the value |
| 108 | /// in case it wasn't produced immediately prior to the call by the caller |
| 109 | /// (or wasn't produced in exactly the right registers) or isn't used |
| 110 | /// immediately within the callee. But some targets may need to further |
| 111 | /// limit the register count due to an inability to support that many |
| 112 | /// return registers. |
| 113 | static bool occupiesMoreThan(CodeGenTypes &cgt, |
| 114 | ArrayRef<llvm::Type*> scalarTypes, |
| 115 | unsigned maxAllRegisters) { |
| 116 | unsigned intCount = 0, fpCount = 0; |
| 117 | for (llvm::Type *type : scalarTypes) { |
| 118 | if (type->isPointerTy()) { |
| 119 | intCount++; |
| 120 | } else if (auto intTy = dyn_cast<llvm::IntegerType>(type)) { |
| 121 | auto ptrWidth = cgt.getTarget().getPointerWidth(0); |
| 122 | intCount += (intTy->getBitWidth() + ptrWidth - 1) / ptrWidth; |
| 123 | } else { |
| 124 | assert(type->isVectorTy() || type->isFloatingPointTy()); |
| 125 | fpCount++; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return (intCount + fpCount > maxAllRegisters); |
| 130 | } |
| 131 | |
| 132 | bool SwiftABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 133 | llvm::Type *eltTy, |
| 134 | unsigned numElts) const { |
| 135 | // The default implementation of this assumes that the target guarantees |
| 136 | // 128-bit SIMD support but nothing more. |
| 137 | return (vectorSize.getQuantity() > 8 && vectorSize.getQuantity() <= 16); |
| 138 | } |
| 139 | |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 140 | static CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 141 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 142 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()); |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 143 | if (!RD) { |
| 144 | if (!RT->getDecl()->canPassInRegisters()) |
| 145 | return CGCXXABI::RAA_Indirect; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 146 | return CGCXXABI::RAA_Default; |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 147 | } |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 148 | return CXXABI.getRecordArgABI(RD); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static CGCXXABI::RecordArgABI getRecordArgABI(QualType T, |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 152 | CGCXXABI &CXXABI) { |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 153 | const RecordType *RT = T->getAs<RecordType>(); |
| 154 | if (!RT) |
| 155 | return CGCXXABI::RAA_Default; |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 156 | return getRecordArgABI(RT, CXXABI); |
| 157 | } |
| 158 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 159 | static bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, |
| 160 | const ABIInfo &Info) { |
| 161 | QualType Ty = FI.getReturnType(); |
| 162 | |
| 163 | if (const auto *RT = Ty->getAs<RecordType>()) |
| 164 | if (!isa<CXXRecordDecl>(RT->getDecl()) && |
| 165 | !RT->getDecl()->canPassInRegisters()) { |
| 166 | FI.getReturnInfo() = Info.getNaturalAlignIndirect(Ty); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | return CXXABI.classifyReturnType(FI); |
| 171 | } |
| 172 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 173 | /// Pass transparent unions as if they were the type of the first element. Sema |
| 174 | /// should ensure that all elements of the union have the same "machine type". |
| 175 | static QualType useFirstFieldIfTransparentUnion(QualType Ty) { |
| 176 | if (const RecordType *UT = Ty->getAsUnionType()) { |
| 177 | const RecordDecl *UD = UT->getDecl(); |
| 178 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 179 | assert(!UD->field_empty() && "sema created an empty transparent union"); |
| 180 | return UD->field_begin()->getType(); |
| 181 | } |
| 182 | } |
| 183 | return Ty; |
| 184 | } |
| 185 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 186 | CGCXXABI &ABIInfo::getCXXABI() const { |
| 187 | return CGT.getCXXABI(); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 190 | ASTContext &ABIInfo::getContext() const { |
| 191 | return CGT.getContext(); |
| 192 | } |
| 193 | |
| 194 | llvm::LLVMContext &ABIInfo::getVMContext() const { |
| 195 | return CGT.getLLVMContext(); |
| 196 | } |
| 197 | |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 198 | const llvm::DataLayout &ABIInfo::getDataLayout() const { |
| 199 | return CGT.getDataLayout(); |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 200 | } |
| 201 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 202 | const TargetInfo &ABIInfo::getTarget() const { |
| 203 | return CGT.getTarget(); |
| 204 | } |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 205 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 206 | const CodeGenOptions &ABIInfo::getCodeGenOpts() const { |
| 207 | return CGT.getCodeGenOpts(); |
| 208 | } |
| 209 | |
| 210 | bool ABIInfo::isAndroid() const { return getTarget().getTriple().isAndroid(); } |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 211 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 212 | bool ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | bool ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 217 | uint64_t Members) const { |
| 218 | return false; |
| 219 | } |
| 220 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 221 | LLVM_DUMP_METHOD void ABIArgInfo::dump() const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 222 | raw_ostream &OS = llvm::errs(); |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 223 | OS << "(ABIArgInfo Kind="; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 224 | switch (TheKind) { |
| 225 | case Direct: |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 226 | OS << "Direct Type="; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 227 | if (llvm::Type *Ty = getCoerceToType()) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 228 | Ty->print(OS); |
| 229 | else |
| 230 | OS << "null"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 231 | break; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 232 | case Extend: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 233 | OS << "Extend"; |
Anton Korobeynikov | 18adbf5 | 2009-06-06 09:36:29 +0000 | [diff] [blame] | 234 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 235 | case Ignore: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 236 | OS << "Ignore"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 237 | break; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 238 | case InAlloca: |
| 239 | OS << "InAlloca Offset=" << getInAllocaFieldIndex(); |
| 240 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 241 | case Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 242 | OS << "Indirect Align=" << getIndirectAlign().getQuantity() |
Joerg Sonnenberger | 4921fe2 | 2011-07-15 18:23:44 +0000 | [diff] [blame] | 243 | << " ByVal=" << getIndirectByVal() |
Daniel Dunbar | 7b7c293 | 2010-09-16 20:42:02 +0000 | [diff] [blame] | 244 | << " Realign=" << getIndirectRealign(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 245 | break; |
| 246 | case Expand: |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 247 | OS << "Expand"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 248 | break; |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 249 | case CoerceAndExpand: |
| 250 | OS << "CoerceAndExpand Type="; |
| 251 | getCoerceAndExpandType()->print(OS); |
| 252 | break; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 253 | } |
Daniel Dunbar | 7230fa5 | 2009-12-03 09:13:49 +0000 | [diff] [blame] | 254 | OS << ")\n"; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 257 | // Dynamically round a pointer up to a multiple of the given alignment. |
| 258 | static llvm::Value *emitRoundPointerUpToAlignment(CodeGenFunction &CGF, |
| 259 | llvm::Value *Ptr, |
| 260 | CharUnits Align) { |
| 261 | llvm::Value *PtrAsInt = Ptr; |
| 262 | // OverflowArgArea = (OverflowArgArea + Align - 1) & -Align; |
| 263 | PtrAsInt = CGF.Builder.CreatePtrToInt(PtrAsInt, CGF.IntPtrTy); |
| 264 | PtrAsInt = CGF.Builder.CreateAdd(PtrAsInt, |
| 265 | llvm::ConstantInt::get(CGF.IntPtrTy, Align.getQuantity() - 1)); |
| 266 | PtrAsInt = CGF.Builder.CreateAnd(PtrAsInt, |
| 267 | llvm::ConstantInt::get(CGF.IntPtrTy, -Align.getQuantity())); |
| 268 | PtrAsInt = CGF.Builder.CreateIntToPtr(PtrAsInt, |
| 269 | Ptr->getType(), |
| 270 | Ptr->getName() + ".aligned"); |
| 271 | return PtrAsInt; |
| 272 | } |
| 273 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 274 | /// Emit va_arg for a platform using the common void* representation, |
| 275 | /// where arguments are simply emitted in an array of slots on the stack. |
| 276 | /// |
| 277 | /// This version implements the core direct-value passing rules. |
| 278 | /// |
| 279 | /// \param SlotSize - The size and alignment of a stack slot. |
| 280 | /// Each argument will be allocated to a multiple of this number of |
| 281 | /// slots, and all the slots will be aligned to this value. |
| 282 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 283 | /// an argument type with an alignment greater than the slot size |
| 284 | /// will be emitted on a higher-alignment address, potentially |
| 285 | /// leaving one or more empty slots behind as padding. If this |
| 286 | /// is false, the returned address might be less-aligned than |
| 287 | /// DirectAlign. |
| 288 | static Address emitVoidPtrDirectVAArg(CodeGenFunction &CGF, |
| 289 | Address VAListAddr, |
| 290 | llvm::Type *DirectTy, |
| 291 | CharUnits DirectSize, |
| 292 | CharUnits DirectAlign, |
| 293 | CharUnits SlotSize, |
| 294 | bool AllowHigherAlign) { |
| 295 | // Cast the element type to i8* if necessary. Some platforms define |
| 296 | // va_list as a struct containing an i8* instead of just an i8*. |
| 297 | if (VAListAddr.getElementType() != CGF.Int8PtrTy) |
| 298 | VAListAddr = CGF.Builder.CreateElementBitCast(VAListAddr, CGF.Int8PtrTy); |
| 299 | |
| 300 | llvm::Value *Ptr = CGF.Builder.CreateLoad(VAListAddr, "argp.cur"); |
| 301 | |
| 302 | // If the CC aligns values higher than the slot size, do so if needed. |
| 303 | Address Addr = Address::invalid(); |
| 304 | if (AllowHigherAlign && DirectAlign > SlotSize) { |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 305 | Addr = Address(emitRoundPointerUpToAlignment(CGF, Ptr, DirectAlign), |
| 306 | DirectAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 307 | } else { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 308 | Addr = Address(Ptr, SlotSize); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Advance the pointer past the argument, then store that back. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 312 | CharUnits FullDirectSize = DirectSize.alignTo(SlotSize); |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 313 | Address NextPtr = |
| 314 | CGF.Builder.CreateConstInBoundsByteGEP(Addr, FullDirectSize, "argp.next"); |
| 315 | CGF.Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 316 | |
| 317 | // If the argument is smaller than a slot, and this is a big-endian |
| 318 | // target, the argument will be right-adjusted in its slot. |
Strahinja Petrovic | 515a1eb | 2016-06-24 12:12:41 +0000 | [diff] [blame] | 319 | if (DirectSize < SlotSize && CGF.CGM.getDataLayout().isBigEndian() && |
| 320 | !DirectTy->isStructTy()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 321 | Addr = CGF.Builder.CreateConstInBoundsByteGEP(Addr, SlotSize - DirectSize); |
| 322 | } |
| 323 | |
| 324 | Addr = CGF.Builder.CreateElementBitCast(Addr, DirectTy); |
| 325 | return Addr; |
| 326 | } |
| 327 | |
| 328 | /// Emit va_arg for a platform using the common void* representation, |
| 329 | /// where arguments are simply emitted in an array of slots on the stack. |
| 330 | /// |
| 331 | /// \param IsIndirect - Values of this type are passed indirectly. |
| 332 | /// \param ValueInfo - The size and alignment of this type, generally |
| 333 | /// computed with getContext().getTypeInfoInChars(ValueTy). |
| 334 | /// \param SlotSizeAndAlign - The size and alignment of a stack slot. |
| 335 | /// Each argument will be allocated to a multiple of this number of |
| 336 | /// slots, and all the slots will be aligned to this value. |
| 337 | /// \param AllowHigherAlign - The slot alignment is not a cap; |
| 338 | /// an argument type with an alignment greater than the slot size |
| 339 | /// will be emitted on a higher-alignment address, potentially |
| 340 | /// leaving one or more empty slots behind as padding. |
| 341 | static Address emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 342 | QualType ValueTy, bool IsIndirect, |
| 343 | std::pair<CharUnits, CharUnits> ValueInfo, |
| 344 | CharUnits SlotSizeAndAlign, |
| 345 | bool AllowHigherAlign) { |
| 346 | // The size and alignment of the value that was passed directly. |
| 347 | CharUnits DirectSize, DirectAlign; |
| 348 | if (IsIndirect) { |
| 349 | DirectSize = CGF.getPointerSize(); |
| 350 | DirectAlign = CGF.getPointerAlign(); |
| 351 | } else { |
| 352 | DirectSize = ValueInfo.first; |
| 353 | DirectAlign = ValueInfo.second; |
| 354 | } |
| 355 | |
| 356 | // Cast the address we've calculated to the right type. |
| 357 | llvm::Type *DirectTy = CGF.ConvertTypeForMem(ValueTy); |
| 358 | if (IsIndirect) |
| 359 | DirectTy = DirectTy->getPointerTo(0); |
| 360 | |
| 361 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, DirectTy, |
| 362 | DirectSize, DirectAlign, |
| 363 | SlotSizeAndAlign, |
| 364 | AllowHigherAlign); |
| 365 | |
| 366 | if (IsIndirect) { |
| 367 | Addr = Address(CGF.Builder.CreateLoad(Addr), ValueInfo.second); |
| 368 | } |
| 369 | |
| 370 | return Addr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 371 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static Address emitMergePHI(CodeGenFunction &CGF, |
| 375 | Address Addr1, llvm::BasicBlock *Block1, |
| 376 | Address Addr2, llvm::BasicBlock *Block2, |
| 377 | const llvm::Twine &Name = "") { |
| 378 | assert(Addr1.getType() == Addr2.getType()); |
| 379 | llvm::PHINode *PHI = CGF.Builder.CreatePHI(Addr1.getType(), 2, Name); |
| 380 | PHI->addIncoming(Addr1.getPointer(), Block1); |
| 381 | PHI->addIncoming(Addr2.getPointer(), Block2); |
| 382 | CharUnits Align = std::min(Addr1.getAlignment(), Addr2.getAlignment()); |
| 383 | return Address(PHI, Align); |
| 384 | } |
| 385 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 386 | TargetCodeGenInfo::~TargetCodeGenInfo() { delete Info; } |
| 387 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 388 | // If someone can figure out a general rule for this, that would be great. |
| 389 | // It's probably just doomed to be platform-dependent, though. |
| 390 | unsigned TargetCodeGenInfo::getSizeOfUnwindException() const { |
| 391 | // Verified for: |
| 392 | // x86-64 FreeBSD, Linux, Darwin |
| 393 | // x86-32 FreeBSD, Linux, Darwin |
| 394 | // PowerPC Linux, Darwin |
| 395 | // ARM Darwin (*not* EABI) |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 396 | // AArch64 Linux |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 397 | return 32; |
| 398 | } |
| 399 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 400 | bool TargetCodeGenInfo::isNoProtoCallVariadic(const CallArgList &args, |
| 401 | const FunctionNoProtoType *fnType) const { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 402 | // The following conventions are known to require this to be false: |
| 403 | // x86_stdcall |
| 404 | // MIPS |
| 405 | // For everything else, we just prefer false unless we opt out. |
| 406 | return false; |
| 407 | } |
| 408 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 409 | void |
| 410 | TargetCodeGenInfo::getDependentLibraryOption(llvm::StringRef Lib, |
| 411 | llvm::SmallString<24> &Opt) const { |
| 412 | // This assumes the user is passing a library name like "rt" instead of a |
| 413 | // filename like "librt.a/so", and that they don't care whether it's static or |
| 414 | // dynamic. |
| 415 | Opt = "-l"; |
| 416 | Opt += Lib; |
| 417 | } |
| 418 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 419 | unsigned TargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 420 | // OpenCL kernels are called via an explicit runtime API with arguments |
| 421 | // set with clSetKernelArg(), not as normal sub-functions. |
| 422 | // Return SPIR_KERNEL by default as the kernel calling convention to |
| 423 | // ensure the fingerprint is fixed such way that each OpenCL argument |
| 424 | // gets one matching argument in the produced kernel function argument |
| 425 | // list to enable feasible implementation of clSetKernelArg() with |
| 426 | // aggregates etc. In case we would use the default C calling conv here, |
| 427 | // clSetKernelArg() might break depending on the target-specific |
| 428 | // conventions; different targets might split structs passed as values |
| 429 | // to multiple function arguments etc. |
| 430 | return llvm::CallingConv::SPIR_KERNEL; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 431 | } |
Yaxun Liu | 37ceede | 2016-07-20 19:21:11 +0000 | [diff] [blame] | 432 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 433 | llvm::Constant *TargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 434 | llvm::PointerType *T, QualType QT) const { |
| 435 | return llvm::ConstantPointerNull::get(T); |
| 436 | } |
| 437 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 438 | LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 439 | const VarDecl *D) const { |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 440 | assert(!CGM.getLangOpts().OpenCL && |
| 441 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 442 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 443 | return D ? D->getType().getAddressSpace() : LangAS::Default; |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 446 | llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 447 | CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, |
| 448 | LangAS DestAddr, llvm::Type *DestTy, bool isNonNull) const { |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 449 | // Since target may map different address spaces in AST to the same address |
| 450 | // space, an address space conversion may end up as a bitcast. |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 451 | if (auto *C = dyn_cast<llvm::Constant>(Src)) |
| 452 | return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestAddr, DestTy); |
Vyacheslav Zakharin | de811d1 | 2019-07-10 17:10:05 +0000 | [diff] [blame] | 453 | // Try to preserve the source's name to make IR more readable. |
| 454 | return CGF.Builder.CreatePointerBitCastOrAddrSpaceCast( |
| 455 | Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : ""); |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 458 | llvm::Constant * |
| 459 | TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 460 | LangAS SrcAddr, LangAS DestAddr, |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 461 | llvm::Type *DestTy) const { |
| 462 | // Since target may map different address spaces in AST to the same address |
| 463 | // space, an address space conversion may end up as a bitcast. |
| 464 | return llvm::ConstantExpr::getPointerCast(Src, DestTy); |
| 465 | } |
| 466 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 467 | llvm::SyncScope::ID |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 468 | TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 469 | SyncScope Scope, |
| 470 | llvm::AtomicOrdering Ordering, |
| 471 | llvm::LLVMContext &Ctx) const { |
| 472 | return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */ |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 475 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 476 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 477 | /// isEmptyField - Return true iff a the field is "empty", that is it |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 478 | /// is an unnamed bit-field or an (array of) empty record(s). |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 479 | static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, |
| 480 | bool AllowArrays) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 481 | if (FD->isUnnamedBitfield()) |
| 482 | return true; |
| 483 | |
| 484 | QualType FT = FD->getType(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 485 | |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 486 | // Constant arrays of empty records count as empty, strip them off. |
| 487 | // Constant arrays of zero length always count as empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 488 | if (AllowArrays) |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 489 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 490 | if (AT->getSize() == 0) |
| 491 | return true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 492 | FT = AT->getElementType(); |
Eli Friedman | 0b3f201 | 2011-11-18 03:47:20 +0000 | [diff] [blame] | 493 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 494 | |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 495 | const RecordType *RT = FT->getAs<RecordType>(); |
| 496 | if (!RT) |
| 497 | return false; |
| 498 | |
| 499 | // C++ record fields are never empty, at least in the Itanium ABI. |
| 500 | // |
| 501 | // FIXME: We should use a predicate for whether this behavior is true in the |
| 502 | // current ABI. |
| 503 | if (isa<CXXRecordDecl>(RT->getDecl())) |
| 504 | return false; |
| 505 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 506 | return isEmptyRecord(Context, FT, AllowArrays); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 509 | /// isEmptyRecord - Return true iff a structure contains only empty |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 510 | /// fields. Note that a structure with a flexible array member is not |
| 511 | /// considered empty. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 512 | static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 513 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 514 | if (!RT) |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 515 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 516 | const RecordDecl *RD = RT->getDecl(); |
| 517 | if (RD->hasFlexibleArrayMember()) |
| 518 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 519 | |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 520 | // If this is a C++ record, check the bases first. |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 521 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 522 | for (const auto &I : CXXRD->bases()) |
| 523 | if (!isEmptyRecord(Context, I.getType(), true)) |
Argyrios Kyrtzidis | d42411f | 2011-05-17 02:17:52 +0000 | [diff] [blame] | 524 | return false; |
Daniel Dunbar | cd20ce1 | 2010-05-17 16:46:00 +0000 | [diff] [blame] | 525 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 526 | for (const auto *I : RD->fields()) |
| 527 | if (!isEmptyField(Context, I, AllowArrays)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 528 | return false; |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | /// isSingleElementStruct - Determine if a structure is a "single |
| 533 | /// element struct", i.e. it has exactly one non-empty field or |
| 534 | /// exactly one field which is itself a single element |
| 535 | /// struct. Structures with flexible array members are never |
| 536 | /// considered single element structs. |
| 537 | /// |
| 538 | /// \return The field declaration for the single non-empty field, if |
| 539 | /// it exists. |
| 540 | static const Type *isSingleElementStruct(QualType T, ASTContext &Context) { |
Benjamin Kramer | 83b1bf3 | 2015-03-02 16:09:24 +0000 | [diff] [blame] | 541 | const RecordType *RT = T->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 542 | if (!RT) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 543 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 544 | |
| 545 | const RecordDecl *RD = RT->getDecl(); |
| 546 | if (RD->hasFlexibleArrayMember()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 547 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 548 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 549 | const Type *Found = nullptr; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 550 | |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 551 | // If this is a C++ record, check the bases first. |
| 552 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 553 | for (const auto &I : CXXRD->bases()) { |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 554 | // Ignore empty records. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 555 | if (isEmptyRecord(Context, I.getType(), true)) |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 556 | continue; |
| 557 | |
| 558 | // If we already found an element then this isn't a single-element struct. |
| 559 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 560 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 561 | |
| 562 | // If this is non-empty and not a single element struct, the composite |
| 563 | // cannot be a single element struct. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 564 | Found = isSingleElementStruct(I.getType(), Context); |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 565 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 566 | return nullptr; |
Daniel Dunbar | 12ebb47 | 2010-05-11 21:15:36 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
| 570 | // Check for single element. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 571 | for (const auto *FD : RD->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 572 | QualType FT = FD->getType(); |
| 573 | |
| 574 | // Ignore empty fields. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 575 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 576 | continue; |
| 577 | |
| 578 | // If we already found an element then this isn't a single-element |
| 579 | // struct. |
| 580 | if (Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 581 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 582 | |
| 583 | // Treat single element arrays as the element. |
| 584 | while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { |
| 585 | if (AT->getSize().getZExtValue() != 1) |
| 586 | break; |
| 587 | FT = AT->getElementType(); |
| 588 | } |
| 589 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 590 | if (!isAggregateTypeForABI(FT)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 591 | Found = FT.getTypePtr(); |
| 592 | } else { |
| 593 | Found = isSingleElementStruct(FT, Context); |
| 594 | if (!Found) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 595 | return nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 599 | // We don't consider a struct a single-element struct if it has |
| 600 | // padding beyond the element type. |
| 601 | if (Found && Context.getTypeSize(Found) != Context.getTypeSize(T)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 602 | return nullptr; |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 603 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 604 | return Found; |
| 605 | } |
| 606 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 607 | namespace { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 608 | Address EmitVAArgInstr(CodeGenFunction &CGF, Address VAListAddr, QualType Ty, |
| 609 | const ABIArgInfo &AI) { |
| 610 | // This default implementation defers to the llvm backend's va_arg |
| 611 | // instruction. It can handle only passing arguments directly |
| 612 | // (typically only handled in the backend for primitive types), or |
| 613 | // aggregates passed indirectly by pointer (NOTE: if the "byval" |
| 614 | // flag has ABI impact in the callee, this implementation cannot |
| 615 | // work.) |
| 616 | |
| 617 | // Only a few cases are covered here at the moment -- those needed |
| 618 | // by the default abi. |
| 619 | llvm::Value *Val; |
| 620 | |
| 621 | if (AI.isIndirect()) { |
| 622 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 623 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 624 | assert( |
| 625 | !AI.getIndirectRealign() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 626 | "Unexpected IndirectRealign seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 627 | |
| 628 | auto TyInfo = CGF.getContext().getTypeInfoInChars(Ty); |
| 629 | CharUnits TyAlignForABI = TyInfo.second; |
| 630 | |
| 631 | llvm::Type *BaseTy = |
| 632 | llvm::PointerType::getUnqual(CGF.ConvertTypeForMem(Ty)); |
| 633 | llvm::Value *Addr = |
| 634 | CGF.Builder.CreateVAArg(VAListAddr.getPointer(), BaseTy); |
| 635 | return Address(Addr, TyAlignForABI); |
| 636 | } else { |
| 637 | assert((AI.isDirect() || AI.isExtend()) && |
| 638 | "Unexpected ArgInfo Kind in generic VAArg emitter!"); |
| 639 | |
| 640 | assert(!AI.getInReg() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 641 | "Unexpected InReg seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 642 | assert(!AI.getPaddingType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 643 | "Unexpected PaddingType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 644 | assert(!AI.getDirectOffset() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 645 | "Unexpected DirectOffset seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 646 | assert(!AI.getCoerceToType() && |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 647 | "Unexpected CoerceToType seen in arginfo in generic VAArg emitter!"); |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 648 | |
| 649 | Address Temp = CGF.CreateMemTemp(Ty, "varet"); |
| 650 | Val = CGF.Builder.CreateVAArg(VAListAddr.getPointer(), CGF.ConvertType(Ty)); |
| 651 | CGF.Builder.CreateStore(Val, Temp); |
| 652 | return Temp; |
| 653 | } |
| 654 | } |
| 655 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 656 | /// DefaultABIInfo - The default implementation for ABI specific |
| 657 | /// details. This implementation provides information which results in |
| 658 | /// self-consistent and sensible LLVM IR generation, but does not |
| 659 | /// conform to any particular ABI. |
| 660 | class DefaultABIInfo : public ABIInfo { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 661 | public: |
| 662 | DefaultABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 663 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 664 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 665 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 666 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 667 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 668 | if (!getCXXABI().classifyReturnType(FI)) |
| 669 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 670 | for (auto &I : FI.arguments()) |
| 671 | I.info = classifyArgumentType(I.type); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 672 | } |
| 673 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 674 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 675 | QualType Ty) const override { |
| 676 | return EmitVAArgInstr(CGF, VAListAddr, Ty, classifyArgumentType(Ty)); |
| 677 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 678 | }; |
| 679 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 680 | class DefaultTargetCodeGenInfo : public TargetCodeGenInfo { |
| 681 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 682 | DefaultTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 683 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 684 | }; |
| 685 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 686 | ABIArgInfo DefaultABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 687 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 688 | |
| 689 | if (isAggregateTypeForABI(Ty)) { |
| 690 | // Records with non-trivial destructors/copy-constructors should not be |
| 691 | // passed by value. |
| 692 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 693 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 694 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 695 | return getNaturalAlignIndirect(Ty); |
Reid Kleckner | ac38506 | 2015-05-18 22:46:30 +0000 | [diff] [blame] | 696 | } |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 697 | |
Chris Lattner | 9723d6c | 2010-03-11 18:19:55 +0000 | [diff] [blame] | 698 | // Treat an enum type as its underlying type. |
| 699 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 700 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 701 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 702 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 703 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 706 | ABIArgInfo DefaultABIInfo::classifyReturnType(QualType RetTy) const { |
| 707 | if (RetTy->isVoidType()) |
| 708 | return ABIArgInfo::getIgnore(); |
| 709 | |
| 710 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 711 | return getNaturalAlignIndirect(RetTy); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 712 | |
| 713 | // Treat an enum type as its underlying type. |
| 714 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 715 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 716 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 717 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 718 | : ABIArgInfo::getDirect()); |
Bob Wilson | bd4520b | 2011-01-10 23:54:17 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 721 | //===----------------------------------------------------------------------===// |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 722 | // WebAssembly ABI Implementation |
| 723 | // |
| 724 | // This is a very simple ABI that relies a lot on DefaultABIInfo. |
| 725 | //===----------------------------------------------------------------------===// |
| 726 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 727 | class WebAssemblyABIInfo final : public SwiftABIInfo { |
| 728 | DefaultABIInfo defaultInfo; |
| 729 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 730 | public: |
| 731 | explicit WebAssemblyABIInfo(CodeGen::CodeGenTypes &CGT) |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 732 | : SwiftABIInfo(CGT), defaultInfo(CGT) {} |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 733 | |
| 734 | private: |
| 735 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 736 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 737 | |
| 738 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
Richard Smith | 81ef0e1 | 2016-05-14 01:21:40 +0000 | [diff] [blame] | 739 | // non-virtual, but computeInfo and EmitVAArg are virtual, so we |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 740 | // overload them. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 741 | void computeInfo(CGFunctionInfo &FI) const override { |
| 742 | if (!getCXXABI().classifyReturnType(FI)) |
| 743 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 744 | for (auto &Arg : FI.arguments()) |
| 745 | Arg.info = classifyArgumentType(Arg.type); |
| 746 | } |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 747 | |
| 748 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 749 | QualType Ty) const override; |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 750 | |
| 751 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 752 | bool asReturnValue) const override { |
| 753 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 754 | } |
| 755 | |
| 756 | bool isSwiftErrorInRegister() const override { |
| 757 | return false; |
| 758 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 759 | }; |
| 760 | |
| 761 | class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo { |
| 762 | public: |
| 763 | explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 764 | : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {} |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 765 | |
| 766 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 767 | CodeGen::CodeGenModule &CGM) const override { |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 768 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 769 | if (const auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 770 | if (const auto *Attr = FD->getAttr<WebAssemblyImportModuleAttr>()) { |
| 771 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 772 | llvm::AttrBuilder B; |
Dan Gohman | cae8459 | 2019-02-01 22:25:23 +0000 | [diff] [blame] | 773 | B.addAttribute("wasm-import-module", Attr->getImportModule()); |
| 774 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 775 | } |
| 776 | if (const auto *Attr = FD->getAttr<WebAssemblyImportNameAttr>()) { |
| 777 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 778 | llvm::AttrBuilder B; |
| 779 | B.addAttribute("wasm-import-name", Attr->getImportName()); |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 780 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 781 | } |
Sam Clegg | 881d877 | 2019-11-05 10:15:56 -0800 | [diff] [blame] | 782 | if (const auto *Attr = FD->getAttr<WebAssemblyExportNameAttr>()) { |
| 783 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 784 | llvm::AttrBuilder B; |
| 785 | B.addAttribute("wasm-export-name", Attr->getExportName()); |
| 786 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
| 787 | } |
Dan Gohman | b432369 | 2019-01-24 21:08:30 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Sam Clegg | 6fd7d68 | 2018-06-25 18:47:32 +0000 | [diff] [blame] | 790 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 791 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 792 | if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype()) |
| 793 | Fn->addFnAttr("no-prototype"); |
| 794 | } |
| 795 | } |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 796 | }; |
| 797 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 798 | /// Classify argument of given type \p Ty. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 799 | ABIArgInfo WebAssemblyABIInfo::classifyArgumentType(QualType Ty) const { |
| 800 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 801 | |
| 802 | if (isAggregateTypeForABI(Ty)) { |
| 803 | // Records with non-trivial destructors/copy-constructors should not be |
| 804 | // passed by value. |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 805 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 806 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 807 | // Ignore empty structs/unions. |
| 808 | if (isEmptyRecord(getContext(), Ty, true)) |
| 809 | return ABIArgInfo::getIgnore(); |
| 810 | // Lower single-element structs to just pass a regular value. TODO: We |
| 811 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 812 | // though watch out for things like bitfields. |
| 813 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 814 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 818 | return defaultInfo.classifyArgumentType(Ty); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | ABIArgInfo WebAssemblyABIInfo::classifyReturnType(QualType RetTy) const { |
| 822 | if (isAggregateTypeForABI(RetTy)) { |
| 823 | // Records with non-trivial destructors/copy-constructors should not be |
| 824 | // returned by value. |
| 825 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 826 | // Ignore empty structs/unions. |
| 827 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 828 | return ABIArgInfo::getIgnore(); |
| 829 | // Lower single-element structs to just return a regular value. TODO: We |
| 830 | // could do reasonable-size multiple-element structs too, using |
| 831 | // ABIArgInfo::getDirect(). |
| 832 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 833 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // Otherwise just do the default thing. |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 838 | return defaultInfo.classifyReturnType(RetTy); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 841 | Address WebAssemblyABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 842 | QualType Ty) const { |
Guanzhong Chen | 82bfd1d | 2019-08-15 19:33:36 +0000 | [diff] [blame] | 843 | bool IsIndirect = isAggregateTypeForABI(Ty) && |
| 844 | !isEmptyRecord(getContext(), Ty, true) && |
| 845 | !isSingleElementStruct(Ty, getContext()); |
Guanzhong Chen | 8a503e4 | 2019-08-13 21:41:11 +0000 | [diff] [blame] | 846 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 847 | getContext().getTypeInfoInChars(Ty), |
| 848 | CharUnits::fromQuantity(4), |
Guanzhong Chen | 8a503e4 | 2019-08-13 21:41:11 +0000 | [diff] [blame] | 849 | /*AllowHigherAlign=*/true); |
Dan Gohman | 1fcd10c | 2016-02-22 19:17:40 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 852 | //===----------------------------------------------------------------------===// |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 853 | // le32/PNaCl bitcode ABI Implementation |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 854 | // |
| 855 | // This is a simplified version of the x86_32 ABI. Arguments and return values |
| 856 | // are always passed on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 857 | //===----------------------------------------------------------------------===// |
| 858 | |
| 859 | class PNaClABIInfo : public ABIInfo { |
| 860 | public: |
| 861 | PNaClABIInfo(CodeGen::CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 862 | |
| 863 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 864 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 865 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 866 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 867 | Address EmitVAArg(CodeGenFunction &CGF, |
| 868 | Address VAListAddr, QualType Ty) const override; |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 869 | }; |
| 870 | |
| 871 | class PNaClTargetCodeGenInfo : public TargetCodeGenInfo { |
| 872 | public: |
| 873 | PNaClTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 874 | : TargetCodeGenInfo(new PNaClABIInfo(CGT)) {} |
| 875 | }; |
| 876 | |
| 877 | void PNaClABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 878 | if (!getCXXABI().classifyReturnType(FI)) |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 879 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 880 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 881 | for (auto &I : FI.arguments()) |
| 882 | I.info = classifyArgumentType(I.type); |
| 883 | } |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 884 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 885 | Address PNaClABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 886 | QualType Ty) const { |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 887 | // The PNaCL ABI is a bit odd, in that varargs don't use normal |
| 888 | // function classification. Structs get passed directly for varargs |
| 889 | // functions, through a rewriting transform in |
| 890 | // pnacl-llvm/lib/Transforms/NaCl/ExpandVarArgs.cpp, which allows |
| 891 | // this target to actually support a va_arg instructions with an |
| 892 | // aggregate type, unlike other targets. |
| 893 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 896 | /// Classify argument of given type \p Ty. |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 897 | ABIArgInfo PNaClABIInfo::classifyArgumentType(QualType Ty) const { |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 898 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 899 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 900 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 901 | return getNaturalAlignIndirect(Ty); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 902 | } else if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
| 903 | // Treat an enum type as its underlying type. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 904 | Ty = EnumTy->getDecl()->getIntegerType(); |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 905 | } else if (Ty->isFloatingType()) { |
| 906 | // Floating-point types don't go inreg. |
| 907 | return ABIArgInfo::getDirect(); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 908 | } |
Eli Bendersky | 4f6791c | 2013-04-08 21:31:01 +0000 | [diff] [blame] | 909 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 910 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 911 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | ABIArgInfo PNaClABIInfo::classifyReturnType(QualType RetTy) const { |
| 915 | if (RetTy->isVoidType()) |
| 916 | return ABIArgInfo::getIgnore(); |
| 917 | |
Eli Bendersky | e20dad6 | 2013-04-04 22:49:35 +0000 | [diff] [blame] | 918 | // In the PNaCl ABI we always return records/structures on the stack. |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 919 | if (isAggregateTypeForABI(RetTy)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 920 | return getNaturalAlignIndirect(RetTy); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 921 | |
| 922 | // Treat an enum type as its underlying type. |
| 923 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 924 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 925 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 926 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 927 | : ABIArgInfo::getDirect()); |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 930 | /// IsX86_MMXType - Return true if this is an MMX type. |
| 931 | bool IsX86_MMXType(llvm::Type *IRType) { |
| 932 | // Return true if the type is an MMX type <2 x i32>, <4 x i16>, or <8 x i8>. |
| 933 | return IRType->isVectorTy() && IRType->getPrimitiveSizeInBits() == 64 && |
| 934 | cast<llvm::VectorType>(IRType)->getElementType()->isIntegerTy() && |
| 935 | IRType->getScalarSizeInBits() != 64; |
| 936 | } |
| 937 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 938 | static llvm::Type* X86AdjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 939 | StringRef Constraint, |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 940 | llvm::Type* Ty) { |
Coby Tayree | 7b49dc9 | 2017-08-24 09:07:34 +0000 | [diff] [blame] | 941 | bool IsMMXCons = llvm::StringSwitch<bool>(Constraint) |
| 942 | .Cases("y", "&y", "^Ym", true) |
| 943 | .Default(false); |
| 944 | if (IsMMXCons && Ty->isVectorTy()) { |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 945 | if (cast<llvm::VectorType>(Ty)->getBitWidth() != 64) { |
| 946 | // Invalid MMX constraint |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 947 | return nullptr; |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 950 | return llvm::Type::getX86_MMXTy(CGF.getLLVMContext()); |
Tim Northover | 0ae9391 | 2013-06-07 00:04:50 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | // No operation needed |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 954 | return Ty; |
| 955 | } |
| 956 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 957 | /// Returns true if this type can be passed in SSE registers with the |
| 958 | /// X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 959 | static bool isX86VectorTypeForVectorCall(ASTContext &Context, QualType Ty) { |
| 960 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 961 | if (BT->isFloatingPoint() && BT->getKind() != BuiltinType::Half) { |
| 962 | if (BT->getKind() == BuiltinType::LongDouble) { |
| 963 | if (&Context.getTargetInfo().getLongDoubleFormat() == |
| 964 | &llvm::APFloat::x87DoubleExtended()) |
| 965 | return false; |
| 966 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 967 | return true; |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 968 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 969 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 970 | // vectorcall can pass XMM, YMM, and ZMM vectors. We don't pass SSE1 MMX |
| 971 | // registers specially. |
| 972 | unsigned VecSize = Context.getTypeSize(VT); |
| 973 | if (VecSize == 128 || VecSize == 256 || VecSize == 512) |
| 974 | return true; |
| 975 | } |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | /// Returns true if this aggregate is small enough to be passed in SSE registers |
| 980 | /// in the X86_VectorCall calling convention. Shared between x86_32 and x86_64. |
| 981 | static bool isX86VectorCallAggregateSmallEnough(uint64_t NumMembers) { |
| 982 | return NumMembers <= 4; |
| 983 | } |
| 984 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 985 | /// Returns a Homogeneous Vector Aggregate ABIArgInfo, used in X86. |
| 986 | static ABIArgInfo getDirectX86Hva(llvm::Type* T = nullptr) { |
| 987 | auto AI = ABIArgInfo::getDirect(T); |
| 988 | AI.setInReg(true); |
| 989 | AI.setCanBeFlattened(false); |
| 990 | return AI; |
| 991 | } |
| 992 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 993 | //===----------------------------------------------------------------------===// |
| 994 | // X86-32 ABI Implementation |
| 995 | //===----------------------------------------------------------------------===// |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 996 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 997 | /// Similar to llvm::CCState, but for Clang. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 998 | struct CCState { |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 999 | CCState(unsigned CC) : CC(CC), FreeRegs(0), FreeSSERegs(0) {} |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1000 | |
| 1001 | unsigned CC; |
| 1002 | unsigned FreeRegs; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1003 | unsigned FreeSSERegs; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1004 | }; |
| 1005 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1006 | enum { |
| 1007 | // Vectorcall only allows the first 6 parameters to be passed in registers. |
| 1008 | VectorcallMaxParamNumAsReg = 6 |
| 1009 | }; |
| 1010 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1011 | /// X86_32ABIInfo - The X86-32 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1012 | class X86_32ABIInfo : public SwiftABIInfo { |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1013 | enum Class { |
| 1014 | Integer, |
| 1015 | Float |
| 1016 | }; |
| 1017 | |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1018 | static const unsigned MinABIStackAlignInBytes = 4; |
| 1019 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1020 | bool IsDarwinVectorABI; |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1021 | bool IsRetSmallStructInRegABI; |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1022 | bool IsWin32StructABI; |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1023 | bool IsSoftFloatABI; |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1024 | bool IsMCUABI; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1025 | unsigned DefaultNumRegisterParameters; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1026 | |
| 1027 | static bool isRegisterSize(unsigned Size) { |
| 1028 | return (Size == 8 || Size == 16 || Size == 32 || Size == 64); |
| 1029 | } |
| 1030 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1031 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 1032 | // FIXME: Assumes vectorcall is in use. |
| 1033 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 1034 | } |
| 1035 | |
| 1036 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 1037 | uint64_t NumMembers) const override { |
| 1038 | // FIXME: Assumes vectorcall is in use. |
| 1039 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 1040 | } |
| 1041 | |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1042 | bool shouldReturnTypeInRegister(QualType Ty, ASTContext &Context) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1043 | |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1044 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
| 1045 | /// such that the argument will be passed in memory. |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1046 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
| 1047 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1048 | ABIArgInfo getIndirectReturnResult(QualType Ty, CCState &State) const; |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1049 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1050 | /// Return the alignment to use for the given type on the stack. |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1051 | unsigned getTypeStackAlignInBytes(QualType Ty, unsigned Align) const; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1052 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1053 | Class classify(QualType Ty) const; |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1054 | ABIArgInfo classifyReturnType(QualType RetTy, CCState &State) const; |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1055 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1056 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1057 | /// Updates the number of available free registers, returns |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1058 | /// true if any registers were allocated. |
| 1059 | bool updateFreeRegs(QualType Ty, CCState &State) const; |
| 1060 | |
| 1061 | bool shouldAggregateUseDirect(QualType Ty, CCState &State, bool &InReg, |
| 1062 | bool &NeedsPadding) const; |
| 1063 | bool shouldPrimitiveUseInReg(QualType Ty, CCState &State) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1064 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1065 | bool canExpandIndirectArgument(QualType Ty) const; |
| 1066 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1067 | /// Rewrite the function info so that all memory arguments use |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1068 | /// inalloca. |
| 1069 | void rewriteWithInAlloca(CGFunctionInfo &FI) const; |
| 1070 | |
| 1071 | void addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1072 | CharUnits &StackOffset, ABIArgInfo &Info, |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1073 | QualType Type) const; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1074 | void computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1075 | bool &UsedInAlloca) const; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1076 | |
Rafael Espindola | 75419dc | 2012-07-23 23:30:29 +0000 | [diff] [blame] | 1077 | public: |
| 1078 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1079 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1080 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 1081 | QualType Ty) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1082 | |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1083 | X86_32ABIInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1084 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1085 | unsigned NumRegisterParameters, bool SoftFloatABI) |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1086 | : SwiftABIInfo(CGT), IsDarwinVectorABI(DarwinVectorABI), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1087 | IsRetSmallStructInRegABI(RetSmallStructInRegABI), |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1088 | IsWin32StructABI(Win32StructABI), |
Manuel Klimek | ab2e28e | 2015-10-19 08:43:46 +0000 | [diff] [blame] | 1089 | IsSoftFloatABI(SoftFloatABI), |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1090 | IsMCUABI(CGT.getTarget().getTriple().isOSIAMCU()), |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1091 | DefaultNumRegisterParameters(NumRegisterParameters) {} |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1092 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 1093 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 1094 | bool asReturnValue) const override { |
| 1095 | // LLVM's x86-32 lowering currently only assigns up to three |
| 1096 | // integer registers and three fp registers. Oddly, it'll use up to |
| 1097 | // four vector registers for vectors, but those can overlap with the |
| 1098 | // scalar registers. |
| 1099 | return occupiesMoreThan(CGT, scalars, /*total*/ 3); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1100 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 1101 | |
| 1102 | bool isSwiftErrorInRegister() const override { |
| 1103 | // x86-32 lowering does not support passing swifterror in a register. |
| 1104 | return false; |
| 1105 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1106 | }; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1107 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1108 | class X86_32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 1109 | public: |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1110 | X86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, bool DarwinVectorABI, |
| 1111 | bool RetSmallStructInRegABI, bool Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1112 | unsigned NumRegisterParameters, bool SoftFloatABI) |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1113 | : TargetCodeGenInfo(new X86_32ABIInfo( |
| 1114 | CGT, DarwinVectorABI, RetSmallStructInRegABI, Win32StructABI, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1115 | NumRegisterParameters, SoftFloatABI)) {} |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1116 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 1117 | static bool isStructReturnInRegABI( |
| 1118 | const llvm::Triple &Triple, const CodeGenOptions &Opts); |
| 1119 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 1120 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1121 | CodeGen::CodeGenModule &CGM) const override; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1122 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1123 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1124 | // Darwin uses different dwarf register numbers for EH. |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 1125 | if (CGM.getTarget().getTriple().isOSDarwin()) return 5; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 1126 | return 4; |
| 1127 | } |
| 1128 | |
| 1129 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1130 | llvm::Value *Address) const override; |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1131 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 1132 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1133 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1134 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 1135 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 1136 | } |
| 1137 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1138 | void addReturnRegisterOutputs(CodeGenFunction &CGF, LValue ReturnValue, |
| 1139 | std::string &Constraints, |
| 1140 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1141 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1142 | std::vector<LValue> &ResultRegDests, |
| 1143 | std::string &AsmString, |
| 1144 | unsigned NumOutputs) const override; |
| 1145 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1146 | llvm::Constant * |
| 1147 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1148 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 1149 | (0x06 << 8) | // .+0x08 |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 1150 | ('v' << 16) | |
| 1151 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 1152 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 1153 | } |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1154 | |
| 1155 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
| 1156 | return "movl\t%ebp, %ebp" |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 1157 | "\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 0139178 | 2016-02-05 21:37:38 +0000 | [diff] [blame] | 1158 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1159 | }; |
| 1160 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1161 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1162 | |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1163 | /// Rewrite input constraint references after adding some output constraints. |
| 1164 | /// In the case where there is one output and one input and we add one output, |
| 1165 | /// we need to replace all operand references greater than or equal to 1: |
| 1166 | /// mov $0, $1 |
| 1167 | /// mov eax, $1 |
| 1168 | /// The result will be: |
| 1169 | /// mov $0, $2 |
| 1170 | /// mov eax, $2 |
| 1171 | static void rewriteInputConstraintReferences(unsigned FirstIn, |
| 1172 | unsigned NumNewOuts, |
| 1173 | std::string &AsmString) { |
| 1174 | std::string Buf; |
| 1175 | llvm::raw_string_ostream OS(Buf); |
| 1176 | size_t Pos = 0; |
| 1177 | while (Pos < AsmString.size()) { |
| 1178 | size_t DollarStart = AsmString.find('$', Pos); |
| 1179 | if (DollarStart == std::string::npos) |
| 1180 | DollarStart = AsmString.size(); |
| 1181 | size_t DollarEnd = AsmString.find_first_not_of('$', DollarStart); |
| 1182 | if (DollarEnd == std::string::npos) |
| 1183 | DollarEnd = AsmString.size(); |
| 1184 | OS << StringRef(&AsmString[Pos], DollarEnd - Pos); |
| 1185 | Pos = DollarEnd; |
| 1186 | size_t NumDollars = DollarEnd - DollarStart; |
| 1187 | if (NumDollars % 2 != 0 && Pos < AsmString.size()) { |
| 1188 | // We have an operand reference. |
| 1189 | size_t DigitStart = Pos; |
| 1190 | size_t DigitEnd = AsmString.find_first_not_of("0123456789", DigitStart); |
| 1191 | if (DigitEnd == std::string::npos) |
| 1192 | DigitEnd = AsmString.size(); |
| 1193 | StringRef OperandStr(&AsmString[DigitStart], DigitEnd - DigitStart); |
| 1194 | unsigned OperandIndex; |
| 1195 | if (!OperandStr.getAsInteger(10, OperandIndex)) { |
| 1196 | if (OperandIndex >= FirstIn) |
| 1197 | OperandIndex += NumNewOuts; |
| 1198 | OS << OperandIndex; |
| 1199 | } else { |
| 1200 | OS << OperandStr; |
| 1201 | } |
| 1202 | Pos = DigitEnd; |
| 1203 | } |
| 1204 | } |
| 1205 | AsmString = std::move(OS.str()); |
| 1206 | } |
| 1207 | |
| 1208 | /// Add output constraints for EAX:EDX because they are return registers. |
| 1209 | void X86_32TargetCodeGenInfo::addReturnRegisterOutputs( |
| 1210 | CodeGenFunction &CGF, LValue ReturnSlot, std::string &Constraints, |
| 1211 | std::vector<llvm::Type *> &ResultRegTypes, |
| 1212 | std::vector<llvm::Type *> &ResultTruncRegTypes, |
| 1213 | std::vector<LValue> &ResultRegDests, std::string &AsmString, |
| 1214 | unsigned NumOutputs) const { |
| 1215 | uint64_t RetWidth = CGF.getContext().getTypeSize(ReturnSlot.getType()); |
| 1216 | |
| 1217 | // Use the EAX constraint if the width is 32 or smaller and EAX:EDX if it is |
| 1218 | // larger. |
| 1219 | if (!Constraints.empty()) |
| 1220 | Constraints += ','; |
| 1221 | if (RetWidth <= 32) { |
| 1222 | Constraints += "={eax}"; |
| 1223 | ResultRegTypes.push_back(CGF.Int32Ty); |
| 1224 | } else { |
| 1225 | // Use the 'A' constraint for EAX:EDX. |
| 1226 | Constraints += "=A"; |
| 1227 | ResultRegTypes.push_back(CGF.Int64Ty); |
| 1228 | } |
| 1229 | |
| 1230 | // Truncate EAX or EAX:EDX to an integer of the appropriate size. |
| 1231 | llvm::Type *CoerceTy = llvm::IntegerType::get(CGF.getLLVMContext(), RetWidth); |
| 1232 | ResultTruncRegTypes.push_back(CoerceTy); |
| 1233 | |
| 1234 | // Coerce the integer by bitcasting the return slot pointer. |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1235 | ReturnSlot.setAddress(CGF.Builder.CreateBitCast(ReturnSlot.getAddress(CGF), |
Reid Kleckner | 9b3e3df | 2014-09-04 20:04:38 +0000 | [diff] [blame] | 1236 | CoerceTy->getPointerTo())); |
| 1237 | ResultRegDests.push_back(ReturnSlot); |
| 1238 | |
| 1239 | rewriteInputConstraintReferences(NumOutputs, 1, AsmString); |
| 1240 | } |
| 1241 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1242 | /// shouldReturnTypeInRegister - Determine if the given type should be |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1243 | /// returned in a register (for the Darwin and MCU ABI). |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1244 | bool X86_32ABIInfo::shouldReturnTypeInRegister(QualType Ty, |
| 1245 | ASTContext &Context) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1246 | uint64_t Size = Context.getTypeSize(Ty); |
| 1247 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1248 | // For i386, type must be register sized. |
| 1249 | // For the MCU ABI, it only needs to be <= 8-byte |
| 1250 | if ((IsMCUABI && Size > 64) || (!IsMCUABI && !isRegisterSize(Size))) |
| 1251 | return false; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1252 | |
| 1253 | if (Ty->isVectorType()) { |
| 1254 | // 64- and 128- bit vectors inside structures are not returned in |
| 1255 | // registers. |
| 1256 | if (Size == 64 || Size == 128) |
| 1257 | return false; |
| 1258 | |
| 1259 | return true; |
| 1260 | } |
| 1261 | |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1262 | // If this is a builtin, pointer, enum, complex type, member pointer, or |
| 1263 | // member function pointer it is ok. |
Daniel Dunbar | 6b45b67 | 2010-05-14 03:40:53 +0000 | [diff] [blame] | 1264 | if (Ty->getAs<BuiltinType>() || Ty->hasPointerRepresentation() || |
Daniel Dunbar | b3b1e53 | 2009-09-24 05:12:36 +0000 | [diff] [blame] | 1265 | Ty->isAnyComplexType() || Ty->isEnumeralType() || |
Daniel Dunbar | 4bd95c6 | 2010-05-15 00:00:30 +0000 | [diff] [blame] | 1266 | Ty->isBlockPointerType() || Ty->isMemberPointerType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1267 | return true; |
| 1268 | |
| 1269 | // Arrays are treated like records. |
| 1270 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1271 | return shouldReturnTypeInRegister(AT->getElementType(), Context); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1272 | |
| 1273 | // Otherwise, it must be a record type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1274 | const RecordType *RT = Ty->getAs<RecordType>(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1275 | if (!RT) return false; |
| 1276 | |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1277 | // FIXME: Traverse bases here too. |
| 1278 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1279 | // Structure types are passed in register if all fields would be |
| 1280 | // passed in a register. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1281 | for (const auto *FD : RT->getDecl()->fields()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1282 | // Empty fields are ignored. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 1283 | if (isEmptyField(Context, FD, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1284 | continue; |
| 1285 | |
| 1286 | // Check fields recursively. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1287 | if (!shouldReturnTypeInRegister(FD->getType(), Context)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1288 | return false; |
| 1289 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1290 | return true; |
| 1291 | } |
| 1292 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1293 | static bool is32Or64BitBasicType(QualType Ty, ASTContext &Context) { |
| 1294 | // Treat complex types as the element type. |
| 1295 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 1296 | Ty = CTy->getElementType(); |
| 1297 | |
| 1298 | // Check for a type which we know has a simple scalar argument-passing |
| 1299 | // convention without any padding. (We're specifically looking for 32 |
| 1300 | // and 64-bit integer and integer-equivalents, float, and double.) |
| 1301 | if (!Ty->getAs<BuiltinType>() && !Ty->hasPointerRepresentation() && |
| 1302 | !Ty->isEnumeralType() && !Ty->isBlockPointerType()) |
| 1303 | return false; |
| 1304 | |
| 1305 | uint64_t Size = Context.getTypeSize(Ty); |
| 1306 | return Size == 32 || Size == 64; |
| 1307 | } |
| 1308 | |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1309 | static bool addFieldSizes(ASTContext &Context, const RecordDecl *RD, |
| 1310 | uint64_t &Size) { |
| 1311 | for (const auto *FD : RD->fields()) { |
| 1312 | // Scalar arguments on the stack get 4 byte alignment on x86. If the |
| 1313 | // argument is smaller than 32-bits, expanding the struct will create |
| 1314 | // alignment padding. |
| 1315 | if (!is32Or64BitBasicType(FD->getType(), Context)) |
| 1316 | return false; |
| 1317 | |
| 1318 | // FIXME: Reject bit-fields wholesale; there are two problems, we don't know |
| 1319 | // how to expand them yet, and the predicate for telling if a bitfield still |
| 1320 | // counts as "basic" is more complicated than what we were doing previously. |
| 1321 | if (FD->isBitField()) |
| 1322 | return false; |
| 1323 | |
| 1324 | Size += Context.getTypeSize(FD->getType()); |
| 1325 | } |
| 1326 | return true; |
| 1327 | } |
| 1328 | |
| 1329 | static bool addBaseAndFieldSizes(ASTContext &Context, const CXXRecordDecl *RD, |
| 1330 | uint64_t &Size) { |
| 1331 | // Don't do this if there are any non-empty bases. |
| 1332 | for (const CXXBaseSpecifier &Base : RD->bases()) { |
| 1333 | if (!addBaseAndFieldSizes(Context, Base.getType()->getAsCXXRecordDecl(), |
| 1334 | Size)) |
| 1335 | return false; |
| 1336 | } |
| 1337 | if (!addFieldSizes(Context, RD, Size)) |
| 1338 | return false; |
| 1339 | return true; |
| 1340 | } |
| 1341 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1342 | /// Test whether an argument type which is to be passed indirectly (on the |
| 1343 | /// stack) would have the equivalent layout if it was expanded into separate |
| 1344 | /// arguments. If so, we prefer to do the latter to avoid inhibiting |
| 1345 | /// optimizations. |
| 1346 | bool X86_32ABIInfo::canExpandIndirectArgument(QualType Ty) const { |
| 1347 | // We can only expand structure types. |
| 1348 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1349 | if (!RT) |
| 1350 | return false; |
| 1351 | const RecordDecl *RD = RT->getDecl(); |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1352 | uint64_t Size = 0; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1353 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1354 | if (!IsWin32StructABI) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1355 | // On non-Windows, we have to conservatively match our old bitcode |
| 1356 | // prototypes in order to be ABI-compatible at the bitcode level. |
| 1357 | if (!CXXRD->isCLike()) |
| 1358 | return false; |
| 1359 | } else { |
| 1360 | // Don't do this for dynamic classes. |
| 1361 | if (CXXRD->isDynamicClass()) |
| 1362 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1363 | } |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1364 | if (!addBaseAndFieldSizes(getContext(), CXXRD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1365 | return false; |
Reid Kleckner | 791bbf6 | 2017-01-13 17:18:19 +0000 | [diff] [blame] | 1366 | } else { |
| 1367 | if (!addFieldSizes(getContext(), RD, Size)) |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1368 | return false; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | // We can do this if there was no alignment padding. |
| 1372 | return Size == getContext().getTypeSize(Ty); |
| 1373 | } |
| 1374 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1375 | ABIArgInfo X86_32ABIInfo::getIndirectReturnResult(QualType RetTy, CCState &State) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1376 | // If the return value is indirect, then the hidden argument is consuming one |
| 1377 | // integer register. |
| 1378 | if (State.FreeRegs) { |
| 1379 | --State.FreeRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1380 | if (!IsMCUABI) |
| 1381 | return getNaturalAlignIndirectInReg(RetTy); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1382 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1383 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/false); |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 1386 | ABIArgInfo X86_32ABIInfo::classifyReturnType(QualType RetTy, |
| 1387 | CCState &State) const { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1388 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1389 | return ABIArgInfo::getIgnore(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1390 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1391 | const Type *Base = nullptr; |
| 1392 | uint64_t NumElts = 0; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1393 | if ((State.CC == llvm::CallingConv::X86_VectorCall || |
| 1394 | State.CC == llvm::CallingConv::X86_RegCall) && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1395 | isHomogeneousAggregate(RetTy, Base, NumElts)) { |
| 1396 | // The LLVM struct type for such an aggregate should lower properly. |
| 1397 | return ABIArgInfo::getDirect(); |
| 1398 | } |
| 1399 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1400 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1401 | // On Darwin, some vectors are returned in registers. |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1402 | if (IsDarwinVectorABI) { |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1403 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 1404 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1405 | // 128-bit vectors are a special case; they are returned in |
| 1406 | // registers and we need to make sure to pick a type the LLVM |
| 1407 | // backend will like. |
| 1408 | if (Size == 128) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1409 | return ABIArgInfo::getDirect(llvm::VectorType::get( |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1410 | llvm::Type::getInt64Ty(getVMContext()), 2)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1411 | |
| 1412 | // Always return in register if it fits in a general purpose |
| 1413 | // register, or if it is 64 bits and has a single element. |
| 1414 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1415 | (Size == 64 && VT->getNumElements() == 1)) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1416 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1417 | Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1418 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1419 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | return ABIArgInfo::getDirect(); |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1423 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1424 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 1425 | if (isAggregateTypeForABI(RetTy)) { |
Anders Carlsson | 40446e8 | 2010-01-27 03:25:19 +0000 | [diff] [blame] | 1426 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1427 | // Structures with flexible arrays are always indirect. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1428 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1429 | return getIndirectReturnResult(RetTy, State); |
Anders Carlsson | 5789c49 | 2009-10-20 22:07:59 +0000 | [diff] [blame] | 1430 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1431 | |
David Chisnall | de3a069 | 2009-08-17 23:08:21 +0000 | [diff] [blame] | 1432 | // If specified, structs and unions are always indirect. |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 1433 | if (!IsRetSmallStructInRegABI && !RetTy->isAnyComplexType()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1434 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1435 | |
Denis Zobnin | 380b224 | 2016-02-11 11:26:03 +0000 | [diff] [blame] | 1436 | // Ignore empty structs/unions. |
| 1437 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 1438 | return ABIArgInfo::getIgnore(); |
| 1439 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1440 | // Small structures which are register sized are generally returned |
| 1441 | // in a register. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1442 | if (shouldReturnTypeInRegister(RetTy, getContext())) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1443 | uint64_t Size = getContext().getTypeSize(RetTy); |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1444 | |
| 1445 | // As a special-case, if the struct is a "single-element" struct, and |
| 1446 | // the field is of type "float" or "double", return it in a |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1447 | // floating-point register. (MSVC does not apply this special case.) |
| 1448 | // We apply a similar transformation for pointer types to improve the |
| 1449 | // quality of the generated IR. |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1450 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 1451 | if ((!IsWin32StructABI && SeltTy->isRealFloatingType()) |
Eli Friedman | a98d1f8 | 2012-01-25 22:46:34 +0000 | [diff] [blame] | 1452 | || SeltTy->hasPointerRepresentation()) |
Eli Friedman | ee94534 | 2011-11-18 01:25:50 +0000 | [diff] [blame] | 1453 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 1454 | |
| 1455 | // FIXME: We should be able to narrow this integer in cases with dead |
| 1456 | // padding. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 1457 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(),Size)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1460 | return getIndirectReturnResult(RetTy, State); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1461 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1462 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1463 | // Treat an enum type as its underlying type. |
| 1464 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 1465 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 1466 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1467 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 1468 | : ABIArgInfo::getDirect()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1471 | static bool isSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1472 | return Ty->getAs<VectorType>() && Context.getTypeSize(Ty) == 128; |
| 1473 | } |
| 1474 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1475 | static bool isRecordWithSSEVectorType(ASTContext &Context, QualType Ty) { |
| 1476 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1477 | if (!RT) |
| 1478 | return 0; |
| 1479 | const RecordDecl *RD = RT->getDecl(); |
| 1480 | |
| 1481 | // If this is a C++ record, check the bases first. |
| 1482 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1483 | for (const auto &I : CXXRD->bases()) |
| 1484 | if (!isRecordWithSSEVectorType(Context, I.getType())) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1485 | return false; |
| 1486 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1487 | for (const auto *i : RD->fields()) { |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1488 | QualType FT = i->getType(); |
| 1489 | |
Eli Friedman | 7919bea | 2012-06-05 19:40:46 +0000 | [diff] [blame] | 1490 | if (isSSEVectorType(Context, FT)) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1491 | return true; |
| 1492 | |
| 1493 | if (isRecordWithSSEVectorType(Context, FT)) |
| 1494 | return true; |
| 1495 | } |
| 1496 | |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1500 | unsigned X86_32ABIInfo::getTypeStackAlignInBytes(QualType Ty, |
| 1501 | unsigned Align) const { |
| 1502 | // Otherwise, if the alignment is less than or equal to the minimum ABI |
| 1503 | // alignment, just use the default; the backend will handle this. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1504 | if (Align <= MinABIStackAlignInBytes) |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1505 | return 0; // Use default alignment. |
| 1506 | |
Pengfei Wang | 48387ec | 2019-05-31 01:50:07 +0000 | [diff] [blame] | 1507 | // On non-Darwin, the stack type alignment is always 4. |
| 1508 | if (!IsDarwinVectorABI) { |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1509 | // Set explicit alignment, since we may need to realign the top. |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1510 | return MinABIStackAlignInBytes; |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1511 | } |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1512 | |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1513 | // 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] | 1514 | if (Align >= 16 && (isSSEVectorType(getContext(), Ty) || |
| 1515 | isRecordWithSSEVectorType(getContext(), Ty))) |
Daniel Dunbar | ed23de3 | 2010-09-16 20:42:00 +0000 | [diff] [blame] | 1516 | return 16; |
| 1517 | |
| 1518 | return MinABIStackAlignInBytes; |
Daniel Dunbar | 8a6c91f | 2010-09-16 20:41:56 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1521 | ABIArgInfo X86_32ABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1522 | CCState &State) const { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1523 | if (!ByVal) { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1524 | if (State.FreeRegs) { |
| 1525 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1526 | if (!IsMCUABI) |
| 1527 | return getNaturalAlignIndirectInReg(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1528 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1529 | return getNaturalAlignIndirect(Ty, false); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1530 | } |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 1531 | |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1532 | // Compute the byval alignment. |
| 1533 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 1534 | unsigned StackAlign = getTypeStackAlignInBytes(Ty, TypeAlign); |
| 1535 | if (StackAlign == 0) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1536 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true); |
Daniel Dunbar | dd38fbc | 2010-09-16 20:42:06 +0000 | [diff] [blame] | 1537 | |
| 1538 | // If the stack alignment is less than the type alignment, realign the |
| 1539 | // argument. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1540 | bool Realign = TypeAlign > StackAlign; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1541 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(StackAlign), |
| 1542 | /*ByVal=*/true, Realign); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1545 | X86_32ABIInfo::Class X86_32ABIInfo::classify(QualType Ty) const { |
| 1546 | const Type *T = isSingleElementStruct(Ty, getContext()); |
| 1547 | if (!T) |
| 1548 | T = Ty.getTypePtr(); |
| 1549 | |
| 1550 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
| 1551 | BuiltinType::Kind K = BT->getKind(); |
| 1552 | if (K == BuiltinType::Float || K == BuiltinType::Double) |
| 1553 | return Float; |
| 1554 | } |
| 1555 | return Integer; |
| 1556 | } |
| 1557 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1558 | bool X86_32ABIInfo::updateFreeRegs(QualType Ty, CCState &State) const { |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 1559 | if (!IsSoftFloatABI) { |
| 1560 | Class C = classify(Ty); |
| 1561 | if (C == Float) |
| 1562 | return false; |
| 1563 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1564 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1565 | unsigned Size = getContext().getTypeSize(Ty); |
| 1566 | unsigned SizeInRegs = (Size + 31) / 32; |
Rafael Espindola | e2a9e90 | 2012-10-23 02:04:01 +0000 | [diff] [blame] | 1567 | |
| 1568 | if (SizeInRegs == 0) |
| 1569 | return false; |
| 1570 | |
Michael Kuperstein | 6890188 | 2015-10-25 08:18:20 +0000 | [diff] [blame] | 1571 | if (!IsMCUABI) { |
| 1572 | if (SizeInRegs > State.FreeRegs) { |
| 1573 | State.FreeRegs = 0; |
| 1574 | return false; |
| 1575 | } |
| 1576 | } else { |
| 1577 | // The MCU psABI allows passing parameters in-reg even if there are |
| 1578 | // earlier parameters that are passed on the stack. Also, |
| 1579 | // it does not allow passing >8-byte structs in-register, |
| 1580 | // even if there are 3 free registers available. |
| 1581 | if (SizeInRegs > State.FreeRegs || SizeInRegs > 2) |
| 1582 | return false; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1583 | } |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1584 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1585 | State.FreeRegs -= SizeInRegs; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1586 | return true; |
| 1587 | } |
| 1588 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1589 | bool X86_32ABIInfo::shouldAggregateUseDirect(QualType Ty, CCState &State, |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1590 | bool &InReg, |
| 1591 | bool &NeedsPadding) const { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1592 | // On Windows, aggregates other than HFAs are never passed in registers, and |
| 1593 | // they do not consume register slots. Homogenous floating-point aggregates |
| 1594 | // (HFAs) have already been dealt with at this point. |
| 1595 | if (IsWin32StructABI && isAggregateTypeForABI(Ty)) |
| 1596 | return false; |
| 1597 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1598 | NeedsPadding = false; |
| 1599 | InReg = !IsMCUABI; |
| 1600 | |
| 1601 | if (!updateFreeRegs(Ty, State)) |
| 1602 | return false; |
| 1603 | |
| 1604 | if (IsMCUABI) |
| 1605 | return true; |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1606 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1607 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1608 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1609 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1610 | if (getContext().getTypeSize(Ty) <= 32 && State.FreeRegs) |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1611 | NeedsPadding = true; |
| 1612 | |
Rafael Espindola | 077dd59 | 2012-10-24 01:58:58 +0000 | [diff] [blame] | 1613 | return false; |
| 1614 | } |
| 1615 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1616 | return true; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1619 | bool X86_32ABIInfo::shouldPrimitiveUseInReg(QualType Ty, CCState &State) const { |
| 1620 | if (!updateFreeRegs(Ty, State)) |
| 1621 | return false; |
| 1622 | |
| 1623 | if (IsMCUABI) |
| 1624 | return false; |
| 1625 | |
| 1626 | if (State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1627 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1628 | State.CC == llvm::CallingConv::X86_RegCall) { |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1629 | if (getContext().getTypeSize(Ty) > 32) |
| 1630 | return false; |
| 1631 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1632 | return (Ty->isIntegralOrEnumerationType() || Ty->isPointerType() || |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1633 | Ty->isReferenceType()); |
| 1634 | } |
| 1635 | |
| 1636 | return true; |
| 1637 | } |
| 1638 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1639 | ABIArgInfo X86_32ABIInfo::classifyArgumentType(QualType Ty, |
| 1640 | CCState &State) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1641 | // FIXME: Set alignment on indirect arguments. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1642 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 1643 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 1644 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1645 | // Check with the C++ ABI first. |
| 1646 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 1647 | if (RT) { |
| 1648 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 1649 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 1650 | return getIndirectResult(Ty, false, State); |
| 1651 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 1652 | // The field index doesn't matter, we'll fix it up later. |
| 1653 | return ABIArgInfo::getInAlloca(/*FieldIndex=*/0); |
| 1654 | } |
| 1655 | } |
| 1656 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1657 | // Regcall uses the concept of a homogenous vector aggregate, similar |
| 1658 | // to other targets. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1659 | const Type *Base = nullptr; |
| 1660 | uint64_t NumElts = 0; |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1661 | if (State.CC == llvm::CallingConv::X86_RegCall && |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1662 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1663 | |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1664 | if (State.FreeSSERegs >= NumElts) { |
| 1665 | State.FreeSSERegs -= NumElts; |
| 1666 | if (Ty->isBuiltinType() || Ty->isVectorType()) |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1667 | return ABIArgInfo::getDirect(); |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1668 | return ABIArgInfo::getExpand(); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1669 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1670 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | if (isAggregateTypeForABI(Ty)) { |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1674 | // Structures with flexible arrays are always indirect. |
| 1675 | // FIXME: This should not be byval! |
| 1676 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 1677 | return getIndirectResult(Ty, true, State); |
Daniel Dunbar | 557893d | 2010-04-21 19:10:51 +0000 | [diff] [blame] | 1678 | |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1679 | // Ignore empty structs/unions on non-Windows. |
| 1680 | if (!IsWin32StructABI && isEmptyRecord(getContext(), Ty, true)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1681 | return ABIArgInfo::getIgnore(); |
| 1682 | |
Rafael Espindola | fad28de | 2012-10-24 01:59:00 +0000 | [diff] [blame] | 1683 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 1684 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1685 | bool NeedsPadding = false; |
| 1686 | bool InReg; |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1687 | if (shouldAggregateUseDirect(Ty, State, InReg, NeedsPadding)) { |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1688 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Craig Topper | ac9201a | 2013-07-08 04:47:18 +0000 | [diff] [blame] | 1689 | SmallVector<llvm::Type*, 3> Elements(SizeInRegs, Int32); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1690 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1691 | if (InReg) |
| 1692 | return ABIArgInfo::getDirectInReg(Result); |
| 1693 | else |
| 1694 | return ABIArgInfo::getDirect(Result); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1695 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1696 | llvm::IntegerType *PaddingType = NeedsPadding ? Int32 : nullptr; |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1697 | |
Daniel Dunbar | 11c08c8 | 2009-11-09 01:33:53 +0000 | [diff] [blame] | 1698 | // Expand small (<= 128-bit) record types when we know that the stack layout |
| 1699 | // of those arguments will match the struct. This is important because the |
| 1700 | // LLVM backend isn't smart enough to remove byval, which inhibits many |
| 1701 | // optimizations. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1702 | // Don't do this for the MCU if there are still free integer registers |
| 1703 | // (see X86_64 ABI for full explanation). |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1704 | if (getContext().getTypeSize(Ty) <= 4 * 32 && |
| 1705 | (!IsMCUABI || State.FreeRegs == 0) && canExpandIndirectArgument(Ty)) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1706 | return ABIArgInfo::getExpandWithPadding( |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1707 | State.CC == llvm::CallingConv::X86_FastCall || |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1708 | State.CC == llvm::CallingConv::X86_VectorCall || |
| 1709 | State.CC == llvm::CallingConv::X86_RegCall, |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1710 | PaddingType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1711 | |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1712 | return getIndirectResult(Ty, true, State); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1715 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | d7e5480 | 2010-08-26 20:08:43 +0000 | [diff] [blame] | 1716 | // On Darwin, some vectors are passed in memory, we handle this by passing |
| 1717 | // it as an i8/i16/i32/i64. |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1718 | if (IsDarwinVectorABI) { |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1719 | uint64_t Size = getContext().getTypeSize(Ty); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1720 | if ((Size == 8 || Size == 16 || Size == 32) || |
| 1721 | (Size == 64 && VT->getNumElements() == 1)) |
| 1722 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 1723 | Size)); |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1724 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 1725 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1726 | if (IsX86_MMXType(CGT.ConvertType(Ty))) |
| 1727 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), 64)); |
| 1728 | |
Chris Lattner | d774ae9 | 2010-08-26 20:05:13 +0000 | [diff] [blame] | 1729 | return ABIArgInfo::getDirect(); |
| 1730 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 1731 | |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 1732 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 1733 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 1734 | Ty = EnumTy->getDecl()->getIntegerType(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 1735 | |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1736 | bool InReg = shouldPrimitiveUseInReg(Ty, State); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1737 | |
| 1738 | if (Ty->isPromotableIntegerType()) { |
| 1739 | if (InReg) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 1740 | return ABIArgInfo::getExtendInReg(Ty); |
| 1741 | return ABIArgInfo::getExtend(Ty); |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1742 | } |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1743 | |
Rafael Espindola | 703c47f | 2012-10-19 05:04:37 +0000 | [diff] [blame] | 1744 | if (InReg) |
| 1745 | return ABIArgInfo::getDirectInReg(); |
| 1746 | return ABIArgInfo::getDirect(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1749 | void X86_32ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, CCState &State, |
| 1750 | bool &UsedInAlloca) const { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1751 | // Vectorcall x86 works subtly different than in x64, so the format is |
| 1752 | // a bit different than the x64 version. First, all vector types (not HVAs) |
| 1753 | // are assigned, with the first 6 ending up in the YMM0-5 or XMM0-5 registers. |
| 1754 | // This differs from the x64 implementation, where the first 6 by INDEX get |
| 1755 | // registers. |
| 1756 | // After that, integers AND HVAs are assigned Left to Right in the same pass. |
| 1757 | // Integers are passed as ECX/EDX if one is available (in order). HVAs will |
| 1758 | // first take up the remaining YMM/XMM registers. If insufficient registers |
| 1759 | // remain but an integer register (ECX/EDX) is available, it will be passed |
| 1760 | // in that, else, on the stack. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1761 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1762 | // First pass do all the vector types. |
| 1763 | const Type *Base = nullptr; |
| 1764 | uint64_t NumElts = 0; |
| 1765 | const QualType& Ty = I.type; |
| 1766 | if ((Ty->isVectorType() || Ty->isBuiltinType()) && |
| 1767 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 1768 | if (State.FreeSSERegs >= NumElts) { |
| 1769 | State.FreeSSERegs -= NumElts; |
| 1770 | I.info = ABIArgInfo::getDirect(); |
| 1771 | } else { |
| 1772 | I.info = classifyArgumentType(Ty, State); |
| 1773 | } |
| 1774 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1775 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1776 | } |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1777 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1778 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 1779 | // Second pass, do the rest! |
| 1780 | const Type *Base = nullptr; |
| 1781 | uint64_t NumElts = 0; |
| 1782 | const QualType& Ty = I.type; |
| 1783 | bool IsHva = isHomogeneousAggregate(Ty, Base, NumElts); |
| 1784 | |
| 1785 | if (IsHva && !Ty->isVectorType() && !Ty->isBuiltinType()) { |
| 1786 | // Assign true HVAs (non vector/native FP types). |
| 1787 | if (State.FreeSSERegs >= NumElts) { |
| 1788 | State.FreeSSERegs -= NumElts; |
| 1789 | I.info = getDirectX86Hva(); |
| 1790 | } else { |
| 1791 | I.info = getIndirectResult(Ty, /*ByVal=*/false, State); |
| 1792 | } |
| 1793 | } else if (!IsHva) { |
| 1794 | // Assign all Non-HVAs, so this will exclude Vector/FP args. |
| 1795 | I.info = classifyArgumentType(Ty, State); |
| 1796 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1797 | } |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } |
| 1800 | |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1801 | void X86_32ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1802 | CCState State(FI.getCallingConvention()); |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1803 | if (IsMCUABI) |
| 1804 | State.FreeRegs = 3; |
| 1805 | else if (State.CC == llvm::CallingConv::X86_FastCall) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1806 | State.FreeRegs = 2; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 1807 | else if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1808 | State.FreeRegs = 2; |
| 1809 | State.FreeSSERegs = 6; |
| 1810 | } else if (FI.getHasRegParm()) |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1811 | State.FreeRegs = FI.getRegParm(); |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 1812 | else if (State.CC == llvm::CallingConv::X86_RegCall) { |
| 1813 | State.FreeRegs = 5; |
| 1814 | State.FreeSSERegs = 8; |
| 1815 | } else |
Reid Kleckner | 661f35b | 2014-01-18 01:12:41 +0000 | [diff] [blame] | 1816 | State.FreeRegs = DefaultNumRegisterParameters; |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1817 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 1818 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 1819 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), State); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1820 | } else if (FI.getReturnInfo().isIndirect()) { |
| 1821 | // The C++ ABI is not aware of register usage, so we have to check if the |
| 1822 | // return value was sret and put it in a register ourselves if appropriate. |
| 1823 | if (State.FreeRegs) { |
| 1824 | --State.FreeRegs; // The sret parameter consumes a register. |
Michael Kuperstein | f3163dc | 2015-12-28 14:39:54 +0000 | [diff] [blame] | 1825 | if (!IsMCUABI) |
| 1826 | FI.getReturnInfo().setInReg(true); |
Reid Kleckner | 677539d | 2014-07-10 01:58:55 +0000 | [diff] [blame] | 1827 | } |
| 1828 | } |
Rafael Espindola | 06b2b4a | 2012-07-31 02:44:24 +0000 | [diff] [blame] | 1829 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 1830 | // The chain argument effectively gives us another free register. |
| 1831 | if (FI.isChainCall()) |
| 1832 | ++State.FreeRegs; |
| 1833 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1834 | bool UsedInAlloca = false; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 1835 | if (State.CC == llvm::CallingConv::X86_VectorCall) { |
| 1836 | computeVectorCallArgs(FI, State, UsedInAlloca); |
| 1837 | } else { |
| 1838 | // If not vectorcall, revert to normal behavior. |
| 1839 | for (auto &I : FI.arguments()) { |
| 1840 | I.info = classifyArgumentType(I.type, State); |
| 1841 | UsedInAlloca |= (I.info.getKind() == ABIArgInfo::InAlloca); |
| 1842 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | // If we needed to use inalloca for any argument, do a second pass and rewrite |
| 1846 | // all the memory arguments to use inalloca. |
| 1847 | if (UsedInAlloca) |
| 1848 | rewriteWithInAlloca(FI); |
| 1849 | } |
| 1850 | |
| 1851 | void |
| 1852 | X86_32ABIInfo::addFieldToArgStruct(SmallVector<llvm::Type *, 6> &FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1853 | CharUnits &StackOffset, ABIArgInfo &Info, |
| 1854 | QualType Type) const { |
| 1855 | // Arguments are always 4-byte-aligned. |
| 1856 | CharUnits FieldAlign = CharUnits::fromQuantity(4); |
| 1857 | |
| 1858 | assert(StackOffset.isMultipleOf(FieldAlign) && "unaligned inalloca struct"); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1859 | Info = ABIArgInfo::getInAlloca(FrameFields.size()); |
| 1860 | FrameFields.push_back(CGT.ConvertTypeForMem(Type)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1861 | StackOffset += getContext().getTypeSizeInChars(Type); |
Reid Kleckner | d378a71 | 2014-04-10 19:09:43 +0000 | [diff] [blame] | 1862 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1863 | // Insert padding bytes to respect alignment. |
| 1864 | CharUnits FieldEnd = StackOffset; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1865 | StackOffset = FieldEnd.alignTo(FieldAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1866 | if (StackOffset != FieldEnd) { |
| 1867 | CharUnits NumBytes = StackOffset - FieldEnd; |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1868 | llvm::Type *Ty = llvm::Type::getInt8Ty(getVMContext()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1869 | Ty = llvm::ArrayType::get(Ty, NumBytes.getQuantity()); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1870 | FrameFields.push_back(Ty); |
| 1871 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1874 | static bool isArgInAlloca(const ABIArgInfo &Info) { |
| 1875 | // Leave ignored and inreg arguments alone. |
| 1876 | switch (Info.getKind()) { |
| 1877 | case ABIArgInfo::InAlloca: |
| 1878 | return true; |
| 1879 | case ABIArgInfo::Indirect: |
| 1880 | assert(Info.getIndirectByVal()); |
| 1881 | return true; |
| 1882 | case ABIArgInfo::Ignore: |
| 1883 | return false; |
| 1884 | case ABIArgInfo::Direct: |
| 1885 | case ABIArgInfo::Extend: |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1886 | if (Info.getInReg()) |
| 1887 | return false; |
| 1888 | return true; |
Reid Kleckner | 0404605 | 2016-05-02 17:41:07 +0000 | [diff] [blame] | 1889 | case ABIArgInfo::Expand: |
| 1890 | case ABIArgInfo::CoerceAndExpand: |
| 1891 | // These are aggregate types which are never passed in registers when |
| 1892 | // inalloca is involved. |
| 1893 | return true; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1894 | } |
| 1895 | llvm_unreachable("invalid enum"); |
| 1896 | } |
| 1897 | |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1898 | void X86_32ABIInfo::rewriteWithInAlloca(CGFunctionInfo &FI) const { |
| 1899 | assert(IsWin32StructABI && "inalloca only supported on win32"); |
| 1900 | |
| 1901 | // Build a packed struct type for all of the arguments in memory. |
| 1902 | SmallVector<llvm::Type *, 6> FrameFields; |
| 1903 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1904 | // The stack alignment is always 4. |
| 1905 | CharUnits StackAlign = CharUnits::fromQuantity(4); |
| 1906 | |
| 1907 | CharUnits StackOffset; |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1908 | CGFunctionInfo::arg_iterator I = FI.arg_begin(), E = FI.arg_end(); |
| 1909 | |
| 1910 | // Put 'this' into the struct before 'sret', if necessary. |
| 1911 | bool IsThisCall = |
| 1912 | FI.getCallingConvention() == llvm::CallingConv::X86_ThisCall; |
| 1913 | ABIArgInfo &Ret = FI.getReturnInfo(); |
| 1914 | if (Ret.isIndirect() && Ret.isSRetAfterThis() && !IsThisCall && |
| 1915 | isArgInAlloca(I->info)) { |
| 1916 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
| 1917 | ++I; |
| 1918 | } |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1919 | |
| 1920 | // Put the sret parameter into the inalloca struct if it's in memory. |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1921 | if (Ret.isIndirect() && !Ret.getInReg()) { |
| 1922 | CanQualType PtrTy = getContext().getPointerType(FI.getReturnType()); |
| 1923 | addFieldToArgStruct(FrameFields, StackOffset, Ret, PtrTy); |
Reid Kleckner | fab1e89 | 2014-02-25 00:59:14 +0000 | [diff] [blame] | 1924 | // On Windows, the hidden sret parameter is always returned in eax. |
| 1925 | Ret.setInAllocaSRet(IsWin32StructABI); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | // Skip the 'this' parameter in ecx. |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1929 | if (IsThisCall) |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1930 | ++I; |
| 1931 | |
| 1932 | // Put arguments passed in memory into the struct. |
| 1933 | for (; I != E; ++I) { |
Reid Kleckner | 852361d | 2014-07-26 00:12:26 +0000 | [diff] [blame] | 1934 | if (isArgInAlloca(I->info)) |
| 1935 | addFieldToArgStruct(FrameFields, StackOffset, I->info, I->type); |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | FI.setArgStruct(llvm::StructType::get(getVMContext(), FrameFields, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1939 | /*isPacked=*/true), |
| 1940 | StackAlign); |
Rafael Espindola | a647296 | 2012-07-24 00:01:07 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1943 | Address X86_32ABIInfo::EmitVAArg(CodeGenFunction &CGF, |
| 1944 | Address VAListAddr, QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1945 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1946 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1947 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1948 | // x86-32 changes the alignment of certain arguments on the stack. |
| 1949 | // |
| 1950 | // Just messing with TypeInfo like this works because we never pass |
| 1951 | // anything indirectly. |
| 1952 | TypeInfo.second = CharUnits::fromQuantity( |
| 1953 | getTypeStackAlignInBytes(Ty, TypeInfo.second.getQuantity())); |
Eli Friedman | 1d7dd3b | 2011-11-18 02:12:09 +0000 | [diff] [blame] | 1954 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1955 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 1956 | TypeInfo, CharUnits::fromQuantity(4), |
| 1957 | /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1960 | bool X86_32TargetCodeGenInfo::isStructReturnInRegABI( |
| 1961 | const llvm::Triple &Triple, const CodeGenOptions &Opts) { |
| 1962 | assert(Triple.getArch() == llvm::Triple::x86); |
| 1963 | |
| 1964 | switch (Opts.getStructReturnConvention()) { |
| 1965 | case CodeGenOptions::SRCK_Default: |
| 1966 | break; |
| 1967 | case CodeGenOptions::SRCK_OnStack: // -fpcc-struct-return |
| 1968 | return false; |
| 1969 | case CodeGenOptions::SRCK_InRegs: // -freg-struct-return |
| 1970 | return true; |
| 1971 | } |
| 1972 | |
Michael Kuperstein | d749f23 | 2015-10-27 07:46:22 +0000 | [diff] [blame] | 1973 | if (Triple.isOSDarwin() || Triple.isOSIAMCU()) |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1974 | return true; |
| 1975 | |
| 1976 | switch (Triple.getOS()) { |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1977 | case llvm::Triple::DragonFly: |
| 1978 | case llvm::Triple::FreeBSD: |
| 1979 | case llvm::Triple::OpenBSD: |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1980 | case llvm::Triple::Win32: |
Reid Kleckner | 2918fef | 2014-11-24 22:05:42 +0000 | [diff] [blame] | 1981 | return true; |
Richard Sandiford | dcb8d9c | 2014-07-08 11:10:34 +0000 | [diff] [blame] | 1982 | default: |
| 1983 | return false; |
| 1984 | } |
| 1985 | } |
| 1986 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1987 | void X86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 1988 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 1989 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 1990 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 1991 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1992 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1993 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 1994 | Fn->addFnAttr("stackrealign"); |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 1995 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 1996 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 1997 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 1998 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 1999 | } |
Charles Davis | 4ea31ab | 2010-02-13 15:54:06 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2003 | bool X86_32TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 2004 | CodeGen::CodeGenFunction &CGF, |
| 2005 | llvm::Value *Address) const { |
| 2006 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2007 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2008 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2009 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2010 | // 0-7 are the eight integer registers; the order is different |
| 2011 | // on Darwin (for EH), but the range is the same. |
| 2012 | // 8 is %eip. |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2013 | AssignToArrayRange(Builder, Address, Four8, 0, 8); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2014 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2015 | if (CGF.CGM.getTarget().getTriple().isOSDarwin()) { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2016 | // 12-16 are st(0..4). Not sure why we stop at 4. |
| 2017 | // These have size 16, which is sizeof(long double) on |
| 2018 | // platforms with 8-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2019 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(CGF.Int8Ty, 16); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2020 | AssignToArrayRange(Builder, Address, Sixteen8, 12, 16); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2021 | |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2022 | } else { |
| 2023 | // 9 is %eflags, which doesn't get a size on Darwin for some |
| 2024 | // reason. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2025 | Builder.CreateAlignedStore( |
| 2026 | Four8, Builder.CreateConstInBoundsGEP1_32(CGF.Int8Ty, Address, 9), |
| 2027 | CharUnits::One()); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2028 | |
| 2029 | // 11-16 are st(0..5). Not sure why we stop at 5. |
| 2030 | // These have size 12, which is sizeof(long double) on |
| 2031 | // platforms with 4-byte alignment for that type. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2032 | llvm::Value *Twelve8 = llvm::ConstantInt::get(CGF.Int8Ty, 12); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2033 | AssignToArrayRange(Builder, Address, Twelve8, 11, 16); |
| 2034 | } |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2035 | |
| 2036 | return false; |
| 2037 | } |
| 2038 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 2039 | //===----------------------------------------------------------------------===// |
| 2040 | // X86-64 ABI Implementation |
| 2041 | //===----------------------------------------------------------------------===// |
| 2042 | |
| 2043 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2044 | namespace { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2045 | /// The AVX ABI level for X86 targets. |
| 2046 | enum class X86AVXABILevel { |
| 2047 | None, |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2048 | AVX, |
| 2049 | AVX512 |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2050 | }; |
| 2051 | |
| 2052 | /// \p returns the size in bits of the largest (native) vector for \p AVXLevel. |
| 2053 | static unsigned getNativeVectorSizeForAVXABI(X86AVXABILevel AVXLevel) { |
| 2054 | switch (AVXLevel) { |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2055 | case X86AVXABILevel::AVX512: |
| 2056 | return 512; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2057 | case X86AVXABILevel::AVX: |
| 2058 | return 256; |
| 2059 | case X86AVXABILevel::None: |
| 2060 | return 128; |
| 2061 | } |
Yaron Keren | b76cb04 | 2015-06-23 09:45:42 +0000 | [diff] [blame] | 2062 | llvm_unreachable("Unknown AVXLevel"); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2065 | /// X86_64ABIInfo - The X86_64 ABI information. |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2066 | class X86_64ABIInfo : public SwiftABIInfo { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2067 | enum Class { |
| 2068 | Integer = 0, |
| 2069 | SSE, |
| 2070 | SSEUp, |
| 2071 | X87, |
| 2072 | X87Up, |
| 2073 | ComplexX87, |
| 2074 | NoClass, |
| 2075 | Memory |
| 2076 | }; |
| 2077 | |
| 2078 | /// merge - Implement the X86_64 ABI merging algorithm. |
| 2079 | /// |
| 2080 | /// Merge an accumulating classification \arg Accum with a field |
| 2081 | /// classification \arg Field. |
| 2082 | /// |
| 2083 | /// \param Accum - The accumulating classification. This should |
| 2084 | /// always be either NoClass or the result of a previous merge |
| 2085 | /// call. In addition, this should never be Memory (the caller |
| 2086 | /// should just return Memory for the aggregate). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2087 | static Class merge(Class Accum, Class Field); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2088 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2089 | /// postMerge - Implement the X86_64 ABI post merging algorithm. |
| 2090 | /// |
| 2091 | /// Post merger cleanup, reduces a malformed Hi and Lo pair to |
| 2092 | /// final MEMORY or SSE classes when necessary. |
| 2093 | /// |
| 2094 | /// \param AggregateSize - The size of the current aggregate in |
| 2095 | /// the classification process. |
| 2096 | /// |
| 2097 | /// \param Lo - The classification for the parts of the type |
| 2098 | /// residing in the low word of the containing object. |
| 2099 | /// |
| 2100 | /// \param Hi - The classification for the parts of the type |
| 2101 | /// residing in the higher words of the containing object. |
| 2102 | /// |
| 2103 | void postMerge(unsigned AggregateSize, Class &Lo, Class &Hi) const; |
| 2104 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2105 | /// classify - Determine the x86_64 register classes in which the |
| 2106 | /// given type T should be passed. |
| 2107 | /// |
| 2108 | /// \param Lo - The classification for the parts of the type |
| 2109 | /// residing in the low word of the containing object. |
| 2110 | /// |
| 2111 | /// \param Hi - The classification for the parts of the type |
| 2112 | /// residing in the high word of the containing object. |
| 2113 | /// |
| 2114 | /// \param OffsetBase - The bit offset of this type in the |
| 2115 | /// containing object. Some parameters are classified different |
| 2116 | /// depending on whether they straddle an eightbyte boundary. |
| 2117 | /// |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2118 | /// \param isNamedArg - Whether the argument in question is a "named" |
| 2119 | /// argument, as used in AMD64-ABI 3.5.7. |
| 2120 | /// |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2121 | /// If a word is unused its result will be NoClass; if a type should |
| 2122 | /// be passed in Memory then at least the classification of \arg Lo |
| 2123 | /// will be Memory. |
| 2124 | /// |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 2125 | /// The \arg Lo class will be NoClass iff the argument is ignored. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2126 | /// |
| 2127 | /// If the \arg Lo class is ComplexX87, then the \arg Hi class will |
| 2128 | /// also be ComplexX87. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2129 | void classify(QualType T, uint64_t OffsetBase, Class &Lo, Class &Hi, |
| 2130 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2131 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2132 | llvm::Type *GetByteVectorType(QualType Ty) const; |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2133 | llvm::Type *GetSSETypeAtOffset(llvm::Type *IRType, |
| 2134 | unsigned IROffset, QualType SourceTy, |
| 2135 | unsigned SourceOffset) const; |
| 2136 | llvm::Type *GetINTEGERTypeAtOffset(llvm::Type *IRType, |
| 2137 | unsigned IROffset, QualType SourceTy, |
| 2138 | unsigned SourceOffset) const; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2139 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2140 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2141 | /// such that the argument will be returned in memory. |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2142 | ABIArgInfo getIndirectReturnResult(QualType Ty) const; |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2143 | |
| 2144 | /// getIndirectResult - Give a source type \arg Ty, return a suitable result |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2145 | /// such that the argument will be passed in memory. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2146 | /// |
| 2147 | /// \param freeIntRegs - The number of free integer registers remaining |
| 2148 | /// available. |
| 2149 | ABIArgInfo getIndirectResult(QualType Ty, unsigned freeIntRegs) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2150 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 2151 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2152 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2153 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned freeIntRegs, |
| 2154 | unsigned &neededInt, unsigned &neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2155 | bool isNamedArg) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2156 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 2157 | ABIArgInfo classifyRegCallStructType(QualType Ty, unsigned &NeededInt, |
| 2158 | unsigned &NeededSSE) const; |
| 2159 | |
| 2160 | ABIArgInfo classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 2161 | unsigned &NeededSSE) const; |
| 2162 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2163 | bool IsIllegalVectorType(QualType Ty) const; |
| 2164 | |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2165 | /// The 0.98 ABI revision clarified a lot of ambiguities, |
| 2166 | /// unfortunately in ways that were not always consistent with |
| 2167 | /// certain previous compilers. In particular, platforms which |
| 2168 | /// required strict binary compatibility with older versions of GCC |
| 2169 | /// may need to exempt themselves. |
| 2170 | bool honorsRevision0_98() const { |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 2171 | return !getTarget().getTriple().isOSDarwin(); |
John McCall | e0fda73 | 2011-04-21 01:20:55 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2174 | /// GCC classifies <1 x long long> as SSE but some platform ABIs choose to |
| 2175 | /// classify it as INTEGER (for compatibility with older clang compilers). |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2176 | bool classifyIntegerMMXAsSSE() const { |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2177 | // Clang <= 3.8 did not do this. |
Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 2178 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2179 | LangOptions::ClangABI::Ver3_8) |
Richard Smith | f667ad5 | 2017-08-26 01:04:35 +0000 | [diff] [blame] | 2180 | return false; |
| 2181 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2182 | const llvm::Triple &Triple = getTarget().getTriple(); |
| 2183 | if (Triple.isOSDarwin() || Triple.getOS() == llvm::Triple::PS4) |
| 2184 | return false; |
| 2185 | if (Triple.isOSFreeBSD() && Triple.getOSMajorVersion() >= 10) |
| 2186 | return false; |
| 2187 | return true; |
| 2188 | } |
| 2189 | |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 2190 | // GCC classifies vectors of __int128 as memory. |
| 2191 | bool passInt128VectorsInMem() const { |
| 2192 | // Clang <= 9.0 did not do this. |
| 2193 | if (getContext().getLangOpts().getClangABICompat() <= |
| 2194 | LangOptions::ClangABI::Ver9) |
| 2195 | return false; |
| 2196 | |
| 2197 | const llvm::Triple &T = getTarget().getTriple(); |
| 2198 | return T.isOSLinux() || T.isOSNetBSD(); |
| 2199 | } |
| 2200 | |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2201 | X86AVXABILevel AVXLevel; |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2202 | // Some ABIs (e.g. X32 ABI and Native Client OS) use 32 bit pointers on |
| 2203 | // 64-bit hardware. |
| 2204 | bool Has64BitPointers; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2205 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2206 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2207 | X86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) : |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2208 | SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Derek Schuff | 8a872f3 | 2012-10-11 18:21:13 +0000 | [diff] [blame] | 2209 | Has64BitPointers(CGT.getDataLayout().getPointerSize(0) == 8) { |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 2210 | } |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2211 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2212 | bool isPassedUsingAVXType(QualType type) const { |
| 2213 | unsigned neededInt, neededSSE; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2214 | // The freeIntRegs argument doesn't matter here. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2215 | ABIArgInfo info = classifyArgumentType(type, 0, neededInt, neededSSE, |
| 2216 | /*isNamedArg*/true); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2217 | if (info.isDirect()) { |
| 2218 | llvm::Type *ty = info.getCoerceToType(); |
| 2219 | if (llvm::VectorType *vectorTy = dyn_cast_or_null<llvm::VectorType>(ty)) |
| 2220 | return (vectorTy->getBitWidth() > 128); |
| 2221 | } |
| 2222 | return false; |
| 2223 | } |
| 2224 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2225 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2226 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2227 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2228 | QualType Ty) const override; |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 2229 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2230 | QualType Ty) const override; |
Peter Collingbourne | 69b004d | 2015-02-25 23:18:42 +0000 | [diff] [blame] | 2231 | |
| 2232 | bool has64BitPointers() const { |
| 2233 | return Has64BitPointers; |
| 2234 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2235 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2236 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 2237 | bool asReturnValue) const override { |
| 2238 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2239 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2240 | bool isSwiftErrorInRegister() const override { |
| 2241 | return true; |
| 2242 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2243 | }; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2244 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2245 | /// WinX86_64ABIInfo - The Windows X86_64 ABI information. |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2246 | class WinX86_64ABIInfo : public SwiftABIInfo { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2247 | public: |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2248 | WinX86_64ABIInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
| 2249 | : SwiftABIInfo(CGT), AVXLevel(AVXLevel), |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2250 | IsMingw64(getTarget().getTriple().isWindowsGNUEnvironment()) {} |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 2251 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2252 | void computeInfo(CGFunctionInfo &FI) const override; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2253 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2254 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 2255 | QualType Ty) const override; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 2256 | |
| 2257 | bool isHomogeneousAggregateBaseType(QualType Ty) const override { |
| 2258 | // FIXME: Assumes vectorcall is in use. |
| 2259 | return isX86VectorTypeForVectorCall(getContext(), Ty); |
| 2260 | } |
| 2261 | |
| 2262 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 2263 | uint64_t NumMembers) const override { |
| 2264 | // FIXME: Assumes vectorcall is in use. |
| 2265 | return isX86VectorCallAggregateSmallEnough(NumMembers); |
| 2266 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2267 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 2268 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type *> scalars, |
Arnold Schwaighofer | 4fc955e | 2016-10-12 18:59:24 +0000 | [diff] [blame] | 2269 | bool asReturnValue) const override { |
| 2270 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 2271 | } |
| 2272 | |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 2273 | bool isSwiftErrorInRegister() const override { |
| 2274 | return true; |
| 2275 | } |
| 2276 | |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2277 | private: |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 2278 | ABIArgInfo classify(QualType Ty, unsigned &FreeSSERegs, bool IsReturnType, |
| 2279 | bool IsVectorCall, bool IsRegCall) const; |
| 2280 | ABIArgInfo reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 2281 | const ABIArgInfo ¤t) const; |
| 2282 | void computeVectorCallArgs(CGFunctionInfo &FI, unsigned FreeSSERegs, |
| 2283 | bool IsVectorCall, bool IsRegCall) const; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 2284 | |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2285 | X86AVXABILevel AVXLevel; |
| 2286 | |
| 2287 | bool IsMingw64; |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2288 | }; |
| 2289 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2290 | class X86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2291 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2292 | X86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, X86AVXABILevel AVXLevel) |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2293 | : TargetCodeGenInfo(new X86_64ABIInfo(CGT, AVXLevel)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2294 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2295 | const X86_64ABIInfo &getABIInfo() const { |
| 2296 | return static_cast<const X86_64ABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 2297 | } |
| 2298 | |
Akira Hatanaka | 65bb3f9 | 2019-03-21 19:59:49 +0000 | [diff] [blame] | 2299 | /// Disable tail call on x86-64. The epilogue code before the tail jump blocks |
| 2300 | /// the autoreleaseRV/retainRV optimization. |
| 2301 | bool shouldSuppressTailCallsOfRetainAutoreleasedReturnValue() const override { |
| 2302 | return true; |
| 2303 | } |
| 2304 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2305 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2306 | return 7; |
| 2307 | } |
| 2308 | |
| 2309 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2310 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2311 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2312 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 2313 | // 0-15 are the 16 integer registers. |
| 2314 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2315 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 2316 | return false; |
| 2317 | } |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2318 | |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2319 | llvm::Type* adjustInlineAsmType(CodeGen::CodeGenFunction &CGF, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2320 | StringRef Constraint, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2321 | llvm::Type* Ty) const override { |
Peter Collingbourne | 8f5cf74 | 2011-02-19 23:03:58 +0000 | [diff] [blame] | 2322 | return X86AdjustInlineAsmType(CGF, Constraint, Ty); |
| 2323 | } |
| 2324 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2325 | bool isNoProtoCallVariadic(const CallArgList &args, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2326 | const FunctionNoProtoType *fnType) const override { |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2327 | // The default CC on x86-64 sets %al to the number of SSA |
| 2328 | // registers used, and GCC sets this when calling an unprototyped |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2329 | // function, so we override the default behavior. However, don't do |
Eli Friedman | b8e45b2 | 2011-12-06 03:08:26 +0000 | [diff] [blame] | 2330 | // that when AVX types are involved: the ABI explicitly states it is |
| 2331 | // undefined, and it doesn't work in practice because of how the ABI |
| 2332 | // defines varargs anyway. |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2333 | if (fnType->getCallConv() == CC_C) { |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2334 | bool HasAVXType = false; |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2335 | for (CallArgList::const_iterator |
| 2336 | it = args.begin(), ie = args.end(); it != ie; ++it) { |
| 2337 | if (getABIInfo().isPassedUsingAVXType(it->Ty)) { |
| 2338 | HasAVXType = true; |
| 2339 | break; |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2340 | } |
| 2341 | } |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2342 | |
Eli Friedman | f37bd2f | 2011-12-01 04:53:19 +0000 | [diff] [blame] | 2343 | if (!HasAVXType) |
| 2344 | return true; |
| 2345 | } |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2346 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2347 | return TargetCodeGenInfo::isNoProtoCallVariadic(args, fnType); |
John McCall | cbc038a | 2011-09-21 08:08:30 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2350 | llvm::Constant * |
| 2351 | getUBSanFunctionSignature(CodeGen::CodeGenModule &CGM) const override { |
Vedant Kumar | bb5d485 | 2017-09-13 00:04:35 +0000 | [diff] [blame] | 2352 | unsigned Sig = (0xeb << 0) | // jmp rel8 |
| 2353 | (0x06 << 8) | // .+0x08 |
| 2354 | ('v' << 16) | |
| 2355 | ('2' << 24); |
Peter Collingbourne | b453cd6 | 2013-10-20 21:29:19 +0000 | [diff] [blame] | 2356 | return llvm::ConstantInt::get(CGM.Int32Ty, Sig); |
| 2357 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2358 | |
| 2359 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2360 | CodeGen::CodeGenModule &CGM) const override { |
| 2361 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2362 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2363 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2364 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2365 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2366 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2367 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2368 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2369 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2370 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2371 | } |
| 2372 | } |
| 2373 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2374 | }; |
| 2375 | |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2376 | static std::string qualifyWindowsLibrary(llvm::StringRef Lib) { |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2377 | // If the argument does not end in .lib, automatically add the suffix. |
| 2378 | // If the argument contains a space, enclose it in quotes. |
| 2379 | // This matches the behavior of MSVC. |
| 2380 | bool Quote = (Lib.find(" ") != StringRef::npos); |
| 2381 | std::string ArgStr = Quote ? "\"" : ""; |
| 2382 | ArgStr += Lib; |
Martin Storsjo | 3cd67c9 | 2018-10-10 09:01:00 +0000 | [diff] [blame] | 2383 | if (!Lib.endswith_lower(".lib") && !Lib.endswith_lower(".a")) |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2384 | ArgStr += ".lib"; |
Michael Kuperstein | f0e4ccf | 2015-02-16 11:57:43 +0000 | [diff] [blame] | 2385 | ArgStr += Quote ? "\"" : ""; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2386 | return ArgStr; |
| 2387 | } |
| 2388 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2389 | class WinX86_32TargetCodeGenInfo : public X86_32TargetCodeGenInfo { |
| 2390 | public: |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 2391 | WinX86_32TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 2392 | bool DarwinVectorABI, bool RetSmallStructInRegABI, bool Win32StructABI, |
| 2393 | unsigned NumRegisterParameters) |
| 2394 | : X86_32TargetCodeGenInfo(CGT, DarwinVectorABI, RetSmallStructInRegABI, |
Michael Kuperstein | b1ec50d | 2015-10-19 08:09:43 +0000 | [diff] [blame] | 2395 | Win32StructABI, NumRegisterParameters, false) {} |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2396 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2397 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2398 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2399 | |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2400 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2401 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2402 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2403 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2404 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2405 | |
| 2406 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2407 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2408 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2409 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2410 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2411 | }; |
| 2412 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2413 | static void addStackProbeTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 2414 | CodeGen::CodeGenModule &CGM) { |
| 2415 | if (llvm::Function *Fn = dyn_cast_or_null<llvm::Function>(GV)) { |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2416 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2417 | if (CGM.getCodeGenOpts().StackProbeSize != 4096) |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 2418 | Fn->addFnAttr("stack-probe-size", |
| 2419 | llvm::utostr(CGM.getCodeGenOpts().StackProbeSize)); |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2420 | if (CGM.getCodeGenOpts().NoStackArgProbe) |
| 2421 | Fn->addFnAttr("no-stack-arg-probe"); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2425 | void WinX86_32TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2426 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2427 | X86_32TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2428 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2429 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2430 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2433 | class WinX86_64TargetCodeGenInfo : public TargetCodeGenInfo { |
| 2434 | public: |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2435 | WinX86_64TargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, |
| 2436 | X86AVXABILevel AVXLevel) |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 2437 | : TargetCodeGenInfo(new WinX86_64ABIInfo(CGT, AVXLevel)) {} |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2438 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 2439 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2440 | CodeGen::CodeGenModule &CGM) const override; |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2441 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2442 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2443 | return 7; |
| 2444 | } |
| 2445 | |
| 2446 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2447 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2448 | llvm::Value *Eight8 = llvm::ConstantInt::get(CGF.Int8Ty, 8); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 2449 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2450 | // 0-15 are the 16 integer registers. |
| 2451 | // 16 is %rip. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 2452 | AssignToArrayRange(CGF.Builder, Address, Eight8, 0, 16); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2453 | return false; |
| 2454 | } |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2455 | |
| 2456 | void getDependentLibraryOption(llvm::StringRef Lib, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2457 | llvm::SmallString<24> &Opt) const override { |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2458 | Opt = "/DEFAULTLIB:"; |
Aaron Ballman | ef50ee9 | 2013-05-24 15:06:56 +0000 | [diff] [blame] | 2459 | Opt += qualifyWindowsLibrary(Lib); |
Reid Kleckner | e43f0fe | 2013-05-08 13:44:39 +0000 | [diff] [blame] | 2460 | } |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2461 | |
| 2462 | void getDetectMismatchOption(llvm::StringRef Name, |
| 2463 | llvm::StringRef Value, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2464 | llvm::SmallString<32> &Opt) const override { |
Eli Friedman | f60b8ce | 2013-06-07 22:42:22 +0000 | [diff] [blame] | 2465 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
Aaron Ballman | 5d041be | 2013-06-04 02:07:14 +0000 | [diff] [blame] | 2466 | } |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 2467 | }; |
| 2468 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2469 | void WinX86_64TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 2470 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 2471 | TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 2472 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 2473 | return; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2474 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2475 | if (FD->hasAttr<X86ForceAlignArgPointerAttr>()) { |
Erich Keane | b127a394 | 2018-04-19 14:27:05 +0000 | [diff] [blame] | 2476 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2477 | Fn->addFnAttr("stackrealign"); |
Erich Keane | bb9c704 | 2017-08-30 21:17:40 +0000 | [diff] [blame] | 2478 | } |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 2479 | if (FD->hasAttr<AnyX86InterruptAttr>()) { |
| 2480 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 2481 | Fn->setCallingConv(llvm::CallingConv::X86_INTR); |
| 2482 | } |
| 2483 | } |
| 2484 | |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 2485 | addStackProbeTargetAttributes(D, GV, CGM); |
Hans Wennborg | 77dc236 | 2015-01-20 19:45:50 +0000 | [diff] [blame] | 2486 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2487 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2488 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2489 | void X86_64ABIInfo::postMerge(unsigned AggregateSize, Class &Lo, |
| 2490 | Class &Hi) const { |
| 2491 | // AMD64-ABI 3.2.3p2: Rule 5. Then a post merger cleanup is done: |
| 2492 | // |
| 2493 | // (a) If one of the classes is Memory, the whole argument is passed in |
| 2494 | // memory. |
| 2495 | // |
| 2496 | // (b) If X87UP is not preceded by X87, the whole argument is passed in |
| 2497 | // memory. |
| 2498 | // |
| 2499 | // (c) If the size of the aggregate exceeds two eightbytes and the first |
| 2500 | // eightbyte isn't SSE or any other eightbyte isn't SSEUP, the whole |
| 2501 | // argument is passed in memory. NOTE: This is necessary to keep the |
| 2502 | // ABI working for processors that don't support the __m256 type. |
| 2503 | // |
| 2504 | // (d) If SSEUP is not preceded by SSE or SSEUP, it is converted to SSE. |
| 2505 | // |
| 2506 | // Some of these are enforced by the merging logic. Others can arise |
| 2507 | // only with unions; for example: |
| 2508 | // union { _Complex double; unsigned; } |
| 2509 | // |
| 2510 | // Note that clauses (b) and (c) were added in 0.98. |
| 2511 | // |
| 2512 | if (Hi == Memory) |
| 2513 | Lo = Memory; |
| 2514 | if (Hi == X87Up && Lo != X87 && honorsRevision0_98()) |
| 2515 | Lo = Memory; |
| 2516 | if (AggregateSize > 128 && (Lo != SSE || Hi != SSEUp)) |
| 2517 | Lo = Memory; |
| 2518 | if (Hi == SSEUp && Lo != SSE) |
| 2519 | Hi = SSE; |
| 2520 | } |
| 2521 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2522 | X86_64ABIInfo::Class X86_64ABIInfo::merge(Class Accum, Class Field) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2523 | // AMD64-ABI 3.2.3p2: Rule 4. Each field of an object is |
| 2524 | // classified recursively so that always two fields are |
| 2525 | // considered. The resulting class is calculated according to |
| 2526 | // the classes of the fields in the eightbyte: |
| 2527 | // |
| 2528 | // (a) If both classes are equal, this is the resulting class. |
| 2529 | // |
| 2530 | // (b) If one of the classes is NO_CLASS, the resulting class is |
| 2531 | // the other class. |
| 2532 | // |
| 2533 | // (c) If one of the classes is MEMORY, the result is the MEMORY |
| 2534 | // class. |
| 2535 | // |
| 2536 | // (d) If one of the classes is INTEGER, the result is the |
| 2537 | // INTEGER. |
| 2538 | // |
| 2539 | // (e) If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| 2540 | // MEMORY is used as class. |
| 2541 | // |
| 2542 | // (f) Otherwise class SSE is used. |
| 2543 | |
| 2544 | // Accum should never be memory (we should have returned) or |
| 2545 | // ComplexX87 (because this cannot be passed in a structure). |
| 2546 | assert((Accum != Memory && Accum != ComplexX87) && |
| 2547 | "Invalid accumulated classification during merge."); |
| 2548 | if (Accum == Field || Field == NoClass) |
| 2549 | return Accum; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2550 | if (Field == Memory) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2551 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2552 | if (Accum == NoClass) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2553 | return Field; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2554 | if (Accum == Integer || Field == Integer) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2555 | return Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2556 | if (Field == X87 || Field == X87Up || Field == ComplexX87 || |
| 2557 | Accum == X87 || Accum == X87Up) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2558 | return Memory; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2559 | return SSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Chris Lattner | 5c740f1 | 2010-06-30 19:14:05 +0000 | [diff] [blame] | 2562 | void X86_64ABIInfo::classify(QualType Ty, uint64_t OffsetBase, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2563 | Class &Lo, Class &Hi, bool isNamedArg) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2564 | // FIXME: This code can be simplified by introducing a simple value class for |
| 2565 | // Class pairs with appropriate constructor methods for the various |
| 2566 | // situations. |
| 2567 | |
| 2568 | // FIXME: Some of the split computations are wrong; unaligned vectors |
| 2569 | // shouldn't be passed in registers for example, so there is no chance they |
| 2570 | // can straddle an eightbyte. Verify & simplify. |
| 2571 | |
| 2572 | Lo = Hi = NoClass; |
| 2573 | |
| 2574 | Class &Current = OffsetBase < 64 ? Lo : Hi; |
| 2575 | Current = Memory; |
| 2576 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2577 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2578 | BuiltinType::Kind k = BT->getKind(); |
| 2579 | |
| 2580 | if (k == BuiltinType::Void) { |
| 2581 | Current = NoClass; |
| 2582 | } else if (k == BuiltinType::Int128 || k == BuiltinType::UInt128) { |
| 2583 | Lo = Integer; |
| 2584 | Hi = Integer; |
| 2585 | } else if (k >= BuiltinType::Bool && k <= BuiltinType::LongLong) { |
| 2586 | Current = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2587 | } else if (k == BuiltinType::Float || k == BuiltinType::Double) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2588 | Current = SSE; |
| 2589 | } else if (k == BuiltinType::LongDouble) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2590 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2591 | if (LDF == &llvm::APFloat::IEEEquad()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2592 | Lo = SSE; |
| 2593 | Hi = SSEUp; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2594 | } else if (LDF == &llvm::APFloat::x87DoubleExtended()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2595 | Lo = X87; |
| 2596 | Hi = X87Up; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2597 | } else if (LDF == &llvm::APFloat::IEEEdouble()) { |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2598 | Current = SSE; |
| 2599 | } else |
| 2600 | llvm_unreachable("unexpected long double representation!"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2601 | } |
| 2602 | // FIXME: _Decimal32 and _Decimal64 are SSE. |
| 2603 | // FIXME: _float128 and _Decimal128 are (SSE, SSEUp). |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2604 | return; |
| 2605 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2607 | if (const EnumType *ET = Ty->getAs<EnumType>()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2608 | // Classify the underlying integer type. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2609 | classify(ET->getDecl()->getIntegerType(), OffsetBase, Lo, Hi, isNamedArg); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2610 | return; |
| 2611 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2612 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2613 | if (Ty->hasPointerRepresentation()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2614 | Current = Integer; |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2615 | return; |
| 2616 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2617 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2618 | if (Ty->isMemberPointerType()) { |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2619 | if (Ty->isMemberFunctionPointerType()) { |
| 2620 | if (Has64BitPointers) { |
| 2621 | // If Has64BitPointers, this is an {i64, i64}, so classify both |
| 2622 | // Lo and Hi now. |
| 2623 | Lo = Hi = Integer; |
| 2624 | } else { |
| 2625 | // Otherwise, with 32-bit pointers, this is an {i32, i32}. If that |
| 2626 | // straddles an eightbyte boundary, Hi should be classified as well. |
| 2627 | uint64_t EB_FuncPtr = (OffsetBase) / 64; |
| 2628 | uint64_t EB_ThisAdj = (OffsetBase + 64 - 1) / 64; |
| 2629 | if (EB_FuncPtr != EB_ThisAdj) { |
| 2630 | Lo = Hi = Integer; |
| 2631 | } else { |
| 2632 | Current = Integer; |
| 2633 | } |
| 2634 | } |
| 2635 | } else { |
Daniel Dunbar | 36d4d15 | 2010-05-15 00:00:37 +0000 | [diff] [blame] | 2636 | Current = Integer; |
Jan Wen Voung | 01c21e8 | 2014-10-02 16:56:57 +0000 | [diff] [blame] | 2637 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2638 | return; |
| 2639 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2640 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2641 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2642 | uint64_t Size = getContext().getTypeSize(VT); |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2643 | if (Size == 1 || Size == 8 || Size == 16 || Size == 32) { |
| 2644 | // gcc passes the following as integer: |
| 2645 | // 4 bytes - <4 x char>, <2 x short>, <1 x int>, <1 x float> |
| 2646 | // 2 bytes - <2 x char>, <1 x short> |
| 2647 | // 1 byte - <1 x char> |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2648 | Current = Integer; |
| 2649 | |
| 2650 | // If this type crosses an eightbyte boundary, it should be |
| 2651 | // split. |
David Majnemer | f8d14db | 2015-07-17 05:49:13 +0000 | [diff] [blame] | 2652 | uint64_t EB_Lo = (OffsetBase) / 64; |
| 2653 | uint64_t EB_Hi = (OffsetBase + Size - 1) / 64; |
| 2654 | if (EB_Lo != EB_Hi) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2655 | Hi = Lo; |
| 2656 | } else if (Size == 64) { |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2657 | QualType ElementType = VT->getElementType(); |
| 2658 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2659 | // gcc passes <1 x double> in memory. :( |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2660 | if (ElementType->isSpecificBuiltinType(BuiltinType::Double)) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2661 | return; |
| 2662 | |
David Majnemer | e2ae228 | 2016-03-04 05:26:16 +0000 | [diff] [blame] | 2663 | // gcc passes <1 x long long> as SSE but clang used to unconditionally |
| 2664 | // pass them as integer. For platforms where clang is the de facto |
| 2665 | // platform compiler, we must continue to use integer. |
| 2666 | if (!classifyIntegerMMXAsSSE() && |
| 2667 | (ElementType->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2668 | ElementType->isSpecificBuiltinType(BuiltinType::ULongLong) || |
| 2669 | ElementType->isSpecificBuiltinType(BuiltinType::Long) || |
| 2670 | ElementType->isSpecificBuiltinType(BuiltinType::ULong))) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2671 | Current = Integer; |
| 2672 | else |
| 2673 | Current = SSE; |
| 2674 | |
| 2675 | // If this type crosses an eightbyte boundary, it should be |
| 2676 | // split. |
| 2677 | if (OffsetBase && OffsetBase != 64) |
| 2678 | Hi = Lo; |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2679 | } else if (Size == 128 || |
| 2680 | (isNamedArg && Size <= getNativeVectorSizeForAVXABI(AVXLevel))) { |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 2681 | QualType ElementType = VT->getElementType(); |
| 2682 | |
| 2683 | // gcc passes 256 and 512 bit <X x __int128> vectors in memory. :( |
| 2684 | if (passInt128VectorsInMem() && Size != 128 && |
| 2685 | (ElementType->isSpecificBuiltinType(BuiltinType::Int128) || |
| 2686 | ElementType->isSpecificBuiltinType(BuiltinType::UInt128))) |
| 2687 | return; |
| 2688 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2689 | // Arguments of 256-bits are split into four eightbyte chunks. The |
| 2690 | // least significant one belongs to class SSE and all the others to class |
| 2691 | // SSEUP. The original Lo and Hi design considers that types can't be |
| 2692 | // greater than 128-bits, so a 64-bit split in Hi and Lo makes sense. |
| 2693 | // This design isn't correct for 256-bits, but since there're no cases |
| 2694 | // where the upper parts would need to be inspected, avoid adding |
| 2695 | // complexity and just consider Hi to match the 64-256 part. |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2696 | // |
| 2697 | // Note that per 3.5.7 of AMD64-ABI, 256-bit args are only passed in |
| 2698 | // registers if they are "named", i.e. not part of the "..." of a |
| 2699 | // variadic function. |
Ahmed Bougacha | 0b93828 | 2015-06-22 21:31:43 +0000 | [diff] [blame] | 2700 | // |
| 2701 | // Similarly, per 3.2.3. of the AVX512 draft, 512-bits ("named") args are |
| 2702 | // split into eight eightbyte chunks, one SSE and seven SSEUP. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2703 | Lo = SSE; |
| 2704 | Hi = SSEUp; |
| 2705 | } |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2706 | return; |
| 2707 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2708 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2709 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2710 | QualType ET = getContext().getCanonicalType(CT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2711 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2712 | uint64_t Size = getContext().getTypeSize(Ty); |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2713 | if (ET->isIntegralOrEnumerationType()) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2714 | if (Size <= 64) |
| 2715 | Current = Integer; |
| 2716 | else if (Size <= 128) |
| 2717 | Lo = Hi = Integer; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2718 | } else if (ET == getContext().FloatTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2719 | Current = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2720 | } else if (ET == getContext().DoubleTy) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2721 | Lo = Hi = SSE; |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2722 | } else if (ET == getContext().LongDoubleTy) { |
| 2723 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2724 | if (LDF == &llvm::APFloat::IEEEquad()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2725 | Current = Memory; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2726 | else if (LDF == &llvm::APFloat::x87DoubleExtended()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2727 | Current = ComplexX87; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 2728 | else if (LDF == &llvm::APFloat::IEEEdouble()) |
Chih-Hung Hsieh | 241a890 | 2015-08-10 17:33:31 +0000 | [diff] [blame] | 2729 | Lo = Hi = SSE; |
| 2730 | else |
| 2731 | llvm_unreachable("unexpected long double representation!"); |
| 2732 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2733 | |
| 2734 | // If this complex type crosses an eightbyte boundary then it |
| 2735 | // should be split. |
| 2736 | uint64_t EB_Real = (OffsetBase) / 64; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2737 | uint64_t EB_Imag = (OffsetBase + getContext().getTypeSize(ET)) / 64; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2738 | if (Hi == NoClass && EB_Real != EB_Imag) |
| 2739 | Hi = Lo; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2740 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2741 | return; |
| 2742 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2743 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2744 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2745 | // Arrays are treated like structures. |
| 2746 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2747 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2748 | |
| 2749 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2750 | // than eight eightbytes, ..., it has class MEMORY. |
| 2751 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2752 | return; |
| 2753 | |
| 2754 | // AMD64-ABI 3.2.3p2: Rule 1. If ..., or it contains unaligned |
| 2755 | // fields, it has class MEMORY. |
| 2756 | // |
| 2757 | // Only need to check alignment of array base. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2758 | if (OffsetBase % getContext().getTypeAlign(AT->getElementType())) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | |
| 2761 | // Otherwise implement simplified merge. We could be smarter about |
| 2762 | // this, but it isn't worth it and would be harder to verify. |
| 2763 | Current = NoClass; |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2764 | uint64_t EltSize = getContext().getTypeSize(AT->getElementType()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2765 | uint64_t ArraySize = AT->getSize().getZExtValue(); |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2766 | |
| 2767 | // The only case a 256-bit wide vector could be used is when the array |
| 2768 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2769 | // to work for sizes wider than 128, early check and fallback to memory. |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2770 | // |
| 2771 | if (Size > 128 && |
| 2772 | (Size != EltSize || Size > getNativeVectorSizeForAVXABI(AVXLevel))) |
Bruno Cardoso Lopes | 75541d0 | 2011-07-12 01:27:38 +0000 | [diff] [blame] | 2773 | return; |
| 2774 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2775 | for (uint64_t i=0, Offset=OffsetBase; i<ArraySize; ++i, Offset += EltSize) { |
| 2776 | Class FieldLo, FieldHi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2777 | classify(AT->getElementType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2778 | Lo = merge(Lo, FieldLo); |
| 2779 | Hi = merge(Hi, FieldHi); |
| 2780 | if (Lo == Memory || Hi == Memory) |
| 2781 | break; |
| 2782 | } |
| 2783 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2784 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2785 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp array classification."); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2786 | return; |
| 2787 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 2788 | |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 2789 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2790 | uint64_t Size = getContext().getTypeSize(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2791 | |
| 2792 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2793 | // than eight eightbytes, ..., it has class MEMORY. |
| 2794 | if (Size > 512) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2795 | return; |
| 2796 | |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2797 | // AMD64-ABI 3.2.3p2: Rule 2. If a C++ object has either a non-trivial |
| 2798 | // copy constructor or a non-trivial destructor, it is passed by invisible |
| 2799 | // reference. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2800 | if (getRecordArgABI(RT, getCXXABI())) |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2801 | return; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2802 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2803 | const RecordDecl *RD = RT->getDecl(); |
| 2804 | |
| 2805 | // Assume variable sized types are passed in memory. |
| 2806 | if (RD->hasFlexibleArrayMember()) |
| 2807 | return; |
| 2808 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2809 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2810 | |
| 2811 | // Reset Lo class, this will be recomputed. |
| 2812 | Current = NoClass; |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2813 | |
| 2814 | // If this is a C++ record, classify the bases first. |
| 2815 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2816 | for (const auto &I : CXXRD->bases()) { |
| 2817 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2818 | "Unexpected base class!"); |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 2819 | const auto *Base = |
| 2820 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2821 | |
| 2822 | // Classify this field. |
| 2823 | // |
| 2824 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate exceeds a |
| 2825 | // single eightbyte, each is classified separately. Each eightbyte gets |
| 2826 | // initialized to class NO_CLASS. |
| 2827 | Class FieldLo, FieldHi; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 2828 | uint64_t Offset = |
| 2829 | OffsetBase + getContext().toBits(Layout.getBaseClassOffset(Base)); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2830 | classify(I.getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2831 | Lo = merge(Lo, FieldLo); |
| 2832 | Hi = merge(Hi, FieldHi); |
David Majnemer | cefbc7c | 2015-07-08 05:14:29 +0000 | [diff] [blame] | 2833 | if (Lo == Memory || Hi == Memory) { |
| 2834 | postMerge(Size, Lo, Hi); |
| 2835 | return; |
| 2836 | } |
Daniel Dunbar | e1cd015 | 2009-11-22 23:01:23 +0000 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | // Classify the fields one at a time, merging the results. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2841 | unsigned idx = 0; |
Bruno Cardoso Lopes | 0aadf83 | 2011-07-12 22:30:58 +0000 | [diff] [blame] | 2842 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2843 | i != e; ++i, ++idx) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2844 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
| 2845 | bool BitField = i->isBitField(); |
| 2846 | |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2847 | // Ignore padding bit-fields. |
| 2848 | if (BitField && i->isUnnamedBitfield()) |
| 2849 | continue; |
| 2850 | |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2851 | // AMD64-ABI 3.2.3p2: Rule 1. If the size of an object is larger than |
| 2852 | // four eightbytes, or it contains unaligned fields, it has class MEMORY. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2853 | // |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2854 | // The only case a 256-bit wide vector could be used is when the struct |
| 2855 | // contains a single 256-bit element. Since Lo and Hi logic isn't extended |
| 2856 | // to work for sizes wider than 128, early check and fallback to memory. |
| 2857 | // |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 2858 | if (Size > 128 && (Size != getContext().getTypeSize(i->getType()) || |
| 2859 | Size > getNativeVectorSizeForAVXABI(AVXLevel))) { |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2860 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2861 | postMerge(Size, Lo, Hi); |
Bruno Cardoso Lopes | 98154a7 | 2011-07-13 21:58:55 +0000 | [diff] [blame] | 2862 | return; |
| 2863 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2864 | // Note, skip this test for bit-fields, see below. |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 2865 | if (!BitField && Offset % getContext().getTypeAlign(i->getType())) { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2866 | Lo = Memory; |
David Majnemer | 699dd04 | 2015-07-08 05:07:05 +0000 | [diff] [blame] | 2867 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2868 | return; |
| 2869 | } |
| 2870 | |
| 2871 | // Classify this field. |
| 2872 | // |
| 2873 | // AMD64-ABI 3.2.3p2: Rule 3. If the size of the aggregate |
| 2874 | // exceeds a single eightbyte, each is classified |
| 2875 | // separately. Each eightbyte gets initialized to class |
| 2876 | // NO_CLASS. |
| 2877 | Class FieldLo, FieldHi; |
| 2878 | |
| 2879 | // Bit-fields require special handling, they do not force the |
| 2880 | // structure to be passed in memory even if unaligned, and |
| 2881 | // therefore they can straddle an eightbyte. |
| 2882 | if (BitField) { |
David Majnemer | b439dfe | 2016-08-15 07:20:40 +0000 | [diff] [blame] | 2883 | assert(!i->isUnnamedBitfield()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2884 | uint64_t Offset = OffsetBase + Layout.getFieldOffset(idx); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2885 | uint64_t Size = i->getBitWidthValue(getContext()); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2886 | |
| 2887 | uint64_t EB_Lo = Offset / 64; |
| 2888 | uint64_t EB_Hi = (Offset + Size - 1) / 64; |
Sylvestre Ledru | 0c4813e | 2013-10-06 09:54:18 +0000 | [diff] [blame] | 2889 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2890 | if (EB_Lo) { |
| 2891 | assert(EB_Hi == EB_Lo && "Invalid classification, type > 16 bytes."); |
| 2892 | FieldLo = NoClass; |
| 2893 | FieldHi = Integer; |
| 2894 | } else { |
| 2895 | FieldLo = Integer; |
| 2896 | FieldHi = EB_Hi ? Integer : NoClass; |
| 2897 | } |
| 2898 | } else |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 2899 | classify(i->getType(), Offset, FieldLo, FieldHi, isNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2900 | Lo = merge(Lo, FieldLo); |
| 2901 | Hi = merge(Hi, FieldHi); |
| 2902 | if (Lo == Memory || Hi == Memory) |
| 2903 | break; |
| 2904 | } |
| 2905 | |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 2906 | postMerge(Size, Lo, Hi); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 2910 | ABIArgInfo X86_64ABIInfo::getIndirectReturnResult(QualType Ty) const { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2911 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2912 | // place naturally. |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 2913 | if (!isAggregateTypeForABI(Ty)) { |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2914 | // Treat an enum type as its underlying type. |
| 2915 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2916 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2917 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2918 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2919 | : ABIArgInfo::getDirect()); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2920 | } |
| 2921 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2922 | return getNaturalAlignIndirect(Ty); |
Daniel Dunbar | 53fac69 | 2010-04-21 19:49:55 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2925 | bool X86_64ABIInfo::IsIllegalVectorType(QualType Ty) const { |
| 2926 | if (const VectorType *VecTy = Ty->getAs<VectorType>()) { |
| 2927 | uint64_t Size = getContext().getTypeSize(VecTy); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 2928 | unsigned LargestVector = getNativeVectorSizeForAVXABI(AVXLevel); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2929 | if (Size <= 64 || Size > LargestVector) |
| 2930 | return true; |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 2931 | QualType EltTy = VecTy->getElementType(); |
| 2932 | if (passInt128VectorsInMem() && |
| 2933 | (EltTy->isSpecificBuiltinType(BuiltinType::Int128) || |
| 2934 | EltTy->isSpecificBuiltinType(BuiltinType::UInt128))) |
| 2935 | return true; |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | return false; |
| 2939 | } |
| 2940 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2941 | ABIArgInfo X86_64ABIInfo::getIndirectResult(QualType Ty, |
| 2942 | unsigned freeIntRegs) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2943 | // If this is a scalar LLVM value then assume LLVM will pass it in the right |
| 2944 | // place naturally. |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2945 | // |
| 2946 | // This assumption is optimistic, as there could be free registers available |
| 2947 | // when we need to pass this argument in memory, and LLVM could try to pass |
| 2948 | // the argument in the free register. This does not seem to happen currently, |
| 2949 | // but this code would be much safer if we could mark the argument with |
| 2950 | // 'onstack'. See PR12193. |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 2951 | if (!isAggregateTypeForABI(Ty) && !IsIllegalVectorType(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2952 | // Treat an enum type as its underlying type. |
| 2953 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 2954 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 2955 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 2956 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 2957 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 2958 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2959 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 2960 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2961 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Anders Carlsson | 20759ad | 2009-09-16 15:53:40 +0000 | [diff] [blame] | 2962 | |
Chris Lattner | 44c2b90 | 2011-05-22 23:21:23 +0000 | [diff] [blame] | 2963 | // Compute the byval alignment. We specify the alignment of the byval in all |
| 2964 | // cases so that the mid-level optimizer knows the alignment of the byval. |
| 2965 | unsigned Align = std::max(getContext().getTypeAlign(Ty) / 8, 8U); |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 2966 | |
| 2967 | // Attempt to avoid passing indirect results using byval when possible. This |
| 2968 | // is important for good codegen. |
| 2969 | // |
| 2970 | // We do this by coercing the value into a scalar type which the backend can |
| 2971 | // handle naturally (i.e., without using byval). |
| 2972 | // |
| 2973 | // For simplicity, we currently only do this when we have exhausted all of the |
| 2974 | // free integer registers. Doing this when there are free integer registers |
| 2975 | // would require more care, as we would have to ensure that the coerced value |
| 2976 | // did not claim the unused register. That would require either reording the |
| 2977 | // arguments to the function (so that any subsequent inreg values came first), |
| 2978 | // or only doing this optimization when there were no following arguments that |
| 2979 | // might be inreg. |
| 2980 | // |
| 2981 | // We currently expect it to be rare (particularly in well written code) for |
| 2982 | // arguments to be passed on the stack when there are still free integer |
| 2983 | // registers available (this would typically imply large structs being passed |
| 2984 | // by value), so this seems like a fair tradeoff for now. |
| 2985 | // |
| 2986 | // We can revisit this if the backend grows support for 'onstack' parameter |
| 2987 | // attributes. See PR12193. |
| 2988 | if (freeIntRegs == 0) { |
| 2989 | uint64_t Size = getContext().getTypeSize(Ty); |
| 2990 | |
| 2991 | // If this type fits in an eightbyte, coerce it into the matching integral |
| 2992 | // type, which will end up on the stack (with alignment 8). |
| 2993 | if (Align == 8 && Size <= 64) |
| 2994 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), |
| 2995 | Size)); |
| 2996 | } |
| 2997 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2998 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(Align)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 3001 | /// The ABI specifies that a value should be passed in a full vector XMM/YMM |
| 3002 | /// register. Pick an LLVM IR type that will be passed as a vector register. |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3003 | llvm::Type *X86_64ABIInfo::GetByteVectorType(QualType Ty) const { |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 3004 | // Wrapper structs/arrays that only contain vectors are passed just like |
| 3005 | // vectors; strip them off if present. |
| 3006 | if (const Type *InnerTy = isSingleElementStruct(Ty, getContext())) |
| 3007 | Ty = QualType(InnerTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3008 | |
Sanjay Patel | eb2af4e | 2015-02-16 17:26:51 +0000 | [diff] [blame] | 3009 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 3010 | if (isa<llvm::VectorType>(IRType)) { |
| 3011 | // Don't pass vXi128 vectors in their native type, the backend can't |
| 3012 | // legalize them. |
| 3013 | if (passInt128VectorsInMem() && |
| 3014 | IRType->getVectorElementType()->isIntegerTy(128)) { |
| 3015 | // Use a vXi64 vector. |
| 3016 | uint64_t Size = getContext().getTypeSize(Ty); |
| 3017 | return llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), |
| 3018 | Size / 64); |
| 3019 | } |
| 3020 | |
| 3021 | return IRType; |
| 3022 | } |
| 3023 | |
| 3024 | if (IRType->getTypeID() == llvm::Type::FP128TyID) |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 3025 | return IRType; |
| 3026 | |
| 3027 | // We couldn't find the preferred IR vector type for 'Ty'. |
| 3028 | uint64_t Size = getContext().getTypeSize(Ty); |
David Majnemer | b229cb0 | 2016-08-15 06:39:18 +0000 | [diff] [blame] | 3029 | assert((Size == 128 || Size == 256 || Size == 512) && "Invalid type found!"); |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 3030 | |
Craig Topper | 6c8a34e | 2019-09-06 06:02:13 +0000 | [diff] [blame] | 3031 | |
Andrea Di Biagio | e7347c6 | 2015-06-02 19:34:40 +0000 | [diff] [blame] | 3032 | // Return a LLVM IR vector type based on the size of 'Ty'. |
| 3033 | return llvm::VectorType::get(llvm::Type::getDoubleTy(getVMContext()), |
| 3034 | Size / 64); |
Chris Lattner | 4200fe4 | 2010-07-29 04:56:46 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3037 | /// BitsContainNoUserData - Return true if the specified [start,end) bit range |
| 3038 | /// is known to either be off the end of the specified type or being in |
| 3039 | /// alignment padding. The user type specified is known to be at most 128 bits |
| 3040 | /// in size, and have passed through X86_64ABIInfo::classify with a successful |
| 3041 | /// classification that put one of the two halves in the INTEGER class. |
| 3042 | /// |
| 3043 | /// It is conservatively correct to return false. |
| 3044 | static bool BitsContainNoUserData(QualType Ty, unsigned StartBit, |
| 3045 | unsigned EndBit, ASTContext &Context) { |
| 3046 | // If the bytes being queried are off the end of the type, there is no user |
| 3047 | // data hiding here. This handles analysis of builtins, vectors and other |
| 3048 | // types that don't contain interesting padding. |
| 3049 | unsigned TySize = (unsigned)Context.getTypeSize(Ty); |
| 3050 | if (TySize <= StartBit) |
| 3051 | return true; |
| 3052 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3053 | if (const ConstantArrayType *AT = Context.getAsConstantArrayType(Ty)) { |
| 3054 | unsigned EltSize = (unsigned)Context.getTypeSize(AT->getElementType()); |
| 3055 | unsigned NumElts = (unsigned)AT->getSize().getZExtValue(); |
| 3056 | |
| 3057 | // Check each element to see if the element overlaps with the queried range. |
| 3058 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3059 | // If the element is after the span we care about, then we're done.. |
| 3060 | unsigned EltOffset = i*EltSize; |
| 3061 | if (EltOffset >= EndBit) break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3062 | |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3063 | unsigned EltStart = EltOffset < StartBit ? StartBit-EltOffset :0; |
| 3064 | if (!BitsContainNoUserData(AT->getElementType(), EltStart, |
| 3065 | EndBit-EltOffset, Context)) |
| 3066 | return false; |
| 3067 | } |
| 3068 | // If it overlaps no elements, then it is safe to process as padding. |
| 3069 | return true; |
| 3070 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3072 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 3073 | const RecordDecl *RD = RT->getDecl(); |
| 3074 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3075 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3076 | // If this is a C++ record, check the bases first. |
| 3077 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3078 | for (const auto &I : CXXRD->bases()) { |
| 3079 | assert(!I.isVirtual() && !I.getType()->isDependentType() && |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3080 | "Unexpected base class!"); |
Simon Pilgrim | 1cd399c | 2019-10-03 11:22:48 +0000 | [diff] [blame] | 3081 | const auto *Base = |
| 3082 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3083 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3084 | // If the base is after the span we care about, ignore it. |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 3085 | unsigned BaseOffset = Context.toBits(Layout.getBaseClassOffset(Base)); |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3086 | if (BaseOffset >= EndBit) continue; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3087 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3088 | unsigned BaseStart = BaseOffset < StartBit ? StartBit-BaseOffset :0; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 3089 | if (!BitsContainNoUserData(I.getType(), BaseStart, |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3090 | EndBit-BaseOffset, Context)) |
| 3091 | return false; |
| 3092 | } |
| 3093 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3094 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3095 | // Verify that no field has data that overlaps the region of interest. Yes |
| 3096 | // this could be sped up a lot by being smarter about queried fields, |
| 3097 | // however we're only looking at structs up to 16 bytes, so we don't care |
| 3098 | // much. |
| 3099 | unsigned idx = 0; |
| 3100 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 3101 | i != e; ++i, ++idx) { |
| 3102 | unsigned FieldOffset = (unsigned)Layout.getFieldOffset(idx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3103 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3104 | // If we found a field after the region we care about, then we're done. |
| 3105 | if (FieldOffset >= EndBit) break; |
| 3106 | |
| 3107 | unsigned FieldStart = FieldOffset < StartBit ? StartBit-FieldOffset :0; |
| 3108 | if (!BitsContainNoUserData(i->getType(), FieldStart, EndBit-FieldOffset, |
| 3109 | Context)) |
| 3110 | return false; |
| 3111 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3112 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3113 | // If nothing in this record overlapped the area of interest, then we're |
| 3114 | // clean. |
| 3115 | return true; |
| 3116 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3118 | return false; |
| 3119 | } |
| 3120 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3121 | /// ContainsFloatAtOffset - Return true if the specified LLVM IR type has a |
| 3122 | /// float member at the specified offset. For example, {int,{float}} has a |
| 3123 | /// float at offset 4. It is conservatively correct for this routine to return |
| 3124 | /// false. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3125 | static bool ContainsFloatAtOffset(llvm::Type *IRType, unsigned IROffset, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3126 | const llvm::DataLayout &TD) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3127 | // Base case if we find a float. |
| 3128 | if (IROffset == 0 && IRType->isFloatTy()) |
| 3129 | return true; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3130 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3131 | // 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] | 3132 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3133 | const llvm::StructLayout *SL = TD.getStructLayout(STy); |
| 3134 | unsigned Elt = SL->getElementContainingOffset(IROffset); |
| 3135 | IROffset -= SL->getElementOffset(Elt); |
| 3136 | return ContainsFloatAtOffset(STy->getElementType(Elt), IROffset, TD); |
| 3137 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3138 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3139 | // 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] | 3140 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
| 3141 | llvm::Type *EltTy = ATy->getElementType(); |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3142 | unsigned EltSize = TD.getTypeAllocSize(EltTy); |
| 3143 | IROffset -= IROffset/EltSize*EltSize; |
| 3144 | return ContainsFloatAtOffset(EltTy, IROffset, TD); |
| 3145 | } |
| 3146 | |
| 3147 | return false; |
| 3148 | } |
| 3149 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3150 | |
| 3151 | /// GetSSETypeAtOffset - Return a type that will be passed by the backend in the |
| 3152 | /// low 8 bytes of an XMM register, corresponding to the SSE class. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3153 | llvm::Type *X86_64ABIInfo:: |
| 3154 | GetSSETypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3155 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | 50a357e | 2010-07-29 18:19:50 +0000 | [diff] [blame] | 3156 | // 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] | 3157 | // pass as float if the last 4 bytes is just padding. This happens for |
| 3158 | // structs that contain 3 floats. |
| 3159 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+32, |
| 3160 | SourceOffset*8+64, getContext())) |
| 3161 | return llvm::Type::getFloatTy(getVMContext()); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3162 | |
Chris Lattner | e556a71 | 2010-07-29 18:39:32 +0000 | [diff] [blame] | 3163 | // We want to pass as <2 x float> if the LLVM IR type contains a float at |
| 3164 | // offset+0 and offset+4. Walk the LLVM IR type to find out if this is the |
| 3165 | // case. |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3166 | if (ContainsFloatAtOffset(IRType, IROffset, getDataLayout()) && |
| 3167 | ContainsFloatAtOffset(IRType, IROffset+4, getDataLayout())) |
Chris Lattner | 9f8b451 | 2010-08-25 23:39:14 +0000 | [diff] [blame] | 3168 | return llvm::VectorType::get(llvm::Type::getFloatTy(getVMContext()), 2); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3169 | |
Chris Lattner | 7f4b81a | 2010-07-29 18:13:09 +0000 | [diff] [blame] | 3170 | return llvm::Type::getDoubleTy(getVMContext()); |
| 3171 | } |
| 3172 | |
| 3173 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3174 | /// GetINTEGERTypeAtOffset - The ABI specifies that a value should be passed in |
| 3175 | /// an 8-byte GPR. This means that we either have a scalar or we are talking |
| 3176 | /// about the high or low part of an up-to-16-byte struct. This routine picks |
| 3177 | /// 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] | 3178 | /// else that the backend will pass in a GPR that works better (e.g. i8, %foo*, |
| 3179 | /// etc). |
| 3180 | /// |
| 3181 | /// PrefType is an LLVM IR type that corresponds to (part of) the IR type for |
| 3182 | /// the source type. IROffset is an offset in bytes into the LLVM IR type that |
| 3183 | /// the 8-byte value references. PrefType may be null. |
| 3184 | /// |
Alp Toker | 9907f08 | 2014-07-09 14:06:35 +0000 | [diff] [blame] | 3185 | /// SourceTy is the source-level type for the entire argument. SourceOffset is |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3186 | /// an offset into this that we're processing (which is always either 0 or 8). |
| 3187 | /// |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3188 | llvm::Type *X86_64ABIInfo:: |
| 3189 | GetINTEGERTypeAtOffset(llvm::Type *IRType, unsigned IROffset, |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3190 | QualType SourceTy, unsigned SourceOffset) const { |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3191 | // If we're dealing with an un-offset LLVM IR type, then it means that we're |
| 3192 | // returning an 8-byte unit starting with it. See if we can safely use it. |
| 3193 | if (IROffset == 0) { |
| 3194 | // Pointers and int64's always fill the 8-byte unit. |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3195 | if ((isa<llvm::PointerType>(IRType) && Has64BitPointers) || |
| 3196 | IRType->isIntegerTy(64)) |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3197 | return IRType; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3198 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3199 | // If we have a 1/2/4-byte integer, we can use it only if the rest of the |
| 3200 | // goodness in the source type is just tail padding. This is allowed to |
| 3201 | // kick in for struct {double,int} on the int, but not on |
| 3202 | // struct{double,int,int} because we wouldn't return the second int. We |
| 3203 | // have to do this analysis on the source type because we can't depend on |
| 3204 | // unions being lowered a specific way etc. |
| 3205 | if (IRType->isIntegerTy(8) || IRType->isIntegerTy(16) || |
Derek Schuff | c7dd722 | 2012-10-11 15:52:22 +0000 | [diff] [blame] | 3206 | IRType->isIntegerTy(32) || |
| 3207 | (isa<llvm::PointerType>(IRType) && !Has64BitPointers)) { |
| 3208 | unsigned BitWidth = isa<llvm::PointerType>(IRType) ? 32 : |
| 3209 | cast<llvm::IntegerType>(IRType)->getBitWidth(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3210 | |
Chris Lattner | c8b7b53 | 2010-07-29 07:30:00 +0000 | [diff] [blame] | 3211 | if (BitsContainNoUserData(SourceTy, SourceOffset*8+BitWidth, |
| 3212 | SourceOffset*8+64, getContext())) |
| 3213 | return IRType; |
| 3214 | } |
| 3215 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3216 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3217 | if (llvm::StructType *STy = dyn_cast<llvm::StructType>(IRType)) { |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3218 | // 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] | 3219 | const llvm::StructLayout *SL = getDataLayout().getStructLayout(STy); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3220 | if (IROffset < SL->getSizeInBytes()) { |
| 3221 | unsigned FieldIdx = SL->getElementContainingOffset(IROffset); |
| 3222 | IROffset -= SL->getElementOffset(FieldIdx); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3223 | |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3224 | return GetINTEGERTypeAtOffset(STy->getElementType(FieldIdx), IROffset, |
| 3225 | SourceTy, SourceOffset); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3226 | } |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3227 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3228 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3229 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(IRType)) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3230 | llvm::Type *EltTy = ATy->getElementType(); |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3231 | unsigned EltSize = getDataLayout().getTypeAllocSize(EltTy); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3232 | unsigned EltOffset = IROffset/EltSize*EltSize; |
Chris Lattner | 1c56d9a | 2010-07-29 17:40:35 +0000 | [diff] [blame] | 3233 | return GetINTEGERTypeAtOffset(EltTy, IROffset-EltOffset, SourceTy, |
| 3234 | SourceOffset); |
Chris Lattner | 98076a2 | 2010-07-29 07:43:55 +0000 | [diff] [blame] | 3235 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3236 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3237 | // Okay, we don't have any better idea of what to pass, so we pass this in an |
| 3238 | // 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] | 3239 | unsigned TySizeInBytes = |
| 3240 | (unsigned)getContext().getTypeSizeInChars(SourceTy).getQuantity(); |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3241 | |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3242 | assert(TySizeInBytes != SourceOffset && "Empty field?"); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3243 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3244 | // It is always safe to classify this as an integer type up to i64 that |
| 3245 | // isn't larger than the structure. |
Chris Lattner | 3f76342 | 2010-07-29 17:34:39 +0000 | [diff] [blame] | 3246 | return llvm::IntegerType::get(getVMContext(), |
| 3247 | std::min(TySizeInBytes-SourceOffset, 8U)*8); |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3250 | |
| 3251 | /// GetX86_64ByValArgumentPair - Given a high and low type that can ideally |
| 3252 | /// be used as elements of a two register pair to pass or return, return a |
| 3253 | /// first class aggregate to represent them. For example, if the low part of |
| 3254 | /// a by-value argument should be passed as i32* and the high part as float, |
| 3255 | /// return {i32*, float}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3256 | static llvm::Type * |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 3257 | GetX86_64ByValArgumentPair(llvm::Type *Lo, llvm::Type *Hi, |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3258 | const llvm::DataLayout &TD) { |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3259 | // In order to correctly satisfy the ABI, we need to the high part to start |
| 3260 | // at offset 8. If the high and low parts we inferred are both 4-byte types |
| 3261 | // (e.g. i32 and i32) then the resultant struct type ({i32,i32}) won't have |
| 3262 | // the second element at offset 8. Check for this: |
| 3263 | unsigned LoSize = (unsigned)TD.getTypeAllocSize(Lo); |
| 3264 | unsigned HiAlign = TD.getABITypeAlignment(Hi); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 3265 | unsigned HiStart = llvm::alignTo(LoSize, HiAlign); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3266 | assert(HiStart != 0 && HiStart <= 8 && "Invalid x86-64 argument pair!"); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3267 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3268 | // To handle this, we have to increase the size of the low part so that the |
| 3269 | // second element will start at an 8 byte offset. We can't increase the size |
| 3270 | // of the second element because it might make us access off the end of the |
| 3271 | // struct. |
| 3272 | if (HiStart != 8) { |
Derek Schuff | 5ec5128 | 2015-06-24 22:36:38 +0000 | [diff] [blame] | 3273 | // There are usually two sorts of types the ABI generation code can produce |
| 3274 | // for the low part of a pair that aren't 8 bytes in size: float or |
| 3275 | // i8/i16/i32. This can also include pointers when they are 32-bit (X32 and |
| 3276 | // NaCl). |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3277 | // Promote these to a larger type. |
| 3278 | if (Lo->isFloatTy()) |
| 3279 | Lo = llvm::Type::getDoubleTy(Lo->getContext()); |
| 3280 | else { |
Derek Schuff | 3c6a48d | 2015-06-24 22:36:36 +0000 | [diff] [blame] | 3281 | assert((Lo->isIntegerTy() || Lo->isPointerTy()) |
| 3282 | && "Invalid/unknown lo type"); |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3283 | Lo = llvm::Type::getInt64Ty(Lo->getContext()); |
| 3284 | } |
| 3285 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3286 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3287 | llvm::StructType *Result = llvm::StructType::get(Lo, Hi); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3288 | |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3289 | // Verify that the second element is at an 8-byte offset. |
| 3290 | assert(TD.getStructLayout(Result)->getElementOffset(1) == 8 && |
| 3291 | "Invalid x86-64 argument pair!"); |
| 3292 | return Result; |
| 3293 | } |
| 3294 | |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3295 | ABIArgInfo X86_64ABIInfo:: |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 3296 | classifyReturnType(QualType RetTy) const { |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3297 | // AMD64-ABI 3.2.3p4: Rule 1. Classify the return type with the |
| 3298 | // classification algorithm. |
| 3299 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3300 | classify(RetTy, 0, Lo, Hi, /*isNamedArg*/ true); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3301 | |
| 3302 | // Check some invariants. |
| 3303 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3304 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3305 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3306 | llvm::Type *ResType = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3307 | switch (Lo) { |
| 3308 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3309 | if (Hi == NoClass) |
| 3310 | return ABIArgInfo::getIgnore(); |
| 3311 | // If the low part is just padding, it takes no register, leave ResType |
| 3312 | // null. |
| 3313 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3314 | "Unknown missing lo part"); |
| 3315 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3316 | |
| 3317 | case SSEUp: |
| 3318 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3319 | llvm_unreachable("Invalid classification for lo word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3320 | |
| 3321 | // AMD64-ABI 3.2.3p4: Rule 2. Types of class memory are returned via |
| 3322 | // hidden argument. |
| 3323 | case Memory: |
| 3324 | return getIndirectReturnResult(RetTy); |
| 3325 | |
| 3326 | // AMD64-ABI 3.2.3p4: Rule 3. If the class is INTEGER, the next |
| 3327 | // available register of the sequence %rax, %rdx is used. |
| 3328 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3329 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3330 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3331 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3332 | // so that the parameter gets the right LLVM IR attributes. |
| 3333 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3334 | // Treat an enum type as its underlying type. |
| 3335 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 3336 | RetTy = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3337 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3338 | if (RetTy->isIntegralOrEnumerationType() && |
| 3339 | RetTy->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3340 | return ABIArgInfo::getExtend(RetTy); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3341 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3342 | break; |
| 3343 | |
| 3344 | // AMD64-ABI 3.2.3p4: Rule 4. If the class is SSE, the next |
| 3345 | // available SSE register of the sequence %xmm0, %xmm1 is used. |
| 3346 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3347 | ResType = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 0, RetTy, 0); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3348 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3349 | |
| 3350 | // AMD64-ABI 3.2.3p4: Rule 6. If the class is X87, the value is |
| 3351 | // returned on the X87 stack in %st0 as 80-bit x87 number. |
| 3352 | case X87: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 3353 | ResType = llvm::Type::getX86_FP80Ty(getVMContext()); |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3354 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3355 | |
| 3356 | // AMD64-ABI 3.2.3p4: Rule 8. If the class is COMPLEX_X87, the real |
| 3357 | // part of the value is returned in %st0 and the imaginary part in |
| 3358 | // %st1. |
| 3359 | case ComplexX87: |
| 3360 | assert(Hi == ComplexX87 && "Unexpected ComplexX87 classification."); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 3361 | ResType = llvm::StructType::get(llvm::Type::getX86_FP80Ty(getVMContext()), |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 3362 | llvm::Type::getX86_FP80Ty(getVMContext())); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3363 | break; |
| 3364 | } |
| 3365 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3366 | llvm::Type *HighPart = nullptr; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3367 | switch (Hi) { |
| 3368 | // Memory was handled previously and X87 should |
| 3369 | // never occur as a hi class. |
| 3370 | case Memory: |
| 3371 | case X87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3372 | llvm_unreachable("Invalid classification for hi word."); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3373 | |
| 3374 | case ComplexX87: // Previously handled. |
Chris Lattner | fa560fe | 2010-07-28 23:12:33 +0000 | [diff] [blame] | 3375 | case NoClass: |
| 3376 | break; |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3377 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3378 | case Integer: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3379 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3380 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3381 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3382 | break; |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3383 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3384 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3385 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3386 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3387 | break; |
| 3388 | |
| 3389 | // 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] | 3390 | // is passed in the next available eightbyte chunk if the last used |
| 3391 | // vector register. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3392 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3393 | // SSEUP should always be preceded by SSE, just widen. |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3394 | case SSEUp: |
| 3395 | assert(Lo == SSE && "Unexpected SSEUp classification."); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3396 | ResType = GetByteVectorType(RetTy); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3397 | break; |
| 3398 | |
| 3399 | // AMD64-ABI 3.2.3p4: Rule 7. If the class is X87UP, the value is |
| 3400 | // returned together with the previous X87 value in %st0. |
| 3401 | case X87Up: |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3402 | // If X87Up is preceded by X87, we don't need to do |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3403 | // anything. However, in some cases with unions it may not be |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3404 | // preceded by X87. In such situations we follow gcc and pass the |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3405 | // extra bits in an SSE reg. |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3406 | if (Lo != X87) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3407 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(RetTy), 8, RetTy, 8); |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3408 | if (Lo == NoClass) // Return HighPart at offset 8 in memory. |
| 3409 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | c95a398 | 2010-07-29 17:49:08 +0000 | [diff] [blame] | 3410 | } |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3411 | break; |
| 3412 | } |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3413 | |
Chris Lattner | 52b3c13 | 2010-09-01 00:20:33 +0000 | [diff] [blame] | 3414 | // 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] | 3415 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3416 | // first class struct aggregate with the high and low part: {low, high} |
Chris Lattner | d426c8e | 2010-09-01 00:50:20 +0000 | [diff] [blame] | 3417 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3418 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3419 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3420 | return ABIArgInfo::getDirect(ResType); |
Chris Lattner | 31faff5 | 2010-07-28 23:06:14 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3423 | ABIArgInfo X86_64ABIInfo::classifyArgumentType( |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3424 | QualType Ty, unsigned freeIntRegs, unsigned &neededInt, unsigned &neededSSE, |
| 3425 | bool isNamedArg) |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3426 | const |
| 3427 | { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 3428 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 3429 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3430 | X86_64ABIInfo::Class Lo, Hi; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3431 | classify(Ty, 0, Lo, Hi, isNamedArg); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3432 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3433 | // Check some invariants. |
| 3434 | // FIXME: Enforce these by construction. |
| 3435 | assert((Hi != Memory || Lo == Memory) && "Invalid memory classification."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3436 | assert((Hi != SSEUp || Lo == SSE) && "Invalid SSEUp classification."); |
| 3437 | |
| 3438 | neededInt = 0; |
| 3439 | neededSSE = 0; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3440 | llvm::Type *ResType = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3441 | switch (Lo) { |
| 3442 | case NoClass: |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3443 | if (Hi == NoClass) |
| 3444 | return ABIArgInfo::getIgnore(); |
| 3445 | // If the low part is just padding, it takes no register, leave ResType |
| 3446 | // null. |
| 3447 | assert((Hi == SSE || Hi == Integer || Hi == X87Up) && |
| 3448 | "Unknown missing lo part"); |
| 3449 | break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3450 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3451 | // AMD64-ABI 3.2.3p3: Rule 1. If the class is MEMORY, pass the argument |
| 3452 | // on the stack. |
| 3453 | case Memory: |
| 3454 | |
| 3455 | // AMD64-ABI 3.2.3p3: Rule 5. If the class is X87, X87UP or |
| 3456 | // COMPLEX_X87, it is passed in memory. |
| 3457 | case X87: |
| 3458 | case ComplexX87: |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3459 | if (getRecordArgABI(Ty, getCXXABI()) == CGCXXABI::RAA_Indirect) |
Eli Friedman | 4774b7e | 2011-06-29 07:04:55 +0000 | [diff] [blame] | 3460 | ++neededInt; |
Daniel Dunbar | f07b5ec | 2012-03-10 01:03:58 +0000 | [diff] [blame] | 3461 | return getIndirectResult(Ty, freeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3462 | |
| 3463 | case SSEUp: |
| 3464 | case X87Up: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3465 | llvm_unreachable("Invalid classification for lo word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3466 | |
| 3467 | // AMD64-ABI 3.2.3p3: Rule 2. If the class is INTEGER, the next |
| 3468 | // available register of the sequence %rdi, %rsi, %rdx, %rcx, %r8 |
| 3469 | // and %r9 is used. |
| 3470 | case Integer: |
Chris Lattner | 22a931e | 2010-06-29 06:01:59 +0000 | [diff] [blame] | 3471 | ++neededInt; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3472 | |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3473 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3474 | ResType = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 0, Ty, 0); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3475 | |
| 3476 | // If we have a sign or zero extended integer, make sure to return Extend |
| 3477 | // so that the parameter gets the right LLVM IR attributes. |
| 3478 | if (Hi == NoClass && isa<llvm::IntegerType>(ResType)) { |
| 3479 | // Treat an enum type as its underlying type. |
| 3480 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3481 | Ty = EnumTy->getDecl()->getIntegerType(); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3482 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3483 | if (Ty->isIntegralOrEnumerationType() && |
| 3484 | Ty->isPromotableIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 3485 | return ABIArgInfo::getExtend(Ty); |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3486 | } |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3487 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3488 | break; |
| 3489 | |
| 3490 | // AMD64-ABI 3.2.3p3: Rule 3. If the class is SSE, the next |
| 3491 | // available SSE register is used, the registers are taken in the |
| 3492 | // order from %xmm0 to %xmm7. |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3493 | case SSE: { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3494 | llvm::Type *IRType = CGT.ConvertType(Ty); |
Eli Friedman | 1310c68 | 2011-07-02 00:57:27 +0000 | [diff] [blame] | 3495 | ResType = GetSSETypeAtOffset(IRType, 0, Ty, 0); |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3496 | ++neededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3497 | break; |
| 3498 | } |
Bill Wendling | 5cd41c4 | 2010-10-18 03:41:31 +0000 | [diff] [blame] | 3499 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3500 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3501 | llvm::Type *HighPart = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3502 | switch (Hi) { |
| 3503 | // Memory was handled previously, ComplexX87 and X87 should |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 3504 | // never occur as hi classes, and X87Up must be preceded by X87, |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3505 | // which is passed in memory. |
| 3506 | case Memory: |
| 3507 | case X87: |
| 3508 | case ComplexX87: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3509 | llvm_unreachable("Invalid classification for hi word."); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3510 | |
| 3511 | case NoClass: break; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3512 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3513 | case Integer: |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3514 | ++neededInt; |
Chris Lattner | b22f1c8 | 2010-07-28 22:44:07 +0000 | [diff] [blame] | 3515 | // Pick an 8-byte type based on the preferred type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3516 | HighPart = GetINTEGERTypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3517 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3518 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3519 | return ABIArgInfo::getDirect(HighPart, 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3520 | break; |
| 3521 | |
| 3522 | // X87Up generally doesn't occur here (long double is passed in |
| 3523 | // memory), except in situations involving unions. |
| 3524 | case X87Up: |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3525 | case SSE: |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 3526 | HighPart = GetSSETypeAtOffset(CGT.ConvertType(Ty), 8, Ty, 8); |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3527 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3528 | if (Lo == NoClass) // Pass HighPart at offset 8 in memory. |
| 3529 | return ABIArgInfo::getDirect(HighPart, 8); |
Chris Lattner | 8a2f3c7 | 2010-07-30 04:02:24 +0000 | [diff] [blame] | 3530 | |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3531 | ++neededSSE; |
| 3532 | break; |
| 3533 | |
| 3534 | // AMD64-ABI 3.2.3p3: Rule 4. If the class is SSEUP, the |
| 3535 | // 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] | 3536 | // register. This only happens when 128-bit vectors are passed. |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3537 | case SSEUp: |
Chris Lattner | f4ba08a | 2010-07-28 23:47:21 +0000 | [diff] [blame] | 3538 | assert(Lo == SSE && "Unexpected SSEUp classification"); |
Bruno Cardoso Lopes | 21a41bb | 2011-07-11 22:41:29 +0000 | [diff] [blame] | 3539 | ResType = GetByteVectorType(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3540 | break; |
| 3541 | } |
| 3542 | |
Chris Lattner | be5eb17 | 2010-09-01 00:24:35 +0000 | [diff] [blame] | 3543 | // If a high part was specified, merge it together with the low part. It is |
| 3544 | // known to pass in the high eightbyte of the result. We do this by forming a |
| 3545 | // first class struct aggregate with the high and low part: {low, high} |
| 3546 | if (HighPart) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3547 | ResType = GetX86_64ByValArgumentPair(ResType, HighPart, getDataLayout()); |
Michael J. Spencer | f5a1fbc | 2010-10-19 06:39:39 +0000 | [diff] [blame] | 3548 | |
Chris Lattner | 1f3a063 | 2010-07-29 21:42:50 +0000 | [diff] [blame] | 3549 | return ABIArgInfo::getDirect(ResType); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3552 | ABIArgInfo |
| 3553 | X86_64ABIInfo::classifyRegCallStructTypeImpl(QualType Ty, unsigned &NeededInt, |
| 3554 | unsigned &NeededSSE) const { |
| 3555 | auto RT = Ty->getAs<RecordType>(); |
| 3556 | assert(RT && "classifyRegCallStructType only valid with struct types"); |
| 3557 | |
| 3558 | if (RT->getDecl()->hasFlexibleArrayMember()) |
| 3559 | return getIndirectReturnResult(Ty); |
| 3560 | |
| 3561 | // Sum up bases |
| 3562 | if (auto CXXRD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
| 3563 | if (CXXRD->isDynamicClass()) { |
| 3564 | NeededInt = NeededSSE = 0; |
| 3565 | return getIndirectReturnResult(Ty); |
| 3566 | } |
| 3567 | |
| 3568 | for (const auto &I : CXXRD->bases()) |
| 3569 | if (classifyRegCallStructTypeImpl(I.getType(), NeededInt, NeededSSE) |
| 3570 | .isIndirect()) { |
| 3571 | NeededInt = NeededSSE = 0; |
| 3572 | return getIndirectReturnResult(Ty); |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | // Sum up members |
| 3577 | for (const auto *FD : RT->getDecl()->fields()) { |
| 3578 | if (FD->getType()->isRecordType() && !FD->getType()->isUnionType()) { |
| 3579 | if (classifyRegCallStructTypeImpl(FD->getType(), NeededInt, NeededSSE) |
| 3580 | .isIndirect()) { |
| 3581 | NeededInt = NeededSSE = 0; |
| 3582 | return getIndirectReturnResult(Ty); |
| 3583 | } |
| 3584 | } else { |
| 3585 | unsigned LocalNeededInt, LocalNeededSSE; |
| 3586 | if (classifyArgumentType(FD->getType(), UINT_MAX, LocalNeededInt, |
| 3587 | LocalNeededSSE, true) |
| 3588 | .isIndirect()) { |
| 3589 | NeededInt = NeededSSE = 0; |
| 3590 | return getIndirectReturnResult(Ty); |
| 3591 | } |
| 3592 | NeededInt += LocalNeededInt; |
| 3593 | NeededSSE += LocalNeededSSE; |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | return ABIArgInfo::getDirect(); |
| 3598 | } |
| 3599 | |
| 3600 | ABIArgInfo X86_64ABIInfo::classifyRegCallStructType(QualType Ty, |
| 3601 | unsigned &NeededInt, |
| 3602 | unsigned &NeededSSE) const { |
| 3603 | |
| 3604 | NeededInt = 0; |
| 3605 | NeededSSE = 0; |
| 3606 | |
| 3607 | return classifyRegCallStructTypeImpl(Ty, NeededInt, NeededSSE); |
| 3608 | } |
| 3609 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 3610 | void X86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3611 | |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3612 | const unsigned CallingConv = FI.getCallingConvention(); |
| 3613 | // It is possible to force Win64 calling convention on any x86_64 target by |
| 3614 | // using __attribute__((ms_abi)). In such case to correctly emit Win64 |
| 3615 | // compatible code delegate this call to WinX86_64ABIInfo::computeInfo. |
| 3616 | if (CallingConv == llvm::CallingConv::Win64) { |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 3617 | WinX86_64ABIInfo Win64ABIInfo(CGT, AVXLevel); |
Alexander Ivchenko | 4b20b3c | 2018-02-08 11:15:21 +0000 | [diff] [blame] | 3618 | Win64ABIInfo.computeInfo(FI); |
| 3619 | return; |
| 3620 | } |
| 3621 | |
| 3622 | bool IsRegCall = CallingConv == llvm::CallingConv::X86_RegCall; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3623 | |
| 3624 | // Keep track of the number of assigned registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3625 | unsigned FreeIntRegs = IsRegCall ? 11 : 6; |
| 3626 | unsigned FreeSSERegs = IsRegCall ? 16 : 8; |
| 3627 | unsigned NeededInt, NeededSSE; |
| 3628 | |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 3629 | if (!::classifyReturnType(getCXXABI(), FI, *this)) { |
Erich Keane | de1b2a9 | 2017-07-21 18:50:36 +0000 | [diff] [blame] | 3630 | if (IsRegCall && FI.getReturnType()->getTypePtr()->isRecordType() && |
| 3631 | !FI.getReturnType()->getTypePtr()->isUnionType()) { |
| 3632 | FI.getReturnInfo() = |
| 3633 | classifyRegCallStructType(FI.getReturnType(), NeededInt, NeededSSE); |
| 3634 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3635 | FreeIntRegs -= NeededInt; |
| 3636 | FreeSSERegs -= NeededSSE; |
| 3637 | } else { |
| 3638 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3639 | } |
| 3640 | } else if (IsRegCall && FI.getReturnType()->getAs<ComplexType>()) { |
| 3641 | // Complex Long Double Type is passed in Memory when Regcall |
| 3642 | // calling convention is used. |
| 3643 | const ComplexType *CT = FI.getReturnType()->getAs<ComplexType>(); |
| 3644 | if (getContext().getCanonicalType(CT->getElementType()) == |
| 3645 | getContext().LongDoubleTy) |
| 3646 | FI.getReturnInfo() = getIndirectReturnResult(FI.getReturnType()); |
| 3647 | } else |
| 3648 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 3649 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3650 | |
| 3651 | // If the return value is indirect, then the hidden argument is consuming one |
| 3652 | // integer register. |
| 3653 | if (FI.getReturnInfo().isIndirect()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3654 | --FreeIntRegs; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3655 | |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3656 | // The chain argument effectively gives us another free register. |
| 3657 | if (FI.isChainCall()) |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3658 | ++FreeIntRegs; |
Peter Collingbourne | f770683 | 2014-12-12 23:41:25 +0000 | [diff] [blame] | 3659 | |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3660 | unsigned NumRequiredArgs = FI.getNumRequiredArgs(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3661 | // AMD64-ABI 3.2.3p3: Once arguments are classified, the registers |
| 3662 | // get assigned (in left-to-right order) for passing as follows... |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3663 | unsigned ArgNo = 0; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3664 | for (CGFunctionInfo::arg_iterator it = FI.arg_begin(), ie = FI.arg_end(); |
Alexey Samsonov | 34625dd | 2014-09-29 21:21:48 +0000 | [diff] [blame] | 3665 | it != ie; ++it, ++ArgNo) { |
| 3666 | bool IsNamedArg = ArgNo < NumRequiredArgs; |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3667 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3668 | if (IsRegCall && it->type->isStructureOrClassType()) |
| 3669 | it->info = classifyRegCallStructType(it->type, NeededInt, NeededSSE); |
| 3670 | else |
| 3671 | it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, |
| 3672 | NeededSSE, IsNamedArg); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3673 | |
| 3674 | // AMD64-ABI 3.2.3p3: If there are no registers available for any |
| 3675 | // eightbyte of an argument, the whole argument is passed on the |
| 3676 | // stack. If registers have already been assigned for some |
| 3677 | // eightbytes of such an argument, the assignments get reverted. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3678 | if (FreeIntRegs >= NeededInt && FreeSSERegs >= NeededSSE) { |
| 3679 | FreeIntRegs -= NeededInt; |
| 3680 | FreeSSERegs -= NeededSSE; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3681 | } else { |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 3682 | it->info = getIndirectResult(it->type, FreeIntRegs); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3687 | static Address EmitX86_64VAArgFromMemory(CodeGenFunction &CGF, |
| 3688 | Address VAListAddr, QualType Ty) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3689 | Address overflow_arg_area_p = |
| 3690 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3691 | llvm::Value *overflow_arg_area = |
| 3692 | CGF.Builder.CreateLoad(overflow_arg_area_p, "overflow_arg_area"); |
| 3693 | |
| 3694 | // AMD64-ABI 3.5.7p5: Step 7. Align l->overflow_arg_area upwards to a 16 |
| 3695 | // byte boundary if alignment needed by type exceeds 8 byte boundary. |
Eli Friedman | a174856 | 2011-11-18 02:44:19 +0000 | [diff] [blame] | 3696 | // It isn't stated explicitly in the standard, but in practice we use |
| 3697 | // alignment greater than 16 where necessary. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3698 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 3699 | if (Align > CharUnits::fromQuantity(8)) { |
| 3700 | overflow_arg_area = emitRoundPointerUpToAlignment(CGF, overflow_arg_area, |
| 3701 | Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | // 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] | 3705 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3706 | llvm::Value *Res = |
| 3707 | CGF.Builder.CreateBitCast(overflow_arg_area, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3708 | llvm::PointerType::getUnqual(LTy)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3709 | |
| 3710 | // AMD64-ABI 3.5.7p5: Step 9. Set l->overflow_arg_area to: |
| 3711 | // l->overflow_arg_area + sizeof(type). |
| 3712 | // AMD64-ABI 3.5.7p5: Step 10. Align l->overflow_arg_area upwards to |
| 3713 | // an 8 byte boundary. |
| 3714 | |
| 3715 | uint64_t SizeInBytes = (CGF.getContext().getTypeSize(Ty) + 7) / 8; |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3716 | llvm::Value *Offset = |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3717 | llvm::ConstantInt::get(CGF.Int32Ty, (SizeInBytes + 7) & ~7); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3718 | overflow_arg_area = CGF.Builder.CreateGEP(overflow_arg_area, Offset, |
| 3719 | "overflow_arg_area.next"); |
| 3720 | CGF.Builder.CreateStore(overflow_arg_area, overflow_arg_area_p); |
| 3721 | |
| 3722 | // AMD64-ABI 3.5.7p5: Step 11. Return the fetched type. |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 3723 | return Address(Res, Align); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3726 | Address X86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3727 | QualType Ty) const { |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3728 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 3729 | // struct { |
| 3730 | // i32 gp_offset; |
| 3731 | // i32 fp_offset; |
| 3732 | // i8* overflow_arg_area; |
| 3733 | // i8* reg_save_area; |
| 3734 | // }; |
Bill Wendling | 9987c0e | 2010-10-18 23:51:38 +0000 | [diff] [blame] | 3735 | unsigned neededInt, neededSSE; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 3736 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3737 | Ty = getContext().getCanonicalType(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 3738 | ABIArgInfo AI = classifyArgumentType(Ty, 0, neededInt, neededSSE, |
Eli Friedman | 96fd264 | 2013-06-12 00:13:45 +0000 | [diff] [blame] | 3739 | /*isNamedArg*/false); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3740 | |
| 3741 | // AMD64-ABI 3.5.7p5: Step 1. Determine whether type may be passed |
| 3742 | // in the registers. If not go to step 7. |
| 3743 | if (!neededInt && !neededSSE) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3744 | return EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3745 | |
| 3746 | // AMD64-ABI 3.5.7p5: Step 2. Compute num_gp to hold the number of |
| 3747 | // general purpose registers needed to pass type and num_fp to hold |
| 3748 | // the number of floating point registers needed. |
| 3749 | |
| 3750 | // AMD64-ABI 3.5.7p5: Step 3. Verify whether arguments fit into |
| 3751 | // registers. In the case: l->gp_offset > 48 - num_gp * 8 or |
| 3752 | // l->fp_offset > 304 - num_fp * 16 go to step 7. |
| 3753 | // |
| 3754 | // NOTE: 304 is a typo, there are (6 * 8 + 8 * 16) = 176 bytes of |
| 3755 | // register save space). |
| 3756 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3757 | llvm::Value *InRegs = nullptr; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3758 | Address gp_offset_p = Address::invalid(), fp_offset_p = Address::invalid(); |
| 3759 | llvm::Value *gp_offset = nullptr, *fp_offset = nullptr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3760 | if (neededInt) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3761 | gp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "gp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3762 | gp_offset = CGF.Builder.CreateLoad(gp_offset_p, "gp_offset"); |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3763 | InRegs = llvm::ConstantInt::get(CGF.Int32Ty, 48 - neededInt * 8); |
| 3764 | InRegs = CGF.Builder.CreateICmpULE(gp_offset, InRegs, "fits_in_gp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | if (neededSSE) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3768 | fp_offset_p = CGF.Builder.CreateStructGEP(VAListAddr, 1, "fp_offset_p"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3769 | fp_offset = CGF.Builder.CreateLoad(fp_offset_p, "fp_offset"); |
| 3770 | llvm::Value *FitsInFP = |
Chris Lattner | d776fb1 | 2010-06-28 21:43:59 +0000 | [diff] [blame] | 3771 | llvm::ConstantInt::get(CGF.Int32Ty, 176 - neededSSE * 16); |
| 3772 | FitsInFP = CGF.Builder.CreateICmpULE(fp_offset, FitsInFP, "fits_in_fp"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3773 | InRegs = InRegs ? CGF.Builder.CreateAnd(InRegs, FitsInFP) : FitsInFP; |
| 3774 | } |
| 3775 | |
| 3776 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 3777 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 3778 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 3779 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 3780 | |
| 3781 | // Emit code to load the value if it was passed in registers. |
| 3782 | |
| 3783 | CGF.EmitBlock(InRegBlock); |
| 3784 | |
| 3785 | // AMD64-ABI 3.5.7p5: Step 4. Fetch type from l->reg_save_area with |
| 3786 | // an offset of l->gp_offset and/or l->fp_offset. This may require |
| 3787 | // copying to a temporary location in case the parameter is passed |
| 3788 | // in different register classes or requires an alignment greater |
| 3789 | // than 8 for general purpose registers and 16 for XMM registers. |
| 3790 | // |
| 3791 | // FIXME: This really results in shameful code when we end up needing to |
| 3792 | // collect arguments from different places; often what should result in a |
| 3793 | // simple assembling of a structure from scattered addresses has many more |
| 3794 | // loads than necessary. Can we clean this up? |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3795 | llvm::Type *LTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3796 | llvm::Value *RegSaveArea = CGF.Builder.CreateLoad( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3797 | CGF.Builder.CreateStructGEP(VAListAddr, 3), "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3798 | |
| 3799 | Address RegAddr = Address::invalid(); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3800 | if (neededInt && neededSSE) { |
| 3801 | // FIXME: Cleanup. |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 3802 | assert(AI.isDirect() && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3803 | llvm::StructType *ST = cast<llvm::StructType>(AI.getCoerceToType()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3804 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3805 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3806 | assert(ST->getNumElements() == 2 && "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3807 | llvm::Type *TyLo = ST->getElementType(0); |
| 3808 | llvm::Type *TyHi = ST->getElementType(1); |
Chris Lattner | 51e1cc2 | 2010-08-26 06:28:35 +0000 | [diff] [blame] | 3809 | assert((TyLo->isFPOrFPVectorTy() ^ TyHi->isFPOrFPVectorTy()) && |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3810 | "Unexpected ABI info for mixed regs"); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3811 | llvm::Type *PTyLo = llvm::PointerType::getUnqual(TyLo); |
| 3812 | llvm::Type *PTyHi = llvm::PointerType::getUnqual(TyHi); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3813 | llvm::Value *GPAddr = CGF.Builder.CreateGEP(RegSaveArea, gp_offset); |
| 3814 | llvm::Value *FPAddr = CGF.Builder.CreateGEP(RegSaveArea, fp_offset); |
Rafael Espindola | 0a500af | 2014-06-24 20:01:50 +0000 | [diff] [blame] | 3815 | llvm::Value *RegLoAddr = TyLo->isFPOrFPVectorTy() ? FPAddr : GPAddr; |
| 3816 | llvm::Value *RegHiAddr = TyLo->isFPOrFPVectorTy() ? GPAddr : FPAddr; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3817 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3818 | // Copy the first element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3819 | // FIXME: Our choice of alignment here and below is probably pessimistic. |
| 3820 | llvm::Value *V = CGF.Builder.CreateAlignedLoad( |
| 3821 | TyLo, CGF.Builder.CreateBitCast(RegLoAddr, PTyLo), |
| 3822 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyLo))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3823 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3824 | |
| 3825 | // Copy the second element. |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 3826 | V = CGF.Builder.CreateAlignedLoad( |
| 3827 | TyHi, CGF.Builder.CreateBitCast(RegHiAddr, PTyHi), |
| 3828 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(TyHi))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3829 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3830 | |
| 3831 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3832 | } else if (neededInt) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3833 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, gp_offset), |
| 3834 | CharUnits::fromQuantity(8)); |
| 3835 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3836 | |
| 3837 | // Copy to a temporary if necessary to ensure the appropriate alignment. |
| 3838 | std::pair<CharUnits, CharUnits> SizeAlign = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3839 | getContext().getTypeInfoInChars(Ty); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3840 | uint64_t TySize = SizeAlign.first.getQuantity(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3841 | CharUnits TyAlign = SizeAlign.second; |
| 3842 | |
| 3843 | // Copy into a temporary if the type is more aligned than the |
| 3844 | // register save area. |
| 3845 | if (TyAlign.getQuantity() > 8) { |
| 3846 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3847 | CGF.Builder.CreateMemCpy(Tmp, RegAddr, TySize, false); |
Eli Friedman | c11c169 | 2013-06-07 23:20:55 +0000 | [diff] [blame] | 3848 | RegAddr = Tmp; |
| 3849 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3850 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3851 | } else if (neededSSE == 1) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3852 | RegAddr = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3853 | CharUnits::fromQuantity(16)); |
| 3854 | RegAddr = CGF.Builder.CreateElementBitCast(RegAddr, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3855 | } else { |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 3856 | assert(neededSSE == 2 && "Invalid number of needed registers!"); |
| 3857 | // SSE registers are spaced 16 bytes apart in the register save |
| 3858 | // area, we need to collect the two eightbytes together. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3859 | // The ABI isn't explicit about this, but it seems reasonable |
| 3860 | // to assume that the slots are 16-byte aligned, since the stack is |
| 3861 | // naturally 16-byte aligned and the prologue is expected to store |
| 3862 | // all the SSE registers to the RSA. |
| 3863 | Address RegAddrLo = Address(CGF.Builder.CreateGEP(RegSaveArea, fp_offset), |
| 3864 | CharUnits::fromQuantity(16)); |
| 3865 | Address RegAddrHi = |
| 3866 | CGF.Builder.CreateConstInBoundsByteGEP(RegAddrLo, |
| 3867 | CharUnits::fromQuantity(16)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3868 | llvm::Type *ST = AI.canHaveCoerceToType() |
| 3869 | ? AI.getCoerceToType() |
| 3870 | : llvm::StructType::get(CGF.DoubleTy, CGF.DoubleTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3871 | llvm::Value *V; |
| 3872 | Address Tmp = CGF.CreateMemTemp(Ty); |
| 3873 | Tmp = CGF.Builder.CreateElementBitCast(Tmp, ST); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3874 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3875 | RegAddrLo, ST->getStructElementType(0))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3876 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 0)); |
Erich Keane | 24e6840 | 2018-02-02 15:53:35 +0000 | [diff] [blame] | 3877 | V = CGF.Builder.CreateLoad(CGF.Builder.CreateElementBitCast( |
| 3878 | RegAddrHi, ST->getStructElementType(1))); |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3879 | CGF.Builder.CreateStore(V, CGF.Builder.CreateStructGEP(Tmp, 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3880 | |
| 3881 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, LTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | // AMD64-ABI 3.5.7p5: Step 5. Set: |
| 3885 | // l->gp_offset = l->gp_offset + num_gp * 8 |
| 3886 | // l->fp_offset = l->fp_offset + num_fp * 16. |
| 3887 | if (neededInt) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3888 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededInt * 8); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3889 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(gp_offset, Offset), |
| 3890 | gp_offset_p); |
| 3891 | } |
| 3892 | if (neededSSE) { |
Chris Lattner | 5e016ae | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3893 | llvm::Value *Offset = llvm::ConstantInt::get(CGF.Int32Ty, neededSSE * 16); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3894 | CGF.Builder.CreateStore(CGF.Builder.CreateAdd(fp_offset, Offset), |
| 3895 | fp_offset_p); |
| 3896 | } |
| 3897 | CGF.EmitBranch(ContBlock); |
| 3898 | |
| 3899 | // Emit code to load the value if it was passed in memory. |
| 3900 | |
| 3901 | CGF.EmitBlock(InMemBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3902 | Address MemAddr = EmitX86_64VAArgFromMemory(CGF, VAListAddr, Ty); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3903 | |
| 3904 | // Return the appropriate result. |
| 3905 | |
| 3906 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3907 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, MemAddr, InMemBlock, |
| 3908 | "vaarg.addr"); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 3909 | return ResAddr; |
| 3910 | } |
| 3911 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 3912 | Address X86_64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 3913 | QualType Ty) const { |
| 3914 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 3915 | CGF.getContext().getTypeInfoInChars(Ty), |
| 3916 | CharUnits::fromQuantity(8), |
| 3917 | /*allowHigherAlign*/ false); |
| 3918 | } |
| 3919 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3920 | ABIArgInfo |
| 3921 | WinX86_64ABIInfo::reclassifyHvaArgType(QualType Ty, unsigned &FreeSSERegs, |
| 3922 | const ABIArgInfo ¤t) const { |
| 3923 | // Assumes vectorCall calling convention. |
| 3924 | const Type *Base = nullptr; |
| 3925 | uint64_t NumElts = 0; |
| 3926 | |
| 3927 | if (!Ty->isBuiltinType() && !Ty->isVectorType() && |
| 3928 | isHomogeneousAggregate(Ty, Base, NumElts) && FreeSSERegs >= NumElts) { |
| 3929 | FreeSSERegs -= NumElts; |
| 3930 | return getDirectX86Hva(); |
| 3931 | } |
| 3932 | return current; |
| 3933 | } |
| 3934 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3935 | ABIArgInfo WinX86_64ABIInfo::classify(QualType Ty, unsigned &FreeSSERegs, |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3936 | bool IsReturnType, bool IsVectorCall, |
| 3937 | bool IsRegCall) const { |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3938 | |
| 3939 | if (Ty->isVoidType()) |
| 3940 | return ABIArgInfo::getIgnore(); |
| 3941 | |
| 3942 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 3943 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 3944 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3945 | TypeInfo Info = getContext().getTypeInfo(Ty); |
| 3946 | uint64_t Width = Info.Width; |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 3947 | CharUnits Align = getContext().toCharUnitsFromBits(Info.Align); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3948 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3949 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 3950 | if (RT) { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 3951 | if (!IsReturnType) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 3952 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3953 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Timur Iskhodzhanov | 8fe501d | 2013-04-17 12:54:10 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | if (RT->getDecl()->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3957 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 3958 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3959 | } |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3960 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3961 | const Type *Base = nullptr; |
| 3962 | uint64_t NumElts = 0; |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3963 | // vectorcall adds the concept of a homogenous vector aggregate, similar to |
| 3964 | // other targets. |
| 3965 | if ((IsVectorCall || IsRegCall) && |
| 3966 | isHomogeneousAggregate(Ty, Base, NumElts)) { |
| 3967 | if (IsRegCall) { |
| 3968 | if (FreeSSERegs >= NumElts) { |
| 3969 | FreeSSERegs -= NumElts; |
| 3970 | if (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType()) |
| 3971 | return ABIArgInfo::getDirect(); |
| 3972 | return ABIArgInfo::getExpand(); |
| 3973 | } |
| 3974 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3975 | } else if (IsVectorCall) { |
| 3976 | if (FreeSSERegs >= NumElts && |
| 3977 | (IsReturnType || Ty->isBuiltinType() || Ty->isVectorType())) { |
| 3978 | FreeSSERegs -= NumElts; |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3979 | return ABIArgInfo::getDirect(); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 3980 | } else if (IsReturnType) { |
| 3981 | return ABIArgInfo::getExpand(); |
| 3982 | } else if (!Ty->isBuiltinType() && !Ty->isVectorType()) { |
| 3983 | // HVAs are delayed and reclassified in the 2nd step. |
| 3984 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 3985 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3986 | } |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Reid Kleckner | ec87fec | 2014-05-02 01:17:12 +0000 | [diff] [blame] | 3989 | if (Ty->isMemberPointerType()) { |
Reid Kleckner | 7f5f0f3 | 2014-05-02 01:14:59 +0000 | [diff] [blame] | 3990 | // If the member pointer is represented by an LLVM int or ptr, pass it |
| 3991 | // directly. |
| 3992 | llvm::Type *LLTy = CGT.ConvertType(Ty); |
| 3993 | if (LLTy->isPointerTy() || LLTy->isIntegerTy()) |
| 3994 | return ABIArgInfo::getDirect(); |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Michael Kuperstein | 4f81870 | 2015-02-24 09:35:58 +0000 | [diff] [blame] | 3997 | if (RT || Ty->isAnyComplexType() || Ty->isMemberPointerType()) { |
NAKAMURA Takumi | f8a6e80 | 2011-02-22 03:56:57 +0000 | [diff] [blame] | 3998 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 3999 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4000 | if (Width > 64 || !llvm::isPowerOf2_64(Width)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4001 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4002 | |
Reid Kleckner | 9005f41 | 2014-05-02 00:51:20 +0000 | [diff] [blame] | 4003 | // Otherwise, coerce it to a small integer. |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4004 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Width)); |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
Reid Kleckner | 08f64e9 | 2018-10-31 17:43:55 +0000 | [diff] [blame] | 4007 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4008 | switch (BT->getKind()) { |
| 4009 | case BuiltinType::Bool: |
| 4010 | // Bool type is always extended to the ABI, other builtin types are not |
| 4011 | // extended. |
| 4012 | return ABIArgInfo::getExtend(Ty); |
| 4013 | |
| 4014 | case BuiltinType::LongDouble: |
| 4015 | // Mingw64 GCC uses the old 80 bit extended precision floating point |
| 4016 | // unit. It passes them indirectly through memory. |
| 4017 | if (IsMingw64) { |
| 4018 | const llvm::fltSemantics *LDF = &getTarget().getLongDoubleFormat(); |
| 4019 | if (LDF == &llvm::APFloat::x87DoubleExtended()) |
| 4020 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 4021 | } |
| 4022 | break; |
| 4023 | |
| 4024 | case BuiltinType::Int128: |
| 4025 | case BuiltinType::UInt128: |
| 4026 | // If it's a parameter type, the normal ABI rule is that arguments larger |
| 4027 | // than 8 bytes are passed indirectly. GCC follows it. We follow it too, |
| 4028 | // even though it isn't particularly efficient. |
| 4029 | if (!IsReturnType) |
| 4030 | return ABIArgInfo::getIndirect(Align, /*ByVal=*/false); |
| 4031 | |
| 4032 | // Mingw64 GCC returns i128 in XMM0. Coerce to v2i64 to handle that. |
| 4033 | // Clang matches them for compatibility. |
| 4034 | return ABIArgInfo::getDirect( |
| 4035 | llvm::VectorType::get(llvm::Type::getInt64Ty(getVMContext()), 2)); |
| 4036 | |
| 4037 | default: |
| 4038 | break; |
| 4039 | } |
Reid Kleckner | 11a1719 | 2015-10-28 22:29:52 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4042 | return ABIArgInfo::getDirect(); |
| 4043 | } |
| 4044 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4045 | void WinX86_64ABIInfo::computeVectorCallArgs(CGFunctionInfo &FI, |
| 4046 | unsigned FreeSSERegs, |
| 4047 | bool IsVectorCall, |
| 4048 | bool IsRegCall) const { |
| 4049 | unsigned Count = 0; |
| 4050 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4051 | // Vectorcall in x64 only permits the first 6 arguments to be passed |
| 4052 | // as XMM/YMM registers. |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4053 | if (Count < VectorcallMaxParamNumAsReg) |
| 4054 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4055 | else { |
| 4056 | // Since these cannot be passed in registers, pretend no registers |
| 4057 | // are left. |
| 4058 | unsigned ZeroSSERegsAvail = 0; |
| 4059 | I.info = classify(I.type, /*FreeSSERegs=*/ZeroSSERegsAvail, false, |
| 4060 | IsVectorCall, IsRegCall); |
| 4061 | } |
| 4062 | ++Count; |
| 4063 | } |
| 4064 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4065 | for (auto &I : FI.arguments()) { |
Erich Keane | 4bd3930 | 2017-06-21 16:37:22 +0000 | [diff] [blame] | 4066 | I.info = reclassifyHvaArgType(I.type, FreeSSERegs, I.info); |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4067 | } |
| 4068 | } |
| 4069 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4070 | void WinX86_64ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 3fd3de1 | 2019-06-20 20:07:20 +0000 | [diff] [blame] | 4071 | const unsigned CC = FI.getCallingConvention(); |
| 4072 | bool IsVectorCall = CC == llvm::CallingConv::X86_VectorCall; |
| 4073 | bool IsRegCall = CC == llvm::CallingConv::X86_RegCall; |
| 4074 | |
| 4075 | // If __attribute__((sysv_abi)) is in use, use the SysV argument |
| 4076 | // classification rules. |
| 4077 | if (CC == llvm::CallingConv::X86_64_SysV) { |
| 4078 | X86_64ABIInfo SysVABIInfo(CGT, AVXLevel); |
| 4079 | SysVABIInfo.computeInfo(FI); |
| 4080 | return; |
| 4081 | } |
Reid Kleckner | 37abaca | 2014-05-09 22:46:15 +0000 | [diff] [blame] | 4082 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4083 | unsigned FreeSSERegs = 0; |
| 4084 | if (IsVectorCall) { |
| 4085 | // We can use up to 4 SSE return registers with vectorcall. |
| 4086 | FreeSSERegs = 4; |
| 4087 | } else if (IsRegCall) { |
| 4088 | // RegCall gives us 16 SSE registers. |
| 4089 | FreeSSERegs = 16; |
| 4090 | } |
| 4091 | |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4092 | if (!getCXXABI().classifyReturnType(FI)) |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4093 | FI.getReturnInfo() = classify(FI.getReturnType(), FreeSSERegs, true, |
| 4094 | IsVectorCall, IsRegCall); |
Reid Kleckner | 80944df | 2014-10-31 22:00:51 +0000 | [diff] [blame] | 4095 | |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4096 | if (IsVectorCall) { |
| 4097 | // We can use up to 6 SSE register parameters with vectorcall. |
| 4098 | FreeSSERegs = 6; |
| 4099 | } else if (IsRegCall) { |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4100 | // RegCall gives us 16 SSE registers, we can reuse the return registers. |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4101 | FreeSSERegs = 16; |
| 4102 | } |
| 4103 | |
Erich Keane | 521ed96 | 2017-01-05 00:20:51 +0000 | [diff] [blame] | 4104 | if (IsVectorCall) { |
| 4105 | computeVectorCallArgs(FI, FreeSSERegs, IsVectorCall, IsRegCall); |
| 4106 | } else { |
| 4107 | for (auto &I : FI.arguments()) |
| 4108 | I.info = classify(I.type, FreeSSERegs, false, IsVectorCall, IsRegCall); |
| 4109 | } |
| 4110 | |
NAKAMURA Takumi | bd91f50 | 2011-01-17 22:56:31 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4113 | Address WinX86_64ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4114 | QualType Ty) const { |
Reid Kleckner | b04449d | 2016-08-25 20:42:26 +0000 | [diff] [blame] | 4115 | |
| 4116 | bool IsIndirect = false; |
| 4117 | |
| 4118 | // MS x64 ABI requirement: "Any argument that doesn't fit in 8 bytes, or is |
| 4119 | // not 1, 2, 4, or 8 bytes, must be passed by reference." |
| 4120 | if (isAggregateTypeForABI(Ty) || Ty->isMemberPointerType()) { |
| 4121 | uint64_t Width = getContext().getTypeSize(Ty); |
| 4122 | IsIndirect = Width > 64 || !llvm::isPowerOf2_64(Width); |
| 4123 | } |
| 4124 | |
| 4125 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4126 | CGF.getContext().getTypeInfoInChars(Ty), |
| 4127 | CharUnits::fromQuantity(8), |
| 4128 | /*allowHigherAlign*/ false); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 4129 | } |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4130 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4131 | // PowerPC-32 |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4132 | namespace { |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4133 | /// PPC32_SVR4_ABIInfo - The 32-bit PowerPC ELF (SVR4) ABI information. |
| 4134 | class PPC32_SVR4_ABIInfo : public DefaultABIInfo { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4135 | bool IsSoftFloatABI; |
| 4136 | |
| 4137 | CharUnits getParamTypeAlignment(QualType Ty) const; |
| 4138 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4139 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4140 | PPC32_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, bool SoftFloatABI) |
| 4141 | : DefaultABIInfo(CGT), IsSoftFloatABI(SoftFloatABI) {} |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4142 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4143 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4144 | QualType Ty) const override; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4145 | }; |
| 4146 | |
| 4147 | class PPC32TargetCodeGenInfo : public TargetCodeGenInfo { |
| 4148 | public: |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4149 | PPC32TargetCodeGenInfo(CodeGenTypes &CGT, bool SoftFloatABI) |
| 4150 | : TargetCodeGenInfo(new PPC32_SVR4_ABIInfo(CGT, SoftFloatABI)) {} |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4151 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4152 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4153 | // This is recovered from gcc output. |
| 4154 | return 1; // r1 is the dedicated stack pointer |
| 4155 | } |
| 4156 | |
| 4157 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4158 | llvm::Value *Address) const override; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4159 | }; |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4160 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4161 | |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4162 | CharUnits PPC32_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
| 4163 | // Complex types are passed just like their elements |
| 4164 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4165 | Ty = CTy->getElementType(); |
| 4166 | |
| 4167 | if (Ty->isVectorType()) |
| 4168 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 |
| 4169 | : 4); |
| 4170 | |
| 4171 | // For single-element float/vector structs, we consider the whole type |
| 4172 | // to have the same alignment requirements as its single element. |
| 4173 | const Type *AlignTy = nullptr; |
| 4174 | if (const Type *EltType = isSingleElementStruct(Ty, getContext())) { |
| 4175 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
| 4176 | if ((EltType->isVectorType() && getContext().getTypeSize(EltType) == 128) || |
| 4177 | (BT && BT->isFloatingPoint())) |
| 4178 | AlignTy = EltType; |
| 4179 | } |
| 4180 | |
| 4181 | if (AlignTy) |
| 4182 | return CharUnits::fromQuantity(AlignTy->isVectorType() ? 16 : 4); |
| 4183 | return CharUnits::fromQuantity(4); |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4184 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4185 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4186 | // TODO: this implementation is now likely redundant with |
| 4187 | // DefaultABIInfo::EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4188 | Address PPC32_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAList, |
| 4189 | QualType Ty) const { |
Saleem Abdulrasool | 2a5015b | 2017-10-25 17:56:50 +0000 | [diff] [blame] | 4190 | if (getTarget().getTriple().isOSDarwin()) { |
| 4191 | auto TI = getContext().getTypeInfoInChars(Ty); |
| 4192 | TI.second = getParamTypeAlignment(Ty); |
| 4193 | |
| 4194 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 4195 | return emitVoidPtrVAArg(CGF, VAList, Ty, |
| 4196 | classifyArgumentType(Ty).isIndirect(), TI, SlotSize, |
| 4197 | /*AllowHigherAlign=*/true); |
| 4198 | } |
| 4199 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4200 | const unsigned OverflowLimit = 8; |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4201 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4202 | // TODO: Implement this. For now ignore. |
| 4203 | (void)CTy; |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 4204 | return Address::invalid(); // FIXME? |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4207 | // struct __va_list_tag { |
| 4208 | // unsigned char gpr; |
| 4209 | // unsigned char fpr; |
| 4210 | // unsigned short reserved; |
| 4211 | // void *overflow_arg_area; |
| 4212 | // void *reg_save_area; |
| 4213 | // }; |
| 4214 | |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4215 | bool isI64 = Ty->isIntegerType() && getContext().getTypeSize(Ty) == 64; |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4216 | bool isInt = |
| 4217 | Ty->isIntegerType() || Ty->isPointerType() || Ty->isAggregateType(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4218 | bool isF64 = Ty->isFloatingType() && getContext().getTypeSize(Ty) == 64; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4219 | |
| 4220 | // All aggregates are passed indirectly? That doesn't seem consistent |
| 4221 | // with the argument-lowering code. |
| 4222 | bool isIndirect = Ty->isAggregateType(); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4223 | |
| 4224 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4225 | |
| 4226 | // The calling convention either uses 1-2 GPRs or 1 FPR. |
| 4227 | Address NumRegsAddr = Address::invalid(); |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4228 | if (isInt || IsSoftFloatABI) { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4229 | NumRegsAddr = Builder.CreateStructGEP(VAList, 0, "gpr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4230 | } else { |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4231 | NumRegsAddr = Builder.CreateStructGEP(VAList, 1, "fpr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4232 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4233 | |
| 4234 | llvm::Value *NumRegs = Builder.CreateLoad(NumRegsAddr, "numUsedRegs"); |
| 4235 | |
| 4236 | // "Align" the register count when TY is i64. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4237 | if (isI64 || (isF64 && IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4238 | NumRegs = Builder.CreateAdd(NumRegs, Builder.getInt8(1)); |
| 4239 | NumRegs = Builder.CreateAnd(NumRegs, Builder.getInt8((uint8_t) ~1U)); |
| 4240 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4241 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 4242 | llvm::Value *CC = |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4243 | Builder.CreateICmpULT(NumRegs, Builder.getInt8(OverflowLimit), "cond"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4244 | |
| 4245 | llvm::BasicBlock *UsingRegs = CGF.createBasicBlock("using_regs"); |
| 4246 | llvm::BasicBlock *UsingOverflow = CGF.createBasicBlock("using_overflow"); |
| 4247 | llvm::BasicBlock *Cont = CGF.createBasicBlock("cont"); |
| 4248 | |
| 4249 | Builder.CreateCondBr(CC, UsingRegs, UsingOverflow); |
| 4250 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4251 | llvm::Type *DirectTy = CGF.ConvertType(Ty); |
| 4252 | if (isIndirect) DirectTy = DirectTy->getPointerTo(0); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4253 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4254 | // Case 1: consume registers. |
| 4255 | Address RegAddr = Address::invalid(); |
| 4256 | { |
| 4257 | CGF.EmitBlock(UsingRegs); |
| 4258 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4259 | Address RegSaveAreaPtr = Builder.CreateStructGEP(VAList, 4); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4260 | RegAddr = Address(Builder.CreateLoad(RegSaveAreaPtr), |
| 4261 | CharUnits::fromQuantity(8)); |
| 4262 | assert(RegAddr.getElementType() == CGF.Int8Ty); |
| 4263 | |
| 4264 | // Floating-point registers start after the general-purpose registers. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4265 | if (!(isInt || IsSoftFloatABI)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4266 | RegAddr = Builder.CreateConstInBoundsByteGEP(RegAddr, |
| 4267 | CharUnits::fromQuantity(32)); |
| 4268 | } |
| 4269 | |
| 4270 | // Get the address of the saved value by scaling the number of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4271 | // registers we've used by the number of |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4272 | CharUnits RegSize = CharUnits::fromQuantity((isInt || IsSoftFloatABI) ? 4 : 8); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4273 | llvm::Value *RegOffset = |
| 4274 | Builder.CreateMul(NumRegs, Builder.getInt8(RegSize.getQuantity())); |
| 4275 | RegAddr = Address(Builder.CreateInBoundsGEP(CGF.Int8Ty, |
| 4276 | RegAddr.getPointer(), RegOffset), |
| 4277 | RegAddr.getAlignment().alignmentOfArrayElement(RegSize)); |
| 4278 | RegAddr = Builder.CreateElementBitCast(RegAddr, DirectTy); |
| 4279 | |
| 4280 | // Increase the used-register count. |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4281 | NumRegs = |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4282 | Builder.CreateAdd(NumRegs, |
Petar Jovanovic | 88a328f | 2015-12-14 17:51:50 +0000 | [diff] [blame] | 4283 | Builder.getInt8((isI64 || (isF64 && IsSoftFloatABI)) ? 2 : 1)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4284 | Builder.CreateStore(NumRegs, NumRegsAddr); |
| 4285 | |
| 4286 | CGF.EmitBranch(Cont); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4287 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4288 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4289 | // Case 2: consume space in the overflow area. |
| 4290 | Address MemAddr = Address::invalid(); |
| 4291 | { |
| 4292 | CGF.EmitBlock(UsingOverflow); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4293 | |
Roman Divacky | 039b970 | 2016-02-20 08:31:24 +0000 | [diff] [blame] | 4294 | Builder.CreateStore(Builder.getInt8(OverflowLimit), NumRegsAddr); |
| 4295 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4296 | // Everything in the overflow area is rounded up to a size of at least 4. |
| 4297 | CharUnits OverflowAreaAlign = CharUnits::fromQuantity(4); |
| 4298 | |
| 4299 | CharUnits Size; |
| 4300 | if (!isIndirect) { |
| 4301 | auto TypeInfo = CGF.getContext().getTypeInfoInChars(Ty); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4302 | Size = TypeInfo.first.alignTo(OverflowAreaAlign); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4303 | } else { |
| 4304 | Size = CGF.getPointerSize(); |
| 4305 | } |
| 4306 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 4307 | Address OverflowAreaAddr = Builder.CreateStructGEP(VAList, 3); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4308 | Address OverflowArea(Builder.CreateLoad(OverflowAreaAddr, "argp.cur"), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4309 | OverflowAreaAlign); |
Petar Jovanovic | 402257b | 2015-12-04 00:26:47 +0000 | [diff] [blame] | 4310 | // Round up address of argument to alignment |
| 4311 | CharUnits Align = CGF.getContext().getTypeAlignInChars(Ty); |
| 4312 | if (Align > OverflowAreaAlign) { |
| 4313 | llvm::Value *Ptr = OverflowArea.getPointer(); |
| 4314 | OverflowArea = Address(emitRoundPointerUpToAlignment(CGF, Ptr, Align), |
| 4315 | Align); |
| 4316 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4317 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4318 | MemAddr = Builder.CreateElementBitCast(OverflowArea, DirectTy); |
| 4319 | |
| 4320 | // Increase the overflow area. |
| 4321 | OverflowArea = Builder.CreateConstInBoundsByteGEP(OverflowArea, Size); |
| 4322 | Builder.CreateStore(OverflowArea.getPointer(), OverflowAreaAddr); |
| 4323 | CGF.EmitBranch(Cont); |
| 4324 | } |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4325 | |
| 4326 | CGF.EmitBlock(Cont); |
| 4327 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4328 | // Merge the cases with a phi. |
| 4329 | Address Result = emitMergePHI(CGF, RegAddr, UsingRegs, MemAddr, UsingOverflow, |
| 4330 | "vaarg.addr"); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4332 | // Load the pointer if the argument was passed indirectly. |
| 4333 | if (isIndirect) { |
| 4334 | Result = Address(Builder.CreateLoad(Result, "aggr"), |
| 4335 | getContext().getTypeAlignInChars(Ty)); |
Roman Divacky | 8a12d84 | 2014-11-03 18:32:54 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | return Result; |
| 4339 | } |
| 4340 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4341 | bool |
| 4342 | PPC32TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4343 | llvm::Value *Address) const { |
| 4344 | // This is calculated from the LLVM and GCC tables and verified |
| 4345 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4346 | |
| 4347 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4348 | |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4349 | llvm::IntegerType *i8 = CGF.Int8Ty; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4350 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4351 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4352 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4353 | |
| 4354 | // 0-31: r0-31, the 4-byte general-purpose registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4355 | AssignToArrayRange(Builder, Address, Four8, 0, 31); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4356 | |
| 4357 | // 32-63: fp0-31, the 8-byte floating-point registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4358 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4359 | |
| 4360 | // 64-76 are various 4-byte special-purpose registers: |
| 4361 | // 64: mq |
| 4362 | // 65: lr |
| 4363 | // 66: ctr |
| 4364 | // 67: ap |
| 4365 | // 68-75 cr0-7 |
| 4366 | // 76: xer |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4367 | AssignToArrayRange(Builder, Address, Four8, 64, 76); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4368 | |
| 4369 | // 77-108: v0-31, the 16-byte vector registers |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4370 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4371 | |
| 4372 | // 109: vrsave |
| 4373 | // 110: vscr |
| 4374 | // 111: spe_acc |
| 4375 | // 112: spefscr |
| 4376 | // 113: sfp |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 4377 | AssignToArrayRange(Builder, Address, Four8, 109, 113); |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4378 | |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 4379 | return false; |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4382 | // PowerPC-64 |
| 4383 | |
| 4384 | namespace { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4385 | /// PPC64_SVR4_ABIInfo - The 64-bit PowerPC ELF (SVR4) ABI information. |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4386 | class PPC64_SVR4_ABIInfo : public SwiftABIInfo { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4387 | public: |
| 4388 | enum ABIKind { |
| 4389 | ELFv1 = 0, |
| 4390 | ELFv2 |
| 4391 | }; |
| 4392 | |
| 4393 | private: |
| 4394 | static const unsigned GPRBits = 64; |
| 4395 | ABIKind Kind; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4396 | bool HasQPX; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4397 | bool IsSoftFloatABI; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4398 | |
| 4399 | // A vector of float or double will be promoted to <4 x f32> or <4 x f64> and |
| 4400 | // will be passed in a QPX register. |
| 4401 | bool IsQPXVectorTy(const Type *Ty) const { |
| 4402 | if (!HasQPX) |
| 4403 | return false; |
| 4404 | |
| 4405 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 4406 | unsigned NumElements = VT->getNumElements(); |
| 4407 | if (NumElements == 1) |
| 4408 | return false; |
| 4409 | |
| 4410 | if (VT->getElementType()->isSpecificBuiltinType(BuiltinType::Double)) { |
| 4411 | if (getContext().getTypeSize(Ty) <= 256) |
| 4412 | return true; |
| 4413 | } else if (VT->getElementType()-> |
| 4414 | isSpecificBuiltinType(BuiltinType::Float)) { |
| 4415 | if (getContext().getTypeSize(Ty) <= 128) |
| 4416 | return true; |
| 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | return false; |
| 4421 | } |
| 4422 | |
| 4423 | bool IsQPXVectorTy(QualType Ty) const { |
| 4424 | return IsQPXVectorTy(Ty.getTypePtr()); |
| 4425 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4426 | |
| 4427 | public: |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4428 | PPC64_SVR4_ABIInfo(CodeGen::CodeGenTypes &CGT, ABIKind Kind, bool HasQPX, |
| 4429 | bool SoftFloatABI) |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4430 | : SwiftABIInfo(CGT), Kind(Kind), HasQPX(HasQPX), |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4431 | IsSoftFloatABI(SoftFloatABI) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4432 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4433 | bool isPromotableTypeForABI(QualType Ty) const; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4434 | CharUnits getParamTypeAlignment(QualType Ty) const; |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4435 | |
| 4436 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 4437 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 4438 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4439 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 4440 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 4441 | uint64_t Members) const override; |
| 4442 | |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4443 | // TODO: We can add more logic to computeInfo to improve performance. |
| 4444 | // Example: For aggregate arguments that fit in a register, we could |
| 4445 | // use getDirectInReg (as is done below for structs containing a single |
| 4446 | // floating-point value) to avoid pushing them to memory on function |
| 4447 | // entry. This would require changing the logic in PPCISelLowering |
| 4448 | // when lowering the parameters in the caller and args in the callee. |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4449 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 4450 | if (!getCXXABI().classifyReturnType(FI)) |
| 4451 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4452 | for (auto &I : FI.arguments()) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4453 | // We rely on the default argument classification for the most part. |
| 4454 | // One exception: An aggregate containing a single floating-point |
Bill Schmidt | 179afae | 2013-07-23 22:15:57 +0000 | [diff] [blame] | 4455 | // 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] | 4456 | const Type *T = isSingleElementStruct(I.type, getContext()); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4457 | if (T) { |
| 4458 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4459 | if (IsQPXVectorTy(T) || |
| 4460 | (T->isVectorType() && getContext().getTypeSize(T) == 128) || |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4461 | (BT && BT->isFloatingPoint())) { |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4462 | QualType QT(T, 0); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4463 | I.info = ABIArgInfo::getDirectInReg(CGT.ConvertType(QT)); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4464 | continue; |
| 4465 | } |
| 4466 | } |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 4467 | I.info = classifyArgumentType(I.type); |
Bill Schmidt | 84d3779 | 2012-10-12 19:26:17 +0000 | [diff] [blame] | 4468 | } |
| 4469 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4470 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4471 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4472 | QualType Ty) const override; |
Bob Wilson | fa84fc9 | 2018-05-25 21:26:03 +0000 | [diff] [blame] | 4473 | |
| 4474 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
| 4475 | bool asReturnValue) const override { |
| 4476 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 4477 | } |
| 4478 | |
| 4479 | bool isSwiftErrorInRegister() const override { |
| 4480 | return false; |
| 4481 | } |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4482 | }; |
| 4483 | |
| 4484 | class PPC64_SVR4_TargetCodeGenInfo : public TargetCodeGenInfo { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4485 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4486 | public: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4487 | PPC64_SVR4_TargetCodeGenInfo(CodeGenTypes &CGT, |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4488 | PPC64_SVR4_ABIInfo::ABIKind Kind, bool HasQPX, |
| 4489 | bool SoftFloatABI) |
| 4490 | : TargetCodeGenInfo(new PPC64_SVR4_ABIInfo(CGT, Kind, HasQPX, |
| 4491 | SoftFloatABI)) {} |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4492 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4493 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4494 | // This is recovered from gcc output. |
| 4495 | return 1; // r1 is the dedicated stack pointer |
| 4496 | } |
| 4497 | |
| 4498 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4499 | llvm::Value *Address) const override; |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4500 | }; |
| 4501 | |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4502 | class PPC64TargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 4503 | public: |
| 4504 | PPC64TargetCodeGenInfo(CodeGenTypes &CGT) : DefaultTargetCodeGenInfo(CGT) {} |
| 4505 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4506 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4507 | // This is recovered from gcc output. |
| 4508 | return 1; // r1 is the dedicated stack pointer |
| 4509 | } |
| 4510 | |
| 4511 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 4512 | llvm::Value *Address) const override; |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4513 | }; |
| 4514 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4515 | } |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4516 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4517 | // Return true if the ABI requires Ty to be passed sign- or zero- |
| 4518 | // extended to 64 bits. |
| 4519 | bool |
| 4520 | PPC64_SVR4_ABIInfo::isPromotableTypeForABI(QualType Ty) const { |
| 4521 | // Treat an enum type as its underlying type. |
| 4522 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 4523 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 4524 | |
| 4525 | // Promotable integer types are required to be promoted by the ABI. |
| 4526 | if (Ty->isPromotableIntegerType()) |
| 4527 | return true; |
| 4528 | |
| 4529 | // In addition to the usual promotable integer types, we also need to |
| 4530 | // extend all 32-bit types, since the ABI requires promotion to 64 bits. |
| 4531 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 4532 | switch (BT->getKind()) { |
| 4533 | case BuiltinType::Int: |
| 4534 | case BuiltinType::UInt: |
| 4535 | return true; |
| 4536 | default: |
| 4537 | break; |
| 4538 | } |
| 4539 | |
| 4540 | return false; |
| 4541 | } |
| 4542 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4543 | /// isAlignedParamType - Determine whether a type requires 16-byte or |
| 4544 | /// higher alignment in the parameter area. Always returns at least 8. |
| 4545 | CharUnits PPC64_SVR4_ABIInfo::getParamTypeAlignment(QualType Ty) const { |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4546 | // Complex types are passed just like their elements. |
| 4547 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) |
| 4548 | Ty = CTy->getElementType(); |
| 4549 | |
| 4550 | // Only vector types of size 16 bytes need alignment (larger types are |
| 4551 | // passed via reference, smaller types are not aligned). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4552 | if (IsQPXVectorTy(Ty)) { |
| 4553 | if (getContext().getTypeSize(Ty) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4554 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4555 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4556 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4557 | } else if (Ty->isVectorType()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4558 | return CharUnits::fromQuantity(getContext().getTypeSize(Ty) == 128 ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4559 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4560 | |
| 4561 | // For single-element float/vector structs, we consider the whole type |
| 4562 | // to have the same alignment requirements as its single element. |
| 4563 | const Type *AlignAsType = nullptr; |
| 4564 | const Type *EltType = isSingleElementStruct(Ty, getContext()); |
| 4565 | if (EltType) { |
| 4566 | const BuiltinType *BT = EltType->getAs<BuiltinType>(); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4567 | if (IsQPXVectorTy(EltType) || (EltType->isVectorType() && |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4568 | getContext().getTypeSize(EltType) == 128) || |
| 4569 | (BT && BT->isFloatingPoint())) |
| 4570 | AlignAsType = EltType; |
| 4571 | } |
| 4572 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4573 | // Likewise for ELFv2 homogeneous aggregates. |
| 4574 | const Type *Base = nullptr; |
| 4575 | uint64_t Members = 0; |
| 4576 | if (!AlignAsType && Kind == ELFv2 && |
| 4577 | isAggregateTypeForABI(Ty) && isHomogeneousAggregate(Ty, Base, Members)) |
| 4578 | AlignAsType = Base; |
| 4579 | |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4580 | // With special case aggregates, only vector base types need alignment. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4581 | if (AlignAsType && IsQPXVectorTy(AlignAsType)) { |
| 4582 | if (getContext().getTypeSize(AlignAsType) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4583 | return CharUnits::fromQuantity(32); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4584 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4585 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4586 | } else if (AlignAsType) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4587 | return CharUnits::fromQuantity(AlignAsType->isVectorType() ? 16 : 8); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4588 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4589 | |
| 4590 | // Otherwise, we only need alignment for any aggregate type that |
| 4591 | // has an alignment requirement of >= 16 bytes. |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4592 | if (isAggregateTypeForABI(Ty) && getContext().getTypeAlign(Ty) >= 128) { |
| 4593 | if (HasQPX && getContext().getTypeAlign(Ty) >= 256) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4594 | return CharUnits::fromQuantity(32); |
| 4595 | return CharUnits::fromQuantity(16); |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4596 | } |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4597 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4598 | return CharUnits::fromQuantity(8); |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4601 | /// isHomogeneousAggregate - Return true if a type is an ELFv2 homogeneous |
| 4602 | /// aggregate. Base is set to the base element type, and Members is set |
| 4603 | /// to the number of base elements. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4604 | bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base, |
| 4605 | uint64_t &Members) const { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4606 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 4607 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 4608 | if (NElements == 0) |
| 4609 | return false; |
| 4610 | if (!isHomogeneousAggregate(AT->getElementType(), Base, Members)) |
| 4611 | return false; |
| 4612 | Members *= NElements; |
| 4613 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4614 | const RecordDecl *RD = RT->getDecl(); |
| 4615 | if (RD->hasFlexibleArrayMember()) |
| 4616 | return false; |
| 4617 | |
| 4618 | Members = 0; |
Ulrich Weigand | a094f04 | 2014-10-29 13:23:20 +0000 | [diff] [blame] | 4619 | |
| 4620 | // If this is a C++ record, check the bases first. |
| 4621 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 4622 | for (const auto &I : CXXRD->bases()) { |
| 4623 | // Ignore empty records. |
| 4624 | if (isEmptyRecord(getContext(), I.getType(), true)) |
| 4625 | continue; |
| 4626 | |
| 4627 | uint64_t FldMembers; |
| 4628 | if (!isHomogeneousAggregate(I.getType(), Base, FldMembers)) |
| 4629 | return false; |
| 4630 | |
| 4631 | Members += FldMembers; |
| 4632 | } |
| 4633 | } |
| 4634 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4635 | for (const auto *FD : RD->fields()) { |
| 4636 | // Ignore (non-zero arrays of) empty records. |
| 4637 | QualType FT = FD->getType(); |
| 4638 | while (const ConstantArrayType *AT = |
| 4639 | getContext().getAsConstantArrayType(FT)) { |
| 4640 | if (AT->getSize().getZExtValue() == 0) |
| 4641 | return false; |
| 4642 | FT = AT->getElementType(); |
| 4643 | } |
| 4644 | if (isEmptyRecord(getContext(), FT, true)) |
| 4645 | continue; |
| 4646 | |
| 4647 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
| 4648 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4649 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4650 | continue; |
| 4651 | |
| 4652 | uint64_t FldMembers; |
| 4653 | if (!isHomogeneousAggregate(FD->getType(), Base, FldMembers)) |
| 4654 | return false; |
| 4655 | |
| 4656 | Members = (RD->isUnion() ? |
| 4657 | std::max(Members, FldMembers) : Members + FldMembers); |
| 4658 | } |
| 4659 | |
| 4660 | if (!Base) |
| 4661 | return false; |
| 4662 | |
| 4663 | // Ensure there is no padding. |
| 4664 | if (getContext().getTypeSize(Base) * Members != |
| 4665 | getContext().getTypeSize(Ty)) |
| 4666 | return false; |
| 4667 | } else { |
| 4668 | Members = 1; |
| 4669 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) { |
| 4670 | Members = 2; |
| 4671 | Ty = CT->getElementType(); |
| 4672 | } |
| 4673 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4674 | // Most ABIs only support float, double, and some vector type widths. |
| 4675 | if (!isHomogeneousAggregateBaseType(Ty)) |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4676 | return false; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4677 | |
| 4678 | // The base type must be the same for all members. Types that |
| 4679 | // agree in both total size and mode (float vs. vector) are |
| 4680 | // treated as being equivalent here. |
| 4681 | const Type *TyPtr = Ty.getTypePtr(); |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4682 | if (!Base) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4683 | Base = TyPtr; |
Ahmed Bougacha | 40a34c2 | 2016-04-19 17:54:29 +0000 | [diff] [blame] | 4684 | // If it's a non-power-of-2 vector, its size is already a power-of-2, |
| 4685 | // so make sure to widen it explicitly. |
| 4686 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 4687 | QualType EltTy = VT->getElementType(); |
| 4688 | unsigned NumElements = |
| 4689 | getContext().getTypeSize(VT) / getContext().getTypeSize(EltTy); |
| 4690 | Base = getContext() |
| 4691 | .getVectorType(EltTy, NumElements, VT->getVectorKind()) |
| 4692 | .getTypePtr(); |
| 4693 | } |
| 4694 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4695 | |
| 4696 | if (Base->isVectorType() != TyPtr->isVectorType() || |
| 4697 | getContext().getTypeSize(Base) != getContext().getTypeSize(TyPtr)) |
| 4698 | return false; |
| 4699 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4700 | return Members > 0 && isHomogeneousAggregateSmallEnough(Base, Members); |
| 4701 | } |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4702 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4703 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 4704 | // Homogeneous aggregates for ELFv2 must have base types of float, |
| 4705 | // double, long double, or 128-bit vectors. |
| 4706 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 4707 | if (BT->getKind() == BuiltinType::Float || |
| 4708 | BT->getKind() == BuiltinType::Double || |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4709 | BT->getKind() == BuiltinType::LongDouble || |
| 4710 | (getContext().getTargetInfo().hasFloat128Type() && |
| 4711 | (BT->getKind() == BuiltinType::Float128))) { |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4712 | if (IsSoftFloatABI) |
| 4713 | return false; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4714 | return true; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 4715 | } |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4716 | } |
| 4717 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4718 | if (getContext().getTypeSize(VT) == 128 || IsQPXVectorTy(Ty)) |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4719 | return true; |
| 4720 | } |
| 4721 | return false; |
| 4722 | } |
| 4723 | |
| 4724 | bool PPC64_SVR4_ABIInfo::isHomogeneousAggregateSmallEnough( |
| 4725 | const Type *Base, uint64_t Members) const { |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4726 | // Vector and fp128 types require one register, other floating point types |
| 4727 | // require one or two registers depending on their size. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4728 | uint32_t NumRegs = |
Lei Huang | 449252d | 2018-07-05 04:32:01 +0000 | [diff] [blame] | 4729 | ((getContext().getTargetInfo().hasFloat128Type() && |
| 4730 | Base->isFloat128Type()) || |
| 4731 | Base->isVectorType()) ? 1 |
| 4732 | : (getContext().getTypeSize(Base) + 63) / 64; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4733 | |
| 4734 | // Homogeneous Aggregates may occupy at most 8 registers. |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 4735 | return Members * NumRegs <= 8; |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4738 | ABIArgInfo |
| 4739 | PPC64_SVR4_ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 4740 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 4741 | |
Bill Schmidt | 90b22c9 | 2012-11-27 02:46:43 +0000 | [diff] [blame] | 4742 | if (Ty->isAnyComplexType()) |
| 4743 | return ABIArgInfo::getDirect(); |
| 4744 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4745 | // Non-Altivec vector types are passed in GPRs (smaller than 16 bytes) |
| 4746 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4747 | if (Ty->isVectorType() && !IsQPXVectorTy(Ty)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4748 | uint64_t Size = getContext().getTypeSize(Ty); |
| 4749 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4750 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4751 | else if (Size < 128) { |
| 4752 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4753 | return ABIArgInfo::getDirect(CoerceTy); |
| 4754 | } |
| 4755 | } |
| 4756 | |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4757 | if (isAggregateTypeForABI(Ty)) { |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 4758 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4759 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4760 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4761 | uint64_t ABIAlign = getParamTypeAlignment(Ty).getQuantity(); |
| 4762 | uint64_t TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4763 | |
| 4764 | // ELFv2 homogeneous aggregates are passed as array types. |
| 4765 | const Type *Base = nullptr; |
| 4766 | uint64_t Members = 0; |
| 4767 | if (Kind == ELFv2 && |
| 4768 | isHomogeneousAggregate(Ty, Base, Members)) { |
| 4769 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4770 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4771 | return ABIArgInfo::getDirect(CoerceTy); |
| 4772 | } |
| 4773 | |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4774 | // If an aggregate may end up fully in registers, we do not |
| 4775 | // use the ByVal method, but pass the aggregate as array. |
| 4776 | // This is usually beneficial since we avoid forcing the |
| 4777 | // back-end to store the argument to memory. |
| 4778 | uint64_t Bits = getContext().getTypeSize(Ty); |
| 4779 | if (Bits > 0 && Bits <= 8 * GPRBits) { |
| 4780 | llvm::Type *CoerceTy; |
| 4781 | |
| 4782 | // Types up to 8 bytes are passed as integer type (which will be |
| 4783 | // properly aligned in the argument save area doubleword). |
| 4784 | if (Bits <= GPRBits) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4785 | CoerceTy = |
| 4786 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4787 | // Larger types are passed as arrays, with the base type selected |
| 4788 | // according to the required alignment in the save area. |
| 4789 | else { |
| 4790 | uint64_t RegBits = ABIAlign * 8; |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4791 | uint64_t NumRegs = llvm::alignTo(Bits, RegBits) / RegBits; |
Ulrich Weigand | 601957f | 2014-07-21 00:56:36 +0000 | [diff] [blame] | 4792 | llvm::Type *RegTy = llvm::IntegerType::get(getVMContext(), RegBits); |
| 4793 | CoerceTy = llvm::ArrayType::get(RegTy, NumRegs); |
| 4794 | } |
| 4795 | |
| 4796 | return ABIArgInfo::getDirect(CoerceTy); |
| 4797 | } |
| 4798 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4799 | // All other aggregates are passed ByVal. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4800 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 4801 | /*ByVal=*/true, |
Ulrich Weigand | 581badc | 2014-07-10 17:20:07 +0000 | [diff] [blame] | 4802 | /*Realign=*/TyAlign > ABIAlign); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4805 | return (isPromotableTypeForABI(Ty) ? ABIArgInfo::getExtend(Ty) |
| 4806 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | ABIArgInfo |
| 4810 | PPC64_SVR4_ABIInfo::classifyReturnType(QualType RetTy) const { |
| 4811 | if (RetTy->isVoidType()) |
| 4812 | return ABIArgInfo::getIgnore(); |
| 4813 | |
Bill Schmidt | a3d121c | 2012-12-17 04:20:17 +0000 | [diff] [blame] | 4814 | if (RetTy->isAnyComplexType()) |
| 4815 | return ABIArgInfo::getDirect(); |
| 4816 | |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4817 | // Non-Altivec vector types are returned in GPRs (smaller than 16 bytes) |
| 4818 | // or via reference (larger than 16 bytes). |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 4819 | if (RetTy->isVectorType() && !IsQPXVectorTy(RetTy)) { |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4820 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 4821 | if (Size > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4822 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | f4eba98 | 2014-07-10 16:39:01 +0000 | [diff] [blame] | 4823 | else if (Size < 128) { |
| 4824 | llvm::Type *CoerceTy = llvm::IntegerType::get(getVMContext(), Size); |
| 4825 | return ABIArgInfo::getDirect(CoerceTy); |
| 4826 | } |
| 4827 | } |
| 4828 | |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4829 | if (isAggregateTypeForABI(RetTy)) { |
| 4830 | // ELFv2 homogeneous aggregates are returned as array types. |
| 4831 | const Type *Base = nullptr; |
| 4832 | uint64_t Members = 0; |
| 4833 | if (Kind == ELFv2 && |
| 4834 | isHomogeneousAggregate(RetTy, Base, Members)) { |
| 4835 | llvm::Type *BaseTy = CGT.ConvertType(QualType(Base, 0)); |
| 4836 | llvm::Type *CoerceTy = llvm::ArrayType::get(BaseTy, Members); |
| 4837 | return ABIArgInfo::getDirect(CoerceTy); |
| 4838 | } |
| 4839 | |
| 4840 | // ELFv2 small aggregates are returned in up to two registers. |
| 4841 | uint64_t Bits = getContext().getTypeSize(RetTy); |
| 4842 | if (Kind == ELFv2 && Bits <= 2 * GPRBits) { |
| 4843 | if (Bits == 0) |
| 4844 | return ABIArgInfo::getIgnore(); |
| 4845 | |
| 4846 | llvm::Type *CoerceTy; |
| 4847 | if (Bits > GPRBits) { |
| 4848 | CoerceTy = llvm::IntegerType::get(getVMContext(), GPRBits); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 4849 | CoerceTy = llvm::StructType::get(CoerceTy, CoerceTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4850 | } else |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 4851 | CoerceTy = |
| 4852 | llvm::IntegerType::get(getVMContext(), llvm::alignTo(Bits, 8)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4853 | return ABIArgInfo::getDirect(CoerceTy); |
| 4854 | } |
| 4855 | |
| 4856 | // All other aggregates are returned indirectly. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4857 | return getNaturalAlignIndirect(RetTy); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 4858 | } |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4859 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 4860 | return (isPromotableTypeForABI(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 4861 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 77ed89d | 2012-11-05 19:13:42 +0000 | [diff] [blame] | 4862 | } |
| 4863 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4864 | // Based on ARMABIInfo::EmitVAArg, adjusted for 64-bit machine. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4865 | Address PPC64_SVR4_ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 4866 | QualType Ty) const { |
| 4867 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 4868 | TypeInfo.second = getParamTypeAlignment(Ty); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4869 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4870 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4871 | |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4872 | // If we have a complex type and the base type is smaller than 8 bytes, |
| 4873 | // the ABI calls for the real and imaginary parts to be right-adjusted |
| 4874 | // in separate doublewords. However, Clang expects us to produce a |
| 4875 | // pointer to a structure with the two parts packed tightly. So generate |
| 4876 | // loads of the real and imaginary parts relative to the va_list pointer, |
| 4877 | // and store them to a temporary structure. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4878 | if (const ComplexType *CTy = Ty->getAs<ComplexType>()) { |
| 4879 | CharUnits EltSize = TypeInfo.first / 2; |
| 4880 | if (EltSize < SlotSize) { |
| 4881 | Address Addr = emitVoidPtrDirectVAArg(CGF, VAListAddr, CGF.Int8Ty, |
| 4882 | SlotSize * 2, SlotSize, |
| 4883 | SlotSize, /*AllowHigher*/ true); |
| 4884 | |
| 4885 | Address RealAddr = Addr; |
| 4886 | Address ImagAddr = RealAddr; |
| 4887 | if (CGF.CGM.getDataLayout().isBigEndian()) { |
| 4888 | RealAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, |
| 4889 | SlotSize - EltSize); |
| 4890 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(ImagAddr, |
| 4891 | 2 * SlotSize - EltSize); |
| 4892 | } else { |
| 4893 | ImagAddr = CGF.Builder.CreateConstInBoundsByteGEP(RealAddr, SlotSize); |
| 4894 | } |
| 4895 | |
| 4896 | llvm::Type *EltTy = CGF.ConvertTypeForMem(CTy->getElementType()); |
| 4897 | RealAddr = CGF.Builder.CreateElementBitCast(RealAddr, EltTy); |
| 4898 | ImagAddr = CGF.Builder.CreateElementBitCast(ImagAddr, EltTy); |
| 4899 | llvm::Value *Real = CGF.Builder.CreateLoad(RealAddr, ".vareal"); |
| 4900 | llvm::Value *Imag = CGF.Builder.CreateLoad(ImagAddr, ".vaimag"); |
| 4901 | |
| 4902 | Address Temp = CGF.CreateMemTemp(Ty, "vacplx"); |
| 4903 | CGF.EmitStoreOfComplex({Real, Imag}, CGF.MakeAddrLValue(Temp, Ty), |
| 4904 | /*init*/ true); |
| 4905 | return Temp; |
Ulrich Weigand | bebc55b | 2014-06-20 16:37:40 +0000 | [diff] [blame] | 4906 | } |
Bill Schmidt | 924c478 | 2013-01-14 17:45:36 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4909 | // Otherwise, just use the general rule. |
| 4910 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*Indirect*/ false, |
| 4911 | TypeInfo, SlotSize, /*AllowHigher*/ true); |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | static bool |
| 4915 | PPC64_initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4916 | llvm::Value *Address) { |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4917 | // This is calculated from the LLVM and GCC tables and verified |
| 4918 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 4919 | |
| 4920 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 4921 | |
| 4922 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 4923 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 4924 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 4925 | llvm::Value *Sixteen8 = llvm::ConstantInt::get(i8, 16); |
| 4926 | |
| 4927 | // 0-31: r0-31, the 8-byte general-purpose registers |
| 4928 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 4929 | |
| 4930 | // 32-63: fp0-31, the 8-byte floating-point registers |
| 4931 | AssignToArrayRange(Builder, Address, Eight8, 32, 63); |
| 4932 | |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4933 | // 64-67 are various 8-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4934 | // 64: mq |
| 4935 | // 65: lr |
| 4936 | // 66: ctr |
| 4937 | // 67: ap |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4938 | AssignToArrayRange(Builder, Address, Eight8, 64, 67); |
| 4939 | |
| 4940 | // 68-76 are various 4-byte special-purpose registers: |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4941 | // 68-75 cr0-7 |
| 4942 | // 76: xer |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4943 | AssignToArrayRange(Builder, Address, Four8, 68, 76); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4944 | |
| 4945 | // 77-108: v0-31, the 16-byte vector registers |
| 4946 | AssignToArrayRange(Builder, Address, Sixteen8, 77, 108); |
| 4947 | |
| 4948 | // 109: vrsave |
| 4949 | // 110: vscr |
| 4950 | // 111: spe_acc |
| 4951 | // 112: spefscr |
| 4952 | // 113: sfp |
Hal Finkel | 84832a7 | 2016-08-30 02:38:34 +0000 | [diff] [blame] | 4953 | // 114: tfhar |
| 4954 | // 115: tfiar |
| 4955 | // 116: texasr |
| 4956 | AssignToArrayRange(Builder, Address, Eight8, 109, 116); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 4957 | |
| 4958 | return false; |
| 4959 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 4960 | |
Bill Schmidt | 25cb349 | 2012-10-03 19:18:57 +0000 | [diff] [blame] | 4961 | bool |
| 4962 | PPC64_SVR4_TargetCodeGenInfo::initDwarfEHRegSizeTable( |
| 4963 | CodeGen::CodeGenFunction &CGF, |
| 4964 | llvm::Value *Address) const { |
| 4965 | |
| 4966 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4967 | } |
| 4968 | |
| 4969 | bool |
| 4970 | PPC64TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 4971 | llvm::Value *Address) const { |
| 4972 | |
| 4973 | return PPC64_initDwarfEHRegSizeTable(CGF, Address); |
| 4974 | } |
| 4975 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 4976 | //===----------------------------------------------------------------------===// |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 4977 | // AArch64 ABI Implementation |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4978 | //===----------------------------------------------------------------------===// |
| 4979 | |
| 4980 | namespace { |
| 4981 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4982 | class AArch64ABIInfo : public SwiftABIInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4983 | public: |
| 4984 | enum ABIKind { |
| 4985 | AAPCS = 0, |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 4986 | DarwinPCS, |
| 4987 | Win64 |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4988 | }; |
| 4989 | |
| 4990 | private: |
| 4991 | ABIKind Kind; |
| 4992 | |
| 4993 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 4994 | AArch64ABIInfo(CodeGenTypes &CGT, ABIKind Kind) |
| 4995 | : SwiftABIInfo(CGT), Kind(Kind) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 4996 | |
| 4997 | private: |
| 4998 | ABIKind getABIKind() const { return Kind; } |
| 4999 | bool isDarwinPCS() const { return Kind == DarwinPCS; } |
| 5000 | |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5001 | ABIArgInfo classifyReturnType(QualType RetTy, bool IsVariadic) const; |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5002 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5003 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5004 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5005 | uint64_t Members) const override; |
| 5006 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5007 | bool isIllegalVectorType(QualType Ty) const; |
| 5008 | |
David Blaikie | 1cbb971 | 2014-11-14 19:09:44 +0000 | [diff] [blame] | 5009 | void computeInfo(CGFunctionInfo &FI) const override { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5010 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5011 | FI.getReturnInfo() = |
| 5012 | classifyReturnType(FI.getReturnType(), FI.isVariadic()); |
Tim Northover | 5ffc092 | 2014-04-17 10:20:38 +0000 | [diff] [blame] | 5013 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5014 | for (auto &it : FI.arguments()) |
| 5015 | it.info = classifyArgumentType(it.type); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5018 | Address EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5019 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5020 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5021 | Address EmitAAPCSVAArg(Address VAListAddr, QualType Ty, |
| 5022 | CodeGenFunction &CGF) const; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5023 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5024 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5025 | QualType Ty) const override { |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5026 | return Kind == Win64 ? EmitMSVAArg(CGF, VAListAddr, Ty) |
| 5027 | : isDarwinPCS() ? EmitDarwinVAArg(VAListAddr, Ty, CGF) |
| 5028 | : EmitAAPCSVAArg(VAListAddr, Ty, CGF); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5029 | } |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5030 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5031 | Address EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5032 | QualType Ty) const override; |
| 5033 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5034 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5035 | bool asReturnValue) const override { |
| 5036 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5037 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5038 | bool isSwiftErrorInRegister() const override { |
| 5039 | return true; |
| 5040 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5041 | |
| 5042 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5043 | unsigned elts) const override; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5044 | }; |
| 5045 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5046 | class AArch64TargetCodeGenInfo : public TargetCodeGenInfo { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5047 | public: |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5048 | AArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind Kind) |
| 5049 | : TargetCodeGenInfo(new AArch64ABIInfo(CGT, Kind)) {} |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5050 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5051 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5052 | return "mov\tfp, fp\t\t// marker for objc_retainAutoreleaseReturnValue"; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5055 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
| 5056 | return 31; |
| 5057 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5058 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 5059 | bool doesReturnSlotInterfereWithArgs() const override { return false; } |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5060 | |
| 5061 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5062 | CodeGen::CodeGenModule &CGM) const override { |
| 5063 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 5064 | if (!FD) |
| 5065 | return; |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5066 | |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 5067 | CodeGenOptions::SignReturnAddressScope Scope = CGM.getCodeGenOpts().getSignReturnAddress(); |
| 5068 | CodeGenOptions::SignReturnAddressKeyValue Key = CGM.getCodeGenOpts().getSignReturnAddressKey(); |
| 5069 | bool BranchTargetEnforcement = CGM.getCodeGenOpts().BranchTargetEnforcement; |
| 5070 | if (const auto *TA = FD->getAttr<TargetAttr>()) { |
Craig Topper | 505aa24 | 2019-12-09 10:34:24 -0800 | [diff] [blame] | 5071 | ParsedTargetAttr Attr = TA->parse(); |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 5072 | if (!Attr.BranchProtection.empty()) { |
| 5073 | TargetInfo::BranchProtectionInfo BPI; |
| 5074 | StringRef Error; |
| 5075 | (void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection, |
| 5076 | BPI, Error); |
| 5077 | assert(Error.empty()); |
| 5078 | Scope = BPI.SignReturnAddr; |
| 5079 | Key = BPI.SignKey; |
| 5080 | BranchTargetEnforcement = BPI.BranchTargetEnforcement; |
| 5081 | } |
| 5082 | } |
| 5083 | |
| 5084 | auto *Fn = cast<llvm::Function>(GV); |
| 5085 | if (Scope != CodeGenOptions::SignReturnAddressScope::None) { |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5086 | Fn->addFnAttr("sign-return-address", |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 5087 | Scope == CodeGenOptions::SignReturnAddressScope::All |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5088 | ? "all" |
| 5089 | : "non-leaf"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5090 | |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5091 | Fn->addFnAttr("sign-return-address-key", |
| 5092 | Key == CodeGenOptions::SignReturnAddressKeyValue::AKey |
| 5093 | ? "a_key" |
| 5094 | : "b_key"); |
| 5095 | } |
| 5096 | |
Momchil Velikov | aa6d48f | 2019-11-15 11:34:47 +0000 | [diff] [blame] | 5097 | if (BranchTargetEnforcement) |
Luke Cheeseman | a8a24aa | 2018-10-25 15:23:49 +0000 | [diff] [blame] | 5098 | Fn->addFnAttr("branch-target-enforcement"); |
Luke Cheeseman | 0ac44c1 | 2018-08-17 12:55:05 +0000 | [diff] [blame] | 5099 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5100 | }; |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5101 | |
| 5102 | class WindowsAArch64TargetCodeGenInfo : public AArch64TargetCodeGenInfo { |
| 5103 | public: |
| 5104 | WindowsAArch64TargetCodeGenInfo(CodeGenTypes &CGT, AArch64ABIInfo::ABIKind K) |
| 5105 | : AArch64TargetCodeGenInfo(CGT, K) {} |
| 5106 | |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5107 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 5108 | CodeGen::CodeGenModule &CGM) const override; |
| 5109 | |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 5110 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5111 | llvm::SmallString<24> &Opt) const override { |
| 5112 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5113 | } |
| 5114 | |
| 5115 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5116 | llvm::SmallString<32> &Opt) const override { |
| 5117 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5118 | } |
| 5119 | }; |
Eli Friedman | 540be6d | 2018-10-26 01:31:57 +0000 | [diff] [blame] | 5120 | |
| 5121 | void WindowsAArch64TargetCodeGenInfo::setTargetAttributes( |
| 5122 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5123 | AArch64TargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5124 | if (GV->isDeclaration()) |
| 5125 | return; |
| 5126 | addStackProbeTargetAttributes(D, GV, CGM); |
| 5127 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5128 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5129 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5130 | ABIArgInfo AArch64ABIInfo::classifyArgumentType(QualType Ty) const { |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5131 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5132 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5133 | // Handle illegal vector types here. |
| 5134 | if (isIllegalVectorType(Ty)) { |
| 5135 | uint64_t Size = getContext().getTypeSize(Ty); |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5136 | // Android promotes <2 x i8> to i16, not i32 |
Ahmed Bougacha | 8862cae | 2016-04-19 17:54:24 +0000 | [diff] [blame] | 5137 | if (isAndroid() && (Size <= 16)) { |
Nirav Dave | 9a8f97e | 2016-02-22 16:48:42 +0000 | [diff] [blame] | 5138 | llvm::Type *ResType = llvm::Type::getInt16Ty(getVMContext()); |
| 5139 | return ABIArgInfo::getDirect(ResType); |
| 5140 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5141 | if (Size <= 32) { |
| 5142 | llvm::Type *ResType = llvm::Type::getInt32Ty(getVMContext()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5143 | return ABIArgInfo::getDirect(ResType); |
| 5144 | } |
| 5145 | if (Size == 64) { |
| 5146 | llvm::Type *ResType = |
| 5147 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 2); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5148 | return ABIArgInfo::getDirect(ResType); |
| 5149 | } |
| 5150 | if (Size == 128) { |
| 5151 | llvm::Type *ResType = |
| 5152 | llvm::VectorType::get(llvm::Type::getInt32Ty(getVMContext()), 4); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5153 | return ABIArgInfo::getDirect(ResType); |
| 5154 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5155 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5156 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5157 | |
| 5158 | if (!isAggregateTypeForABI(Ty)) { |
| 5159 | // Treat an enum type as its underlying type. |
| 5160 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 5161 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 5162 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5163 | return (Ty->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5164 | ? ABIArgInfo::getExtend(Ty) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5165 | : ABIArgInfo::getDirect()); |
| 5166 | } |
| 5167 | |
| 5168 | // Structures with either a non-trivial destructor or a non-trivial |
| 5169 | // copy constructor are always indirect. |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 5170 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5171 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 5172 | CGCXXABI::RAA_DirectInMemory); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | // Empty records are always ignored on Darwin, but actually passed in C++ mode |
| 5176 | // elsewhere for GNU compatibility. |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5177 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5178 | bool IsEmpty = isEmptyRecord(getContext(), Ty, true); |
| 5179 | if (IsEmpty || Size == 0) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5180 | if (!getContext().getLangOpts().CPlusPlus || isDarwinPCS()) |
| 5181 | return ABIArgInfo::getIgnore(); |
| 5182 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5183 | // GNU C mode. The only argument that gets ignored is an empty one with size |
| 5184 | // 0. |
| 5185 | if (IsEmpty && Size == 0) |
| 5186 | return ABIArgInfo::getIgnore(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5187 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 5188 | } |
| 5189 | |
| 5190 | // Homogeneous Floating-point Aggregates (HFAs) need to be expanded. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5191 | const Type *Base = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5192 | uint64_t Members = 0; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5193 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5194 | return ABIArgInfo::getDirect( |
| 5195 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5196 | } |
| 5197 | |
| 5198 | // Aggregates <= 16 bytes are passed directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5199 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5200 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5201 | // same size and alignment. |
| 5202 | if (getTarget().isRenderScriptTarget()) { |
| 5203 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5204 | } |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5205 | unsigned Alignment; |
| 5206 | if (Kind == AArch64ABIInfo::AAPCS) { |
| 5207 | Alignment = getContext().getTypeUnadjustedAlign(Ty); |
| 5208 | Alignment = Alignment < 128 ? 64 : 128; |
| 5209 | } else { |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5210 | Alignment = std::max(getContext().getTypeAlign(Ty), |
| 5211 | (unsigned)getTarget().getPointerWidth(0)); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5212 | } |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5213 | Size = llvm::alignTo(Size, Alignment); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5214 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5215 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5216 | // For aggregates with 16-byte alignment, we use i128. |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5217 | llvm::Type *BaseTy = llvm::Type::getIntNTy(getVMContext(), Alignment); |
| 5218 | return ABIArgInfo::getDirect( |
| 5219 | Size == Alignment ? BaseTy |
| 5220 | : llvm::ArrayType::get(BaseTy, Size / Alignment)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5223 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5226 | ABIArgInfo AArch64ABIInfo::classifyReturnType(QualType RetTy, |
| 5227 | bool IsVariadic) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5228 | if (RetTy->isVoidType()) |
| 5229 | return ABIArgInfo::getIgnore(); |
| 5230 | |
| 5231 | // Large vector types should be returned via memory. |
| 5232 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 128) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5233 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5234 | |
| 5235 | if (!isAggregateTypeForABI(RetTy)) { |
| 5236 | // Treat an enum type as its underlying type. |
| 5237 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 5238 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 5239 | |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5240 | return (RetTy->isPromotableIntegerType() && isDarwinPCS() |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5241 | ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 4dab698 | 2014-04-18 13:46:08 +0000 | [diff] [blame] | 5242 | : ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5243 | } |
| 5244 | |
Tim Northover | 23bcad2 | 2017-05-05 22:36:06 +0000 | [diff] [blame] | 5245 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 5246 | if (isEmptyRecord(getContext(), RetTy, true) || Size == 0) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5247 | return ABIArgInfo::getIgnore(); |
| 5248 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5249 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5250 | uint64_t Members = 0; |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5251 | if (isHomogeneousAggregate(RetTy, Base, Members) && |
| 5252 | !(getTarget().getTriple().getArch() == llvm::Triple::aarch64_32 && |
| 5253 | IsVariadic)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5254 | // Homogeneous Floating-point Aggregates (HFAs) are returned directly. |
| 5255 | return ABIArgInfo::getDirect(); |
| 5256 | |
| 5257 | // Aggregates <= 16 bytes are returned directly in registers or on the stack. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5258 | if (Size <= 128) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5259 | // On RenderScript, coerce Aggregates <= 16 bytes to an integer array of |
| 5260 | // same size and alignment. |
| 5261 | if (getTarget().isRenderScriptTarget()) { |
| 5262 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 5263 | } |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5264 | unsigned Alignment = getContext().getTypeAlign(RetTy); |
Davide Italiano | 7a3b69d | 2017-04-03 16:51:39 +0000 | [diff] [blame] | 5265 | Size = llvm::alignTo(Size, 64); // round up to multiple of 8 bytes |
Pete Cooper | 635b509 | 2015-04-17 22:16:24 +0000 | [diff] [blame] | 5266 | |
| 5267 | // We use a pair of i64 for 16-byte aggregate with 8-byte alignment. |
| 5268 | // For aggregates with 16-byte alignment, we use i128. |
| 5269 | if (Alignment < 128 && Size == 128) { |
| 5270 | llvm::Type *BaseTy = llvm::Type::getInt64Ty(getVMContext()); |
| 5271 | return ABIArgInfo::getDirect(llvm::ArrayType::get(BaseTy, Size / 64)); |
| 5272 | } |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5273 | return ABIArgInfo::getDirect(llvm::IntegerType::get(getVMContext(), Size)); |
| 5274 | } |
| 5275 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5276 | return getNaturalAlignIndirect(RetTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5277 | } |
| 5278 | |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 5279 | /// isIllegalVectorType - check whether the vector type is legal for AArch64. |
| 5280 | bool AArch64ABIInfo::isIllegalVectorType(QualType Ty) const { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5281 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5282 | // Check whether VT is legal. |
| 5283 | unsigned NumElements = VT->getNumElements(); |
| 5284 | uint64_t Size = getContext().getTypeSize(VT); |
Tim Northover | 34fd4fb | 2016-05-03 19:24:47 +0000 | [diff] [blame] | 5285 | // NumElements should be power of 2. |
Tim Northover | 360d2b3 | 2016-05-03 19:22:41 +0000 | [diff] [blame] | 5286 | if (!llvm::isPowerOf2_32(NumElements)) |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5287 | return true; |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5288 | |
| 5289 | // arm64_32 has to be compatible with the ARM logic here, which allows huge |
| 5290 | // vectors for some reason. |
| 5291 | llvm::Triple Triple = getTarget().getTriple(); |
| 5292 | if (Triple.getArch() == llvm::Triple::aarch64_32 && |
| 5293 | Triple.isOSBinFormatMachO()) |
| 5294 | return Size <= 32; |
| 5295 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5296 | return Size != 64 && (Size != 128 || NumElements == 1); |
| 5297 | } |
| 5298 | return false; |
| 5299 | } |
| 5300 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5301 | bool AArch64ABIInfo::isLegalVectorTypeForSwift(CharUnits totalSize, |
| 5302 | llvm::Type *eltTy, |
| 5303 | unsigned elts) const { |
| 5304 | if (!llvm::isPowerOf2_32(elts)) |
| 5305 | return false; |
| 5306 | if (totalSize.getQuantity() != 8 && |
| 5307 | (totalSize.getQuantity() != 16 || elts == 1)) |
| 5308 | return false; |
| 5309 | return true; |
| 5310 | } |
| 5311 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5312 | bool AArch64ABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 5313 | // Homogeneous aggregates for AAPCS64 must have base types of a floating |
| 5314 | // point type or a short-vector type. This is the same as the 32-bit ABI, |
| 5315 | // but with the difference that any floating-point type is allowed, |
| 5316 | // including __fp16. |
| 5317 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 5318 | if (BT->isFloatingPoint()) |
| 5319 | return true; |
| 5320 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 5321 | unsigned VecSize = getContext().getTypeSize(VT); |
| 5322 | if (VecSize == 64 || VecSize == 128) |
| 5323 | return true; |
| 5324 | } |
| 5325 | return false; |
| 5326 | } |
| 5327 | |
| 5328 | bool AArch64ABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 5329 | uint64_t Members) const { |
| 5330 | return Members <= 4; |
| 5331 | } |
| 5332 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5333 | Address AArch64ABIInfo::EmitAAPCSVAArg(Address VAListAddr, |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5334 | QualType Ty, |
| 5335 | CodeGenFunction &CGF) const { |
| 5336 | ABIArgInfo AI = classifyArgumentType(Ty); |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5337 | bool IsIndirect = AI.isIndirect(); |
| 5338 | |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5339 | llvm::Type *BaseTy = CGF.ConvertType(Ty); |
| 5340 | if (IsIndirect) |
| 5341 | BaseTy = llvm::PointerType::getUnqual(BaseTy); |
| 5342 | else if (AI.getCoerceToType()) |
| 5343 | BaseTy = AI.getCoerceToType(); |
| 5344 | |
| 5345 | unsigned NumRegs = 1; |
| 5346 | if (llvm::ArrayType *ArrTy = dyn_cast<llvm::ArrayType>(BaseTy)) { |
| 5347 | BaseTy = ArrTy->getElementType(); |
| 5348 | NumRegs = ArrTy->getNumElements(); |
| 5349 | } |
| 5350 | bool IsFPR = BaseTy->isFloatingPointTy() || BaseTy->isVectorTy(); |
| 5351 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5352 | // The AArch64 va_list type and handling is specified in the Procedure Call |
| 5353 | // Standard, section B.4: |
| 5354 | // |
| 5355 | // struct { |
| 5356 | // void *__stack; |
| 5357 | // void *__gr_top; |
| 5358 | // void *__vr_top; |
| 5359 | // int __gr_offs; |
| 5360 | // int __vr_offs; |
| 5361 | // }; |
| 5362 | |
| 5363 | llvm::BasicBlock *MaybeRegBlock = CGF.createBasicBlock("vaarg.maybe_reg"); |
| 5364 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 5365 | llvm::BasicBlock *OnStackBlock = CGF.createBasicBlock("vaarg.on_stack"); |
| 5366 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5367 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5368 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 5369 | CharUnits TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5370 | |
| 5371 | Address reg_offs_p = Address::invalid(); |
| 5372 | llvm::Value *reg_offs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5373 | int reg_top_index; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5374 | int RegSize = IsIndirect ? 8 : TySize.getQuantity(); |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5375 | if (!IsFPR) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5376 | // 3 is the field number of __gr_offs |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5377 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 3, "gr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5378 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "gr_offs"); |
| 5379 | reg_top_index = 1; // field number for __gr_top |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 5380 | RegSize = llvm::alignTo(RegSize, 8); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5381 | } else { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5382 | // 4 is the field number of __vr_offs. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5383 | reg_offs_p = CGF.Builder.CreateStructGEP(VAListAddr, 4, "vr_offs_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5384 | reg_offs = CGF.Builder.CreateLoad(reg_offs_p, "vr_offs"); |
| 5385 | reg_top_index = 2; // field number for __vr_top |
Tim Northover | b047bfa | 2014-11-27 21:02:49 +0000 | [diff] [blame] | 5386 | RegSize = 16 * NumRegs; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | //======================================= |
| 5390 | // Find out where argument was passed |
| 5391 | //======================================= |
| 5392 | |
| 5393 | // If reg_offs >= 0 we're already using the stack for this type of |
| 5394 | // argument. We don't want to keep updating reg_offs (in case it overflows, |
| 5395 | // though anyone passing 2GB of arguments, each at most 16 bytes, deserves |
| 5396 | // whatever they get). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5397 | llvm::Value *UsingStack = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5398 | UsingStack = CGF.Builder.CreateICmpSGE( |
| 5399 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, 0)); |
| 5400 | |
| 5401 | CGF.Builder.CreateCondBr(UsingStack, OnStackBlock, MaybeRegBlock); |
| 5402 | |
| 5403 | // 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] | 5404 | // question is whether this particular type is too big. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5405 | CGF.EmitBlock(MaybeRegBlock); |
| 5406 | |
| 5407 | // Integer arguments may need to correct register alignment (for example a |
| 5408 | // "struct { __int128 a; };" gets passed in x_2N, x_{2N+1}). In this case we |
| 5409 | // align __gr_offs to calculate the potential address. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5410 | if (!IsFPR && !IsIndirect && TyAlign.getQuantity() > 8) { |
| 5411 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5412 | |
| 5413 | reg_offs = CGF.Builder.CreateAdd( |
| 5414 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, Align - 1), |
| 5415 | "align_regoffs"); |
| 5416 | reg_offs = CGF.Builder.CreateAnd( |
| 5417 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, -Align), |
| 5418 | "aligned_regoffs"); |
| 5419 | } |
| 5420 | |
| 5421 | // Update the gr_offs/vr_offs pointer for next call to va_arg on this va_list. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5422 | // The fact that this is done unconditionally reflects the fact that |
| 5423 | // allocating an argument to the stack also uses up all the remaining |
| 5424 | // registers of the appropriate kind. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5425 | llvm::Value *NewOffset = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5426 | NewOffset = CGF.Builder.CreateAdd( |
| 5427 | reg_offs, llvm::ConstantInt::get(CGF.Int32Ty, RegSize), "new_reg_offs"); |
| 5428 | CGF.Builder.CreateStore(NewOffset, reg_offs_p); |
| 5429 | |
| 5430 | // Now we're in a position to decide whether this argument really was in |
| 5431 | // registers or not. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5432 | llvm::Value *InRegs = nullptr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5433 | InRegs = CGF.Builder.CreateICmpSLE( |
| 5434 | NewOffset, llvm::ConstantInt::get(CGF.Int32Ty, 0), "inreg"); |
| 5435 | |
| 5436 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, OnStackBlock); |
| 5437 | |
| 5438 | //======================================= |
| 5439 | // Argument was in registers |
| 5440 | //======================================= |
| 5441 | |
| 5442 | // Now we emit the code for if the argument was originally passed in |
| 5443 | // registers. First start the appropriate block: |
| 5444 | CGF.EmitBlock(InRegBlock); |
| 5445 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5446 | llvm::Value *reg_top = nullptr; |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5447 | Address reg_top_p = |
| 5448 | CGF.Builder.CreateStructGEP(VAListAddr, reg_top_index, "reg_top_p"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5449 | reg_top = CGF.Builder.CreateLoad(reg_top_p, "reg_top"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5450 | Address BaseAddr(CGF.Builder.CreateInBoundsGEP(reg_top, reg_offs), |
| 5451 | CharUnits::fromQuantity(IsFPR ? 16 : 8)); |
| 5452 | Address RegAddr = Address::invalid(); |
| 5453 | llvm::Type *MemTy = CGF.ConvertTypeForMem(Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5454 | |
| 5455 | if (IsIndirect) { |
| 5456 | // If it's been passed indirectly (actually a struct), whatever we find from |
| 5457 | // stored registers or on the stack will actually be a struct **. |
| 5458 | MemTy = llvm::PointerType::getUnqual(MemTy); |
| 5459 | } |
| 5460 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5461 | const Type *Base = nullptr; |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5462 | uint64_t NumMembers = 0; |
| 5463 | bool IsHFA = isHomogeneousAggregate(Ty, Base, NumMembers); |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5464 | if (IsHFA && NumMembers > 1) { |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5465 | // Homogeneous aggregates passed in registers will have their elements split |
| 5466 | // and stored 16-bytes apart regardless of size (they're notionally in qN, |
| 5467 | // qN+1, ...). We reload and store into a temporary local variable |
| 5468 | // contiguously. |
| 5469 | assert(!IsIndirect && "Homogeneous aggregates should be passed directly"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5470 | auto BaseTyInfo = getContext().getTypeInfoInChars(QualType(Base, 0)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5471 | llvm::Type *BaseTy = CGF.ConvertType(QualType(Base, 0)); |
| 5472 | llvm::Type *HFATy = llvm::ArrayType::get(BaseTy, NumMembers); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5473 | Address Tmp = CGF.CreateTempAlloca(HFATy, |
| 5474 | std::max(TyAlign, BaseTyInfo.second)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5475 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5476 | // On big-endian platforms, the value will be right-aligned in its slot. |
| 5477 | int Offset = 0; |
| 5478 | if (CGF.CGM.getDataLayout().isBigEndian() && |
| 5479 | BaseTyInfo.first.getQuantity() < 16) |
| 5480 | Offset = 16 - BaseTyInfo.first.getQuantity(); |
| 5481 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5482 | for (unsigned i = 0; i < NumMembers; ++i) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5483 | CharUnits BaseOffset = CharUnits::fromQuantity(16 * i + Offset); |
| 5484 | Address LoadAddr = |
| 5485 | CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, BaseOffset); |
| 5486 | LoadAddr = CGF.Builder.CreateElementBitCast(LoadAddr, BaseTy); |
| 5487 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5488 | Address StoreAddr = CGF.Builder.CreateConstArrayGEP(Tmp, i); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5489 | |
| 5490 | llvm::Value *Elem = CGF.Builder.CreateLoad(LoadAddr); |
| 5491 | CGF.Builder.CreateStore(Elem, StoreAddr); |
| 5492 | } |
| 5493 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5494 | RegAddr = CGF.Builder.CreateElementBitCast(Tmp, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5495 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5496 | // Otherwise the object is contiguous in memory. |
| 5497 | |
| 5498 | // It might be right-aligned in its slot. |
| 5499 | CharUnits SlotSize = BaseAddr.getAlignment(); |
| 5500 | if (CGF.CGM.getDataLayout().isBigEndian() && !IsIndirect && |
James Molloy | 467be60 | 2014-05-07 14:45:55 +0000 | [diff] [blame] | 5501 | (IsHFA || !isAggregateTypeForABI(Ty)) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5502 | TySize < SlotSize) { |
| 5503 | CharUnits Offset = SlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5504 | BaseAddr = CGF.Builder.CreateConstInBoundsByteGEP(BaseAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5505 | } |
| 5506 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5507 | RegAddr = CGF.Builder.CreateElementBitCast(BaseAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5508 | } |
| 5509 | |
| 5510 | CGF.EmitBranch(ContBlock); |
| 5511 | |
| 5512 | //======================================= |
| 5513 | // Argument was on the stack |
| 5514 | //======================================= |
| 5515 | CGF.EmitBlock(OnStackBlock); |
| 5516 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 5517 | Address stack_p = CGF.Builder.CreateStructGEP(VAListAddr, 0, "stack_p"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5518 | llvm::Value *OnStackPtr = CGF.Builder.CreateLoad(stack_p, "stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5519 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5520 | // Again, stack arguments may need realignment. In this case both integer and |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5521 | // floating-point ones might be affected. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5522 | if (!IsIndirect && TyAlign.getQuantity() > 8) { |
| 5523 | int Align = TyAlign.getQuantity(); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5524 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5525 | OnStackPtr = CGF.Builder.CreatePtrToInt(OnStackPtr, CGF.Int64Ty); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5526 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5527 | OnStackPtr = CGF.Builder.CreateAdd( |
| 5528 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, Align - 1), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5529 | "align_stack"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5530 | OnStackPtr = CGF.Builder.CreateAnd( |
| 5531 | OnStackPtr, llvm::ConstantInt::get(CGF.Int64Ty, -Align), |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5532 | "align_stack"); |
| 5533 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5534 | OnStackPtr = CGF.Builder.CreateIntToPtr(OnStackPtr, CGF.Int8PtrTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5535 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5536 | Address OnStackAddr(OnStackPtr, |
| 5537 | std::max(CharUnits::fromQuantity(8), TyAlign)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5538 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5539 | // All stack slots are multiples of 8 bytes. |
| 5540 | CharUnits StackSlotSize = CharUnits::fromQuantity(8); |
| 5541 | CharUnits StackSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5542 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5543 | StackSize = StackSlotSize; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5544 | else |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5545 | StackSize = TySize.alignTo(StackSlotSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5546 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5547 | llvm::Value *StackSizeC = CGF.Builder.getSize(StackSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5548 | llvm::Value *NewStack = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5549 | CGF.Builder.CreateInBoundsGEP(OnStackPtr, StackSizeC, "new_stack"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5550 | |
| 5551 | // Write the new value of __stack for the next call to va_arg |
| 5552 | CGF.Builder.CreateStore(NewStack, stack_p); |
| 5553 | |
| 5554 | if (CGF.CGM.getDataLayout().isBigEndian() && !isAggregateTypeForABI(Ty) && |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5555 | TySize < StackSlotSize) { |
| 5556 | CharUnits Offset = StackSlotSize - TySize; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5557 | OnStackAddr = CGF.Builder.CreateConstInBoundsByteGEP(OnStackAddr, Offset); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5560 | OnStackAddr = CGF.Builder.CreateElementBitCast(OnStackAddr, MemTy); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5561 | |
| 5562 | CGF.EmitBranch(ContBlock); |
| 5563 | |
| 5564 | //======================================= |
| 5565 | // Tidy up |
| 5566 | //======================================= |
| 5567 | CGF.EmitBlock(ContBlock); |
| 5568 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5569 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 5570 | OnStackAddr, OnStackBlock, "vaargs.addr"); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5571 | |
| 5572 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5573 | return Address(CGF.Builder.CreateLoad(ResAddr, "vaarg.addr"), |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 5574 | TyAlign); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5575 | |
| 5576 | return ResAddr; |
| 5577 | } |
| 5578 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5579 | Address AArch64ABIInfo::EmitDarwinVAArg(Address VAListAddr, QualType Ty, |
| 5580 | CodeGenFunction &CGF) const { |
| 5581 | // The backend's lowering doesn't support va_arg for aggregates or |
| 5582 | // illegal vector types. Lower VAArg here for these cases and use |
| 5583 | // the LLVM va_arg instruction for everything else. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5584 | if (!isAggregateTypeForABI(Ty) && !isIllegalVectorType(Ty)) |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 5585 | return EmitVAArgInstr(CGF, VAListAddr, Ty, ABIArgInfo::getDirect()); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5586 | |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 5587 | uint64_t PointerSize = getTarget().getPointerWidth(0) / 8; |
| 5588 | CharUnits SlotSize = CharUnits::fromQuantity(PointerSize); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5589 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5590 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5591 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5592 | Address Addr(CGF.Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 5593 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 5594 | return Addr; |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5597 | // The size of the actual thing passed, which might end up just |
| 5598 | // being a pointer for indirect types. |
| 5599 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
| 5600 | |
| 5601 | // Arguments bigger than 16 bytes which aren't homogeneous |
| 5602 | // aggregates should be passed indirectly. |
| 5603 | bool IsIndirect = false; |
| 5604 | if (TyInfo.first.getQuantity() > 16) { |
| 5605 | const Type *Base = nullptr; |
| 5606 | uint64_t Members = 0; |
| 5607 | IsIndirect = !isHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5608 | } |
| 5609 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5610 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, |
| 5611 | TyInfo, SlotSize, /*AllowHigherAlign*/ true); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 5614 | Address AArch64ABIInfo::EmitMSVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5615 | QualType Ty) const { |
| 5616 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 5617 | CGF.getContext().getTypeInfoInChars(Ty), |
| 5618 | CharUnits::fromQuantity(8), |
| 5619 | /*allowHigherAlign*/ false); |
| 5620 | } |
| 5621 | |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 5622 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5623 | // ARM ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 5624 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5625 | |
| 5626 | namespace { |
| 5627 | |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5628 | class ARMABIInfo : public SwiftABIInfo { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5629 | public: |
| 5630 | enum ABIKind { |
| 5631 | APCS = 0, |
| 5632 | AAPCS = 1, |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5633 | AAPCS_VFP = 2, |
| 5634 | AAPCS16_VFP = 3, |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5635 | }; |
| 5636 | |
| 5637 | private: |
| 5638 | ABIKind Kind; |
| 5639 | |
| 5640 | public: |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5641 | ARMABIInfo(CodeGenTypes &CGT, ABIKind _Kind) |
| 5642 | : SwiftABIInfo(CGT), Kind(_Kind) { |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5643 | setCCs(); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5644 | } |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5645 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5646 | bool isEABI() const { |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5647 | switch (getTarget().getTriple().getEnvironment()) { |
| 5648 | case llvm::Triple::Android: |
| 5649 | case llvm::Triple::EABI: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5650 | case llvm::Triple::EABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5651 | case llvm::Triple::GNUEABI: |
Joerg Sonnenberger | 0c1652d | 2013-12-16 18:30:28 +0000 | [diff] [blame] | 5652 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5653 | case llvm::Triple::MuslEABI: |
| 5654 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | 782e6aa | 2013-12-12 21:29:27 +0000 | [diff] [blame] | 5655 | return true; |
| 5656 | default: |
| 5657 | return false; |
| 5658 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5661 | bool isEABIHF() const { |
| 5662 | switch (getTarget().getTriple().getEnvironment()) { |
| 5663 | case llvm::Triple::EABIHF: |
| 5664 | case llvm::Triple::GNUEABIHF: |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 5665 | case llvm::Triple::MuslEABIHF: |
Joerg Sonnenberger | d75a1f8 | 2013-12-16 19:16:04 +0000 | [diff] [blame] | 5666 | return true; |
| 5667 | default: |
| 5668 | return false; |
| 5669 | } |
| 5670 | } |
| 5671 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5672 | ABIKind getABIKind() const { return Kind; } |
| 5673 | |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5674 | private: |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5675 | ABIArgInfo classifyReturnType(QualType RetTy, bool isVariadic, |
| 5676 | unsigned functionCallConv) const; |
| 5677 | ABIArgInfo classifyArgumentType(QualType RetTy, bool isVariadic, |
| 5678 | unsigned functionCallConv) const; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5679 | ABIArgInfo classifyHomogeneousAggregate(QualType Ty, const Type *Base, |
| 5680 | uint64_t Members) const; |
| 5681 | ABIArgInfo coerceIllegalVector(QualType Ty) const; |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5682 | bool isIllegalVectorType(QualType Ty) const; |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 5683 | bool containsAnyFP16Vectors(QualType Ty) const; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5684 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 5685 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 5686 | bool isHomogeneousAggregateSmallEnough(const Type *Ty, |
| 5687 | uint64_t Members) const override; |
| 5688 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5689 | bool isEffectivelyAAPCS_VFP(unsigned callConvention, bool acceptHalf) const; |
| 5690 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5691 | void computeInfo(CGFunctionInfo &FI) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5692 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5693 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 5694 | QualType Ty) const override; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5695 | |
| 5696 | llvm::CallingConv::ID getLLVMDefaultCC() const; |
| 5697 | llvm::CallingConv::ID getABIDefaultCC() const; |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5698 | void setCCs(); |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5699 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 5700 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
John McCall | 12f2352 | 2016-04-04 18:33:08 +0000 | [diff] [blame] | 5701 | bool asReturnValue) const override { |
| 5702 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 5703 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 5704 | bool isSwiftErrorInRegister() const override { |
| 5705 | return true; |
| 5706 | } |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 5707 | bool isLegalVectorTypeForSwift(CharUnits totalSize, llvm::Type *eltTy, |
| 5708 | unsigned elts) const override; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5709 | }; |
| 5710 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5711 | class ARMTargetCodeGenInfo : public TargetCodeGenInfo { |
| 5712 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 5713 | ARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5714 | :TargetCodeGenInfo(new ARMABIInfo(CGT, K)) {} |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5715 | |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5716 | const ARMABIInfo &getABIInfo() const { |
| 5717 | return static_cast<const ARMABIInfo&>(TargetCodeGenInfo::getABIInfo()); |
| 5718 | } |
| 5719 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5720 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
John McCall | beec5a0 | 2010-03-06 00:35:14 +0000 | [diff] [blame] | 5721 | return 13; |
| 5722 | } |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5723 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5724 | StringRef getARCRetainAutoreleasedReturnValueMarker() const override { |
Oliver Stannard | 7f18864 | 2017-08-21 09:54:46 +0000 | [diff] [blame] | 5725 | return "mov\tr7, r7\t\t// marker for objc_retainAutoreleaseReturnValue"; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5726 | } |
| 5727 | |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5728 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5729 | llvm::Value *Address) const override { |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5730 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5731 | |
| 5732 | // 0-15 are the 16 integer registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5733 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 15); |
Roman Divacky | c161735 | 2011-05-18 19:36:54 +0000 | [diff] [blame] | 5734 | return false; |
| 5735 | } |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5736 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 5737 | unsigned getSizeOfUnwindException() const override { |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 5738 | if (getABIInfo().isEABI()) return 88; |
| 5739 | return TargetCodeGenInfo::getSizeOfUnwindException(); |
| 5740 | } |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5741 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5742 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5743 | CodeGen::CodeGenModule &CGM) const override { |
| 5744 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5745 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 5746 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5747 | if (!FD) |
| 5748 | return; |
| 5749 | |
| 5750 | const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>(); |
| 5751 | if (!Attr) |
| 5752 | return; |
| 5753 | |
| 5754 | const char *Kind; |
| 5755 | switch (Attr->getInterrupt()) { |
| 5756 | case ARMInterruptAttr::Generic: Kind = ""; break; |
| 5757 | case ARMInterruptAttr::IRQ: Kind = "IRQ"; break; |
| 5758 | case ARMInterruptAttr::FIQ: Kind = "FIQ"; break; |
| 5759 | case ARMInterruptAttr::SWI: Kind = "SWI"; break; |
| 5760 | case ARMInterruptAttr::ABORT: Kind = "ABORT"; break; |
| 5761 | case ARMInterruptAttr::UNDEF: Kind = "UNDEF"; break; |
| 5762 | } |
| 5763 | |
| 5764 | llvm::Function *Fn = cast<llvm::Function>(GV); |
| 5765 | |
| 5766 | Fn->addFnAttr("interrupt", Kind); |
| 5767 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5768 | ARMABIInfo::ABIKind ABI = cast<ARMABIInfo>(getABIInfo()).getABIKind(); |
| 5769 | if (ABI == ARMABIInfo::APCS) |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5770 | return; |
| 5771 | |
| 5772 | // AAPCS guarantees that sp will be 8-byte aligned on any public interface, |
| 5773 | // however this is not necessarily true on taking any interrupt. Instruct |
| 5774 | // the backend to perform a realignment as part of the function prologue. |
| 5775 | llvm::AttrBuilder B; |
| 5776 | B.addStackAlignmentAttr(8); |
Reid Kleckner | ee4930b | 2017-05-02 22:07:37 +0000 | [diff] [blame] | 5777 | Fn->addAttributes(llvm::AttributeList::FunctionIndex, B); |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 5778 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 5779 | }; |
| 5780 | |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5781 | class WindowsARMTargetCodeGenInfo : public ARMTargetCodeGenInfo { |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5782 | public: |
| 5783 | WindowsARMTargetCodeGenInfo(CodeGenTypes &CGT, ARMABIInfo::ABIKind K) |
| 5784 | : ARMTargetCodeGenInfo(CGT, K) {} |
| 5785 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5786 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5787 | CodeGen::CodeGenModule &CGM) const override; |
Saleem Abdulrasool | 6e9e88b | 2016-06-23 13:45:33 +0000 | [diff] [blame] | 5788 | |
| 5789 | void getDependentLibraryOption(llvm::StringRef Lib, |
| 5790 | llvm::SmallString<24> &Opt) const override { |
| 5791 | Opt = "/DEFAULTLIB:" + qualifyWindowsLibrary(Lib); |
| 5792 | } |
| 5793 | |
| 5794 | void getDetectMismatchOption(llvm::StringRef Name, llvm::StringRef Value, |
| 5795 | llvm::SmallString<32> &Opt) const override { |
| 5796 | Opt = "/FAILIFMISMATCH:\"" + Name.str() + "=" + Value.str() + "\""; |
| 5797 | } |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5798 | }; |
| 5799 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 5800 | void WindowsARMTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 5801 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &CGM) const { |
| 5802 | ARMTargetCodeGenInfo::setTargetAttributes(D, GV, CGM); |
| 5803 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 5804 | return; |
Hans Wennborg | d43f40d | 2018-02-23 13:47:36 +0000 | [diff] [blame] | 5805 | addStackProbeTargetAttributes(D, GV, CGM); |
Saleem Abdulrasool | 71d1dd1 | 2015-01-30 23:29:19 +0000 | [diff] [blame] | 5806 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 5807 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 5808 | |
Chris Lattner | 22326a1 | 2010-07-29 02:31:05 +0000 | [diff] [blame] | 5809 | void ARMABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | d791e92 | 2018-03-19 17:38:40 +0000 | [diff] [blame] | 5810 | if (!::classifyReturnType(getCXXABI(), FI, *this)) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5811 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType(), FI.isVariadic(), |
| 5812 | FI.getCallingConvention()); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5813 | |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5814 | for (auto &I : FI.arguments()) |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5815 | I.info = classifyArgumentType(I.type, FI.isVariadic(), |
| 5816 | FI.getCallingConvention()); |
| 5817 | |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5818 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 5819 | // Always honor user-specified calling convention. |
| 5820 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 5821 | return; |
| 5822 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5823 | llvm::CallingConv::ID cc = getRuntimeCC(); |
| 5824 | if (cc != llvm::CallingConv::C) |
Tim Northover | bc784d1 | 2015-02-24 17:22:40 +0000 | [diff] [blame] | 5825 | FI.setEffectiveCallingConvention(cc); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5826 | } |
Rafael Espindola | a92c442 | 2010-06-16 16:13:39 +0000 | [diff] [blame] | 5827 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5828 | /// Return the default calling convention that LLVM will use. |
| 5829 | llvm::CallingConv::ID ARMABIInfo::getLLVMDefaultCC() const { |
| 5830 | // The default calling convention that LLVM will infer. |
Tim Northover | d88ecb3 | 2016-01-27 19:32:40 +0000 | [diff] [blame] | 5831 | if (isEABIHF() || getTarget().getTriple().isWatchABI()) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5832 | return llvm::CallingConv::ARM_AAPCS_VFP; |
| 5833 | else if (isEABI()) |
| 5834 | return llvm::CallingConv::ARM_AAPCS; |
| 5835 | else |
| 5836 | return llvm::CallingConv::ARM_APCS; |
| 5837 | } |
| 5838 | |
| 5839 | /// Return the calling convention that our ABI would like us to use |
| 5840 | /// as the C calling convention. |
| 5841 | llvm::CallingConv::ID ARMABIInfo::getABIDefaultCC() const { |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5842 | switch (getABIKind()) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5843 | case APCS: return llvm::CallingConv::ARM_APCS; |
| 5844 | case AAPCS: return llvm::CallingConv::ARM_AAPCS; |
| 5845 | case AAPCS_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5846 | case AAPCS16_VFP: return llvm::CallingConv::ARM_AAPCS_VFP; |
Daniel Dunbar | 020daa9 | 2009-09-12 01:00:39 +0000 | [diff] [blame] | 5847 | } |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5848 | llvm_unreachable("bad ABI kind"); |
| 5849 | } |
| 5850 | |
Anton Korobeynikov | d90dd79 | 2014-12-02 16:04:58 +0000 | [diff] [blame] | 5851 | void ARMABIInfo::setCCs() { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 5852 | assert(getRuntimeCC() == llvm::CallingConv::C); |
| 5853 | |
| 5854 | // Don't muddy up the IR with a ton of explicit annotations if |
| 5855 | // they'd just match what LLVM will infer from the triple. |
| 5856 | llvm::CallingConv::ID abiCC = getABIDefaultCC(); |
| 5857 | if (abiCC != getLLVMDefaultCC()) |
| 5858 | RuntimeCC = abiCC; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5861 | ABIArgInfo ARMABIInfo::coerceIllegalVector(QualType Ty) const { |
| 5862 | uint64_t Size = getContext().getTypeSize(Ty); |
| 5863 | if (Size <= 32) { |
| 5864 | llvm::Type *ResType = |
| 5865 | llvm::Type::getInt32Ty(getVMContext()); |
| 5866 | return ABIArgInfo::getDirect(ResType); |
| 5867 | } |
| 5868 | if (Size == 64 || Size == 128) { |
| 5869 | llvm::Type *ResType = llvm::VectorType::get( |
| 5870 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5871 | return ABIArgInfo::getDirect(ResType); |
| 5872 | } |
| 5873 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 5874 | } |
| 5875 | |
| 5876 | ABIArgInfo ARMABIInfo::classifyHomogeneousAggregate(QualType Ty, |
| 5877 | const Type *Base, |
| 5878 | uint64_t Members) const { |
| 5879 | assert(Base && "Base class should be set for homogeneous aggregate"); |
| 5880 | // Base can be a floating-point or a vector. |
| 5881 | if (const VectorType *VT = Base->getAs<VectorType>()) { |
| 5882 | // FP16 vectors should be converted to integer vectors |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 5883 | if (!getTarget().hasLegalHalfType() && containsAnyFP16Vectors(Ty)) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5884 | uint64_t Size = getContext().getTypeSize(VT); |
| 5885 | llvm::Type *NewVecTy = llvm::VectorType::get( |
| 5886 | llvm::Type::getInt32Ty(getVMContext()), Size / 32); |
| 5887 | llvm::Type *Ty = llvm::ArrayType::get(NewVecTy, Members); |
| 5888 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5889 | } |
| 5890 | } |
| 5891 | return ABIArgInfo::getDirect(nullptr, 0, nullptr, false); |
| 5892 | } |
| 5893 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5894 | ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic, |
| 5895 | unsigned functionCallConv) const { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5896 | // 6.1.2.1 The following argument types are VFP CPRCs: |
| 5897 | // A single-precision floating-point type (including promoted |
| 5898 | // half-precision types); A double-precision floating-point type; |
| 5899 | // A 64-bit or 128-bit containerized vector type; Homogeneous Aggregate |
| 5900 | // with a Base Type of a single- or double-precision floating-point type, |
| 5901 | // 64-bit containerized vectors or 128-bit containerized vectors with one |
| 5902 | // to four Elements. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5903 | // Variadic functions should always marshal to the base standard. |
| 5904 | bool IsAAPCS_VFP = |
| 5905 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ false); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 5906 | |
Reid Kleckner | b1be683 | 2014-11-15 01:41:41 +0000 | [diff] [blame] | 5907 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 5908 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5909 | // Handle illegal vector types here. |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5910 | if (isIllegalVectorType(Ty)) |
| 5911 | return coerceIllegalVector(Ty); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 5912 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 5913 | // _Float16 and __fp16 get passed as if it were an int or float, but with |
| 5914 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 5915 | // half type natively, and does not need to interwork with AAPCS code. |
| 5916 | if ((Ty->isFloat16Type() || Ty->isHalfType()) && |
| 5917 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5918 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 5919 | llvm::Type::getFloatTy(getVMContext()) : |
| 5920 | llvm::Type::getInt32Ty(getVMContext()); |
| 5921 | return ABIArgInfo::getDirect(ResType); |
| 5922 | } |
| 5923 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 5924 | if (!isAggregateTypeForABI(Ty)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5925 | // Treat an enum type as its underlying type. |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5926 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5927 | Ty = EnumTy->getDecl()->getIntegerType(); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5928 | } |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5929 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 5930 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 5931 | : ABIArgInfo::getDirect()); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 5932 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 5933 | |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5934 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5935 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 5936 | } |
Tim Northover | 1060eae | 2013-06-21 22:49:34 +0000 | [diff] [blame] | 5937 | |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5938 | // Ignore empty records. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 5939 | if (isEmptyRecord(getContext(), Ty, true)) |
Daniel Dunbar | 09d3362 | 2009-09-14 21:54:03 +0000 | [diff] [blame] | 5940 | return ABIArgInfo::getIgnore(); |
| 5941 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 5942 | if (IsAAPCS_VFP) { |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5943 | // Homogeneous Aggregates need to be expanded when we can fit the aggregate |
| 5944 | // into VFP registers. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 5945 | const Type *Base = nullptr; |
Manman Ren | 2a523d8 | 2012-10-30 23:21:41 +0000 | [diff] [blame] | 5946 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 5947 | if (isHomogeneousAggregate(Ty, Base, Members)) |
| 5948 | return classifyHomogeneousAggregate(Ty, Base, Members); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5949 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 5950 | // WatchOS does have homogeneous aggregates. Note that we intentionally use |
| 5951 | // this convention even for a variadic function: the backend will use GPRs |
| 5952 | // if needed. |
| 5953 | const Type *Base = nullptr; |
| 5954 | uint64_t Members = 0; |
| 5955 | if (isHomogeneousAggregate(Ty, Base, Members)) { |
| 5956 | assert(Base && Members <= 4 && "unexpected homogeneous aggregate"); |
| 5957 | llvm::Type *Ty = |
| 5958 | llvm::ArrayType::get(CGT.ConvertType(QualType(Base, 0)), Members); |
| 5959 | return ABIArgInfo::getDirect(Ty, 0, nullptr, false); |
| 5960 | } |
| 5961 | } |
| 5962 | |
| 5963 | if (getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 5964 | getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(16)) { |
| 5965 | // WatchOS is adopting the 64-bit AAPCS rule on composite types: if they're |
| 5966 | // bigger than 128-bits, they get placed in space allocated by the caller, |
| 5967 | // and a pointer is passed. |
| 5968 | return ABIArgInfo::getIndirect( |
| 5969 | CharUnits::fromQuantity(getContext().getTypeAlign(Ty) / 8), false); |
Bob Wilson | e826a2a | 2011-08-03 05:58:22 +0000 | [diff] [blame] | 5970 | } |
| 5971 | |
Manman Ren | 6c30e13 | 2012-08-13 21:23:55 +0000 | [diff] [blame] | 5972 | // Support byval for ARM. |
Manman Ren | 77b0238 | 2012-11-06 19:05:29 +0000 | [diff] [blame] | 5973 | // The ABI alignment for APCS is 4-byte and for AAPCS at least 4-byte and at |
| 5974 | // most 8-byte. We realign the indirect argument if type alignment is bigger |
| 5975 | // than ABI alignment. |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5976 | uint64_t ABIAlign = 4; |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5977 | uint64_t TyAlign; |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5978 | if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5979 | getABIKind() == ARMABIInfo::AAPCS) { |
| 5980 | TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity(); |
Manman Ren | 505d68f | 2012-11-05 22:42:46 +0000 | [diff] [blame] | 5981 | ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 5982 | } else { |
| 5983 | TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity(); |
| 5984 | } |
Manman Ren | 8cd9981 | 2012-11-06 04:58:01 +0000 | [diff] [blame] | 5985 | if (getContext().getTypeSizeInChars(Ty) > CharUnits::fromQuantity(64)) { |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 5986 | assert(getABIKind() != ARMABIInfo::AAPCS16_VFP && "unexpected byval"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 5987 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(ABIAlign), |
| 5988 | /*ByVal=*/true, |
| 5989 | /*Realign=*/TyAlign > ABIAlign); |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 5990 | } |
| 5991 | |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 5992 | // On RenderScript, coerce Aggregates <= 64 bytes to an integer array of |
| 5993 | // same size and alignment. |
| 5994 | if (getTarget().isRenderScriptTarget()) { |
| 5995 | return coerceToIntArray(Ty, getContext(), getVMContext()); |
| 5996 | } |
| 5997 | |
Daniel Dunbar | b34b080 | 2010-09-23 01:54:28 +0000 | [diff] [blame] | 5998 | // Otherwise, pass by coercing to a structure of the appropriate size. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5999 | llvm::Type* ElemTy; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6000 | unsigned SizeRegs; |
Eli Friedman | e66abda | 2012-08-09 00:31:40 +0000 | [diff] [blame] | 6001 | // FIXME: Try to match the types of the arguments more accurately where |
| 6002 | // we can. |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 6003 | if (TyAlign <= 4) { |
Bob Wilson | 8e2b75d | 2011-08-01 23:39:04 +0000 | [diff] [blame] | 6004 | ElemTy = llvm::Type::getInt32Ty(getVMContext()); |
| 6005 | SizeRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 6006 | } else { |
Manman Ren | 6fdb158 | 2012-06-25 22:04:00 +0000 | [diff] [blame] | 6007 | ElemTy = llvm::Type::getInt64Ty(getVMContext()); |
| 6008 | SizeRegs = (getContext().getTypeSize(Ty) + 63) / 64; |
Stuart Hastings | f2752a3 | 2011-04-27 17:24:02 +0000 | [diff] [blame] | 6009 | } |
Stuart Hastings | 4b21495 | 2011-04-28 18:16:06 +0000 | [diff] [blame] | 6010 | |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6011 | return ABIArgInfo::getDirect(llvm::ArrayType::get(ElemTy, SizeRegs)); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6012 | } |
| 6013 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6014 | static bool isIntegerLikeType(QualType Ty, ASTContext &Context, |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6015 | llvm::LLVMContext &VMContext) { |
| 6016 | // APCS, C Language Calling Conventions, Non-Simple Return Values: A structure |
| 6017 | // is called integer-like if its size is less than or equal to one word, and |
| 6018 | // the offset of each of its addressable sub-fields is zero. |
| 6019 | |
| 6020 | uint64_t Size = Context.getTypeSize(Ty); |
| 6021 | |
| 6022 | // Check that the type fits in a word. |
| 6023 | if (Size > 32) |
| 6024 | return false; |
| 6025 | |
| 6026 | // FIXME: Handle vector types! |
| 6027 | if (Ty->isVectorType()) |
| 6028 | return false; |
| 6029 | |
Daniel Dunbar | d53bac7 | 2009-09-14 02:20:34 +0000 | [diff] [blame] | 6030 | // Float types are never treated as "integer like". |
| 6031 | if (Ty->isRealFloatingType()) |
| 6032 | return false; |
| 6033 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6034 | // If this is a builtin or pointer type then it is ok. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6035 | if (Ty->getAs<BuiltinType>() || Ty->isPointerType()) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6036 | return true; |
| 6037 | |
Daniel Dunbar | 96ebba5 | 2010-02-01 23:31:26 +0000 | [diff] [blame] | 6038 | // Small complex integer types are "integer like". |
| 6039 | if (const ComplexType *CT = Ty->getAs<ComplexType>()) |
| 6040 | return isIntegerLikeType(CT->getElementType(), Context, VMContext); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6041 | |
| 6042 | // Single element and zero sized arrays should be allowed, by the definition |
| 6043 | // above, but they are not. |
| 6044 | |
| 6045 | // Otherwise, it must be a record type. |
| 6046 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 6047 | if (!RT) return false; |
| 6048 | |
| 6049 | // Ignore records with flexible arrays. |
| 6050 | const RecordDecl *RD = RT->getDecl(); |
| 6051 | if (RD->hasFlexibleArrayMember()) |
| 6052 | return false; |
| 6053 | |
| 6054 | // Check that all sub-fields are at offset 0, and are themselves "integer |
| 6055 | // like". |
| 6056 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 6057 | |
| 6058 | bool HadField = false; |
| 6059 | unsigned idx = 0; |
| 6060 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 6061 | i != e; ++i, ++idx) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 6062 | const FieldDecl *FD = *i; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6063 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6064 | // Bit-fields are not addressable, we only need to verify they are "integer |
| 6065 | // like". We still have to disallow a subsequent non-bitfield, for example: |
| 6066 | // struct { int : 0; int x } |
| 6067 | // is non-integer like according to gcc. |
| 6068 | if (FD->isBitField()) { |
| 6069 | if (!RD->isUnion()) |
| 6070 | HadField = true; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6071 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6072 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6073 | return false; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6074 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6075 | continue; |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6078 | // Check if this field is at offset 0. |
| 6079 | if (Layout.getFieldOffset(idx) != 0) |
| 6080 | return false; |
| 6081 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6082 | if (!isIntegerLikeType(FD->getType(), Context, VMContext)) |
| 6083 | return false; |
Michael J. Spencer | b2f376b | 2010-08-25 18:17:27 +0000 | [diff] [blame] | 6084 | |
Daniel Dunbar | 45c7ff1 | 2010-01-29 03:22:29 +0000 | [diff] [blame] | 6085 | // Only allow at most one field in a structure. This doesn't match the |
| 6086 | // wording above, but follows gcc in situations with a field following an |
| 6087 | // empty structure. |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6088 | if (!RD->isUnion()) { |
| 6089 | if (HadField) |
| 6090 | return false; |
| 6091 | |
| 6092 | HadField = true; |
| 6093 | } |
| 6094 | } |
| 6095 | |
| 6096 | return true; |
| 6097 | } |
| 6098 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6099 | ABIArgInfo ARMABIInfo::classifyReturnType(QualType RetTy, bool isVariadic, |
| 6100 | unsigned functionCallConv) const { |
| 6101 | |
| 6102 | // Variadic functions should always marshal to the base standard. |
| 6103 | bool IsAAPCS_VFP = |
| 6104 | !isVariadic && isEffectivelyAAPCS_VFP(functionCallConv, /* AAPCS16 */ true); |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6105 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6106 | if (RetTy->isVoidType()) |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6107 | return ABIArgInfo::getIgnore(); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6108 | |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6109 | if (const VectorType *VT = RetTy->getAs<VectorType>()) { |
| 6110 | // Large vector types should be returned via memory. |
| 6111 | if (getContext().getTypeSize(RetTy) > 128) |
| 6112 | return getNaturalAlignIndirect(RetTy); |
| 6113 | // FP16 vectors should be converted to integer vectors |
| 6114 | if (!getTarget().hasLegalHalfType() && |
| 6115 | (VT->getElementType()->isFloat16Type() || |
| 6116 | VT->getElementType()->isHalfType())) |
| 6117 | return coerceIllegalVector(RetTy); |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6118 | } |
Daniel Dunbar | 19964db | 2010-09-23 01:54:32 +0000 | [diff] [blame] | 6119 | |
Sjoerd Meijer | ca8f4e7 | 2018-01-23 10:13:49 +0000 | [diff] [blame] | 6120 | // _Float16 and __fp16 get returned as if it were an int or float, but with |
| 6121 | // the top 16 bits unspecified. This is not done for OpenCL as it handles the |
| 6122 | // half type natively, and does not need to interwork with AAPCS code. |
| 6123 | if ((RetTy->isFloat16Type() || RetTy->isHalfType()) && |
| 6124 | !getContext().getLangOpts().NativeHalfArgsAndReturns) { |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6125 | llvm::Type *ResType = IsAAPCS_VFP ? |
Oliver Stannard | dc2854c | 2015-09-03 12:40:58 +0000 | [diff] [blame] | 6126 | llvm::Type::getFloatTy(getVMContext()) : |
| 6127 | llvm::Type::getInt32Ty(getVMContext()); |
| 6128 | return ABIArgInfo::getDirect(ResType); |
| 6129 | } |
| 6130 | |
John McCall | a1dee530 | 2010-08-22 10:59:02 +0000 | [diff] [blame] | 6131 | if (!isAggregateTypeForABI(RetTy)) { |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6132 | // Treat an enum type as its underlying type. |
| 6133 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6134 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6135 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6136 | return RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6137 | : ABIArgInfo::getDirect(); |
Douglas Gregor | a71cc15 | 2010-02-02 20:10:50 +0000 | [diff] [blame] | 6138 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6139 | |
| 6140 | // Are we following APCS? |
| 6141 | if (getABIKind() == APCS) { |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6142 | if (isEmptyRecord(getContext(), RetTy, false)) |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6143 | return ABIArgInfo::getIgnore(); |
| 6144 | |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6145 | // Complex types are all returned as packed integers. |
| 6146 | // |
| 6147 | // FIXME: Consider using 2 x vector types if the back end handles them |
| 6148 | // correctly. |
| 6149 | if (RetTy->isAnyComplexType()) |
Oliver Stannard | 2bfdc5b | 2014-08-27 10:43:15 +0000 | [diff] [blame] | 6150 | return ABIArgInfo::getDirect(llvm::IntegerType::get( |
| 6151 | getVMContext(), getContext().getTypeSize(RetTy))); |
Daniel Dunbar | eedf151 | 2010-02-01 23:31:19 +0000 | [diff] [blame] | 6152 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6153 | // Integer like structures are returned in r0. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6154 | if (isIntegerLikeType(RetTy, getContext(), getVMContext())) { |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6155 | // Return in the smallest viable integer type. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6156 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6157 | if (Size <= 8) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6158 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6159 | if (Size <= 16) |
Chris Lattner | fe34c1d | 2010-07-29 06:26:06 +0000 | [diff] [blame] | 6160 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6161 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
| 6164 | // Otherwise return in memory. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6165 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6166 | } |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6167 | |
| 6168 | // Otherwise this is an AAPCS variant. |
| 6169 | |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6170 | if (isEmptyRecord(getContext(), RetTy, true)) |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6171 | return ABIArgInfo::getIgnore(); |
| 6172 | |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6173 | // Check for homogeneous aggregates with AAPCS-VFP. |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6174 | if (IsAAPCS_VFP) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6175 | const Type *Base = nullptr; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6176 | uint64_t Members = 0; |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6177 | if (isHomogeneousAggregate(RetTy, Base, Members)) |
| 6178 | return classifyHomogeneousAggregate(RetTy, Base, Members); |
Bob Wilson | 1d9269a | 2011-11-02 04:51:36 +0000 | [diff] [blame] | 6179 | } |
| 6180 | |
Daniel Dunbar | 626f1d8 | 2009-09-13 08:03:58 +0000 | [diff] [blame] | 6181 | // Aggregates <= 4 bytes are returned in r0; other aggregates |
| 6182 | // are returned indirectly. |
Chris Lattner | 458b2aa | 2010-07-29 02:16:43 +0000 | [diff] [blame] | 6183 | uint64_t Size = getContext().getTypeSize(RetTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6184 | if (Size <= 32) { |
Pirama Arumuga Nainar | bb846a3 | 2016-07-27 19:01:51 +0000 | [diff] [blame] | 6185 | // On RenderScript, coerce Aggregates <= 4 bytes to an integer array of |
| 6186 | // same size and alignment. |
| 6187 | if (getTarget().isRenderScriptTarget()) { |
| 6188 | return coerceToIntArray(RetTy, getContext(), getVMContext()); |
| 6189 | } |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6190 | if (getDataLayout().isBigEndian()) |
| 6191 | // Return in 32 bit integer integer type (as if loaded by LDR, AAPCS 5.4) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6192 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Christian Pirker | c3d3217 | 2014-07-03 09:28:12 +0000 | [diff] [blame] | 6193 | |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6194 | // Return in the smallest viable integer type. |
| 6195 | if (Size <= 8) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6196 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6197 | if (Size <= 16) |
Tim Northover | 5a1558e | 2014-11-07 22:30:50 +0000 | [diff] [blame] | 6198 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 6199 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6200 | } else if (Size <= 128 && getABIKind() == AAPCS16_VFP) { |
| 6201 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 6202 | llvm::Type *CoerceTy = |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 6203 | llvm::ArrayType::get(Int32Ty, llvm::alignTo(Size, 32) / 32); |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6204 | return ABIArgInfo::getDirect(CoerceTy); |
Daniel Dunbar | 1ce7251 | 2009-09-14 00:56:55 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6207 | return getNaturalAlignIndirect(RetTy); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6208 | } |
| 6209 | |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6210 | /// isIllegalVector - check whether Ty is an illegal vector type. |
| 6211 | bool ARMABIInfo::isIllegalVectorType(QualType Ty) const { |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6212 | if (const VectorType *VT = Ty->getAs<VectorType> ()) { |
Mikhail Maltsev | e04ab4f | 2018-09-12 09:19:19 +0000 | [diff] [blame] | 6213 | // On targets that don't support FP16, FP16 is expanded into float, and we |
| 6214 | // don't want the ABI to depend on whether or not FP16 is supported in |
| 6215 | // hardware. Thus return false to coerce FP16 vectors into integer vectors. |
| 6216 | if (!getTarget().hasLegalHalfType() && |
| 6217 | (VT->getElementType()->isFloat16Type() || |
| 6218 | VT->getElementType()->isHalfType())) |
| 6219 | return true; |
Stephen Hines | 8267e7d | 2015-12-04 01:39:30 +0000 | [diff] [blame] | 6220 | if (isAndroid()) { |
| 6221 | // Android shipped using Clang 3.1, which supported a slightly different |
| 6222 | // vector ABI. The primary differences were that 3-element vector types |
| 6223 | // were legal, and so were sub 32-bit vectors (i.e. <2 x i8>). This path |
| 6224 | // accepts that legacy behavior for Android only. |
| 6225 | // Check whether VT is legal. |
| 6226 | unsigned NumElements = VT->getNumElements(); |
| 6227 | // NumElements should be power of 2 or equal to 3. |
| 6228 | if (!llvm::isPowerOf2_32(NumElements) && NumElements != 3) |
| 6229 | return true; |
| 6230 | } else { |
| 6231 | // Check whether VT is legal. |
| 6232 | unsigned NumElements = VT->getNumElements(); |
| 6233 | uint64_t Size = getContext().getTypeSize(VT); |
| 6234 | // NumElements should be power of 2. |
| 6235 | if (!llvm::isPowerOf2_32(NumElements)) |
| 6236 | return true; |
| 6237 | // Size should be greater than 32 bits. |
| 6238 | return Size <= 32; |
| 6239 | } |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6240 | } |
| 6241 | return false; |
| 6242 | } |
| 6243 | |
Mikhail Maltsev | a45292c | 2019-06-18 14:34:27 +0000 | [diff] [blame] | 6244 | /// Return true if a type contains any 16-bit floating point vectors |
| 6245 | bool ARMABIInfo::containsAnyFP16Vectors(QualType Ty) const { |
| 6246 | if (const ConstantArrayType *AT = getContext().getAsConstantArrayType(Ty)) { |
| 6247 | uint64_t NElements = AT->getSize().getZExtValue(); |
| 6248 | if (NElements == 0) |
| 6249 | return false; |
| 6250 | return containsAnyFP16Vectors(AT->getElementType()); |
| 6251 | } else if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6252 | const RecordDecl *RD = RT->getDecl(); |
| 6253 | |
| 6254 | // If this is a C++ record, check the bases first. |
| 6255 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6256 | if (llvm::any_of(CXXRD->bases(), [this](const CXXBaseSpecifier &B) { |
| 6257 | return containsAnyFP16Vectors(B.getType()); |
| 6258 | })) |
| 6259 | return true; |
| 6260 | |
| 6261 | if (llvm::any_of(RD->fields(), [this](FieldDecl *FD) { |
| 6262 | return FD && containsAnyFP16Vectors(FD->getType()); |
| 6263 | })) |
| 6264 | return true; |
| 6265 | |
| 6266 | return false; |
| 6267 | } else { |
| 6268 | if (const VectorType *VT = Ty->getAs<VectorType>()) |
| 6269 | return (VT->getElementType()->isFloat16Type() || |
| 6270 | VT->getElementType()->isHalfType()); |
| 6271 | return false; |
| 6272 | } |
| 6273 | } |
| 6274 | |
Arnold Schwaighofer | 634e320 | 2017-05-26 18:11:54 +0000 | [diff] [blame] | 6275 | bool ARMABIInfo::isLegalVectorTypeForSwift(CharUnits vectorSize, |
| 6276 | llvm::Type *eltTy, |
| 6277 | unsigned numElts) const { |
| 6278 | if (!llvm::isPowerOf2_32(numElts)) |
| 6279 | return false; |
| 6280 | unsigned size = getDataLayout().getTypeStoreSizeInBits(eltTy); |
| 6281 | if (size > 64) |
| 6282 | return false; |
| 6283 | if (vectorSize.getQuantity() != 8 && |
| 6284 | (vectorSize.getQuantity() != 16 || numElts == 1)) |
| 6285 | return false; |
| 6286 | return true; |
| 6287 | } |
| 6288 | |
Reid Kleckner | e9f6a71 | 2014-10-31 17:10:41 +0000 | [diff] [blame] | 6289 | bool ARMABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 6290 | // Homogeneous aggregates for AAPCS-VFP must have base types of float, |
| 6291 | // double, or 64-bit or 128-bit vectors. |
| 6292 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) { |
| 6293 | if (BT->getKind() == BuiltinType::Float || |
| 6294 | BT->getKind() == BuiltinType::Double || |
| 6295 | BT->getKind() == BuiltinType::LongDouble) |
| 6296 | return true; |
| 6297 | } else if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 6298 | unsigned VecSize = getContext().getTypeSize(VT); |
| 6299 | if (VecSize == 64 || VecSize == 128) |
| 6300 | return true; |
| 6301 | } |
| 6302 | return false; |
| 6303 | } |
| 6304 | |
| 6305 | bool ARMABIInfo::isHomogeneousAggregateSmallEnough(const Type *Base, |
| 6306 | uint64_t Members) const { |
| 6307 | return Members <= 4; |
| 6308 | } |
| 6309 | |
Carey Williams | 2c3c9ca | 2019-03-22 16:20:45 +0000 | [diff] [blame] | 6310 | bool ARMABIInfo::isEffectivelyAAPCS_VFP(unsigned callConvention, |
| 6311 | bool acceptHalf) const { |
| 6312 | // Give precedence to user-specified calling conventions. |
| 6313 | if (callConvention != llvm::CallingConv::C) |
| 6314 | return (callConvention == llvm::CallingConv::ARM_AAPCS_VFP); |
| 6315 | else |
| 6316 | return (getABIKind() == AAPCS_VFP) || |
| 6317 | (acceptHalf && (getABIKind() == AAPCS16_VFP)); |
| 6318 | } |
| 6319 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6320 | Address ARMABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6321 | QualType Ty) const { |
| 6322 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6323 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6324 | // Empty records are ignored for parameter passing purposes. |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6325 | if (isEmptyRecord(getContext(), Ty, true)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6326 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 6327 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 6328 | return Addr; |
Tim Northover | 1711cc9 | 2013-06-21 23:05:33 +0000 | [diff] [blame] | 6329 | } |
| 6330 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6331 | CharUnits TySize = getContext().getTypeSizeInChars(Ty); |
| 6332 | CharUnits TyAlignForABI = getContext().getTypeUnadjustedAlignInChars(Ty); |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6333 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6334 | // Use indirect if size of the illegal vector is bigger than 16 bytes. |
| 6335 | bool IsIndirect = false; |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6336 | const Type *Base = nullptr; |
| 6337 | uint64_t Members = 0; |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6338 | if (TySize > CharUnits::fromQuantity(16) && isIllegalVectorType(Ty)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6339 | IsIndirect = true; |
| 6340 | |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6341 | // ARMv7k passes structs bigger than 16 bytes indirectly, in space |
| 6342 | // allocated by the caller. |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6343 | } else if (TySize > CharUnits::fromQuantity(16) && |
Tim Northover | 5627d39 | 2015-10-30 16:30:45 +0000 | [diff] [blame] | 6344 | getABIKind() == ARMABIInfo::AAPCS16_VFP && |
| 6345 | !isHomogeneousAggregate(Ty, Base, Members)) { |
| 6346 | IsIndirect = true; |
| 6347 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6348 | // Otherwise, bound the type's ABI alignment. |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6349 | // The ABI alignment for 64-bit or 128-bit vectors is 8 for AAPCS and 4 for |
| 6350 | // APCS. For AAPCS, the ABI alignment is at least 4-byte and at most 8-byte. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6351 | // Our callers should be prepared to handle an under-aligned address. |
| 6352 | } else if (getABIKind() == ARMABIInfo::AAPCS_VFP || |
| 6353 | getABIKind() == ARMABIInfo::AAPCS) { |
| 6354 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6355 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(8)); |
Tim Northover | 4c5cb9c | 2015-11-02 19:32:23 +0000 | [diff] [blame] | 6356 | } else if (getABIKind() == ARMABIInfo::AAPCS16_VFP) { |
| 6357 | // ARMv7k allows type alignment up to 16 bytes. |
| 6358 | TyAlignForABI = std::max(TyAlignForABI, CharUnits::fromQuantity(4)); |
| 6359 | TyAlignForABI = std::min(TyAlignForABI, CharUnits::fromQuantity(16)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6360 | } else { |
| 6361 | TyAlignForABI = CharUnits::fromQuantity(4); |
Manman Ren | fef9e31 | 2012-10-16 19:18:39 +0000 | [diff] [blame] | 6362 | } |
Manman Ren | cca54d0 | 2012-10-16 19:01:37 +0000 | [diff] [blame] | 6363 | |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 6364 | std::pair<CharUnits, CharUnits> TyInfo = { TySize, TyAlignForABI }; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6365 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TyInfo, |
| 6366 | SlotSize, /*AllowHigherAlign*/ true); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6367 | } |
| 6368 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6369 | //===----------------------------------------------------------------------===// |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6370 | // NVPTX ABI Implementation |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6371 | //===----------------------------------------------------------------------===// |
| 6372 | |
| 6373 | namespace { |
| 6374 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6375 | class NVPTXABIInfo : public ABIInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6376 | public: |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6377 | NVPTXABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6378 | |
| 6379 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6380 | ABIArgInfo classifyArgumentType(QualType Ty) const; |
| 6381 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6382 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6383 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6384 | QualType Ty) const override; |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6385 | }; |
| 6386 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6387 | class NVPTXTargetCodeGenInfo : public TargetCodeGenInfo { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6388 | public: |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6389 | NVPTXTargetCodeGenInfo(CodeGenTypes &CGT) |
| 6390 | : TargetCodeGenInfo(new NVPTXABIInfo(CGT)) {} |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6391 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6392 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6393 | CodeGen::CodeGenModule &M) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6394 | bool shouldEmitStaticExternCAliases() const override; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6395 | |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6396 | private: |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6397 | // Adds a NamedMDNode with F, Name, and Operand as operands, and adds the |
| 6398 | // resulting MDNode to the nvvm.annotations MDNode. |
| 6399 | static void addNVVMMetadata(llvm::Function *F, StringRef Name, int Operand); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6400 | }; |
| 6401 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6402 | /// Checks if the type is unsupported directly by the current target. |
| 6403 | static bool isUnsupportedType(ASTContext &Context, QualType T) { |
| 6404 | if (!Context.getTargetInfo().hasFloat16Type() && T->isFloat16Type()) |
| 6405 | return true; |
Alexey Bataev | 7ae267d | 2019-06-18 19:04:27 +0000 | [diff] [blame] | 6406 | if (!Context.getTargetInfo().hasFloat128Type() && |
| 6407 | (T->isFloat128Type() || |
| 6408 | (T->isRealFloatingType() && Context.getTypeSize(T) == 128))) |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6409 | return true; |
| 6410 | if (!Context.getTargetInfo().hasInt128Type() && T->isIntegerType() && |
| 6411 | Context.getTypeSize(T) > 64) |
| 6412 | return true; |
| 6413 | if (const auto *AT = T->getAsArrayTypeUnsafe()) |
| 6414 | return isUnsupportedType(Context, AT->getElementType()); |
| 6415 | const auto *RT = T->getAs<RecordType>(); |
| 6416 | if (!RT) |
| 6417 | return false; |
| 6418 | const RecordDecl *RD = RT->getDecl(); |
| 6419 | |
| 6420 | // If this is a C++ record, check the bases first. |
| 6421 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
| 6422 | for (const CXXBaseSpecifier &I : CXXRD->bases()) |
| 6423 | if (isUnsupportedType(Context, I.getType())) |
| 6424 | return true; |
| 6425 | |
| 6426 | for (const FieldDecl *I : RD->fields()) |
| 6427 | if (isUnsupportedType(Context, I->getType())) |
| 6428 | return true; |
| 6429 | return false; |
| 6430 | } |
| 6431 | |
| 6432 | /// Coerce the given type into an array with maximum allowed size of elements. |
| 6433 | static ABIArgInfo coerceToIntArrayWithLimit(QualType Ty, ASTContext &Context, |
| 6434 | llvm::LLVMContext &LLVMContext, |
| 6435 | unsigned MaxSize) { |
| 6436 | // Alignment and Size are measured in bits. |
| 6437 | const uint64_t Size = Context.getTypeSize(Ty); |
| 6438 | const uint64_t Alignment = Context.getTypeAlign(Ty); |
| 6439 | const unsigned Div = std::min<unsigned>(MaxSize, Alignment); |
| 6440 | llvm::Type *IntType = llvm::Type::getIntNTy(LLVMContext, Div); |
| 6441 | const uint64_t NumElements = (Size + Div - 1) / Div; |
| 6442 | return ABIArgInfo::getDirect(llvm::ArrayType::get(IntType, NumElements)); |
| 6443 | } |
| 6444 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6445 | ABIArgInfo NVPTXABIInfo::classifyReturnType(QualType RetTy) const { |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6446 | if (RetTy->isVoidType()) |
| 6447 | return ABIArgInfo::getIgnore(); |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6448 | |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 6449 | if (getContext().getLangOpts().OpenMP && |
| 6450 | getContext().getLangOpts().OpenMPIsDevice && |
| 6451 | isUnsupportedType(getContext(), RetTy)) |
| 6452 | return coerceToIntArrayWithLimit(RetTy, getContext(), getVMContext(), 64); |
| 6453 | |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6454 | // note: this is different from default ABI |
| 6455 | if (!RetTy->isScalarType()) |
| 6456 | return ABIArgInfo::getDirect(); |
| 6457 | |
| 6458 | // Treat an enum type as its underlying type. |
| 6459 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 6460 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 6461 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6462 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 6463 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6464 | } |
| 6465 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6466 | ABIArgInfo NVPTXABIInfo::classifyArgumentType(QualType Ty) const { |
Justin Holewinski | f9329ff | 2013-11-20 20:35:34 +0000 | [diff] [blame] | 6467 | // Treat an enum type as its underlying type. |
| 6468 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6469 | Ty = EnumTy->getDecl()->getIntegerType(); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6470 | |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6471 | // Return aggregates type as indirect by value |
| 6472 | if (isAggregateTypeForABI(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6473 | return getNaturalAlignIndirect(Ty, /* byval */ true); |
Eli Bendersky | 95338a0 | 2014-10-29 13:43:21 +0000 | [diff] [blame] | 6474 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6475 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 6476 | : ABIArgInfo::getDirect()); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6479 | void NVPTXABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6480 | if (!getCXXABI().classifyReturnType(FI)) |
| 6481 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6482 | for (auto &I : FI.arguments()) |
| 6483 | I.info = classifyArgumentType(I.type); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6484 | |
| 6485 | // Always honor user-specified calling convention. |
| 6486 | if (FI.getCallingConvention() != llvm::CallingConv::C) |
| 6487 | return; |
| 6488 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 6489 | FI.setEffectiveCallingConvention(getRuntimeCC()); |
| 6490 | } |
| 6491 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6492 | Address NVPTXABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6493 | QualType Ty) const { |
Justin Holewinski | 83e9668 | 2012-05-24 17:43:12 +0000 | [diff] [blame] | 6494 | llvm_unreachable("NVPTX does not support varargs"); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6495 | } |
| 6496 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6497 | void NVPTXTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6498 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6499 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6500 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6501 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6502 | if (!FD) return; |
| 6503 | |
| 6504 | llvm::Function *F = cast<llvm::Function>(GV); |
| 6505 | |
| 6506 | // Perform special handling in OpenCL mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6507 | if (M.getLangOpts().OpenCL) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6508 | // Use OpenCL function attributes to check for kernel functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6509 | // By default, all functions are device functions |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6510 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6511 | // OpenCL __kernel functions get kernel metadata |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6512 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6513 | addNVVMMetadata(F, "kernel", 1); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6514 | // And kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 6515 | F->addFnAttr(llvm::Attribute::NoInline); |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6516 | } |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6517 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6518 | |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6519 | // Perform special handling in CUDA mode. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6520 | if (M.getLangOpts().CUDA) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6521 | // CUDA __global__ functions get a kernel metadata entry. Since |
Peter Collingbourne | 5bad4af | 2011-10-06 16:49:54 +0000 | [diff] [blame] | 6522 | // __global__ functions cannot be called from the device, we do not |
| 6523 | // need to set the noinline attribute. |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6524 | if (FD->hasAttr<CUDAGlobalAttr>()) { |
| 6525 | // Create !{<func-ref>, metadata !"kernel", i32 1} node |
| 6526 | addNVVMMetadata(F, "kernel", 1); |
| 6527 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6528 | if (CUDALaunchBoundsAttr *Attr = FD->getAttr<CUDALaunchBoundsAttr>()) { |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6529 | // Create !{<func-ref>, metadata !"maxntidx", i32 <val>} node |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 6530 | llvm::APSInt MaxThreads(32); |
| 6531 | MaxThreads = Attr->getMaxThreads()->EvaluateKnownConstInt(M.getContext()); |
| 6532 | if (MaxThreads > 0) |
| 6533 | addNVVMMetadata(F, "maxntidx", MaxThreads.getExtValue()); |
| 6534 | |
| 6535 | // min blocks is an optional argument for CUDALaunchBoundsAttr. If it was |
| 6536 | // not specified in __launch_bounds__ or if the user specified a 0 value, |
| 6537 | // we don't have to add a PTX directive. |
| 6538 | if (Attr->getMinBlocks()) { |
| 6539 | llvm::APSInt MinBlocks(32); |
| 6540 | MinBlocks = Attr->getMinBlocks()->EvaluateKnownConstInt(M.getContext()); |
| 6541 | if (MinBlocks > 0) |
| 6542 | // Create !{<func-ref>, metadata !"minctasm", i32 <val>} node |
| 6543 | addNVVMMetadata(F, "minctasm", MinBlocks.getExtValue()); |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6544 | } |
| 6545 | } |
Justin Holewinski | 3803197 | 2011-10-05 17:58:44 +0000 | [diff] [blame] | 6546 | } |
| 6547 | } |
| 6548 | |
Eli Bendersky | e06a2c4 | 2014-04-15 16:57:05 +0000 | [diff] [blame] | 6549 | void NVPTXTargetCodeGenInfo::addNVVMMetadata(llvm::Function *F, StringRef Name, |
| 6550 | int Operand) { |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6551 | llvm::Module *M = F->getParent(); |
| 6552 | llvm::LLVMContext &Ctx = M->getContext(); |
| 6553 | |
| 6554 | // Get "nvvm.annotations" metadata node |
| 6555 | llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations"); |
| 6556 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 6557 | llvm::Metadata *MDVals[] = { |
| 6558 | llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, Name), |
| 6559 | llvm::ConstantAsMetadata::get( |
| 6560 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), Operand))}; |
Justin Holewinski | 3683743 | 2013-03-30 14:38:24 +0000 | [diff] [blame] | 6561 | // Append metadata to nvvm.annotations |
| 6562 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 6563 | } |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 6564 | |
| 6565 | bool NVPTXTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 6566 | return false; |
| 6567 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6568 | } |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 6569 | |
| 6570 | //===----------------------------------------------------------------------===// |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6571 | // SystemZ ABI Implementation |
| 6572 | //===----------------------------------------------------------------------===// |
| 6573 | |
| 6574 | namespace { |
| 6575 | |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6576 | class SystemZABIInfo : public SwiftABIInfo { |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6577 | bool HasVector; |
| 6578 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6579 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6580 | SystemZABIInfo(CodeGenTypes &CGT, bool HV) |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6581 | : SwiftABIInfo(CGT), HasVector(HV) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6582 | |
| 6583 | bool isPromotableIntegerType(QualType Ty) const; |
| 6584 | bool isCompoundType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6585 | bool isVectorArgumentType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6586 | bool isFPArgumentType(QualType Ty) const; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6587 | QualType GetSingleElementType(QualType Ty) const; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6588 | |
| 6589 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 6590 | ABIArgInfo classifyArgumentType(QualType ArgTy) const; |
| 6591 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6592 | void computeInfo(CGFunctionInfo &FI) const override { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 6593 | if (!getCXXABI().classifyReturnType(FI)) |
| 6594 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 6595 | for (auto &I : FI.arguments()) |
| 6596 | I.info = classifyArgumentType(I.type); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6597 | } |
| 6598 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6599 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6600 | QualType Ty) const override; |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6601 | |
John McCall | 56331e2 | 2018-01-07 06:28:49 +0000 | [diff] [blame] | 6602 | bool shouldPassIndirectlyForSwift(ArrayRef<llvm::Type*> scalars, |
Bryan Chan | e3f1ed5 | 2016-04-28 13:56:43 +0000 | [diff] [blame] | 6603 | bool asReturnValue) const override { |
| 6604 | return occupiesMoreThan(CGT, scalars, /*total*/ 4); |
| 6605 | } |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6606 | bool isSwiftErrorInRegister() const override { |
Arnold Schwaighofer | 612d693 | 2017-11-07 16:40:51 +0000 | [diff] [blame] | 6607 | return false; |
Arnold Schwaighofer | b0f2c33 | 2016-12-01 18:07:38 +0000 | [diff] [blame] | 6608 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6609 | }; |
| 6610 | |
| 6611 | class SystemZTargetCodeGenInfo : public TargetCodeGenInfo { |
| 6612 | public: |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6613 | SystemZTargetCodeGenInfo(CodeGenTypes &CGT, bool HasVector) |
| 6614 | : TargetCodeGenInfo(new SystemZABIInfo(CGT, HasVector)) {} |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6615 | }; |
| 6616 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6617 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6618 | |
| 6619 | bool SystemZABIInfo::isPromotableIntegerType(QualType Ty) const { |
| 6620 | // Treat an enum type as its underlying type. |
| 6621 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 6622 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 6623 | |
| 6624 | // Promotable integer types are required to be promoted by the ABI. |
| 6625 | if (Ty->isPromotableIntegerType()) |
| 6626 | return true; |
| 6627 | |
| 6628 | // 32-bit values must also be promoted. |
| 6629 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6630 | switch (BT->getKind()) { |
| 6631 | case BuiltinType::Int: |
| 6632 | case BuiltinType::UInt: |
| 6633 | return true; |
| 6634 | default: |
| 6635 | return false; |
| 6636 | } |
| 6637 | return false; |
| 6638 | } |
| 6639 | |
| 6640 | bool SystemZABIInfo::isCompoundType(QualType Ty) const { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6641 | return (Ty->isAnyComplexType() || |
| 6642 | Ty->isVectorType() || |
| 6643 | isAggregateTypeForABI(Ty)); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6646 | bool SystemZABIInfo::isVectorArgumentType(QualType Ty) const { |
| 6647 | return (HasVector && |
| 6648 | Ty->isVectorType() && |
| 6649 | getContext().getTypeSize(Ty) <= 128); |
| 6650 | } |
| 6651 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6652 | bool SystemZABIInfo::isFPArgumentType(QualType Ty) const { |
| 6653 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
| 6654 | switch (BT->getKind()) { |
| 6655 | case BuiltinType::Float: |
| 6656 | case BuiltinType::Double: |
| 6657 | return true; |
| 6658 | default: |
| 6659 | return false; |
| 6660 | } |
| 6661 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6662 | return false; |
| 6663 | } |
| 6664 | |
| 6665 | QualType SystemZABIInfo::GetSingleElementType(QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6666 | if (const RecordType *RT = Ty->getAsStructureType()) { |
| 6667 | const RecordDecl *RD = RT->getDecl(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6668 | QualType Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6669 | |
| 6670 | // If this is a C++ record, check the bases first. |
| 6671 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6672 | for (const auto &I : CXXRD->bases()) { |
| 6673 | QualType Base = I.getType(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6674 | |
| 6675 | // Empty bases don't affect things either way. |
| 6676 | if (isEmptyRecord(getContext(), Base, true)) |
| 6677 | continue; |
| 6678 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6679 | if (!Found.isNull()) |
| 6680 | return Ty; |
| 6681 | Found = GetSingleElementType(Base); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
| 6684 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6685 | for (const auto *FD : RD->fields()) { |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6686 | // For compatibility with GCC, ignore empty bitfields in C++ mode. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6687 | // Unlike isSingleElementStruct(), empty structure and array fields |
| 6688 | // do count. So do anonymous bitfields that aren't zero-sized. |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6689 | if (getContext().getLangOpts().CPlusPlus && |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 6690 | FD->isZeroLengthBitField(getContext())) |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6691 | continue; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6692 | |
| 6693 | // Unlike isSingleElementStruct(), arrays do not count. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6694 | // Nested structures still do though. |
| 6695 | if (!Found.isNull()) |
| 6696 | return Ty; |
| 6697 | Found = GetSingleElementType(FD->getType()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
| 6700 | // Unlike isSingleElementStruct(), trailing padding is allowed. |
| 6701 | // An 8-byte aligned struct s { float f; } is passed as a double. |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6702 | if (!Found.isNull()) |
| 6703 | return Found; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6704 | } |
| 6705 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6706 | return Ty; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6707 | } |
| 6708 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6709 | Address SystemZABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6710 | QualType Ty) const { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6711 | // Assume that va_list type is correct; should be pointer to LLVM type: |
| 6712 | // struct { |
| 6713 | // i64 __gpr; |
| 6714 | // i64 __fpr; |
| 6715 | // i8 *__overflow_arg_area; |
| 6716 | // i8 *__reg_save_area; |
| 6717 | // }; |
| 6718 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6719 | // Every non-vector argument occupies 8 bytes and is passed by preference |
| 6720 | // in either GPRs or FPRs. Vector arguments occupy 8 or 16 bytes and are |
| 6721 | // always passed on the stack. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6722 | Ty = getContext().getCanonicalType(Ty); |
| 6723 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6724 | llvm::Type *ArgTy = CGF.ConvertTypeForMem(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6725 | llvm::Type *DirectTy = ArgTy; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6726 | ABIArgInfo AI = classifyArgumentType(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6727 | bool IsIndirect = AI.isIndirect(); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6728 | bool InFPRs = false; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6729 | bool IsVector = false; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6730 | CharUnits UnpaddedSize; |
| 6731 | CharUnits DirectAlign; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6732 | if (IsIndirect) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6733 | DirectTy = llvm::PointerType::getUnqual(DirectTy); |
| 6734 | UnpaddedSize = DirectAlign = CharUnits::fromQuantity(8); |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6735 | } else { |
| 6736 | if (AI.getCoerceToType()) |
| 6737 | ArgTy = AI.getCoerceToType(); |
| 6738 | InFPRs = ArgTy->isFloatTy() || ArgTy->isDoubleTy(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6739 | IsVector = ArgTy->isVectorTy(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6740 | UnpaddedSize = TyInfo.first; |
| 6741 | DirectAlign = TyInfo.second; |
Ulrich Weigand | 759449c | 2015-03-30 13:49:01 +0000 | [diff] [blame] | 6742 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6743 | CharUnits PaddedSize = CharUnits::fromQuantity(8); |
| 6744 | if (IsVector && UnpaddedSize > PaddedSize) |
| 6745 | PaddedSize = CharUnits::fromQuantity(16); |
| 6746 | assert((UnpaddedSize <= PaddedSize) && "Invalid argument size."); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6747 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6748 | CharUnits Padding = (PaddedSize - UnpaddedSize); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6749 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6750 | llvm::Type *IndexTy = CGF.Int64Ty; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6751 | llvm::Value *PaddedSizeV = |
| 6752 | llvm::ConstantInt::get(IndexTy, PaddedSize.getQuantity()); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6753 | |
| 6754 | if (IsVector) { |
| 6755 | // Work out the address of a vector argument on the stack. |
| 6756 | // Vector arguments are always passed in the high bits of a |
| 6757 | // single (8 byte) or double (16 byte) stack slot. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6758 | Address OverflowArgAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6759 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6760 | Address OverflowArgArea = |
| 6761 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6762 | TyInfo.second); |
| 6763 | Address MemAddr = |
| 6764 | CGF.Builder.CreateElementBitCast(OverflowArgArea, DirectTy, "mem_addr"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6765 | |
| 6766 | // Update overflow_arg_area_ptr pointer |
| 6767 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6768 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6769 | "overflow_arg_area"); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6770 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6771 | |
| 6772 | return MemAddr; |
| 6773 | } |
| 6774 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6775 | assert(PaddedSize.getQuantity() == 8); |
| 6776 | |
| 6777 | unsigned MaxRegs, RegCountField, RegSaveIndex; |
| 6778 | CharUnits RegPadding; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6779 | if (InFPRs) { |
| 6780 | MaxRegs = 4; // Maximum of 4 FPR arguments |
| 6781 | RegCountField = 1; // __fpr |
| 6782 | RegSaveIndex = 16; // save offset for f0 |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6783 | RegPadding = CharUnits(); // floats are passed in the high bits of an FPR |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6784 | } else { |
| 6785 | MaxRegs = 5; // Maximum of 5 GPR arguments |
| 6786 | RegCountField = 0; // __gpr |
| 6787 | RegSaveIndex = 2; // save offset for r2 |
| 6788 | RegPadding = Padding; // values are passed in the low bits of a GPR |
| 6789 | } |
| 6790 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6791 | Address RegCountPtr = |
| 6792 | CGF.Builder.CreateStructGEP(VAListAddr, RegCountField, "reg_count_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6793 | llvm::Value *RegCount = CGF.Builder.CreateLoad(RegCountPtr, "reg_count"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6794 | llvm::Value *MaxRegsV = llvm::ConstantInt::get(IndexTy, MaxRegs); |
| 6795 | llvm::Value *InRegs = CGF.Builder.CreateICmpULT(RegCount, MaxRegsV, |
Oliver Stannard | 405bded | 2014-02-11 09:25:50 +0000 | [diff] [blame] | 6796 | "fits_in_regs"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6797 | |
| 6798 | llvm::BasicBlock *InRegBlock = CGF.createBasicBlock("vaarg.in_reg"); |
| 6799 | llvm::BasicBlock *InMemBlock = CGF.createBasicBlock("vaarg.in_mem"); |
| 6800 | llvm::BasicBlock *ContBlock = CGF.createBasicBlock("vaarg.end"); |
| 6801 | CGF.Builder.CreateCondBr(InRegs, InRegBlock, InMemBlock); |
| 6802 | |
| 6803 | // Emit code to load the value if it was passed in registers. |
| 6804 | CGF.EmitBlock(InRegBlock); |
| 6805 | |
| 6806 | // Work out the address of an argument register. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6807 | llvm::Value *ScaledRegCount = |
| 6808 | CGF.Builder.CreateMul(RegCount, PaddedSizeV, "scaled_reg_count"); |
| 6809 | llvm::Value *RegBase = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6810 | llvm::ConstantInt::get(IndexTy, RegSaveIndex * PaddedSize.getQuantity() |
| 6811 | + RegPadding.getQuantity()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6812 | llvm::Value *RegOffset = |
| 6813 | CGF.Builder.CreateAdd(ScaledRegCount, RegBase, "reg_offset"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6814 | Address RegSaveAreaPtr = |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6815 | CGF.Builder.CreateStructGEP(VAListAddr, 3, "reg_save_area_ptr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6816 | llvm::Value *RegSaveArea = |
| 6817 | CGF.Builder.CreateLoad(RegSaveAreaPtr, "reg_save_area"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6818 | Address RawRegAddr(CGF.Builder.CreateGEP(RegSaveArea, RegOffset, |
| 6819 | "raw_reg_addr"), |
| 6820 | PaddedSize); |
| 6821 | Address RegAddr = |
| 6822 | CGF.Builder.CreateElementBitCast(RawRegAddr, DirectTy, "reg_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6823 | |
| 6824 | // Update the register count |
| 6825 | llvm::Value *One = llvm::ConstantInt::get(IndexTy, 1); |
| 6826 | llvm::Value *NewRegCount = |
| 6827 | CGF.Builder.CreateAdd(RegCount, One, "reg_count"); |
| 6828 | CGF.Builder.CreateStore(NewRegCount, RegCountPtr); |
| 6829 | CGF.EmitBranch(ContBlock); |
| 6830 | |
| 6831 | // Emit code to load the value if it was passed in memory. |
| 6832 | CGF.EmitBlock(InMemBlock); |
| 6833 | |
| 6834 | // Work out the address of a stack argument. |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 6835 | Address OverflowArgAreaPtr = |
| 6836 | CGF.Builder.CreateStructGEP(VAListAddr, 2, "overflow_arg_area_ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6837 | Address OverflowArgArea = |
| 6838 | Address(CGF.Builder.CreateLoad(OverflowArgAreaPtr, "overflow_arg_area"), |
| 6839 | PaddedSize); |
| 6840 | Address RawMemAddr = |
| 6841 | CGF.Builder.CreateConstByteGEP(OverflowArgArea, Padding, "raw_mem_addr"); |
| 6842 | Address MemAddr = |
| 6843 | CGF.Builder.CreateElementBitCast(RawMemAddr, DirectTy, "mem_addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6844 | |
| 6845 | // Update overflow_arg_area_ptr pointer |
| 6846 | llvm::Value *NewOverflowArgArea = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6847 | CGF.Builder.CreateGEP(OverflowArgArea.getPointer(), PaddedSizeV, |
| 6848 | "overflow_arg_area"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6849 | CGF.Builder.CreateStore(NewOverflowArgArea, OverflowArgAreaPtr); |
| 6850 | CGF.EmitBranch(ContBlock); |
| 6851 | |
| 6852 | // Return the appropriate result. |
| 6853 | CGF.EmitBlock(ContBlock); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6854 | Address ResAddr = emitMergePHI(CGF, RegAddr, InRegBlock, |
| 6855 | MemAddr, InMemBlock, "va_arg.addr"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6856 | |
| 6857 | if (IsIndirect) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6858 | ResAddr = Address(CGF.Builder.CreateLoad(ResAddr, "indirect_arg"), |
| 6859 | TyInfo.second); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6860 | |
| 6861 | return ResAddr; |
| 6862 | } |
| 6863 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6864 | ABIArgInfo SystemZABIInfo::classifyReturnType(QualType RetTy) const { |
| 6865 | if (RetTy->isVoidType()) |
| 6866 | return ABIArgInfo::getIgnore(); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6867 | if (isVectorArgumentType(RetTy)) |
| 6868 | return ABIArgInfo::getDirect(); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6869 | if (isCompoundType(RetTy) || getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6870 | return getNaturalAlignIndirect(RetTy); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6871 | return (isPromotableIntegerType(RetTy) ? ABIArgInfo::getExtend(RetTy) |
| 6872 | : ABIArgInfo::getDirect()); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6873 | } |
| 6874 | |
| 6875 | ABIArgInfo SystemZABIInfo::classifyArgumentType(QualType Ty) const { |
| 6876 | // Handle the generic C++ ABI. |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 6877 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6878 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6879 | |
| 6880 | // Integers and enums are extended to full register width. |
| 6881 | if (isPromotableIntegerType(Ty)) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6882 | return ABIArgInfo::getExtend(Ty); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6883 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6884 | // Handle vector types and vector-like structure types. Note that |
| 6885 | // as opposed to float-like structure types, we do not allow any |
| 6886 | // padding for vector-like structures, so verify the sizes match. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6887 | uint64_t Size = getContext().getTypeSize(Ty); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6888 | QualType SingleElementTy = GetSingleElementType(Ty); |
| 6889 | if (isVectorArgumentType(SingleElementTy) && |
| 6890 | getContext().getTypeSize(SingleElementTy) == Size) |
| 6891 | return ABIArgInfo::getDirect(CGT.ConvertType(SingleElementTy)); |
| 6892 | |
| 6893 | // Values that are not 1, 2, 4 or 8 bytes in size are passed indirectly. |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6894 | if (Size != 8 && Size != 16 && Size != 32 && Size != 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6895 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6896 | |
| 6897 | // Handle small structures. |
| 6898 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 6899 | // Structures with flexible arrays have variable length, so really |
| 6900 | // fail the size test above. |
| 6901 | const RecordDecl *RD = RT->getDecl(); |
| 6902 | if (RD->hasFlexibleArrayMember()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6903 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6904 | |
| 6905 | // The structure is passed as an unextended integer, a float, or a double. |
| 6906 | llvm::Type *PassTy; |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 6907 | if (isFPArgumentType(SingleElementTy)) { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6908 | assert(Size == 32 || Size == 64); |
| 6909 | if (Size == 32) |
| 6910 | PassTy = llvm::Type::getFloatTy(getVMContext()); |
| 6911 | else |
| 6912 | PassTy = llvm::Type::getDoubleTy(getVMContext()); |
| 6913 | } else |
| 6914 | PassTy = llvm::IntegerType::get(getVMContext(), Size); |
| 6915 | return ABIArgInfo::getDirect(PassTy); |
| 6916 | } |
| 6917 | |
| 6918 | // Non-structure compounds are passed indirectly. |
| 6919 | if (isCompoundType(Ty)) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6920 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6921 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 6922 | return ABIArgInfo::getDirect(nullptr); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6926 | // MSP430 ABI Implementation |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6927 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6928 | |
| 6929 | namespace { |
| 6930 | |
| 6931 | class MSP430TargetCodeGenInfo : public TargetCodeGenInfo { |
| 6932 | public: |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 6933 | MSP430TargetCodeGenInfo(CodeGenTypes &CGT) |
| 6934 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 6935 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6936 | CodeGen::CodeGenModule &M) const override; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6937 | }; |
| 6938 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 6939 | } |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6940 | |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6941 | void MSP430TargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 6942 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 6943 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6944 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 6945 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6946 | const auto *InterruptAttr = FD->getAttr<MSP430InterruptAttr>(); |
| 6947 | if (!InterruptAttr) |
| 6948 | return; |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6949 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6950 | // Handle 'interrupt' attribute: |
| 6951 | llvm::Function *F = cast<llvm::Function>(GV); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6952 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6953 | // Step 1: Set ISR calling convention. |
| 6954 | F->setCallingConv(llvm::CallingConv::MSP430_INTR); |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 6955 | |
Anton Korobeynikov | 383e827 | 2019-01-16 13:44:01 +0000 | [diff] [blame] | 6956 | // Step 2: Add attributes goodness. |
| 6957 | F->addFnAttr(llvm::Attribute::NoInline); |
| 6958 | F->addFnAttr("interrupt", llvm::utostr(InterruptAttr->getNumber())); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 6959 | } |
| 6960 | } |
| 6961 | |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6962 | //===----------------------------------------------------------------------===// |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6963 | // MIPS ABI Implementation. This works for both little-endian and |
| 6964 | // big-endian variants. |
Chris Lattner | 0cf2419 | 2010-06-28 20:05:43 +0000 | [diff] [blame] | 6965 | //===----------------------------------------------------------------------===// |
| 6966 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6967 | namespace { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6968 | class MipsABIInfo : public ABIInfo { |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6969 | bool IsO32; |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6970 | unsigned MinABIStackAlignInBytes, StackAlignInBytes; |
| 6971 | void CoerceToIntArgs(uint64_t TySize, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 6972 | SmallVectorImpl<llvm::Type *> &ArgList) const; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 6973 | llvm::Type* HandleAggregates(QualType Ty, uint64_t TySize) const; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 6974 | llvm::Type* returnAggregateInRegs(QualType RetTy, uint64_t Size) const; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 6975 | llvm::Type* getPaddingType(uint64_t Align, uint64_t Offset) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6976 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6977 | MipsABIInfo(CodeGenTypes &CGT, bool _IsO32) : |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 6978 | ABIInfo(CGT), IsO32(_IsO32), MinABIStackAlignInBytes(IsO32 ? 4 : 8), |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6979 | StackAlignInBytes(IsO32 ? 8 : 16) {} |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6980 | |
| 6981 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 6982 | ABIArgInfo classifyArgumentType(QualType RetTy, uint64_t &Offset) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6983 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 6984 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 6985 | QualType Ty) const override; |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 6986 | ABIArgInfo extendType(QualType Ty) const; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 6987 | }; |
| 6988 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6989 | class MIPSTargetCodeGenInfo : public TargetCodeGenInfo { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 6990 | unsigned SizeOfUnwindException; |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6991 | public: |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 6992 | MIPSTargetCodeGenInfo(CodeGenTypes &CGT, bool IsO32) |
| 6993 | : TargetCodeGenInfo(new MipsABIInfo(CGT, IsO32)), |
Akira Hatanaka | 1437852 | 2011-11-02 23:14:57 +0000 | [diff] [blame] | 6994 | SizeOfUnwindException(IsO32 ? 24 : 32) {} |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6995 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 6996 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &CGM) const override { |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 6997 | return 29; |
| 6998 | } |
| 6999 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7000 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7001 | CodeGen::CodeGenModule &CGM) const override { |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7002 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 7003 | if (!FD) return; |
Rafael Espindola | a0851a2 | 2013-03-19 14:32:23 +0000 | [diff] [blame] | 7004 | llvm::Function *Fn = cast<llvm::Function>(GV); |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7005 | |
| 7006 | if (FD->hasAttr<MipsLongCallAttr>()) |
| 7007 | Fn->addFnAttr("long-call"); |
| 7008 | else if (FD->hasAttr<MipsShortCallAttr>()) |
| 7009 | Fn->addFnAttr("short-call"); |
| 7010 | |
| 7011 | // Other attributes do not have a meaning for declarations. |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7012 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7013 | return; |
| 7014 | |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 7015 | if (FD->hasAttr<Mips16Attr>()) { |
| 7016 | Fn->addFnAttr("mips16"); |
| 7017 | } |
| 7018 | else if (FD->hasAttr<NoMips16Attr>()) { |
| 7019 | Fn->addFnAttr("nomips16"); |
| 7020 | } |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 7021 | |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 7022 | if (FD->hasAttr<MicroMipsAttr>()) |
| 7023 | Fn->addFnAttr("micromips"); |
| 7024 | else if (FD->hasAttr<NoMicroMipsAttr>()) |
| 7025 | Fn->addFnAttr("nomicromips"); |
| 7026 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 7027 | const MipsInterruptAttr *Attr = FD->getAttr<MipsInterruptAttr>(); |
| 7028 | if (!Attr) |
| 7029 | return; |
| 7030 | |
| 7031 | const char *Kind; |
| 7032 | switch (Attr->getInterrupt()) { |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 7033 | case MipsInterruptAttr::eic: Kind = "eic"; break; |
| 7034 | case MipsInterruptAttr::sw0: Kind = "sw0"; break; |
| 7035 | case MipsInterruptAttr::sw1: Kind = "sw1"; break; |
| 7036 | case MipsInterruptAttr::hw0: Kind = "hw0"; break; |
| 7037 | case MipsInterruptAttr::hw1: Kind = "hw1"; break; |
| 7038 | case MipsInterruptAttr::hw2: Kind = "hw2"; break; |
| 7039 | case MipsInterruptAttr::hw3: Kind = "hw3"; break; |
| 7040 | case MipsInterruptAttr::hw4: Kind = "hw4"; break; |
| 7041 | case MipsInterruptAttr::hw5: Kind = "hw5"; break; |
| 7042 | } |
| 7043 | |
| 7044 | Fn->addFnAttr("interrupt", Kind); |
| 7045 | |
Reed Kotler | 373feca | 2013-01-16 17:10:28 +0000 | [diff] [blame] | 7046 | } |
Reed Kotler | 3d5966f | 2013-03-13 20:40:30 +0000 | [diff] [blame] | 7047 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7048 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7049 | llvm::Value *Address) const override; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 7050 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7051 | unsigned getSizeOfUnwindException() const override { |
Akira Hatanaka | 0486db0 | 2011-09-20 18:23:28 +0000 | [diff] [blame] | 7052 | return SizeOfUnwindException; |
John McCall | 3480ef2 | 2011-08-30 01:42:09 +0000 | [diff] [blame] | 7053 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7054 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7055 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7056 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7057 | void MipsABIInfo::CoerceToIntArgs( |
| 7058 | uint64_t TySize, SmallVectorImpl<llvm::Type *> &ArgList) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7059 | llvm::IntegerType *IntTy = |
| 7060 | llvm::IntegerType::get(getVMContext(), MinABIStackAlignInBytes * 8); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7061 | |
| 7062 | // Add (TySize / MinABIStackAlignInBytes) args of IntTy. |
| 7063 | for (unsigned N = TySize / (MinABIStackAlignInBytes * 8); N; --N) |
| 7064 | ArgList.push_back(IntTy); |
| 7065 | |
| 7066 | // If necessary, add one more integer type to ArgList. |
| 7067 | unsigned R = TySize % (MinABIStackAlignInBytes * 8); |
| 7068 | |
| 7069 | if (R) |
| 7070 | ArgList.push_back(llvm::IntegerType::get(getVMContext(), R)); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7071 | } |
| 7072 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7073 | // In N32/64, an aligned double precision floating point field is passed in |
| 7074 | // a register. |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7075 | llvm::Type* MipsABIInfo::HandleAggregates(QualType Ty, uint64_t TySize) const { |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7076 | SmallVector<llvm::Type*, 8> ArgList, IntArgList; |
| 7077 | |
| 7078 | if (IsO32) { |
| 7079 | CoerceToIntArgs(TySize, ArgList); |
| 7080 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7081 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7082 | |
Akira Hatanaka | 02e13e5 | 2012-01-12 00:52:17 +0000 | [diff] [blame] | 7083 | if (Ty->isComplexType()) |
| 7084 | return CGT.ConvertType(Ty); |
Akira Hatanaka | 79f0461 | 2012-01-10 23:12:19 +0000 | [diff] [blame] | 7085 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 7086 | const RecordType *RT = Ty->getAs<RecordType>(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7087 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7088 | // Unions/vectors are passed in integer registers. |
| 7089 | if (!RT || !RT->isStructureOrClassType()) { |
| 7090 | CoerceToIntArgs(TySize, ArgList); |
| 7091 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7092 | } |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7093 | |
| 7094 | const RecordDecl *RD = RT->getDecl(); |
| 7095 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7096 | assert(!(TySize % 8) && "Size of structure must be multiple of 8."); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7097 | |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7098 | uint64_t LastOffset = 0; |
| 7099 | unsigned idx = 0; |
| 7100 | llvm::IntegerType *I64 = llvm::IntegerType::get(getVMContext(), 64); |
| 7101 | |
Akira Hatanaka | 4984f5d | 2012-02-09 19:54:16 +0000 | [diff] [blame] | 7102 | // Iterate over fields in the struct/class and check if there are any aligned |
| 7103 | // double fields. |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7104 | for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); |
| 7105 | i != e; ++i, ++idx) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7106 | const QualType Ty = i->getType(); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7107 | const BuiltinType *BT = Ty->getAs<BuiltinType>(); |
| 7108 | |
| 7109 | if (!BT || BT->getKind() != BuiltinType::Double) |
| 7110 | continue; |
| 7111 | |
| 7112 | uint64_t Offset = Layout.getFieldOffset(idx); |
| 7113 | if (Offset % 64) // Ignore doubles that are not aligned. |
| 7114 | continue; |
| 7115 | |
| 7116 | // Add ((Offset - LastOffset) / 64) args of type i64. |
| 7117 | for (unsigned j = (Offset - LastOffset) / 64; j > 0; --j) |
| 7118 | ArgList.push_back(I64); |
| 7119 | |
| 7120 | // Add double type. |
| 7121 | ArgList.push_back(llvm::Type::getDoubleTy(getVMContext())); |
| 7122 | LastOffset = Offset + 64; |
| 7123 | } |
| 7124 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7125 | CoerceToIntArgs(TySize - LastOffset, IntArgList); |
| 7126 | ArgList.append(IntArgList.begin(), IntArgList.end()); |
Akira Hatanaka | 101f70d | 2011-11-02 23:54:49 +0000 | [diff] [blame] | 7127 | |
| 7128 | return llvm::StructType::get(getVMContext(), ArgList); |
| 7129 | } |
| 7130 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7131 | llvm::Type *MipsABIInfo::getPaddingType(uint64_t OrigOffset, |
| 7132 | uint64_t Offset) const { |
| 7133 | if (OrigOffset + MinABIStackAlignInBytes > Offset) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7134 | return nullptr; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7135 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7136 | return llvm::IntegerType::get(getVMContext(), (Offset - OrigOffset) * 8); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7137 | } |
Akira Hatanaka | 21ee88c | 2012-01-10 22:44:52 +0000 | [diff] [blame] | 7138 | |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7139 | ABIArgInfo |
| 7140 | MipsABIInfo::classifyArgumentType(QualType Ty, uint64_t &Offset) const { |
Daniel Sanders | 998c910 | 2015-01-14 12:00:12 +0000 | [diff] [blame] | 7141 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7142 | |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7143 | uint64_t OrigOffset = Offset; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7144 | uint64_t TySize = getContext().getTypeSize(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7145 | uint64_t Align = getContext().getTypeAlign(Ty) / 8; |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7146 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7147 | Align = std::min(std::max(Align, (uint64_t)MinABIStackAlignInBytes), |
| 7148 | (uint64_t)StackAlignInBytes); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 7149 | unsigned CurrOffset = llvm::alignTo(Offset, Align); |
| 7150 | Offset = CurrOffset + llvm::alignTo(TySize, Align * 8) / 8; |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7151 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7152 | if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) { |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7153 | // Ignore empty aggregates. |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7154 | if (TySize == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7155 | return ABIArgInfo::getIgnore(); |
| 7156 | |
Mark Lacey | 3825e83 | 2013-10-06 01:33:34 +0000 | [diff] [blame] | 7157 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7158 | Offset = OrigOffset + MinABIStackAlignInBytes; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7159 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Akira Hatanaka | f64e1ad | 2012-01-07 00:25:33 +0000 | [diff] [blame] | 7160 | } |
Akira Hatanaka | df425db | 2011-08-01 18:09:58 +0000 | [diff] [blame] | 7161 | |
Akira Hatanaka | 8ab86cb | 2012-05-11 21:56:58 +0000 | [diff] [blame] | 7162 | // If we have reached here, aggregates are passed directly by coercing to |
| 7163 | // another structure type. Padding is inserted if the offset of the |
| 7164 | // aggregate is unaligned. |
Daniel Sanders | aa1b355 | 2014-10-24 15:30:16 +0000 | [diff] [blame] | 7165 | ABIArgInfo ArgInfo = |
| 7166 | ABIArgInfo::getDirect(HandleAggregates(Ty, TySize), 0, |
| 7167 | getPaddingType(OrigOffset, CurrOffset)); |
| 7168 | ArgInfo.setInReg(true); |
| 7169 | return ArgInfo; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7170 | } |
| 7171 | |
| 7172 | // Treat an enum type as its underlying type. |
| 7173 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7174 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7175 | |
Daniel Sanders | 5b445b3 | 2014-10-24 14:42:42 +0000 | [diff] [blame] | 7176 | // All integral types are promoted to the GPR width. |
| 7177 | if (Ty->isIntegralOrEnumerationType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7178 | return extendType(Ty); |
Akira Hatanaka | 1632af6 | 2012-01-09 19:31:25 +0000 | [diff] [blame] | 7179 | |
Akira Hatanaka | ddd6634 | 2013-10-29 18:41:15 +0000 | [diff] [blame] | 7180 | return ABIArgInfo::getDirect( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 7181 | nullptr, 0, IsO32 ? nullptr : getPaddingType(OrigOffset, CurrOffset)); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7182 | } |
| 7183 | |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7184 | llvm::Type* |
| 7185 | MipsABIInfo::returnAggregateInRegs(QualType RetTy, uint64_t Size) const { |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7186 | const RecordType *RT = RetTy->getAs<RecordType>(); |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7187 | SmallVector<llvm::Type*, 8> RTList; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7188 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7189 | if (RT && RT->isStructureOrClassType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7190 | const RecordDecl *RD = RT->getDecl(); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7191 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 7192 | unsigned FieldCnt = Layout.getFieldCount(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7193 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7194 | // N32/64 returns struct/classes in floating point registers if the |
| 7195 | // following conditions are met: |
| 7196 | // 1. The size of the struct/class is no larger than 128-bit. |
| 7197 | // 2. The struct/class has one or two fields all of which are floating |
| 7198 | // point types. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7199 | // 3. The offset of the first field is zero (this follows what gcc does). |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7200 | // |
| 7201 | // Any other composite results are returned in integer registers. |
| 7202 | // |
| 7203 | if (FieldCnt && (FieldCnt <= 2) && !Layout.getFieldOffset(0)) { |
| 7204 | RecordDecl::field_iterator b = RD->field_begin(), e = RD->field_end(); |
| 7205 | for (; b != e; ++b) { |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7206 | const BuiltinType *BT = b->getType()->getAs<BuiltinType>(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7207 | |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7208 | if (!BT || !BT->isFloatingPoint()) |
| 7209 | break; |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7210 | |
David Blaikie | 2d7c57e | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 7211 | RTList.push_back(CGT.ConvertType(b->getType())); |
Akira Hatanaka | b6f7443 | 2012-02-09 18:49:26 +0000 | [diff] [blame] | 7212 | } |
| 7213 | |
| 7214 | if (b == e) |
| 7215 | return llvm::StructType::get(getVMContext(), RTList, |
| 7216 | RD->hasAttr<PackedAttr>()); |
| 7217 | |
| 7218 | RTList.clear(); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7219 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7220 | } |
| 7221 | |
Akira Hatanaka | e1e3ad3 | 2012-07-03 19:24:06 +0000 | [diff] [blame] | 7222 | CoerceToIntArgs(Size, RTList); |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7223 | return llvm::StructType::get(getVMContext(), RTList); |
| 7224 | } |
| 7225 | |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7226 | ABIArgInfo MipsABIInfo::classifyReturnType(QualType RetTy) const { |
Akira Hatanaka | 60f5fe6 | 2012-01-23 23:18:57 +0000 | [diff] [blame] | 7227 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7228 | |
Daniel Sanders | ed39f58 | 2014-09-04 13:28:14 +0000 | [diff] [blame] | 7229 | if (RetTy->isVoidType()) |
| 7230 | return ABIArgInfo::getIgnore(); |
| 7231 | |
| 7232 | // O32 doesn't treat zero-sized structs differently from other structs. |
| 7233 | // However, N32/N64 ignores zero sized return values. |
| 7234 | if (!IsO32 && Size == 0) |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7235 | return ABIArgInfo::getIgnore(); |
| 7236 | |
Akira Hatanaka | c37eddf | 2012-05-11 21:01:17 +0000 | [diff] [blame] | 7237 | if (isAggregateTypeForABI(RetTy) || RetTy->isVectorType()) { |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7238 | if (Size <= 128) { |
| 7239 | if (RetTy->isAnyComplexType()) |
| 7240 | return ABIArgInfo::getDirect(); |
| 7241 | |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7242 | // O32 returns integer vectors in registers and N32/N64 returns all small |
Daniel Sanders | 00a56ff | 2014-09-04 15:07:43 +0000 | [diff] [blame] | 7243 | // aggregates in registers. |
Daniel Sanders | e5018b6 | 2014-09-04 15:05:39 +0000 | [diff] [blame] | 7244 | if (!IsO32 || |
| 7245 | (RetTy->isVectorType() && !RetTy->hasFloatingRepresentation())) { |
| 7246 | ABIArgInfo ArgInfo = |
| 7247 | ABIArgInfo::getDirect(returnAggregateInRegs(RetTy, Size)); |
| 7248 | ArgInfo.setInReg(true); |
| 7249 | return ArgInfo; |
| 7250 | } |
Akira Hatanaka | f093f5b | 2012-01-04 03:34:42 +0000 | [diff] [blame] | 7251 | } |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7252 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7253 | return getNaturalAlignIndirect(RetTy); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7254 | } |
| 7255 | |
| 7256 | // Treat an enum type as its underlying type. |
| 7257 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7258 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7259 | |
Stefan Maksimovic | b9da8a5 | 2018-07-30 10:44:46 +0000 | [diff] [blame] | 7260 | if (RetTy->isPromotableIntegerType()) |
| 7261 | return ABIArgInfo::getExtend(RetTy); |
| 7262 | |
| 7263 | if ((RetTy->isUnsignedIntegerOrEnumerationType() || |
| 7264 | RetTy->isSignedIntegerOrEnumerationType()) && Size == 32 && !IsO32) |
| 7265 | return ABIArgInfo::getSignExtend(RetTy); |
| 7266 | |
| 7267 | return ABIArgInfo::getDirect(); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7268 | } |
| 7269 | |
| 7270 | void MipsABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7271 | ABIArgInfo &RetInfo = FI.getReturnInfo(); |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7272 | if (!getCXXABI().classifyReturnType(FI)) |
| 7273 | RetInfo = classifyReturnType(FI.getReturnType()); |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7274 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7275 | // 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] | 7276 | uint64_t Offset = RetInfo.isIndirect() ? MinABIStackAlignInBytes : 0; |
Akira Hatanaka | 32604a9 | 2012-01-12 01:10:09 +0000 | [diff] [blame] | 7277 | |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7278 | for (auto &I : FI.arguments()) |
| 7279 | I.info = classifyArgumentType(I.type, Offset); |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7282 | Address MipsABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7283 | QualType OrigTy) const { |
| 7284 | QualType Ty = OrigTy; |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7285 | |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7286 | // Integer arguments are promoted to 32-bit on O32 and 64-bit on N32/N64. |
| 7287 | // Pointers are also promoted in the same way but this only matters for N32. |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7288 | unsigned SlotSizeInBits = IsO32 ? 32 : 64; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7289 | unsigned PtrWidth = getTarget().getPointerWidth(0); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7290 | bool DidPromote = false; |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7291 | if ((Ty->isIntegerType() && |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7292 | getContext().getIntWidth(Ty) < SlotSizeInBits) || |
Daniel Sanders | cdcb580 | 2015-01-13 10:47:00 +0000 | [diff] [blame] | 7293 | (Ty->isPointerType() && PtrWidth < SlotSizeInBits)) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7294 | DidPromote = true; |
| 7295 | Ty = getContext().getIntTypeForBitwidth(SlotSizeInBits, |
| 7296 | Ty->isSignedIntegerType()); |
Daniel Sanders | 59229dc | 2014-11-19 10:01:35 +0000 | [diff] [blame] | 7297 | } |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7298 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7299 | auto TyInfo = getContext().getTypeInfoInChars(Ty); |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7300 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7301 | // The alignment of things in the argument area is never larger than |
| 7302 | // StackAlignInBytes. |
| 7303 | TyInfo.second = |
| 7304 | std::min(TyInfo.second, CharUnits::fromQuantity(StackAlignInBytes)); |
| 7305 | |
| 7306 | // MinABIStackAlignInBytes is the size of argument slots on the stack. |
| 7307 | CharUnits ArgSlotSize = CharUnits::fromQuantity(MinABIStackAlignInBytes); |
| 7308 | |
| 7309 | Address Addr = emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7310 | TyInfo, ArgSlotSize, /*AllowHigherAlign*/ true); |
| 7311 | |
| 7312 | |
| 7313 | // If there was a promotion, "unpromote" into a temporary. |
| 7314 | // TODO: can we just use a pointer into a subset of the original slot? |
| 7315 | if (DidPromote) { |
| 7316 | Address Temp = CGF.CreateMemTemp(OrigTy, "vaarg.promotion-temp"); |
| 7317 | llvm::Value *Promoted = CGF.Builder.CreateLoad(Addr); |
| 7318 | |
| 7319 | // Truncate down to the right width. |
| 7320 | llvm::Type *IntTy = (OrigTy->isIntegerType() ? Temp.getElementType() |
| 7321 | : CGF.IntPtrTy); |
| 7322 | llvm::Value *V = CGF.Builder.CreateTrunc(Promoted, IntTy); |
| 7323 | if (OrigTy->isPointerType()) |
| 7324 | V = CGF.Builder.CreateIntToPtr(V, Temp.getElementType()); |
| 7325 | |
| 7326 | CGF.Builder.CreateStore(V, Temp); |
| 7327 | Addr = Temp; |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7328 | } |
Daniel Sanders | 2ef3cdd3 | 2014-08-01 13:26:28 +0000 | [diff] [blame] | 7329 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7330 | return Addr; |
Akira Hatanaka | b579fe5 | 2011-06-02 00:09:17 +0000 | [diff] [blame] | 7331 | } |
| 7332 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7333 | ABIArgInfo MipsABIInfo::extendType(QualType Ty) const { |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7334 | int TySize = getContext().getTypeSize(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7335 | |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7336 | // MIPS64 ABI requires unsigned 32 bit integers to be sign extended. |
| 7337 | if (Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7338 | return ABIArgInfo::getSignExtend(Ty); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7339 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7340 | return ABIArgInfo::getExtend(Ty); |
Petar Jovanovic | 1a3f965 | 2015-05-26 21:07:19 +0000 | [diff] [blame] | 7341 | } |
| 7342 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7343 | bool |
| 7344 | MIPSTargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 7345 | llvm::Value *Address) const { |
| 7346 | // This information comes from gcc's implementation, which seems to |
| 7347 | // as canonical as it gets. |
| 7348 | |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7349 | // Everything on MIPS is 4 bytes. Double-precision FP registers |
| 7350 | // are aliased to pairs of single-precision FP registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7351 | llvm::Value *Four8 = llvm::ConstantInt::get(CGF.Int8Ty, 4); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7352 | |
| 7353 | // 0-31 are the general purpose registers, $0 - $31. |
| 7354 | // 32-63 are the floating-point registers, $f0 - $f31. |
| 7355 | // 64 and 65 are the multiply/divide registers, $hi and $lo. |
| 7356 | // 66 is the (notional, I think) register for signal-handler return. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7357 | AssignToArrayRange(CGF.Builder, Address, Four8, 0, 65); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7358 | |
| 7359 | // 67-74 are the floating-point status registers, $fcc0 - $fcc7. |
| 7360 | // They are one bit wide and ignored here. |
| 7361 | |
| 7362 | // 80-111 are the coprocessor 0 registers, $c0r0 - $c0r31. |
| 7363 | // (coprocessor 1 is the FP unit) |
| 7364 | // 112-143 are the coprocessor 2 registers, $c2r0 - $c2r31. |
| 7365 | // 144-175 are the coprocessor 3 registers, $c3r0 - $c3r31. |
| 7366 | // 176-181 are the DSP accumulator registers. |
Chris Lattner | ece0409 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 7367 | AssignToArrayRange(CGF.Builder, Address, Four8, 80, 181); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7368 | return false; |
| 7369 | } |
| 7370 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7371 | //===----------------------------------------------------------------------===// |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7372 | // AVR ABI Implementation. |
| 7373 | //===----------------------------------------------------------------------===// |
| 7374 | |
| 7375 | namespace { |
| 7376 | class AVRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7377 | public: |
| 7378 | AVRTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7379 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) { } |
| 7380 | |
| 7381 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7382 | CodeGen::CodeGenModule &CGM) const override { |
| 7383 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7384 | return; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 7385 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 7386 | if (!FD) return; |
| 7387 | auto *Fn = cast<llvm::Function>(GV); |
| 7388 | |
| 7389 | if (FD->getAttr<AVRInterruptAttr>()) |
| 7390 | Fn->addFnAttr("interrupt"); |
| 7391 | |
| 7392 | if (FD->getAttr<AVRSignalAttr>()) |
| 7393 | Fn->addFnAttr("signal"); |
| 7394 | } |
| 7395 | }; |
| 7396 | } |
| 7397 | |
| 7398 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7399 | // TCE ABI Implementation (see http://tce.cs.tut.fi). Uses mostly the defaults. |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7400 | // Currently subclassed only to implement custom OpenCL C function attribute |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7401 | // handling. |
| 7402 | //===----------------------------------------------------------------------===// |
| 7403 | |
| 7404 | namespace { |
| 7405 | |
| 7406 | class TCETargetCodeGenInfo : public DefaultTargetCodeGenInfo { |
| 7407 | public: |
| 7408 | TCETargetCodeGenInfo(CodeGenTypes &CGT) |
| 7409 | : DefaultTargetCodeGenInfo(CGT) {} |
| 7410 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7411 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7412 | CodeGen::CodeGenModule &M) const override; |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7413 | }; |
| 7414 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7415 | void TCETargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7416 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
| 7417 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 7418 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 7419 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7420 | if (!FD) return; |
| 7421 | |
| 7422 | llvm::Function *F = cast<llvm::Function>(GV); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7423 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7424 | if (M.getLangOpts().OpenCL) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7425 | if (FD->hasAttr<OpenCLKernelAttr>()) { |
| 7426 | // OpenCL C Kernel functions are not subject to inlining |
Bill Wendling | 207f053 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 7427 | F->addFnAttr(llvm::Attribute::NoInline); |
Aaron Ballman | 36a18ff | 2013-12-19 13:16:35 +0000 | [diff] [blame] | 7428 | const ReqdWorkGroupSizeAttr *Attr = FD->getAttr<ReqdWorkGroupSizeAttr>(); |
| 7429 | if (Attr) { |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7430 | // Convert the reqd_work_group_size() attributes to metadata. |
| 7431 | llvm::LLVMContext &Context = F->getContext(); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7432 | llvm::NamedMDNode *OpenCLMetadata = |
| 7433 | M.getModule().getOrInsertNamedMetadata( |
| 7434 | "opencl.kernel_wg_size_info"); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7435 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7436 | SmallVector<llvm::Metadata *, 5> Operands; |
| 7437 | Operands.push_back(llvm::ConstantAsMetadata::get(F)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7438 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7439 | Operands.push_back( |
| 7440 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7441 | M.Int32Ty, llvm::APInt(32, Attr->getXDim())))); |
| 7442 | Operands.push_back( |
| 7443 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7444 | M.Int32Ty, llvm::APInt(32, Attr->getYDim())))); |
| 7445 | Operands.push_back( |
| 7446 | llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue( |
| 7447 | M.Int32Ty, llvm::APInt(32, Attr->getZDim())))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7448 | |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 7449 | // Add a boolean constant operand for "required" (true) or "hint" |
| 7450 | // (false) for implementing the work_group_size_hint attr later. |
| 7451 | // Currently always true as the hint is not yet implemented. |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 7452 | Operands.push_back( |
| 7453 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::getTrue(Context))); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 7454 | OpenCLMetadata->addOperand(llvm::MDNode::get(Context, Operands)); |
| 7455 | } |
| 7456 | } |
| 7457 | } |
| 7458 | } |
| 7459 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7460 | } |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 7461 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7462 | //===----------------------------------------------------------------------===// |
| 7463 | // Hexagon ABI Implementation |
| 7464 | //===----------------------------------------------------------------------===// |
| 7465 | |
| 7466 | namespace { |
| 7467 | |
| 7468 | class HexagonABIInfo : public ABIInfo { |
| 7469 | |
| 7470 | |
| 7471 | public: |
| 7472 | HexagonABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 7473 | |
| 7474 | private: |
| 7475 | |
| 7476 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7477 | ABIArgInfo classifyArgumentType(QualType RetTy) const; |
| 7478 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7479 | void computeInfo(CGFunctionInfo &FI) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7480 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7481 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7482 | QualType Ty) const override; |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7483 | }; |
| 7484 | |
| 7485 | class HexagonTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7486 | public: |
| 7487 | HexagonTargetCodeGenInfo(CodeGenTypes &CGT) |
| 7488 | :TargetCodeGenInfo(new HexagonABIInfo(CGT)) {} |
| 7489 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 7490 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7491 | return 29; |
| 7492 | } |
| 7493 | }; |
| 7494 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7495 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7496 | |
| 7497 | void HexagonABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Reid Kleckner | 40ca913 | 2014-05-13 22:05:45 +0000 | [diff] [blame] | 7498 | if (!getCXXABI().classifyReturnType(FI)) |
| 7499 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 7500 | for (auto &I : FI.arguments()) |
| 7501 | I.info = classifyArgumentType(I.type); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7502 | } |
| 7503 | |
| 7504 | ABIArgInfo HexagonABIInfo::classifyArgumentType(QualType Ty) const { |
| 7505 | if (!isAggregateTypeForABI(Ty)) { |
| 7506 | // Treat an enum type as its underlying type. |
| 7507 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 7508 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7509 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7510 | return (Ty->isPromotableIntegerType() ? ABIArgInfo::getExtend(Ty) |
| 7511 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7512 | } |
| 7513 | |
Krzysztof Parzyszek | 408b272 | 2017-05-12 13:18:07 +0000 | [diff] [blame] | 7514 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7515 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7516 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7517 | // Ignore empty records. |
| 7518 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7519 | return ABIArgInfo::getIgnore(); |
| 7520 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7521 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7522 | if (Size > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7523 | return getNaturalAlignIndirect(Ty, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7524 | // Pass in the smallest viable integer type. |
| 7525 | else if (Size > 32) |
| 7526 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7527 | else if (Size > 16) |
| 7528 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7529 | else if (Size > 8) |
| 7530 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7531 | else |
| 7532 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7533 | } |
| 7534 | |
| 7535 | ABIArgInfo HexagonABIInfo::classifyReturnType(QualType RetTy) const { |
| 7536 | if (RetTy->isVoidType()) |
| 7537 | return ABIArgInfo::getIgnore(); |
| 7538 | |
| 7539 | // Large vector types should be returned via memory. |
| 7540 | if (RetTy->isVectorType() && getContext().getTypeSize(RetTy) > 64) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7541 | return getNaturalAlignIndirect(RetTy); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7542 | |
| 7543 | if (!isAggregateTypeForABI(RetTy)) { |
| 7544 | // Treat an enum type as its underlying type. |
| 7545 | if (const EnumType *EnumTy = RetTy->getAs<EnumType>()) |
| 7546 | RetTy = EnumTy->getDecl()->getIntegerType(); |
| 7547 | |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7548 | return (RetTy->isPromotableIntegerType() ? ABIArgInfo::getExtend(RetTy) |
| 7549 | : ABIArgInfo::getDirect()); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7550 | } |
| 7551 | |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7552 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7553 | return ABIArgInfo::getIgnore(); |
| 7554 | |
| 7555 | // Aggregates <= 8 bytes are returned in r0; other aggregates |
| 7556 | // are returned indirectly. |
| 7557 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7558 | if (Size <= 64) { |
| 7559 | // Return in the smallest viable integer type. |
| 7560 | if (Size <= 8) |
| 7561 | return ABIArgInfo::getDirect(llvm::Type::getInt8Ty(getVMContext())); |
| 7562 | if (Size <= 16) |
| 7563 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7564 | if (Size <= 32) |
| 7565 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7566 | return ABIArgInfo::getDirect(llvm::Type::getInt64Ty(getVMContext())); |
| 7567 | } |
| 7568 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7569 | return getNaturalAlignIndirect(RetTy, /*ByVal=*/true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7570 | } |
| 7571 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 7572 | Address HexagonABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7573 | QualType Ty) const { |
| 7574 | // FIXME: Someone needs to audit that this handle alignment correctly. |
| 7575 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 7576 | getContext().getTypeInfoInChars(Ty), |
| 7577 | CharUnits::fromQuantity(4), |
| 7578 | /*AllowHigherAlign*/ true); |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 7579 | } |
| 7580 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7581 | //===----------------------------------------------------------------------===// |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7582 | // Lanai ABI Implementation |
| 7583 | //===----------------------------------------------------------------------===// |
| 7584 | |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7585 | namespace { |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7586 | class LanaiABIInfo : public DefaultABIInfo { |
| 7587 | public: |
| 7588 | LanaiABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 7589 | |
| 7590 | bool shouldUseInReg(QualType Ty, CCState &State) const; |
| 7591 | |
| 7592 | void computeInfo(CGFunctionInfo &FI) const override { |
| 7593 | CCState State(FI.getCallingConvention()); |
| 7594 | // Lanai uses 4 registers to pass arguments unless the function has the |
| 7595 | // regparm attribute set. |
| 7596 | if (FI.getHasRegParm()) { |
| 7597 | State.FreeRegs = FI.getRegParm(); |
| 7598 | } else { |
| 7599 | State.FreeRegs = 4; |
| 7600 | } |
| 7601 | |
| 7602 | if (!getCXXABI().classifyReturnType(FI)) |
| 7603 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7604 | for (auto &I : FI.arguments()) |
| 7605 | I.info = classifyArgumentType(I.type, State); |
| 7606 | } |
| 7607 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7608 | ABIArgInfo getIndirectResult(QualType Ty, bool ByVal, CCState &State) const; |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7609 | ABIArgInfo classifyArgumentType(QualType RetTy, CCState &State) const; |
| 7610 | }; |
Benjamin Kramer | 5d28c7f | 2016-04-07 10:14:54 +0000 | [diff] [blame] | 7611 | } // end anonymous namespace |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7612 | |
| 7613 | bool LanaiABIInfo::shouldUseInReg(QualType Ty, CCState &State) const { |
| 7614 | unsigned Size = getContext().getTypeSize(Ty); |
| 7615 | unsigned SizeInRegs = llvm::alignTo(Size, 32U) / 32U; |
| 7616 | |
| 7617 | if (SizeInRegs == 0) |
| 7618 | return false; |
| 7619 | |
| 7620 | if (SizeInRegs > State.FreeRegs) { |
| 7621 | State.FreeRegs = 0; |
| 7622 | return false; |
| 7623 | } |
| 7624 | |
| 7625 | State.FreeRegs -= SizeInRegs; |
| 7626 | |
| 7627 | return true; |
| 7628 | } |
| 7629 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7630 | ABIArgInfo LanaiABIInfo::getIndirectResult(QualType Ty, bool ByVal, |
| 7631 | CCState &State) const { |
| 7632 | if (!ByVal) { |
| 7633 | if (State.FreeRegs) { |
| 7634 | --State.FreeRegs; // Non-byval indirects just use one pointer. |
| 7635 | return getNaturalAlignIndirectInReg(Ty); |
| 7636 | } |
| 7637 | return getNaturalAlignIndirect(Ty, false); |
| 7638 | } |
| 7639 | |
| 7640 | // Compute the byval alignment. |
Kostya Serebryany | 0da4442 | 2016-04-26 01:53:49 +0000 | [diff] [blame] | 7641 | const unsigned MinABIStackAlignInBytes = 4; |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7642 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 7643 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 7644 | /*Realign=*/TypeAlign > |
| 7645 | MinABIStackAlignInBytes); |
| 7646 | } |
| 7647 | |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7648 | ABIArgInfo LanaiABIInfo::classifyArgumentType(QualType Ty, |
| 7649 | CCState &State) const { |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7650 | // Check with the C++ ABI first. |
| 7651 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 7652 | if (RT) { |
| 7653 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 7654 | if (RAA == CGCXXABI::RAA_Indirect) { |
| 7655 | return getIndirectResult(Ty, /*ByVal=*/false, State); |
| 7656 | } else if (RAA == CGCXXABI::RAA_DirectInMemory) { |
| 7657 | return getNaturalAlignIndirect(Ty, /*ByRef=*/true); |
| 7658 | } |
| 7659 | } |
| 7660 | |
| 7661 | if (isAggregateTypeForABI(Ty)) { |
| 7662 | // Structures with flexible arrays are always indirect. |
| 7663 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 7664 | return getIndirectResult(Ty, /*ByVal=*/true, State); |
| 7665 | |
| 7666 | // Ignore empty structs/unions. |
| 7667 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7668 | return ABIArgInfo::getIgnore(); |
| 7669 | |
| 7670 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 7671 | unsigned SizeInRegs = (getContext().getTypeSize(Ty) + 31) / 32; |
| 7672 | if (SizeInRegs <= State.FreeRegs) { |
| 7673 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 7674 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 7675 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 7676 | State.FreeRegs -= SizeInRegs; |
| 7677 | return ABIArgInfo::getDirectInReg(Result); |
| 7678 | } else { |
| 7679 | State.FreeRegs = 0; |
| 7680 | } |
| 7681 | return getIndirectResult(Ty, true, State); |
| 7682 | } |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7683 | |
| 7684 | // Treat an enum type as its underlying type. |
| 7685 | if (const auto *EnumTy = Ty->getAs<EnumType>()) |
| 7686 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 7687 | |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7688 | bool InReg = shouldUseInReg(Ty, State); |
| 7689 | if (Ty->isPromotableIntegerType()) { |
| 7690 | if (InReg) |
| 7691 | return ABIArgInfo::getDirectInReg(); |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 7692 | return ABIArgInfo::getExtend(Ty); |
Jacques Pienaar | e74d913 | 2016-04-26 00:09:29 +0000 | [diff] [blame] | 7693 | } |
| 7694 | if (InReg) |
| 7695 | return ABIArgInfo::getDirectInReg(); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 7696 | return ABIArgInfo::getDirect(); |
| 7697 | } |
| 7698 | |
| 7699 | namespace { |
| 7700 | class LanaiTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7701 | public: |
| 7702 | LanaiTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 7703 | : TargetCodeGenInfo(new LanaiABIInfo(CGT)) {} |
| 7704 | }; |
| 7705 | } |
| 7706 | |
| 7707 | //===----------------------------------------------------------------------===// |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7708 | // AMDGPU ABI Implementation |
| 7709 | //===----------------------------------------------------------------------===// |
| 7710 | |
| 7711 | namespace { |
| 7712 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7713 | class AMDGPUABIInfo final : public DefaultABIInfo { |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7714 | private: |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7715 | static const unsigned MaxNumRegsForArgsRet = 16; |
| 7716 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7717 | unsigned numRegsForType(QualType Ty) const; |
| 7718 | |
| 7719 | bool isHomogeneousAggregateBaseType(QualType Ty) const override; |
| 7720 | bool isHomogeneousAggregateSmallEnough(const Type *Base, |
| 7721 | uint64_t Members) const override; |
| 7722 | |
Michael Liao | 15140e4 | 2019-11-04 11:41:07 -0500 | [diff] [blame] | 7723 | // Coerce HIP pointer arguments from generic pointers to global ones. |
| 7724 | llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS, |
| 7725 | unsigned ToAS) const { |
| 7726 | // Structure types. |
| 7727 | if (auto STy = dyn_cast<llvm::StructType>(Ty)) { |
| 7728 | SmallVector<llvm::Type *, 8> EltTys; |
| 7729 | bool Changed = false; |
| 7730 | for (auto T : STy->elements()) { |
| 7731 | auto NT = coerceKernelArgumentType(T, FromAS, ToAS); |
| 7732 | EltTys.push_back(NT); |
| 7733 | Changed |= (NT != T); |
| 7734 | } |
| 7735 | // Skip if there is no change in element types. |
| 7736 | if (!Changed) |
| 7737 | return STy; |
| 7738 | if (STy->hasName()) |
| 7739 | return llvm::StructType::create( |
| 7740 | EltTys, (STy->getName() + ".coerce").str(), STy->isPacked()); |
| 7741 | return llvm::StructType::get(getVMContext(), EltTys, STy->isPacked()); |
| 7742 | } |
| 7743 | // Arrary types. |
| 7744 | if (auto ATy = dyn_cast<llvm::ArrayType>(Ty)) { |
| 7745 | auto T = ATy->getElementType(); |
| 7746 | auto NT = coerceKernelArgumentType(T, FromAS, ToAS); |
| 7747 | // Skip if there is no change in that element type. |
| 7748 | if (NT == T) |
| 7749 | return ATy; |
| 7750 | return llvm::ArrayType::get(NT, ATy->getNumElements()); |
| 7751 | } |
| 7752 | // Single value types. |
| 7753 | if (Ty->isPointerTy() && Ty->getPointerAddressSpace() == FromAS) |
| 7754 | return llvm::PointerType::get( |
| 7755 | cast<llvm::PointerType>(Ty)->getElementType(), ToAS); |
| 7756 | return Ty; |
| 7757 | } |
| 7758 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7759 | public: |
| 7760 | explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) : |
| 7761 | DefaultABIInfo(CGT) {} |
| 7762 | |
| 7763 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 7764 | ABIArgInfo classifyKernelArgumentType(QualType Ty) const; |
| 7765 | ABIArgInfo classifyArgumentType(QualType Ty, unsigned &NumRegsLeft) const; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7766 | |
| 7767 | void computeInfo(CGFunctionInfo &FI) const override; |
Michael Liao | 5a48678 | 2019-10-24 11:41:11 -0400 | [diff] [blame] | 7768 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7769 | QualType Ty) const override; |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7770 | }; |
| 7771 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7772 | bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const { |
| 7773 | return true; |
| 7774 | } |
| 7775 | |
| 7776 | bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough( |
| 7777 | const Type *Base, uint64_t Members) const { |
| 7778 | uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32; |
| 7779 | |
| 7780 | // Homogeneous Aggregates may occupy at most 16 registers. |
| 7781 | return Members * NumRegs <= MaxNumRegsForArgsRet; |
| 7782 | } |
| 7783 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7784 | /// Estimate number of registers the type will use when passed in registers. |
| 7785 | unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const { |
| 7786 | unsigned NumRegs = 0; |
| 7787 | |
| 7788 | if (const VectorType *VT = Ty->getAs<VectorType>()) { |
| 7789 | // Compute from the number of elements. The reported size is based on the |
| 7790 | // in-memory size, which includes the padding 4th element for 3-vectors. |
| 7791 | QualType EltTy = VT->getElementType(); |
| 7792 | unsigned EltSize = getContext().getTypeSize(EltTy); |
| 7793 | |
| 7794 | // 16-bit element vectors should be passed as packed. |
| 7795 | if (EltSize == 16) |
| 7796 | return (VT->getNumElements() + 1) / 2; |
| 7797 | |
| 7798 | unsigned EltNumRegs = (EltSize + 31) / 32; |
| 7799 | return EltNumRegs * VT->getNumElements(); |
| 7800 | } |
| 7801 | |
| 7802 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7803 | const RecordDecl *RD = RT->getDecl(); |
| 7804 | assert(!RD->hasFlexibleArrayMember()); |
| 7805 | |
| 7806 | for (const FieldDecl *Field : RD->fields()) { |
| 7807 | QualType FieldTy = Field->getType(); |
| 7808 | NumRegs += numRegsForType(FieldTy); |
| 7809 | } |
| 7810 | |
| 7811 | return NumRegs; |
| 7812 | } |
| 7813 | |
| 7814 | return (getContext().getTypeSize(Ty) + 31) / 32; |
| 7815 | } |
| 7816 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7817 | void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const { |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7818 | llvm::CallingConv::ID CC = FI.getCallingConvention(); |
| 7819 | |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7820 | if (!getCXXABI().classifyReturnType(FI)) |
| 7821 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 7822 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7823 | unsigned NumRegsLeft = MaxNumRegsForArgsRet; |
| 7824 | for (auto &Arg : FI.arguments()) { |
| 7825 | if (CC == llvm::CallingConv::AMDGPU_KERNEL) { |
| 7826 | Arg.info = classifyKernelArgumentType(Arg.type); |
| 7827 | } else { |
| 7828 | Arg.info = classifyArgumentType(Arg.type, NumRegsLeft); |
| 7829 | } |
| 7830 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7831 | } |
| 7832 | |
Michael Liao | 5a48678 | 2019-10-24 11:41:11 -0400 | [diff] [blame] | 7833 | Address AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 7834 | QualType Ty) const { |
| 7835 | llvm_unreachable("AMDGPU does not support varargs"); |
| 7836 | } |
| 7837 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7838 | ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const { |
| 7839 | if (isAggregateTypeForABI(RetTy)) { |
| 7840 | // Records with non-trivial destructors/copy-constructors should not be |
| 7841 | // returned by value. |
| 7842 | if (!getRecordArgABI(RetTy, getCXXABI())) { |
| 7843 | // Ignore empty structs/unions. |
| 7844 | if (isEmptyRecord(getContext(), RetTy, true)) |
| 7845 | return ABIArgInfo::getIgnore(); |
| 7846 | |
| 7847 | // Lower single-element structs to just return a regular value. |
| 7848 | if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext())) |
| 7849 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7850 | |
| 7851 | if (const RecordType *RT = RetTy->getAs<RecordType>()) { |
| 7852 | const RecordDecl *RD = RT->getDecl(); |
| 7853 | if (RD->hasFlexibleArrayMember()) |
| 7854 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7855 | } |
| 7856 | |
| 7857 | // Pack aggregates <= 4 bytes into single VGPR or pair. |
| 7858 | uint64_t Size = getContext().getTypeSize(RetTy); |
| 7859 | if (Size <= 16) |
| 7860 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7861 | |
| 7862 | if (Size <= 32) |
| 7863 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7864 | |
| 7865 | if (Size <= 64) { |
| 7866 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7867 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7868 | } |
| 7869 | |
| 7870 | if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet) |
| 7871 | return ABIArgInfo::getDirect(); |
| 7872 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7873 | } |
| 7874 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7875 | // Otherwise just do the default thing. |
| 7876 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 7877 | } |
| 7878 | |
| 7879 | /// For kernels all parameters are really passed in a special buffer. It doesn't |
| 7880 | /// make sense to pass anything byval, so everything must be direct. |
| 7881 | ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const { |
| 7882 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7883 | |
| 7884 | // TODO: Can we omit empty structs? |
| 7885 | |
Michael Liao | 15140e4 | 2019-11-04 11:41:07 -0500 | [diff] [blame] | 7886 | llvm::Type *LTy = nullptr; |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7887 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
Michael Liao | 15140e4 | 2019-11-04 11:41:07 -0500 | [diff] [blame] | 7888 | LTy = CGT.ConvertType(QualType(SeltTy, 0)); |
| 7889 | |
| 7890 | if (getContext().getLangOpts().HIP) { |
| 7891 | if (!LTy) |
| 7892 | LTy = CGT.ConvertType(Ty); |
| 7893 | LTy = coerceKernelArgumentType( |
| 7894 | LTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default), |
| 7895 | /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device)); |
| 7896 | } |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7897 | |
| 7898 | // If we set CanBeFlattened to true, CodeGen will expand the struct to its |
| 7899 | // individual elements, which confuses the Clover OpenCL backend; therefore we |
| 7900 | // have to set it to false here. Other args of getDirect() are just defaults. |
Michael Liao | 15140e4 | 2019-11-04 11:41:07 -0500 | [diff] [blame] | 7901 | return ABIArgInfo::getDirect(LTy, 0, nullptr, false); |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7902 | } |
| 7903 | |
Matt Arsenault | 3fe7395 | 2017-08-09 21:44:58 +0000 | [diff] [blame] | 7904 | ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, |
| 7905 | unsigned &NumRegsLeft) const { |
| 7906 | assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow"); |
| 7907 | |
| 7908 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 7909 | |
| 7910 | if (isAggregateTypeForABI(Ty)) { |
| 7911 | // Records with non-trivial destructors/copy-constructors should not be |
| 7912 | // passed by value. |
| 7913 | if (auto RAA = getRecordArgABI(Ty, getCXXABI())) |
| 7914 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
| 7915 | |
| 7916 | // Ignore empty structs/unions. |
| 7917 | if (isEmptyRecord(getContext(), Ty, true)) |
| 7918 | return ABIArgInfo::getIgnore(); |
| 7919 | |
| 7920 | // Lower single-element structs to just pass a regular value. TODO: We |
| 7921 | // could do reasonable-size multiple-element structs too, using getExpand(), |
| 7922 | // though watch out for things like bitfields. |
| 7923 | if (const Type *SeltTy = isSingleElementStruct(Ty, getContext())) |
| 7924 | return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0))); |
| 7925 | |
| 7926 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 7927 | const RecordDecl *RD = RT->getDecl(); |
| 7928 | if (RD->hasFlexibleArrayMember()) |
| 7929 | return DefaultABIInfo::classifyArgumentType(Ty); |
| 7930 | } |
| 7931 | |
| 7932 | // Pack aggregates <= 8 bytes into single VGPR or pair. |
| 7933 | uint64_t Size = getContext().getTypeSize(Ty); |
| 7934 | if (Size <= 64) { |
| 7935 | unsigned NumRegs = (Size + 31) / 32; |
| 7936 | NumRegsLeft -= std::min(NumRegsLeft, NumRegs); |
| 7937 | |
| 7938 | if (Size <= 16) |
| 7939 | return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext())); |
| 7940 | |
| 7941 | if (Size <= 32) |
| 7942 | return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext())); |
| 7943 | |
| 7944 | // XXX: Should this be i64 instead, and should the limit increase? |
| 7945 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext()); |
| 7946 | return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2)); |
| 7947 | } |
| 7948 | |
| 7949 | if (NumRegsLeft > 0) { |
| 7950 | unsigned NumRegs = numRegsForType(Ty); |
| 7951 | if (NumRegsLeft >= NumRegs) { |
| 7952 | NumRegsLeft -= NumRegs; |
| 7953 | return ABIArgInfo::getDirect(); |
| 7954 | } |
| 7955 | } |
| 7956 | } |
| 7957 | |
| 7958 | // Otherwise just do the default thing. |
| 7959 | ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty); |
| 7960 | if (!ArgInfo.isIndirect()) { |
| 7961 | unsigned NumRegs = numRegsForType(Ty); |
| 7962 | NumRegsLeft -= std::min(NumRegs, NumRegsLeft); |
| 7963 | } |
| 7964 | |
| 7965 | return ArgInfo; |
| 7966 | } |
| 7967 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7968 | class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo { |
| 7969 | public: |
| 7970 | AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT) |
Matt Arsenault | 88d7da0 | 2016-08-22 19:25:59 +0000 | [diff] [blame] | 7971 | : TargetCodeGenInfo(new AMDGPUABIInfo(CGT)) {} |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 7972 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 7973 | CodeGen::CodeGenModule &M) const override; |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 7974 | unsigned getOpenCLKernelCallingConv() const override; |
Nico Weber | 7849eeb | 2016-12-14 21:38:18 +0000 | [diff] [blame] | 7975 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7976 | llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM, |
| 7977 | llvm::PointerType *T, QualType QT) const override; |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7978 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7979 | LangAS getASTAllocaAddressSpace() const override { |
| 7980 | return getLangASFromTargetAS( |
| 7981 | getABIInfo().getDataLayout().getAllocaAddrSpace()); |
Yaxun Liu | 6d96f163 | 2017-05-18 18:51:09 +0000 | [diff] [blame] | 7982 | } |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 7983 | LangAS getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 7984 | const VarDecl *D) const override; |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 7985 | llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 7986 | SyncScope Scope, |
| 7987 | llvm::AtomicOrdering Ordering, |
| 7988 | llvm::LLVMContext &Ctx) const override; |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 7989 | llvm::Function * |
| 7990 | createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 7991 | llvm::Function *BlockInvokeFunc, |
| 7992 | llvm::Value *BlockLiteral) const override; |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 7993 | bool shouldEmitStaticExternCAliases() const override; |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 7994 | void setCUDAKernelCallingConvention(const FunctionType *&FT) const override; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 7995 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 7996 | } |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 7997 | |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 7998 | static bool requiresAMDGPUProtectedVisibility(const Decl *D, |
| 7999 | llvm::GlobalValue *GV) { |
| 8000 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 8001 | return false; |
| 8002 | |
| 8003 | return D->hasAttr<OpenCLKernelAttr>() || |
| 8004 | (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) || |
Michael Liao | 3820506 | 2019-04-26 19:31:48 +0000 | [diff] [blame] | 8005 | (isa<VarDecl>(D) && |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 8006 | (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() || |
| 8007 | D->hasAttr<HIPPinnedShadowAttr>())); |
| 8008 | } |
| 8009 | |
| 8010 | static bool requiresAMDGPUDefaultVisibility(const Decl *D, |
| 8011 | llvm::GlobalValue *GV) { |
| 8012 | if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility) |
| 8013 | return false; |
| 8014 | |
| 8015 | return isa<VarDecl>(D) && D->hasAttr<HIPPinnedShadowAttr>(); |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 8016 | } |
| 8017 | |
Eric Christopher | 162c91c | 2015-06-05 22:03:00 +0000 | [diff] [blame] | 8018 | void AMDGPUTargetCodeGenInfo::setTargetAttributes( |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 8019 | const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const { |
Yaxun Liu | c3dfe90 | 2019-06-26 03:47:37 +0000 | [diff] [blame] | 8020 | if (requiresAMDGPUDefaultVisibility(D, GV)) { |
| 8021 | GV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
| 8022 | GV->setDSOLocal(false); |
| 8023 | } else if (requiresAMDGPUProtectedVisibility(D, GV)) { |
Scott Linder | 80a1ee4 | 2019-02-12 18:30:38 +0000 | [diff] [blame] | 8024 | GV->setVisibility(llvm::GlobalValue::ProtectedVisibility); |
| 8025 | GV->setDSOLocal(true); |
| 8026 | } |
| 8027 | |
Rafael Espindola | deb10be | 2018-02-07 19:04:41 +0000 | [diff] [blame] | 8028 | if (GV->isDeclaration()) |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 8029 | return; |
Akira Hatanaka | aec6b2c | 2015-10-08 20:26:34 +0000 | [diff] [blame] | 8030 | const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8031 | if (!FD) |
| 8032 | return; |
| 8033 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8034 | llvm::Function *F = cast<llvm::Function>(GV); |
| 8035 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 8036 | const auto *ReqdWGS = M.getLangOpts().OpenCL ? |
| 8037 | FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr; |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 8038 | |
Matt Arsenault | eac783a | 2019-08-27 19:25:40 +0000 | [diff] [blame] | 8039 | |
| 8040 | const bool IsOpenCLKernel = M.getLangOpts().OpenCL && |
| 8041 | FD->hasAttr<OpenCLKernelAttr>(); |
Yaxun Liu | 1bea97c | 2019-09-03 18:50:24 +0000 | [diff] [blame] | 8042 | const bool IsHIPKernel = M.getLangOpts().HIP && |
| 8043 | FD->hasAttr<CUDAGlobalAttr>(); |
| 8044 | if ((IsOpenCLKernel || IsHIPKernel) && |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 8045 | (M.getTriple().getOS() == llvm::Triple::AMDHSA)) |
Christudasan Devadasan | 18ba9d6 | 2019-07-10 15:10:08 +0000 | [diff] [blame] | 8046 | F->addFnAttr("amdgpu-implicitarg-num-bytes", "56"); |
Tony Tye | 1a3f3a2 | 2018-03-23 18:43:15 +0000 | [diff] [blame] | 8047 | |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 8048 | const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>(); |
| 8049 | if (ReqdWGS || FlatWGS) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 8050 | unsigned Min = 0; |
| 8051 | unsigned Max = 0; |
| 8052 | if (FlatWGS) { |
| 8053 | Min = FlatWGS->getMin() |
| 8054 | ->EvaluateKnownConstInt(M.getContext()) |
| 8055 | .getExtValue(); |
| 8056 | Max = FlatWGS->getMax() |
| 8057 | ->EvaluateKnownConstInt(M.getContext()) |
| 8058 | .getExtValue(); |
| 8059 | } |
Stanislav Mekhanoshin | 921a423 | 2017-04-06 18:15:44 +0000 | [diff] [blame] | 8060 | if (ReqdWGS && Min == 0 && Max == 0) |
| 8061 | Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8062 | |
| 8063 | if (Min != 0) { |
| 8064 | assert(Min <= Max && "Min must be less than or equal Max"); |
| 8065 | |
| 8066 | std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max); |
| 8067 | F->addFnAttr("amdgpu-flat-work-group-size", AttrVal); |
| 8068 | } else |
| 8069 | assert(Max == 0 && "Max must be zero"); |
Yaxun Liu | 1bea97c | 2019-09-03 18:50:24 +0000 | [diff] [blame] | 8070 | } else if (IsOpenCLKernel || IsHIPKernel) { |
Matt Arsenault | eac783a | 2019-08-27 19:25:40 +0000 | [diff] [blame] | 8071 | // By default, restrict the maximum size to 256. |
| 8072 | F->addFnAttr("amdgpu-flat-work-group-size", "1,256"); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8073 | } |
| 8074 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8075 | if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) { |
Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 8076 | unsigned Min = |
| 8077 | Attr->getMin()->EvaluateKnownConstInt(M.getContext()).getExtValue(); |
| 8078 | unsigned Max = Attr->getMax() ? Attr->getMax() |
| 8079 | ->EvaluateKnownConstInt(M.getContext()) |
| 8080 | .getExtValue() |
| 8081 | : 0; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8082 | |
| 8083 | if (Min != 0) { |
| 8084 | assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max"); |
| 8085 | |
| 8086 | std::string AttrVal = llvm::utostr(Min); |
| 8087 | if (Max != 0) |
| 8088 | AttrVal = AttrVal + "," + llvm::utostr(Max); |
| 8089 | F->addFnAttr("amdgpu-waves-per-eu", AttrVal); |
| 8090 | } else |
| 8091 | assert(Max == 0 && "Max must be zero"); |
| 8092 | } |
| 8093 | |
| 8094 | if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) { |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8095 | unsigned NumSGPR = Attr->getNumSGPR(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8096 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8097 | if (NumSGPR != 0) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 8098 | F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR)); |
| 8099 | } |
| 8100 | |
| 8101 | if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) { |
| 8102 | uint32_t NumVGPR = Attr->getNumVGPR(); |
| 8103 | |
| 8104 | if (NumVGPR != 0) |
| 8105 | F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 8106 | } |
Yaxun Liu | f2e8ab2 | 2016-07-19 19:39:45 +0000 | [diff] [blame] | 8107 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 8108 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8109 | unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8110 | return llvm::CallingConv::AMDGPU_KERNEL; |
| 8111 | } |
| 8112 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 8113 | // Currently LLVM assumes null pointers always have value 0, |
| 8114 | // which results in incorrectly transformed IR. Therefore, instead of |
| 8115 | // emitting null pointers in private and local address spaces, a null |
| 8116 | // pointer in generic address space is emitted which is casted to a |
| 8117 | // pointer in local or private address space. |
| 8118 | llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer( |
| 8119 | const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT, |
| 8120 | QualType QT) const { |
| 8121 | if (CGM.getContext().getTargetNullPointerValue(QT) == 0) |
| 8122 | return llvm::ConstantPointerNull::get(PT); |
| 8123 | |
| 8124 | auto &Ctx = CGM.getContext(); |
| 8125 | auto NPT = llvm::PointerType::get(PT->getElementType(), |
| 8126 | Ctx.getTargetAddressSpace(LangAS::opencl_generic)); |
| 8127 | return llvm::ConstantExpr::getAddrSpaceCast( |
| 8128 | llvm::ConstantPointerNull::get(NPT), PT); |
| 8129 | } |
| 8130 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8131 | LangAS |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8132 | AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, |
| 8133 | const VarDecl *D) const { |
| 8134 | assert(!CGM.getLangOpts().OpenCL && |
| 8135 | !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) && |
| 8136 | "Address space agnostic languages only"); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8137 | LangAS DefaultGlobalAS = getLangASFromTargetAS( |
| 8138 | CGM.getContext().getTargetAddressSpace(LangAS::opencl_global)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8139 | if (!D) |
| 8140 | return DefaultGlobalAS; |
| 8141 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 8142 | LangAS AddrSpace = D->getType().getAddressSpace(); |
| 8143 | assert(AddrSpace == LangAS::Default || isTargetAddressSpace(AddrSpace)); |
Yaxun Liu | cbf647c | 2017-07-08 13:24:52 +0000 | [diff] [blame] | 8144 | if (AddrSpace != LangAS::Default) |
| 8145 | return AddrSpace; |
| 8146 | |
| 8147 | if (CGM.isTypeConstant(D->getType(), false)) { |
| 8148 | if (auto ConstAS = CGM.getTarget().getConstantAddressSpace()) |
| 8149 | return ConstAS.getValue(); |
| 8150 | } |
| 8151 | return DefaultGlobalAS; |
| 8152 | } |
| 8153 | |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8154 | llvm::SyncScope::ID |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 8155 | AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, |
| 8156 | SyncScope Scope, |
| 8157 | llvm::AtomicOrdering Ordering, |
| 8158 | llvm::LLVMContext &Ctx) const { |
| 8159 | std::string Name; |
| 8160 | switch (Scope) { |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8161 | case SyncScope::OpenCLWorkGroup: |
| 8162 | Name = "workgroup"; |
| 8163 | break; |
| 8164 | case SyncScope::OpenCLDevice: |
| 8165 | Name = "agent"; |
| 8166 | break; |
| 8167 | case SyncScope::OpenCLAllSVMDevices: |
| 8168 | Name = ""; |
| 8169 | break; |
| 8170 | case SyncScope::OpenCLSubGroup: |
Konstantin Zhuravlyov | 3161c89 | 2019-03-06 20:54:48 +0000 | [diff] [blame] | 8171 | Name = "wavefront"; |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8172 | } |
Konstantin Zhuravlyov | ec28a1d | 2019-03-25 20:54:00 +0000 | [diff] [blame] | 8173 | |
| 8174 | if (Ordering != llvm::AtomicOrdering::SequentiallyConsistent) { |
| 8175 | if (!Name.empty()) |
| 8176 | Name = Twine(Twine(Name) + Twine("-")).str(); |
| 8177 | |
| 8178 | Name = Twine(Twine(Name) + Twine("one-as")).str(); |
| 8179 | } |
| 8180 | |
| 8181 | return Ctx.getOrInsertSyncScopeID(Name); |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 8182 | } |
| 8183 | |
Yaxun Liu | b0eee29 | 2018-03-29 14:50:00 +0000 | [diff] [blame] | 8184 | bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const { |
| 8185 | return false; |
| 8186 | } |
| 8187 | |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 8188 | void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention( |
Yaxun Liu | 6c10a66 | 2018-06-12 00:16:33 +0000 | [diff] [blame] | 8189 | const FunctionType *&FT) const { |
| 8190 | FT = getABIInfo().getContext().adjustFunctionType( |
| 8191 | FT, FT->getExtInfo().withCallingConv(CC_OpenCLKernel)); |
Yaxun Liu | 4306f20 | 2018-04-20 17:01:03 +0000 | [diff] [blame] | 8192 | } |
| 8193 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8194 | //===----------------------------------------------------------------------===// |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 8195 | // SPARC v8 ABI Implementation. |
| 8196 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8197 | // |
| 8198 | // Ensures that complex values are passed in registers. |
| 8199 | // |
| 8200 | namespace { |
| 8201 | class SparcV8ABIInfo : public DefaultABIInfo { |
| 8202 | public: |
| 8203 | SparcV8ABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
| 8204 | |
| 8205 | private: |
| 8206 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8207 | void computeInfo(CGFunctionInfo &FI) const override; |
| 8208 | }; |
| 8209 | } // end anonymous namespace |
| 8210 | |
| 8211 | |
| 8212 | ABIArgInfo |
| 8213 | SparcV8ABIInfo::classifyReturnType(QualType Ty) const { |
| 8214 | if (Ty->isAnyComplexType()) { |
| 8215 | return ABIArgInfo::getDirect(); |
| 8216 | } |
| 8217 | else { |
| 8218 | return DefaultABIInfo::classifyReturnType(Ty); |
| 8219 | } |
| 8220 | } |
| 8221 | |
| 8222 | void SparcV8ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8223 | |
| 8224 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8225 | for (auto &Arg : FI.arguments()) |
| 8226 | Arg.info = classifyArgumentType(Arg.type); |
| 8227 | } |
| 8228 | |
| 8229 | namespace { |
| 8230 | class SparcV8TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8231 | public: |
| 8232 | SparcV8TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8233 | : TargetCodeGenInfo(new SparcV8ABIInfo(CGT)) {} |
| 8234 | }; |
| 8235 | } // end anonymous namespace |
| 8236 | |
| 8237 | //===----------------------------------------------------------------------===// |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8238 | // SPARC v9 ABI Implementation. |
| 8239 | // Based on the SPARC Compliance Definition version 2.4.1. |
| 8240 | // |
| 8241 | // Function arguments a mapped to a nominal "parameter array" and promoted to |
| 8242 | // registers depending on their type. Each argument occupies 8 or 16 bytes in |
| 8243 | // the array, structs larger than 16 bytes are passed indirectly. |
| 8244 | // |
| 8245 | // One case requires special care: |
| 8246 | // |
| 8247 | // struct mixed { |
| 8248 | // int i; |
| 8249 | // float f; |
| 8250 | // }; |
| 8251 | // |
| 8252 | // When a struct mixed is passed by value, it only occupies 8 bytes in the |
| 8253 | // parameter array, but the int is passed in an integer register, and the float |
| 8254 | // is passed in a floating point register. This is represented as two arguments |
| 8255 | // with the LLVM IR inreg attribute: |
| 8256 | // |
| 8257 | // declare void f(i32 inreg %i, float inreg %f) |
| 8258 | // |
| 8259 | // The code generator will only allocate 4 bytes from the parameter array for |
| 8260 | // the inreg arguments. All other arguments are allocated a multiple of 8 |
| 8261 | // bytes. |
| 8262 | // |
| 8263 | namespace { |
| 8264 | class SparcV9ABIInfo : public ABIInfo { |
| 8265 | public: |
| 8266 | SparcV9ABIInfo(CodeGenTypes &CGT) : ABIInfo(CGT) {} |
| 8267 | |
| 8268 | private: |
| 8269 | ABIArgInfo classifyType(QualType RetTy, unsigned SizeLimit) const; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8270 | void computeInfo(CGFunctionInfo &FI) const override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8271 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8272 | QualType Ty) const override; |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8273 | |
| 8274 | // Coercion type builder for structs passed in registers. The coercion type |
| 8275 | // serves two purposes: |
| 8276 | // |
| 8277 | // 1. Pad structs to a multiple of 64 bits, so they are passed 'left-aligned' |
| 8278 | // in registers. |
| 8279 | // 2. Expose aligned floating point elements as first-level elements, so the |
| 8280 | // code generator knows to pass them in floating point registers. |
| 8281 | // |
| 8282 | // We also compute the InReg flag which indicates that the struct contains |
| 8283 | // aligned 32-bit floats. |
| 8284 | // |
| 8285 | struct CoerceBuilder { |
| 8286 | llvm::LLVMContext &Context; |
| 8287 | const llvm::DataLayout &DL; |
| 8288 | SmallVector<llvm::Type*, 8> Elems; |
| 8289 | uint64_t Size; |
| 8290 | bool InReg; |
| 8291 | |
| 8292 | CoerceBuilder(llvm::LLVMContext &c, const llvm::DataLayout &dl) |
| 8293 | : Context(c), DL(dl), Size(0), InReg(false) {} |
| 8294 | |
| 8295 | // Pad Elems with integers until Size is ToSize. |
| 8296 | void pad(uint64_t ToSize) { |
| 8297 | assert(ToSize >= Size && "Cannot remove elements"); |
| 8298 | if (ToSize == Size) |
| 8299 | return; |
| 8300 | |
| 8301 | // Finish the current 64-bit word. |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8302 | uint64_t Aligned = llvm::alignTo(Size, 64); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8303 | if (Aligned > Size && Aligned <= ToSize) { |
| 8304 | Elems.push_back(llvm::IntegerType::get(Context, Aligned - Size)); |
| 8305 | Size = Aligned; |
| 8306 | } |
| 8307 | |
| 8308 | // Add whole 64-bit words. |
| 8309 | while (Size + 64 <= ToSize) { |
| 8310 | Elems.push_back(llvm::Type::getInt64Ty(Context)); |
| 8311 | Size += 64; |
| 8312 | } |
| 8313 | |
| 8314 | // Final in-word padding. |
| 8315 | if (Size < ToSize) { |
| 8316 | Elems.push_back(llvm::IntegerType::get(Context, ToSize - Size)); |
| 8317 | Size = ToSize; |
| 8318 | } |
| 8319 | } |
| 8320 | |
| 8321 | // Add a floating point element at Offset. |
| 8322 | void addFloat(uint64_t Offset, llvm::Type *Ty, unsigned Bits) { |
| 8323 | // Unaligned floats are treated as integers. |
| 8324 | if (Offset % Bits) |
| 8325 | return; |
| 8326 | // The InReg flag is only required if there are any floats < 64 bits. |
| 8327 | if (Bits < 64) |
| 8328 | InReg = true; |
| 8329 | pad(Offset); |
| 8330 | Elems.push_back(Ty); |
| 8331 | Size = Offset + Bits; |
| 8332 | } |
| 8333 | |
| 8334 | // Add a struct type to the coercion type, starting at Offset (in bits). |
| 8335 | void addStruct(uint64_t Offset, llvm::StructType *StrTy) { |
| 8336 | const llvm::StructLayout *Layout = DL.getStructLayout(StrTy); |
| 8337 | for (unsigned i = 0, e = StrTy->getNumElements(); i != e; ++i) { |
| 8338 | llvm::Type *ElemTy = StrTy->getElementType(i); |
| 8339 | uint64_t ElemOffset = Offset + Layout->getElementOffsetInBits(i); |
| 8340 | switch (ElemTy->getTypeID()) { |
| 8341 | case llvm::Type::StructTyID: |
| 8342 | addStruct(ElemOffset, cast<llvm::StructType>(ElemTy)); |
| 8343 | break; |
| 8344 | case llvm::Type::FloatTyID: |
| 8345 | addFloat(ElemOffset, ElemTy, 32); |
| 8346 | break; |
| 8347 | case llvm::Type::DoubleTyID: |
| 8348 | addFloat(ElemOffset, ElemTy, 64); |
| 8349 | break; |
| 8350 | case llvm::Type::FP128TyID: |
| 8351 | addFloat(ElemOffset, ElemTy, 128); |
| 8352 | break; |
| 8353 | case llvm::Type::PointerTyID: |
| 8354 | if (ElemOffset % 64 == 0) { |
| 8355 | pad(ElemOffset); |
| 8356 | Elems.push_back(ElemTy); |
| 8357 | Size += 64; |
| 8358 | } |
| 8359 | break; |
| 8360 | default: |
| 8361 | break; |
| 8362 | } |
| 8363 | } |
| 8364 | } |
| 8365 | |
| 8366 | // Check if Ty is a usable substitute for the coercion type. |
| 8367 | bool isUsableType(llvm::StructType *Ty) const { |
Benjamin Kramer | 39ccabe | 2015-03-02 11:57:06 +0000 | [diff] [blame] | 8368 | return llvm::makeArrayRef(Elems) == Ty->elements(); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8369 | } |
| 8370 | |
| 8371 | // Get the coercion type as a literal struct type. |
| 8372 | llvm::Type *getType() const { |
| 8373 | if (Elems.size() == 1) |
| 8374 | return Elems.front(); |
| 8375 | else |
| 8376 | return llvm::StructType::get(Context, Elems); |
| 8377 | } |
| 8378 | }; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8379 | }; |
| 8380 | } // end anonymous namespace |
| 8381 | |
| 8382 | ABIArgInfo |
| 8383 | SparcV9ABIInfo::classifyType(QualType Ty, unsigned SizeLimit) const { |
| 8384 | if (Ty->isVoidType()) |
| 8385 | return ABIArgInfo::getIgnore(); |
| 8386 | |
| 8387 | uint64_t Size = getContext().getTypeSize(Ty); |
| 8388 | |
| 8389 | // Anything too big to fit in registers is passed with an explicit indirect |
| 8390 | // pointer / sret pointer. |
| 8391 | if (Size > SizeLimit) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8392 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8393 | |
| 8394 | // Treat an enum type as its underlying type. |
| 8395 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8396 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8397 | |
| 8398 | // Integer types smaller than a register are extended. |
| 8399 | if (Size < 64 && Ty->isIntegerType()) |
Alex Bradbury | e41a5e2 | 2018-01-12 20:08:16 +0000 | [diff] [blame] | 8400 | return ABIArgInfo::getExtend(Ty); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8401 | |
| 8402 | // Other non-aggregates go in registers. |
| 8403 | if (!isAggregateTypeForABI(Ty)) |
| 8404 | return ABIArgInfo::getDirect(); |
| 8405 | |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8406 | // If a C++ object has either a non-trivial copy constructor or a non-trivial |
| 8407 | // destructor, it is passed with an explicit indirect pointer / sret pointer. |
| 8408 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8409 | return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory); |
Jakob Stoklund Olesen | b81eb3e | 2014-01-12 06:54:56 +0000 | [diff] [blame] | 8410 | |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8411 | // 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] | 8412 | // Build a coercion type from the LLVM struct type. |
| 8413 | llvm::StructType *StrTy = dyn_cast<llvm::StructType>(CGT.ConvertType(Ty)); |
| 8414 | if (!StrTy) |
| 8415 | return ABIArgInfo::getDirect(); |
| 8416 | |
| 8417 | CoerceBuilder CB(getVMContext(), getDataLayout()); |
| 8418 | CB.addStruct(0, StrTy); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8419 | CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64)); |
Jakob Stoklund Olesen | 02dc6a1 | 2013-05-28 04:57:37 +0000 | [diff] [blame] | 8420 | |
| 8421 | // Try to use the original type for coercion. |
| 8422 | llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType(); |
| 8423 | |
| 8424 | if (CB.InReg) |
| 8425 | return ABIArgInfo::getDirectInReg(CoerceTy); |
| 8426 | else |
| 8427 | return ABIArgInfo::getDirect(CoerceTy); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8428 | } |
| 8429 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8430 | Address SparcV9ABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8431 | QualType Ty) const { |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8432 | ABIArgInfo AI = classifyType(Ty, 16 * 8); |
| 8433 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8434 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8435 | AI.setCoerceToType(ArgTy); |
| 8436 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8437 | CharUnits SlotSize = CharUnits::fromQuantity(8); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8438 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8439 | CGBuilderTy &Builder = CGF.Builder; |
| 8440 | Address Addr(Builder.CreateLoad(VAListAddr, "ap.cur"), SlotSize); |
| 8441 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
| 8442 | |
| 8443 | auto TypeInfo = getContext().getTypeInfoInChars(Ty); |
| 8444 | |
| 8445 | Address ArgAddr = Address::invalid(); |
| 8446 | CharUnits Stride; |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8447 | switch (AI.getKind()) { |
| 8448 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8449 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8450 | case ABIArgInfo::InAlloca: |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8451 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8452 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8453 | case ABIArgInfo::Extend: { |
| 8454 | Stride = SlotSize; |
| 8455 | CharUnits Offset = SlotSize - TypeInfo.first; |
| 8456 | ArgAddr = Builder.CreateConstInBoundsByteGEP(Addr, Offset, "extend"); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8457 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8458 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8459 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8460 | case ABIArgInfo::Direct: { |
| 8461 | auto AllocSize = getDataLayout().getTypeAllocSize(AI.getCoerceToType()); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8462 | Stride = CharUnits::fromQuantity(AllocSize).alignTo(SlotSize); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8463 | ArgAddr = Addr; |
| 8464 | break; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8465 | } |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8466 | |
| 8467 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8468 | Stride = SlotSize; |
| 8469 | ArgAddr = Builder.CreateElementBitCast(Addr, ArgPtrTy, "indirect"); |
| 8470 | ArgAddr = Address(Builder.CreateLoad(ArgAddr, "indirect.arg"), |
| 8471 | TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8472 | break; |
| 8473 | |
| 8474 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8475 | return Address(llvm::UndefValue::get(ArgPtrTy), TypeInfo.second); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8476 | } |
| 8477 | |
| 8478 | // Update VAList. |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8479 | Address NextPtr = Builder.CreateConstInBoundsByteGEP(Addr, Stride, "ap.next"); |
| 8480 | Builder.CreateStore(NextPtr.getPointer(), VAListAddr); |
Jakob Stoklund Olesen | 303caed | 2013-06-05 03:00:18 +0000 | [diff] [blame] | 8481 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8482 | return Builder.CreateBitCast(ArgAddr, ArgPtrTy, "arg.addr"); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
| 8485 | void SparcV9ABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 8486 | FI.getReturnInfo() = classifyType(FI.getReturnType(), 32 * 8); |
Aaron Ballman | ec47bc2 | 2014-03-17 18:10:01 +0000 | [diff] [blame] | 8487 | for (auto &I : FI.arguments()) |
| 8488 | I.info = classifyType(I.type, 16 * 8); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8489 | } |
| 8490 | |
| 8491 | namespace { |
| 8492 | class SparcV9TargetCodeGenInfo : public TargetCodeGenInfo { |
| 8493 | public: |
| 8494 | SparcV9TargetCodeGenInfo(CodeGenTypes &CGT) |
| 8495 | : TargetCodeGenInfo(new SparcV9ABIInfo(CGT)) {} |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8496 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8497 | int getDwarfEHStackPointer(CodeGen::CodeGenModule &M) const override { |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8498 | return 14; |
| 8499 | } |
| 8500 | |
| 8501 | bool initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 8502 | llvm::Value *Address) const override; |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8503 | }; |
| 8504 | } // end anonymous namespace |
| 8505 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8506 | bool |
| 8507 | SparcV9TargetCodeGenInfo::initDwarfEHRegSizeTable(CodeGen::CodeGenFunction &CGF, |
| 8508 | llvm::Value *Address) const { |
| 8509 | // This is calculated from the LLVM and GCC tables and verified |
| 8510 | // against gcc output. AFAIK all ABIs use the same encoding. |
| 8511 | |
| 8512 | CodeGen::CGBuilderTy &Builder = CGF.Builder; |
| 8513 | |
| 8514 | llvm::IntegerType *i8 = CGF.Int8Ty; |
| 8515 | llvm::Value *Four8 = llvm::ConstantInt::get(i8, 4); |
| 8516 | llvm::Value *Eight8 = llvm::ConstantInt::get(i8, 8); |
| 8517 | |
| 8518 | // 0-31: the 8-byte general-purpose registers |
| 8519 | AssignToArrayRange(Builder, Address, Eight8, 0, 31); |
| 8520 | |
| 8521 | // 32-63: f0-31, the 4-byte floating-point registers |
| 8522 | AssignToArrayRange(Builder, Address, Four8, 32, 63); |
| 8523 | |
| 8524 | // Y = 64 |
| 8525 | // PSR = 65 |
| 8526 | // WIM = 66 |
| 8527 | // TBR = 67 |
| 8528 | // PC = 68 |
| 8529 | // NPC = 69 |
| 8530 | // FSR = 70 |
| 8531 | // CSR = 71 |
| 8532 | AssignToArrayRange(Builder, Address, Eight8, 64, 71); |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8533 | |
Roman Divacky | f02c994 | 2014-02-24 18:46:27 +0000 | [diff] [blame] | 8534 | // 72-87: d0-15, the 8-byte floating-point registers |
| 8535 | AssignToArrayRange(Builder, Address, Eight8, 72, 87); |
| 8536 | |
| 8537 | return false; |
| 8538 | } |
| 8539 | |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8540 | // ARC ABI implementation. |
| 8541 | namespace { |
| 8542 | |
| 8543 | class ARCABIInfo : public DefaultABIInfo { |
| 8544 | public: |
| 8545 | using DefaultABIInfo::DefaultABIInfo; |
| 8546 | |
| 8547 | private: |
| 8548 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8549 | QualType Ty) const override; |
| 8550 | |
| 8551 | void updateState(const ABIArgInfo &Info, QualType Ty, CCState &State) const { |
| 8552 | if (!State.FreeRegs) |
| 8553 | return; |
| 8554 | if (Info.isIndirect() && Info.getInReg()) |
| 8555 | State.FreeRegs--; |
| 8556 | else if (Info.isDirect() && Info.getInReg()) { |
| 8557 | unsigned sz = (getContext().getTypeSize(Ty) + 31) / 32; |
| 8558 | if (sz < State.FreeRegs) |
| 8559 | State.FreeRegs -= sz; |
| 8560 | else |
| 8561 | State.FreeRegs = 0; |
| 8562 | } |
| 8563 | } |
| 8564 | |
| 8565 | void computeInfo(CGFunctionInfo &FI) const override { |
| 8566 | CCState State(FI.getCallingConvention()); |
| 8567 | // ARC uses 8 registers to pass arguments. |
| 8568 | State.FreeRegs = 8; |
| 8569 | |
| 8570 | if (!getCXXABI().classifyReturnType(FI)) |
| 8571 | FI.getReturnInfo() = classifyReturnType(FI.getReturnType()); |
| 8572 | updateState(FI.getReturnInfo(), FI.getReturnType(), State); |
| 8573 | for (auto &I : FI.arguments()) { |
| 8574 | I.info = classifyArgumentType(I.type, State.FreeRegs); |
| 8575 | updateState(I.info, I.type, State); |
| 8576 | } |
| 8577 | } |
| 8578 | |
| 8579 | ABIArgInfo getIndirectByRef(QualType Ty, bool HasFreeRegs) const; |
| 8580 | ABIArgInfo getIndirectByValue(QualType Ty) const; |
| 8581 | ABIArgInfo classifyArgumentType(QualType Ty, uint8_t FreeRegs) const; |
| 8582 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 8583 | }; |
| 8584 | |
| 8585 | class ARCTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8586 | public: |
| 8587 | ARCTargetCodeGenInfo(CodeGenTypes &CGT) |
| 8588 | : TargetCodeGenInfo(new ARCABIInfo(CGT)) {} |
| 8589 | }; |
| 8590 | |
| 8591 | |
| 8592 | ABIArgInfo ARCABIInfo::getIndirectByRef(QualType Ty, bool HasFreeRegs) const { |
| 8593 | return HasFreeRegs ? getNaturalAlignIndirectInReg(Ty) : |
| 8594 | getNaturalAlignIndirect(Ty, false); |
| 8595 | } |
| 8596 | |
| 8597 | ABIArgInfo ARCABIInfo::getIndirectByValue(QualType Ty) const { |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8598 | // Compute the byval alignment. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8599 | const unsigned MinABIStackAlignInBytes = 4; |
| 8600 | unsigned TypeAlign = getContext().getTypeAlign(Ty) / 8; |
| 8601 | return ABIArgInfo::getIndirect(CharUnits::fromQuantity(4), /*ByVal=*/true, |
| 8602 | TypeAlign > MinABIStackAlignInBytes); |
| 8603 | } |
| 8604 | |
| 8605 | Address ARCABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8606 | QualType Ty) const { |
| 8607 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, /*indirect*/ false, |
| 8608 | getContext().getTypeInfoInChars(Ty), |
| 8609 | CharUnits::fromQuantity(4), true); |
| 8610 | } |
| 8611 | |
| 8612 | ABIArgInfo ARCABIInfo::classifyArgumentType(QualType Ty, |
| 8613 | uint8_t FreeRegs) const { |
| 8614 | // Handle the generic C++ ABI. |
| 8615 | const RecordType *RT = Ty->getAs<RecordType>(); |
| 8616 | if (RT) { |
| 8617 | CGCXXABI::RecordArgABI RAA = getRecordArgABI(RT, getCXXABI()); |
| 8618 | if (RAA == CGCXXABI::RAA_Indirect) |
| 8619 | return getIndirectByRef(Ty, FreeRegs > 0); |
| 8620 | |
| 8621 | if (RAA == CGCXXABI::RAA_DirectInMemory) |
| 8622 | return getIndirectByValue(Ty); |
| 8623 | } |
| 8624 | |
| 8625 | // Treat an enum type as its underlying type. |
| 8626 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 8627 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 8628 | |
| 8629 | auto SizeInRegs = llvm::alignTo(getContext().getTypeSize(Ty), 32) / 32; |
| 8630 | |
| 8631 | if (isAggregateTypeForABI(Ty)) { |
| 8632 | // Structures with flexible arrays are always indirect. |
| 8633 | if (RT && RT->getDecl()->hasFlexibleArrayMember()) |
| 8634 | return getIndirectByValue(Ty); |
| 8635 | |
| 8636 | // Ignore empty structs/unions. |
| 8637 | if (isEmptyRecord(getContext(), Ty, true)) |
| 8638 | return ABIArgInfo::getIgnore(); |
| 8639 | |
| 8640 | llvm::LLVMContext &LLVMContext = getVMContext(); |
| 8641 | |
| 8642 | llvm::IntegerType *Int32 = llvm::Type::getInt32Ty(LLVMContext); |
| 8643 | SmallVector<llvm::Type *, 3> Elements(SizeInRegs, Int32); |
| 8644 | llvm::Type *Result = llvm::StructType::get(LLVMContext, Elements); |
| 8645 | |
| 8646 | return FreeRegs >= SizeInRegs ? |
| 8647 | ABIArgInfo::getDirectInReg(Result) : |
| 8648 | ABIArgInfo::getDirect(Result, 0, nullptr, false); |
| 8649 | } |
| 8650 | |
| 8651 | return Ty->isPromotableIntegerType() ? |
| 8652 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getExtendInReg(Ty) : |
| 8653 | ABIArgInfo::getExtend(Ty)) : |
| 8654 | (FreeRegs >= SizeInRegs ? ABIArgInfo::getDirectInReg() : |
| 8655 | ABIArgInfo::getDirect()); |
| 8656 | } |
| 8657 | |
| 8658 | ABIArgInfo ARCABIInfo::classifyReturnType(QualType RetTy) const { |
| 8659 | if (RetTy->isAnyComplexType()) |
| 8660 | return ABIArgInfo::getDirectInReg(); |
| 8661 | |
Daniel Dunbar | a39bab3 | 2019-01-03 23:24:50 +0000 | [diff] [blame] | 8662 | // Arguments of size > 4 registers are indirect. |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 8663 | auto RetSize = llvm::alignTo(getContext().getTypeSize(RetTy), 32) / 32; |
| 8664 | if (RetSize > 4) |
| 8665 | return getIndirectByRef(RetTy, /*HasFreeRegs*/ true); |
| 8666 | |
| 8667 | return DefaultABIInfo::classifyReturnType(RetTy); |
| 8668 | } |
| 8669 | |
| 8670 | } // End anonymous namespace. |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 8671 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8672 | //===----------------------------------------------------------------------===// |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8673 | // XCore ABI Implementation |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8674 | //===----------------------------------------------------------------------===// |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8675 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8676 | namespace { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8677 | |
| 8678 | /// A SmallStringEnc instance is used to build up the TypeString by passing |
| 8679 | /// it by reference between functions that append to it. |
| 8680 | typedef llvm::SmallString<128> SmallStringEnc; |
| 8681 | |
| 8682 | /// TypeStringCache caches the meta encodings of Types. |
| 8683 | /// |
| 8684 | /// The reason for caching TypeStrings is two fold: |
| 8685 | /// 1. To cache a type's encoding for later uses; |
| 8686 | /// 2. As a means to break recursive member type inclusion. |
| 8687 | /// |
| 8688 | /// A cache Entry can have a Status of: |
| 8689 | /// NonRecursive: The type encoding is not recursive; |
| 8690 | /// Recursive: The type encoding is recursive; |
| 8691 | /// Incomplete: An incomplete TypeString; |
| 8692 | /// IncompleteUsed: An incomplete TypeString that has been used in a |
| 8693 | /// Recursive type encoding. |
| 8694 | /// |
| 8695 | /// A NonRecursive entry will have all of its sub-members expanded as fully |
| 8696 | /// as possible. Whilst it may contain types which are recursive, the type |
| 8697 | /// itself is not recursive and thus its encoding may be safely used whenever |
| 8698 | /// the type is encountered. |
| 8699 | /// |
| 8700 | /// A Recursive entry will have all of its sub-members expanded as fully as |
| 8701 | /// possible. The type itself is recursive and it may contain other types which |
| 8702 | /// are recursive. The Recursive encoding must not be used during the expansion |
| 8703 | /// of a recursive type's recursive branch. For simplicity the code uses |
| 8704 | /// IncompleteCount to reject all usage of Recursive encodings for member types. |
| 8705 | /// |
| 8706 | /// An Incomplete entry is always a RecordType and only encodes its |
| 8707 | /// identifier e.g. "s(S){}". Incomplete 'StubEnc' entries are ephemeral and |
| 8708 | /// are placed into the cache during type expansion as a means to identify and |
| 8709 | /// handle recursive inclusion of types as sub-members. If there is recursion |
| 8710 | /// the entry becomes IncompleteUsed. |
| 8711 | /// |
| 8712 | /// During the expansion of a RecordType's members: |
| 8713 | /// |
| 8714 | /// If the cache contains a NonRecursive encoding for the member type, the |
| 8715 | /// cached encoding is used; |
| 8716 | /// |
| 8717 | /// If the cache contains a Recursive encoding for the member type, the |
| 8718 | /// cached encoding is 'Swapped' out, as it may be incorrect, and... |
| 8719 | /// |
| 8720 | /// If the member is a RecordType, an Incomplete encoding is placed into the |
| 8721 | /// cache to break potential recursive inclusion of itself as a sub-member; |
| 8722 | /// |
| 8723 | /// Once a member RecordType has been expanded, its temporary incomplete |
| 8724 | /// entry is removed from the cache. If a Recursive encoding was swapped out |
| 8725 | /// it is swapped back in; |
| 8726 | /// |
| 8727 | /// If an incomplete entry is used to expand a sub-member, the incomplete |
| 8728 | /// entry is marked as IncompleteUsed. The cache keeps count of how many |
| 8729 | /// IncompleteUsed entries it currently contains in IncompleteUsedCount; |
| 8730 | /// |
| 8731 | /// If a member's encoding is found to be a NonRecursive or Recursive viz: |
| 8732 | /// IncompleteUsedCount==0, the member's encoding is added to the cache. |
| 8733 | /// Else the member is part of a recursive type and thus the recursion has |
| 8734 | /// been exited too soon for the encoding to be correct for the member. |
| 8735 | /// |
| 8736 | class TypeStringCache { |
| 8737 | enum Status {NonRecursive, Recursive, Incomplete, IncompleteUsed}; |
| 8738 | struct Entry { |
| 8739 | std::string Str; // The encoded TypeString for the type. |
| 8740 | enum Status State; // Information about the encoding in 'Str'. |
| 8741 | std::string Swapped; // A temporary place holder for a Recursive encoding |
| 8742 | // during the expansion of RecordType's members. |
| 8743 | }; |
| 8744 | std::map<const IdentifierInfo *, struct Entry> Map; |
| 8745 | unsigned IncompleteCount; // Number of Incomplete entries in the Map. |
| 8746 | unsigned IncompleteUsedCount; // Number of IncompleteUsed entries in the Map. |
| 8747 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8748 | TypeStringCache() : IncompleteCount(0), IncompleteUsedCount(0) {} |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8749 | void addIncomplete(const IdentifierInfo *ID, std::string StubEnc); |
| 8750 | bool removeIncomplete(const IdentifierInfo *ID); |
| 8751 | void addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8752 | bool IsRecursive); |
| 8753 | StringRef lookupStr(const IdentifierInfo *ID); |
| 8754 | }; |
| 8755 | |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 8756 | /// TypeString encodings for enum & union fields must be order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8757 | /// FieldEncoding is a helper for this ordering process. |
| 8758 | class FieldEncoding { |
| 8759 | bool HasName; |
| 8760 | std::string Enc; |
| 8761 | public: |
Hans Wennborg | 4afe504 | 2015-07-22 20:46:26 +0000 | [diff] [blame] | 8762 | FieldEncoding(bool b, SmallStringEnc &e) : HasName(b), Enc(e.c_str()) {} |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8763 | StringRef str() { return Enc; } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8764 | bool operator<(const FieldEncoding &rhs) const { |
| 8765 | if (HasName != rhs.HasName) return HasName; |
| 8766 | return Enc < rhs.Enc; |
| 8767 | } |
| 8768 | }; |
| 8769 | |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8770 | class XCoreABIInfo : public DefaultABIInfo { |
| 8771 | public: |
| 8772 | XCoreABIInfo(CodeGen::CodeGenTypes &CGT) : DefaultABIInfo(CGT) {} |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8773 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8774 | QualType Ty) const override; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8775 | }; |
| 8776 | |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8777 | class XCoreTargetCodeGenInfo : public TargetCodeGenInfo { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8778 | mutable TypeStringCache TSC; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8779 | public: |
Robert Lytton | d21e2d7 | 2014-03-03 13:45:29 +0000 | [diff] [blame] | 8780 | XCoreTargetCodeGenInfo(CodeGenTypes &CGT) |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8781 | :TargetCodeGenInfo(new XCoreABIInfo(CGT)) {} |
Rafael Espindola | 8dcd6e7 | 2014-05-08 15:01:48 +0000 | [diff] [blame] | 8782 | void emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8783 | CodeGen::CodeGenModule &M) const override; |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8784 | }; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8785 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8786 | } // End anonymous namespace. |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8787 | |
James Y Knight | 29b5f08 | 2016-02-24 02:59:33 +0000 | [diff] [blame] | 8788 | // TODO: this implementation is likely now redundant with the default |
| 8789 | // EmitVAArg. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8790 | Address XCoreABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 8791 | QualType Ty) const { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8792 | CGBuilderTy &Builder = CGF.Builder; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8793 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8794 | // Get the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8795 | CharUnits SlotSize = CharUnits::fromQuantity(4); |
| 8796 | Address AP(Builder.CreateLoad(VAListAddr), SlotSize); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8797 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8798 | // Handle the argument. |
| 8799 | ABIArgInfo AI = classifyArgumentType(Ty); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8800 | CharUnits TypeAlign = getContext().getTypeAlignInChars(Ty); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8801 | llvm::Type *ArgTy = CGT.ConvertType(Ty); |
| 8802 | if (AI.canHaveCoerceToType() && !AI.getCoerceToType()) |
| 8803 | AI.setCoerceToType(ArgTy); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8804 | llvm::Type *ArgPtrTy = llvm::PointerType::getUnqual(ArgTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8805 | |
| 8806 | Address Val = Address::invalid(); |
| 8807 | CharUnits ArgSize = CharUnits::Zero(); |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8808 | switch (AI.getKind()) { |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8809 | case ABIArgInfo::Expand: |
John McCall | f26e73d | 2016-03-11 04:30:43 +0000 | [diff] [blame] | 8810 | case ABIArgInfo::CoerceAndExpand: |
Reid Kleckner | 314ef7b | 2014-02-01 00:04:45 +0000 | [diff] [blame] | 8811 | case ABIArgInfo::InAlloca: |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8812 | llvm_unreachable("Unsupported ABI kind for va_arg"); |
| 8813 | case ABIArgInfo::Ignore: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8814 | Val = Address(llvm::UndefValue::get(ArgPtrTy), TypeAlign); |
| 8815 | ArgSize = CharUnits::Zero(); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8816 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8817 | case ABIArgInfo::Extend: |
| 8818 | case ABIArgInfo::Direct: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8819 | Val = Builder.CreateBitCast(AP, ArgPtrTy); |
| 8820 | ArgSize = CharUnits::fromQuantity( |
| 8821 | getDataLayout().getTypeAllocSize(AI.getCoerceToType())); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 8822 | ArgSize = ArgSize.alignTo(SlotSize); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8823 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8824 | case ABIArgInfo::Indirect: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8825 | Val = Builder.CreateElementBitCast(AP, ArgPtrTy); |
| 8826 | Val = Address(Builder.CreateLoad(Val), TypeAlign); |
| 8827 | ArgSize = SlotSize; |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8828 | break; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8829 | } |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8830 | |
| 8831 | // Increment the VAList. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8832 | if (!ArgSize.isZero()) { |
James Y Knight | 3d2df5a | 2019-02-05 19:01:33 +0000 | [diff] [blame] | 8833 | Address APN = Builder.CreateConstInBoundsByteGEP(AP, ArgSize); |
| 8834 | Builder.CreateStore(APN.getPointer(), VAListAddr); |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8835 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 8836 | |
Robert Lytton | 2d19695 | 2013-10-11 10:29:34 +0000 | [diff] [blame] | 8837 | return Val; |
Robert Lytton | 7d1db15 | 2013-08-19 09:46:39 +0000 | [diff] [blame] | 8838 | } |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 8839 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8840 | /// During the expansion of a RecordType, an incomplete TypeString is placed |
| 8841 | /// into the cache as a means to identify and break recursion. |
| 8842 | /// If there is a Recursive encoding in the cache, it is swapped out and will |
| 8843 | /// be reinserted by removeIncomplete(). |
| 8844 | /// All other types of encoding should have been used rather than arriving here. |
| 8845 | void TypeStringCache::addIncomplete(const IdentifierInfo *ID, |
| 8846 | std::string StubEnc) { |
| 8847 | if (!ID) |
| 8848 | return; |
| 8849 | Entry &E = Map[ID]; |
| 8850 | assert( (E.Str.empty() || E.State == Recursive) && |
| 8851 | "Incorrectly use of addIncomplete"); |
| 8852 | assert(!StubEnc.empty() && "Passing an empty string to addIncomplete()"); |
| 8853 | E.Swapped.swap(E.Str); // swap out the Recursive |
| 8854 | E.Str.swap(StubEnc); |
| 8855 | E.State = Incomplete; |
| 8856 | ++IncompleteCount; |
| 8857 | } |
| 8858 | |
| 8859 | /// Once the RecordType has been expanded, the temporary incomplete TypeString |
| 8860 | /// must be removed from the cache. |
| 8861 | /// If a Recursive was swapped out by addIncomplete(), it will be replaced. |
| 8862 | /// Returns true if the RecordType was defined recursively. |
| 8863 | bool TypeStringCache::removeIncomplete(const IdentifierInfo *ID) { |
| 8864 | if (!ID) |
| 8865 | return false; |
| 8866 | auto I = Map.find(ID); |
| 8867 | assert(I != Map.end() && "Entry not present"); |
| 8868 | Entry &E = I->second; |
| 8869 | assert( (E.State == Incomplete || |
| 8870 | E.State == IncompleteUsed) && |
| 8871 | "Entry must be an incomplete type"); |
| 8872 | bool IsRecursive = false; |
| 8873 | if (E.State == IncompleteUsed) { |
| 8874 | // We made use of our Incomplete encoding, thus we are recursive. |
| 8875 | IsRecursive = true; |
| 8876 | --IncompleteUsedCount; |
| 8877 | } |
| 8878 | if (E.Swapped.empty()) |
| 8879 | Map.erase(I); |
| 8880 | else { |
| 8881 | // Swap the Recursive back. |
| 8882 | E.Swapped.swap(E.Str); |
| 8883 | E.Swapped.clear(); |
| 8884 | E.State = Recursive; |
| 8885 | } |
| 8886 | --IncompleteCount; |
| 8887 | return IsRecursive; |
| 8888 | } |
| 8889 | |
| 8890 | /// Add the encoded TypeString to the cache only if it is NonRecursive or |
| 8891 | /// Recursive (viz: all sub-members were expanded as fully as possible). |
| 8892 | void TypeStringCache::addIfComplete(const IdentifierInfo *ID, StringRef Str, |
| 8893 | bool IsRecursive) { |
| 8894 | if (!ID || IncompleteUsedCount) |
| 8895 | return; // No key or it is is an incomplete sub-type so don't add. |
| 8896 | Entry &E = Map[ID]; |
| 8897 | if (IsRecursive && !E.Str.empty()) { |
| 8898 | assert(E.State==Recursive && E.Str.size() == Str.size() && |
| 8899 | "This is not the same Recursive entry"); |
| 8900 | // The parent container was not recursive after all, so we could have used |
| 8901 | // this Recursive sub-member entry after all, but we assumed the worse when |
| 8902 | // we started viz: IncompleteCount!=0. |
| 8903 | return; |
| 8904 | } |
| 8905 | assert(E.Str.empty() && "Entry already present"); |
| 8906 | E.Str = Str.str(); |
| 8907 | E.State = IsRecursive? Recursive : NonRecursive; |
| 8908 | } |
| 8909 | |
| 8910 | /// Return a cached TypeString encoding for the ID. If there isn't one, or we |
| 8911 | /// are recursively expanding a type (IncompleteCount != 0) and the cached |
| 8912 | /// encoding is Recursive, return an empty StringRef. |
| 8913 | StringRef TypeStringCache::lookupStr(const IdentifierInfo *ID) { |
| 8914 | if (!ID) |
| 8915 | return StringRef(); // We have no key. |
| 8916 | auto I = Map.find(ID); |
| 8917 | if (I == Map.end()) |
| 8918 | return StringRef(); // We have no encoding. |
| 8919 | Entry &E = I->second; |
| 8920 | if (E.State == Recursive && IncompleteCount) |
| 8921 | return StringRef(); // We don't use Recursive encodings for member types. |
| 8922 | |
| 8923 | if (E.State == Incomplete) { |
| 8924 | // The incomplete type is being used to break out of recursion. |
| 8925 | E.State = IncompleteUsed; |
| 8926 | ++IncompleteUsedCount; |
| 8927 | } |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 8928 | return E.Str; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8929 | } |
| 8930 | |
| 8931 | /// The XCore ABI includes a type information section that communicates symbol |
| 8932 | /// type information to the linker. The linker uses this information to verify |
| 8933 | /// safety/correctness of things such as array bound and pointers et al. |
| 8934 | /// The ABI only requires C (and XC) language modules to emit TypeStrings. |
| 8935 | /// This type information (TypeString) is emitted into meta data for all global |
| 8936 | /// symbols: definitions, declarations, functions & variables. |
| 8937 | /// |
| 8938 | /// The TypeString carries type, qualifier, name, size & value details. |
| 8939 | /// Please see 'Tools Development Guide' section 2.16.2 for format details: |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8940 | /// https://www.xmos.com/download/public/Tools-Development-Guide%28X9114A%29.pdf |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8941 | /// The output is tested by test/CodeGen/xcore-stringtype.c. |
| 8942 | /// |
| 8943 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 8944 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC); |
| 8945 | |
| 8946 | /// XCore uses emitTargetMD to emit TypeString metadata for global symbols. |
| 8947 | void XCoreTargetCodeGenInfo::emitTargetMD(const Decl *D, llvm::GlobalValue *GV, |
| 8948 | CodeGen::CodeGenModule &CGM) const { |
| 8949 | SmallStringEnc Enc; |
| 8950 | if (getTypeString(Enc, D, CGM, TSC)) { |
| 8951 | llvm::LLVMContext &Ctx = CGM.getModule().getContext(); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 8952 | llvm::Metadata *MDVals[] = {llvm::ConstantAsMetadata::get(GV), |
| 8953 | llvm::MDString::get(Ctx, Enc.str())}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8954 | llvm::NamedMDNode *MD = |
| 8955 | CGM.getModule().getOrInsertNamedMetadata("xcore.typestrings"); |
| 8956 | MD->addOperand(llvm::MDNode::get(Ctx, MDVals)); |
| 8957 | } |
| 8958 | } |
| 8959 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8960 | //===----------------------------------------------------------------------===// |
| 8961 | // SPIR ABI Implementation |
| 8962 | //===----------------------------------------------------------------------===// |
| 8963 | |
| 8964 | namespace { |
| 8965 | class SPIRTargetCodeGenInfo : public TargetCodeGenInfo { |
| 8966 | public: |
| 8967 | SPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT) |
| 8968 | : TargetCodeGenInfo(new DefaultABIInfo(CGT)) {} |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8969 | unsigned getOpenCLKernelCallingConv() const override; |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8970 | }; |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8971 | |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 8972 | } // End anonymous namespace. |
| 8973 | |
Pekka Jaaskelainen | fc2629a | 2017-06-01 07:18:49 +0000 | [diff] [blame] | 8974 | namespace clang { |
| 8975 | namespace CodeGen { |
| 8976 | void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI) { |
| 8977 | DefaultABIInfo SPIRABI(CGM.getTypes()); |
| 8978 | SPIRABI.computeInfo(FI); |
| 8979 | } |
| 8980 | } |
| 8981 | } |
| 8982 | |
Nikolay Haustov | 8c6538b | 2016-06-30 09:06:33 +0000 | [diff] [blame] | 8983 | unsigned SPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const { |
| 8984 | return llvm::CallingConv::SPIR_KERNEL; |
| 8985 | } |
| 8986 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8987 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 8988 | const CodeGen::CodeGenModule &CGM, |
| 8989 | TypeStringCache &TSC); |
| 8990 | |
| 8991 | /// Helper function for appendRecordType(). |
Eric Christopher | 7565e0d | 2015-05-29 23:09:49 +0000 | [diff] [blame] | 8992 | /// Builds a SmallVector containing the encoded field types in declaration |
| 8993 | /// order. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8994 | static bool extractFieldType(SmallVectorImpl<FieldEncoding> &FE, |
| 8995 | const RecordDecl *RD, |
| 8996 | const CodeGen::CodeGenModule &CGM, |
| 8997 | TypeStringCache &TSC) { |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 8998 | for (const auto *Field : RD->fields()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 8999 | SmallStringEnc Enc; |
| 9000 | Enc += "m("; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 9001 | Enc += Field->getName(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9002 | Enc += "){"; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 9003 | if (Field->isBitField()) { |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9004 | Enc += "b("; |
| 9005 | llvm::raw_svector_ostream OS(Enc); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 9006 | OS << Field->getBitWidthValue(CGM.getContext()); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9007 | Enc += ':'; |
| 9008 | } |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 9009 | if (!appendType(Enc, Field->getType(), CGM, TSC)) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9010 | return false; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 9011 | if (Field->isBitField()) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9012 | Enc += ')'; |
| 9013 | Enc += '}'; |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 9014 | FE.emplace_back(!Field->getName().empty(), Enc); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9015 | } |
| 9016 | return true; |
| 9017 | } |
| 9018 | |
| 9019 | /// Appends structure and union types to Enc and adds encoding to cache. |
| 9020 | /// Recursively calls appendType (via extractFieldType) for each field. |
| 9021 | /// Union types have their fields ordered according to the ABI. |
| 9022 | static bool appendRecordType(SmallStringEnc &Enc, const RecordType *RT, |
| 9023 | const CodeGen::CodeGenModule &CGM, |
| 9024 | TypeStringCache &TSC, const IdentifierInfo *ID) { |
| 9025 | // Append the cached TypeString if we have one. |
| 9026 | StringRef TypeString = TSC.lookupStr(ID); |
| 9027 | if (!TypeString.empty()) { |
| 9028 | Enc += TypeString; |
| 9029 | return true; |
| 9030 | } |
| 9031 | |
| 9032 | // Start to emit an incomplete TypeString. |
| 9033 | size_t Start = Enc.size(); |
| 9034 | Enc += (RT->isUnionType()? 'u' : 's'); |
| 9035 | Enc += '('; |
| 9036 | if (ID) |
| 9037 | Enc += ID->getName(); |
| 9038 | Enc += "){"; |
| 9039 | |
| 9040 | // We collect all encoded fields and order as necessary. |
| 9041 | bool IsRecursive = false; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9042 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 9043 | if (RD && !RD->field_empty()) { |
| 9044 | // An incomplete TypeString stub is placed in the cache for this RecordType |
| 9045 | // so that recursive calls to this RecordType will use it whilst building a |
| 9046 | // complete TypeString for this RecordType. |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9047 | SmallVector<FieldEncoding, 16> FE; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9048 | std::string StubEnc(Enc.substr(Start).str()); |
| 9049 | StubEnc += '}'; // StubEnc now holds a valid incomplete TypeString. |
| 9050 | TSC.addIncomplete(ID, std::move(StubEnc)); |
| 9051 | if (!extractFieldType(FE, RD, CGM, TSC)) { |
| 9052 | (void) TSC.removeIncomplete(ID); |
| 9053 | return false; |
| 9054 | } |
| 9055 | IsRecursive = TSC.removeIncomplete(ID); |
| 9056 | // The ABI requires unions to be sorted but not structures. |
| 9057 | // See FieldEncoding::operator< for sort algorithm. |
| 9058 | if (RT->isUnionType()) |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 9059 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9060 | // We can now complete the TypeString. |
| 9061 | unsigned E = FE.size(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9062 | for (unsigned I = 0; I != E; ++I) { |
| 9063 | if (I) |
| 9064 | Enc += ','; |
| 9065 | Enc += FE[I].str(); |
| 9066 | } |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9067 | } |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9068 | Enc += '}'; |
| 9069 | TSC.addIfComplete(ID, Enc.substr(Start), IsRecursive); |
| 9070 | return true; |
| 9071 | } |
| 9072 | |
| 9073 | /// Appends enum types to Enc and adds the encoding to the cache. |
| 9074 | static bool appendEnumType(SmallStringEnc &Enc, const EnumType *ET, |
| 9075 | TypeStringCache &TSC, |
| 9076 | const IdentifierInfo *ID) { |
| 9077 | // Append the cached TypeString if we have one. |
| 9078 | StringRef TypeString = TSC.lookupStr(ID); |
| 9079 | if (!TypeString.empty()) { |
| 9080 | Enc += TypeString; |
| 9081 | return true; |
| 9082 | } |
| 9083 | |
| 9084 | size_t Start = Enc.size(); |
| 9085 | Enc += "e("; |
| 9086 | if (ID) |
| 9087 | Enc += ID->getName(); |
| 9088 | Enc += "){"; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9089 | |
| 9090 | // We collect all encoded enumerations and order them alphanumerically. |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9091 | if (const EnumDecl *ED = ET->getDecl()->getDefinition()) { |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9092 | SmallVector<FieldEncoding, 16> FE; |
| 9093 | for (auto I = ED->enumerator_begin(), E = ED->enumerator_end(); I != E; |
| 9094 | ++I) { |
| 9095 | SmallStringEnc EnumEnc; |
| 9096 | EnumEnc += "m("; |
| 9097 | EnumEnc += I->getName(); |
| 9098 | EnumEnc += "){"; |
| 9099 | I->getInitVal().toString(EnumEnc); |
| 9100 | EnumEnc += '}'; |
| 9101 | FE.push_back(FieldEncoding(!I->getName().empty(), EnumEnc)); |
| 9102 | } |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 9103 | llvm::sort(FE); |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9104 | unsigned E = FE.size(); |
| 9105 | for (unsigned I = 0; I != E; ++I) { |
| 9106 | if (I) |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9107 | Enc += ','; |
Robert Lytton | db8c1cb | 2014-05-20 07:19:33 +0000 | [diff] [blame] | 9108 | Enc += FE[I].str(); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9109 | } |
| 9110 | } |
| 9111 | Enc += '}'; |
| 9112 | TSC.addIfComplete(ID, Enc.substr(Start), false); |
| 9113 | return true; |
| 9114 | } |
| 9115 | |
| 9116 | /// Appends type's qualifier to Enc. |
| 9117 | /// This is done prior to appending the type's encoding. |
| 9118 | static void appendQualifier(SmallStringEnc &Enc, QualType QT) { |
| 9119 | // Qualifiers are emitted in alphabetical order. |
Craig Topper | 273dbc6 | 2015-10-18 05:29:26 +0000 | [diff] [blame] | 9120 | static const char *const Table[]={"","c:","r:","cr:","v:","cv:","rv:","crv:"}; |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9121 | int Lookup = 0; |
| 9122 | if (QT.isConstQualified()) |
| 9123 | Lookup += 1<<0; |
| 9124 | if (QT.isRestrictQualified()) |
| 9125 | Lookup += 1<<1; |
| 9126 | if (QT.isVolatileQualified()) |
| 9127 | Lookup += 1<<2; |
| 9128 | Enc += Table[Lookup]; |
| 9129 | } |
| 9130 | |
| 9131 | /// Appends built-in types to Enc. |
| 9132 | static bool appendBuiltinType(SmallStringEnc &Enc, const BuiltinType *BT) { |
| 9133 | const char *EncType; |
| 9134 | switch (BT->getKind()) { |
| 9135 | case BuiltinType::Void: |
| 9136 | EncType = "0"; |
| 9137 | break; |
| 9138 | case BuiltinType::Bool: |
| 9139 | EncType = "b"; |
| 9140 | break; |
| 9141 | case BuiltinType::Char_U: |
| 9142 | EncType = "uc"; |
| 9143 | break; |
| 9144 | case BuiltinType::UChar: |
| 9145 | EncType = "uc"; |
| 9146 | break; |
| 9147 | case BuiltinType::SChar: |
| 9148 | EncType = "sc"; |
| 9149 | break; |
| 9150 | case BuiltinType::UShort: |
| 9151 | EncType = "us"; |
| 9152 | break; |
| 9153 | case BuiltinType::Short: |
| 9154 | EncType = "ss"; |
| 9155 | break; |
| 9156 | case BuiltinType::UInt: |
| 9157 | EncType = "ui"; |
| 9158 | break; |
| 9159 | case BuiltinType::Int: |
| 9160 | EncType = "si"; |
| 9161 | break; |
| 9162 | case BuiltinType::ULong: |
| 9163 | EncType = "ul"; |
| 9164 | break; |
| 9165 | case BuiltinType::Long: |
| 9166 | EncType = "sl"; |
| 9167 | break; |
| 9168 | case BuiltinType::ULongLong: |
| 9169 | EncType = "ull"; |
| 9170 | break; |
| 9171 | case BuiltinType::LongLong: |
| 9172 | EncType = "sll"; |
| 9173 | break; |
| 9174 | case BuiltinType::Float: |
| 9175 | EncType = "ft"; |
| 9176 | break; |
| 9177 | case BuiltinType::Double: |
| 9178 | EncType = "d"; |
| 9179 | break; |
| 9180 | case BuiltinType::LongDouble: |
| 9181 | EncType = "ld"; |
| 9182 | break; |
| 9183 | default: |
| 9184 | return false; |
| 9185 | } |
| 9186 | Enc += EncType; |
| 9187 | return true; |
| 9188 | } |
| 9189 | |
| 9190 | /// Appends a pointer encoding to Enc before calling appendType for the pointee. |
| 9191 | static bool appendPointerType(SmallStringEnc &Enc, const PointerType *PT, |
| 9192 | const CodeGen::CodeGenModule &CGM, |
| 9193 | TypeStringCache &TSC) { |
| 9194 | Enc += "p("; |
| 9195 | if (!appendType(Enc, PT->getPointeeType(), CGM, TSC)) |
| 9196 | return false; |
| 9197 | Enc += ')'; |
| 9198 | return true; |
| 9199 | } |
| 9200 | |
| 9201 | /// Appends array encoding to Enc before calling appendType for the element. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9202 | static bool appendArrayType(SmallStringEnc &Enc, QualType QT, |
| 9203 | const ArrayType *AT, |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9204 | const CodeGen::CodeGenModule &CGM, |
| 9205 | TypeStringCache &TSC, StringRef NoSizeEnc) { |
| 9206 | if (AT->getSizeModifier() != ArrayType::Normal) |
| 9207 | return false; |
| 9208 | Enc += "a("; |
| 9209 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 9210 | CAT->getSize().toStringUnsigned(Enc); |
| 9211 | else |
| 9212 | Enc += NoSizeEnc; // Global arrays use "*", otherwise it is "". |
| 9213 | Enc += ':'; |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9214 | // The Qualifiers should be attached to the type rather than the array. |
| 9215 | appendQualifier(Enc, QT); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9216 | if (!appendType(Enc, AT->getElementType(), CGM, TSC)) |
| 9217 | return false; |
| 9218 | Enc += ')'; |
| 9219 | return true; |
| 9220 | } |
| 9221 | |
| 9222 | /// Appends a function encoding to Enc, calling appendType for the return type |
| 9223 | /// and the arguments. |
| 9224 | static bool appendFunctionType(SmallStringEnc &Enc, const FunctionType *FT, |
| 9225 | const CodeGen::CodeGenModule &CGM, |
| 9226 | TypeStringCache &TSC) { |
| 9227 | Enc += "f{"; |
| 9228 | if (!appendType(Enc, FT->getReturnType(), CGM, TSC)) |
| 9229 | return false; |
| 9230 | Enc += "}("; |
| 9231 | if (const FunctionProtoType *FPT = FT->getAs<FunctionProtoType>()) { |
| 9232 | // N.B. we are only interested in the adjusted param types. |
| 9233 | auto I = FPT->param_type_begin(); |
| 9234 | auto E = FPT->param_type_end(); |
| 9235 | if (I != E) { |
| 9236 | do { |
| 9237 | if (!appendType(Enc, *I, CGM, TSC)) |
| 9238 | return false; |
| 9239 | ++I; |
| 9240 | if (I != E) |
| 9241 | Enc += ','; |
| 9242 | } while (I != E); |
| 9243 | if (FPT->isVariadic()) |
| 9244 | Enc += ",va"; |
| 9245 | } else { |
| 9246 | if (FPT->isVariadic()) |
| 9247 | Enc += "va"; |
| 9248 | else |
| 9249 | Enc += '0'; |
| 9250 | } |
| 9251 | } |
| 9252 | Enc += ')'; |
| 9253 | return true; |
| 9254 | } |
| 9255 | |
| 9256 | /// Handles the type's qualifier before dispatching a call to handle specific |
| 9257 | /// type encodings. |
| 9258 | static bool appendType(SmallStringEnc &Enc, QualType QType, |
| 9259 | const CodeGen::CodeGenModule &CGM, |
| 9260 | TypeStringCache &TSC) { |
| 9261 | |
| 9262 | QualType QT = QType.getCanonicalType(); |
| 9263 | |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9264 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) |
| 9265 | // The Qualifiers should be attached to the type rather than the array. |
| 9266 | // Thus we don't call appendQualifier() here. |
| 9267 | return appendArrayType(Enc, QT, AT, CGM, TSC, ""); |
| 9268 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9269 | appendQualifier(Enc, QT); |
| 9270 | |
| 9271 | if (const BuiltinType *BT = QT->getAs<BuiltinType>()) |
| 9272 | return appendBuiltinType(Enc, BT); |
| 9273 | |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9274 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 9275 | return appendPointerType(Enc, PT, CGM, TSC); |
| 9276 | |
| 9277 | if (const EnumType *ET = QT->getAs<EnumType>()) |
| 9278 | return appendEnumType(Enc, ET, TSC, QT.getBaseTypeIdentifier()); |
| 9279 | |
| 9280 | if (const RecordType *RT = QT->getAsStructureType()) |
| 9281 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9282 | |
| 9283 | if (const RecordType *RT = QT->getAsUnionType()) |
| 9284 | return appendRecordType(Enc, RT, CGM, TSC, QT.getBaseTypeIdentifier()); |
| 9285 | |
| 9286 | if (const FunctionType *FT = QT->getAs<FunctionType>()) |
| 9287 | return appendFunctionType(Enc, FT, CGM, TSC); |
| 9288 | |
| 9289 | return false; |
| 9290 | } |
| 9291 | |
| 9292 | static bool getTypeString(SmallStringEnc &Enc, const Decl *D, |
| 9293 | CodeGen::CodeGenModule &CGM, TypeStringCache &TSC) { |
| 9294 | if (!D) |
| 9295 | return false; |
| 9296 | |
| 9297 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9298 | if (FD->getLanguageLinkage() != CLanguageLinkage) |
| 9299 | return false; |
| 9300 | return appendType(Enc, FD->getType(), CGM, TSC); |
| 9301 | } |
| 9302 | |
| 9303 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 9304 | if (VD->getLanguageLinkage() != CLanguageLinkage) |
| 9305 | return false; |
| 9306 | QualType QT = VD->getType().getCanonicalType(); |
| 9307 | if (const ArrayType *AT = QT->getAsArrayTypeUnsafe()) { |
| 9308 | // Global ArrayTypes are given a size of '*' if the size is unknown. |
Robert Lytton | 6adb20f | 2014-06-05 09:06:21 +0000 | [diff] [blame] | 9309 | // The Qualifiers should be attached to the type rather than the array. |
| 9310 | // Thus we don't call appendQualifier() here. |
| 9311 | return appendArrayType(Enc, QT, AT, CGM, TSC, "*"); |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9312 | } |
| 9313 | return appendType(Enc, QT, CGM, TSC); |
| 9314 | } |
| 9315 | return false; |
| 9316 | } |
| 9317 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9318 | //===----------------------------------------------------------------------===// |
| 9319 | // RISCV ABI Implementation |
| 9320 | //===----------------------------------------------------------------------===// |
| 9321 | |
| 9322 | namespace { |
| 9323 | class RISCVABIInfo : public DefaultABIInfo { |
| 9324 | private: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9325 | // Size of the integer ('x') registers in bits. |
| 9326 | unsigned XLen; |
| 9327 | // Size of the floating point ('f') registers in bits. Note that the target |
| 9328 | // ISA might have a wider FLen than the selected ABI (e.g. an RV32IF target |
| 9329 | // with soft float ABI has FLen==0). |
| 9330 | unsigned FLen; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9331 | static const int NumArgGPRs = 8; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9332 | static const int NumArgFPRs = 8; |
| 9333 | bool detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, |
| 9334 | llvm::Type *&Field1Ty, |
| 9335 | CharUnits &Field1Off, |
| 9336 | llvm::Type *&Field2Ty, |
| 9337 | CharUnits &Field2Off) const; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9338 | |
| 9339 | public: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9340 | RISCVABIInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, unsigned FLen) |
| 9341 | : DefaultABIInfo(CGT), XLen(XLen), FLen(FLen) {} |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9342 | |
| 9343 | // DefaultABIInfo's classifyReturnType and classifyArgumentType are |
| 9344 | // non-virtual, but computeInfo is virtual, so we overload it. |
| 9345 | void computeInfo(CGFunctionInfo &FI) const override; |
| 9346 | |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9347 | ABIArgInfo classifyArgumentType(QualType Ty, bool IsFixed, int &ArgGPRsLeft, |
| 9348 | int &ArgFPRsLeft) const; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9349 | ABIArgInfo classifyReturnType(QualType RetTy) const; |
| 9350 | |
| 9351 | Address EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9352 | QualType Ty) const override; |
| 9353 | |
| 9354 | ABIArgInfo extendType(QualType Ty) const; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9355 | |
| 9356 | bool detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, |
| 9357 | CharUnits &Field1Off, llvm::Type *&Field2Ty, |
| 9358 | CharUnits &Field2Off, int &NeededArgGPRs, |
| 9359 | int &NeededArgFPRs) const; |
| 9360 | ABIArgInfo coerceAndExpandFPCCEligibleStruct(llvm::Type *Field1Ty, |
| 9361 | CharUnits Field1Off, |
| 9362 | llvm::Type *Field2Ty, |
| 9363 | CharUnits Field2Off) const; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9364 | }; |
| 9365 | } // end anonymous namespace |
| 9366 | |
| 9367 | void RISCVABIInfo::computeInfo(CGFunctionInfo &FI) const { |
| 9368 | QualType RetTy = FI.getReturnType(); |
| 9369 | if (!getCXXABI().classifyReturnType(FI)) |
| 9370 | FI.getReturnInfo() = classifyReturnType(RetTy); |
| 9371 | |
| 9372 | // IsRetIndirect is true if classifyArgumentType indicated the value should |
| 9373 | // be passed indirect or if the type size is greater than 2*xlen. e.g. fp128 |
| 9374 | // is passed direct in LLVM IR, relying on the backend lowering code to |
| 9375 | // rewrite the argument list and pass indirectly on RV32. |
| 9376 | bool IsRetIndirect = FI.getReturnInfo().getKind() == ABIArgInfo::Indirect || |
| 9377 | getContext().getTypeSize(RetTy) > (2 * XLen); |
| 9378 | |
| 9379 | // We must track the number of GPRs used in order to conform to the RISC-V |
| 9380 | // ABI, as integer scalars passed in registers should have signext/zeroext |
| 9381 | // when promoted, but are anyext if passed on the stack. As GPR usage is |
| 9382 | // different for variadic arguments, we must also track whether we are |
| 9383 | // examining a vararg or not. |
| 9384 | int ArgGPRsLeft = IsRetIndirect ? NumArgGPRs - 1 : NumArgGPRs; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9385 | int ArgFPRsLeft = FLen ? NumArgFPRs : 0; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9386 | int NumFixedArgs = FI.getNumRequiredArgs(); |
| 9387 | |
| 9388 | int ArgNum = 0; |
| 9389 | for (auto &ArgInfo : FI.arguments()) { |
| 9390 | bool IsFixed = ArgNum < NumFixedArgs; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9391 | ArgInfo.info = |
| 9392 | classifyArgumentType(ArgInfo.type, IsFixed, ArgGPRsLeft, ArgFPRsLeft); |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9393 | ArgNum++; |
| 9394 | } |
| 9395 | } |
| 9396 | |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9397 | // Returns true if the struct is a potential candidate for the floating point |
| 9398 | // calling convention. If this function returns true, the caller is |
| 9399 | // responsible for checking that if there is only a single field then that |
| 9400 | // field is a float. |
| 9401 | bool RISCVABIInfo::detectFPCCEligibleStructHelper(QualType Ty, CharUnits CurOff, |
| 9402 | llvm::Type *&Field1Ty, |
| 9403 | CharUnits &Field1Off, |
| 9404 | llvm::Type *&Field2Ty, |
| 9405 | CharUnits &Field2Off) const { |
| 9406 | bool IsInt = Ty->isIntegralOrEnumerationType(); |
| 9407 | bool IsFloat = Ty->isRealFloatingType(); |
| 9408 | |
| 9409 | if (IsInt || IsFloat) { |
| 9410 | uint64_t Size = getContext().getTypeSize(Ty); |
| 9411 | if (IsInt && Size > XLen) |
| 9412 | return false; |
| 9413 | // Can't be eligible if larger than the FP registers. Half precision isn't |
| 9414 | // currently supported on RISC-V and the ABI hasn't been confirmed, so |
| 9415 | // default to the integer ABI in that case. |
| 9416 | if (IsFloat && (Size > FLen || Size < 32)) |
| 9417 | return false; |
| 9418 | // Can't be eligible if an integer type was already found (int+int pairs |
| 9419 | // are not eligible). |
| 9420 | if (IsInt && Field1Ty && Field1Ty->isIntegerTy()) |
| 9421 | return false; |
| 9422 | if (!Field1Ty) { |
| 9423 | Field1Ty = CGT.ConvertType(Ty); |
| 9424 | Field1Off = CurOff; |
| 9425 | return true; |
| 9426 | } |
| 9427 | if (!Field2Ty) { |
| 9428 | Field2Ty = CGT.ConvertType(Ty); |
| 9429 | Field2Off = CurOff; |
| 9430 | return true; |
| 9431 | } |
| 9432 | return false; |
| 9433 | } |
| 9434 | |
| 9435 | if (auto CTy = Ty->getAs<ComplexType>()) { |
| 9436 | if (Field1Ty) |
| 9437 | return false; |
| 9438 | QualType EltTy = CTy->getElementType(); |
| 9439 | if (getContext().getTypeSize(EltTy) > FLen) |
| 9440 | return false; |
| 9441 | Field1Ty = CGT.ConvertType(EltTy); |
| 9442 | Field1Off = CurOff; |
| 9443 | assert(CurOff.isZero() && "Unexpected offset for first field"); |
| 9444 | Field2Ty = Field1Ty; |
| 9445 | Field2Off = Field1Off + getContext().getTypeSizeInChars(EltTy); |
| 9446 | return true; |
| 9447 | } |
| 9448 | |
| 9449 | if (const ConstantArrayType *ATy = getContext().getAsConstantArrayType(Ty)) { |
| 9450 | uint64_t ArraySize = ATy->getSize().getZExtValue(); |
| 9451 | QualType EltTy = ATy->getElementType(); |
| 9452 | CharUnits EltSize = getContext().getTypeSizeInChars(EltTy); |
| 9453 | for (uint64_t i = 0; i < ArraySize; ++i) { |
| 9454 | bool Ret = detectFPCCEligibleStructHelper(EltTy, CurOff, Field1Ty, |
| 9455 | Field1Off, Field2Ty, Field2Off); |
| 9456 | if (!Ret) |
| 9457 | return false; |
| 9458 | CurOff += EltSize; |
| 9459 | } |
| 9460 | return true; |
| 9461 | } |
| 9462 | |
| 9463 | if (const auto *RTy = Ty->getAs<RecordType>()) { |
| 9464 | // Structures with either a non-trivial destructor or a non-trivial |
| 9465 | // copy constructor are not eligible for the FP calling convention. |
Denis Bakhvalov | a29002e | 2019-07-19 21:59:42 +0000 | [diff] [blame] | 9466 | if (getRecordArgABI(Ty, CGT.getCXXABI())) |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9467 | return false; |
| 9468 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9469 | return true; |
| 9470 | const RecordDecl *RD = RTy->getDecl(); |
| 9471 | // Unions aren't eligible unless they're empty (which is caught above). |
| 9472 | if (RD->isUnion()) |
| 9473 | return false; |
| 9474 | int ZeroWidthBitFieldCount = 0; |
| 9475 | for (const FieldDecl *FD : RD->fields()) { |
| 9476 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 9477 | uint64_t FieldOffInBits = Layout.getFieldOffset(FD->getFieldIndex()); |
| 9478 | QualType QTy = FD->getType(); |
| 9479 | if (FD->isBitField()) { |
| 9480 | unsigned BitWidth = FD->getBitWidthValue(getContext()); |
| 9481 | // Allow a bitfield with a type greater than XLen as long as the |
| 9482 | // bitwidth is XLen or less. |
| 9483 | if (getContext().getTypeSize(QTy) > XLen && BitWidth <= XLen) |
| 9484 | QTy = getContext().getIntTypeForBitwidth(XLen, false); |
| 9485 | if (BitWidth == 0) { |
| 9486 | ZeroWidthBitFieldCount++; |
| 9487 | continue; |
| 9488 | } |
| 9489 | } |
| 9490 | |
| 9491 | bool Ret = detectFPCCEligibleStructHelper( |
| 9492 | QTy, CurOff + getContext().toCharUnitsFromBits(FieldOffInBits), |
| 9493 | Field1Ty, Field1Off, Field2Ty, Field2Off); |
| 9494 | if (!Ret) |
| 9495 | return false; |
| 9496 | |
| 9497 | // As a quirk of the ABI, zero-width bitfields aren't ignored for fp+fp |
| 9498 | // or int+fp structs, but are ignored for a struct with an fp field and |
| 9499 | // any number of zero-width bitfields. |
| 9500 | if (Field2Ty && ZeroWidthBitFieldCount > 0) |
| 9501 | return false; |
| 9502 | } |
| 9503 | return Field1Ty != nullptr; |
| 9504 | } |
| 9505 | |
| 9506 | return false; |
| 9507 | } |
| 9508 | |
| 9509 | // Determine if a struct is eligible for passing according to the floating |
| 9510 | // point calling convention (i.e., when flattened it contains a single fp |
| 9511 | // value, fp+fp, or int+fp of appropriate size). If so, NeededArgFPRs and |
| 9512 | // NeededArgGPRs are incremented appropriately. |
| 9513 | bool RISCVABIInfo::detectFPCCEligibleStruct(QualType Ty, llvm::Type *&Field1Ty, |
| 9514 | CharUnits &Field1Off, |
| 9515 | llvm::Type *&Field2Ty, |
| 9516 | CharUnits &Field2Off, |
| 9517 | int &NeededArgGPRs, |
| 9518 | int &NeededArgFPRs) const { |
| 9519 | Field1Ty = nullptr; |
| 9520 | Field2Ty = nullptr; |
| 9521 | NeededArgGPRs = 0; |
| 9522 | NeededArgFPRs = 0; |
| 9523 | bool IsCandidate = detectFPCCEligibleStructHelper( |
| 9524 | Ty, CharUnits::Zero(), Field1Ty, Field1Off, Field2Ty, Field2Off); |
| 9525 | // Not really a candidate if we have a single int but no float. |
| 9526 | if (Field1Ty && !Field2Ty && !Field1Ty->isFloatingPointTy()) |
Sylvestre Ledru | fb190c8 | 2019-10-08 09:17:46 +0000 | [diff] [blame] | 9527 | return false; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9528 | if (!IsCandidate) |
| 9529 | return false; |
| 9530 | if (Field1Ty && Field1Ty->isFloatingPointTy()) |
| 9531 | NeededArgFPRs++; |
| 9532 | else if (Field1Ty) |
| 9533 | NeededArgGPRs++; |
| 9534 | if (Field2Ty && Field2Ty->isFloatingPointTy()) |
| 9535 | NeededArgFPRs++; |
| 9536 | else if (Field2Ty) |
| 9537 | NeededArgGPRs++; |
| 9538 | return IsCandidate; |
| 9539 | } |
| 9540 | |
| 9541 | // Call getCoerceAndExpand for the two-element flattened struct described by |
| 9542 | // Field1Ty, Field1Off, Field2Ty, Field2Off. This method will create an |
| 9543 | // appropriate coerceToType and unpaddedCoerceToType. |
| 9544 | ABIArgInfo RISCVABIInfo::coerceAndExpandFPCCEligibleStruct( |
| 9545 | llvm::Type *Field1Ty, CharUnits Field1Off, llvm::Type *Field2Ty, |
| 9546 | CharUnits Field2Off) const { |
| 9547 | SmallVector<llvm::Type *, 3> CoerceElts; |
| 9548 | SmallVector<llvm::Type *, 2> UnpaddedCoerceElts; |
| 9549 | if (!Field1Off.isZero()) |
| 9550 | CoerceElts.push_back(llvm::ArrayType::get( |
| 9551 | llvm::Type::getInt8Ty(getVMContext()), Field1Off.getQuantity())); |
| 9552 | |
| 9553 | CoerceElts.push_back(Field1Ty); |
| 9554 | UnpaddedCoerceElts.push_back(Field1Ty); |
| 9555 | |
| 9556 | if (!Field2Ty) { |
| 9557 | return ABIArgInfo::getCoerceAndExpand( |
| 9558 | llvm::StructType::get(getVMContext(), CoerceElts, !Field1Off.isZero()), |
| 9559 | UnpaddedCoerceElts[0]); |
| 9560 | } |
| 9561 | |
| 9562 | CharUnits Field2Align = |
| 9563 | CharUnits::fromQuantity(getDataLayout().getABITypeAlignment(Field2Ty)); |
| 9564 | CharUnits Field1Size = |
| 9565 | CharUnits::fromQuantity(getDataLayout().getTypeStoreSize(Field1Ty)); |
| 9566 | CharUnits Field2OffNoPadNoPack = Field1Size.alignTo(Field2Align); |
| 9567 | |
| 9568 | CharUnits Padding = CharUnits::Zero(); |
| 9569 | if (Field2Off > Field2OffNoPadNoPack) |
| 9570 | Padding = Field2Off - Field2OffNoPadNoPack; |
| 9571 | else if (Field2Off != Field2Align && Field2Off > Field1Size) |
| 9572 | Padding = Field2Off - Field1Size; |
| 9573 | |
| 9574 | bool IsPacked = !Field2Off.isMultipleOf(Field2Align); |
| 9575 | |
| 9576 | if (!Padding.isZero()) |
| 9577 | CoerceElts.push_back(llvm::ArrayType::get( |
| 9578 | llvm::Type::getInt8Ty(getVMContext()), Padding.getQuantity())); |
| 9579 | |
| 9580 | CoerceElts.push_back(Field2Ty); |
| 9581 | UnpaddedCoerceElts.push_back(Field2Ty); |
| 9582 | |
| 9583 | auto CoerceToType = |
| 9584 | llvm::StructType::get(getVMContext(), CoerceElts, IsPacked); |
| 9585 | auto UnpaddedCoerceToType = |
| 9586 | llvm::StructType::get(getVMContext(), UnpaddedCoerceElts, IsPacked); |
| 9587 | |
| 9588 | return ABIArgInfo::getCoerceAndExpand(CoerceToType, UnpaddedCoerceToType); |
| 9589 | } |
| 9590 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9591 | ABIArgInfo RISCVABIInfo::classifyArgumentType(QualType Ty, bool IsFixed, |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9592 | int &ArgGPRsLeft, |
| 9593 | int &ArgFPRsLeft) const { |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9594 | assert(ArgGPRsLeft <= NumArgGPRs && "Arg GPR tracking underflow"); |
| 9595 | Ty = useFirstFieldIfTransparentUnion(Ty); |
| 9596 | |
| 9597 | // Structures with either a non-trivial destructor or a non-trivial |
| 9598 | // copy constructor are always passed indirectly. |
| 9599 | if (CGCXXABI::RecordArgABI RAA = getRecordArgABI(Ty, getCXXABI())) { |
| 9600 | if (ArgGPRsLeft) |
| 9601 | ArgGPRsLeft -= 1; |
| 9602 | return getNaturalAlignIndirect(Ty, /*ByVal=*/RAA == |
| 9603 | CGCXXABI::RAA_DirectInMemory); |
| 9604 | } |
| 9605 | |
| 9606 | // Ignore empty structs/unions. |
| 9607 | if (isEmptyRecord(getContext(), Ty, true)) |
| 9608 | return ABIArgInfo::getIgnore(); |
| 9609 | |
| 9610 | uint64_t Size = getContext().getTypeSize(Ty); |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9611 | |
| 9612 | // Pass floating point values via FPRs if possible. |
| 9613 | if (IsFixed && Ty->isFloatingType() && FLen >= Size && ArgFPRsLeft) { |
| 9614 | ArgFPRsLeft--; |
| 9615 | return ABIArgInfo::getDirect(); |
| 9616 | } |
| 9617 | |
| 9618 | // Complex types for the hard float ABI must be passed direct rather than |
| 9619 | // using CoerceAndExpand. |
| 9620 | if (IsFixed && Ty->isComplexType() && FLen && ArgFPRsLeft >= 2) { |
Simon Pilgrim | 7e38f0c | 2019-10-07 16:42:25 +0000 | [diff] [blame] | 9621 | QualType EltTy = Ty->castAs<ComplexType>()->getElementType(); |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9622 | if (getContext().getTypeSize(EltTy) <= FLen) { |
| 9623 | ArgFPRsLeft -= 2; |
| 9624 | return ABIArgInfo::getDirect(); |
| 9625 | } |
| 9626 | } |
| 9627 | |
| 9628 | if (IsFixed && FLen && Ty->isStructureOrClassType()) { |
| 9629 | llvm::Type *Field1Ty = nullptr; |
| 9630 | llvm::Type *Field2Ty = nullptr; |
| 9631 | CharUnits Field1Off = CharUnits::Zero(); |
| 9632 | CharUnits Field2Off = CharUnits::Zero(); |
| 9633 | int NeededArgGPRs; |
| 9634 | int NeededArgFPRs; |
| 9635 | bool IsCandidate = |
| 9636 | detectFPCCEligibleStruct(Ty, Field1Ty, Field1Off, Field2Ty, Field2Off, |
| 9637 | NeededArgGPRs, NeededArgFPRs); |
| 9638 | if (IsCandidate && NeededArgGPRs <= ArgGPRsLeft && |
| 9639 | NeededArgFPRs <= ArgFPRsLeft) { |
| 9640 | ArgGPRsLeft -= NeededArgGPRs; |
| 9641 | ArgFPRsLeft -= NeededArgFPRs; |
| 9642 | return coerceAndExpandFPCCEligibleStruct(Field1Ty, Field1Off, Field2Ty, |
| 9643 | Field2Off); |
| 9644 | } |
| 9645 | } |
| 9646 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9647 | uint64_t NeededAlign = getContext().getTypeAlign(Ty); |
| 9648 | bool MustUseStack = false; |
| 9649 | // Determine the number of GPRs needed to pass the current argument |
| 9650 | // according to the ABI. 2*XLen-aligned varargs are passed in "aligned" |
| 9651 | // register pairs, so may consume 3 registers. |
| 9652 | int NeededArgGPRs = 1; |
| 9653 | if (!IsFixed && NeededAlign == 2 * XLen) |
| 9654 | NeededArgGPRs = 2 + (ArgGPRsLeft % 2); |
| 9655 | else if (Size > XLen && Size <= 2 * XLen) |
| 9656 | NeededArgGPRs = 2; |
| 9657 | |
| 9658 | if (NeededArgGPRs > ArgGPRsLeft) { |
| 9659 | MustUseStack = true; |
| 9660 | NeededArgGPRs = ArgGPRsLeft; |
| 9661 | } |
| 9662 | |
| 9663 | ArgGPRsLeft -= NeededArgGPRs; |
| 9664 | |
| 9665 | if (!isAggregateTypeForABI(Ty) && !Ty->isVectorType()) { |
| 9666 | // Treat an enum type as its underlying type. |
| 9667 | if (const EnumType *EnumTy = Ty->getAs<EnumType>()) |
| 9668 | Ty = EnumTy->getDecl()->getIntegerType(); |
| 9669 | |
| 9670 | // All integral types are promoted to XLen width, unless passed on the |
| 9671 | // stack. |
| 9672 | if (Size < XLen && Ty->isIntegralOrEnumerationType() && !MustUseStack) { |
| 9673 | return extendType(Ty); |
| 9674 | } |
| 9675 | |
| 9676 | return ABIArgInfo::getDirect(); |
| 9677 | } |
| 9678 | |
| 9679 | // Aggregates which are <= 2*XLen will be passed in registers if possible, |
| 9680 | // so coerce to integers. |
| 9681 | if (Size <= 2 * XLen) { |
| 9682 | unsigned Alignment = getContext().getTypeAlign(Ty); |
| 9683 | |
| 9684 | // Use a single XLen int if possible, 2*XLen if 2*XLen alignment is |
| 9685 | // required, and a 2-element XLen array if only XLen alignment is required. |
| 9686 | if (Size <= XLen) { |
| 9687 | return ABIArgInfo::getDirect( |
| 9688 | llvm::IntegerType::get(getVMContext(), XLen)); |
| 9689 | } else if (Alignment == 2 * XLen) { |
| 9690 | return ABIArgInfo::getDirect( |
| 9691 | llvm::IntegerType::get(getVMContext(), 2 * XLen)); |
| 9692 | } else { |
| 9693 | return ABIArgInfo::getDirect(llvm::ArrayType::get( |
| 9694 | llvm::IntegerType::get(getVMContext(), XLen), 2)); |
| 9695 | } |
| 9696 | } |
| 9697 | return getNaturalAlignIndirect(Ty, /*ByVal=*/false); |
| 9698 | } |
| 9699 | |
| 9700 | ABIArgInfo RISCVABIInfo::classifyReturnType(QualType RetTy) const { |
| 9701 | if (RetTy->isVoidType()) |
| 9702 | return ABIArgInfo::getIgnore(); |
| 9703 | |
| 9704 | int ArgGPRsLeft = 2; |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9705 | int ArgFPRsLeft = FLen ? 2 : 0; |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9706 | |
| 9707 | // The rules for return and argument types are the same, so defer to |
| 9708 | // classifyArgumentType. |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9709 | return classifyArgumentType(RetTy, /*IsFixed=*/true, ArgGPRsLeft, |
| 9710 | ArgFPRsLeft); |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9711 | } |
| 9712 | |
| 9713 | Address RISCVABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, |
| 9714 | QualType Ty) const { |
| 9715 | CharUnits SlotSize = CharUnits::fromQuantity(XLen / 8); |
| 9716 | |
| 9717 | // Empty records are ignored for parameter passing purposes. |
| 9718 | if (isEmptyRecord(getContext(), Ty, true)) { |
| 9719 | Address Addr(CGF.Builder.CreateLoad(VAListAddr), SlotSize); |
| 9720 | Addr = CGF.Builder.CreateElementBitCast(Addr, CGF.ConvertTypeForMem(Ty)); |
| 9721 | return Addr; |
| 9722 | } |
| 9723 | |
| 9724 | std::pair<CharUnits, CharUnits> SizeAndAlign = |
| 9725 | getContext().getTypeInfoInChars(Ty); |
| 9726 | |
| 9727 | // Arguments bigger than 2*Xlen bytes are passed indirectly. |
| 9728 | bool IsIndirect = SizeAndAlign.first > 2 * SlotSize; |
| 9729 | |
| 9730 | return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, SizeAndAlign, |
| 9731 | SlotSize, /*AllowHigherAlign=*/true); |
| 9732 | } |
| 9733 | |
| 9734 | ABIArgInfo RISCVABIInfo::extendType(QualType Ty) const { |
| 9735 | int TySize = getContext().getTypeSize(Ty); |
| 9736 | // RV64 ABI requires unsigned 32 bit integers to be sign extended. |
| 9737 | if (XLen == 64 && Ty->isUnsignedIntegerOrEnumerationType() && TySize == 32) |
| 9738 | return ABIArgInfo::getSignExtend(Ty); |
| 9739 | return ABIArgInfo::getExtend(Ty); |
| 9740 | } |
| 9741 | |
| 9742 | namespace { |
| 9743 | class RISCVTargetCodeGenInfo : public TargetCodeGenInfo { |
| 9744 | public: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9745 | RISCVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT, unsigned XLen, |
| 9746 | unsigned FLen) |
| 9747 | : TargetCodeGenInfo(new RISCVABIInfo(CGT, XLen, FLen)) {} |
Ana Pazos | 1eee1b7 | 2018-07-26 17:37:45 +0000 | [diff] [blame] | 9748 | |
| 9749 | void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV, |
| 9750 | CodeGen::CodeGenModule &CGM) const override { |
| 9751 | const auto *FD = dyn_cast_or_null<FunctionDecl>(D); |
| 9752 | if (!FD) return; |
| 9753 | |
| 9754 | const auto *Attr = FD->getAttr<RISCVInterruptAttr>(); |
| 9755 | if (!Attr) |
| 9756 | return; |
| 9757 | |
| 9758 | const char *Kind; |
| 9759 | switch (Attr->getInterrupt()) { |
| 9760 | case RISCVInterruptAttr::user: Kind = "user"; break; |
| 9761 | case RISCVInterruptAttr::supervisor: Kind = "supervisor"; break; |
| 9762 | case RISCVInterruptAttr::machine: Kind = "machine"; break; |
| 9763 | } |
| 9764 | |
| 9765 | auto *Fn = cast<llvm::Function>(GV); |
| 9766 | |
| 9767 | Fn->addFnAttr("interrupt", Kind); |
| 9768 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9769 | }; |
| 9770 | } // namespace |
Robert Lytton | 844aeeb | 2014-05-02 09:33:20 +0000 | [diff] [blame] | 9771 | |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9772 | //===----------------------------------------------------------------------===// |
| 9773 | // Driver code |
| 9774 | //===----------------------------------------------------------------------===// |
| 9775 | |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9776 | bool CodeGenModule::supportsCOMDAT() const { |
Xinliang David Li | 865cfdd | 2016-05-25 17:25:57 +0000 | [diff] [blame] | 9777 | return getTriple().supportsCOMDAT(); |
Rafael Espindola | 9f83473 | 2014-09-19 01:54:22 +0000 | [diff] [blame] | 9778 | } |
| 9779 | |
Chris Lattner | 2b03797 | 2010-07-29 02:01:43 +0000 | [diff] [blame] | 9780 | const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() { |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9781 | if (TheTargetCodeGenInfo) |
| 9782 | return *TheTargetCodeGenInfo; |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9783 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9784 | // Helper to set the unique_ptr while still keeping the return value. |
| 9785 | auto SetCGInfo = [&](TargetCodeGenInfo *P) -> const TargetCodeGenInfo & { |
| 9786 | this->TheTargetCodeGenInfo.reset(P); |
| 9787 | return *P; |
| 9788 | }; |
| 9789 | |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 9790 | const llvm::Triple &Triple = getTarget().getTriple(); |
Daniel Dunbar | 4016518 | 2009-08-24 09:10:05 +0000 | [diff] [blame] | 9791 | switch (Triple.getArch()) { |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9792 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9793 | return SetCGInfo(new DefaultTargetCodeGenInfo(Types)); |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9794 | |
Derek Schuff | 09338a2 | 2012-09-06 17:37:28 +0000 | [diff] [blame] | 9795 | case llvm::Triple::le32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9796 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
John McCall | 943fae9 | 2010-05-27 06:19:26 +0000 | [diff] [blame] | 9797 | case llvm::Triple::mips: |
| 9798 | case llvm::Triple::mipsel: |
Petar Jovanovic | 26a4a40 | 2015-07-08 13:07:31 +0000 | [diff] [blame] | 9799 | if (Triple.getOS() == llvm::Triple::NaCl) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9800 | return SetCGInfo(new PNaClTargetCodeGenInfo(Types)); |
| 9801 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, true)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9802 | |
Akira Hatanaka | ec11b4f | 2011-09-20 18:30:57 +0000 | [diff] [blame] | 9803 | case llvm::Triple::mips64: |
| 9804 | case llvm::Triple::mips64el: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9805 | return SetCGInfo(new MIPSTargetCodeGenInfo(Types, false)); |
Akira Hatanaka | c4baedd | 2013-11-11 22:10:46 +0000 | [diff] [blame] | 9806 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 9807 | case llvm::Triple::avr: |
| 9808 | return SetCGInfo(new AVRTargetCodeGenInfo(Types)); |
| 9809 | |
Tim Northover | 25e8a67 | 2014-05-24 12:51:25 +0000 | [diff] [blame] | 9810 | case llvm::Triple::aarch64: |
Tim Northover | 44e5879 | 2018-09-18 10:34:39 +0100 | [diff] [blame] | 9811 | case llvm::Triple::aarch64_32: |
Tim Northover | 40956e6 | 2014-07-23 12:32:58 +0000 | [diff] [blame] | 9812 | case llvm::Triple::aarch64_be: { |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9813 | AArch64ABIInfo::ABIKind Kind = AArch64ABIInfo::AAPCS; |
Alp Toker | 4925ba7 | 2014-06-07 23:30:42 +0000 | [diff] [blame] | 9814 | if (getTarget().getABI() == "darwinpcs") |
Tim Northover | 573cbee | 2014-05-24 12:52:07 +0000 | [diff] [blame] | 9815 | Kind = AArch64ABIInfo::DarwinPCS; |
Martin Storsjo | 502de22 | 2017-07-13 17:59:14 +0000 | [diff] [blame] | 9816 | else if (Triple.isOSWindows()) |
Martin Storsjo | 1c8af27 | 2017-07-20 05:47:06 +0000 | [diff] [blame] | 9817 | return SetCGInfo( |
| 9818 | new WindowsAArch64TargetCodeGenInfo(Types, AArch64ABIInfo::Win64)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9819 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9820 | return SetCGInfo(new AArch64TargetCodeGenInfo(Types, Kind)); |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9821 | } |
| 9822 | |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9823 | case llvm::Triple::wasm32: |
| 9824 | case llvm::Triple::wasm64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9825 | return SetCGInfo(new WebAssemblyTargetCodeGenInfo(Types)); |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9826 | |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9827 | case llvm::Triple::arm: |
Christian Pirker | f01cd6f | 2014-03-28 14:40:46 +0000 | [diff] [blame] | 9828 | case llvm::Triple::armeb: |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9829 | case llvm::Triple::thumb: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9830 | case llvm::Triple::thumbeb: { |
| 9831 | if (Triple.getOS() == llvm::Triple::Win32) { |
| 9832 | return SetCGInfo( |
| 9833 | new WindowsARMTargetCodeGenInfo(Types, ARMABIInfo::AAPCS_VFP)); |
Sandeep Patel | 45df3dd | 2011-04-05 00:23:47 +0000 | [diff] [blame] | 9834 | } |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9835 | |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9836 | ARMABIInfo::ABIKind Kind = ARMABIInfo::AAPCS; |
| 9837 | StringRef ABIStr = getTarget().getABI(); |
| 9838 | if (ABIStr == "apcs-gnu") |
| 9839 | Kind = ARMABIInfo::APCS; |
| 9840 | else if (ABIStr == "aapcs16") |
| 9841 | Kind = ARMABIInfo::AAPCS16_VFP; |
| 9842 | else if (CodeGenOpts.FloatABI == "hard" || |
| 9843 | (CodeGenOpts.FloatABI != "soft" && |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9844 | (Triple.getEnvironment() == llvm::Triple::GNUEABIHF || |
Rafael Espindola | 0fa6680 | 2016-06-24 21:35:06 +0000 | [diff] [blame] | 9845 | Triple.getEnvironment() == llvm::Triple::MuslEABIHF || |
Oleg Ranevskyy | 7232f66 | 2016-05-13 14:45:57 +0000 | [diff] [blame] | 9846 | Triple.getEnvironment() == llvm::Triple::EABIHF))) |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9847 | Kind = ARMABIInfo::AAPCS_VFP; |
| 9848 | |
| 9849 | return SetCGInfo(new ARMTargetCodeGenInfo(Types, Kind)); |
| 9850 | } |
| 9851 | |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9852 | case llvm::Triple::ppc: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9853 | return SetCGInfo( |
Justin Hibbits | 3dac214 | 2019-09-05 13:38:46 +0000 | [diff] [blame] | 9854 | new PPC32TargetCodeGenInfo(Types, CodeGenOpts.FloatABI == "soft" || |
| 9855 | getTarget().hasFeature("spe"))); |
Roman Divacky | d966e72 | 2012-05-09 18:22:46 +0000 | [diff] [blame] | 9856 | case llvm::Triple::ppc64: |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9857 | if (Triple.isOSBinFormatELF()) { |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9858 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9859 | if (getTarget().getABI() == "elfv2") |
| 9860 | Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9861 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9862 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9863 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9864 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9865 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9866 | } else |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9867 | return SetCGInfo(new PPC64TargetCodeGenInfo(Types)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9868 | case llvm::Triple::ppc64le: { |
Bill Schmidt | 778d387 | 2013-07-26 01:36:11 +0000 | [diff] [blame] | 9869 | assert(Triple.isOSBinFormatELF() && "PPC64 LE non-ELF not supported!"); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9870 | PPC64_SVR4_ABIInfo::ABIKind Kind = PPC64_SVR4_ABIInfo::ELFv2; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9871 | if (getTarget().getABI() == "elfv1" || getTarget().getABI() == "elfv1-qpx") |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9872 | Kind = PPC64_SVR4_ABIInfo::ELFv1; |
Hal Finkel | 0d0a1a5 | 2015-03-11 19:14:15 +0000 | [diff] [blame] | 9873 | bool HasQPX = getTarget().getABI() == "elfv1-qpx"; |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9874 | bool IsSoftFloat = CodeGenOpts.FloatABI == "soft"; |
Ulrich Weigand | 8afad61 | 2014-07-28 13:17:52 +0000 | [diff] [blame] | 9875 | |
Hal Finkel | 415c2a3 | 2016-10-02 02:10:45 +0000 | [diff] [blame] | 9876 | return SetCGInfo(new PPC64_SVR4_TargetCodeGenInfo(Types, Kind, HasQPX, |
| 9877 | IsSoftFloat)); |
Ulrich Weigand | b712237 | 2014-07-21 00:48:09 +0000 | [diff] [blame] | 9878 | } |
John McCall | ea8d8bb | 2010-03-11 00:10:12 +0000 | [diff] [blame] | 9879 | |
Peter Collingbourne | c947aae | 2012-05-20 23:28:41 +0000 | [diff] [blame] | 9880 | case llvm::Triple::nvptx: |
| 9881 | case llvm::Triple::nvptx64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9882 | return SetCGInfo(new NVPTXTargetCodeGenInfo(Types)); |
Justin Holewinski | bd4a3c0 | 2011-04-22 11:10:38 +0000 | [diff] [blame] | 9883 | |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 9884 | case llvm::Triple::msp430: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9885 | return SetCGInfo(new MSP430TargetCodeGenInfo(Types)); |
Daniel Dunbar | d59655c | 2009-09-12 00:59:49 +0000 | [diff] [blame] | 9886 | |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9887 | case llvm::Triple::riscv32: |
Alex Bradbury | e078967a | 2019-07-18 18:29:59 +0000 | [diff] [blame] | 9888 | case llvm::Triple::riscv64: { |
| 9889 | StringRef ABIStr = getTarget().getABI(); |
| 9890 | unsigned XLen = getTarget().getPointerWidth(0); |
| 9891 | unsigned ABIFLen = 0; |
| 9892 | if (ABIStr.endswith("f")) |
| 9893 | ABIFLen = 32; |
| 9894 | else if (ABIStr.endswith("d")) |
| 9895 | ABIFLen = 64; |
| 9896 | return SetCGInfo(new RISCVTargetCodeGenInfo(Types, XLen, ABIFLen)); |
| 9897 | } |
Alex Bradbury | 8cbdd48 | 2018-01-15 17:54:52 +0000 | [diff] [blame] | 9898 | |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9899 | case llvm::Triple::systemz: { |
| 9900 | bool HasVector = getTarget().getABI() == "vector"; |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9901 | return SetCGInfo(new SystemZTargetCodeGenInfo(Types, HasVector)); |
Ulrich Weigand | 66ff51b | 2015-05-05 19:35:52 +0000 | [diff] [blame] | 9902 | } |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 9903 | |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9904 | case llvm::Triple::tce: |
Pekka Jaaskelainen | 6735448 | 2016-11-16 15:22:31 +0000 | [diff] [blame] | 9905 | case llvm::Triple::tcele: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9906 | return SetCGInfo(new TCETargetCodeGenInfo(Types)); |
Peter Collingbourne | adcf7c9 | 2011-10-13 16:24:41 +0000 | [diff] [blame] | 9907 | |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9908 | case llvm::Triple::x86: { |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9909 | bool IsDarwinVectorABI = Triple.isOSDarwin(); |
Michael Kuperstein | dc74520 | 2015-10-19 07:52:25 +0000 | [diff] [blame] | 9910 | bool RetSmallStructInRegABI = |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9911 | X86_32TargetCodeGenInfo::isStructReturnInRegABI(Triple, CodeGenOpts); |
Saleem Abdulrasool | ec5c624 | 2014-11-23 02:16:24 +0000 | [diff] [blame] | 9912 | bool IsWin32FloatStructABI = Triple.isOSWindows() && !Triple.isOSCygMing(); |
Daniel Dunbar | 14ad22f | 2011-04-19 21:43:27 +0000 | [diff] [blame] | 9913 | |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9914 | if (Triple.getOS() == llvm::Triple::Win32) { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9915 | return SetCGInfo(new WinX86_32TargetCodeGenInfo( |
| 9916 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9917 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters)); |
John McCall | 1fe2a8c | 2013-06-18 02:46:29 +0000 | [diff] [blame] | 9918 | } else { |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9919 | return SetCGInfo(new X86_32TargetCodeGenInfo( |
| 9920 | Types, IsDarwinVectorABI, RetSmallStructInRegABI, |
| 9921 | IsWin32FloatStructABI, CodeGenOpts.NumRegisterParameters, |
Hans Wennborg | d874c05 | 2019-06-19 11:34:08 +0000 | [diff] [blame] | 9922 | CodeGenOpts.FloatABI == "soft")); |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9923 | } |
Eli Friedman | 3346582 | 2011-07-08 23:31:17 +0000 | [diff] [blame] | 9924 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9925 | |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9926 | case llvm::Triple::x86_64: { |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9927 | StringRef ABI = getTarget().getABI(); |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9928 | X86AVXABILevel AVXLevel = |
| 9929 | (ABI == "avx512" |
| 9930 | ? X86AVXABILevel::AVX512 |
| 9931 | : ABI == "avx" ? X86AVXABILevel::AVX : X86AVXABILevel::None); |
Ahmed Bougacha | d39a415 | 2015-06-22 21:30:39 +0000 | [diff] [blame] | 9932 | |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9933 | switch (Triple.getOS()) { |
| 9934 | case llvm::Triple::Win32: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9935 | return SetCGInfo(new WinX86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9936 | default: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9937 | return SetCGInfo(new X86_64TargetCodeGenInfo(Types, AVXLevel)); |
Chris Lattner | 04dc957 | 2010-08-31 16:44:54 +0000 | [diff] [blame] | 9938 | } |
Daniel Dunbar | e3532f8 | 2009-08-24 08:52:16 +0000 | [diff] [blame] | 9939 | } |
Tony Linthicum | 76329bf | 2011-12-12 21:14:55 +0000 | [diff] [blame] | 9940 | case llvm::Triple::hexagon: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9941 | return SetCGInfo(new HexagonTargetCodeGenInfo(Types)); |
Jacques Pienaar | d964cc2 | 2016-03-28 21:02:54 +0000 | [diff] [blame] | 9942 | case llvm::Triple::lanai: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9943 | return SetCGInfo(new LanaiTargetCodeGenInfo(Types)); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 9944 | case llvm::Triple::r600: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9945 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Tom Stellard | d8e38a3 | 2015-01-06 20:34:47 +0000 | [diff] [blame] | 9946 | case llvm::Triple::amdgcn: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9947 | return SetCGInfo(new AMDGPUTargetCodeGenInfo(Types)); |
Chris Dewhurst | 7e7ee96 | 2016-06-08 14:47:25 +0000 | [diff] [blame] | 9948 | case llvm::Triple::sparc: |
| 9949 | return SetCGInfo(new SparcV8TargetCodeGenInfo(Types)); |
Jakob Stoklund Olesen | d28ab7e | 2013-05-27 21:48:25 +0000 | [diff] [blame] | 9950 | case llvm::Triple::sparcv9: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9951 | return SetCGInfo(new SparcV9TargetCodeGenInfo(Types)); |
Robert Lytton | 0e07649 | 2013-08-13 09:43:10 +0000 | [diff] [blame] | 9952 | case llvm::Triple::xcore: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9953 | return SetCGInfo(new XCoreTargetCodeGenInfo(Types)); |
Tatyana Krasnukha | f8c264e | 2018-11-27 19:52:10 +0000 | [diff] [blame] | 9954 | case llvm::Triple::arc: |
| 9955 | return SetCGInfo(new ARCTargetCodeGenInfo(Types)); |
Xiuli Pan | 972bea8 | 2016-03-24 03:57:17 +0000 | [diff] [blame] | 9956 | case llvm::Triple::spir: |
| 9957 | case llvm::Triple::spir64: |
Reid Kleckner | 9305fd1 | 2016-04-13 23:37:17 +0000 | [diff] [blame] | 9958 | return SetCGInfo(new SPIRTargetCodeGenInfo(Types)); |
Eli Friedman | bfd5add | 2011-12-02 00:11:43 +0000 | [diff] [blame] | 9959 | } |
Anton Korobeynikov | 244360d | 2009-06-05 22:08:42 +0000 | [diff] [blame] | 9960 | } |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 9961 | |
| 9962 | /// Create an OpenCL kernel for an enqueued block. |
| 9963 | /// |
| 9964 | /// The kernel has the same function type as the block invoke function. Its |
| 9965 | /// name is the name of the block invoke function postfixed with "_kernel". |
| 9966 | /// It simply calls the block invoke function then returns. |
| 9967 | llvm::Function * |
| 9968 | TargetCodeGenInfo::createEnqueuedBlockKernel(CodeGenFunction &CGF, |
| 9969 | llvm::Function *Invoke, |
| 9970 | llvm::Value *BlockLiteral) const { |
| 9971 | auto *InvokeFT = Invoke->getFunctionType(); |
| 9972 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 9973 | for (auto &P : InvokeFT->params()) |
| 9974 | ArgTys.push_back(P); |
| 9975 | auto &C = CGF.getLLVMContext(); |
| 9976 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 9977 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 9978 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 9979 | &CGF.CGM.getModule()); |
| 9980 | auto IP = CGF.Builder.saveIP(); |
| 9981 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 9982 | auto &Builder = CGF.Builder; |
| 9983 | Builder.SetInsertPoint(BB); |
| 9984 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 9985 | for (auto &A : F->args()) |
| 9986 | Args.push_back(&A); |
| 9987 | Builder.CreateCall(Invoke, Args); |
| 9988 | Builder.CreateRetVoid(); |
| 9989 | Builder.restoreIP(IP); |
| 9990 | return F; |
| 9991 | } |
| 9992 | |
| 9993 | /// Create an OpenCL kernel for an enqueued block. |
| 9994 | /// |
| 9995 | /// The type of the first argument (the block literal) is the struct type |
| 9996 | /// of the block literal instead of a pointer type. The first argument |
| 9997 | /// (block literal) is passed directly by value to the kernel. The kernel |
| 9998 | /// allocates the same type of struct on stack and stores the block literal |
| 9999 | /// to it and passes its pointer to the block invoke function. The kernel |
| 10000 | /// has "enqueued-block" function attribute and kernel argument metadata. |
| 10001 | llvm::Function *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel( |
| 10002 | CodeGenFunction &CGF, llvm::Function *Invoke, |
| 10003 | llvm::Value *BlockLiteral) const { |
| 10004 | auto &Builder = CGF.Builder; |
| 10005 | auto &C = CGF.getLLVMContext(); |
| 10006 | |
| 10007 | auto *BlockTy = BlockLiteral->getType()->getPointerElementType(); |
| 10008 | auto *InvokeFT = Invoke->getFunctionType(); |
| 10009 | llvm::SmallVector<llvm::Type *, 2> ArgTys; |
| 10010 | llvm::SmallVector<llvm::Metadata *, 8> AddressQuals; |
| 10011 | llvm::SmallVector<llvm::Metadata *, 8> AccessQuals; |
| 10012 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames; |
| 10013 | llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames; |
| 10014 | llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals; |
| 10015 | llvm::SmallVector<llvm::Metadata *, 8> ArgNames; |
| 10016 | |
| 10017 | ArgTys.push_back(BlockTy); |
| 10018 | ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 10019 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0))); |
| 10020 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal")); |
| 10021 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 10022 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 10023 | ArgNames.push_back(llvm::MDString::get(C, "block_literal")); |
| 10024 | for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) { |
| 10025 | ArgTys.push_back(InvokeFT->getParamType(I)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 10026 | ArgTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 10027 | AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3))); |
| 10028 | AccessQuals.push_back(llvm::MDString::get(C, "none")); |
| 10029 | ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*")); |
| 10030 | ArgTypeQuals.push_back(llvm::MDString::get(C, "")); |
| 10031 | ArgNames.push_back( |
Yaxun Liu | 98f0c43 | 2017-10-14 12:51:52 +0000 | [diff] [blame] | 10032 | llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str())); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 10033 | } |
| 10034 | std::string Name = Invoke->getName().str() + "_kernel"; |
| 10035 | auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false); |
| 10036 | auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name, |
| 10037 | &CGF.CGM.getModule()); |
| 10038 | F->addFnAttr("enqueued-block"); |
| 10039 | auto IP = CGF.Builder.saveIP(); |
| 10040 | auto *BB = llvm::BasicBlock::Create(C, "entry", F); |
| 10041 | Builder.SetInsertPoint(BB); |
| 10042 | unsigned BlockAlign = CGF.CGM.getDataLayout().getPrefTypeAlignment(BlockTy); |
| 10043 | auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr); |
Guillaume Chatelet | ab11b91 | 2019-09-30 13:34:44 +0000 | [diff] [blame] | 10044 | BlockPtr->setAlignment(llvm::MaybeAlign(BlockAlign)); |
Yaxun Liu | c2a87a0 | 2017-10-14 12:23:50 +0000 | [diff] [blame] | 10045 | Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign); |
| 10046 | auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0)); |
| 10047 | llvm::SmallVector<llvm::Value *, 2> Args; |
| 10048 | Args.push_back(Cast); |
| 10049 | for (auto I = F->arg_begin() + 1, E = F->arg_end(); I != E; ++I) |
| 10050 | Args.push_back(I); |
| 10051 | Builder.CreateCall(Invoke, Args); |
| 10052 | Builder.CreateRetVoid(); |
| 10053 | Builder.restoreIP(IP); |
| 10054 | |
| 10055 | F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals)); |
| 10056 | F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals)); |
| 10057 | F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames)); |
| 10058 | F->setMetadata("kernel_arg_base_type", |
| 10059 | llvm::MDNode::get(C, ArgBaseTypeNames)); |
| 10060 | F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals)); |
| 10061 | if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata) |
| 10062 | F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames)); |
| 10063 | |
| 10064 | return F; |
| 10065 | } |